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-2016 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>
38 #include <utils/common/RGBColor.h>
41 #include "MSEdge.h"
42 #include "MSLane.h"
43 #include "MSRoute.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 #ifdef HAVE_FOX
56 FXMutex MSRoute::myDictMutex(true);
57 #endif
58 
59 
60 // ===========================================================================
61 // member method definitions
62 // ===========================================================================
63 MSRoute::MSRoute(const std::string& id,
64  const ConstMSEdgeVector& edges,
65  const bool isPermanent, const RGBColor* const c,
66  const std::vector<SUMOVehicleParameter::Stop>& stops)
67  : Named(id), myEdges(edges), myAmPermanent(isPermanent),
68  myReferenceCounter(isPermanent ? 1 : 0),
69  myColor(c), myStops(stops) {}
70 
71 
73  delete myColor;
74 }
75 
76 
78 MSRoute::begin() const {
79  return myEdges.begin();
80 }
81 
82 
84 MSRoute::end() const {
85  return myEdges.end();
86 }
87 
88 
89 int
90 MSRoute::size() const {
91  return (int)myEdges.size();
92 }
93 
94 
95 const MSEdge*
97  assert(myEdges.size() > 0);
98  return myEdges[myEdges.size() - 1];
99 }
100 
101 
102 void
105 }
106 
107 
108 void
111  if (myReferenceCounter == 0) {
112 #ifdef HAVE_FOX
113  FXMutexLock f(myDictMutex);
114 #endif
115  myDict.erase(myID);
116  delete this;
117  }
118 }
119 
120 
121 bool
122 MSRoute::dictionary(const std::string& id, const MSRoute* route) {
123 #ifdef HAVE_FOX
124  FXMutexLock f(myDictMutex);
125 #endif
126  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
127  myDict[id] = route;
128  return true;
129  }
130  return false;
131 }
132 
133 
134 bool
135 MSRoute::dictionary(const std::string& id, RandomDistributor<const MSRoute*>* const routeDist, const bool permanent) {
136 #ifdef HAVE_FOX
137  FXMutexLock f(myDictMutex);
138 #endif
139  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
140  myDistDict[id] = std::make_pair(routeDist, permanent);
141  return true;
142  }
143  return false;
144 }
145 
146 
147 const MSRoute*
148 MSRoute::dictionary(const std::string& id, MTRand* rng) {
149 #ifdef HAVE_FOX
150  FXMutexLock f(myDictMutex);
151 #endif
152  RouteDict::iterator it = myDict.find(id);
153  if (it == myDict.end()) {
154  RouteDistDict::iterator it2 = myDistDict.find(id);
155  if (it2 == myDistDict.end() || it2->second.first->getOverallProb() == 0) {
156  return 0;
157  }
158  return it2->second.first->get(rng);
159  }
160  return it->second;
161 }
162 
163 
165 MSRoute::distDictionary(const std::string& id) {
166 #ifdef HAVE_FOX
167  FXMutexLock f(myDictMutex);
168 #endif
169  RouteDistDict::iterator it2 = myDistDict.find(id);
170  if (it2 == myDistDict.end()) {
171  return 0;
172  }
173  return it2->second.first;
174 }
175 
176 
177 void
179 #ifdef HAVE_FOX
180  FXMutexLock f(myDictMutex);
181 #endif
182  for (RouteDistDict::iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
183  delete i->second.first;
184  }
185  myDistDict.clear();
186  for (RouteDict::iterator i = myDict.begin(); i != myDict.end(); ++i) {
187  delete i->second;
188  }
189  myDict.clear();
190 }
191 
192 
193 void
194 MSRoute::checkDist(const std::string& id) {
195 #ifdef HAVE_FOX
196  FXMutexLock f(myDictMutex);
197 #endif
198  RouteDistDict::iterator it = myDistDict.find(id);
199  if (it != myDistDict.end() && !it->second.second) {
200  const std::vector<const MSRoute*>& routes = it->second.first->getVals();
201  for (std::vector<const MSRoute*>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
202  (*i)->release();
203  }
204  delete it->second.first;
205  myDistDict.erase(it);
206  }
207 }
208 
209 
210 void
211 MSRoute::insertIDs(std::vector<std::string>& into) {
212 #ifdef HAVE_FOX
213  FXMutexLock f(myDictMutex);
214 #endif
215  into.reserve(myDict.size() + myDistDict.size() + into.size());
216  for (RouteDict::const_iterator i = myDict.begin(); i != myDict.end(); ++i) {
217  into.push_back((*i).first);
218  }
219  for (RouteDistDict::const_iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
220  into.push_back((*i).first);
221  }
222 }
223 
224 
225 int
226 MSRoute::writeEdgeIDs(OutputDevice& os, const MSEdge* const from, const MSEdge* const upTo) const {
227  int numWritten = 0;
228  ConstMSEdgeVector::const_iterator i = myEdges.begin();
229  if (from != 0) {
230  i = std::find(myEdges.begin(), myEdges.end(), from);
231  }
232  for (; i != myEdges.end(); ++i) {
233  if ((*i) == upTo) {
234  return numWritten;
235  }
236  os << (*i)->getID();
237  numWritten++;
238  if (upTo || i != myEdges.end() - 1) {
239  os << ' ';
240  }
241  }
242  return numWritten;
243 }
244 
245 
246 bool
247 MSRoute::containsAnyOf(const MSEdgeVector& edgelist) const {
248  MSEdgeVector::const_iterator i = edgelist.begin();
249  for (; i != edgelist.end(); ++i) {
250  if (contains(*i)) {
251  return true;
252  }
253  }
254  return false;
255 }
256 
257 
258 const MSEdge*
259 MSRoute::operator[](int index) const {
260  return myEdges[index];
261 }
262 
263 
264 void
266 #ifdef HAVE_FOX
267  FXMutexLock f(myDictMutex);
268 #endif
269  for (RouteDict::iterator it = myDict.begin(); it != myDict.end(); ++it) {
270  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, (*it).second->getID());
271  out.writeAttr(SUMO_ATTR_STATE, (*it).second->myAmPermanent);
272  out.writeAttr(SUMO_ATTR_EDGES, (*it).second->myEdges).closeTag();
273  }
274  for (RouteDistDict::iterator it = myDistDict.begin(); it != myDistDict.end(); ++it) {
276  out.writeAttr(SUMO_ATTR_STATE, (*it).second.second);
277  out.writeAttr(SUMO_ATTR_ROUTES, (*it).second.first->getVals());
278  out.writeAttr(SUMO_ATTR_PROBS, (*it).second.first->getProbs());
279  out.closeTag();
280  }
281 }
282 
283 
284 SUMOReal
286  const MSEdge* fromEdge, const MSEdge* toEdge, bool includeInternal) const {
287  ConstMSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
288  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
289  // start or destination not contained in route
291  }
292  ConstMSEdgeVector::const_iterator it2 = std::find(it + 1, myEdges.end(), toEdge);
293 
294  if (fromEdge == toEdge) {
295  if (fromPos <= toPos) {
296  return toPos - fromPos;
297  } else if (it2 == myEdges.end()) {
298  // we don't visit the edge again
300  }
301  }
302  return getDistanceBetween(fromPos, toPos, it, it2, includeInternal);
303 }
304 
305 
306 SUMOReal
308  const MSRouteIterator& fromEdge, const MSRouteIterator& toEdge, bool includeInternal) const {
309  bool isFirstIteration = true;
310  SUMOReal distance = -fromPos;
311  MSRouteIterator it = fromEdge;
312  if (fromEdge == toEdge) {
313  // destination position is on start edge
314  if (fromPos <= toPos) {
315  return toPos - fromPos;
316  } else {
317  // we cannot go backwards. Something is wrong here
319  }
320  } else if (fromEdge > toEdge) {
321  // we don't visit the edge again
323  }
324  for (; it != end(); ++it) {
325  if (it == toEdge && !isFirstIteration) {
326  distance += toPos;
327  break;
328  } else {
329  distance += (*it)->getLength();
330 #ifdef HAVE_INTERNAL_LANES
331  if (includeInternal) {
332  // add length of internal lanes to the result
333  const MSEdge* internal = (*it)->getInternalFollowingEdge(*(it + 1));
334  if (internal != 0) {
335  distance += internal->getLength();
336  }
337  }
338 #else
339  UNUSED_PARAMETER(includeInternal);
340 #endif
341  }
342  isFirstIteration = false;
343  }
344  return distance;
345 }
346 
347 
348 const RGBColor&
350  if (myColor == 0) {
352  }
353  return *myColor;
354 }
355 
356 
357 const std::vector<SUMOVehicleParameter::Stop>&
359  return myStops;
360 }
361 
362 
363 /****************************************************************************/
364 
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:253
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:109
Represents a generic random distribution.
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:349
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:96
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
static RandomDistributor< const MSRoute * > * distDictionary(const std::string &id)
Returns the named route distribution.
Definition: MSRoute.cpp:165
int size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:90
#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:265
The state of a link.
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:211
A road/street connecting two junctions.
Definition: MSEdge.h:80
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:285
#define max(a, b)
Definition: polyfonts.c:65
static void clear()
Clears the dictionary (delete all known routes, too)
Definition: MSRoute.cpp:178
the edges of a route
static RouteDistDict myDistDict
The dictionary container.
Definition: MSRoute.h:262
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:63
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:65
std::map< std::string, std::pair< RandomDistributor< const MSRoute * > *, bool > > RouteDistDict
Definition of the dictionary container.
Definition: MSRoute.h:259
virtual ~MSRoute()
Destructor.
Definition: MSRoute.cpp:72
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:226
std::vector< SUMOVehicleParameter::Stop > myStops
List of the stops on the parsed route.
Definition: MSRoute.h:249
Base class for objects which have an id.
Definition: Named.h:46
const MSEdge * operator[](int index) const
Definition: MSRoute.cpp:259
std::string myID
The name of the object.
Definition: Named.h:136
bool containsAnyOf(const MSEdgeVector &edgelist) const
Definition: MSRoute.cpp:247
ConstMSEdgeVector myEdges
The list of edges to pass.
Definition: MSRoute.h:234
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:591
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:103
int myReferenceCounter
Information by how many vehicles the route is used.
Definition: MSRoute.h:240
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:78
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:77
static void checkDist(const std::string &id)
Checks the distribution whether it is permanent and deletes it if not.
Definition: MSRoute.cpp:194
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:358
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:84
static RouteDict myDict
The dictionary container.
Definition: MSRoute.h:256
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool contains(const MSEdge *const edge) const
Definition: MSRoute.h:110
const RGBColor *const myColor
The color.
Definition: MSRoute.h:243
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:122