SUMO - Simulation of Urban MObility

NIImporter_RobocupRescue.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Importer for networks stored in robocup rescue league format
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 #include <string>
00030 #include <utils/xml/SUMOSAXHandler.h>
00031 #include <utils/common/UtilExceptions.h>
00032 #include <utils/common/TplConvert.h>
00033 #include <utils/common/ToString.h>
00034 #include <utils/common/MsgHandler.h>
00035 #include <netbuild/NBEdge.h>
00036 #include <netbuild/NBEdgeCont.h>
00037 #include <netbuild/NBNode.h>
00038 #include <netbuild/NBNodeCont.h>
00039 #include <netbuild/NBNetBuilder.h>
00040 #include <utils/xml/SUMOXMLDefinitions.h>
00041 #include <utils/geom/GeoConvHelper.h>
00042 #include <utils/geom/GeomConvHelper.h>
00043 #include <utils/options/OptionsCont.h>
00044 #include <utils/common/FileHelpers.h>
00045 #include <utils/xml/XMLSubSys.h>
00046 #include <utils/iodevices/BinaryInputDevice.h>
00047 #include "NILoader.h"
00048 #include "NIImporter_RobocupRescue.h"
00049 
00050 #ifdef CHECK_MEMORY_LEAKS
00051 #include <foreign/nvwa/debug_new.h>
00052 #endif // CHECK_MEMORY_LEAKS
00053 
00054 
00055 // ===========================================================================
00056 // method definitions
00057 // ===========================================================================
00058 // ---------------------------------------------------------------------------
00059 // static methods (interface in this case)
00060 // ---------------------------------------------------------------------------
00061 void
00062 NIImporter_RobocupRescue::loadNetwork(const OptionsCont& oc, NBNetBuilder& nb) {
00063     // check whether the option is set (properly)
00064     if (!oc.isSet("robocup-dir")) {
00065         return;
00066     }
00067     // build the handler
00068     NIImporter_RobocupRescue handler(nb.getNodeCont(), nb.getEdgeCont());
00069     // parse file(s)
00070     std::vector<std::string> files = oc.getStringVector("robocup-dir");
00071     for (std::vector<std::string>::const_iterator file=files.begin(); file!=files.end(); ++file) {
00072         // nodes
00073         std::string nodesName = (*file) + "/node.bin";
00074         if (!FileHelpers::exists(nodesName)) {
00075             WRITE_ERROR("Could not open robocup-node-file '" + nodesName + "'.");
00076             return;
00077         }
00078         PROGRESS_BEGIN_MESSAGE("Parsing robocup-nodes from '" + nodesName + "'");
00079         handler.loadNodes(nodesName);
00080         PROGRESS_DONE_MESSAGE();
00081         // edges
00082         std::string edgesName = (*file) + "/road.bin";
00083         if (!FileHelpers::exists(edgesName)) {
00084             WRITE_ERROR("Could not open robocup-road-file '" + edgesName + "'.");
00085             return;
00086         }
00087         PROGRESS_BEGIN_MESSAGE("Parsing robocup-roads from '" + edgesName + "'");
00088         handler.loadEdges(edgesName);
00089         PROGRESS_DONE_MESSAGE();
00090     }
00091 }
00092 
00093 
00094 
00095 // ---------------------------------------------------------------------------
00096 // loader methods
00097 // ---------------------------------------------------------------------------
00098 NIImporter_RobocupRescue::NIImporter_RobocupRescue(NBNodeCont& nc, NBEdgeCont& ec)
00099     : myNodeCont(nc), myEdgeCont(ec) {}
00100 
00101 
00102 NIImporter_RobocupRescue::~NIImporter_RobocupRescue() throw() {
00103 }
00104 
00105 
00106 void
00107 NIImporter_RobocupRescue::loadNodes(const std::string& file) {
00108     BinaryInputDevice dev(file);
00109     unsigned int skip;
00110     dev >> skip; // the number in 19_s
00111     dev >> skip; // x-offset in 19_s
00112     dev >> skip; // y-offset in 19_s
00113     //
00114     unsigned int noNodes;
00115     dev >> noNodes;
00116     WRITE_MESSAGE("Expected node number: " + toString(noNodes));
00117     do {
00118         //cout << "  left " << (noNodes) << endl;
00119         unsigned int entrySize, id, posX, posY, numEdges;
00120         dev >> entrySize;
00121         entrySize /= 4;
00122         dev >> id;
00123         dev >> posX;
00124         dev >> posY;
00125         dev >> numEdges;
00126 
00127         std::vector<int> edges;
00128         for (unsigned int j=0; j<numEdges; ++j) {
00129             unsigned int edge;
00130             dev >> edge;
00131             edges.push_back(edge);
00132         }
00133 
00134         unsigned int signal;
00135         dev >> signal;
00136 
00137         std::vector<int> turns;
00138         for (unsigned int j=0; j<numEdges; ++j) {
00139             unsigned int turn;
00140             dev >> turn;
00141             turns.push_back(turn);
00142         }
00143 
00144         std::vector<std::pair<int, int> > conns;
00145         for (unsigned int j=0; j<numEdges; ++j) {
00146             unsigned int connF, connT;
00147             dev >> connF;
00148             dev >> connT;
00149             conns.push_back(std::pair<int, int>(connF, connT));
00150         }
00151 
00152         std::vector<std::vector<int> > times;
00153         for (unsigned int j=0; j<numEdges; ++j) {
00154             unsigned int t1, t2, t3;
00155             dev >> t1;
00156             dev >> t2;
00157             dev >> t3;
00158             std::vector<int> time;
00159             time.push_back(t1);
00160             time.push_back(t2);
00161             time.push_back(t3);
00162             times.push_back(time);
00163         }
00164 
00165         Position pos((SUMOReal)(posX / 1000.), -(SUMOReal)(posY / 1000.));
00166         NILoader::transformCoordinates(pos);
00167         NBNode* node = new NBNode(toString(id), pos);
00168         myNodeCont.insert(node);
00169         --noNodes;
00170     } while (noNodes!=0);
00171 }
00172 
00173 
00174 void
00175 NIImporter_RobocupRescue::loadEdges(const std::string& file) {
00176     BinaryInputDevice dev(file);
00177     unsigned int skip;
00178     dev >> skip; // the number in 19_s
00179     dev >> skip; // x-offset in 19_s
00180     dev >> skip; // y-offset in 19_s
00181     //
00182     unsigned int noEdges;
00183     dev >> noEdges;
00184     std::cout << "Expected edge number: " << noEdges << std::endl;
00185     do {
00186         std::cout << "  left " << (noEdges) << std::endl;
00187         unsigned int entrySize, id, begNode, endNode, length, roadKind, carsToHead,
00188                  carsToTail, humansToHead, humansToTail, width, block, repairCost, median,
00189                  linesToHead, linesToTail, widthForWalkers;
00190         dev >> entrySize >> id >> begNode >> endNode >> length >> roadKind >> carsToHead
00191             >> carsToTail >> humansToHead >> humansToTail >> width >> block >> repairCost
00192             >> median >> linesToHead >> linesToTail >> widthForWalkers;
00193         NBNode* fromNode = myNodeCont.retrieve(toString(begNode));
00194         NBNode* toNode = myNodeCont.retrieve(toString(endNode));
00195         SUMOReal speed = (SUMOReal)(50. / 3.6);
00196         int priority = -1;
00197         LaneSpreadFunction spread = linesToHead>0&&linesToTail>0 ? LANESPREAD_RIGHT : LANESPREAD_CENTER;
00198         if (linesToHead>0) {
00199             NBEdge* edge = new NBEdge(toString(id), fromNode, toNode, "",
00200                                       speed, linesToHead, priority, -1, -1, "", spread);
00201             if (!myEdgeCont.insert(edge)) {
00202                 WRITE_ERROR("Could not insert edge '" + toString(id) + "'");
00203             }
00204         }
00205         if (linesToTail>0) {
00206             NBEdge* edge = new NBEdge("-" + toString(id), toNode, fromNode, "",
00207                                       speed, linesToTail, priority, -1, -1, "", spread);
00208             if (!myEdgeCont.insert(edge)) {
00209                 WRITE_ERROR("Could not insert edge '-" + toString(id) + "'");
00210             }
00211         }
00212         --noEdges;
00213     } while (noEdges!=0);
00214 }
00215 
00216 
00217 /****************************************************************************/
00218 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines