Eclipse SUMO - Simulation of Urban MObility
netgen_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Main for NETGENERATE
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #ifdef HAVE_VERSION_H
28 #include <version.h>
29 #endif
30 
31 #include <iostream>
32 #include <fstream>
33 #include <string>
34 #include <ctime>
35 #include <netgen/NGNet.h>
37 #include <netgen/NGFrame.h>
38 #include <netbuild/NBNetBuilder.h>
39 #include <netbuild/NBFrame.h>
40 #include <netwrite/NWFrame.h>
43 #include <utils/options/Option.h>
48 #include <utils/common/ToString.h>
51 #include <utils/xml/XMLSubSys.h>
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 void
61  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
62  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
63  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
64  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
65 
66  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
67 
68  // insert options sub-topics
69  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
70  oc.addOptionSubTopic("Grid Network");
71  oc.addOptionSubTopic("Spider Network");
72  oc.addOptionSubTopic("Random Network");
73  oc.addOptionSubTopic("Output");
74  oc.addOptionSubTopic("Processing");
75  oc.addOptionSubTopic("Building Defaults");
76  oc.addOptionSubTopic("TLS Building");
77  oc.addOptionSubTopic("Edge Removal");
78  oc.addOptionSubTopic("Unregulated Nodes");
79  oc.addOptionSubTopic("Junctions");
80  oc.addOptionSubTopic("Pedestrian");
81  oc.addOptionSubTopic("Bicycle");
82  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
83 
87  oc.doRegister("default-junction-type", 'j', new Option_String());
88  oc.addSynonyme("default-junction-type", "junctions");
89  oc.addDescription("default-junction-type", "Building Defaults", "[traffic_light|priority|right_before_left|traffic_light_right_on_red|priority_stop|allway_stop|...] Determines junction type (see wiki/Networks/PlainXML#Node_types)");
91 }
92 
93 
94 bool
96  bool ok = NGFrame::checkOptions();
97  ok &= NBFrame::checkOptions();
98  ok &= NWFrame::checkOptions();
100  return ok;
101 }
102 
103 
104 NGNet*
107 
108  // spider-net
109  if (oc.getBool("spider")) {
110  // check values
111  bool hadError = false;
112  if (oc.getInt("spider.arm-number") < 3) {
113  WRITE_ERROR("Spider networks need at least 3 arms.");
114  hadError = true;
115  }
116  if (oc.getInt("spider.circle-number") < 1) {
117  WRITE_ERROR("Spider networks need at least one circle.");
118  hadError = true;
119  }
120  if (oc.getFloat("spider.space-radius") < 10) {
121  WRITE_ERROR("The radius of spider networks must be at least 10m.");
122  hadError = true;
123  }
124  if (hadError) {
125  throw ProcessError();
126  }
127  // build if everything's ok
128  NGNet* net = new NGNet(nb);
129  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
130  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
131  return net;
132  }
133  // grid-net
134  if (oc.getBool("grid")) {
135  // get options
136  int xNo = oc.getInt("grid.x-number");
137  int yNo = oc.getInt("grid.y-number");
138  double xLength = oc.getFloat("grid.x-length");
139  double yLength = oc.getFloat("grid.y-length");
140  double attachLength = oc.getFloat("grid.attach-length");
141  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
142  xNo = oc.getInt("grid.number");
143  }
144  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
145  yNo = oc.getInt("grid.number");
146  }
147  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
148  xLength = oc.getFloat("grid.length");
149  }
150  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
151  yLength = oc.getFloat("grid.length");
152  }
153  // check values
154  bool hadError = false;
155  if (attachLength == 0 && (xNo < 2 || yNo < 2)) {
156  WRITE_ERROR("The number of nodes must be at least 2 in both directions.");
157  hadError = true;
158  }
159  if (xLength < 10. || yLength < 10.) {
160  WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
161  hadError = true;
162  }
163  if (attachLength != 0.0 && attachLength < 10.) {
164  WRITE_ERROR("The length of attached streets must be at least 10m.");
165  hadError = true;
166  }
167  if (hadError) {
168  throw ProcessError();
169  }
170  // build if everything's ok
171  NGNet* net = new NGNet(nb);
172  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
173  return net;
174  }
175  // random net
176  RandomDistributor<int> neighborDist;
177  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
178  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
179  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
180  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
181  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
182  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
183  NGNet* net = new NGNet(nb);
184  NGRandomNetBuilder randomNet(*net,
185  DEG2RAD(oc.getFloat("rand.min-angle")),
186  oc.getFloat("rand.min-distance"),
187  oc.getFloat("rand.max-distance"),
188  oc.getFloat("rand.connectivity"),
189  oc.getInt("rand.num-tries"),
190  neighborDist);
191  randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
192  return net;
193 }
194 
195 
196 
197 int
198 main(int argc, char** argv) {
200  // give some application descriptions
201  oc.setApplicationDescription("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO.");
202  oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
203  int ret = 0;
204  try {
205  // initialise the application system (messaging, xml, options)
206  XMLSubSys::init();
207  fillOptions();
208  OptionsIO::setArgs(argc, argv);
210  if (oc.processMetaOptions(argc < 2)) {
212  return 0;
213  }
214  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
216  if (!checkOptions()) {
217  throw ProcessError();
218  }
220  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
221  Boundary(), Boundary());
223  NBNetBuilder nb;
224  nb.applyOptions(oc);
225  // build the netgen-network description
226  NGNet* net = buildNetwork(nb);
227  // ... and we have to do this...
228  oc.resetWritable();
229  // transfer to the netbuilding structures
230  net->toNB();
231  delete net;
232  // report generated structures
233  WRITE_MESSAGE(" Generation done;");
234  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
235  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
236  nb.compute(oc);
237  NWFrame::writeNetwork(oc, nb);
238  } catch (const ProcessError& e) {
239  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
240  WRITE_ERROR(e.what());
241  }
242  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
243  ret = 1;
244 #ifndef _DEBUG
245  } catch (const std::exception& e) {
246  if (std::string(e.what()) != std::string("")) {
247  WRITE_ERROR(e.what());
248  }
249  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
250  ret = 1;
251  } catch (...) {
252  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
253  ret = 1;
254 #endif
255  }
257  if (ret == 0) {
258  std::cout << "Success." << std::endl;
259  }
260  return ret;
261 }
262 
263 
264 
265 /****************************************************************************/
266 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:43
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:126
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:577
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void resetWritable()
Resets all options to be writeable.
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:65
bool checkOptions()
Definition: netgen_main.cpp:95
int main(int argc, char **argv)
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:231
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
NGNet * buildNetwork(NBNetBuilder &nb)
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
void fillOptions()
Definition: netgen_main.cpp:59
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:40
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
int size() const
Returns the number of nodes stored in this container.
Definition: NBNodeCont.h:264
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:151
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NGFrame.cpp:206
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool checkOptions()
checks shared options and sets StdDefs
#define DEG2RAD(x)
Definition: GeomHelper.h:38
The class storing the generated network.
Definition: NGNet.h:50
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
#define VERSION_STRING
Definition: config.h:207
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition: NWFrame.cpp:175
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:156
Instance responsible for building networks.
Definition: NBNetBuilder.h:110
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:160
int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:306
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
A storage for options typed value containers)
Definition: OptionsCont.h:90
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:72
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:95
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:40
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:49
A class that builds random network using an algorithm by Markus Hartinger.
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool mayAddOrRemove=true)
Performs the network building steps.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:50
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
void createNet(int numNodes, bool gridMode)
Builds a NGNet using the set values.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.