SUMO - Simulation of Urban MObility

netgen_main.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00010 // Main for NETGEN
00011 /****************************************************************************/
00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00014 /****************************************************************************/
00015 //
00016 //   This file is part of SUMO.
00017 //   SUMO is free software: you can redistribute it and/or modify
00018 //   it under the terms of the GNU General Public License as published by
00019 //   the Free Software Foundation, either version 3 of the License, or
00020 //   (at your option) any later version.
00021 //
00022 /****************************************************************************/
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #ifdef HAVE_VERSION_H
00035 #include <version.h>
00036 #endif
00037 
00038 #include <iostream>
00039 #include <fstream>
00040 #include <string>
00041 #include <ctime>
00042 #include <netgen/NGNet.h>
00043 #include <netgen/NGRandomNetBuilder.h>
00044 #include <netgen/NGFrame.h>
00045 #include <netbuild/NBNetBuilder.h>
00046 #include <netbuild/NBFrame.h>
00047 #include <netwrite/NWFrame.h>
00048 #include <utils/options/OptionsCont.h>
00049 #include <utils/options/OptionsIO.h>
00050 #include <utils/options/Option.h>
00051 #include <utils/common/MsgHandler.h>
00052 #include <utils/common/SystemFrame.h>
00053 #include <utils/common/UtilExceptions.h>
00054 #include <utils/common/RandHelper.h>
00055 #include <utils/common/ToString.h>
00056 #include <utils/geom/GeoConvHelper.h>
00057 #include <utils/xml/XMLSubSys.h>
00058 #include <utils/iodevices/OutputDevice.h>
00059 
00060 #ifdef CHECK_MEMORY_LEAKS
00061 #include <foreign/nvwa/debug_new.h>
00062 #endif // CHECK_MEMORY_LEAKS
00063 
00064 
00065 // ===========================================================================
00066 // method definitions
00067 // ===========================================================================
00068 void
00069 fillOptions() {
00070     OptionsCont& oc = OptionsCont::getOptions();
00071     oc.addCallExample("-c <CONFIGURATION>");
00072     oc.addCallExample("--grid [grid-network options] -o <OUTPUTFILE>");
00073     oc.addCallExample("--spider [spider-network options] -o <OUTPUTFILE>");
00074     oc.addCallExample("--rand [random-network options] -o <OUTPUTFILE>");
00075 
00076     oc.setAdditionalHelpMessage(" Either \"--grid\", \"--spider\" or \"--rand\" must be supplied.\n  In dependance to these switches other options are used.");
00077 
00078     // insert options sub-topics
00079     SystemFrame::addConfigurationOptions(oc); // this subtopic is filled here, too
00080     oc.addOptionSubTopic("Grid Network");
00081     oc.addOptionSubTopic("Spider Network");
00082     oc.addOptionSubTopic("Random Network");
00083     oc.addOptionSubTopic("Output");
00084     oc.addOptionSubTopic("TLS Building");
00085     //oc.addOptionSubTopic("Ramp Guessing");
00086     oc.addOptionSubTopic("Edge Removal");
00087     oc.addOptionSubTopic("Unregulated Nodes");
00088     oc.addOptionSubTopic("Processing");
00089     oc.addOptionSubTopic("Building Defaults");
00090     SystemFrame::addReportOptions(oc); // this subtopic is filled here, too
00091 
00092     NGFrame::fillOptions();
00093     NBFrame::fillOptions(true);
00094     NWFrame::fillOptions(true);
00095     oc.doRegister("default-junction-type", 'j', new Option_String());
00096     oc.addSynonyme("default-junction-type", "junctions");
00097     oc.addDescription("default-junction-type", "Building Defaults", "[traffic_light|priority|right_before_left] Determines the type of the build junctions");
00098     RandHelper::insertRandOptions();
00099 }
00100 
00101 
00102 bool
00103 checkOptions() {
00104     bool ok = NGFrame::checkOptions();
00105     ok &= NBFrame::checkOptions();
00106     ok &= NWFrame::checkOptions();
00107     return ok;
00108 }
00109 
00110 
00111 NGNet*
00112 buildNetwork(NBNetBuilder& nb) {
00113     OptionsCont& oc = OptionsCont::getOptions();
00114     // spider-net
00115     if (oc.getBool("spider")) {
00116         // check values
00117         bool hadError = false;
00118         if (oc.getInt("spider.arm-number") < 3) {
00119             WRITE_ERROR("Spider networks need at least 3 arms.");
00120             hadError = true;
00121         }
00122         if (oc.getInt("spider.circle-number") < 1) {
00123             WRITE_ERROR("Spider networks need at least one circle.");
00124             hadError = true;
00125         }
00126         if (oc.getFloat("spider.space-radius") < 10) {
00127             WRITE_ERROR("The radius of spider networks must be at least 10m.");
00128             hadError = true;
00129         }
00130         if (hadError) {
00131             throw ProcessError();
00132         }
00133         // build if everything's ok
00134         NGNet* net = new NGNet(nb);
00135         net->createSpiderWeb(oc.getInt("spider.arm-number"), oc.getInt("spider.circle-number"),
00136                              oc.getFloat("spider.space-radius"), !oc.getBool("spider.omit-center"));
00137         return net;
00138     }
00139     // grid-net
00140     if (oc.getBool("grid")) {
00141         // get options
00142         int xNo = oc.getInt("grid.x-number");
00143         int yNo = oc.getInt("grid.y-number");
00144         SUMOReal xLength = oc.getFloat("grid.x-length");
00145         SUMOReal yLength = oc.getFloat("grid.y-length");
00146         SUMOReal attachLength = oc.getFloat("grid.attach-length");
00147         if (oc.isDefault("grid.x-number") && !oc.isDefault("grid.number")) {
00148             xNo = oc.getInt("grid.number");
00149         }
00150         if (oc.isDefault("grid.y-number") && !oc.isDefault("grid.number")) {
00151             yNo = oc.getInt("grid.number");
00152         }
00153         if (oc.isDefault("grid.x-length") && !oc.isDefault("grid.length")) {
00154             xLength = oc.getFloat("grid.length");
00155         }
00156         if (oc.isDefault("grid.y-length") && !oc.isDefault("grid.length")) {
00157             yLength = oc.getFloat("grid.length");
00158         }
00159         // check values
00160         bool hadError = false;
00161         if (xNo < 2 || yNo < 2) {
00162             WRITE_ERROR("The number of nodes must be at least 2 in both directions.");
00163             hadError = true;
00164         }
00165         if (xLength < 10. || yLength < 10.) {
00166             WRITE_ERROR("The distance between nodes must be at least 10m in both directions.");
00167             hadError = true;
00168         }
00169         if (attachLength != 0.0 && attachLength < 10.) {
00170             WRITE_ERROR("The length of attached streets must be at least 10m.");
00171             hadError = true;
00172         }
00173         if (hadError) {
00174             throw ProcessError();
00175         }
00176         // build if everything's ok
00177         NGNet* net = new NGNet(nb);
00178         net->createChequerBoard(xNo, yNo, xLength, yLength, attachLength);
00179         return net;
00180     }
00181     // random net
00182     TNeighbourDistribution neighborDist;
00183     neighborDist.add(1, oc.getFloat("rand.neighbor-dist1"));
00184     neighborDist.add(2, oc.getFloat("rand.neighbor-dist2"));
00185     neighborDist.add(3, oc.getFloat("rand.neighbor-dist3"));
00186     neighborDist.add(4, oc.getFloat("rand.neighbor-dist4"));
00187     neighborDist.add(5, oc.getFloat("rand.neighbor-dist5"));
00188     neighborDist.add(6, oc.getFloat("rand.neighbor-dist6"));
00189     NGNet* net = new NGNet(nb);
00190     NGRandomNetBuilder randomNet(*net,
00191                                  oc.getFloat("rand.min-angle"),
00192                                  oc.getFloat("rand.min-distance"),
00193                                  oc.getFloat("rand.max-distance"),
00194                                  oc.getFloat("rand.connectivity"),
00195                                  oc.getInt("rand.num-tries"),
00196                                  neighborDist);
00197     randomNet.createNet(oc.getInt("rand.iterations"));
00198     return net;
00199 }
00200 
00201 
00202 
00203 int
00204 main(int argc, char** argv) {
00205     OptionsCont& oc = OptionsCont::getOptions();
00206     // give some application descriptions
00207     oc.setApplicationDescription("Road network generator for the microscopic road traffic simulation SUMO.");
00208     oc.setApplicationName("netgen", "SUMO netgen Version " + (std::string)VERSION_STRING);
00209     int ret = 0;
00210     try {
00211         // initialise the application system (messaging, xml, options)
00212         XMLSubSys::init(false);
00213         fillOptions();
00214         OptionsIO::getOptions(true, argc, argv);
00215         if (oc.processMetaOptions(argc < 2)) {
00216             SystemFrame::close();
00217             return 0;
00218         }
00219         MsgHandler::initOutputOptions();
00220         if (!checkOptions()) {
00221             throw ProcessError();
00222         }
00223         RandHelper::initRandGlobal();
00224         NBNetBuilder nb;
00225         nb.applyOptions(oc);
00226         // build the netgen-network description
00227         NGNet* net = buildNetwork(nb);
00228         // ... and we have to do this...
00229         oc.resetWritable();
00230         // transfer to the netbuilding structures
00231         net->toNB();
00232         delete net;
00233         // report generated structures
00234         WRITE_MESSAGE(" Generation done;");
00235         WRITE_MESSAGE("   " + toString<int>(nb.getNodeCont().size()) + " nodes generated.");
00236         WRITE_MESSAGE("   " + toString<int>(nb.getEdgeCont().size()) + " edges generated.");
00237         nb.compute(oc);
00238         NWFrame::writeNetwork(oc, nb);
00239     } catch (ProcessError& e) {
00240         if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
00241             WRITE_ERROR(e.what());
00242         }
00243         MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00244         ret = 1;
00245 #ifndef _DEBUG
00246     } catch (...) {
00247         MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
00248         ret = 1;
00249 #endif
00250     }
00251     OutputDevice::closeAll();
00252     SystemFrame::close();
00253     if (ret == 0) {
00254         std::cout << "Success." << std::endl;
00255     }
00256     return ret;
00257 }
00258 
00259 
00260 
00261 /****************************************************************************/
00262 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines