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 // ===========================================================================
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
59 MSRoute::MSRoute(const std::string& id,
60  const MSEdgeVector& edges,
61  const bool isPermanent, const RGBColor* const c,
62  const std::vector<SUMOVehicleParameter::Stop>& stops)
63  : Named(id), myEdges(edges), myAmPermanent(isPermanent),
64  myReferenceCounter(isPermanent ? 1 : 0),
65  myColor(c), myStops(stops) {}
66 
67 
69  delete myColor;
70 }
71 
72 
74 MSRoute::begin() const {
75  return myEdges.begin();
76 }
77 
78 
80 MSRoute::end() const {
81  return myEdges.end();
82 }
83 
84 
85 unsigned
86 MSRoute::size() const {
87  return (unsigned) myEdges.size();
88 }
89 
90 
91 const MSEdge*
93  assert(myEdges.size() > 0);
94  return myEdges[myEdges.size() - 1];
95 }
96 
97 
98 void
101 }
102 
103 
104 void
107  if (myReferenceCounter == 0) {
108  myDict.erase(myID);
109  delete this;
110  }
111 }
112 
113 
114 bool
115 MSRoute::dictionary(const std::string& id, const MSRoute* route) {
116  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
117  myDict[id] = route;
118  return true;
119  }
120  return false;
121 }
122 
123 
124 bool
125 MSRoute::dictionary(const std::string& id, RandomDistributor<const MSRoute*>* const routeDist, const bool permanent) {
126  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
127  myDistDict[id] = std::make_pair(routeDist, permanent);
128  return true;
129  }
130  return false;
131 }
132 
133 
134 const MSRoute*
135 MSRoute::dictionary(const std::string& id) {
136  RouteDict::iterator it = myDict.find(id);
137  if (it == myDict.end()) {
138  RouteDistDict::iterator it2 = myDistDict.find(id);
139  if (it2 == myDistDict.end() || it2->second.first->getOverallProb() == 0) {
140  return 0;
141  }
142  return it2->second.first->get();
143  }
144  return it->second;
145 }
146 
147 
149 MSRoute::distDictionary(const std::string& id) {
150  RouteDistDict::iterator it2 = myDistDict.find(id);
151  if (it2 == myDistDict.end()) {
152  return 0;
153  }
154  return it2->second.first;
155 }
156 
157 
158 void
160  for (RouteDistDict::iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
161  delete i->second.first;
162  }
163  myDistDict.clear();
164  for (RouteDict::iterator i = myDict.begin(); i != myDict.end(); ++i) {
165  delete i->second;
166  }
167  myDict.clear();
168 }
169 
170 
171 void
172 MSRoute::checkDist(const std::string& id) {
173  RouteDistDict::iterator it = myDistDict.find(id);
174  if (it != myDistDict.end() && !it->second.second) {
175  const std::vector<const MSRoute*>& routes = it->second.first->getVals();
176  for (std::vector<const MSRoute*>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
177  (*i)->release();
178  }
179  delete it->second.first;
180  myDistDict.erase(it);
181  }
182 }
183 
184 
185 void
186 MSRoute::insertIDs(std::vector<std::string>& into) {
187  into.reserve(myDict.size() + myDistDict.size() + into.size());
188  for (RouteDict::const_iterator i = myDict.begin(); i != myDict.end(); ++i) {
189  into.push_back((*i).first);
190  }
191  for (RouteDistDict::const_iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
192  into.push_back((*i).first);
193  }
194 }
195 
196 
197 int
198 MSRoute::writeEdgeIDs(OutputDevice& os, const MSEdge* const from, const MSEdge* const upTo) const {
199  int numWritten = 0;
200  MSEdgeVector::const_iterator i = myEdges.begin();
201  if (from != 0) {
202  i = std::find(myEdges.begin(), myEdges.end(), from);
203  }
204  for (; i != myEdges.end(); ++i) {
205  if ((*i) == upTo) {
206  return numWritten;
207  }
208  os << (*i)->getID();
209  numWritten++;
210  if (upTo || i != myEdges.end() - 1) {
211  os << ' ';
212  }
213  }
214  return numWritten;
215 }
216 
217 
218 bool
219 MSRoute::containsAnyOf(const std::vector<MSEdge*>& edgelist) const {
220  std::vector<MSEdge*>::const_iterator i = edgelist.begin();
221  for (; i != edgelist.end(); ++i) {
222  if (contains(*i)) {
223  return true;
224  }
225  }
226  return false;
227 }
228 
229 
230 const MSEdge*
231 MSRoute::operator[](unsigned index) const {
232  return myEdges[index];
233 }
234 
235 
236 void
238  for (RouteDict::iterator it = myDict.begin(); it != myDict.end(); ++it) {
239  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, (*it).second->getID());
240  out.writeAttr(SUMO_ATTR_STATE, (*it).second->myAmPermanent);
241  out.writeAttr(SUMO_ATTR_EDGES, (*it).second->myEdges).closeTag();
242  }
243  for (RouteDistDict::iterator it = myDistDict.begin(); it != myDistDict.end(); ++it) {
245  out.writeAttr(SUMO_ATTR_STATE, (*it).second.second);
246  out.writeAttr(SUMO_ATTR_ROUTES, (*it).second.first->getVals());
247  out.writeAttr(SUMO_ATTR_PROBS, (*it).second.first->getProbs());
248  out.closeTag();
249  }
250 }
251 
252 
253 SUMOReal
255  SUMOReal ret = 0;
256  for (MSEdgeVector::const_iterator i = myEdges.begin(); i != myEdges.end(); ++i) {
257  ret += (*i)->getLength();
258  }
259  return ret;
260 }
261 
262 
263 SUMOReal
264 MSRoute::getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge* fromEdge, const MSEdge* toEdge) const {
265  bool isFirstIteration = true;
266  SUMOReal distance = -fromPos;
267  MSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
268 
269  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
270  // start or destination not contained in route
272  }
273  if (fromEdge == toEdge) {
274  // destination position is on start edge
275  if (fromPos <= toPos) {
276  return toPos - fromPos;
277  } else if (std::find(it + 1, myEdges.end(), toEdge) == myEdges.end()) {
278  // we don't visit the edge again
280  }
281  }
282  for (; it != end(); ++it) {
283  if ((*it) == toEdge && !isFirstIteration) {
284  distance += toPos;
285  break;
286  } else {
287  const std::vector<MSLane*>& lanes = (*it)->getLanes();
288  distance += lanes[0]->getLength();
289 #ifdef HAVE_INTERNAL_LANES
290  // add length of internal lanes to the result
291  for (std::vector<MSLane*>::const_iterator laneIt = lanes.begin(); laneIt != lanes.end(); ++laneIt) {
292  const MSLinkCont& links = (*laneIt)->getLinkCont();
293  for (MSLinkCont::const_iterator linkIt = links.begin(); linkIt != links.end(); ++linkIt) {
294  if ((*linkIt) == 0 || (*linkIt)->getLane() == 0) {
295  continue;
296  }
297  std::string succLaneId = (*(it + 1))->getLanes()[0]->getID();
298  if ((*linkIt)->getLane()->getID().compare(succLaneId) == 0) {
299  distance += (*linkIt)->getLength();
300  }
301  }
302  }
303 #endif
304  }
305  isFirstIteration = false;
306  }
307  return distance;
308 }
309 
310 
311 const RGBColor&
313  if (myColor == 0) {
315  }
316  return *myColor;
317 }
318 
319 
320 const std::vector<SUMOVehicleParameter::Stop>&
322  return myStops;
323 }
324 
325 
326 /****************************************************************************/
327 
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:213
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:198
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:59
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:92
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:149
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:264
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:321
static void dict_saveState(OutputDevice &out)
Saves all known routes into the given stream.
Definition: MSRoute.cpp:237
The state of a link.
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:186
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:159
the edges of a route
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
bool containsAnyOf(const std::vector< MSEdge * > &edgelist) const
Definition: MSRoute.cpp:219
static RouteDistDict myDistDict
The dictionary container.
Definition: MSRoute.h:222
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:197
bool contains(const MSEdge *const edge) const
Definition: MSRoute.h:104
std::map< std::string, std::pair< RandomDistributor< const MSRoute * > *, bool > > RouteDistDict
Definition of the dictionary container.
Definition: MSRoute.h:219
virtual ~MSRoute()
Destructor.
Definition: MSRoute.cpp:68
SUMOReal getLength() const
Definition: MSRoute.cpp:254
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:99
std::vector< SUMOVehicleParameter::Stop > myStops
List of the stops on the parsed route.
Definition: MSRoute.h:209
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:80
unsigned size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:86
const MSEdge * operator[](unsigned index) const
Definition: MSRoute.cpp:231
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:312
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:203
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:105
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:172
static RouteDict myDict
The dictionary container.
Definition: MSRoute.h:216
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const RGBColor *const myColor
The color.
Definition: MSRoute.h:206
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:74
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:59
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:115