SUMO - Simulation of Urban MObility
NBConnection.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The class holds a description of a connection between two edges
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2017 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 <sstream>
34 #include <iostream>
35 #include <cassert>
36 #include "NBEdgeCont.h"
37 #include "NBEdge.h"
38 #include "NBConnection.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static members
47 // ===========================================================================
48 const int NBConnection::InvalidTlIndex = -1;
49 const NBConnection NBConnection::InvalidConnection("invalidFrom", 0, "invalidTo", 0);
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  myFrom(from), myTo(to),
56  myFromID(from->getID()), myToID(to->getID()),
57  myFromLane(-1), myToLane(-1),
58  myTlIndex(InvalidTlIndex) {
59 }
60 
61 
62 NBConnection::NBConnection(const std::string& fromID, NBEdge* from,
63  const std::string& toID, NBEdge* to) :
64  myFrom(from), myTo(to),
65  myFromID(fromID), myToID(toID),
66  myFromLane(-1), myToLane(-1),
68 }
69 
70 
71 NBConnection::NBConnection(NBEdge* from, int fromLane,
72  NBEdge* to, int toLane, int tlIndex) :
73  myFrom(from), myTo(to),
74  myFromLane(fromLane), myToLane(toLane),
75  myTlIndex(tlIndex) {
76  /* @todo what should we assert here?
77  assert(myFromLane<0||from->getNumLanes()>(int) myFromLane);
78  assert(myToLane<0||to->getNumLanes()>(int) myToLane);
79  */
80  myFromID = from->getID();
81  myToID = to != 0 ? to->getID() : "";
82 }
83 
84 
86 
87 
89  myFrom(c.myFrom), myTo(c.myTo),
92  myTlIndex(c.myTlIndex) {
93 }
94 
95 
96 NBEdge*
98  return myFrom;
99 }
100 
101 
102 NBEdge*
104  return myTo;
105 }
106 
107 
108 bool
110  if (myFrom == which) {
111  myFrom = by;
112  if (myFrom != 0) {
113  myFromID = myFrom->getID();
114  } else {
115  myFromID = "invalidFrom";
116  }
117  return true;
118  }
119  return false;
120 }
121 
122 
123 bool
124 NBConnection::replaceFrom(NBEdge* which, int whichLane,
125  NBEdge* by, int byLane) {
126  if (myFrom == which && (myFromLane == whichLane || myFromLane < 0 || whichLane < 0)) {
127  myFrom = by;
128  if (myFrom != 0) {
129  myFromID = myFrom->getID();
130  } else {
131  myFromID = "invalidFrom";
132  }
133  if (byLane >= 0) {
134  myFromLane = byLane;
135  }
136  return true;
137  }
138  return false;
139 }
140 
141 
142 bool
144  if (myTo == which) {
145  myTo = by;
146  if (myTo != 0) {
147  myToID = myTo->getID();
148  } else {
149  myToID = "invalidTo";
150  }
151  return true;
152  }
153  return false;
154 }
155 
156 
157 bool
158 NBConnection::replaceTo(NBEdge* which, int whichLane,
159  NBEdge* by, int byLane) {
160  if (myTo == which && (myToLane == whichLane || myFromLane < 0 || whichLane < 0)) {
161  myTo = by;
162  if (myTo != 0) {
163  myToID = myTo->getID();
164  } else {
165  myToID = "invalidTo";
166  }
167  if (byLane >= 0) {
168  myToLane = byLane;
169  }
170  return true;
171  }
172  return false;
173 }
174 
175 
176 bool
177 operator<(const NBConnection& c1, const NBConnection& c2) {
178  if (c1.myFromID != c2.myFromID) {
179  return c1.myFromID < c2.myFromID;
180  }
181  if (c1.myToID != c2.myToID) {
182  return c1.myToID < c2.myToID;
183  }
184  if (c1.myFromLane != c2.myFromLane) {
185  return c1.myFromLane < c2.myFromLane;
186  }
187  return c1.myToLane < c2.myToLane;
188 }
189 
190 
191 bool
193  return (myFrom == c.myFrom && myTo == c.myTo &&
194  myFromID == c.myFromID && myToID == c.myToID &&
195  myFromLane == c.myFromLane && myToLane == c.myToLane &&
196  myTlIndex == c.myTlIndex);
197 }
198 
199 
200 bool
202  myFrom = checkFrom(ec);
203  myTo = checkTo(ec);
204  return myFrom != 0 && myTo != 0;
205 }
206 
207 
208 NBEdge*
210  NBEdge* e = ec.retrieve(myFromID);
211  // ok, the edge was not changed
212  if (e == myFrom) {
213  return myFrom;
214  }
215  // try to get the edge
216  return ec.retrievePossiblySplit(myFromID, myToID, true);
217 }
218 
219 
220 NBEdge*
222  NBEdge* e = ec.retrieve(myToID);
223  // ok, the edge was not changed
224  if (e == myTo) {
225  return myTo;
226  }
227  // try to get the edge
228  return ec.retrievePossiblySplit(myToID, myFromID, false);
229 }
230 
231 
232 std::string
234  std::stringstream str;
235  str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
236  return str.str();
237 }
238 
239 
240 int
242  return myFromLane;
243 }
244 
245 
246 int
248  return myToLane;
249 }
250 
251 
252 void
254  if (myFrom == edge) {
255  myFromLane += offset;
256  } else if (myTo == edge) {
257  myToLane += offset;
258  }
259 }
260 
261 
262 std::ostream&
263 operator<<(std::ostream& os, const NBConnection& c) {
264  os
265  << "Con(from=" << Named::getIDSecure(c.getFrom())
266  << " fromLane=" << c.getFromLane()
267  << " to=" << Named::getIDSecure(c.getTo())
268  << " toLane=" << c.getToLane()
269  << " tlIndex=" << c.getTLIndex()
270  << ")";
271  return os;
272 }
273 
274 
275 
276 /****************************************************************************/
277 
std::string myFromID
The names of both edges, needed for verification of validity.
Definition: NBConnection.h:141
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:291
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled ...
Definition: NBConnection.h:100
virtual ~NBConnection()
Destructor.
static const NBConnection InvalidConnection
Definition: NBConnection.h:127
The representation of a single edge during network building.
Definition: NBEdge.h:71
bool replaceTo(NBEdge *which, NBEdge *by)
replaces the to-edge by the one given
friend bool operator<(const NBConnection &c1, const NBConnection &c2)
Compares both connections in order to allow sorting.
int getFromLane() const
returns the from-lane
static std::string getIDSecure(const T *obj, const std::string &fallBack="NULL")
get an identifier for Named-like object which may be Null
Definition: Named.h:59
NBEdge * getFrom() const
returns the from-edge (start of the connection)
void shiftLaneIndex(NBEdge *edge, int offset)
patches lane indices refering to the given edge
int myFromLane
The lanes; may be -1 if no certain lane was specified.
Definition: NBConnection.h:144
std::string getID() const
returns the id of the connection (!!! not really pretty)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
bool operator==(const NBConnection &c) const
Comparison operator.
static const int InvalidTlIndex
Definition: NBConnection.h:126
bool replaceFrom(NBEdge *which, NBEdge *by)
replaces the from-edge by the one given
NBEdge * checkTo(const NBEdgeCont &ec)
Checks whether the to-edge is still valid.
friend std::ostream & operator<<(std::ostream &os, const NBConnection &c)
Output operator.
NBEdge * myTo
Definition: NBConnection.h:138
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
NBConnection(NBEdge *from, NBEdge *to)
Constructor.
std::string myToID
Definition: NBConnection.h:141
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:255
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
NBEdge * myFrom
The from- and the to-edges.
Definition: NBConnection.h:138
NBEdge * checkFrom(const NBEdgeCont &ec)
Checks whether the from-edge is still valid.