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