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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Main for NETGENERATE
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 #include <ctime>
34 #include <netgen/NGNet.h>
36 #include <netgen/NGFrame.h>
37 #include <netbuild/NBNetBuilder.h>
38 #include <netbuild/NBFrame.h>
39 #include <netwrite/NWFrame.h>
40 #include <netimport/NITypeLoader.h>
44 #include <utils/options/Option.h>
49 #include <utils/common/ToString.h>
52 #include <utils/xml/XMLSubSys.h>
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 void
62  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
63  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
64  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
65  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
66 
67  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
68 
69  // insert options sub-topics
70  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
71  oc.addOptionSubTopic("Grid Network");
72  oc.addOptionSubTopic("Spider Network");
73  oc.addOptionSubTopic("Random Network");
74  oc.addOptionSubTopic("Input");
75  oc.addOptionSubTopic("Output");
76  oc.addOptionSubTopic("Processing");
77  oc.addOptionSubTopic("Building Defaults");
78  oc.addOptionSubTopic("TLS Building");
79  oc.addOptionSubTopic("Edge Removal");
80  oc.addOptionSubTopic("Unregulated Nodes");
81  oc.addOptionSubTopic("Junctions");
82  oc.addOptionSubTopic("Pedestrian");
83  oc.addOptionSubTopic("Bicycle");
84  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
85 
89  oc.doRegister("default-junction-type", 'j', new Option_String());
90  oc.addSynonyme("default-junction-type", "junctions");
91  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)");
93 }
94 
95 
96 bool
98  bool ok = NGFrame::checkOptions();
99  ok &= NBFrame::checkOptions();
100  ok &= NWFrame::checkOptions();
102  return ok;
103 }
104 
105 
106 NGNet*
109 
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 (attachLength == 0 && (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  if (hadError) {
170  throw ProcessError();
171  }
172  // build if everything's ok
173  NGNet* net = new NGNet(nb);
174  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
175  return net;
176  }
177  // random net
178  RandomDistributor<int> neighborDist;
179  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
180  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
181  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
182  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
183  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
184  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
185  NGNet* net = new NGNet(nb);
186  NGRandomNetBuilder randomNet(*net,
187  DEG2RAD(oc.getFloat("rand.min-angle")),
188  oc.getFloat("rand.min-distance"),
189  oc.getFloat("rand.max-distance"),
190  oc.getFloat("rand.connectivity"),
191  oc.getInt("rand.num-tries"),
192  neighborDist);
193  randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
194  return net;
195 }
196 
197 
198 
199 int
200 main(int argc, char** argv) {
202  // give some application descriptions
203  oc.setApplicationDescription("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO.");
204  oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
205  int ret = 0;
206  try {
207  // initialise the application system (messaging, xml, options)
208  XMLSubSys::init();
209  fillOptions();
210  OptionsIO::setArgs(argc, argv);
212  if (oc.processMetaOptions(argc < 2)) {
214  return 0;
215  }
216  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
218  if (!checkOptions()) {
219  throw ProcessError();
220  }
222  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
223  Boundary(), Boundary());
225  NBNetBuilder nb;
226  nb.applyOptions(oc);
227  if (oc.isSet("type-files")) {
228  NIXMLTypesHandler* handler = new NIXMLTypesHandler(nb.getTypeCont());
229  NITypeLoader::load(handler, oc.getStringVector("type-files"), "types");
230  }
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 
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
NGNet::createChequerBoard
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:94
RandomDistributor::add
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
Definition: RandomDistributor.h:70
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:215
OptionsCont::processMetaOptions
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
Definition: OptionsCont.cpp:557
ToString.h
SystemFrame::addConfigurationOptions
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:39
NWFrame.h
SystemFrame::close
static void close()
Closes all of an applications subsystems.
Definition: SystemFrame.cpp:133
NGNet::toNB
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:230
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:216
NIXMLTypesHandler.h
NBNetBuilder
Instance responsible for building networks.
Definition: NBNetBuilder.h:109
OptionsCont.h
OptionsCont::resetWritable
void resetWritable()
Resets all options to be writeable.
Definition: OptionsCont.cpp:441
MsgHandler.h
NGFrame::fillOptions
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:39
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
NITypeLoader::load
static void load(SUMOSAXHandler *handler, const std::vector< std::string > &files, const std::string &type, const bool stringParse=false)
Definition: NITypeLoader.cpp:43
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
checkOptions
bool checkOptions()
Definition: netgen_main.cpp:97
GeoConvHelper.h
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
main
int main(int argc, char **argv)
Definition: netgen_main.cpp:200
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
NWFrame::fillOptions
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:49
GeoConvHelper::init
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
Definition: GeoConvHelper.cpp:200
NBNetBuilder::getEdgeCont
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:150
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
SystemFrame::addReportOptions
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:64
NGRandomNetBuilder
A class that builds random network using an algorithm by Markus Hartinger.
Definition: NGRandomNetBuilder.h:41
fillOptions
void fillOptions()
Definition: netgen_main.cpp:60
OptionsCont::addDescription
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Definition: OptionsCont.cpp:469
RandomDistributor.h
NGNet::createSpiderWeb
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:159
NWFrame::writeNetwork
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition: NWFrame.cpp:175
NWFrame::checkOptions
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NWFrame.cpp:126
NBFrame::checkOptions
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NBFrame.cpp:591
OptionsCont::setApplicationName
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
Definition: OptionsCont.cpp:481
NGNet
The class storing the generated network.
Definition: NGNet.h:49
SystemFrame.h
XMLSubSys::setValidation
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:58
OptionsCont::addSynonyme
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:95
NBEdgeCont::size
int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:304
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
NIXMLTypesHandler
Importer for edge type information stored in XML.
Definition: NIXMLTypesHandler.h:46
OutputDevice.h
NBNetBuilder.h
NITypeLoader.h
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:74
ProcessError
Definition: UtilExceptions.h:39
NBNetBuilder::applyOptions
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
Definition: NBNetBuilder.cpp:65
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
OptionsCont::setAdditionalHelpMessage
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
Definition: OptionsCont.cpp:501
Option_String
Definition: Option.h:399
UtilExceptions.h
XMLSubSys::init
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:47
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
NBFrame::fillOptions
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:48
NGRandomNetBuilder.h
RandomDistributor< int >
DEG2RAD
#define DEG2RAD(x)
Definition: GeomHelper.h:37
OptionsCont::isDefault
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
Definition: OptionsCont.cpp:163
OptionsCont::addOptionSubTopic
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
Definition: OptionsCont.cpp:519
RandHelper::initRandGlobal
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:77
NGFrame.h
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:75
OptionsCont::setApplicationDescription
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
Definition: OptionsCont.cpp:489
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
Option.h
OptionsCont::addCallExample
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
Definition: OptionsCont.cpp:495
NBNodeCont::size
int size() const
Returns the number of nodes stored in this container.
Definition: NBNodeCont.h:263
NGRandomNetBuilder::createNet
void createNet(int numNodes, bool gridMode)
Builds a NGNet using the set values.
Definition: NGRandomNetBuilder.cpp:209
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:54
NBNetBuilder::compute
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool mayAddOrRemove=true)
Performs the network building steps.
Definition: NBNetBuilder.cpp:77
config.h
NBFrame.h
RandHelper::insertRandOptions
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:45
RandHelper.h
SystemFrame::checkOptions
static bool checkOptions()
checks shared options and sets StdDefs
Definition: SystemFrame.cpp:120
buildNetwork
NGNet * buildNetwork(NBNetBuilder &nb)
Definition: netgen_main.cpp:107
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
NBNetBuilder::getNodeCont
NBNodeCont & getNodeCont()
Returns a reference to the node container.
Definition: NBNetBuilder.h:155
NGFrame::checkOptions
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NGFrame.cpp:208
NBNetBuilder::getTypeCont
NBTypeCont & getTypeCont()
Returns a reference to the type container.
Definition: NBNetBuilder.h:160
VERSION_STRING
#define VERSION_STRING
Definition: config.h:210
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:277
NGNet.h
OptionsIO.h
XMLSubSys.h