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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // The class holds a description of a connection between two edges
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <sstream>
27 #include <iostream>
28 #include <cassert>
29 #include "NBEdgeCont.h"
30 #include "NBEdge.h"
31 #include "NBConnection.h"
32 
33 
34 // ===========================================================================
35 // static members
36 // ===========================================================================
37 const int NBConnection::InvalidTlIndex = -1;
38 const NBConnection NBConnection::InvalidConnection("invalidFrom", nullptr, "invalidTo", nullptr);
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  myFrom(from), myTo(to),
45  myFromID(from->getID()), myToID(to->getID()),
46  myFromLane(-1), myToLane(-1),
47  myTlIndex(InvalidTlIndex) {
48 }
49 
50 
51 NBConnection::NBConnection(const std::string& fromID, NBEdge* from,
52  const std::string& toID, NBEdge* to) :
53  myFrom(from), myTo(to),
54  myFromID(fromID), myToID(toID),
55  myFromLane(-1), myToLane(-1),
57 }
58 
59 
60 NBConnection::NBConnection(NBEdge* from, int fromLane,
61  NBEdge* to, int toLane, int tlIndex) :
62  myFrom(from), myTo(to),
63  myFromLane(fromLane), myToLane(toLane),
64  myTlIndex(tlIndex) {
65  /* @todo what should we assert here?
66  assert(myFromLane<0||from->getNumLanes()>(int) myFromLane);
67  assert(myToLane<0||to->getNumLanes()>(int) myToLane);
68  */
69  myFromID = from->getID();
70  myToID = to != nullptr ? to->getID() : "";
71 }
72 
73 
75 
76 
78  myFrom(c.myFrom), myTo(c.myTo),
81  myTlIndex(c.myTlIndex) {
82 }
83 
84 
85 NBEdge*
87  return myFrom;
88 }
89 
90 
91 NBEdge*
93  return myTo;
94 }
95 
96 
97 bool
99  if (myFrom == which) {
100  myFrom = by;
101  if (myFrom != nullptr) {
102  myFromID = myFrom->getID();
103  } else {
104  myFromID = "invalidFrom";
105  }
106  return true;
107  }
108  return false;
109 }
110 
111 
112 bool
113 NBConnection::replaceFrom(NBEdge* which, int whichLane,
114  NBEdge* by, int byLane) {
115  if (myFrom == which && (myFromLane == whichLane || myFromLane < 0 || whichLane < 0)) {
116  myFrom = by;
117  if (myFrom != nullptr) {
118  myFromID = myFrom->getID();
119  } else {
120  myFromID = "invalidFrom";
121  }
122  if (byLane >= 0) {
123  myFromLane = byLane;
124  }
125  return true;
126  }
127  return false;
128 }
129 
130 
131 bool
133  if (myTo == which) {
134  myTo = by;
135  if (myTo != nullptr) {
136  myToID = myTo->getID();
137  } else {
138  myToID = "invalidTo";
139  }
140  return true;
141  }
142  return false;
143 }
144 
145 
146 bool
147 NBConnection::replaceTo(NBEdge* which, int whichLane,
148  NBEdge* by, int byLane) {
149  if (myTo == which && (myToLane == whichLane || myFromLane < 0 || whichLane < 0)) {
150  myTo = by;
151  if (myTo != nullptr) {
152  myToID = myTo->getID();
153  } else {
154  myToID = "invalidTo";
155  }
156  if (byLane >= 0) {
157  myToLane = byLane;
158  }
159  return true;
160  }
161  return false;
162 }
163 
164 
165 bool
166 operator<(const NBConnection& c1, const NBConnection& c2) {
167  if (c1.myFromID != c2.myFromID) {
168  return c1.myFromID < c2.myFromID;
169  }
170  if (c1.myToID != c2.myToID) {
171  return c1.myToID < c2.myToID;
172  }
173  if (c1.myFromLane != c2.myFromLane) {
174  return c1.myFromLane < c2.myFromLane;
175  }
176  return c1.myToLane < c2.myToLane;
177 }
178 
179 
180 bool
182  return (myFrom == c.myFrom && myTo == c.myTo &&
183  myFromID == c.myFromID && myToID == c.myToID &&
184  myFromLane == c.myFromLane && myToLane == c.myToLane &&
185  myTlIndex == c.myTlIndex);
186 }
187 
188 
189 bool
191  myFrom = checkFrom(ec);
192  myTo = checkTo(ec);
193  return myFrom != nullptr && myTo != nullptr;
194 }
195 
196 
197 NBEdge*
199  NBEdge* e = ec.retrieve(myFromID);
200  // ok, the edge was not changed
201  if (e == myFrom) {
202  return myFrom;
203  }
204  // try to get the edge
205  return ec.retrievePossiblySplit(myFromID, myToID, true);
206 }
207 
208 
209 NBEdge*
211  NBEdge* e = ec.retrieve(myToID);
212  // ok, the edge was not changed
213  if (e == myTo) {
214  return myTo;
215  }
216  // try to get the edge
217  return ec.retrievePossiblySplit(myToID, myFromID, false);
218 }
219 
220 
221 std::string
223  std::stringstream str;
224  str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
225  return str.str();
226 }
227 
228 
229 int
231  return myFromLane;
232 }
233 
234 
235 int
237  return myToLane;
238 }
239 
240 
241 void
242 NBConnection::shiftLaneIndex(NBEdge* edge, int offset, int threshold) {
243  if (myFrom == edge && myFromLane > threshold) {
244  myFromLane += offset;
245  } else if (myTo == edge && myToLane > threshold) {
246  myToLane += offset;
247  }
248 }
249 
250 
251 std::ostream&
252 operator<<(std::ostream& os, const NBConnection& c) {
253  os
254  << "Con(from=" << Named::getIDSecure(c.getFrom())
255  << " fromLane=" << c.getFromLane()
256  << " to=" << Named::getIDSecure(c.getTo())
257  << " toLane=" << c.getToLane()
258  << " tlIndex=" << c.getTLIndex()
259  << ")";
260  return os;
261 }
262 
263 
264 
265 /****************************************************************************/
266 
std::string myFromID
The names of both edges, needed for verification of validity.
Definition: NBConnection.h:135
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:121
The representation of a single edge during network building.
Definition: NBEdge.h:65
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:71
NBEdge * getFrom() const
returns the from-edge (start of the connection)
const std::string & getID() const
Returns the id.
Definition: Named.h:78
int myFromLane
The lanes; may be -1 if no certain lane was specified.
Definition: NBConnection.h:138
static const int InvalidTlIndex
Definition: NBConnection.h:120
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:281
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:132
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:61
NBConnection(NBEdge *from, NBEdge *to)
Constructor.
std::string myToID
Definition: NBConnection.h:135
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:245
void shiftLaneIndex(NBEdge *edge, int offset, int threshold=-1)
patches lane indices refering to the given edge and above the threshold by the given offset ...
int getFromLane() const
returns the from-lane
NBEdge * myFrom
The from- and the to-edges.
Definition: NBConnection.h:132
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled ...
Definition: NBConnection.h:94
NBEdge * checkFrom(const NBEdgeCont &ec)
Checks whether the from-edge is still valid.