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-2020 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 // ===========================================================================
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("Input");
74  oc.addOptionSubTopic("Output");
75  oc.addOptionSubTopic("Processing");
76  oc.addOptionSubTopic("Building Defaults");
77  oc.addOptionSubTopic("TLS Building");
78  oc.addOptionSubTopic("Edge Removal");
79  oc.addOptionSubTopic("Unregulated Nodes");
80  oc.addOptionSubTopic("Junctions");
81  oc.addOptionSubTopic("Pedestrian");
82  oc.addOptionSubTopic("Bicycle");
83  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
84 
88  oc.doRegister("default-junction-type", 'j', new Option_String());
89  oc.addSynonyme("default-junction-type", "junctions");
90  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)");
92 }
93 
94 
95 bool
97  bool ok = NGFrame::checkOptions();
98  ok &= NBFrame::checkOptions();
99  ok &= NWFrame::checkOptions();
101  return ok;
102 }
103 
104 
105 NGNet*
108 
109  // spider-net
110  if (oc.getBool("spider")) {
111  // check values
112  bool hadError = false;
113  if (oc.getInt("spider.arm-number") < 3) {
114  WRITE_ERROR("Spider networks need at least 3 arms.");
115  hadError = true;
116  }
117  if (oc.getInt("spider.circle-number") < 1) {
118  WRITE_ERROR("Spider networks need at least one circle.");
119  hadError = true;
120  }
121  if (oc.getFloat("spider.space-radius") < 10) {
122  WRITE_ERROR("The radius of spider networks must be at least 10m.");
123  hadError = true;
124  }
125  if (hadError) {
126  throw ProcessError();
127  }
128  // build if everything's ok
129  NGNet* net = new NGNet(nb);
130  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
131  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
132  return net;
133  }
134  // grid-net
135  if (oc.getBool("grid")) {
136  // get options
137  int xNo = oc.getInt("grid.x-number");
138  int yNo = oc.getInt("grid.y-number");
139  double xLength = oc.getFloat("grid.x-length");
140  double yLength = oc.getFloat("grid.y-length");
141  double attachLength = oc.getFloat("grid.attach-length");
142  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
143  xNo = oc.getInt("grid.number");
144  }
145  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
146  yNo = oc.getInt("grid.number");
147  }
148  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
149  xLength = oc.getFloat("grid.length");
150  }
151  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
152  yLength = oc.getFloat("grid.length");
153  }
154  // check values
155  bool hadError = false;
156  if (xNo < 1 || yNo < 1 || (attachLength == 0 && (xNo < 2 && yNo < 2))) {
157  WRITE_ERROR("The number of nodes must be positive and at least 2 in one direction.");
158  hadError = true;
159  }
160  if (xLength < 10. || yLength < 10.) {
161  WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
162  hadError = true;
163  }
164  if (attachLength != 0.0 && attachLength < 10.) {
165  WRITE_ERROR("The length of attached streets must be at least 10m.");
166  hadError = true;
167  }
168  if (hadError) {
169  throw ProcessError();
170  }
171  // build if everything's ok
172  NGNet* net = new NGNet(nb);
173  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
174  return net;
175  }
176  // random net
177  RandomDistributor<int> neighborDist;
178  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
179  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
180  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
181  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
182  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
183  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
184  NGNet* net = new NGNet(nb);
185  NGRandomNetBuilder randomNet(*net,
186  DEG2RAD(oc.getFloat("rand.min-angle")),
187  oc.getFloat("rand.min-distance"),
188  oc.getFloat("rand.max-distance"),
189  oc.getFloat("rand.connectivity"),
190  oc.getInt("rand.num-tries"),
191  neighborDist);
192  randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
193  return net;
194 }
195 
196 
197 
198 int
199 main(int argc, char** argv) {
201  // give some application descriptions
202  oc.setApplicationDescription("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO.");
203  oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
204  int ret = 0;
205  try {
206  // initialise the application system (messaging, xml, options)
207  XMLSubSys::init();
208  fillOptions();
209  OptionsIO::setArgs(argc, argv);
211  if (oc.processMetaOptions(argc < 2)) {
213  return 0;
214  }
215  XMLSubSys::setValidation(oc.getString("xml-validation"), "never", "never");
217  if (!checkOptions()) {
218  throw ProcessError();
219  }
221  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
222  Boundary(), Boundary());
224  NBNetBuilder nb;
225  nb.applyOptions(oc);
226  if (oc.isSet("type-files")) {
227  NIXMLTypesHandler* handler = new NIXMLTypesHandler(nb.getTypeCont());
228  NITypeLoader::load(handler, oc.getStringVector("type-files"), "types");
229  }
230  // build the netgen-network description
231  NGNet* net = buildNetwork(nb);
232  // ... and we have to do this...
233  oc.resetWritable();
234  // transfer to the netbuilding structures
235  net->toNB();
236  delete net;
237  // report generated structures
238  WRITE_MESSAGE(" Generation done;");
239  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
240  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
241  nb.compute(oc);
242  NWFrame::writeNetwork(oc, nb);
243  } catch (const ProcessError& e) {
244  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
245  WRITE_ERROR(e.what());
246  }
247  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
248  ret = 1;
249 #ifndef _DEBUG
250  } catch (const std::exception& e) {
251  if (std::string(e.what()) != std::string("")) {
252  WRITE_ERROR(e.what());
253  }
254  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
255  ret = 1;
256  } catch (...) {
257  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
258  ret = 1;
259 #endif
260  }
262  if (ret == 0) {
263  std::cout << "Success." << std::endl;
264  }
265  return ret;
266 }
267 
268 
269 /****************************************************************************/
#define DEG2RAD(x)
Definition: GeomHelper.h:35
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:278
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
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:217
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:647
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:284
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:207
The class storing the generated network.
Definition: NGNet.h:47
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:233
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:162
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:93
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:79
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
static void initRandGlobal(std::mt19937 *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:76
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:44
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()
Definition: netgen_main.cpp:96
void fillOptions()
Definition: netgen_main.cpp:59
int main(int argc, char **argv)
NGNet * buildNetwork(NBNetBuilder &nb)