SUMO - Simulation of Urban MObility
NBConnection.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
19 // The class holds a description of a connection between two edges
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <sstream>
33 #include <iostream>
34 #include <cassert>
35 #include "NBEdgeCont.h"
36 #include "NBEdge.h"
37 #include "NBConnection.h"
38 
39 
40 // ===========================================================================
41 // static members
42 // ===========================================================================
43 const int NBConnection::InvalidTlIndex = -1;
44 const NBConnection NBConnection::InvalidConnection("invalidFrom", 0, "invalidTo", 0);
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50  myFrom(from), myTo(to),
51  myFromID(from->getID()), myToID(to->getID()),
52  myFromLane(-1), myToLane(-1),
53  myTlIndex(InvalidTlIndex) {
54 }
55 
56 
57 NBConnection::NBConnection(const std::string& fromID, NBEdge* from,
58  const std::string& toID, NBEdge* to) :
59  myFrom(from), myTo(to),
60  myFromID(fromID), myToID(toID),
61  myFromLane(-1), myToLane(-1),
63 }
64 
65 
66 NBConnection::NBConnection(NBEdge* from, int fromLane,
67  NBEdge* to, int toLane, int tlIndex) :
68  myFrom(from), myTo(to),
69  myFromLane(fromLane), myToLane(toLane),
70  myTlIndex(tlIndex) {
71  /* @todo what should we assert here?
72  assert(myFromLane<0||from->getNumLanes()>(int) myFromLane);
73  assert(myToLane<0||to->getNumLanes()>(int) myToLane);
74  */
75  myFromID = from->getID();
76  myToID = to != 0 ? to->getID() : "";
77 }
78 
79 
81 
82 
84  myFrom(c.myFrom), myTo(c.myTo),
87  myTlIndex(c.myTlIndex) {
88 }
89 
90 
91 NBEdge*
93  return myFrom;
94 }
95 
96 
97 NBEdge*
99  return myTo;
100 }
101 
102 
103 bool
105  if (myFrom == which) {
106  myFrom = by;
107  if (myFrom != 0) {
108  myFromID = myFrom->getID();
109  } else {
110  myFromID = "invalidFrom";
111  }
112  return true;
113  }
114  return false;
115 }
116 
117 
118 bool
119 NBConnection::replaceFrom(NBEdge* which, int whichLane,
120  NBEdge* by, int byLane) {
121  if (myFrom == which && (myFromLane == whichLane || myFromLane < 0 || whichLane < 0)) {
122  myFrom = by;
123  if (myFrom != 0) {
124  myFromID = myFrom->getID();
125  } else {
126  myFromID = "invalidFrom";
127  }
128  if (byLane >= 0) {
129  myFromLane = byLane;
130  }
131  return true;
132  }
133  return false;
134 }
135 
136 
137 bool
139  if (myTo == which) {
140  myTo = by;
141  if (myTo != 0) {
142  myToID = myTo->getID();
143  } else {
144  myToID = "invalidTo";
145  }
146  return true;
147  }
148  return false;
149 }
150 
151 
152 bool
153 NBConnection::replaceTo(NBEdge* which, int whichLane,
154  NBEdge* by, int byLane) {
155  if (myTo == which && (myToLane == whichLane || myFromLane < 0 || whichLane < 0)) {
156  myTo = by;
157  if (myTo != 0) {
158  myToID = myTo->getID();
159  } else {
160  myToID = "invalidTo";
161  }
162  if (byLane >= 0) {
163  myToLane = byLane;
164  }
165  return true;
166  }
167  return false;
168 }
169 
170 
171 bool
172 operator<(const NBConnection& c1, const NBConnection& c2) {
173  if (c1.myFromID != c2.myFromID) {
174  return c1.myFromID < c2.myFromID;
175  }
176  if (c1.myToID != c2.myToID) {
177  return c1.myToID < c2.myToID;
178  }
179  if (c1.myFromLane != c2.myFromLane) {
180  return c1.myFromLane < c2.myFromLane;
181  }
182  return c1.myToLane < c2.myToLane;
183 }
184 
185 
186 bool
188  return (myFrom == c.myFrom && myTo == c.myTo &&
189  myFromID == c.myFromID && myToID == c.myToID &&
190  myFromLane == c.myFromLane && myToLane == c.myToLane &&
191  myTlIndex == c.myTlIndex);
192 }
193 
194 
195 bool
197  myFrom = checkFrom(ec);
198  myTo = checkTo(ec);
199  return myFrom != 0 && myTo != 0;
200 }
201 
202 
203 NBEdge*
205  NBEdge* e = ec.retrieve(myFromID);
206  // ok, the edge was not changed
207  if (e == myFrom) {
208  return myFrom;
209  }
210  // try to get the edge
211  return ec.retrievePossiblySplit(myFromID, myToID, true);
212 }
213 
214 
215 NBEdge*
217  NBEdge* e = ec.retrieve(myToID);
218  // ok, the edge was not changed
219  if (e == myTo) {
220  return myTo;
221  }
222  // try to get the edge
223  return ec.retrievePossiblySplit(myToID, myFromID, false);
224 }
225 
226 
227 std::string
229  std::stringstream str;
230  str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
231  return str.str();
232 }
233 
234 
235 int
237  return myFromLane;
238 }
239 
240 
241 int
243  return myToLane;
244 }
245 
246 
247 void
249  if (myFrom == edge) {
250  myFromLane += offset;
251  } else if (myTo == edge) {
252  myToLane += offset;
253  }
254 }
255 
256 
257 std::ostream&
258 operator<<(std::ostream& os, const NBConnection& c) {
259  os
260  << "Con(from=" << Named::getIDSecure(c.getFrom())
261  << " fromLane=" << c.getFromLane()
262  << " to=" << Named::getIDSecure(c.getTo())
263  << " toLane=" << c.getToLane()
264  << " tlIndex=" << c.getTLIndex()
265  << ")";
266  return os;
267 }
268 
269 
270 
271 /****************************************************************************/
272 
std::string myFromID
The names of both edges, needed for verification of validity.
Definition: NBConnection.h:140
std::string getID() const
returns the id of the connection (!!! not really pretty)
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
virtual ~NBConnection()
Destructor.
static const NBConnection InvalidConnection
Definition: NBConnection.h:126
The representation of a single edge during network building.
Definition: NBEdge.h:70
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.
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:67
NBEdge * getFrom() const
returns the from-edge (start of the connection)
const std::string & getID() const
Returns the id.
Definition: Named.h:74
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:143
static const int InvalidTlIndex
Definition: NBConnection.h:125
bool replaceFrom(NBEdge *which, NBEdge *by)
replaces the from-edge by the one given
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
Definition: NBEdgeCont.cpp:286
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:137
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:140
bool operator==(const NBConnection &c) const
Comparison operator.
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:250
int getFromLane() const
returns the from-lane
NBEdge * myFrom
The from- and the to-edges.
Definition: NBConnection.h:137
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled ...
Definition: NBConnection.h:99
NBEdge * checkFrom(const NBEdgeCont &ec)
Checks whether the from-edge is still valid.