Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
NBConnection.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
20// The class holds a description of a connection between two edges
21/****************************************************************************/
22#include <config.h>
23
24#include <sstream>
25#include <iostream>
26#include <cassert>
27#include "NBEdgeCont.h"
28#include "NBEdge.h"
29#include "NBConnection.h"
30
31
32// ===========================================================================
33// static members
34// ===========================================================================
35const int NBConnection::InvalidTlIndex = -1;
36const NBConnection NBConnection::InvalidConnection("invalidFrom", nullptr, "invalidTo", nullptr);
37
38// ===========================================================================
39// method definitions
40// ===========================================================================
42 myFrom(from), myTo(to),
43 myFromID(from->getID()), myToID(to->getID()),
44 myFromLane(-1), myToLane(-1),
45 myTlIndex(InvalidTlIndex),
46 myTlIndex2(InvalidTlIndex) {
47}
48
49
50NBConnection::NBConnection(const std::string& fromID, NBEdge* from,
51 const std::string& toID, NBEdge* to) :
52 myFrom(from), myTo(to),
53 myFromID(fromID), myToID(toID),
54 myFromLane(-1), myToLane(-1),
55 myTlIndex(InvalidTlIndex),
56 myTlIndex2(InvalidTlIndex) {
57}
58
59
61 NBEdge* to, int toLane, int tlIndex, int tlIndex2) :
62 myFrom(from), myTo(to),
63 myFromLane(fromLane), myToLane(toLane),
64 myTlIndex(tlIndex),
65 myTlIndex2(tlIndex2) {
66 /* @todo what should we assert here?
67 assert(myFromLane<0||from->getNumLanes()>(int) myFromLane);
68 assert(myToLane<0||to->getNumLanes()>(int) myToLane);
69 */
70 myFromID = from != nullptr ? from->getID() : "";
71 myToID = to != nullptr ? to->getID() : "";
72}
73
74
76
77
79 myFrom(c.myFrom), myTo(c.myTo),
80 myFromID(c.myFromID), myToID(c.myToID),
81 myFromLane(c.myFromLane), myToLane(c.myToLane),
82 myTlIndex(c.myTlIndex),
83 myTlIndex2(c.myTlIndex2) {
84}
85
86
87NBEdge*
89 return myFrom;
90}
91
92
93NBEdge*
95 return myTo;
96}
97
98
99bool
101 if (myFrom == which) {
102 myFrom = by;
103 if (myFrom != nullptr) {
104 myFromID = myFrom->getID();
105 } else {
106 myFromID = "invalidFrom";
107 }
108 return true;
109 }
110 return false;
111}
112
113
114bool
115NBConnection::replaceFrom(NBEdge* which, int whichLane,
116 NBEdge* by, int byLane) {
117 if (myFrom == which && (myFromLane == whichLane || myFromLane < 0 || whichLane < 0)) {
118 myFrom = by;
119 if (myFrom != nullptr) {
120 myFromID = myFrom->getID();
121 } else {
122 myFromID = "invalidFrom";
123 }
124 if (byLane >= 0) {
125 myFromLane = byLane;
126 }
127 return true;
128 }
129 return false;
130}
131
132
133bool
135 if (myTo == which) {
136 myTo = by;
137 if (myTo != nullptr) {
138 myToID = myTo->getID();
139 } else {
140 myToID = "invalidTo";
141 }
142 return true;
143 }
144 return false;
145}
146
147
148bool
149NBConnection::replaceTo(NBEdge* which, int whichLane,
150 NBEdge* by, int byLane) {
151 if (myTo == which && (myToLane == whichLane || myFromLane < 0 || whichLane < 0)) {
152 myTo = by;
153 if (myTo != nullptr) {
154 myToID = myTo->getID();
155 } else {
156 myToID = "invalidTo";
157 }
158 if (byLane >= 0) {
159 myToLane = byLane;
160 }
161 return true;
162 }
163 return false;
164}
165
166
167bool
168operator<(const NBConnection& c1, const NBConnection& c2) {
169 if (c1.myFromID != c2.myFromID) {
170 return c1.myFromID < c2.myFromID;
171 }
172 if (c1.myToID != c2.myToID) {
173 return c1.myToID < c2.myToID;
174 }
175 if (c1.myFromLane != c2.myFromLane) {
176 return c1.myFromLane < c2.myFromLane;
177 }
178 return c1.myToLane < c2.myToLane;
179}
180
181
182bool
184 return (myFrom == c.myFrom && myTo == c.myTo &&
185 myFromID == c.myFromID && myToID == c.myToID &&
186 myFromLane == c.myFromLane && myToLane == c.myToLane &&
187 myTlIndex == c.myTlIndex &&
189}
190
191
192bool
194 myFrom = checkFrom(ec);
195 myTo = checkTo(ec);
196 return myFrom != nullptr && myTo != nullptr;
197}
198
199
200NBEdge*
202 NBEdge* e = ec.retrieve(myFromID);
203 // ok, the edge was not changed
204 if (e == myFrom) {
205 return myFrom;
206 }
207 // try to get the edge
208 return ec.retrievePossiblySplit(myFromID, myToID, true);
209}
210
211
212NBEdge*
214 NBEdge* e = ec.retrieve(myToID);
215 // ok, the edge was not changed
216 if (e == myTo) {
217 return myTo;
218 }
219 // try to get the edge
220 return ec.retrievePossiblySplit(myToID, myFromID, false);
221}
222
223
224std::string
226 std::stringstream str;
227 str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
228 return str.str();
229}
230
231
232int
234 return myFromLane;
235}
236
237
238int
240 return myToLane;
241}
242
243
244void
245NBConnection::shiftLaneIndex(NBEdge* edge, int offset, int threshold) {
246 if (myFrom == edge && myFromLane > threshold) {
247 myFromLane += offset;
248 } else if (myTo == edge && myToLane > threshold) {
249 myToLane += offset;
250 }
251}
252
253
254std::ostream&
255operator<<(std::ostream& os, const NBConnection& c) {
256 os
257 << "Con(from=" << Named::getIDSecure(c.getFrom())
258 << " fromLane=" << c.getFromLane()
259 << " to=" << Named::getIDSecure(c.getTo())
260 << " toLane=" << c.getToLane()
261 << " tlIndex=" << c.getTLIndex()
262 << ")";
263 return os;
264}
265
266
267/****************************************************************************/
bool operator<(const NBConnection &c1, const NBConnection &c2)
std::ostream & operator<<(std::ostream &os, const NBConnection &c)
NBEdge * getFrom() const
returns the from-edge (start of the connection)
std::string getID() const
returns the id of the connection (!!! not really pretty)
std::string myToID
NBConnection(NBEdge *from, NBEdge *to)
Constructor.
int getFromLane() const
returns the from-lane
bool replaceTo(NBEdge *which, NBEdge *by)
replaces the to-edge by the one given
NBEdge * myFrom
The from- and the to-edges.
int myTlIndex2
The index of the internal junction within the controlling traffic light (optional)
int myFromLane
The lanes; may be -1 if no certain lane was specified.
int getTLIndex() const
returns the index within the controlling tls or InvalidTLIndex if this link is unontrolled
static const int InvalidTlIndex
bool operator==(const NBConnection &c) const
Comparison operator.
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
bool replaceFrom(NBEdge *which, NBEdge *by)
replaces the from-edge by the one given
NBEdge * checkFrom(const NBEdgeCont &ec)
Checks whether the from-edge is still valid.
NBEdge * myTo
int getToLane() const
returns the to-lane
NBEdge * getTo() const
returns the to-edge (end of the connection)
virtual ~NBConnection()
Destructor.
bool check(const NBEdgeCont &ec)
checks whether the edges are still valid
std::string myFromID
The names of both edges, needed for verification of validity.
NBEdge * checkTo(const NBEdgeCont &ec)
Checks whether the to-edge is still valid.
static const NBConnection InvalidConnection
Storage for edges, including some functionality operating on multiple edges.
Definition NBEdgeCont.h:59
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
NBEdge * retrievePossiblySplit(const std::string &id, bool downstream) const
Tries to retrieve an edge, even if it is splitted.
The representation of a single edge during network building.
Definition NBEdge.h:92
const std::string & getID() const
Definition NBEdge.h:1528
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