SUMO - Simulation of Urban MObility
MSRoute.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A vehicle route
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2002-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include <algorithm>
36 #include <limits>
37 #include "MSRoute.h"
38 #include "MSEdge.h"
39 #include "MSLane.h"
41 #include <utils/common/RGBColor.h>
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // static member variables
52 // ===========================================================================
55 
56 
57 // ===========================================================================
58 // member method definitions
59 // ===========================================================================
60 MSRoute::MSRoute(const std::string& id,
61  const MSEdgeVector& edges,
62  const bool isPermanent, const RGBColor* const c,
63  const std::vector<SUMOVehicleParameter::Stop>& stops)
64  : Named(id), myEdges(edges), myAmPermanent(isPermanent),
65  myReferenceCounter(isPermanent ? 1 : 0),
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*>* const routeDist, const bool permanent) {
127  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
128  myDistDict[id] = std::make_pair(routeDist, permanent);
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.first->getOverallProb() == 0) {
141  return 0;
142  }
143  return it2->second.first->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.first;
156 }
157 
158 
159 void
161  for (RouteDistDict::iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
162  delete i->second.first;
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::checkDist(const std::string& id) {
174  RouteDistDict::iterator it = myDistDict.find(id);
175  if (it != myDistDict.end() && !it->second.second) {
176  const std::vector<const MSRoute*>& routes = it->second.first->getVals();
177  for (std::vector<const MSRoute*>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
178  (*i)->release();
179  }
180  delete it->second.first;
181  myDistDict.erase(it);
182  }
183 }
184 
185 
186 void
187 MSRoute::insertIDs(std::vector<std::string>& into) {
188  into.reserve(myDict.size() + myDistDict.size() + into.size());
189  for (RouteDict::const_iterator i = myDict.begin(); i != myDict.end(); ++i) {
190  into.push_back((*i).first);
191  }
192  for (RouteDistDict::const_iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
193  into.push_back((*i).first);
194  }
195 }
196 
197 
198 int
199 MSRoute::writeEdgeIDs(OutputDevice& os, const MSEdge* const from, const MSEdge* const upTo) const {
200  int numWritten = 0;
201  MSEdgeVector::const_iterator i = myEdges.begin();
202  if (from != 0) {
203  i = std::find(myEdges.begin(), myEdges.end(), from);
204  }
205  for (; i != myEdges.end(); ++i) {
206  if ((*i) == upTo) {
207  return numWritten;
208  }
209  os << (*i)->getID();
210  numWritten++;
211  if (upTo || i != myEdges.end() - 1) {
212  os << ' ';
213  }
214  }
215  return numWritten;
216 }
217 
218 
219 bool
220 MSRoute::containsAnyOf(const std::vector<MSEdge*>& edgelist) const {
221  std::vector<MSEdge*>::const_iterator i = edgelist.begin();
222  for (; i != edgelist.end(); ++i) {
223  if (contains(*i)) {
224  return true;
225  }
226  }
227  return false;
228 }
229 
230 
231 const MSEdge*
232 MSRoute::operator[](unsigned index) const {
233  return myEdges[index];
234 }
235 
236 
237 void
239  for (RouteDict::iterator it = myDict.begin(); it != myDict.end(); ++it) {
240  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, (*it).second->getID());
241  out.writeAttr(SUMO_ATTR_STATE, (*it).second->myAmPermanent);
242  out.writeAttr(SUMO_ATTR_EDGES, (*it).second->myEdges).closeTag();
243  }
244  for (RouteDistDict::iterator it = myDistDict.begin(); it != myDistDict.end(); ++it) {
246  out.writeAttr(SUMO_ATTR_STATE, (*it).second.second);
247  out.writeAttr(SUMO_ATTR_ROUTES, (*it).second.first->getVals());
248  out.writeAttr(SUMO_ATTR_PROBS, (*it).second.first->getProbs());
249  out.closeTag();
250  }
251 }
252 
253 
254 SUMOReal
256  SUMOReal ret = 0;
257  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
258  ret += (*i)->getLength();
259  }
260  return ret;
261 }
262 
263 
264 SUMOReal
265 MSRoute::getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge* fromEdge, const MSEdge* toEdge) const {
266  bool isFirstIteration = true;
267  SUMOReal distance = -fromPos;
268  MSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
269 
270  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
271  // start or destination not contained in route
273  }
274  if (fromEdge == toEdge) {
275  // destination position is on start edge
276  if (fromPos <= toPos) {
277  return toPos - fromPos;
278  } else if (std::find(it + 1, myEdges.end(), toEdge) == myEdges.end()) {
279  // we don't visit the edge again
281  }
282  }
283  for (; it != end(); ++it) {
284  if ((*it) == toEdge && !isFirstIteration) {
285  distance += toPos;
286  break;
287  } else {
288  const std::vector<MSLane*>& lanes = (*it)->getLanes();
289  distance += lanes[0]->getLength();
290 #ifdef HAVE_INTERNAL_LANES
291  // add length of internal lanes to the result
292  for (std::vector<MSLane*>::const_iterator laneIt = lanes.begin(); laneIt != lanes.end(); ++laneIt) {
293  const MSLinkCont& links = (*laneIt)->getLinkCont();
294  for (MSLinkCont::const_iterator linkIt = links.begin(); linkIt != links.end(); ++linkIt) {
295  if ((*linkIt) == 0 || (*linkIt)->getLane() == 0) {
296  continue;
297  }
298  std::string succLaneId = (*(it + 1))->getLanes()[0]->getID();
299  if ((*linkIt)->getLane()->getID().compare(succLaneId) == 0) {
300  distance += (*linkIt)->getLength();
301  }
302  }
303  }
304 #endif
305  }
306  isFirstIteration = false;
307  }
308  return distance;
309 }
310 
311 
312 const RGBColor&
314  if (myColor == 0) {
316  }
317  return *myColor;
318 }
319 
320 
321 const std::vector<SUMOVehicleParameter::Stop>&
323  return myStops;
324 }
325 
326 
327 /****************************************************************************/
328 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
std::map< std::string, const MSRoute * > RouteDict
Definition of the dictionary container.
Definition: MSRoute.h:214
int writeEdgeIDs(OutputDevice &os, const MSEdge *const from, const MSEdge *const upTo=0) const
Output the edge ids up to but not including the id of the given edge.
Definition: MSRoute.cpp:199
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:60
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:93
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:150
SUMOReal getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge *fromEdge, const MSEdge *toEdge) const
Compute the distance between 2 given edges on this route, including the length of internal lanes...
Definition: MSRoute.cpp:265
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:322
static void dict_saveState(OutputDevice &out)
Saves all known routes into the given stream.
Definition: MSRoute.cpp:238
The state of a link.
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:187
A road/street connecting two junctions.
Definition: MSEdge.h:74
#define max(a, b)
Definition: polyfonts.c:65
static void clear()
Clears the dictionary (delete all known routes, too)
Definition: MSRoute.cpp:160
the edges of a route
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:57
bool containsAnyOf(const std::vector< MSEdge * > &edgelist) const
Definition: MSRoute.cpp:220
static RouteDistDict myDistDict
The dictionary container.
Definition: MSRoute.h:223
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:199
MSEdgeVector myEdges
The list of edges to pass.
Definition: MSRoute.h:198
bool contains(const MSEdge *const edge) const
Definition: MSRoute.h:105
std::map< std::string, std::pair< RandomDistributor< const MSRoute * > *, bool > > RouteDistDict
Definition of the dictionary container.
Definition: MSRoute.h:220
virtual ~MSRoute()
Destructor.
Definition: MSRoute.cpp:69
SUMOReal getLength() const
Definition: MSRoute.cpp:255
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:100
std::vector< SUMOVehicleParameter::Stop > myStops
List of the stops on the parsed route.
Definition: MSRoute.h:210
Base class for objects which have an id.
Definition: Named.h:45
std::string myID
The name of the object.
Definition: Named.h:128
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:81
unsigned size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:87
const MSEdge * operator[](unsigned index) const
Definition: MSRoute.cpp:232
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:313
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
unsigned int myReferenceCounter
Information by how many vehicles the route is used.
Definition: MSRoute.h:204
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:106
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:173
static RouteDict myDict
The dictionary container.
Definition: MSRoute.h:217
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const RGBColor *const myColor
The color.
Definition: MSRoute.h:207
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:75
MSRoute(const std::string &id, const MSEdgeVector &edges, const bool isPermanent, const RGBColor *const c, const std::vector< SUMOVehicleParameter::Stop > &stops)
Constructor.
Definition: MSRoute.cpp:60
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116