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-2017 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 
46 // ===========================================================================
47 // static member variables
48 // ===========================================================================
51 #ifdef HAVE_FOX
52 FXMutex MSRoute::myDictMutex(true);
53 #endif
54 
55 
56 // ===========================================================================
57 // member method definitions
58 // ===========================================================================
59 MSRoute::MSRoute(const std::string& id,
60  const ConstMSEdgeVector& 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 int
86 MSRoute::size() const {
87  return (int)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 #ifdef HAVE_FOX
109  FXMutexLock f(myDictMutex);
110 #endif
111  myDict.erase(myID);
112  delete this;
113  }
114 }
115 
116 
117 bool
118 MSRoute::dictionary(const std::string& id, const MSRoute* route) {
119 #ifdef HAVE_FOX
120  FXMutexLock f(myDictMutex);
121 #endif
122  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
123  myDict[id] = route;
124  return true;
125  }
126  return false;
127 }
128 
129 
130 bool
131 MSRoute::dictionary(const std::string& id, RandomDistributor<const MSRoute*>* const routeDist, const bool permanent) {
132 #ifdef HAVE_FOX
133  FXMutexLock f(myDictMutex);
134 #endif
135  if (myDict.find(id) == myDict.end() && myDistDict.find(id) == myDistDict.end()) {
136  myDistDict[id] = std::make_pair(routeDist, permanent);
137  return true;
138  }
139  return false;
140 }
141 
142 
143 const MSRoute*
144 MSRoute::dictionary(const std::string& id, MTRand* rng) {
145 #ifdef HAVE_FOX
146  FXMutexLock f(myDictMutex);
147 #endif
148  RouteDict::iterator it = myDict.find(id);
149  if (it == myDict.end()) {
150  RouteDistDict::iterator it2 = myDistDict.find(id);
151  if (it2 == myDistDict.end() || it2->second.first->getOverallProb() == 0) {
152  return 0;
153  }
154  return it2->second.first->get(rng);
155  }
156  return it->second;
157 }
158 
159 
161 MSRoute::distDictionary(const std::string& id) {
162 #ifdef HAVE_FOX
163  FXMutexLock f(myDictMutex);
164 #endif
165  RouteDistDict::iterator it2 = myDistDict.find(id);
166  if (it2 == myDistDict.end()) {
167  return 0;
168  }
169  return it2->second.first;
170 }
171 
172 
173 void
175 #ifdef HAVE_FOX
176  FXMutexLock f(myDictMutex);
177 #endif
178  for (RouteDistDict::iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
179  delete i->second.first;
180  }
181  myDistDict.clear();
182  for (RouteDict::iterator i = myDict.begin(); i != myDict.end(); ++i) {
183  delete i->second;
184  }
185  myDict.clear();
186 }
187 
188 
189 void
190 MSRoute::checkDist(const std::string& id) {
191 #ifdef HAVE_FOX
192  FXMutexLock f(myDictMutex);
193 #endif
194  RouteDistDict::iterator it = myDistDict.find(id);
195  if (it != myDistDict.end() && !it->second.second) {
196  const std::vector<const MSRoute*>& routes = it->second.first->getVals();
197  for (std::vector<const MSRoute*>::const_iterator i = routes.begin(); i != routes.end(); ++i) {
198  (*i)->release();
199  }
200  delete it->second.first;
201  myDistDict.erase(it);
202  }
203 }
204 
205 
206 void
207 MSRoute::insertIDs(std::vector<std::string>& into) {
208 #ifdef HAVE_FOX
209  FXMutexLock f(myDictMutex);
210 #endif
211  into.reserve(myDict.size() + myDistDict.size() + into.size());
212  for (RouteDict::const_iterator i = myDict.begin(); i != myDict.end(); ++i) {
213  into.push_back((*i).first);
214  }
215  for (RouteDistDict::const_iterator i = myDistDict.begin(); i != myDistDict.end(); ++i) {
216  into.push_back((*i).first);
217  }
218 }
219 
220 
221 int
222 MSRoute::writeEdgeIDs(OutputDevice& os, const MSEdge* const from, const MSEdge* const upTo) const {
223  int numWritten = 0;
224  ConstMSEdgeVector::const_iterator i = myEdges.begin();
225  if (from != 0) {
226  i = std::find(myEdges.begin(), myEdges.end(), from);
227  }
228  for (; i != myEdges.end(); ++i) {
229  if ((*i) == upTo) {
230  return numWritten;
231  }
232  os << (*i)->getID();
233  numWritten++;
234  if (upTo || i != myEdges.end() - 1) {
235  os << ' ';
236  }
237  }
238  return numWritten;
239 }
240 
241 
242 bool
243 MSRoute::containsAnyOf(const MSEdgeVector& edgelist) const {
244  MSEdgeVector::const_iterator i = edgelist.begin();
245  for (; i != edgelist.end(); ++i) {
246  if (contains(*i)) {
247  return true;
248  }
249  }
250  return false;
251 }
252 
253 
254 const MSEdge*
255 MSRoute::operator[](int index) const {
256  return myEdges[index];
257 }
258 
259 
260 void
262 #ifdef HAVE_FOX
263  FXMutexLock f(myDictMutex);
264 #endif
265  for (RouteDict::iterator it = myDict.begin(); it != myDict.end(); ++it) {
266  out.openTag(SUMO_TAG_ROUTE).writeAttr(SUMO_ATTR_ID, (*it).second->getID());
267  out.writeAttr(SUMO_ATTR_STATE, (*it).second->myAmPermanent);
268  out.writeAttr(SUMO_ATTR_EDGES, (*it).second->myEdges).closeTag();
269  }
270  for (RouteDistDict::iterator it = myDistDict.begin(); it != myDistDict.end(); ++it) {
272  out.writeAttr(SUMO_ATTR_STATE, (*it).second.second);
273  out.writeAttr(SUMO_ATTR_ROUTES, (*it).second.first->getVals());
274  out.writeAttr(SUMO_ATTR_PROBS, (*it).second.first->getProbs());
275  out.closeTag();
276  }
277 }
278 
279 
280 double
281 MSRoute::getDistanceBetween(double fromPos, double toPos,
282  const MSEdge* fromEdge, const MSEdge* toEdge, bool includeInternal) const {
283  ConstMSEdgeVector::const_iterator it = std::find(myEdges.begin(), myEdges.end(), fromEdge);
284  if (it == myEdges.end() || std::find(it, myEdges.end(), toEdge) == myEdges.end()) {
285  // start or destination not contained in route
287  }
288  ConstMSEdgeVector::const_iterator it2 = std::find(it + 1, myEdges.end(), toEdge);
289 
290  if (fromEdge == toEdge) {
291  if (fromPos <= toPos) {
292  return toPos - fromPos;
293  } else if (it2 == myEdges.end()) {
294  // we don't visit the edge again
296  }
297  }
298  return getDistanceBetween(fromPos, toPos, it, it2, includeInternal);
299 }
300 
301 
302 double
303 MSRoute::getDistanceBetween(double fromPos, double toPos,
304  const MSRouteIterator& fromEdge, const MSRouteIterator& toEdge, bool includeInternal) const {
305  bool isFirstIteration = true;
306  double distance = -fromPos;
307  MSRouteIterator it = fromEdge;
308  if (fromEdge == toEdge) {
309  // destination position is on start edge
310  if (fromPos <= toPos) {
311  return toPos - fromPos;
312  } else {
313  // we cannot go backwards. Something is wrong here
315  }
316  } else if (fromEdge > toEdge) {
317  // we don't visit the edge again
319  }
320  for (; it != end(); ++it) {
321  if (it == toEdge && !isFirstIteration) {
322  distance += toPos;
323  break;
324  } else {
325  distance += (*it)->getLength();
326  if (includeInternal) {
327  // add length of internal lanes to the result
328  const MSEdge* internal = (*it)->getInternalFollowingEdge(*(it + 1));
329  if (internal != 0) {
330  distance += internal->getLength();
331  }
332  }
333  }
334  isFirstIteration = false;
335  }
336  return distance;
337 }
338 
339 
340 const RGBColor&
342  if (myColor == 0) {
344  }
345  return *myColor;
346 }
347 
348 
349 const std::vector<SUMOVehicleParameter::Stop>&
351  return myStops;
352 }
353 
354 
355 /****************************************************************************/
356 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:105
Represents a generic random distribution.
distribution of a route
double getDistanceBetween(double fromPos, double 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:281
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:341
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:92
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:161
double getLength() const
return the length of the edge
Definition: MSEdge.h:586
int size() const
Returns the number of edges to pass.
Definition: MSRoute.cpp:86
begin/end of the description of a route
static void dict_saveState(OutputDevice &out)
Saves all known routes into the given stream.
Definition: MSRoute.cpp:261
The state of a link.
static void insertIDs(std::vector< std::string > &into)
Definition: MSRoute.cpp:207
A road/street connecting two junctions.
Definition: MSEdge.h:80
#define max(a, b)
Definition: polyfonts.c:65
static void clear()
Clears the dictionary (delete all known routes, too)
Definition: MSRoute.cpp:174
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:59
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:68
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:222
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 * getInternalFollowingEdge(const MSEdge *followerAfterInternal) const
Definition: MSEdge.cpp:650
const MSEdge * operator[](int index) const
Definition: MSRoute.cpp:255
std::string myID
The name of the object.
Definition: Named.h:136
bool containsAnyOf(const MSEdgeVector &edgelist) const
Definition: MSRoute.cpp:243
ConstMSEdgeVector myEdges
The list of edges to pass.
Definition: MSRoute.h:234
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:99
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.
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:74
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:190
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:350
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:80
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:118