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 /****************************************************************************/
18 // Main for NETGENERATE
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #ifdef HAVE_VERSION_H
28 #include <version.h>
29 #endif
30 
31 #include <iostream>
32 #include <fstream>
33 #include <string>
34 #include <ctime>
35 #include <netgen/NGNet.h>
37 #include <netgen/NGFrame.h>
38 #include <netbuild/NBNetBuilder.h>
39 #include <netbuild/NBFrame.h>
40 #include <netwrite/NWFrame.h>
43 #include <utils/options/Option.h>
48 #include <utils/common/ToString.h>
51 #include <utils/xml/XMLSubSys.h>
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 void
61  oc.addCallExample("-c <CONFIGURATION>", "create net from given configuration");
62  oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>", "create grid net");
63  oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>", "create spider net");
64  oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>", "create random net");
65 
66  oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n In dependance to these switches other options are used.");
67 
68  // insert options sub-topics
69  SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
70  oc.addOptionSubTopic("Grid Network");
71  oc.addOptionSubTopic("Spider Network");
72  oc.addOptionSubTopic("Random Network");
73  oc.addOptionSubTopic("Output");
74  oc.addOptionSubTopic("Processing");
75  oc.addOptionSubTopic("Building Defaults");
76  oc.addOptionSubTopic("TLS Building");
77  oc.addOptionSubTopic("Edge Removal");
78  oc.addOptionSubTopic("Unregulated Nodes");
79  oc.addOptionSubTopic("Junctions");
80  oc.addOptionSubTopic("Pedestrian");
81  oc.addOptionSubTopic("Bicycle");
82  SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
83 
87  oc.doRegister("default-junction-type", 'j', new Option_String());
88  oc.addSynonyme("default-junction-type", "junctions");
89  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)");
91 }
92 
93 
94 bool
96  bool ok = NGFrame::checkOptions();
97  ok &= NBFrame::checkOptions();
98  ok &= NWFrame::checkOptions();
100  return ok;
101 }
102 
103 
104 NGNet*
107 
108  // spider-net
109  if (oc.getBool("spider")) {
110  // check values
111  bool hadError = false;
112  if (oc.getInt("spider.arm-number") < 3) {
113  WRITE_ERROR("Spider networks need at least 3 arms.");
114  hadError = true;
115  }
116  if (oc.getInt("spider.circle-number") < 1) {
117  WRITE_ERROR("Spider networks need at least one circle.");
118  hadError = true;
119  }
120  if (oc.getFloat("spider.space-radius") < 10) {
121  WRITE_ERROR("The radius of spider networks must be at least 10m.");
122  hadError = true;
123  }
124  if (hadError) {
125  throw ProcessError();
126  }
127  // build if everything's ok
128  NGNet* net = new NGNet(nb);
129  net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
130  oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
131  return net;
132  }
133  // grid-net
134  if (oc.getBool("grid")) {
135  // get options
136  int xNo = oc.getInt("grid.x-number");
137  int yNo = oc.getInt("grid.y-number");
138  double xLength = oc.getFloat("grid.x-length");
139  double yLength = oc.getFloat("grid.y-length");
140  double attachLength = oc.getFloat("grid.attach-length");
141  if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
142  xNo = oc.getInt("grid.number");
143  }
144  if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
145  yNo = oc.getInt("grid.number");
146  }
147  if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
148  xLength = oc.getFloat("grid.length");
149  }
150  if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
151  yLength = oc.getFloat("grid.length");
152  }
153  // check values
154  bool hadError = false;
155  if (attachLength == 0 && (xNo < 2 || yNo < 2)) {
156  WRITE_ERROR("The number of nodes must be at least 2 in both directions.");
157  hadError = true;
158  }
159  if (xLength < 10. || yLength < 10.) {
160  WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
161  hadError = true;
162  }
163  if (attachLength != 0.0 && attachLength < 10.) {
164  WRITE_ERROR("The length of attached streets must be at least 10m.");
165  hadError = true;
166  }
167  if (hadError) {
168  throw ProcessError();
169  }
170  // build if everything's ok
171  NGNet* net = new NGNet(nb);
172  net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
173  return net;
174  }
175  // random net
176  RandomDistributor<int> neighborDist;
177  neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
178  neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
179  neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
180  neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
181  neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
182  neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
183  NGNet* net = new NGNet(nb);
184  NGRandomNetBuilder randomNet(*net,
185  DEG2RAD(oc.getFloat("rand.min-angle")),
186  oc.getFloat("rand.min-distance"),
187  oc.getFloat("rand.max-distance"),
188  oc.getFloat("rand.connectivity"),
189  oc.getInt("rand.num-tries"),
190  neighborDist);
191  randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
192  return net;
193 }
194 
195 
196 
197 int
198 main(int argc, char** argv) {
200  // give some application descriptions
201  oc.setApplicationDescription("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO.");
202  oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
203  int ret = 0;
204  try {
205  // initialise the application system (messaging, xml, options)
206  XMLSubSys::init();
207  fillOptions();
208  OptionsIO::setArgs(argc, argv);
210  if (oc.processMetaOptions(argc < 2)) {
212  return 0;
213  }
214  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
216  if (!checkOptions()) {
217  throw ProcessError();
218  }
220  Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
221  Boundary(), Boundary());
223  NBNetBuilder nb;
224  nb.applyOptions(oc);
225  // build the netgen-network description
226  NGNet* net = buildNetwork(nb);
227  // ... and we have to do this...
228  oc.resetWritable();
229  // transfer to the netbuilding structures
230  net->toNB();
231  delete net;
232  // report generated structures
233  WRITE_MESSAGE(" Generation done;");
234  WRITE_MESSAGE(" " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
235  WRITE_MESSAGE(" " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
236  nb.compute(oc);
237  NWFrame::writeNetwork(oc, nb);
238  } catch (const ProcessError& e) {
239  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
240  WRITE_ERROR(e.what());
241  }
242  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
243  ret = 1;
244 #ifndef _DEBUG
245  } catch (const std::exception& e) {
246  if (std::string(e.what()) != std::string("")) {
247  WRITE_ERROR(e.what());
248  }
249  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
250  ret = 1;
251  } catch (...) {
252  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
253  ret = 1;
254 #endif
255  }
257  if (ret == 0) {
258  std::cout << "Success." << std::endl;
259  }
260  return ret;
261 }
262 
263 
264 
265 /****************************************************************************/
266 
NGNet::createChequerBoard
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double attachLength)
Creates a grid network.
Definition: NGNet.cpp:95
RandomDistributor::add
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
Definition: RandomDistributor.h:71
OptionsCont::getInt
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
Definition: OptionsCont.cpp:216
OptionsCont::processMetaOptions
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
Definition: OptionsCont.cpp:561
ToString.h
SystemFrame::addConfigurationOptions
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:40
NWFrame.h
SystemFrame::close
static void close()
Closes all of an applications subsystems.
Definition: SystemFrame.cpp:131
NGNet::toNB
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition: NGNet.cpp:231
MsgHandler::initOutputOptions
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:208
NBNetBuilder
Instance responsible for building networks.
Definition: NBNetBuilder.h:110
OptionsCont.h
OptionsCont::resetWritable
void resetWritable()
Resets all options to be writeable.
Definition: OptionsCont.cpp:445
MsgHandler.h
NGFrame::fillOptions
static void fillOptions()
Inserts options used by the network generator.
Definition: NGFrame.cpp:40
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:202
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:95
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:223
main
int main(int argc, char **argv)
Definition: netgen_main.cpp:198
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
NWFrame::fillOptions
static void fillOptions(bool forNetgen)
Inserts options used by the network writer.
Definition: NWFrame.cpp:50
GeoConvHelper::init
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
Definition: GeoConvHelper.cpp:201
NBNetBuilder::getEdgeCont
NBEdgeCont & getEdgeCont()
Definition: NBNetBuilder.h:151
SystemFrame::addReportOptions
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:65
NGRandomNetBuilder
A class that builds random network using an algorithm by Markus Hartinger.
Definition: NGRandomNetBuilder.h:42
fillOptions
void fillOptions()
Definition: netgen_main.cpp:59
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:473
RandomDistributor.h
NGNet::createSpiderWeb
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter)
Creates a spider network.
Definition: NGNet.cpp:160
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:577
OptionsCont::setApplicationName
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
Definition: OptionsCont.cpp:485
NGNet
The class storing the generated network.
Definition: NGNet.h:50
SystemFrame.h
XMLSubSys::setValidation
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:59
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:96
NBEdgeCont::size
int size() const
Returns the number of edges.
Definition: NBEdgeCont.h:306
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
OutputDevice.h
NBNetBuilder.h
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
ProcessError
Definition: UtilExceptions.h:40
NBNetBuilder::applyOptions
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
Definition: NBNetBuilder.cpp:66
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
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:505
Option_String
Definition: Option.h:401
UtilExceptions.h
XMLSubSys::init
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
NBFrame::fillOptions
static void fillOptions(bool forNetgen)
Inserts options used by the network converter.
Definition: NBFrame.cpp:49
NGRandomNetBuilder.h
RandomDistributor< int >
DEG2RAD
#define DEG2RAD(x)
Definition: GeomHelper.h:38
OptionsCont::isDefault
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
Definition: OptionsCont.cpp:164
OptionsCont::addOptionSubTopic
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
Definition: OptionsCont.cpp:523
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:72
NGFrame.h
OptionsIO::getOptions
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:76
OptionsCont::setApplicationDescription
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
Definition: OptionsCont.cpp:493
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:209
Option.h
OptionsCont::addCallExample
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
Definition: OptionsCont.cpp:499
NBNodeCont::size
int size() const
Returns the number of nodes stored in this container.
Definition: NBNodeCont.h:264
NGRandomNetBuilder::createNet
void createNet(int numNodes, bool gridMode)
Builds a NGNet using the set values.
Definition: NGRandomNetBuilder.cpp:210
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
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:78
config.h
NBFrame.h
RandHelper::insertRandOptions
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:43
RandHelper.h
SystemFrame::checkOptions
static bool checkOptions()
checks shared options and sets StdDefs
Definition: SystemFrame.cpp:118
buildNetwork
NGNet * buildNetwork(NBNetBuilder &nb)
Definition: netgen_main.cpp:105
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:156
NGFrame::checkOptions
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition: NGFrame.cpp:206
VERSION_STRING
#define VERSION_STRING
Definition: config.h:207
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
WRITE_MESSAGE
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:240
NGNet.h
OptionsIO.h
XMLSubSys.h