SUMO - Simulation of Urban MObility
netgen_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Main for NETGENERATE
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifdef HAVE_VERSION_H
35 #include <version.h>
36 #endif
37 
38 #include <iostream>
39 #include <fstream>
40 #include <string>
41 #include <ctime>
42 #include <netgen/NGNet.h>
44 #include <netgen/NGFrame.h>
45 #include <netbuild/NBNetBuilder.h>
46 #include <netbuild/NBFrame.h>
47 #include <netwrite/NWFrame.h>
50 #include <utils/options/Option.h>
55 #include <utils/common/ToString.h>
58 #include <utils/xml/XMLSubSys.h>
60 
61 
62 // ===========================================================================
63 // method definitions
64 // ===========================================================================
65 void
68  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
69  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
70  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
71  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
72 
73  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
74 
75  // insert options sub-topics
76  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
77  oc.addOptionSubTopic("Grid Network");
78  oc.addOptionSubTopic("Spider Network");
79  oc.addOptionSubTopic("Random Network");
80  oc.addOptionSubTopic("Output");
81  oc.addOptionSubTopic("TLS Building");
82  //oc.addOptionSubTopic("Ramp Guessing");
83  oc.addOptionSubTopic("Edge Removal");
84  oc.addOptionSubTopic("Unregulated Nodes");
85  oc.addOptionSubTopic("Processing");
86  oc.addOptionSubTopic("Building Defaults");
87  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
88 
92  oc.doRegister("default-junction-type", 'j', new Option_String());
93  oc.addSynonyme("default-junction-type", "junctions");
94  oc.addDescription("default-junction-type", "Building Defaults", "[traffic_light|priority|right_before_left] Determines the type of the build junctions");
96 }
97 
98 
99 bool
101  bool ok = NGFrame::checkOptions();
102  ok &= NBFrame::checkOptions();
103  ok &= NWFrame::checkOptions();
104  return ok;
105 }
106 
107 
108 NGNet*
111  // spider-net
112  if (oc.getBool("spider")) {
113  // check values
114  bool hadError = false;
115  if (oc.getInt("spider.arm-number") < 3) {
116  WRITE_ERROR("Spider networks need at least 3 arms.");
117  hadError = true;
118  }
119  if (oc.getInt("spider.circle-number") < 1) {
120  WRITE_ERROR("Spider networks need at least one circle.");
121  hadError = true;
122  }
123  if (oc.getFloat("spider.space-radius") < 10) {
124  WRITE_ERROR("The radius of spider networks must be at least 10m.");
125  hadError = true;
126  }
127  if (hadError) {
128  throw ProcessError();
129  }
130  // build if everything's ok
131  NGNet* net = new NGNet(nb);
132  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
133  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
134  return net;
135  }
136  // grid-net
137  if (oc.getBool("grid")) {
138  // get options
139  int xNo = oc.getInt("grid.x-number");
140  int yNo = oc.getInt("grid.y-number");
141  double xLength = oc.getFloat("grid.x-length");
142  double yLength = oc.getFloat("grid.y-length");
143  double attachLength = oc.getFloat("grid.attach-length");
144  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
145  xNo = oc.getInt("grid.number");
146  }
147  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
148  yNo = oc.getInt("grid.number");
149  }
150  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
151  xLength = oc.getFloat("grid.length");
152  }
153  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
154  yLength = oc.getFloat("grid.length");
155  }
156  // check values
157  bool hadError = false;
158  if (xNo < 2 || yNo < 2) {
159  WRITE_ERROR("The number of nodes must be at least 2 in both directions.");
160  hadError = true;
161  }
162  if (xLength < 10. || yLength < 10.) {
163  WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
164  hadError = true;
165  }
166  if (attachLength != 0.0 && attachLength < 10.) {
167  WRITE_ERROR("The length of attached streets must be at least 10m.");
168  hadError = true;
169  }
170  const bool alphaIDs = oc.getBool("grid.alphanumerical-ids");
171  if (alphaIDs && xNo > 26) {
172  WRITE_ERROR("There must be at most 26 nodes in the x-direction when using alphanumerical ids.");
173  hadError = true;
174  }
175 
176  if (hadError) {
177  throw ProcessError();
178  }
179  // build if everything's ok
180  NGNet* net = new NGNet(nb);
181  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength, alphaIDs);
182  return net;
183  }
184  // random net
185  RandomDistributor<int> neighborDist;
186  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
187  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
188  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
189  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
190  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
191  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
192  NGNet* net = new NGNet(nb);
193  NGRandomNetBuilder randomNet(*net,
194  oc.getFloat("rand.min-angle"),
195  oc.getFloat("rand.min-distance"),
196  oc.getFloat("rand.max-distance"),
197  oc.getFloat("rand.connectivity"),
198  oc.getInt("rand.num-tries"),
199  neighborDist);
200  randomNet.createNet(oc.getInt("rand.iterations"));
201  return net;
202 }
203 
204 
205 
206 int
207 main(int argc, char** argv) {
209  // give some application descriptions
210  oc.setApplicationDescription("Road network generator for the microscopic road traffic simulation SUMO.");
211  oc.setApplicationName("netgenerate", "SUMO netgenerate Version " VERSION_STRING);
212  int ret = 0;
213  try {
214  // initialise the application system (messaging, xml, options)
215  XMLSubSys::init();
216  fillOptions();
217  OptionsIO::setArgs(argc, argv);
219  if (oc.processMetaOptions(argc < 2)) {
221  return 0;
222  }
223  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
225  if (!checkOptions()) {
226  throw ProcessError();
227  }
229  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
230  Boundary(), Boundary());
232  NBNetBuilder nb;
233  nb.applyOptions(oc);
234  // build the netgen-network description
235  NGNet* net = buildNetwork(nb);
236  // ... and we have to do this...
237  oc.resetWritable();
238  // transfer to the netbuilding structures
239  net->toNB();
240  delete net;
241  // report generated structures
242  WRITE_MESSAGE(" Generation done;");
243  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
244  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
245  nb.compute(oc);
246  NWFrame::writeNetwork(oc, nb);
247  } catch (const ProcessError& e) {
248  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
249  WRITE_ERROR(e.what());
250  }
251  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
252  ret = 1;
253 #ifndef _DEBUG
254  } catch (const std::exception& e) {
255  if (std::string(e.what()) != std::string("")) {
256  WRITE_ERROR(e.what());
257  }
258  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
259  ret = 1;
260  } catch (...) {
261  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
262  ret = 1;
263 #endif
264  }
266  if (ret == 0) {
267  std::cout << "Success." << std::endl;
268  }
269  return ret;
270 }
271 
272 
273 
274 /****************************************************************************/
275 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:49
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:119
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:436
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:82
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:72
bool checkOptions()
int main(int argc, char **argv)
void createNet(int numNodes)
Builds a NGNet using the set values.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:211
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:66
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:47
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:62
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:64
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)
int size() const
Returns the number of nodes stored in this container.
Definition: NBNodeCont.h:229
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
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:155
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:190
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.
The class storing the generated network.
Definition: NGNet.h:56
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:210
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition: NWFrame.cpp:154
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:160
Instance responsible for building networks.
Definition: NBNetBuilder.h:114
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:154
int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:282
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:47
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:56
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength, bool alphaIDs)
Creates a grid network.
Definition: NGNet.cpp:87
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:59
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
static void initOutputOptions()
Definition: MsgHandler.cpp:193
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.