Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MSLink.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2002-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/****************************************************************************/
20// A connection between lanes
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <vector>
26#include <set>
31
32
33// ===========================================================================
34// class declarations
35// ===========================================================================
36class MSLane;
37class MSJunction;
38class MSVehicle;
39class MSPerson;
40class OutputDevice;
42
43
44// ===========================================================================
45// class definitions
46// ===========================================================================
67class MSLink {
68public:
69
75 LL_IN_THE_WAY = 1 << 0,
77 LL_FROM_LEFT = 1 << 1,
81 LL_SAME_TARGET = 1 << 3
82 };
83
84 struct LinkLeader {
85 LinkLeader(MSVehicle* _veh, double _gap, double _distToCrossing, int _llFlags = LL_FROM_LEFT, double _latOffst = 0) :
86 vehAndGap(std::make_pair(_veh, _gap)),
87 distToCrossing(_distToCrossing),
88 llFlags(_llFlags),
89 latOffset(_latOffst)
90 { }
91
92 inline bool fromLeft() const {
93 return (llFlags & LL_FROM_LEFT) != 0;
94 }
95 inline bool inTheWay() const {
96 return (llFlags & LL_IN_THE_WAY) != 0;
97 }
98 inline bool sameTarget() const {
99 return (llFlags & LL_SAME_TARGET) != 0;
100 }
101 inline bool sameSource() const {
102 return (llFlags & LL_SAME_SOURCE) != 0;
103 }
104
105 std::pair<MSVehicle*, double> vehAndGap;
108 double latOffset;
109
110 };
111
112 typedef std::vector<LinkLeader> LinkLeaders;
113
123 ApproachingVehicleInformation(const SUMOTime _arrivalTime, const SUMOTime _leavingTime,
124 const double _arrivalSpeed, const double _leaveSpeed,
125 const bool _willPass,
126 const double _arrivalSpeedBraking,
127 const SUMOTime _waitingTime,
128 const double _dist,
129 const double _speed,
130 const double _latOffset
131 ) :
132 arrivalTime(_arrivalTime), leavingTime(_leavingTime),
133 arrivalSpeed(_arrivalSpeed), leaveSpeed(_leaveSpeed),
134 willPass(_willPass),
135 arrivalSpeedBraking(_arrivalSpeedBraking),
136 waitingTime(_waitingTime),
137 dist(_dist),
138 speed(_speed),
139 latOffset(_latOffset) {
140 }
141
147 const double arrivalSpeed;
149 const double leaveSpeed;
151 const bool willPass;
157 const double dist;
159 const double speed;
161 const double latOffset;
162
163 };
164
174 ApproachingPersonInformation(const SUMOTime _arrivalTime, const SUMOTime _leavingTime) :
175 arrivalTime(_arrivalTime), leavingTime(_leavingTime) {}
180 };
181
182 typedef std::map<const SUMOVehicle*, const ApproachingVehicleInformation, ComparatorNumericalIdLess> ApproachInfos;
183 typedef std::vector<const SUMOTrafficObject*> BlockingFoes;
184 typedef std::map<const MSPerson*, ApproachingPersonInformation> PersonApproachInfos;
185
193
196
197 ConflictInfo(double lbc, double cs, ConflictFlag fl = CONFLICT_DEFAULT) :
200 conflictSize(cs),
201 flag(fl)
202 {}
209
211
212 double getFoeLengthBehindCrossing(const MSLink* foeExitLink) const;
213 double getFoeConflictSize(const MSLink* foeExitLink) const;
214 double getLengthBehindCrossing(const MSLink* exitLink) const;
215 };
216
219 CustomConflict(const MSLane* f, const MSLane* t, double s, double e) :
220 from(f), to(t), startPos(s), endPos(e) {}
221 const MSLane* from;
222 const MSLane* to;
223 double startPos;
224 double endPos;
225 };
226
235 MSLink(MSLane* predLane,
236 MSLane* succLane,
237 MSLane* via,
238 LinkDirection dir,
239 LinkState state,
240 double length,
241 double foeVisibilityDistance,
242 bool keepClear,
243 MSTrafficLightLogic* logic,
244 int tlLinkIdx,
245 bool indirect);
246
247
249 ~MSLink();
250
251 void addCustomConflict(const MSLane* from, const MSLane* to, double startPos, double endPos);
252
260 void setRequestInformation(int index, bool hasFoes, bool isCont,
261 const std::vector<MSLink*>& foeLinks, const std::vector<MSLane*>& foeLanes,
262 MSLane* internalLaneBefore = 0);
263
265 void addWalkingAreaFoe(const MSLane* lane) {
266 myWalkingAreaFoe = lane;
267 }
268
270 void addWalkingAreaFoeExit(const MSLane* lane) {
272 }
273
276 return myWalkingAreaFoe;
277 }
281
286 void setApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime,
287 const double arrivalSpeed, const double leaveSpeed, const bool setRequest,
288 const double arrivalSpeedBraking,
289 const SUMOTime waitingTime, double dist, double latOffset);
290
292 void setApproaching(const SUMOVehicle* approaching, ApproachingVehicleInformation ai);
293
295 void setApproachingPerson(const MSPerson* approaching, const SUMOTime arrivalTime, const SUMOTime leaveTime);
296
298 void removeApproaching(const SUMOVehicle* veh);
299
301 void removeApproachingPerson(const MSPerson* person);
302
303 /* @brief return information about this vehicle if it is registered as
304 * approaching (dummy values otherwise)
305 * @note used for visualisation of link items */
306 ApproachingVehicleInformation getApproaching(const SUMOVehicle* veh) const;
307 const ApproachingVehicleInformation* getApproachingPtr(const SUMOVehicle* veh) const;
308
312 }
313
318
320 void clearState();
321
329 bool opened(SUMOTime arrivalTime, double arrivalSpeed, double leaveSpeed, double vehicleLength,
330 double impatience, double decel, SUMOTime waitingTime,
331 double posLat = 0,
332 BlockingFoes* collectFoes = nullptr,
333 bool ignoreRed = false,
334 const SUMOTrafficObject* ego = nullptr,
335 double dist = -1) const;
336
352 bool blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
353 bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
354 BlockingFoes* collectFoes = nullptr, const SUMOTrafficObject* ego = nullptr, bool lastWasContRed = false, double dist = -1) const;
355
356
366 bool hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime, double speed, double decel) const;
367
372 std::pair<const SUMOVehicle*, const MSLink*> getFirstApproachingFoe(const MSLink* wrapAround) const;
373
375 return myJunction;
376 }
377
378
384 return myState;
385 }
386
387
393 return myOffState;
394 }
395
401 return myLastGreenState;
402 }
403
404
405 //@brief Returns the time of the last state change
407 return myLastStateChange;
408 }
409
410
416 return myDirection;
417 }
418
419
420
426 void setTLState(LinkState state, SUMOTime t);
427
431 void setTLLogic(const MSTrafficLightLogic* logic);
432
437 inline MSLane* getLane() const {
438 return myLane;
439 }
440
441
446 inline int getIndex() const {
447 return myIndex;
448 }
449
451 inline int getTLIndex() const {
452 return myTLIndex;
453 }
454
456 inline const MSTrafficLightLogic* getTLLogic() const {
457 return myLogic;
458 }
459
463 inline bool havePriority() const {
464 return myState >= 'A' && myState <= 'Z';
465 }
466
467 inline bool haveOffPriority() const {
468 return myOffState >= 'A' && myOffState <= 'Z';
469 }
470
474 inline bool haveRed() const {
476 }
477
478 inline bool haveYellow() const {
480 }
481
482 inline bool haveGreen() const {
484 }
485
486 inline bool mustStop() const {
488 }
489
490 inline bool isTLSControlled() const {
491 return myLogic != 0;
492 }
493
497
502 double getLength() const {
503 return myLength;
504 }
505
506
515 }
516
517 double getDistToFoePedCrossing() const {
519 }
520
525 bool hasFoes() const {
526 return myHasFoes;
527 }
528
529 // @brief return whether the vehicle may continute past this link to wait within the intersection
530 bool isCont() const;
531
532
534 bool keepClear() const {
535 return myKeepClear;
536 }
537
539 bool isIndirect() const {
540 return myAmIndirect;
541 }
542
544 bool lastWasContMajor() const;
545
547 bool lastWasContState(LinkState linkState) const;
548
552 double getInternalLengthsAfter() const;
553
557 double getInternalLengthsBefore() const;
558
564 double getLengthsBeforeCrossing(const MSLane* foeLane) const;
565
566
572 double getLengthBeforeCrossing(const MSLane* foeLane) const;
573
574
579 inline MSLane* getViaLane() const {
580 return myInternalLane;
581 }
582
591 const LinkLeaders getLeaderInfo(const MSVehicle* ego, double dist, std::vector<const MSPerson*>* collectBlockers = 0, bool isShadowLink = false) const;
592
594 double getZipperSpeed(const MSVehicle* ego, const double dist, double vSafe,
595 SUMOTime arrivalTime,
596 const BlockingFoes* foes) const;
597
599 inline MSLane* getViaLaneOrLane() const {
600 return myInternalLane != nullptr ? myInternalLane : myLane;
601 }
602
603
605 inline const MSLane* getLaneBefore() const {
606 assert(myInternalLaneBefore == nullptr || myLaneBefore == myInternalLaneBefore); // lane before mismatch!
607 return myLaneBefore;
608 }
609
611 inline const MSLane* getInternalLaneBefore() const {
613 }
614
616 SUMOTime getLeaveTime(const SUMOTime arrivalTime, const double arrivalSpeed, const double leaveSpeed, const double vehicleLength) const;
617
619 void writeApproaching(OutputDevice& od, const std::string fromLaneID) const;
620
622 MSLink* getParallelLink(int direction) const;
623
626
628 inline bool fromInternalLane() const {
629 return myInternalLaneBefore != nullptr;
630 }
631
633 bool isEntryLink() const;
634
636 bool isConflictEntryLink() const;
637
639 bool isExitLink() const;
640
643
645 const MSLink* getCorrespondingExitLink() const;
646
648 const MSLink* getCorrespondingEntryLink() const;
649
651 bool isInternalJunctionLink() const;
652
655 return myMesoTLSPenalty;
656 }
657
659 double getGreenFraction() const {
660 return myGreenFraction;
661 }
662
664 void setMesoTLSPenalty(const SUMOTime penalty) {
665 myMesoTLSPenalty = penalty;
666 }
667
669 void setGreenFraction(const double fraction) {
670 myGreenFraction = fraction;
671 }
672
673 const std::vector<const MSLane*>& getFoeLanes() const {
674 return myFoeLanes;
675 }
676
677 const std::vector<ConflictInfo>& getConflicts() const {
678 return myConflicts;
679 }
680
681 const std::vector<MSLink*>& getFoeLinks() const {
682 return myFoeLinks;
683 }
684
687 return myPermissions;
688 }
689
691 void initParallelLinks();
692
694 inline double getLateralShift() const {
695 return myLateralShift;
696 }
697
699 std::string getDescription() const;
700
702 std::pair<const SUMOVehicle* const, const ApproachingVehicleInformation> getClosest() const;
703
704 inline bool hasFoeCrossing() const {
706 }
707
709 static void recheckSetRequestInformation();
710
711 static bool ignoreFoe(const SUMOTrafficObject* ego, const SUMOTrafficObject* foe);
712
713 static const double NO_INTERSECTION;
714
715private:
717 static inline bool unsafeMergeSpeeds(double leaderSpeed, double followerSpeed, double leaderDecel, double followerDecel) {
718 // XXX mismatch between continuous an discrete deceleration
719 return (leaderSpeed * leaderSpeed / leaderDecel) <= (followerSpeed * followerSpeed / followerDecel);
720 }
721
723 static bool couldBrakeForLeader(double followDist, double leaderDist, const MSVehicle* follow, const MSVehicle* leader);
724
725 MSLink* computeParallelLink(int direction);
726
728 void checkWalkingAreaFoe(const MSVehicle* ego, const MSLane* foeLane, std::vector<const MSPerson*>* collectBlockers, LinkLeaders& result) const;
729
731 bool isInFront(const MSVehicle* ego, const PositionVector& egoPath, const Position& pPos) const;
732
734 double isOnComingPed(const MSVehicle* ego, const MSPerson* p) const;
735
737 Position getFuturePosition(const MSPerson* p, double timeHorizon = 1) const;
738
739 bool blockedByFoe(const SUMOVehicle* veh, const ApproachingVehicleInformation& avi,
740 SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
741 bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
742 const SUMOTrafficObject* ego) const;
743
745 bool checkContOff() const;
746
748 bool contIntersect(const MSLane* lane, const MSLane* foe);
749
751 double computeDistToDivergence(const MSLane* lane, const MSLane* sibling, double minDist, bool sameSource, double siblingPredLength = 0) const;
752
754 static SUMOTime computeFoeArrivalTimeBraking(SUMOTime arrivalTime, const SUMOVehicle* foe, SUMOTime foeArrivalTime, double impatience, double dist, double& fasb);
755
757 static bool lateralOverlap(double posLat, double width, double posLat2, double width2);
758
760 const CustomConflict* getCustomConflict(const MSLane* foeLane) const;
761
763 void updateDistToFoePedCrossing(double dist);
764
765private:
768
771
774
777
779 const int myTLIndex;
780
783
790
793
796
799 double myLength;
800
806
809
812
813 // @brief whether vehicles may continue past this link to wait within the intersection
815 // @brief whether vehicles may continue past this link to wait within the intersection after switching of the traffic light at this intersection
817
818 // @brief whether vehicles must keep the intersection clear if there is a downstream jam
820
823
824 /* @brief The preceding junction-internal lane, only used at
825 * - exit links (from internal lane to normal lane)
826 * - internal junction links (from internal lane to internal lane)
827 */
829
834
837
838 /* @brief lengths after the crossing point with foeLane
839 * (index corresponds to myFoeLanes)
840 * empty vector for entry links
841 * */
842 std::vector<ConflictInfo> myConflicts;
843
844 std::vector<CustomConflict> myCustomConflicts;
845
846 // TODO: documentation
847 std::vector<MSLink*> myFoeLinks;
848 std::vector<const MSLane*> myFoeLanes;
849
850 /* prioritized links when the traffic light is switched off (only needed for RightOfWay::ALLWAYSTOP)
851 * @note stored as a pointer to save space since it won't be used in most cases
852 */
853 std::vector<MSLink*>* myOffFoeLinks;
854
859
862
863 /* @brief Links with the same origin lane and the same destination edge that may
864 be in conflict for sublane simulation */
865 std::vector<MSLink*> mySublaneFoeLinks;
866 /* @brief Links with the same origin lane and different destination edge that may
867 be in conflict for sublane simulation */
868 std::vector<MSLink*> mySublaneFoeLinks2;
869
870 /* @brief Internal Lanes with the same origin lane and the same destination edge that may
871 be in conflict for sublane simulation */
872 std::vector<MSLane*> mySublaneFoeLanes;
873
876
878 static std::set<std::pair<MSLink*, MSLink*> > myRecheck;
879
882
884 const bool myAmIndirect;
885
887 double myRadius;
888
891
894
896 MSLink(const MSLink& s);
897
900
901};
long long int SUMOTime
Definition GUI.h:36
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)....
@ TURN
The link is a 180 degree turn.
@ TURN_LEFTHAND
The link is a 180 degree turn (left-hand network)
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
@ LINKSTATE_TL_REDYELLOW
The link has red light (must brake) but indicates upcoming green.
@ LINKSTATE_ALLWAY_STOP
This is an uncontrolled, all-way stop link.
@ LINKSTATE_STOP
This is an uncontrolled, minor link, has to stop.
@ LINKSTATE_TL_YELLOW_MAJOR
The link has yellow light, may pass.
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
@ LINKSTATE_TL_YELLOW_MINOR
The link has yellow light, has to brake anyway.
@ LINKSTATE_TL_RED
The link has red light (must brake)
@ LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
The base class for an intersection.
Definition MSJunction.h:58
Representation of a lane in the micro simulation.
Definition MSLane.h:84
The parent class for traffic light logics.
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
Static storage of an output device and its base (abstract) implementation.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
A list of positions.
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition SUMOVehicle.h:62
Definition json.hpp:4471