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-2016 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>
57 #include <utils/xml/XMLSubSys.h>
59 
60 #ifdef CHECK_MEMORY_LEAKS
61 #include <foreign/nvwa/debug_new.h>
62 #endif // CHECK_MEMORY_LEAKS
63 
64 
65 // ===========================================================================
66 // method definitions
67 // ===========================================================================
68 void
71  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
72  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
73  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
74  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
75 
76  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
77 
78  // insert options sub-topics
79  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
80  oc.addOptionSubTopic("Grid Network");
81  oc.addOptionSubTopic("Spider Network");
82  oc.addOptionSubTopic("Random Network");
83  oc.addOptionSubTopic("Output");
84  oc.addOptionSubTopic("TLS Building");
85  //oc.addOptionSubTopic("Ramp Guessing");
86  oc.addOptionSubTopic("Edge Removal");
87  oc.addOptionSubTopic("Unregulated Nodes");
88  oc.addOptionSubTopic("Processing");
89  oc.addOptionSubTopic("Building Defaults");
90  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
91 
95  oc.doRegister("default-junction-type", 'j', new Option_String());
96  oc.addSynonyme("default-junction-type", "junctions");
97  oc.addDescription("default-junction-type", "Building Defaults", "[traffic_light|priority|right_before_left] Determines the type of the build junctions");
99 }
100 
101 
102 bool
104  bool ok = NGFrame::checkOptions();
105  ok &= NBFrame::checkOptions();
106  ok &= NWFrame::checkOptions();
107  return ok;
108 }
109 
110 
111 NGNet*
114  // spider-net
115  if (oc.getBool("spider")) {
116  // check values
117  bool hadError = false;
118  if (oc.getInt("spider.arm-number") < 3) {
119  WRITE_ERROR("Spider networks need at least 3 arms.");
120  hadError = true;
121  }
122  if (oc.getInt("spider.circle-number") < 1) {
123  WRITE_ERROR("Spider networks need at least one circle.");
124  hadError = true;
125  }
126  if (oc.getFloat("spider.space-radius") < 10) {
127  WRITE_ERROR("The radius of spider networks must be at least 10m.");
128  hadError = true;
129  }
130  if (hadError) {
131  throw ProcessError();
132  }
133  // build if everything's ok
134  NGNet* net = new NGNet(nb);
135  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
136  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
137  return net;
138  }
139  // grid-net
140  if (oc.getBool("grid")) {
141  // get options
142  int xNo = oc.getInt("grid.x-number");
143  int yNo = oc.getInt("grid.y-number");
144  SUMOReal xLength = oc.getFloat("grid.x-length");
145  SUMOReal yLength = oc.getFloat("grid.y-length");
146  SUMOReal attachLength = oc.getFloat("grid.attach-length");
147  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
148  xNo = oc.getInt("grid.number");
149  }
150  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
151  yNo = oc.getInt("grid.number");
152  }
153  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
154  xLength = oc.getFloat("grid.length");
155  }
156  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
157  yLength = oc.getFloat("grid.length");
158  }
159  // check values
160  bool hadError = false;
161  if (xNo < 2 || yNo < 2) {
162  WRITE_ERROR("The number of nodes must be at least 2 in both directions.");
163  hadError = true;
164  }
165  if (xLength < 10. || yLength < 10.) {
166  WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
167  hadError = true;
168  }
169  if (attachLength != 0.0 && attachLength < 10.) {
170  WRITE_ERROR("The length of attached streets must be at least 10m.");
171  hadError = true;
172  }
173  if (hadError) {
174  throw ProcessError();
175  }
176  // build if everything's ok
177  NGNet* net = new NGNet(nb);
178  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
179  return net;
180  }
181  // random net
182  TNeighbourDistribution neighborDist;
183  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
184  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
185  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
186  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
187  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
188  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
189  NGNet* net = new NGNet(nb);
190  NGRandomNetBuilder randomNet(*net,
191  oc.getFloat("rand.min-angle"),
192  oc.getFloat("rand.min-distance"),
193  oc.getFloat("rand.max-distance"),
194  oc.getFloat("rand.connectivity"),
195  oc.getInt("rand.num-tries"),
196  neighborDist);
197  randomNet.createNet(oc.getInt("rand.iterations"));
198  return net;
199 }
200 
201 
202 
203 int
204 main(int argc, char** argv) {
206  // give some application descriptions
207  oc.setApplicationDescription("Road network generator for the microscopic road traffic simulation SUMO.");
208  oc.setApplicationName("netgenerate", "SUMO netgenerate Version " VERSION_STRING);
209  int ret = 0;
210  try {
211  // initialise the application system (messaging, xml, options)
212  XMLSubSys::init();
213  fillOptions();
214  OptionsIO::setArgs(argc, argv);
216  if (oc.processMetaOptions(argc < 2)) {
218  return 0;
219  }
220  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
222  if (!checkOptions()) {
223  throw ProcessError();
224  }
226  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
227  Boundary(), Boundary());
229  NBNetBuilder nb;
230  nb.applyOptions(oc);
231  // build the netgen-network description
232  NGNet* net = buildNetwork(nb);
233  // ... and we have to do this...
234  oc.resetWritable();
235  // transfer to the netbuilding structures
236  net->toNB();
237  delete net;
238  // report generated structures
239  WRITE_MESSAGE(" Generation done;");
240  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
241  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
242  nb.compute(oc);
243  NWFrame::writeNetwork(oc, nb);
244  } catch (const ProcessError& e) {
245  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
246  WRITE_ERROR(e.what());
247  }
248  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
249  ret = 1;
250 #ifndef _DEBUG
251  } catch (const std::exception& e) {
252  if (std::string(e.what()) != std::string("")) {
253  WRITE_ERROR(e.what());
254  }
255  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
256  ret = 1;
257  } catch (...) {
258  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
259  ret = 1;
260 #endif
261  }
263  if (ret == 0) {
264  std::cout << "Success." << std::endl;
265  }
266  return ret;
267 }
268 
269 
270 
271 /****************************************************************************/
272 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
void add(int numNeighbours, SUMOReal ratio)
adds a neighbour item to list
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:53
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:111
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:407
void resetWritable()
Resets all options to be writeable.
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:75
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:69
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool removeElements=true)
Performs the network building steps.
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
NGNet * buildNetwork(NBNetBuilder &nb)
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:212
void createChequerBoard(int numX, int numY, SUMOReal spaceX, SUMOReal spaceY, SUMOReal attachLength)
Creates a grid network.
Definition: NGNet.cpp:91
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void fillOptions()
Definition: netgen_main.cpp:69
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:50
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:65
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
void createSpiderWeb(int numRadDiv, int numCircles, SUMOReal spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:155
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
unsigned int size() const
Returns the number of known nodes.
Definition: NBNodeCont.h:273
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()
Returns the edge container.
Definition: NBNetBuilder.h:154
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:192
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.
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
The class storing the generated network.
Definition: NGNet.h:56
#define VERSION_STRING
Definition: config.h:225
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition: NWFrame.cpp:137
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static void getOptions()
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
NBNodeCont & getNodeCont()
Returns the node container.
Definition: NBNetBuilder.h:162
Instance responsible for building networks.
Definition: NBNetBuilder.h:113
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:51
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:61
#define SUMOReal
Definition: config.h:213
A class that builds random network using an algorithm by Markus Hartinger.
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:63
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
static void initOutputOptions()
Definition: MsgHandler.cpp:197
unsigned int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:282
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.