Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/netbuild/NBConnection.cpp
169665 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 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
/****************************************************************************/
14
/// @file NBConnection.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Sept 2002
19
///
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
// ===========================================================================
35
const int NBConnection::InvalidTlIndex = -1;
36
const NBConnection NBConnection::InvalidConnection("invalidFrom", nullptr, "invalidTo", nullptr);
37
38
// ===========================================================================
39
// method definitions
40
// ===========================================================================
41
NBConnection::NBConnection(NBEdge* from, NBEdge* to) :
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
50
NBConnection::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
60
NBConnection::NBConnection(NBEdge* from, int fromLane,
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
75
NBConnection::~NBConnection() {}
76
77
78
NBConnection::NBConnection(const NBConnection& c) :
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
87
NBEdge*
88
NBConnection::getFrom() const {
89
return myFrom;
90
}
91
92
93
NBEdge*
94
NBConnection::getTo() const {
95
return myTo;
96
}
97
98
99
bool
100
NBConnection::replaceFrom(NBEdge* which, NBEdge* by) {
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
114
bool
115
NBConnection::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
133
bool
134
NBConnection::replaceTo(NBEdge* which, NBEdge* by) {
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
148
bool
149
NBConnection::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
167
bool
168
operator<(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
182
bool
183
NBConnection::operator==(const NBConnection& c) const {
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 &&
188
myTlIndex2 == c.myTlIndex2);
189
}
190
191
192
bool
193
NBConnection::check(const NBEdgeCont& ec) {
194
myFrom = checkFrom(ec);
195
myTo = checkTo(ec);
196
return myFrom != nullptr && myTo != nullptr;
197
}
198
199
200
NBEdge*
201
NBConnection::checkFrom(const NBEdgeCont& ec) {
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
212
NBEdge*
213
NBConnection::checkTo(const NBEdgeCont& ec) {
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
224
std::string
225
NBConnection::getID() const {
226
std::stringstream str;
227
str << myFromID << "_" << myFromLane << "->" << myToID << "_" << myToLane;
228
return str.str();
229
}
230
231
232
int
233
NBConnection::getFromLane() const {
234
return myFromLane;
235
}
236
237
238
int
239
NBConnection::getToLane() const {
240
return myToLane;
241
}
242
243
244
void
245
NBConnection::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
254
std::ostream&
255
operator<<(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
/****************************************************************************/
268
269