Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
netgen_main.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
21// Main for NETGENERATE
22/****************************************************************************/
23#include <config.h>
24
25#ifdef HAVE_VERSION_H
26#include <version.h>
27#endif
28
29#include <iostream>
30#include <fstream>
31#include <string>
32#include <ctime>
33#include <netgen/NGNet.h>
35#include <netgen/NGFrame.h>
37#include <netbuild/NBFrame.h>
38#include <netwrite/NWFrame.h>
51#include <utils/xml/XMLSubSys.h>
53
54
55// ===========================================================================
56// method definitions
57// ===========================================================================
58void
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 dependence 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("Input");
74 oc.addOptionSubTopic("Output");
75 oc.addOptionSubTopic("Processing");
76 oc.addOptionSubTopic("Building Defaults");
77 oc.addOptionSubTopic("TLS Building");
78 oc.addOptionSubTopic("Edge Removal");
79 oc.addOptionSubTopic("Unregulated Nodes");
80 oc.addOptionSubTopic("Junctions");
81 oc.addOptionSubTopic("Pedestrian");
82 oc.addOptionSubTopic("Bicycle");
83 SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
84
86 NBFrame::fillOptions(oc, true);
87 NWFrame::fillOptions(oc, true);
88 oc.doRegister("default-junction-type", 'j', new Option_String());
89 oc.addSynonyme("default-junction-type", "junctions");
90 oc.addDescription("default-junction-type", "Building Defaults", TL("[traffic_light|priority|right_before_left|left_before_right|traffic_light_right_on_red|priority_stop|allway_stop|...] Determines junction type (see wiki/Networks/PlainXML#Node_types)"));
92
93 oc.doRegister("tls.discard-simple", new Option_Bool(false));
94 oc.addDescription("tls.discard-simple", "TLS Building", "Does not instantiate traffic lights at geometry-like nodes");
95
96 oc.doRegister("railway.signal.permit-unsignalized", new Option_StringVector({"tram", "cable_car"}));
97 oc.addDescription("railway.signal.permit-unsignalized", "TLS Building", TL("List rail classes that may run without rail signals"));
98}
99
100
101bool
104 bool ok = NGFrame::checkOptions();
105 ok &= NBFrame::checkOptions(oc);
106 ok &= NWFrame::checkOptions(oc);
108 return ok;
109}
110
111
112NGNet*
115
116 const double laneWidth = oc.isDefault("default.lanewidth") ? SUMO_const_laneWidth : oc.getFloat("default.lanewidth");
117 double minLength = (oc.getInt("default.lanenumber") + oc.getInt("turn-lanes")) * 2 * laneWidth + oc.getFloat("default.junctions.radius") * 2 + POSITION_EPS;
118 // spider-net
119 if (oc.getBool("spider")) {
120 // check values
121 bool hadError = false;
122 if (oc.getInt("spider.arm-number") < 3) {
123 WRITE_ERROR(TL("Spider networks need at least 3 arms."));
124 hadError = true;
125 }
126 if (oc.getInt("spider.arm-number") > 4 && !oc.getBool("spider.omit-center")) {
127 WRITE_WARNING(TL("Spider networks with many arms should use option spider.omit-center"));
128 }
129 if (oc.getInt("spider.circle-number") < 1) {
130 WRITE_ERROR(TL("Spider networks need at least one circle."));
131 hadError = true;
132 }
133 minLength = MAX2(minLength, laneWidth * 2 * MAX2(oc.getInt("spider.arm-number"), 3) / (2 * M_PI));
134 if (oc.getFloat("spider.space-radius") < POSITION_EPS) {
135 WRITE_ERROR("The radius of spider networks must be at least " + toString(POSITION_EPS));
136 hadError = true;
137 } else if (oc.getFloat("spider.space-radius") < minLength) {
138 WRITE_WARNINGF(TL("The radius of spider networks should be at least % for the given lanenumber, lanewidth and junction radius"), toString(minLength));
139 }
140 if (hadError) {
141 throw ProcessError();
142 }
143 // build if everything's ok
144 NGNet* net = new NGNet(nb);
145 net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
146 oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"),
147 oc.getFloat("spider.attach-length"));
148 return net;
149 }
150 // grid-net
151 if (oc.getBool("grid")) {
152 // get options
153 int xNo = oc.getInt("grid.x-number");
154 int yNo = oc.getInt("grid.y-number");
155 double xLength = oc.getFloat("grid.x-length");
156 double yLength = oc.getFloat("grid.y-length");
157 double xAttachLength = oc.getFloat("grid.x-attach-length");
158 double yAttachLength = oc.getFloat("grid.y-attach-length");
159 if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
160 xNo = oc.getInt("grid.number");
161 }
162 if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
163 yNo = oc.getInt("grid.number");
164 }
165 if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
166 xLength = oc.getFloat("grid.length");
167 }
168 if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
169 yLength = oc.getFloat("grid.length");
170 }
171 if (oc.isDefault("grid.x-attach-length") && !oc.isDefault("grid.attach-length")) {
172 xAttachLength = oc.getFloat("grid.attach-length");
173 }
174 if (oc.isDefault("grid.y-attach-length") && !oc.isDefault("grid.attach-length")) {
175 yAttachLength = oc.getFloat("grid.attach-length");
176 }
177 // check values
178 bool hadError = false;
179 if (xNo < 1 || yNo < 1 || (xAttachLength == 0 && yAttachLength == 0 && (xNo < 2 && yNo < 2))) {
180 WRITE_ERROR(TL("The number of nodes must be positive and at least 2 in one direction if there are no attachments."));
181 hadError = true;
182 }
183 const double minAttachLength = minLength / 2 + POSITION_EPS / 2;
184 if (xLength < POSITION_EPS || yLength < POSITION_EPS) {
185 WRITE_ERROR("The distance between nodes must be at least " + toString(POSITION_EPS));
186 hadError = true;
187 } else if (xLength < minLength || yLength < minLength) {
188 WRITE_WARNINGF(TL("The distance between nodes should be at least % for the given lanenumber, lanewidth and junction radius"), toString(minLength));
189 }
190 if (xAttachLength != 0.0 && xAttachLength < POSITION_EPS) {
191 WRITE_ERROR("The length of attached streets must be at least " + toString(POSITION_EPS));
192 hadError = true;
193 } else if (xAttachLength != 0.0 && xAttachLength < minAttachLength) {
194 WRITE_WARNINGF(TL("The length of attached streets should be at least % for the given lanenumber, lanewidth and junction radius"), toString(minAttachLength));
195 } else if (yAttachLength != 0.0 && yAttachLength < POSITION_EPS) {
196 WRITE_ERROR("The length of attached streets must be at least " + toString(POSITION_EPS));
197 hadError = true;
198 } else if (yAttachLength != 0.0 && yAttachLength < minAttachLength) {
199 WRITE_WARNINGF(TL("The length of attached streets should be at least % for the given lanenumber, lanewidth and junction radius"), toString(minAttachLength));
200 }
201 if (hadError) {
202 throw ProcessError();
203 }
204 // build if everything's ok
205 NGNet* net = new NGNet(nb);
206 net->createChequerBoard(xNo, yNo, xLength, yLength, xAttachLength, yAttachLength);
207 return net;
208 }
209 // random net
210 RandomDistributor<int> neighborDist;
211 neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
212 neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
213 neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
214 neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
215 neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
216 neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
217 NGNet* net = new NGNet(nb);
218 NGRandomNetBuilder randomNet(*net,
219 DEG2RAD(oc.getFloat("rand.min-angle")),
220 oc.getFloat("rand.min-distance"),
221 oc.getFloat("rand.max-distance"),
222 oc.getFloat("rand.connectivity"),
223 oc.getInt("rand.num-tries"),
224 neighborDist);
225 randomNet.createNet(oc.getInt("rand.iterations"), oc.getBool("rand.grid"));
226 return net;
227}
228
229
230
231int
232main(int argc, char** argv) {
234 oc.setApplicationDescription(TL("Synthetic network generator for the microscopic, multi-modal traffic simulation SUMO."));
235 oc.setApplicationName("netgenerate", "Eclipse SUMO netgenerate Version " VERSION_STRING);
236 int ret = 0;
237 try {
238 // initialise the application system (messaging, xml, options)
240 fillOptions();
241 OptionsIO::setArgs(argc, argv);
243 if (oc.processMetaOptions(argc < 2)) {
245 return 0;
246 }
247 XMLSubSys::setValidation(oc.getString("xml-validation"), "never", "never");
249 if (!checkOptions()) {
250 throw ProcessError();
251 }
253 Position(oc.getFloat("offset.x"), oc.getFloat("offset.y")),
254 Boundary(), Boundary());
256 NBNetBuilder nb;
257 nb.applyOptions(oc);
258 if (oc.isSet("type-files")) {
259 NIXMLTypesHandler handler(nb.getTypeCont());
260 NITypeLoader::load(handler, oc.getStringVector("type-files"), "types");
261 }
262 // build the netgen-network description
263 NGNet* net = buildNetwork(nb);
264 // ... and we have to do this...
265 oc.resetWritable();
266 // transfer to the netbuilding structures
267 net->toNB();
268 delete net;
269 // report generated structures
270 WRITE_MESSAGE(TL(" Generation done;"));
271 WRITE_MESSAGEF(TL(" % nodes generated."), toString<int>(nb.getNodeCont().size()));
272 WRITE_MESSAGEF(TL(" % edges generated."), toString<int>(nb.getEdgeCont().size()));
273 if (oc.getBool("tls.discard-simple")) {
275 int removed = nb.getTLLogicCont().getNumExtracted();
276 if (removed > 0) {
277 WRITE_MESSAGEF(TL(" Removed % traffic lights at geometry-like nodes"), toString(removed));
278 }
279 }
280 nb.compute(oc);
282 NWFrame::writeNetwork(oc, nb);
283 } catch (const ProcessError& e) {
284 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
285 WRITE_ERROR(e.what());
286 }
287 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
288 ret = 1;
289#ifndef _DEBUG
290 } catch (const std::exception& e) {
291 if (std::string(e.what()) != std::string("")) {
292 WRITE_ERROR(e.what());
293 }
294 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
295 ret = 1;
296 } catch (...) {
297 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
298 ret = 1;
299#endif
300 }
302 if (ret == 0) {
303 std::cout << "Success." << std::endl;
304 }
305 return ret;
306}
307
308
309/****************************************************************************/
#define DEG2RAD(x)
Definition GeomHelper.h:35
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:288
#define WRITE_MESSAGEF(...)
Definition MsgHandler.h:290
#define WRITE_MESSAGE(msg)
Definition MsgHandler.h:289
#define WRITE_ERROR(msg)
Definition MsgHandler.h:296
#define WRITE_WARNING(msg)
Definition MsgHandler.h:287
#define TL(string)
Definition MsgHandler.h:305
const double SUMO_const_laneWidth
Definition StdDefs.h:48
T MAX2(T a, T b)
Definition StdDefs.h:82
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
A class that stores a 2D geometrical boundary.
Definition Boundary.h:39
static bool init(OptionsCont &oc)
Initialises the processing and the final instance using the given options.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
static void initOutputOptions()
init output options
int size() const
Returns the number of edges.
Definition NBEdgeCont.h:287
static void fillOptions(OptionsCont &oc, bool forNetgen)
Inserts options used by the network converter.
Definition NBFrame.cpp:48
static bool checkOptions(OptionsCont &oc)
Checks set options from the OptionsCont-singleton for being valid.
Definition NBFrame.cpp:755
Instance responsible for building networks.
NBNodeCont & getNodeCont()
Returns a reference to the node container.
NBEdgeCont & getEdgeCont()
NBTypeCont & getTypeCont()
Returns a reference to the type container.
NBTrafficLightLogicCont & getTLLogicCont()
Returns a reference to the traffic light logics container.
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
void compute(OptionsCont &oc, const std::set< std::string > &explicitTurnarounds=std::set< std::string >(), bool mayAddOrRemove=true)
Performs the network building steps.
int size() const
Returns the number of nodes stored in this container.
Definition NBNodeCont.h:307
void printBuiltNodesStatistics() const
Prints statistics about built nodes.
void discardTrafficLights(NBTrafficLightLogicCont &tlc, bool geometryLike)
int getNumExtracted() const
return the number of extracted traffic light definitions
static void fillOptions()
Inserts options used by the network generator.
Definition NGFrame.cpp:38
static bool checkOptions()
Checks set options from the OptionsCont-singleton for being valid.
Definition NGFrame.cpp:220
The class storing the generated network.
Definition NGNet.h:47
void toNB() const
Converts the stored network into its netbuilder-representation.
Definition NGNet.cpp:240
void createChequerBoard(int numX, int numY, double spaceX, double spaceY, double xAttachLength, double yAttachLength)
Creates a grid network.
Definition NGNet.cpp:93
void createSpiderWeb(int numRadDiv, int numCircles, double spaceRad, bool hasCenter, double attachLength)
Creates a spider network.
Definition NGNet.cpp:164
A class that builds random network using an algorithm by Markus Hartinger.
void createNet(int numNodes, bool gridMode)
Builds a NGNet using the set values.
static bool load(SUMOSAXHandler &handler, const std::vector< std::string > &files, const std::string &type, const bool stringParse=false)
Importer for edge type information stored in XML.
static void fillOptions(OptionsCont &oc, bool forNetgen)
Inserts options used by the network writer.
Definition NWFrame.cpp:49
static void writeNetwork(const OptionsCont &oc, NBNetBuilder &nb)
Writes the network stored in the given net builder.
Definition NWFrame.cpp:188
static bool checkOptions(OptionsCont &oc)
Checks set options for being valid.
Definition NWFrame.cpp:139
A storage for options typed value containers)
Definition OptionsCont.h:89
void setAdditionalHelpMessage(const std::string &add)
Sets an additional message to be printed at the begin of the help screen.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
int getInt(const std::string &name) const
Returns the int-value of the named option (only for Option_Integer)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
void resetWritable()
Resets all options to be writeable.
static OptionsCont & getOptions()
Retrieves the options.
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition OptionsIO.cpp:74
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
static void insertRandOptions(OptionsCont &oc)
Initialises the given options container with random number options.
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Represents a generic random distribution.
bool add(T val, double prob, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.
static void close()
Closes all of an applications subsystems.
static bool checkOptions(OptionsCont &oc)
checks shared options and sets StdDefs
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition XMLSubSys.cpp:83
static void init()
Initialises the xml-subsystem.
Definition XMLSubSys.cpp:56
bool checkOptions()
void fillOptions()
int main(int argc, char **argv)
NGNet * buildNetwork(NBNetBuilder &nb)
#define M_PI
Definition odrSpiral.cpp:45