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