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-sim.org/
12 // Copyright (C) 2001-2013 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 void
225  for (RouteDict::iterator it = myDict.begin(); it != myDict.end(); ++it) {
226  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, (*it).second->getID());
227  out.writeAttr(SUMO_ATTR_STATE, (*it).second->myReferenceCounter);
228  out.writeAttr(SUMO_ATTR_EDGES, (*it).second->myEdges).closeTag();
229  }
230  for (RouteDistDict::iterator it = myDistDict.begin(); it != myDistDict.end(); ++it) {
232  out.writeAttr(SUMO_ATTR_ROUTES, (*it).second->getVals());
233  out.writeAttr(SUMO_ATTR_PROBS, (*it).second->getProbs());
234  out.closeTag();
235  }
236 }
237 
238 
239 SUMOReal
241  SUMOReal ret = 0;
242  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
243  ret += (*i)->getLength();
244  }
245  return ret;
246 }
247 
248 
249 SUMOReal
250 MSRoute::getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge* fromEdge, const MSEdge* toEdge) const {
251  bool isFirstIteration = true;
252  SUMOReal distance = -fromPos;
253  MSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
254 
255  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
256  // start or destination not contained in route
258  }
259  if (fromEdge == toEdge && fromPos <= toPos) {
260  // destination position is on start edge
261  return (toPos - fromPos);
262  }
263  for (; it != end(); ++it) {
264  if ((*it) == toEdge && !isFirstIteration) {
265  distance += toPos;
266  break;
267  } else {
268  const std::vector<MSLane*>& lanes = (*it)->getLanes();
269  distance += lanes[0]->getLength();
270 #ifdef HAVE_INTERNAL_LANES
271  // add length of internal lanes to the result
272  for (std::vector<MSLane*>::const_iterator laneIt = lanes.begin(); laneIt != lanes.end(); ++laneIt) {
273  const MSLinkCont& links = (*laneIt)->getLinkCont();
274  for (MSLinkCont::const_iterator linkIt = links.begin(); linkIt != links.end(); ++linkIt) {
275  if ((*linkIt) == 0 || (*linkIt)->getLane() == 0) {
276  continue;
277  }
278  std::string succLaneId = (*(it + 1))->getLanes()[0]->getID();
279  if ((*linkIt)->getLane()->getID().compare(succLaneId) == 0) {
280  distance += (*linkIt)->getLength();
281  }
282  }
283  }
284 #endif
285  }
286  isFirstIteration = false;
287  }
288  return distance;
289 }
290 
291 
292 const RGBColor&
294  if (myColor == 0) {
296  }
297  return *myColor;
298 }
299 
300 
301 const std::vector<SUMOVehicleParameter::Stop>&
303  return myStops;
304 }
305 
306 
307 /****************************************************************************/
308 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
std::map< std::string, const MSRoute * > RouteDict
Definition of the dictionary container.
Definition: MSRoute.h:219
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:185
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:59
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:250
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:302
static void dict_saveState(OutputDevice &out)
Saves all known routes into the given stream.
Definition: MSRoute.cpp:224
The state of a link.
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:173
A road/street connecting two junctions.
Definition: MSEdge.h:73
#define max(a, b)
Definition: polyfonts.c:61
static void clear()
Clears the dictionary (delete all known routes, too)
Definition: MSRoute.cpp:160
the edges of a route
std::map< std::string, RandomDistributor< const MSRoute * > * > RouteDistDict
Definition of the dictionary container.
Definition: MSRoute.h:225
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
bool containsAnyOf(const std::vector< MSEdge * > &edgelist) const
Definition: MSRoute.cpp:206
static RouteDistDict myDistDict
The dictionary container.
Definition: MSRoute.h:228
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:198
MSEdgeVector myEdges
The list of edges to pass.
Definition: MSRoute.h:206
bool contains(const MSEdge *const edge) const
Definition: MSRoute.h:104
virtual ~MSRoute()
Destructor.
Definition: MSRoute.cpp:69
SUMOReal getLength() const
Definition: MSRoute.cpp:240
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:215
Base class for objects which have an id.
Definition: Named.h:45
std::string myID
The name of the object.
Definition: Named.h:121
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:81
MSRoute(const std::string &id, const MSEdgeVector &edges, unsigned int references, const RGBColor *const c, const std::vector< SUMOVehicleParameter::Stop > &stops)
Constructor.
Definition: MSRoute.cpp:60
unsigned size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:87
const MSEdge * operator[](unsigned index) const
Definition: MSRoute.cpp:218
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:293
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:221
unsigned int myReferenceCounter
Information by how many vehicles the route is used.
Definition: MSRoute.h:209
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:106
static unsigned int MaxRouteDistSize
the maximum size for each routeDistribution
Definition: MSRoute.h:231
static RouteDict myDict
The dictionary container.
Definition: MSRoute.h:222
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const RGBColor *const myColor
The color.
Definition: MSRoute.h:212
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:75
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116