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