Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
IntermodalNetwork.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2026 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 Edge definition for the Intermodal Router
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <string>
26#include <vector>
27#include <algorithm>
28#include <assert.h>
30#include <utils/common/Named.h>
33#include <utils/geom/Position.h>
35#include "AccessEdge.h"
36#include "CarEdge.h"
37#include "IntermodalEdge.h"
38#include "PedestrianEdge.h"
39#include "PublicTransportEdge.h"
40#include "StopEdge.h"
41
42//#define IntermodalRouter_DEBUG_NETWORK
43//#define IntermodalRouter_DEBUG_ACCESS
44
45
46// ===========================================================================
47// class definitions
48// ===========================================================================
71
72
73
75template<class E, class L, class N, class V>
77private:
82 typedef std::pair<_IntermodalEdge*, _IntermodalEdge*> EdgePair;
83
84public:
85 /* @brief build the pedestrian part of the intermodal network (once)
86 * @param edges The list of MSEdge or ROEdge to build from
87 * @param numericalID the start number for the creation of new edges
88 */
89 IntermodalNetwork(const std::vector<E*>& edges, const bool pedestrianOnly, const int carWalkTransfer = 0)
90 : myNumericalID(0), myCarWalkTransfer(carWalkTransfer), myHavePTSchedules(false) {
91#ifdef IntermodalRouter_DEBUG_NETWORK
92 std::cout << "initIntermodalNetwork\n";
93#endif
94 // build the pedestrian edges and the depart / arrival connectors with lookup tables
95 bool haveSeenWalkingArea = false;
96 for (const E* const edge : edges) {
97 if (edge->isTazConnector()) {
98 // only a single edge
99 _AccessEdge* access = new _AccessEdge(myNumericalID++, edge->getID(), edge);
100 addEdge(access);
101 myDepartLookup[edge].push_back(access);
102 myArrivalLookup[edge].push_back(access);
103 } else {
104 const L* lane = getSidewalk<E, L>(edge);
105 if (lane != nullptr) {
106 if (edge->isWalkingArea()) {
107 // only a single edge
108 addEdge(new _PedestrianEdge(myNumericalID++, edge, lane, true));
109 myBidiLookup[edge] = std::make_pair(myEdges.back(), myEdges.back());
110 myDepartLookup[edge].push_back(myEdges.back());
111 myArrivalLookup[edge].push_back(myEdges.back());
112 haveSeenWalkingArea = true;
113 } else { // regular edge or crossing
114 // forward and backward edges
115 addEdge(new _PedestrianEdge(myNumericalID++, edge, lane, true));
116 addEdge(new _PedestrianEdge(myNumericalID++, edge, lane, false));
117 myBidiLookup[edge] = std::make_pair(myEdges[myNumericalID - 2], myEdges.back());
118 }
119 }
120 if (!edge->isWalkingArea()) {
121 // depart and arrival edges (the router can decide the initial direction to take and the direction to arrive from)
122 _IntermodalEdge* const departConn = new _IntermodalEdge(edge->getID() + "_depart_connector", myNumericalID++, edge, "!connector");
123 _IntermodalEdge* const arrivalConn = new _IntermodalEdge(edge->getID() + "_arrival_connector", myNumericalID++, edge, "!connector");
124 addConnectors(departConn, arrivalConn, 0);
125 }
126 }
127 }
128
129 // build the walking connectors if there are no walking areas
130 for (const E* const edge : edges) {
131 if (edge->isTazConnector() || edge->isInternal()) {
132 continue;
133 }
134 if (haveSeenWalkingArea) {
135 // connectivity needs to be ensured only in the real intermodal case, for simple pedestrian routing we don't have connectors if we have walking areas
136 if (!pedestrianOnly && getSidewalk<E, L>(edge) == nullptr) {
137 const N* const node = edge->getToJunction();
138 if (myWalkingConnectorLookup.count(node) == 0) {
139 addEdge(new _IntermodalEdge(node->getID() + "_walking_connector", myNumericalID++, nullptr, "!connector"));
141 }
142 }
143 } else {
144 for (const N* const node : {
145 edge->getFromJunction(), edge->getToJunction()
146 }) {
147 if (myWalkingConnectorLookup.count(node) == 0) {
148 addEdge(new _IntermodalEdge(node->getID() + "_walking_connector", myNumericalID++, nullptr, "!connector"));
150 }
151 }
152 }
153 }
154 // build the connections
155 for (const E* const edge : edges) {
156 if (edge->isTazConnector()) {
157 // since pedestrians walk in both directions, also allow departing at sinks and arriving at sources
158 _IntermodalEdge* const tazDepart = getDepartConnector(edge);
159 _IntermodalEdge* const tazArrive = getArrivalConnector(edge);
160 const E* other = edge->getOtherTazConnector();
161 _IntermodalEdge* const otherTazDepart = other != nullptr ? getDepartConnector(other) : tazDepart;
162 _IntermodalEdge* const otherTazArrive = other != nullptr ? getArrivalConnector(other) : tazArrive;
163 for (const E* out : edge->getSuccessors()) {
164 if (out->isNormal()) {
165 tazDepart->addSuccessor(getDepartConnector(out));
166 getArrivalConnector(out)->addSuccessor(otherTazArrive);
167 }
168 }
169 for (const E* in : edge->getPredecessors()) {
170 if (in->isNormal()) {
171 getArrivalConnector(in)->addSuccessor(tazArrive);
172 otherTazDepart->addSuccessor(getDepartConnector(in));
173 }
174 }
175 continue;
176 }
177 const L* const sidewalk = getSidewalk<E, L>(edge);
178 if (sidewalk == nullptr) {
179 continue;
180 }
181 // find all incoming and outgoing lanes for the sidewalk and
182 // connect the corresponding IntermodalEdges
183 const EdgePair& pair = getBothDirections(edge);
184#ifdef IntermodalRouter_DEBUG_NETWORK
185 std::cout << " building connections from " << sidewalk->getID() << "\n";
186#endif
187 if (haveSeenWalkingArea) {
188 const std::vector<std::pair<const L*, const E*> > outgoing = sidewalk->getOutgoingViaLanes();
189 // if one of the outgoing lanes is a walking area it must be used.
190 // All other connections shall be ignored
191 // if it has no outgoing walking area, it probably is a walking area itself
192 bool hasWalkingArea = false;
193 for (const auto& target : outgoing) {
194 if (target.first->getEdge().isWalkingArea()) {
195 hasWalkingArea = true;
196 break;
197 }
198 }
199 for (const auto& target : outgoing) {
200 const E* const targetEdge = &(target.first->getEdge());
201 const bool used = (target.first == getSidewalk<E, L>(targetEdge)
202 && (!hasWalkingArea || targetEdge->isWalkingArea()));
203#ifdef IntermodalRouter_DEBUG_NETWORK
204 const L* potTarget = getSidewalk<E, L>(targetEdge);
205 std::cout << " lane=" << (potTarget == 0 ? "NULL" : potTarget->getID()) << (used ? "(used)" : "") << "\n";
206#endif
207 if (used) {
208 const EdgePair& targetPair = getBothDirections(targetEdge);
209 pair.first->addSuccessor(targetPair.first);
210 targetPair.second->addSuccessor(pair.second);
211#ifdef IntermodalRouter_DEBUG_NETWORK
212 std::cout << " " << pair.first->getID() << " -> " << targetPair.first->getID() << "\n";
213 std::cout << " " << targetPair.second->getID() << " -> " << pair.second->getID() << "\n";
214#endif
215 }
216 }
217 }
218 // We may have a network without pedestrian structures or a car-only edge.
219 // In the first case we assume that all sidewalks at a junction are interconnected,
220 // in the second we connect all car-only edges to all sidewalks.
221 _IntermodalEdge* const toNodeConn = myWalkingConnectorLookup[edge->getToJunction()];
222 if (toNodeConn != nullptr) {
223 // Check for the outgoing vias and use the shortest one as an approximation
224 const std::vector<std::pair<const L*, const E*> > outgoing = sidewalk->getOutgoingViaLanes();
225 double minViaLength = std::numeric_limits<double>::max();
226 const E* minVia = nullptr;
227 for (const auto& target : outgoing) {
228 if (target.second != nullptr && target.second->getLength() < minViaLength) {
229 minViaLength = target.second->getLength();
230 minVia = target.second;
231 }
232 }
233 EdgePair interVia = std::make_pair(nullptr, nullptr);
234 if (minVia != nullptr) {
235 const auto it = myBidiLookup.find(minVia);
236 if (it != myBidiLookup.end()) {
237 interVia = it->second;
238 }
239 }
240 if (!haveSeenWalkingArea) {
241 // if we have walking areas we should use them and not the connector
242 pair.first->addSuccessor(toNodeConn, interVia.first);
243 }
244 toNodeConn->addSuccessor(pair.second, interVia.second);
245 }
246 _IntermodalEdge* const fromNodeConn = myWalkingConnectorLookup[edge->getFromJunction()];
247 if (fromNodeConn != nullptr) {
248 if (!haveSeenWalkingArea) {
249 pair.second->addSuccessor(fromNodeConn);
250 }
251 fromNodeConn->addSuccessor(pair.first);
252 }
253 if (!edge->isWalkingArea()) {
254 // build connections from depart connector
255 _IntermodalEdge* startConnector = getDepartConnector(edge);
256 startConnector->addSuccessor(pair.first);
257 startConnector->addSuccessor(pair.second);
258 // build connections to arrival connector
259 _IntermodalEdge* endConnector = getArrivalConnector(edge);
260 pair.first->addSuccessor(endConnector);
261 pair.second->addSuccessor(endConnector);
262#ifdef IntermodalRouter_DEBUG_NETWORK
263 std::cout << " " << startConnector->getID() << " -> " << pair.first->getID() << "\n";
264 std::cout << " " << startConnector->getID() << " -> " << pair.second->getID() << "\n";
265 std::cout << " " << pair.first->getID() << " -> " << endConnector->getID() << "\n";
266 std::cout << " " << pair.second->getID() << " -> " << endConnector->getID() << "\n";
267#endif
268 }
269 }
270 }
271
273 for (typename std::vector<_IntermodalEdge*>::iterator it = myEdges.begin(); it != myEdges.end(); ++it) {
274 delete *it;
275 }
276 }
277
279 while ((int)myEdges.size() <= edge->getNumericalID()) {
280 myEdges.push_back(0);
281 }
282 myEdges[edge->getNumericalID()] = edge;
283 }
284
285 void addConnectors(_IntermodalEdge* const depConn, _IntermodalEdge* const arrConn, const int index) {
286 addEdge(depConn);
287 addEdge(arrConn);
288 myDepartLookup[depConn->getEdge()].insert(myDepartLookup[depConn->getEdge()].begin() + index, depConn);
289 myArrivalLookup[arrConn->getEdge()].insert(myArrivalLookup[arrConn->getEdge()].begin() + index, arrConn);
290 }
291
292 const std::vector<_IntermodalEdge*>& getAllEdges() {
293 return myEdges;
294 }
295
297 bool hasBothDirections(const E* e) const {
298 return myBidiLookup.count(e) > 0;
299 }
300
302 const EdgePair& getBothDirections(const E* e) const {
303 typename std::map<const E*, EdgePair>::const_iterator it = myBidiLookup.find(e);
304 if (it == myBidiLookup.end()) {
305 assert(false);
306 throw ProcessError(TLF("Edge '%' not found in intermodal network.'", e->getID()));
307 }
308 return (*it).second;
309 }
310
312 const _IntermodalEdge* getDepartEdge(const E* e, const double pos) const {
313 typename std::map<const E*, std::vector<_IntermodalEdge*> >::const_iterator it = myDepartLookup.find(e);
314 if (it == myDepartLookup.end()) {
315 throw ProcessError(TLF("Depart edge '%' not found in intermodal network.", e->getID()));
316 }
317 if ((e->getPermissions() & SVC_PEDESTRIAN) == 0) {
318 // use most specific split (best trainStop, quay etc)
319 double bestDist = std::numeric_limits<double>::max();
320 const _IntermodalEdge* best = nullptr;
321 for (const _IntermodalEdge* const split : it->second) {
322 if (pos >= split->getStartPos() - POSITION_EPS && pos <= split->getEndPos() + POSITION_EPS) {
323 const double dist = split->getEndPos() - split->getStartPos();
324 if (dist < bestDist) {
325 bestDist = dist;
326 best = split;
327 }
328 }
329 }
330 assert(best != nullptr);
331 return best;
332 } else {
333 // use next downstream edge
334 const std::vector<_IntermodalEdge*>& splitList = it->second;
335 typename std::vector<_IntermodalEdge*>::const_iterator splitIt = splitList.begin();
336 double totalLength = 0.;
337 while (splitIt + 1 != splitList.end() && totalLength + (*splitIt)->getLength() < pos) {
338 totalLength += (*splitIt)->getLength();
339 ++splitIt;
340 }
341 return *splitIt;
342 }
343 }
344
346 _IntermodalEdge* getDepartConnector(const E* e, const int splitIndex = 0) const {
347 typename std::map<const E*, std::vector<_IntermodalEdge*> >::const_iterator it = myDepartLookup.find(e);
348 if (it == myDepartLookup.end()) {
349 throw ProcessError(TLF("Depart edge '%' not found in intermodal network.", e->getID()));
350 }
351 if (splitIndex >= (int)it->second.size()) {
352 throw ProcessError("Split index " + toString(splitIndex) + " invalid for depart edge '" + e->getID() + "' .");
353 }
354 return it->second[splitIndex];
355 }
356
358 _IntermodalEdge* getArrivalEdge(const E* e, const double pos) const {
359 typename std::map<const E*, std::vector<_IntermodalEdge*> >::const_iterator it = myArrivalLookup.find(e);
360 if (it == myArrivalLookup.end()) {
361 throw ProcessError(TLF("Arrival edge '%' not found in intermodal network.", e->getID()));
362 }
363 const std::vector<_IntermodalEdge*>& splitList = it->second;
364 typename std::vector<_IntermodalEdge*>::const_iterator splitIt = splitList.begin();
365 double totalLength = 0.;
366 while (splitIt != splitList.end() && totalLength + (*splitIt)->getLength() < pos) {
367 totalLength += (*splitIt)->getLength();
368 ++splitIt;
369 }
370 if (splitIt != splitList.end()) {
371 return *splitIt;
372 } else {
373 return splitList.back();
374 }
375 }
376
378 _IntermodalEdge* getArrivalConnector(const E* e, const int splitIndex = 0) const {
379 return myArrivalLookup.find(e)->second[splitIndex];
380 }
381
384 typename std::map<const N*, _IntermodalEdge*>::const_iterator it = myWalkingConnectorLookup.find(e->getToJunction());
385 if (it == myWalkingConnectorLookup.end()) {
386 const L* const sidewalk = getSidewalk<E, L>(e);
387 if (e->isInternal() || sidewalk == 0) {
388 return 0;
389 }
390 for (const auto& target : sidewalk->getOutgoingViaLanes()) {
391 if (target.first->getEdge().isWalkingArea()) {
392 return getBothDirections(&target.first->getEdge()).first;
393 }
394 }
395 return 0;
396 }
397 return it->second;
398 }
399
400 void addCarEdges(const std::vector<E*>& edges, double taxiWait) {
401 for (const E* const edge : edges) {
402 if (edge->getFunction() == SumoXMLEdgeFunc::NORMAL || edge->getFunction() == SumoXMLEdgeFunc::INTERNAL) {
403 myCarLookup[edge] = new CarEdge<E, L, N, V>(myNumericalID++, edge);
404 addEdge(myCarLookup[edge]);
405 }
406 }
407 for (const auto& edgePair : myCarLookup) {
408 _IntermodalEdge* const carEdge = edgePair.second;
409 // connectivity within the car network
410 for (const auto& suc : edgePair.first->getViaSuccessors()) {
411 _IntermodalEdge* const sucCarEdge = getCarEdge(suc.first);
412 _IntermodalEdge* const sucViaEdge = getCarEdge(suc.second);
413 if (sucCarEdge != nullptr) {
414 carEdge->addSuccessor(sucCarEdge, sucViaEdge);
415 }
416 }
417 // connectivity to the pedestrian network (only for normal edges)
418 if (edgePair.first->getFunction() != SumoXMLEdgeFunc::NORMAL) {
419 continue;
420 }
421 if ((myCarWalkTransfer & ALL_JUNCTIONS) != 0) {
422 _IntermodalEdge* const walkCon = getWalkingConnector(edgePair.first);
423 if (walkCon != 0) {
424 carEdge->addSuccessor(walkCon);
425 } else {
426 // we are on an edge where pedestrians are forbidden and want to continue on an arbitrary pedestrian edge
427 for (const E* const out : edgePair.first->getToJunction()->getOutgoing()) {
428 if (!out->isInternal() && !out->isTazConnector() && getSidewalk<E, L>(out) != 0) {
429 carEdge->addSuccessor(getBothDirections(out).first);
430 }
431 }
432 for (const E* const in : edgePair.first->getToJunction()->getIncoming()) {
433 if (!in->isInternal() && !in->isTazConnector() && getSidewalk<E, L>(in) != 0) {
434 carEdge->addSuccessor(getBothDirections(in).second);
435 }
436 }
437 }
438 }
440 // add access edges that allow exiting a taxi
441 _IntermodalEdge* const walkCon = getWalkingConnector(edgePair.first);
442 if (walkCon != 0) {
443 addRestrictedCarExit(carEdge, walkCon, gTaxiClasses);
444 } else {
445 // we are on an edge where pedestrians are forbidden and want to continue on an arbitrary pedestrian edge
446 for (const E* const out : edgePair.first->getToJunction()->getOutgoing()) {
447 if (!out->isInternal() && !out->isTazConnector() && getSidewalk<E, L>(out) != 0) {
449 }
450 }
451 for (const E* const in : edgePair.first->getToJunction()->getIncoming()) {
452 if (!in->isInternal() && !in->isTazConnector() && getSidewalk<E, L>(in) != 0) {
454 }
455 }
456 }
457 }
458 // use intermediate access edge that prevents taxi departure
459 _IntermodalEdge* departConn = getDepartConnector(edgePair.first);
460 _AccessEdge* access = new _AccessEdge(myNumericalID++, departConn, carEdge, 0, (SVCAll & ~SVC_TAXI));
461 addEdge(access);
462 departConn->addSuccessor(access);
463 access->addSuccessor(carEdge);
465 // taxi may depart anywhere but there is a time penalty
466 _AccessEdge* taxiAccess = new _AccessEdge(myNumericalID++, departConn, carEdge, 0, SVC_TAXI, SVC_IGNORING, taxiWait);
467 addEdge(taxiAccess);
468 departConn->addSuccessor(taxiAccess);
469 taxiAccess->addSuccessor(carEdge);
470 }
472 // taxi (as all other cars) may arrive anywhere
473 carEdge->addSuccessor(getArrivalConnector(edgePair.first));
474 } else {
475 // use intermediate access edge that prevents taxi arrival
476 addRestrictedCarExit(carEdge, getArrivalConnector(edgePair.first), (SVCAll & ~gTaxiClasses));
477 }
478 }
479 }
480
482 _IntermodalEdge* getCarEdge(const E* e) const {
483 if (e == nullptr) {
484 return nullptr;
485 }
486 auto it = myCarLookup.find(e);
487 if (it == myCarLookup.end()) {
488 return nullptr;
489 }
490 return it->second;
491 }
492
494 _IntermodalEdge* getStopEdge(const std::string& stopId) const {
495 auto it = myStopConnections.find(stopId);
496 if (it == myStopConnections.end()) {
497 return nullptr;
498 }
499 return it->second;
500 }
501
519 void addAccess(const std::string& stopId, const E* stopEdge, const double startPos, const double endPos, const double length, const SumoXMLTag category, bool isAccess, double taxiWait) {
520 assert(stopEdge != nullptr);
521 const bool transferCarWalk = ((category == SUMO_TAG_PARKING_AREA && (myCarWalkTransfer & PARKING_AREAS) != 0) ||
522 (category == SUMO_TAG_BUS_STOP && (myCarWalkTransfer & PT_STOPS) != 0));
523 const bool transferTaxiWalk = ((category == SUMO_TAG_PARKING_AREA && (myCarWalkTransfer & TAXI_DROPOFF_PARKING_AREAS) != 0) ||
524 (category == SUMO_TAG_BUS_STOP && (myCarWalkTransfer & TAXI_DROPOFF_PT) != 0));
525 const bool transferWalkTaxi = ((category == SUMO_TAG_PARKING_AREA && (myCarWalkTransfer & TAXI_PICKUP_PARKING_AREAS) != 0) ||
526 (category == SUMO_TAG_BUS_STOP && (myCarWalkTransfer & TAXI_PICKUP_PT) != 0));
527 const double pos = (startPos + endPos) / 2.;
528#ifdef IntermodalRouter_DEBUG_ACCESS
529 std::cout << "addAccess stopId=" << stopId << " stopEdge=" << stopEdge->getID() << " pos=" << pos << " length=" << length << " tag=" << toString(category)
530 << " access=" << isAccess << " tWait=" << taxiWait << "\n";
531#endif
532 if (myStopConnections.count(stopId) == 0) {
533 myStopConnections[stopId] = new StopEdge<E, L, N, V>(stopId, myNumericalID++, stopEdge, startPos, endPos);
534 addEdge(myStopConnections[stopId]);
535 }
536 _IntermodalEdge* const stopConn = myStopConnections[stopId];
537 const L* lane = getSidewalk<E, L>(stopEdge);
538 if (lane != nullptr) {
539 const std::pair<_IntermodalEdge*, _IntermodalEdge*>& pair = getBothDirections(stopEdge);
540 double relPos;
541 bool needSplit;
542 const int splitIndex = findSplitIndex(pair.first, pos, relPos, needSplit);
543 _IntermodalEdge* const fwdSplit = needSplit ? new PedestrianEdge<E, L, N, V>(myNumericalID++, stopEdge, lane, true, pos) : nullptr;
544 splitEdge(pair.first, splitIndex, fwdSplit, relPos, length, needSplit, stopConn);
545 _IntermodalEdge* const backSplit = needSplit ? new PedestrianEdge<E, L, N, V>(myNumericalID++, stopEdge, lane, false, pos) : nullptr;
546 splitEdge(pair.second, splitIndex, backSplit, relPos, length, needSplit, stopConn, false);
547 _IntermodalEdge* carSplit = nullptr;
548 if (myCarLookup.count(stopEdge) > 0) {
549 if (needSplit) {
550 carSplit = new CarEdge<E, L, N, V>(myNumericalID++, stopEdge, pos);
551 }
552 splitEdge(myCarLookup[stopEdge], splitIndex, carSplit, relPos, length, needSplit, stopConn, true, false, transferCarWalk);
553 }
554 if (needSplit) {
555 if (carSplit != nullptr && (transferCarWalk || transferTaxiWalk)) {
556 // adding access from car to walk
557 _IntermodalEdge* const beforeSplit = myAccessSplits[myCarLookup[stopEdge]][splitIndex];
558 for (_IntermodalEdge* conn : {
559 fwdSplit, backSplit
560 }) {
561 if (transferCarWalk) {
562 _AccessEdge* access = new _AccessEdge(myNumericalID++, beforeSplit, conn, length);
563 addEdge(access);
564 beforeSplit->addSuccessor(access);
565 access->addSuccessor(conn);
566 } else if (transferTaxiWalk) {
567 addRestrictedCarExit(beforeSplit, stopConn, gTaxiClasses);
568 }
569 }
570 }
571 if (carSplit != nullptr && transferWalkTaxi && !isAccess) {
572 _AccessEdge* access = new _AccessEdge(myNumericalID++, stopConn, carSplit, 0, SVC_TAXI, SVC_IGNORING, taxiWait);
573 addEdge(access);
574 stopConn->addSuccessor(access);
575 access->addSuccessor(carSplit);
576 }
577
578 // fixing depart connections for the forward pedestrian, the backward pedestrian and the car edge
579 _IntermodalEdge* const prevDep = getDepartConnector(stopEdge, splitIndex);
580 const std::vector<_IntermodalEdge*>& backSplitList = myAccessSplits[pair.second];
581 _IntermodalEdge* const backBeforeSplit = backSplitList[backSplitList.size() - 2 - splitIndex];
582 _IntermodalEdge* const depConn = new _IntermodalEdge(stopEdge->getID() + "_depart_connector" + toString(pos), myNumericalID++, stopEdge, "!connector");
583 depConn->addSuccessor(fwdSplit);
584 depConn->addSuccessor(backBeforeSplit);
585 depConn->setLength(fwdSplit->getLength());
586 prevDep->removeSuccessor(backBeforeSplit);
587 prevDep->addSuccessor(backSplit);
588 prevDep->setLength(backSplit->getLength());
589 if (carSplit != nullptr) {
590 depConn->addSuccessor(carSplit);
591 }
592
593 // fixing arrival connections for the forward pedestrian, the backward pedestrian and the car edge
594 _IntermodalEdge* const prevArr = getArrivalConnector(stopEdge, splitIndex);
595 _IntermodalEdge* const fwdBeforeSplit = myAccessSplits[pair.first][splitIndex];
596 _IntermodalEdge* const arrConn = new _IntermodalEdge(stopEdge->getID() + "_arrival_connector" + toString(pos), myNumericalID++, stopEdge, "!connector");
597 fwdSplit->addSuccessor(arrConn);
598 backBeforeSplit->addSuccessor(arrConn);
599 arrConn->setLength(fwdSplit->getLength());
600 fwdSplit->removeSuccessor(prevArr);
601 fwdBeforeSplit->addSuccessor(prevArr);
602 prevArr->setLength(backSplit->getLength());
603 if (carSplit != nullptr) {
604 if (carSplit->removeSuccessor(prevArr)) {
605 carSplit->addSuccessor(arrConn);
606 myAccessSplits[myCarLookup[stopEdge]][splitIndex]->addSuccessor(prevArr);
607 } else {
608 // check for restricted access
609 for (_IntermodalEdge* out : carSplit->getSuccessors()) {
610 _AccessEdge* aOut = dynamic_cast<_AccessEdge*>(out);
611 if (aOut != nullptr && aOut->removeSuccessor(prevArr)) {
612 aOut->addSuccessor(arrConn);
614 myAccessSplits[myCarLookup[stopEdge]][splitIndex],
615 prevArr,
616 aOut->getVehicleRetriction());
617 break;
618 }
619 }
620 }
621 }
622 addConnectors(depConn, arrConn, splitIndex + 1);
623 }
624 } else {
625 // pedestrians cannot walk here:
626 // add stop edge as depart connector so that pedestrians may start at the stop
627 std::vector<_IntermodalEdge*>& splitList = myDepartLookup[stopEdge];
628 assert(splitList.size() > 0);
629 typename std::vector<_IntermodalEdge*>::iterator splitIt = splitList.begin();
630 while (splitIt != splitList.end() && startPos > (*splitIt)->getEndPos()) {
631 ++splitIt;
632 }
633 splitList.insert(splitIt, stopConn);
634
635 if (!isAccess && (transferWalkTaxi || transferCarWalk || transferTaxiWalk)) {
636 _IntermodalEdge* carEdge = myCarLookup[stopEdge];
637 double relPos;
638 bool needSplit;
639 const int splitIndex = findSplitIndex(carEdge, pos, relPos, needSplit);
640 if (needSplit) {
641 _IntermodalEdge* carSplit = new CarEdge<E, L, N, V>(myNumericalID++, stopEdge, pos);
642 splitEdge(carEdge, splitIndex, carSplit, relPos, length, needSplit, stopConn, true, false, false);
643
644 if (transferCarWalk || transferTaxiWalk) {
645 // adding access from car to walk
646 _IntermodalEdge* const beforeSplit = myAccessSplits[myCarLookup[stopEdge]][splitIndex];
647 if (transferCarWalk) {
648 _AccessEdge* access = new _AccessEdge(myNumericalID++, beforeSplit, stopConn, length);
649 addEdge(access);
650 beforeSplit->addSuccessor(access);
651 access->addSuccessor(stopConn);
652 } else if (transferTaxiWalk) {
653 addRestrictedCarExit(beforeSplit, stopConn, gTaxiClasses);
654 }
655 }
656 if (transferWalkTaxi) {
657 _AccessEdge* access = new _AccessEdge(myNumericalID++, stopConn, carSplit, 0, SVC_TAXI, SVC_IGNORING, taxiWait);
658 addEdge(access);
659 stopConn->addSuccessor(access);
660 access->addSuccessor(carSplit);
661 }
662 }
663 }
664 }
665 }
666
667 bool hasPTSchedules() const {
668 return myHavePTSchedules;
669 }
670
671 void addSchedule(const SUMOVehicleParameter& pars, const StopParVector* addStops = nullptr) {
672 SUMOTime lastUntil = 0;
673 StopParVector validStops;
674 if (addStops != nullptr) {
675 // stops are part of a stand-alone route. until times are offsets from vehicle departure
676 for (const SUMOVehicleParameter::Stop& stop : *addStops) {
677 if (myStopConnections.count(stop.busstop) > 0) {
678 // compute stop times for the first vehicle
679 const SUMOTime newUntil = stop.until + pars.depart;
680 if (newUntil >= lastUntil) {
681 validStops.push_back(stop);
682 validStops.back().until = newUntil;
683 lastUntil = newUntil;
684 } else {
685 WRITE_WARNINGF(TL("Ignoring unordered stop at '%' until % for vehicle '%'."), stop.busstop, time2string(stop.until), pars.id);
686 }
687 }
688 }
689 }
690 for (const SUMOVehicleParameter::Stop& stop : pars.stops) {
691 // stops are part of the vehicle until times are absolute times for the first vehicle
692 if (myStopConnections.count(stop.busstop) > 0 && stop.until >= lastUntil) {
693 validStops.push_back(stop);
694 lastUntil = stop.until;
695 } else {
696 if (stop.busstop != "" && stop.until >= 0) {
697 WRITE_WARNINGF(TL("Ignoring stop at '%' until % for vehicle '%'."), stop.busstop, time2string(stop.until), pars.id);
698 }
699 }
700 }
701 if (validStops.size() < 2 && pars.line != "taxi") {
702 WRITE_WARNINGF(TL("Not using public transport line '%' for routing persons. It has less than two usable stops."), pars.line);
703 return;
704 }
705
706 typename std::vector<_PTEdge*>& lineEdges = myPTLines[pars.line];
707 if (lineEdges.empty()) {
708 _IntermodalEdge* lastStop = nullptr;
709 Position lastPos;
710 SUMOTime lastTime = 0;
711 for (const SUMOVehicleParameter::Stop& s : validStops) {
712 _IntermodalEdge* currStop = myStopConnections[s.busstop];
713 Position stopPos = E::getStopPosition(s);
714 if (lastStop != nullptr) {
715 _PTEdge* const newEdge = new _PTEdge(s.busstop, myNumericalID++, lastStop, currStop->getEdge(), pars.line, lastPos.distanceTo(stopPos));
716 addEdge(newEdge);
717 newEdge->addSchedule(pars.id, lastTime, pars.repetitionNumber, pars.repetitionOffset, s.until - lastTime);
718 myHavePTSchedules = true;
719 lastStop->addSuccessor(newEdge);
720 newEdge->addSuccessor(currStop);
721 lineEdges.push_back(newEdge);
722 }
723 lastTime = s.until;
724 lastStop = currStop;
725 lastPos = stopPos;
726 }
727 if (pars.line != "taxi" && validStops.front().busstop == validStops.back().busstop) {
728 myLoopedLines.insert(pars.line);
729 }
730 } else {
731 if (validStops.size() != lineEdges.size() + 1) {
732 WRITE_WARNINGF("Number of stops for public transport line '%' does not match earlier definitions, ignoring schedule.", pars.line);
733 return;
734 }
735 if (lineEdges.front()->getEntryStop() != myStopConnections[validStops.front().busstop]) {
736 WRITE_WARNINGF("Different stop for '%' compared to earlier definitions, ignoring schedule.", pars.line);
737 return;
738 }
739 typename std::vector<_PTEdge*>::const_iterator lineEdge = lineEdges.begin();
740 typename StopParVector::const_iterator s = validStops.begin() + 1;
741 for (; s != validStops.end(); ++s, ++lineEdge) {
742 if ((*lineEdge)->getSuccessors(SVC_IGNORING)[0] != myStopConnections[s->busstop]) {
743 WRITE_WARNINGF("Different stop for '%' compared to earlier definitions, ignoring schedule.", pars.line);
744 return;
745 }
746 }
747 SUMOTime lastTime = validStops.front().until;
748 if (lineEdges.front()->hasSchedule(lastTime)) {
749 WRITE_WARNINGF("Duplicate schedule for '%' at time=%.", pars.line, time2string(lastTime));
750 }
751 for (lineEdge = lineEdges.begin(), s = validStops.begin() + 1; lineEdge != lineEdges.end(); ++lineEdge, ++s) {
752 (*lineEdge)->addSchedule(pars.id, lastTime, pars.repetitionNumber, pars.repetitionOffset, s->until - lastTime);
753 myHavePTSchedules = true;
754 lastTime = s->until;
755 }
756 }
757 }
758
763 void addCarAccess(const E* edge, SUMOVehicleClass svc, double traveltime) {
764 assert(edge != nullptr);
765 assert(myCarLookup.count(edge) != 0);
766 assert(myBidiLookup.count(edge) != 0);
767 EdgePair pedestrianEdges = myBidiLookup[edge];
768 _IntermodalEdge* carEdge = myCarLookup[edge];
769 _AccessEdge* access = new _AccessEdge(myNumericalID++, pedestrianEdges.first, carEdge, 0, svc, SVC_IGNORING, traveltime);
770 addEdge(access);
771 pedestrianEdges.first->addSuccessor(access);
772 pedestrianEdges.second->addSuccessor(access);
773 access->addSuccessor(carEdge);
774 }
775
782 _AccessEdge* access = new _AccessEdge(myNumericalID++, from, to, 0, SVC_IGNORING, vehicleRestriction);
783 addEdge(access);
784 from->addSuccessor(access);
785 access->addSuccessor(to);
786 }
787
788 bool isLooped(const std::string lineID) const {
789 return myLoopedLines.count(lineID) != 0;
790 }
791
792private:
804 int findSplitIndex(_IntermodalEdge* const toSplit, const double pos, double& relPos, bool& needSplit) const {
805 relPos = pos;
806 needSplit = true;
807 int splitIndex = 0;
808 const auto& splitList = myAccessSplits.find(toSplit);
809 if (splitList != myAccessSplits.end() && !splitList->second.empty()) {
810 for (const _IntermodalEdge* const split : splitList->second) {
811 if (relPos < split->getLength() + POSITION_EPS) {
812 break;
813 }
814 relPos -= split->getLength();
815 splitIndex++;
816 }
817 assert(splitIndex < (int)splitList->second.size());
818 if (splitIndex + 1 < (int)splitList->second.size() && fabs(relPos - splitList->second[splitIndex]->getLength()) < POSITION_EPS) {
819 needSplit = false;
820 }
821 }
822 return splitIndex;
823 }
824
837 void splitEdge(_IntermodalEdge* const toSplit, int splitIndex,
838 _IntermodalEdge* afterSplit, const double relPos, const double length, const bool needSplit,
839 _IntermodalEdge* const stopConn, const bool forward = true, const bool addExit = true, const bool addEntry = true) {
840 std::vector<_IntermodalEdge*>& splitList = myAccessSplits[toSplit];
841 if (splitList.empty()) {
842 splitList.push_back(toSplit);
843 }
844 if (!forward) {
845 splitIndex = (int)splitList.size() - 1 - splitIndex;
846 if (!needSplit) {
847 splitIndex--;
848 }
849 }
850 _IntermodalEdge* beforeSplit = splitList[splitIndex];
851 if (needSplit) {
852 addEdge(afterSplit);
853 beforeSplit->transferSuccessors(afterSplit);
854 beforeSplit->addSuccessor(afterSplit);
855 if (forward) {
856 afterSplit->setLength(MAX2(0.0, beforeSplit->getLength() - relPos));
857 beforeSplit->setLength(relPos);
858 } else {
859 afterSplit->setLength(relPos);
860 beforeSplit->setLength(MAX2(0.0, beforeSplit->getLength() - relPos));
861 // rename backward edges for easier referencing
862 const std::string newID = beforeSplit->getID();
863 beforeSplit->setID(afterSplit->getID());
864 afterSplit->setID(newID);
865 }
866 splitList.insert(splitList.begin() + splitIndex + 1, afterSplit);
867 } else {
868 // don't split, use the present split edges
869 afterSplit = splitList[splitIndex + 1];
870 }
871 // add access to / from edge
872 if (addEntry) {
873 _AccessEdge* access = new _AccessEdge(myNumericalID++, beforeSplit, stopConn, length);
874 addEdge(access);
875 beforeSplit->addSuccessor(access);
876 access->addSuccessor(stopConn);
877 }
878 if (addExit) {
879 // pedestrian case only, exit from public to pedestrian
880 _AccessEdge* exit = new _AccessEdge(myNumericalID++, stopConn, afterSplit, length);
881 addEdge(exit);
882 stopConn->addSuccessor(exit);
883 exit->addSuccessor(afterSplit);
884 }
885 }
886
887
888private:
890 std::vector<_IntermodalEdge*> myEdges;
891
893 std::map<const E*, EdgePair> myBidiLookup;
894
896 std::map<const E*, std::vector<_IntermodalEdge*> > myDepartLookup;
897
899 std::map<const E*, std::vector<_IntermodalEdge*> > myArrivalLookup;
900
902 std::map<const N*, _IntermodalEdge*> myWalkingConnectorLookup;
903
905 std::map<const E*, _IntermodalEdge*, ComparatorNumericalIdLess> myCarLookup;
906
908 std::map<std::string, std::vector<_PTEdge*> > myPTLines;
909
911 std::map<std::string, _IntermodalEdge*> myStopConnections;
912
914 std::map<_IntermodalEdge*, std::vector<_IntermodalEdge*> > myAccessSplits;
915
917 std::set<std::string > myLoopedLines;
918
922
923private:
926
927};
long long int SUMOTime
Definition GUI.h:36
ModeChangeOptions
where mode changes are possible
@ TAXI_DROPOFF_ANYWHERE
taxi customer may exit anywhere
@ PT_STOPS
public transport stops and access
@ TAXI_PICKUP_ANYWHERE
taxi customer may be picked up anywhere
@ TAXI_PICKUP_PARKING_AREAS
taxi customer may be picked up at parking areas
@ TAXI_DROPOFF_PT
taxi customer may exit at public transport stops
@ TAXI_DROPOFF_PARKING_AREAS
taxi customer may exit at parking areas
@ ALL_JUNCTIONS
junctions with edges allowing the additional mode
@ TAXI_PICKUP_PT
taxi customer may be picked up at public transport stops
@ PARKING_AREAS
parking areas
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define TL(string)
Definition MsgHandler.h:316
#define TLF(string,...)
Definition MsgHandler.h:318
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:107
const SVCPermissions SVCAll
all VClasses are allowed
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_IGNORING
vehicles ignoring classes
@ SVC_TAXI
vehicle is a taxi
@ SVC_PEDESTRIAN
pedestrian
std::vector< SUMOVehicleParameter::Stop > StopParVector
SumoXMLTag
Numbers representing SUMO-XML - element names.
@ SUMO_TAG_BUS_STOP
A bus stop.
@ SUMO_TAG_PARKING_AREA
A parking area.
int gTaxiClasses
Definition StdDefs.cpp:38
T MAX2(T a, T b)
Definition StdDefs.h:86
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
the access edge connecting different modes that is given to the internal router (SUMOAbstractRouter)
Definition AccessEdge.h:31
SVCPermissions getVehicleRetriction() const
Definition AccessEdge.h:66
the car edge type that is given to the internal router (SUMOAbstractRouter)
Definition CarEdge.h:34
the base edge type that is given to the internal router (SUMOAbstractRouter)
const E * getEdge() const
void setLength(const double length)
void transferSuccessors(IntermodalEdge *to)
bool removeSuccessor(const IntermodalEdge *const edge)
void addSuccessor(IntermodalEdge *const s, IntermodalEdge *const via=nullptr)
double getLength() const
required by DijkstraRouter et al for external effort computation
int getNumericalID() const
virtual const std::vector< IntermodalEdge * > & getSuccessors(SUMOVehicleClass vClass=SVC_IGNORING) const
virtual double getEndPos() const
the intermodal network storing edges, connections and the mappings to the "real" edges
_IntermodalEdge * getWalkingConnector(const E *e) const
Returns the outgoing pedestrian edge, which is either a walking area or a walking connector.
const std::vector< _IntermodalEdge * > & getAllEdges()
_IntermodalEdge * getArrivalConnector(const E *e, const int splitIndex=0) const
Returns the arriving intermodal connector at the given split offset.
PublicTransportEdge< E, L, N, V > _PTEdge
_IntermodalEdge * getCarEdge(const E *e) const
Returns the associated car edge.
std::map< const E *, _IntermodalEdge *, ComparatorNumericalIdLess > myCarLookup
retrieve the car edge for the given input edge E
void addCarAccess(const E *edge, SUMOVehicleClass svc, double traveltime)
Adds access edges for transfering from walking to vehicle use.
void addAccess(const std::string &stopId, const E *stopEdge, const double startPos, const double endPos, const double length, const SumoXMLTag category, bool isAccess, double taxiWait)
Adds access edges for stopping places to the intermodal network.
std::map< const E *, std::vector< _IntermodalEdge * > > myArrivalLookup
retrieve the arrival edges for the given input edge E
std::map< const N *, _IntermodalEdge * > myWalkingConnectorLookup
the walking connector edge (fake walking area)
std::map< std::string, _IntermodalEdge * > myStopConnections
retrieve the representing edge for the given stopping place
std::map< _IntermodalEdge *, std::vector< _IntermodalEdge * > > myAccessSplits
retrieve the splitted edges for the given "original"
bool hasPTSchedules() const
_IntermodalEdge * getStopEdge(const std::string &stopId) const
Returns the associated stop edge.
void addEdge(_IntermodalEdge *edge)
std::vector< _IntermodalEdge * > myEdges
the edge dictionary
AccessEdge< E, L, N, V > _AccessEdge
IntermodalNetwork & operator=(const IntermodalNetwork &s)
Invalidated assignment operator.
std::map< const E *, EdgePair > myBidiLookup
retrieve the forward and backward edge for the given input edge E
std::set< std::string > myLoopedLines
looped lines need extra checking when building itineraries
bool isLooped(const std::string lineID) const
std::map< std::string, std::vector< _PTEdge * > > myPTLines
retrieve the public transport edges for the given line
void addRestrictedCarExit(_IntermodalEdge *from, _IntermodalEdge *to, SVCPermissions vehicleRestriction)
Adds access edges for transfering from driving to walking that are only usable by a particular vehicl...
void addConnectors(_IntermodalEdge *const depConn, _IntermodalEdge *const arrConn, const int index)
const EdgePair & getBothDirections(const E *e) const
Returns the pair of forward and backward edge.
bool hasBothDirections(const E *e) const
Returns whether the network contains pedestrian edges for the given edge.
void addSchedule(const SUMOVehicleParameter &pars, const StopParVector *addStops=nullptr)
int findSplitIndex(_IntermodalEdge *const toSplit, const double pos, double &relPos, bool &needSplit) const
Returns where to insert or use the split edge.
void addCarEdges(const std::vector< E * > &edges, double taxiWait)
PedestrianEdge< E, L, N, V > _PedestrianEdge
void splitEdge(_IntermodalEdge *const toSplit, int splitIndex, _IntermodalEdge *afterSplit, const double relPos, const double length, const bool needSplit, _IntermodalEdge *const stopConn, const bool forward=true, const bool addExit=true, const bool addEntry=true)
Splits an edge (if necessary) and connects it to a stopping edge.
IntermodalNetwork(const std::vector< E * > &edges, const bool pedestrianOnly, const int carWalkTransfer=0)
_IntermodalEdge * getArrivalEdge(const E *e, const double pos) const
Returns the arriving intermodal edge.
IntermodalEdge< E, L, N, V > _IntermodalEdge
std::map< const E *, std::vector< _IntermodalEdge * > > myDepartLookup
retrieve the depart edges for the given input edge E
_IntermodalEdge * getDepartConnector(const E *e, const int splitIndex=0) const
Returns the departing intermodal connector at the given split offset.
std::pair< _IntermodalEdge *, _IntermodalEdge * > EdgePair
const _IntermodalEdge * getDepartEdge(const E *e, const double pos) const
Returns the departing intermodal edge.
virtual void setID(const std::string &newID)
resets the id
Definition Named.h:81
const std::string & getID() const
Returns the id.
Definition Named.h:73
the pedestrian edge type that is given to the internal router (SUMOAbstractRouter)
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double distanceTo(const Position &p2) const
returns the euclidean distance in 3 dimensions
Definition Position.h:263
the public transport edge type connecting the stop edges
void addSchedule(const std::string id, const SUMOTime begin, const long long int repetitionNumber, const SUMOTime period, const SUMOTime travelTime)
Definition of vehicle stop (position and duration)
Structure representing possible vehicle parameter.
SUMOTime repetitionOffset
The time offset between vehicle reinsertions.
std::string id
The vehicle's id.
std::vector< Stop > stops
List of the stops the vehicle will make, TraCI may add entries here.
std::string line
The vehicle's line (mainly for public transport)
the stop edge type representing bus and train stops
Definition StopEdge.h:31
unsigned node