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  const bool alphaIDs = oc.getBool("grid.alphanumerical-ids");
174  if (alphaIDs && xNo > 26) {
175  WRITE_ERROR("There must be at most 26 nodes in the x-direction when using alphanumerical ids.");
176  hadError = true;
177  }
178 
179  if (hadError) {
180  throw ProcessError();
181  }
182  // build if everything's ok
183  NGNet* net = new NGNet(nb);
184  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength, alphaIDs);
185  return net;
186  }
187  // random net
188  TNeighbourDistribution neighborDist;
189  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
190  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
191  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
192  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
193  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
194  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
195  NGNet* net = new NGNet(nb);
196  NGRandomNetBuilder randomNet(*net,
197  oc.getFloat("rand.min-angle"),
198  oc.getFloat("rand.min-distance"),
199  oc.getFloat("rand.max-distance"),
200  oc.getFloat("rand.connectivity"),
201  oc.getInt("rand.num-tries"),
202  neighborDist);
203  randomNet.createNet(oc.getInt("rand.iterations"));
204  return net;
205 }
206 
207 
208 
209 int
210 main(int argc, char** argv) {
212  // give some application descriptions
213  oc.setApplicationDescription("Road network generator for the microscopic road traffic simulation SUMO.");
214  oc.setApplicationName("netgenerate", "SUMO netgenerate Version " VERSION_STRING);
215  int ret = 0;
216  try {
217  // initialise the application system (messaging, xml, options)
218  XMLSubSys::init();
219  fillOptions();
220  OptionsIO::setArgs(argc, argv);
222  if (oc.processMetaOptions(argc < 2)) {
224  return 0;
225  }
226  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
228  if (!checkOptions()) {
229  throw ProcessError();
230  }
232  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
233  Boundary(), Boundary());
235  NBNetBuilder nb;
236  nb.applyOptions(oc);
237  // build the netgen-network description
238  NGNet* net = buildNetwork(nb);
239  // ... and we have to do this...
240  oc.resetWritable();
241  // transfer to the netbuilding structures
242  net->toNB();
243  delete net;
244  // report generated structures
245  WRITE_MESSAGE(" Generation done;");
246  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
247  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
248  nb.compute(oc);
249  NWFrame::writeNetwork(oc, nb);
250  } catch (const ProcessError& e) {
251  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
252  WRITE_ERROR(e.what());
253  }
254  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
255  ret = 1;
256 #ifndef _DEBUG
257  } catch (const std::exception& e) {
258  if (std::string(e.what()) != std::string("")) {
259  WRITE_ERROR(e.what());
260  }
261  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
262  ret = 1;
263  } catch (...) {
264  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
265  ret = 1;
266 #endif
267  }
269  if (ret == 0) {
270  std::cout << "Success." << std::endl;
271  }
272  return ret;
273 }
274 
275 
276 
277 /****************************************************************************/
278 
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:428
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:72
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 toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:215
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.
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:69
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:50
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
void createChequerBoard(int numX, int numY, SUMOReal spaceX, SUMOReal spaceY, SUMOReal attachLength, bool alphaIDs)
Creates a grid network.
Definition: NGNet.cpp:91
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:158
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 known nodes.
Definition: NBNodeCont.h:271
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:153
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:194
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
#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:143
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
NBNodeCont & getNodeCont()
Returns the node container.
Definition: NBNetBuilder.h:161
Instance responsible for building networks.
Definition: NBNetBuilder.h:112
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:89
A storage for options typed value containers)
Definition: OptionsCont.h:99
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
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
static void initOutputOptions()
Definition: MsgHandler.cpp:197
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.