1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    NIImporter_RobocupRescue.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    Mon, 14.04.2008
15 /// @version $Id$
16 ///
17 // Importer for networks stored in robocup rescue league format
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 #include <string>
26 #include <utils/xml/SUMOSAXHandler.h>
27 #include <utils/common/UtilExceptions.h>
28 #include <utils/common/StringUtils.h>
29 #include <utils/common/ToString.h>
30 #include <utils/common/MsgHandler.h>
31 #include <netbuild/NBEdge.h>
32 #include <netbuild/NBEdgeCont.h>
33 #include <netbuild/NBNode.h>
34 #include <netbuild/NBNodeCont.h>
35 #include <netbuild/NBNetBuilder.h>
36 #include <utils/xml/SUMOXMLDefinitions.h>
37 #include <utils/geom/GeoConvHelper.h>
38 #include <utils/geom/GeomConvHelper.h>
39 #include <utils/options/OptionsCont.h>
40 #include <utils/common/FileHelpers.h>
41 #include <utils/xml/XMLSubSys.h>
42 #include <utils/iodevices/BinaryInputDevice.h>
43 #include "NILoader.h"
44 #include "NIImporter_RobocupRescue.h"
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 // ---------------------------------------------------------------------------
51 // static methods (interface in this case)
52 // ---------------------------------------------------------------------------
53 void
loadNetwork(const OptionsCont & oc,NBNetBuilder & nb)54 NIImporter_RobocupRescue::loadNetwork(const OptionsCont& oc, NBNetBuilder& nb) {
55     // check whether the option is set (properly)
56     if (!oc.isSet("robocup-dir")) {
57         return;
58     }
59     // build the handler
60     NIImporter_RobocupRescue handler(nb.getNodeCont(), nb.getEdgeCont());
61     // parse file(s)
62     std::vector<std::string> files = oc.getStringVector("robocup-dir");
63     for (std::vector<std::string>::const_iterator file = files.begin(); file != files.end(); ++file) {
64         // nodes
65         std::string nodesName = (*file) + "/node.bin";
66         if (!FileHelpers::isReadable(nodesName)) {
67             WRITE_ERROR("Could not open robocup-node-file '" + nodesName + "'.");
68             return;
69         }
70         PROGRESS_BEGIN_MESSAGE("Parsing robocup-nodes from '" + nodesName + "'");
71         handler.loadNodes(nodesName);
72         PROGRESS_DONE_MESSAGE();
73         // edges
74         std::string edgesName = (*file) + "/road.bin";
75         if (!FileHelpers::isReadable(edgesName)) {
76             WRITE_ERROR("Could not open robocup-road-file '" + edgesName + "'.");
77             return;
78         }
79         PROGRESS_BEGIN_MESSAGE("Parsing robocup-roads from '" + edgesName + "'");
80         handler.loadEdges(edgesName);
81         PROGRESS_DONE_MESSAGE();
82     }
83 }
84 
85 
86 
87 // ---------------------------------------------------------------------------
88 // loader methods
89 // ---------------------------------------------------------------------------
NIImporter_RobocupRescue(NBNodeCont & nc,NBEdgeCont & ec)90 NIImporter_RobocupRescue::NIImporter_RobocupRescue(NBNodeCont& nc, NBEdgeCont& ec)
91     : myNodeCont(nc), myEdgeCont(ec) {}
92 
93 
~NIImporter_RobocupRescue()94 NIImporter_RobocupRescue::~NIImporter_RobocupRescue() {
95 }
96 
97 
98 void
loadNodes(const std::string & file)99 NIImporter_RobocupRescue::loadNodes(const std::string& file) {
100     BinaryInputDevice dev(file);
101     int skip;
102     dev >> skip; // the number in 19_s
103     dev >> skip; // x-offset in 19_s
104     dev >> skip; // y-offset in 19_s
105     //
106     int noNodes;
107     dev >> noNodes;
108     WRITE_MESSAGE("Expected node number: " + toString(noNodes));
109     do {
110         //cout << "  left " << (noNodes) << endl;
111         int entrySize, id, posX, posY, numEdges;
112         dev >> entrySize;
113         entrySize /= 4;
114         dev >> id;
115         dev >> posX;
116         dev >> posY;
117         dev >> numEdges;
118 
119         std::vector<int> edges;
120         for (int j = 0; j < numEdges; ++j) {
121             int edge;
122             dev >> edge;
123             edges.push_back(edge);
124         }
125 
126         int signal;
127         dev >> signal;
128 
129         std::vector<int> turns;
130         for (int j = 0; j < numEdges; ++j) {
131             int turn;
132             dev >> turn;
133             turns.push_back(turn);
134         }
135 
136         std::vector<std::pair<int, int> > conns;
137         for (int j = 0; j < numEdges; ++j) {
138             int connF, connT;
139             dev >> connF;
140             dev >> connT;
141             conns.push_back(std::pair<int, int>(connF, connT));
142         }
143 
144         std::vector<std::vector<int> > times;
145         for (int j = 0; j < numEdges; ++j) {
146             int t1, t2, t3;
147             dev >> t1;
148             dev >> t2;
149             dev >> t3;
150             std::vector<int> time;
151             time.push_back(t1);
152             time.push_back(t2);
153             time.push_back(t3);
154             times.push_back(time);
155         }
156 
157         Position pos((double)(posX / 1000.), -(double)(posY / 1000.));
158         NBNetBuilder::transformCoordinate(pos);
159         NBNode* node = new NBNode(toString(id), pos);
160         myNodeCont.insert(node);
161         --noNodes;
162     } while (noNodes != 0);
163 }
164 
165 
166 void
loadEdges(const std::string & file)167 NIImporter_RobocupRescue::loadEdges(const std::string& file) {
168     BinaryInputDevice dev(file);
169     int skip;
170     dev >> skip; // the number in 19_s
171     dev >> skip; // x-offset in 19_s
172     dev >> skip; // y-offset in 19_s
173     //
174     int noEdges;
175     dev >> noEdges;
176     std::cout << "Expected edge number: " << noEdges << std::endl;
177     do {
178         std::cout << "  left " << (noEdges) << std::endl;
179         int entrySize, id, begNode, endNode, length, roadKind, carsToHead,
180             carsToTail, humansToHead, humansToTail, width, block, repairCost, median,
181             linesToHead, linesToTail, widthForWalkers;
182         dev >> entrySize >> id >> begNode >> endNode >> length >> roadKind >> carsToHead
183             >> carsToTail >> humansToHead >> humansToTail >> width >> block >> repairCost
184             >> median >> linesToHead >> linesToTail >> widthForWalkers;
185         NBNode* fromNode = myNodeCont.retrieve(toString(begNode));
186         NBNode* toNode = myNodeCont.retrieve(toString(endNode));
187         double speed = (double)(50. / 3.6);
188         int priority = -1;
189         LaneSpreadFunction spread = linesToHead > 0 && linesToTail > 0 ? LANESPREAD_RIGHT : LANESPREAD_CENTER;
190         if (linesToHead > 0) {
191             NBEdge* edge = new NBEdge(toString(id), fromNode, toNode, "", speed, linesToHead, priority, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET, "", spread);
192             if (!myEdgeCont.insert(edge)) {
193                 WRITE_ERROR("Could not insert edge '" + toString(id) + "'");
194             }
195         }
196         if (linesToTail > 0) {
197             NBEdge* edge = new NBEdge("-" + toString(id), toNode, fromNode, "", speed, linesToTail, priority, NBEdge::UNSPECIFIED_WIDTH, NBEdge::UNSPECIFIED_OFFSET, "", spread);
198             if (!myEdgeCont.insert(edge)) {
199                 WRITE_ERROR("Could not insert edge '-" + toString(id) + "'");
200             }
201         }
202         --noEdges;
203     } while (noEdges != 0);
204 }
205 
206 
207 /****************************************************************************/
208 
209