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.dlr.de/
13 // Copyright (C) 2002-2015 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 ConstMSEdgeVector& 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, MTRand* rng) {
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(rng);
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  ConstMSEdgeVector::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 MSEdgeVector& edgelist) const {
221  MSEdgeVector::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  const MSEdge* fromEdge, const MSEdge* toEdge, bool includeInternal) const {
257  ConstMSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
258  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
259  // start or destination not contained in route
261  }
262  ConstMSEdgeVector::const_iterator it2 = std::find(it + 1, myEdges.end(), toEdge);
263 
264  if (fromEdge == toEdge) {
265  if (fromPos < toPos) {
266  return toPos - fromPos;
267  } else if (it2 == myEdges.end()) {
268  // we don't visit the edge again
270  }
271  }
272  return getDistanceBetween(fromPos, toPos, it, it2, includeInternal);
273 }
274 
275 
276 SUMOReal
278  const MSRouteIterator& fromEdge, const MSRouteIterator& toEdge, bool includeInternal) const {
279  bool isFirstIteration = true;
280  SUMOReal distance = -fromPos;
281  MSRouteIterator it = fromEdge;
282  if (fromEdge == toEdge) {
283  // destination position is on start edge
284  if (fromPos <= toPos) {
285  return toPos - fromPos;
286  } else {
287  // we cannot go backwards. Something is wrong here
289  }
290  } else if (fromEdge > toEdge) {
291  // we don't visit the edge again
293  }
294  for (; it != end(); ++it) {
295  if (it == toEdge && !isFirstIteration) {
296  distance += toPos;
297  break;
298  } else {
299  const std::vector<MSLane*>& lanes = (*it)->getLanes();
300  distance += lanes[0]->getLength();
301 #ifdef HAVE_INTERNAL_LANES
302  if (includeInternal) {
303  // add length of internal lanes to the result
304  for (std::vector<MSLane*>::const_iterator laneIt = lanes.begin(); laneIt != lanes.end(); ++laneIt) {
305  const MSLinkCont& links = (*laneIt)->getLinkCont();
306  for (MSLinkCont::const_iterator linkIt = links.begin(); linkIt != links.end(); ++linkIt) {
307  if ((*linkIt) == 0 || (*linkIt)->getLane() == 0) {
308  continue;
309  }
310  std::string succLaneId = (*(it + 1))->getLanes()[0]->getID();
311  if ((*linkIt)->getLane()->getID().compare(succLaneId) == 0) {
312  distance += (*linkIt)->getLength();
313  }
314  }
315  }
316  }
317 #else
318  UNUSED_PARAMETER(includeInternal);
319 #endif
320  }
321  isFirstIteration = false;
322  }
323  return distance;
324 }
325 
326 
327 const RGBColor&
329  if (myColor == 0) {
331  }
332  return *myColor;
333 }
334 
335 
336 const std::vector<SUMOVehicleParameter::Stop>&
338  return myStops;
339 }
340 
341 
342 /****************************************************************************/
343 
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:249
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
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:93
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:79
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:150
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:337
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
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:81
#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
SUMOReal getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge *fromEdge, const MSEdge *toEdge, bool includeInternal=true) const
Compute the distance between 2 given edges on this route, including the length of internal lanes...
Definition: MSRoute.cpp:255
static RouteDistDict myDistDict
The dictionary container.
Definition: MSRoute.h:258
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:200
MSRoute(const std::string &id, const ConstMSEdgeVector &edges, const bool isPermanent, const RGBColor *const c, const std::vector< SUMOVehicleParameter::Stop > &stops)
Constructor.
Definition: MSRoute.cpp:60
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:61
bool contains(const MSEdge *const edge) const
Definition: MSRoute.h:106
std::map< std::string, std::pair< RandomDistributor< const MSRoute * > *, bool > > RouteDistDict
Definition of the dictionary container.
Definition: MSRoute.h:255
virtual ~MSRoute()
Destructor.
Definition: MSRoute.cpp:69
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:245
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
ConstMSEdgeVector myEdges
The list of edges to pass.
Definition: MSRoute.h:230
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:328
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:78
unsigned int myReferenceCounter
Information by how many vehicles the route is used.
Definition: MSRoute.h:236
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:252
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const RGBColor *const myColor
The color.
Definition: MSRoute.h:239
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
bool containsAnyOf(const MSEdgeVector &edgelist) const
Definition: MSRoute.cpp:220