Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/router/PedestrianRouter.h
169678 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 PedestrianRouter.h
15
/// @author Jakob Erdmann
16
/// @date Mon, 03 March 2014
17
///
18
// The Pedestrian Router builds a special network and delegates to a SUMOAbstractRouter.
19
/****************************************************************************/
20
#pragma once
21
#include <config.h>
22
23
#include <string>
24
#include <vector>
25
#include <algorithm>
26
#include <assert.h>
27
#include <utils/common/MsgHandler.h>
28
#include <utils/common/SUMOTime.h>
29
#include <utils/common/ToString.h>
30
#include "SUMOAbstractRouter.h"
31
#include "DijkstraRouter.h"
32
#include "IntermodalNetwork.h"
33
34
//#define PedestrianRouter_DEBUG_ROUTES
35
36
37
// ===========================================================================
38
// class definitions
39
// ===========================================================================
40
/**
41
* @class PedestrianRouter
42
* The router for pedestrians (on a bidirectional network of sidewalks and crossings)
43
*/
44
template<class E, class L, class N, class V>
45
class PedestrianRouter : public SUMOAbstractRouter<E, IntermodalTrip<E, N, V> > {
46
private:
47
typedef IntermodalEdge<E, L, N, V> _IntermodalEdge;
48
typedef IntermodalNetwork<E, L, N, V> _IntermodalNetwork;
49
typedef IntermodalTrip<E, N, V> _IntermodalTrip;
50
typedef DijkstraRouter<_IntermodalEdge, _IntermodalTrip> _InternalRouter;
51
52
public:
53
/// Constructor
54
PedestrianRouter():
55
SUMOAbstractRouter<E, _IntermodalTrip>("PedestrianRouter", true, nullptr, nullptr, false, false), myAmClone(false) {
56
myPedNet = new _IntermodalNetwork(E::getAllEdges(), true);
57
myInternalRouter = new _InternalRouter(myPedNet->getAllEdges(), true,
58
gWeightsRandomFactor > 1 ? &_IntermodalEdge::getTravelTimeStaticRandomized : &_IntermodalEdge::getTravelTimeStatic,
59
nullptr, false, nullptr, true);
60
}
61
62
PedestrianRouter(_IntermodalNetwork* net):
63
SUMOAbstractRouter<E, _IntermodalTrip>("PedestrianRouterClone", true, nullptr, nullptr, false, false), myAmClone(true) {
64
myPedNet = net;
65
myInternalRouter = new _InternalRouter(myPedNet->getAllEdges(), true,
66
gWeightsRandomFactor > 1 ? &_IntermodalEdge::getTravelTimeStaticRandomized : &_IntermodalEdge::getTravelTimeStatic,
67
nullptr, false, nullptr, true);
68
}
69
70
/// Destructor
71
virtual ~PedestrianRouter() {
72
delete myInternalRouter;
73
if (!myAmClone) {
74
delete myPedNet;
75
}
76
}
77
78
virtual SUMOAbstractRouter<E, _IntermodalTrip>* clone() {
79
return new PedestrianRouter<E, L, N, V>(myPedNet);
80
}
81
82
/** @brief Builds the route between the given edges using the minimum effort at the given time
83
The definition of the effort depends on the wished routing scheme */
84
double compute(const E* from, const E* to, double departPos, double arrivalPos, double speed,
85
SUMOTime msTime, const N* onlyNode, std::vector<const E*>& into, bool allEdges = false) const {
86
if (getSidewalk<E, L>(from) == 0) {
87
WRITE_WARNINGF(TL("Departure edge '%' does not allow pedestrians."), from->getID());
88
return false;
89
}
90
if (getSidewalk<E, L>(to) == 0) {
91
WRITE_WARNINGF(TL("Destination edge '%' does not allow pedestrians."), to->getID());
92
return false;
93
}
94
_IntermodalTrip trip(from, to, departPos, arrivalPos, speed, msTime, onlyNode);
95
std::vector<const _IntermodalEdge*> intoPed;
96
const bool silent = allEdges; // no warning is needed when called from MSPModel_Striping
97
const bool success = myInternalRouter->compute(myPedNet->getDepartConnector(from),
98
myPedNet->getArrivalConnector(to),
99
&trip, msTime, intoPed, silent);
100
double time = 0.;
101
if (success) {
102
for (const _IntermodalEdge* pedEdge : intoPed) {
103
if (pedEdge->includeInRoute(allEdges)) {
104
into.push_back(pedEdge->getEdge());
105
}
106
time += myInternalRouter->getEffort(pedEdge, &trip, time);
107
}
108
}
109
#ifdef PedestrianRouter_DEBUG_ROUTES
110
std::cout << TIME2STEPS(msTime) << " trip from " << from->getID() << " to " << to->getID()
111
<< " departPos=" << departPos
112
<< " arrivalPos=" << arrivalPos
113
<< " onlyNode=" << (onlyNode == 0 ? "NULL" : onlyNode->getID())
114
<< " edges=" << toString(intoPed)
115
<< " resultEdges=" << toString(into)
116
<< " time=" << time
117
<< "\n";
118
#endif
119
return success ? time : -1.;
120
}
121
122
/** @brief Builds the route between the given edges using the minimum effort at the given time
123
The definition of the effort depends on the wished routing scheme */
124
bool compute(const E*, const E*, const _IntermodalTrip* const,
125
SUMOTime, std::vector<const E*>&, bool) {
126
throw ProcessError(TL("Do not use this method"));
127
}
128
129
void prohibit(const std::map<const E*, double>& toProhibit) {
130
std::map<const _IntermodalEdge*, double> toProhibitPE;
131
for (auto item : toProhibit) {
132
toProhibitPE[myPedNet->getBothDirections(item.first).first] = item.second;
133
toProhibitPE[myPedNet->getBothDirections(item.first).second] = item.second;
134
}
135
myInternalRouter->prohibit(toProhibitPE);
136
}
137
138
double recomputeWalkCosts(const std::vector<const E*>& edges, double speed, double fromPos, double toPos, SUMOTime msTime, double& length) const {
139
// edges are normal edges so we need to reconstruct paths across intersection
140
if (edges.size() == 0) {
141
length = 0;
142
return 0;
143
} else if (edges.size() == 1) {
144
length = fabs(toPos - fromPos);
145
return length / speed;
146
} else {
147
double cost = 0;
148
int last = (int)edges.size() - 1;
149
for (int i = 0; i < last; i++) {
150
std::vector<const E*> into;
151
const E* from = edges[i];
152
const E* to = edges[i + 1];
153
const double fp = (i == 0 ? fromPos : from->getLength() / 2);
154
const double tp = (i == (last - 1) ? toPos : to->getLength() / 2);
155
const N* node = getCommonNode(from, to);
156
if (i == 0) {
157
if (node == from->getToJunction()) {
158
length += from->getLength() - fromPos;
159
} else {
160
length += fromPos;
161
}
162
} else {
163
length += from->getLength();
164
}
165
if (i == (last - 1)) {
166
if (node == to->getFromJunction()) {
167
length += toPos;
168
} else {
169
length += to->getLength() - toPos;
170
}
171
}
172
double time = this->compute(from, to, fp, tp, speed, msTime, node, into, true);
173
if (time >= 0) {
174
cost += time;
175
for (const E* edge : into) {
176
if (edge->isCrossing()) {
177
length += edge->getLength();
178
} else if (edge->isWalkingArea()) {
179
// this is wrong because the length is path-dependent
180
length += edge->getLength();
181
}
182
}
183
} else {
184
throw ProcessError("Could not compute cost between edge '" + from->getID() + "' and edge '" + to->getID() + "'.");
185
}
186
}
187
return cost;
188
}
189
}
190
191
192
private:
193
const bool myAmClone;
194
_InternalRouter* myInternalRouter;
195
_IntermodalNetwork* myPedNet;
196
197
const N* getCommonNode(const E* from, const E* to) const {
198
if (from->getToJunction() == to->getFromJunction() || from->getToJunction() == to->getToJunction()) {
199
return from->getToJunction();
200
} else if (from->getFromJunction() == to->getFromJunction() || from->getFromJunction() == to->getToJunction()) {
201
return from->getFromJunction();
202
} else {
203
return nullptr;
204
}
205
}
206
207
private:
208
/// @brief Invalidated assignment operator
209
PedestrianRouter& operator=(const PedestrianRouter& s);
210
211
};
212
213