SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSRoute.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A vehicle route
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cassert>
34 #include <algorithm>
35 #include <limits>
36 #include "MSRoute.h"
37 #include "MSEdge.h"
38 #include "MSLane.h"
40 #include <utils/common/RGBColor.h>
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // static member variables
51 // ===========================================================================
55 
56 
57 // ===========================================================================
58 // member method definitions
59 // ===========================================================================
60 MSRoute::MSRoute(const std::string& id,
61  const MSEdgeVector& edges,
62  unsigned int references, const RGBColor* const c,
63  const std::vector<SUMOVehicleParameter::Stop>& stops)
64  : Named(id), myEdges(edges),
65  myReferenceCounter(references),
66  myColor(c), myStops(stops) {}
67 
68 
70  delete myColor;
71 }
72 
73 
75 MSRoute::begin() const {
76  return myEdges.begin();
77 }
78 
79 
81 MSRoute::end() const {
82  return myEdges.end();
83 }
84 
85 
86 unsigned
87 MSRoute::size() const {
88  return (unsigned) myEdges.size();
89 }
90 
91 
92 const MSEdge*
94  assert(myEdges.size() > 0);
95  return myEdges[myEdges.size() - 1];
96 }
97 
98 
99 void
102 }
103 
104 
105 void
108  if (myReferenceCounter == 0) {
109  myDict.erase(myID);
110  delete this;
111  }
112 }
113 
114 
115 bool
116 MSRoute::dictionary(const std::string& id, const MSRoute* route) {
117  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
118  myDict[id] = route;
119  return true;
120  }
121  return false;
122 }
123 
124 
125 bool
126 MSRoute::dictionary(const std::string& id, RandomDistributor<const MSRoute*>* routeDist) {
127  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
128  myDistDict[id] = routeDist;
129  return true;
130  }
131  return false;
132 }
133 
134 
135 const MSRoute*
136 MSRoute::dictionary(const std::string& id) {
137  RouteDict::iterator it = myDict.find(id);
138  if (it == myDict.end()) {
139  RouteDistDict::iterator it2 = myDistDict.find(id);
140  if (it2 == myDistDict.end() || it2->second->getOverallProb() == 0) {
141  return 0;
142  }
143  return it2->second->get();
144  }
145  return it->second;
146 }
147 
148 
150 MSRoute::distDictionary(const std::string& id) {
151  RouteDistDict::iterator it2 = myDistDict.find(id);
152  if (it2 == myDistDict.end()) {
153  return 0;
154  }
155  return it2->second;
156 }
157 
158 
159 void
161  for (RouteDistDict::iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
162  delete i->second;
163  }
164  myDistDict.clear();
165  for (RouteDict::iterator i = myDict.begin(); i != myDict.end(); ++i) {
166  delete i->second;
167  }
168  myDict.clear();
169 }
170 
171 
172 void
173 MSRoute::insertIDs(std::vector<std::string>& into) {
174  into.reserve(myDict.size() + myDistDict.size() + into.size());
175  for (RouteDict::const_iterator i = myDict.begin(); i != myDict.end(); ++i) {
176  into.push_back((*i).first);
177  }
178  for (RouteDistDict::const_iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
179  into.push_back((*i).first);
180  }
181 }
182 
183 
184 int
185 MSRoute::writeEdgeIDs(OutputDevice& os, const MSEdge* const from, const MSEdge* const upTo) const {
186  int numWritten = 0;
187  MSEdgeVector::const_iterator i = myEdges.begin();
188  if (from != 0) {
189  i = std::find(myEdges.begin(), myEdges.end(), from);
190  }
191  for (; i != myEdges.end(); ++i) {
192  if ((*i) == upTo) {
193  return numWritten;
194  }
195  os << (*i)->getID();
196  numWritten++;
197  if (upTo || i != myEdges.end() - 1) {
198  os << ' ';
199  }
200  }
201  return numWritten;
202 }
203 
204 
205 bool
206 MSRoute::containsAnyOf(const std::vector<MSEdge*>& edgelist) const {
207  std::vector<MSEdge*>::const_iterator i = edgelist.begin();
208  for (; i != edgelist.end(); ++i) {
209  if (contains(*i)) {
210  return true;
211  }
212  }
213  return false;
214 }
215 
216 
217 const MSEdge*
218 MSRoute::operator[](unsigned index) const {
219  return myEdges[index];
220 }
221 
222 
223 #ifdef HAVE_INTERNAL
224 void
225 MSRoute::dict_saveState(std::ostream& os) {
226  FileHelpers::writeUInt(os, (unsigned int) myDict.size());
227  for (RouteDict::iterator it = myDict.begin(); it != myDict.end(); ++it) {
228  FileHelpers::writeString(os, (*it).second->getID());
229  FileHelpers::writeUInt(os, (*it).second->myReferenceCounter);
230  FileHelpers::writeEdgeVector(os, (*it).second->myEdges);
231  }
232  FileHelpers::writeUInt(os, (unsigned int) myDistDict.size());
233  for (RouteDistDict::iterator it = myDistDict.begin(); it != myDistDict.end(); ++it) {
234  FileHelpers::writeString(os, (*it).first);
235  const unsigned int size = (unsigned int)(*it).second->getVals().size();
236  FileHelpers::writeUInt(os, size);
237  for (unsigned int i = 0; i < size; ++i) {
238  FileHelpers::writeString(os, (*it).second->getVals()[i]->getID());
239  FileHelpers::writeFloat(os, (*it).second->getProbs()[i]);
240  }
241  }
242 }
243 
244 
245 void
246 MSRoute::dict_loadState(BinaryInputDevice& bis) {
247  unsigned int numRoutes;
248  bis >> numRoutes;
249  for (; numRoutes > 0; numRoutes--) {
250  std::string id;
251  bis >> id;
252  unsigned int references;
253  bis >> references;
254  MSEdgeVector edges;
255  FileHelpers::readEdgeVector(bis.getIStream(), edges, id);
256  if (dictionary(id) == 0) {
257  MSRoute* r = new MSRoute(id, edges, references,
258  0, std::vector<SUMOVehicleParameter::Stop>());
259  dictionary(id, r);
260  }
261  }
262  unsigned int numRouteDists;
263  bis >> numRouteDists;
264  for (; numRouteDists > 0; numRouteDists--) {
265  std::string id;
266  bis >> id;
267  unsigned int no;
268  bis >> no;
269  if (dictionary(id) == 0) {
271  for (; no > 0; no--) {
272  std::string routeID;
273  bis >> routeID;
274  const MSRoute* r = dictionary(routeID);
275  assert(r != 0);
276  SUMOReal prob;
277  bis >> prob;
278  dist->add(prob, r, false);
279  }
280  dictionary(id, dist);
281  } else {
282  for (; no > 0; no--) {
283  std::string routeID;
284  bis >> routeID;
285  SUMOReal prob;
286  bis >> prob;
287  }
288  }
289  }
290  WRITE_MESSAGE(" " + toString(myDict.size()) + " routes");
291  WRITE_MESSAGE(" " + toString(myDistDict.size()) + " route distributions");
292 }
293 #endif
294 
295 
296 SUMOReal
298  SUMOReal ret = 0;
299  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
300  ret += (*i)->getLength();
301  }
302  return ret;
303 }
304 
305 
306 SUMOReal
307 MSRoute::getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge* fromEdge, const MSEdge* toEdge) const {
308  bool isFirstIteration = true;
309  SUMOReal distance = -fromPos;
310  MSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
311 
312  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
313  // start or destination not contained in route
315  }
316  if (fromEdge == toEdge && fromPos <= toPos) {
317  // destination position is on start edge
318  return (toPos - fromPos);
319  }
320  for (; it != end(); ++it) {
321  if ((*it) == toEdge && !isFirstIteration) {
322  distance += toPos;
323  break;
324  } else {
325  const std::vector<MSLane*>& lanes = (*it)->getLanes();
326  distance += lanes[0]->getLength();
327 #ifdef HAVE_INTERNAL_LANES
328  // add length of internal lanes to the result
329  for (std::vector<MSLane*>::const_iterator laneIt = lanes.begin(); laneIt != lanes.end(); laneIt++) {
330  const MSLinkCont& links = (*laneIt)->getLinkCont();
331  for (MSLinkCont::const_iterator linkIt = links.begin(); linkIt != links.end(); linkIt++) {
332  if ((*linkIt) == 0 || (*linkIt)->getLane() == 0) {
333  continue;
334  }
335  std::string succLaneId = (*(it + 1))->getLanes()[0]->getID();
336  if ((*linkIt)->getLane()->getID().compare(succLaneId) == 0) {
337  distance += (*linkIt)->getLength();
338  }
339  }
340  }
341 #endif
342  }
343  isFirstIteration = false;
344  }
345  return distance;
346 }
347 
348 
349 const RGBColor&
351  if (myColor == 0) {
353  }
354  return *myColor;
355 }
356 
357 
358 const std::vector<SUMOVehicleParameter::Stop>&
360  return myStops;
361 }
362 
363 
364 /****************************************************************************/
365