SUMO - Simulation of Urban MObility

netgen_main.cpp

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