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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // Main for NETGENERATE
22 /****************************************************************************/
23 #include <config.h>
24 
25 #ifdef HAVE_VERSION_H
26 #include <version.h>
27 #endif
28 
29 #include <iostream>
30 #include <fstream>
31 #include <string>
32 #include <ctime>
33 #include <netgen/NGNet.h>
35 #include <netgen/NGFrame.h>
36 #include <netbuild/NBNetBuilder.h>
37 #include <netbuild/NBFrame.h>
38 #include <netwrite/NWFrame.h>
39 #include <netimport/NITypeLoader.h>
43 #include <utils/options/Option.h>
48 #include <utils/common/ToString.h>
51 #include <utils/xml/XMLSubSys.h>
53 
54 // ===========================================================================
55 // method declaration
56 // ===========================================================================
57 
58 void fillOptions();
59 bool checkOptions();
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
65 
66 void
69  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
70  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
71  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
72  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
73 
74  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
75 
76  // insert options sub-topics
77  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
78  oc.addOptionSubTopic("Grid Network");
79  oc.addOptionSubTopic("Spider Network");
80  oc.addOptionSubTopic("Random Network");
81  oc.addOptionSubTopic("Input");
82  oc.addOptionSubTopic("Output");
83  oc.addOptionSubTopic("Processing");
84  oc.addOptionSubTopic("Building Defaults");
85  oc.addOptionSubTopic("TLS Building");
86  oc.addOptionSubTopic("Edge Removal");
87  oc.addOptionSubTopic("Unregulated Nodes");
88  oc.addOptionSubTopic("Junctions");
89  oc.addOptionSubTopic("Pedestrian");
90  oc.addOptionSubTopic("Bicycle");
91  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
92 
96  oc.doRegister("default-junction-type", 'j', new Option_String());
97  oc.addSynonyme("default-junction-type", "junctions");
98  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)");
100 }
101 
102 
103 bool
105  bool ok = NGFrame::checkOptions();
106  ok &= NBFrame::checkOptions();
107  ok &= NWFrame::checkOptions();
109  return ok;
110 }
111 
112 
113 NGNet*
116 
117  const double laneWidth = oc.isDefault("default.lanewidth") ? SUMO_const_laneWidth : oc.getFloat("default.lanewidth");
118  double minLength = (oc.getInt("default.lanenumber") + oc.getInt("turn-lanes")) * 2 * laneWidth + oc.getFloat("default.junctions.radius") * 2 + POSITION_EPS;
119  // spider-net
120  if (oc.getBool("spider")) {
121  // check values
122  bool hadError = false;
123  if (oc.getInt("spider.arm-number") < 3) {
124  WRITE_ERROR("Spider networks need at least 3 arms.");
125  hadError = true;
126  }
127  if (oc.getInt("spider.arm-number") > 4 && oc.isDefault("spider.omit-center")) {
128  WRITE_WARNING("Spider networks with many arms should use option ommit-center");
129  }
130  if (oc.getInt("spider.circle-number") < 1) {
131  WRITE_ERROR("Spider networks need at least one circle.");
132  hadError = true;
133  }
134  minLength = MAX2(minLength, laneWidth * 2 * MAX2(oc.getInt("spider.arm-number"), 3) / (2 * M_PI));
135  if (oc.getFloat("spider.space-radius") < POSITION_EPS) {
136  WRITE_ERROR("The radius of spider networks must be at least " + toString(POSITION_EPS));
137  hadError = true;
138  } else if (oc.getFloat("spider.space-radius") < minLength) {
139  WRITE_WARNING("The radius of spider networks should be at least " + toString(minLength) + " for the given lanenumber, lanewidth and junction radius");
140  }
141  if (hadError) {
142  throw ProcessError();
143  }
144  // build if everything's ok
145  NGNet* net = new NGNet(nb);
146  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
147  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
148  return net;
149  }
150  // grid-net
151  if (oc.getBool("grid")) {
152  // get options
153  int xNo = oc.getInt("grid.x-number");
154  int yNo = oc.getInt("grid.y-number");
155  double xLength = oc.getFloat("grid.x-length");
156  double yLength = oc.getFloat("grid.y-length");
157  double xAttachLength = oc.getFloat("grid.x-attach-length");
158  double yAttachLength = oc.getFloat("grid.y-attach-length");
159  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
160  xNo = oc.getInt("grid.number");
161  }
162  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
163  yNo = oc.getInt("grid.number");
164  }
165  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
166  xLength = oc.getFloat("grid.length");
167  }
168  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
169  yLength = oc.getFloat("grid.length");
170  }
171  if (oc.isDefault("grid.x-attach-length") && !oc.isDefault("grid.attach-length")) {
172  xAttachLength = oc.getFloat("grid.attach-length");
173  }
174  if (oc.isDefault("grid.y-attach-length") && !oc.isDefault("grid.attach-length")) {
175  yAttachLength = oc.getFloat("grid.attach-length");
176  }
177  // check values
178  bool hadError = false;
179  if (xNo < 1 || yNo < 1 || (xAttachLength == 0 && yAttachLength == 0 && (xNo < 2 && yNo < 2))) {
180  WRITE_ERROR("The number of nodes must be positive and at least 2 in one direction if there are no attachments.");
181  hadError = true;
182  }
183  const double minAttachLength = minLength / 2 + POSITION_EPS / 2;
184  if (xLength < POSITION_EPS || yLength < POSITION_EPS) {
185  WRITE_ERROR("The distance between nodes must be at least " + toString(POSITION_EPS));
186  hadError = true;
187  } else if (xLength < minLength || yLength < minLength) {
188  WRITE_WARNING("The distance between nodes should be at least " + toString(minLength) + " for the given lanenumber, lanewidth and junction radius");
189  }
190  if (xAttachLength != 0.0 && xAttachLength < POSITION_EPS) {
191  WRITE_ERROR("The length of attached streets must be at least " + toString(POSITION_EPS));
192  hadError = true;
193  } else if (xAttachLength != 0.0 && xAttachLength < minAttachLength) {
194  WRITE_WARNING("The length of attached streets should be at least " + toString(minAttachLength) + " for the given lanenumber, lanewidth and junction radius");
195  } else if (yAttachLength != 0.0 && yAttachLength < POSITION_EPS) {
196  WRITE_ERROR("The length of attached streets must be at least " + toString(POSITION_EPS));
197  hadError = true;
198  } else if (yAttachLength != 0.0 && yAttachLength < minAttachLength) {
199  WRITE_WARNING("The length of attached streets should be at least " + toString(minAttachLength) + " for the given lanenumber, lanewidth and junction radius");
200  }
201  if (hadError) {
202  throw ProcessError();
203  }
204  // build if everything's ok
205  NGNet* net = new NGNet(nb);
206  net->createChequerBoard(xNo, yNo, xLength, yLength, xAttachLength, yAttachLength);
207  return net;
208  }
209  // random net
210  RandomDistributor<int> neighborDist;
211  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
212  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
213  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
214  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
215  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
216  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
217  NGNet* net = new NGNet(nb);
218  NGRandomNetBuilder randomNet(*net,
219  DEG2RAD(oc.getFloat("rand.min-angle")),
220  oc.getFloat("rand.min-distance"),
221  oc.getFloat("rand.max-distance"),
222  oc.getFloat("rand.connectivity"),
223  oc.getInt("rand.num-tries"),
224  neighborDist);
225  randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
226  return net;
227 }
228 
229 
230 
231 int
232 main(int argc, char** argv) {
234  // give some application descriptions
235  oc.setApplicationDescription("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO.");
236  oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
237  int ret = 0;
238  try {
239  // initialise the application system (messaging, xml, options)
240  XMLSubSys::init();
241  fillOptions();
242  OptionsIO::setArgs(argc, argv);
244  if (oc.processMetaOptions(argc < 2)) {
246  return 0;
247  }
248  XMLSubSys::setValidation(oc.getString("xml-validation"), "never", "never");
250  if (!checkOptions()) {
251  throw ProcessError();
252  }
254  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
255  Boundary(), Boundary());
257  NBNetBuilder nb;
258  nb.applyOptions(oc);
259  if (oc.isSet("type-files")) {
260  NIXMLTypesHandler* handler = new NIXMLTypesHandler(nb.getTypeCont());
261  NITypeLoader::load(handler, oc.getStringVector("type-files"), "types");
262  }
263  // build the netgen-network description
264  NGNet* net = buildNetwork(nb);
265  // ... and we have to do this...
266  oc.resetWritable();
267  // transfer to the netbuilding structures
268  net->toNB();
269  delete net;
270  // report generated structures
271  WRITE_MESSAGE(" Generation done;");
272  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
273  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
274  nb.compute(oc);
276  NWFrame::writeNetwork(oc, nb);
277  } catch (const ProcessError& e) {
278  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
279  WRITE_ERROR(e.what());
280  }
281  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
282  ret = 1;
283 #ifndef _DEBUG
284  } catch (const std::exception& e) {
285  if (std::string(e.what()) != std::string("")) {
286  WRITE_ERROR(e.what());
287  }
288  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
289  ret = 1;
290  } catch (...) {
291  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
292  ret = 1;
293 #endif
294  }
296  if (ret == 0) {
297  std::cout << "Success." << std::endl;
298  }
299  return ret;
300 }
301 
302 
303 /****************************************************************************/
#define DEG2RAD(x)
Definition: GeomHelper.h:35
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:282
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
const double SUMO_const_laneWidth
Definition: StdDefs.h:48
T MAX2(T a, T b)
Definition: StdDefs.h:80
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:228
int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:303
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:670
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:47
Instance responsible for building networks.
Definition: NBNetBuilder.h:107
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:158
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:148
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool mayAddOrRemove=true)
Performs the network building steps.
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:153
int size() const
Returns the number of nodes stored in this container.
Definition: NBNodeCont.h:288
void printBuiltNodesStatistics() const
Prints statistics about built nodes.
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:38
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NGFrame.cpp:213
The class storing the generated network.
Definition: NGNet.h:47
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:234
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double xAttachLength, double yAttachLength)
Creates a grid network.
Definition: NGNet.cpp:93
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:164
A class that builds random network using an algorithm by Markus Hartinger.
void createNet(int numNodes, bool gridMode)
Builds a NGNet using the set values.
static bool load(SUMOSAXHandler *handler, const std::vector< std::string > &files, const std::string &type, const bool stringParse=false)
Importer for edge type information stored in XML.
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition: NWFrame.cpp:174
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:48
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:125
A storage for options typed value containers)
Definition: OptionsCont.h:89
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
void resetWritable()
Resets all options to be writeable.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:85
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:43
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:75
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:38
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:63
static bool checkOptions()
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
bool checkOptions()
void fillOptions()
Definition: netgen_main.cpp:67
int main(int argc, char **argv)
NGNet * buildNetwork(NBNetBuilder &nb)
#define M_PI
Definition: odrSpiral.cpp:40