Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-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// A connection between lanes
21/****************************************************************************/
22#pragma once
23#include <config.h>
24
25#include <vector>
26#include <set>
31#include "MSLane.h"
32
33
34// ===========================================================================
35// class declarations
36// ===========================================================================
37class MSLane;
38class MSJunction;
39class MSVehicle;
40class MSPerson;
41class OutputDevice;
43
44
45// ===========================================================================
46// class definitions
47// ===========================================================================
68class MSLink {
69public:
70
76 LL_IN_THE_WAY = 1 << 0,
78 LL_FROM_LEFT = 1 << 1,
82 LL_SAME_TARGET = 1 << 3
83 };
84
85 struct LinkLeader {
86 LinkLeader(MSVehicle* _veh, double _gap, double _distToCrossing, int _llFlags = LL_FROM_LEFT, double _latOffst = 0) :
87 vehAndGap(std::make_pair(_veh, _gap)),
88 distToCrossing(_distToCrossing),
89 llFlags(_llFlags),
90 latOffset(_latOffst)
91 { }
92
93 inline bool fromLeft() const {
94 return (llFlags & LL_FROM_LEFT) != 0;
95 }
96 inline bool inTheWay() const {
97 return (llFlags & LL_IN_THE_WAY) != 0;
98 }
99 inline bool sameTarget() const {
100 return (llFlags & LL_SAME_TARGET) != 0;
101 }
102 inline bool sameSource() const {
103 return (llFlags & LL_SAME_SOURCE) != 0;
104 }
105
106 std::pair<MSVehicle*, double> vehAndGap;
109 double latOffset;
110
111 };
112
113 typedef std::vector<LinkLeader> LinkLeaders;
114
124 ApproachingVehicleInformation(const SUMOTime _arrivalTime, const SUMOTime _leavingTime,
125 const double _arrivalSpeed, const double _leaveSpeed,
126 const bool _willPass,
127 const double _arrivalSpeedBraking,
128 const SUMOTime _waitingTime,
129 const double _dist,
130 const double _speed,
131 const double _latOffset
132 ) :
133 arrivalTime(_arrivalTime), leavingTime(_leavingTime),
134 arrivalSpeed(_arrivalSpeed), leaveSpeed(_leaveSpeed),
135 willPass(_willPass),
136 arrivalSpeedBraking(_arrivalSpeedBraking),
137 waitingTime(_waitingTime),
138 dist(_dist),
139 speed(_speed),
140 latOffset(_latOffset) {
141 }
142
158 double dist;
160 double speed;
162 double latOffset;
163
164 };
165
175 ApproachingPersonInformation(const SUMOTime _arrivalTime, const SUMOTime _leavingTime) :
176 arrivalTime(_arrivalTime), leavingTime(_leavingTime) {}
181 };
182
183 typedef std::map<const SUMOVehicle*, ApproachingVehicleInformation, ComparatorNumericalIdLess> ApproachInfos;
184 typedef std::vector<const SUMOTrafficObject*> BlockingFoes;
185 typedef std::map<const MSPerson*, ApproachingPersonInformation> PersonApproachInfos;
186
194
197
198 ConflictInfo(double lbc, double cs, ConflictFlag fl = CONFLICT_DEFAULT) :
201 conflictSize(cs),
202 flag(fl)
203 {}
210
212
213 double getFoeLengthBehindCrossing(const MSLink* foeExitLink) const;
214 double getFoeConflictSize(const MSLink* foeExitLink) const;
215 double getLengthBehindCrossing(const MSLink* exitLink) const;
216 };
217
220 CustomConflict(const MSLane* f, const MSLane* t, double s, double e) :
221 from(f), to(t), startPos(s), endPos(e) {}
222 const MSLane* from;
223 const MSLane* to;
224 double startPos;
225 double endPos;
226 };
227
230 bool operator()(const MSLink* const a, const MSLink* const b) const {
231 return (a->getLane()->getNumericalID() < b->getLane()->getNumericalID())
232 || (a->getLane()->getNumericalID() == b->getLane()->getNumericalID() && a->getIndex() < b->getIndex());
233 }
234 };
235
236
245 MSLink(MSLane* predLane,
246 MSLane* succLane,
247 MSLane* via,
248 LinkDirection dir,
249 LinkState state,
250 double length,
251 double foeVisibilityDistance,
252 bool keepClear,
253 MSTrafficLightLogic* logic,
254 int tlLinkIdx,
255 bool indirect);
256
257
259 ~MSLink();
260
261 void addCustomConflict(const MSLane* from, const MSLane* to, double startPos, double endPos);
262
270 void setRequestInformation(int index, bool hasFoes, bool isCont,
271 const std::vector<MSLink*>& foeLinks, const std::vector<MSLane*>& foeLanes,
272 MSLane* internalLaneBefore = 0);
273
275 void addWalkingAreaFoe(const MSLane* lane) {
276 myWalkingAreaFoe = lane;
277 }
278
280 void addWalkingAreaFoeExit(const MSLane* lane) {
282 }
283
286 return myWalkingAreaFoe;
287 }
291
296 void setApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime,
297 const double arrivalSpeed, const double leaveSpeed, const bool setRequest,
298 const double arrivalSpeedBraking,
299 const SUMOTime waitingTime, double dist, double latOffset);
300
302 void updateApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime,
303 const double arrivalSpeed, const double leaveSpeed, const bool setRequest,
304 const double arrivalSpeedBraking,
305 const SUMOTime waitingTime, double dist, double latOffset);
306
308 void setApproaching(const SUMOVehicle* approaching, ApproachingVehicleInformation ai);
309
311 void setApproachingPerson(const MSPerson* approaching, const SUMOTime arrivalTime, const SUMOTime leaveTime);
312
314 void removeApproaching(const SUMOVehicle* veh);
315
317 void removeApproachingPerson(const MSPerson* person);
318
319 /* @brief return information about this vehicle if it is registered as
320 * approaching (dummy values otherwise)
321 * @note used for visualisation of link items */
322 ApproachingVehicleInformation getApproaching(const SUMOVehicle* veh) const;
323 const ApproachingVehicleInformation* getApproachingPtr(const SUMOVehicle* veh) const;
324
328 }
329
334
336 void clearState();
337
345 bool opened(SUMOTime arrivalTime, double arrivalSpeed, double leaveSpeed, double vehicleLength,
346 double impatience, double decel, SUMOTime waitingTime,
347 double posLat = 0,
348 BlockingFoes* collectFoes = nullptr,
349 bool ignoreRed = false,
350 const SUMOTrafficObject* ego = nullptr,
351 double dist = -1) const;
352
368 bool blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
369 bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
370 BlockingFoes* collectFoes = nullptr, const SUMOTrafficObject* ego = nullptr, bool lastWasContRed = false, double dist = -1) const;
371
372
382 bool hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime, double speed, double decel) const;
383
388 std::pair<const SUMOVehicle*, const MSLink*> getFirstApproachingFoe(const MSLink* wrapAround) const;
389
391 return myJunction;
392 }
393
394
400 return myState;
401 }
402
403
409 return myOffState;
410 }
411
417 return myLastGreenState;
418 }
419
420
421 //@brief Returns the time of the last state change
423 return myLastStateChange;
424 }
425
426
432 return myDirection;
433 }
434
435
436
442 void setTLState(LinkState state, SUMOTime t);
443
447 void setTLLogic(const MSTrafficLightLogic* logic);
448
453 inline MSLane* getLane() const {
454 return myLane;
455 }
456
457
462 inline int getIndex() const {
463 return myIndex;
464 }
465
467 inline int getTLIndex() const {
468 return myTLIndex;
469 }
470
472 inline const MSTrafficLightLogic* getTLLogic() const {
473 return myLogic;
474 }
475
479 inline bool havePriority() const {
480 return myState >= 'A' && myState <= 'Z';
481 }
482
483 inline bool haveOffPriority() const {
484 return myOffState >= 'A' && myOffState <= 'Z';
485 }
486
490 inline bool haveRed() const {
492 }
493
494 inline bool haveYellow() const {
496 }
497
498 inline bool haveGreen() const {
500 }
501
502 inline bool mustStop() const {
504 }
505
506 inline bool isTLSControlled() const {
507 return myLogic != 0;
508 }
509
513
518 double getLength() const {
519 return myLength;
520 }
521
522
531 }
532
533 double getDistToFoePedCrossing() const {
535 }
536
541 bool hasFoes() const {
542 return myHasFoes;
543 }
544
545 // @brief return whether the vehicle may continute past this link to wait within the intersection
546 bool isCont() const;
547
548
550 bool keepClear() const {
551 return myKeepClear;
552 }
553
555 bool isIndirect() const {
556 return myAmIndirect;
557 }
558
560 bool lastWasContMajor() const;
561
563 bool lastWasContState(LinkState linkState) const;
564
568 double getInternalLengthsAfter() const;
569
573 double getInternalLengthsBefore() const;
574
580 double getLengthsBeforeCrossing(const MSLane* foeLane) const;
581
582
588 double getLengthBeforeCrossing(const MSLane* foeLane) const;
589
590
595 inline MSLane* getViaLane() const {
596 return myInternalLane;
597 }
598
607 const LinkLeaders getLeaderInfo(const MSVehicle* ego, double dist, std::vector<const MSPerson*>* collectBlockers = 0, bool isShadowLink = false) const;
608
610 double getZipperSpeed(const MSVehicle* ego, const double dist, double vSafe,
611 SUMOTime arrivalTime,
612 const BlockingFoes* foes) const;
613
615 inline MSLane* getViaLaneOrLane() const {
616 return myInternalLane != nullptr ? myInternalLane : myLane;
617 }
618
619
621 inline const MSLane* getLaneBefore() const {
622 assert(myInternalLaneBefore == nullptr || myLaneBefore == myInternalLaneBefore); // lane before mismatch!
623 return myLaneBefore;
624 }
625
627 inline const MSLane* getInternalLaneBefore() const {
629 }
630
632 SUMOTime getLeaveTime(const SUMOTime arrivalTime, const double arrivalSpeed, const double leaveSpeed, const double vehicleLength) const;
633
635 void writeApproaching(OutputDevice& od, const std::string fromLaneID) const;
636
638 MSLink* getParallelLink(int direction) const;
639
642
644 inline bool fromInternalLane() const {
645 return myInternalLaneBefore != nullptr;
646 }
647
649 bool isEntryLink() const;
650
652 bool isConflictEntryLink() const;
653
655 bool isExitLink() const;
656
659
661 const MSLink* getCorrespondingExitLink() const;
662
664 const MSLink* getCorrespondingEntryLink() const;
665
667 bool isInternalJunctionLink() const;
668
671 return myMesoTLSPenalty;
672 }
673
675 double getGreenFraction() const {
676 return myGreenFraction;
677 }
678
680 void setMesoTLSPenalty(const SUMOTime penalty) {
681 myMesoTLSPenalty = penalty;
682 }
683
685 void setGreenFraction(const double fraction) {
686 myGreenFraction = fraction;
687 }
688
689 const std::vector<const MSLane*>& getFoeLanes() const {
690 return myFoeLanes;
691 }
692
693 const std::vector<ConflictInfo>& getConflicts() const {
694 return myConflicts;
695 }
696
697 const std::vector<MSLink*>& getFoeLinks() const {
698 return myFoeLinks;
699 }
700
703 return myPermissions;
704 }
705
706 void updatePermissions();
707
709 void initParallelLinks();
710
712 inline double getLateralShift() const {
713 return myLateralShift;
714 }
715
717 std::string getDescription() const;
718
720 std::pair<const SUMOVehicle* const, const ApproachingVehicleInformation> getClosest() const;
721
722 inline bool hasFoeCrossing() const {
724 }
725
727 bool railSignalWasPassed() const;
728
730 static void recheckSetRequestInformation();
731
732 static bool ignoreFoe(const SUMOTrafficObject* ego, const SUMOTrafficObject* foe);
733
734 static const double NO_INTERSECTION;
735
736private:
738 static inline bool unsafeMergeSpeeds(double leaderSpeed, double followerSpeed, double leaderDecel, double followerDecel) {
739 // XXX mismatch between continuous an discrete deceleration
740 return (leaderSpeed * leaderSpeed / leaderDecel) <= (followerSpeed * followerSpeed / followerDecel);
741 }
742
744 static bool couldBrakeForLeader(double followDist, double leaderDist, const MSVehicle* follow, const MSVehicle* leader);
745
746 MSLink* computeParallelLink(int direction);
747
749 void checkWalkingAreaFoe(const MSVehicle* ego, const MSLane* foeLane, std::vector<const MSPerson*>* collectBlockers, LinkLeaders& result) const;
750
752 bool isInFront(const MSVehicle* ego, const PositionVector& egoPath, const Position& pPos) const;
753
755 double isOnComingPed(const MSVehicle* ego, const MSPerson* p) const;
756
758 Position getFuturePosition(const MSPerson* p, double timeHorizon = 1) const;
759
760 bool blockedByFoe(const SUMOVehicle* veh, const ApproachingVehicleInformation& avi,
761 SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
762 bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
763 const SUMOTrafficObject* ego) const;
764
766 bool checkContOff() const;
767
769 bool contIntersect(const MSLane* lane, const MSLane* foe);
770
772 double computeDistToDivergence(const MSLane* lane, const MSLane* sibling, double minDist, bool sameSource, double siblingPredLength = 0) const;
773
775 static SUMOTime computeFoeArrivalTimeBraking(SUMOTime arrivalTime, const SUMOVehicle* foe, SUMOTime foeArrivalTime, double impatience, double dist, double& fasb);
776
778 static bool lateralOverlap(double posLat, double width, double posLat2, double width2);
779
781 const CustomConflict* getCustomConflict(const MSLane* foeLane) const;
782
784 void updateDistToFoePedCrossing(double dist);
785
786private:
789
792
795#ifdef HAVE_FOX
797 FXMutex myApproachingVehiclesMutex;
798#endif
799
802
804 const int myTLIndex;
805
808
815
818
821
824 double myLength;
825
831
834
837
838 // @brief whether vehicles may continue past this link to wait within the intersection
840 // @brief whether vehicles may continue past this link to wait within the intersection after switching of the traffic light at this intersection
842
843 // @brief whether vehicles must keep the intersection clear if there is a downstream jam
845
848
849 /* @brief The preceding junction-internal lane, only used at
850 * - exit links (from internal lane to normal lane)
851 * - internal junction links (from internal lane to internal lane)
852 */
854
859
862
863 /* @brief lengths after the crossing point with foeLane
864 * (index corresponds to myFoeLanes)
865 * empty vector for entry links
866 * */
867 std::vector<ConflictInfo> myConflicts;
868
869 std::vector<CustomConflict> myCustomConflicts;
870
871 // TODO: documentation
872 std::vector<MSLink*> myFoeLinks;
873 std::vector<const MSLane*> myFoeLanes;
874
875 /* prioritized links when the traffic light is switched off (only needed for RightOfWay::ALLWAYSTOP)
876 * @note stored as a pointer to save space since it won't be used in most cases
877 */
878 std::vector<MSLink*>* myOffFoeLinks;
879
884
887
888 /* @brief Links with the same origin lane and the same destination edge that may
889 be in conflict for sublane simulation */
890 std::vector<MSLink*> mySublaneFoeLinks;
891 /* @brief Links with the same origin lane and different destination edge that may
892 be in conflict for sublane simulation */
893 std::vector<MSLink*> mySublaneFoeLinks2;
894
895 /* @brief Internal Lanes with the same origin lane and the same destination edge that may
896 be in conflict for sublane simulation */
897 std::vector<MSLane*> mySublaneFoeLanes;
898
901
903 static std::set<std::pair<MSLink*, MSLink*> > myRecheck;
904
907
909 const bool myAmIndirect;
910
912 double myRadius;
913
916
919
921 MSLink(const MSLink& s);
922
925
926};
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
int getNumericalID() const
Returns this lane's numerical id.
Definition MSLane.h:526
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:63
Definition json.hpp:5639