Eclipse SUMO - Simulation of Urban MObility
RailEdge.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2001-2022 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/****************************************************************************/
18// The RailEdge is a wrapper around a ROEdge or a MSEdge used for railway routing
19/****************************************************************************/
20#pragma once
21#include <config.h>
22#include <cassert>
23
24//#define RailEdge_DEBUG_TURNS
25//#define RailEdge_DEBUG_INIT
26//#define RailEdge_DEBUG_SUCCESSORS
27#define RailEdge_DEBUGID ""
28//#define RailEdge_DEBUG_COND(obj) ((obj != 0 && (obj)->getID() == RailEdge_DEBUGID))
29#define RailEdge_DEBUG_COND(obj) (true)
30
31#define REVERSAL_SLACK (POSITION_EPS + NUMERICAL_EPS)
32
33// ===========================================================================
34// class definitions
35// ===========================================================================
37template<class E, class V>
38class RailEdge {
39public:
41 typedef std::vector<std::pair<const _RailEdge*, const _RailEdge*> > ConstEdgePairVector;
42
43 RailEdge(const E* orig) :
45 myOriginal(orig),
46 myTurnaround(nullptr),
47 myIsVirtual(true)
48 { }
49
50 RailEdge(const E* turnStart, const E* turnEnd, int numericalID) :
51 myNumericalID(numericalID),
52 myID("TrainReversal!" + turnStart->getID() + "->" + turnEnd->getID()),
53 myOriginal(nullptr),
54 myTurnaround(nullptr),
55 myIsVirtual(true),
56 myMaxLength(turnStart->getLength() - REVERSAL_SLACK),
57 myStartLength(turnStart->getLength()) {
58 myViaSuccessors.push_back(std::make_pair(turnEnd->getRailwayRoutingEdge(), nullptr));
59 }
60
62 virtual ~RailEdge() {
63 delete myTurnaround;
64 }
65
66 void update(double maxTrainLength, const std::vector<const E*>& replacementEdges) {
67 if (maxTrainLength > myMaxLength) {
68 myMaxLength = maxTrainLength;
69 myReplacementEdges = replacementEdges;
70#ifdef RailEdge_DEBUG_INIT
71 std::cout << " update RailEdge " << getID() << " myMaxLength=" << myMaxLength << " repl=" << toString(myReplacementEdges) << "\n";
72#endif
73 }
74 }
75
76 void addVirtualTurns(const E* forward, const E* backward,
77 std::vector<_RailEdge*>& railEdges, int& numericalID, double dist,
78 double maxTrainLength, const std::vector<const E*>& replacementEdges) {
79 // search backwards until dist and add virtual turnaround edges with
80 // replacement edges up to the real turnaround
81#ifdef RailEdge_DEBUG_INIT
82 std::cout << "addVirtualTurns forward=" << forward->getID() << " backward=" << backward->getID() << " dist=" << dist
83 << " maxLength=" << maxTrainLength << " repl=" << toString(replacementEdges) << "\n";
84#endif
85 if (dist <= 0) {
86 return;
87 }
88 for (const E* prev : forward->getPredecessors()) {
89 if (prev == backward) {
90 continue;
91 }
92 const E* bidi = prev->getBidiEdge();
93 if (bidi != nullptr && backward->isConnectedTo(*bidi, SVC_IGNORING)) {
94 _RailEdge* prevRailEdge = prev->getRailwayRoutingEdge();
95 if (prevRailEdge->myTurnaround == nullptr) {
96 prevRailEdge->myTurnaround = new _RailEdge(prev, bidi, numericalID++);
97 prevRailEdge->myViaSuccessors.push_back(std::make_pair(prevRailEdge->myTurnaround, nullptr));
98 railEdges.push_back(prevRailEdge->myTurnaround);
99#ifdef RailEdge_DEBUG_INIT
100 std::cout << " RailEdge " << prevRailEdge->getID() << " virtual turnaround " << prevRailEdge->myTurnaround->getID() << "\n";
101#endif
102 }
103 /*
104 // doesn't compile though I don't know why
105 auto itFound = std::find(replacementEdges.begin(), replacementEdges.end(), prev);
106 bool notFound = itFound == replacementEdges.end();
107 */
108 bool notFound = true;
109 for (const E* r : replacementEdges) {
110 if (r == prev) {
111 notFound = false;
112 break;
113 }
114 }
115
116 if (notFound) {
117 // prevent loops in replacementEdges
118 prevRailEdge->myTurnaround->update(prev->getLength() + maxTrainLength - REVERSAL_SLACK, replacementEdges);
119 std::vector<const E*> replacementEdges2;
120 replacementEdges2.push_back(prev);
121 replacementEdges2.insert(replacementEdges2.end(), replacementEdges.begin(), replacementEdges.end());
122 addVirtualTurns(prev, bidi, railEdges, numericalID, dist - prev->getLength(),
123 maxTrainLength + prev->getLength(), replacementEdges2);
124 }
125 }
126 }
127 }
128
129 void init(std::vector<_RailEdge*>& railEdges, int& numericalID, double maxTrainLength) {
130 // replace turnaround-via with an explicit RailEdge that checks length
131 for (const auto& viaPair : myOriginal->getViaSuccessors()) {
132 if (viaPair.first == myOriginal->getBidiEdge()) {
133 // direction reversal
134 if (myTurnaround == nullptr) {
135 myTurnaround = new _RailEdge(myOriginal, viaPair.first, numericalID++);
136 myViaSuccessors.push_back(std::make_pair(myTurnaround, nullptr));
137 railEdges.push_back(myTurnaround);
138#ifdef RailEdge_DEBUG_INIT
139 std::cout << " added new turnaround " << myTurnaround->getID() << "\n";
140#endif
141 }
142#ifdef RailEdge_DEBUG_INIT
143 std::cout << "RailEdge " << getID() << " actual turnaround " << myTurnaround->getID() << "\n";
144#endif
145 myTurnaround->myIsVirtual = false;
146 addVirtualTurns(myOriginal, viaPair.first, railEdges, numericalID,
147 maxTrainLength - getLength(), getLength(), std::vector<const E*> {myOriginal});
148 } else {
149 myViaSuccessors.push_back(std::make_pair(viaPair.first->getRailwayRoutingEdge(),
150 viaPair.second == nullptr ? nullptr : viaPair.second->getRailwayRoutingEdge()));
151 }
152 }
153#ifdef RailEdge_DEBUG_SUCCESSORS
154 std::cout << "RailEdge " << getID() << " successors=" << myViaSuccessors.size() << " orig=" << myOriginal->getViaSuccessors().size() << "\n";
155 for (const auto& viaPair : myViaSuccessors) {
156 std::cout << " " << viaPair.first->getID() << "\n";
157 }
158#endif
159 }
160
162 inline int getNumericalID() const {
163 return myNumericalID;
164 }
165
167 const E* getOriginal() const {
168 return myOriginal;
169 }
170
174 const std::string& getID() const {
175 return myOriginal != nullptr ? myOriginal->getID() : myID;
176 }
177
178 void insertOriginalEdges(double length, std::vector<const E*>& into) const {
179 if (myOriginal != nullptr) {
180 into.push_back(myOriginal);
181 } else {
182 double seen = myStartLength;
183 int nPushed = 0;
184 if (seen >= length && !myIsVirtual) {
185 return;
186 }
187 // we need to find a replacement edge that has a real turn
188 for (const E* edge : myReplacementEdges) {
189 into.push_back(edge);
190 nPushed++;
191 seen += edge->getLength();
192 if (seen >= length && edge->isConnectedTo(*edge->getBidiEdge(), SVC_IGNORING)) {
193 break;
194 }
195 //std::cout << "insertOriginalEdges length=" << length << " seen=" << seen << " into=" << toString(into) << "\n";
196 }
197 const int last = (int)into.size() - 1;
198 for (int i = 0; i < nPushed; i++) {
199 into.push_back(into[last - i]->getBidiEdge());
200 }
201 }
202 }
203
207 double getLength() const {
208 return myOriginal == nullptr ? 0 : myOriginal->getLength();
209 }
210
211 //const RailEdge* getBidiEdge() const {
212 // return myOriginal->getBidiEdge()->getRailwayRoutingEdge();
213 //}
214
215 bool isInternal() const {
216 return myOriginal->isInternal();
217 }
218
219 inline bool prohibits(const V* const vehicle) const {
220#ifdef RailEdge_DEBUG_TURNS
221 if (myOriginal == nullptr && RailEdge_DEBUG_COND(vehicle)) {
222 std::cout << getID() << " maxLength=" << myMaxLength << " veh=" << vehicle->getID() << " length=" << vehicle->getLength() << "\n";
223 }
224#endif
225 return vehicle->getLength() > myMaxLength || (myOriginal != nullptr && myOriginal->prohibits(vehicle));
226 }
227
228 inline bool restricts(const V* const vehicle) const {
229 return myOriginal != nullptr && myOriginal->restricts(vehicle);
230 }
231
233 if (vClass == SVC_IGNORING || myOriginal == nullptr || myOriginal->isTazConnector()) { // || !MSNet::getInstance()->hasPermissions()) {
234 return myViaSuccessors;
235 }
236#ifdef HAVE_FOX
237 FXMutexLock lock(mySuccessorMutex);
238#endif
239 auto i = myClassesViaSuccessorMap.find(vClass);
240 if (i != myClassesViaSuccessorMap.end()) {
241 // can use cached value
242 return i->second;
243 }
244 // instantiate vector
246 // this vClass is requested for the first time. rebuild all successors
247 for (const auto& viaPair : myViaSuccessors) {
248 if (viaPair.first->myOriginal == nullptr
249 || viaPair.first->myOriginal->isTazConnector()
250 || myOriginal->isConnectedTo(*viaPair.first->myOriginal, vClass)) {
251 result.push_back(viaPair);
252 }
253 }
254 return result;
255 }
256
257 bool isVirtual() const {
258 return myIsVirtual;
259 }
260
261private:
262 const int myNumericalID;
263 const std::string myID;
264 const E* myOriginal;
267
269 std::vector<const E*> myReplacementEdges;
270
272 double myMaxLength = std::numeric_limits<double>::max();
274 double myStartLength = 0;
275
277 mutable std::map<SUMOVehicleClass, ConstEdgePairVector> myClassesViaSuccessorMap;
278
280
281#ifdef HAVE_FOX
283 mutable FXMutex mySuccessorMutex;
284#endif
285
286};
#define REVERSAL_SLACK
Definition: RailEdge.h:31
#define RailEdge_DEBUG_COND(obj)
Definition: RailEdge.h:29
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_IGNORING
vehicles ignoring classes
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
the edge type representing backward edges
Definition: RailEdge.h:38
RailEdge(const E *orig)
Definition: RailEdge.h:43
const std::string myID
Definition: RailEdge.h:263
double myStartLength
length of the edge where this turn starts
Definition: RailEdge.h:274
void insertOriginalEdges(double length, std::vector< const E * > &into) const
Definition: RailEdge.h:178
bool myIsVirtual
Definition: RailEdge.h:266
int getNumericalID() const
Returns the index (numeric id) of the edge.
Definition: RailEdge.h:162
ConstEdgePairVector myViaSuccessors
Definition: RailEdge.h:279
const std::string & getID() const
Returns the id of the edge.
Definition: RailEdge.h:174
std::vector< const E * > myReplacementEdges
actual edges to return when passing this (turnaround) edge - only forward
Definition: RailEdge.h:269
void addVirtualTurns(const E *forward, const E *backward, std::vector< _RailEdge * > &railEdges, int &numericalID, double dist, double maxTrainLength, const std::vector< const E * > &replacementEdges)
Definition: RailEdge.h:76
bool isVirtual() const
Definition: RailEdge.h:257
double getLength() const
Returns the length of the edge.
Definition: RailEdge.h:207
virtual ~RailEdge()
Destructor.
Definition: RailEdge.h:62
const E * getOriginal() const
Returns the original edge.
Definition: RailEdge.h:167
const E * myOriginal
Definition: RailEdge.h:264
const int myNumericalID
Definition: RailEdge.h:262
bool isInternal() const
Definition: RailEdge.h:215
double myMaxLength
maximum train length for passing this (turnaround) edge
Definition: RailEdge.h:272
bool prohibits(const V *const vehicle) const
Definition: RailEdge.h:219
RailEdge< E, V > _RailEdge
Definition: RailEdge.h:40
bool restricts(const V *const vehicle) const
Definition: RailEdge.h:228
std::map< SUMOVehicleClass, ConstEdgePairVector > myClassesViaSuccessorMap
The successors available for a given vClass.
Definition: RailEdge.h:277
std::vector< std::pair< const _RailEdge *, const _RailEdge * > > ConstEdgePairVector
Definition: RailEdge.h:41
const ConstEdgePairVector & getViaSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
Definition: RailEdge.h:232
void init(std::vector< _RailEdge * > &railEdges, int &numericalID, double maxTrainLength)
Definition: RailEdge.h:129
_RailEdge * myTurnaround
Definition: RailEdge.h:265
RailEdge(const E *turnStart, const E *turnEnd, int numericalID)
Definition: RailEdge.h:50
void update(double maxTrainLength, const std::vector< const E * > &replacementEdges)
Definition: RailEdge.h:66