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