Eclipse SUMO - Simulation of Urban MObility
MSLink.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-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 /****************************************************************************/
20 // A connnection between lanes
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <vector>
26 #include <set>
27 #include <utils/common/SUMOTime.h>
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class MSLane;
37 class MSJunction;
38 class MSVehicle;
39 class MSPerson;
40 class OutputDevice;
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
67 class MSLink {
68 public:
69 
70  struct LinkLeader {
71  LinkLeader(MSVehicle* _veh, double _gap, double _distToCrossing, bool _fromLeft = true, bool _inTheWay = false) :
72  vehAndGap(std::make_pair(_veh, _gap)),
73  distToCrossing(_distToCrossing),
74  fromLeft(_fromLeft),
75  inTheWay(_inTheWay) {
76  }
77 
78  std::pair<MSVehicle*, double> vehAndGap;
80  bool fromLeft;
81  bool inTheWay;
82  };
83 
84  typedef std::vector<LinkLeader> LinkLeaders;
85 
95  ApproachingVehicleInformation(const SUMOTime _arrivalTime, const SUMOTime _leavingTime,
96  const double _arrivalSpeed, const double _leaveSpeed,
97  const bool _willPass,
98  const SUMOTime _arrivalTimeBraking,
99  const double _arrivalSpeedBraking,
100  const SUMOTime _waitingTime,
101  const double _dist,
102  const double _speed,
103  const double _latOffset
104  ) :
105  arrivalTime(_arrivalTime), leavingTime(_leavingTime),
106  arrivalSpeed(_arrivalSpeed), leaveSpeed(_leaveSpeed),
107  willPass(_willPass),
108  arrivalTimeBraking(_arrivalTimeBraking),
109  arrivalSpeedBraking(_arrivalSpeedBraking),
110  waitingTime(_waitingTime),
111  dist(_dist),
112  speed(_speed),
113  latOffset(_latOffset) {
114  }
115 
121  const double arrivalSpeed;
123  const double leaveSpeed;
125  const bool willPass;
129  const double arrivalSpeedBraking;
133  const double dist;
135  const double speed;
137  const double latOffset;
138 
139  private:
142 
143  };
144 
145  typedef std::map<const SUMOVehicle*, const ApproachingVehicleInformation, ComparatorNumericalIdLess> ApproachInfos;
146  typedef std::vector<const SUMOVehicle*> BlockingFoes;
147 
156  MSLink(MSLane* predLane,
157  MSLane* succLane,
158  MSLane* via,
159  LinkDirection dir,
160  LinkState state,
161  double length,
162  double foeVisibilityDistance,
163  bool keepClear,
164  MSTrafficLightLogic* logic,
165  int tlLinkIdx,
166  bool indirect);
167 
168 
170  ~MSLink();
171 
172 
180  void setRequestInformation(int index, bool hasFoes, bool isCont,
181  const std::vector<MSLink*>& foeLinks, const std::vector<MSLane*>& foeLanes,
182  MSLane* internalLaneBefore = 0);
183 
185  void addWalkingAreaFoe(const MSLane* lane) {
186  myWalkingAreaFoe = lane;
187  }
188 
190  void addWalkingAreaFoeExit(const MSLane* lane) {
191  myWalkingAreaFoeExit = lane;
192  }
193 
196  return myWalkingAreaFoe;
197  }
199  return myWalkingAreaFoeExit;
200  }
201 
206  void setApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime,
207  const double arrivalSpeed, const double leaveSpeed, const bool setRequest,
208  const SUMOTime arrivalTimeBraking, const double arrivalSpeedBraking,
209  const SUMOTime waitingTime, double dist, double latOffset);
210 
212  void setApproaching(const SUMOVehicle* approaching, ApproachingVehicleInformation ai);
213 
215  void removeApproaching(const SUMOVehicle* veh);
216 
217  void addBlockedLink(MSLink* link);
218 
219  /* @brief return information about this vehicle if it is registered as
220  * approaching (dummy values otherwise)
221  * @note used for visualisation of link items */
222  ApproachingVehicleInformation getApproaching(const SUMOVehicle* veh) const;
223 
225  const ApproachInfos& getApproaching() const {
226  return myApproachingVehicles;
227  }
228 
230  void clearState();
231 
239  bool opened(SUMOTime arrivalTime, double arrivalSpeed, double leaveSpeed, double vehicleLength,
240  double impatience, double decel, SUMOTime waitingTime,
241  double posLat = 0,
242  BlockingFoes* collectFoes = nullptr,
243  bool ignoreRed = false,
244  const SUMOTrafficObject* ego = nullptr) const;
245 
260  bool blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
261  bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
262  BlockingFoes* collectFoes = nullptr, const SUMOTrafficObject* ego = nullptr) const;
263 
264 
265  bool isBlockingAnyone() const {
266  return myApproachingVehicles.size() != 0;
267  }
268 
269  bool willHaveBlockedFoe() const;
270 
271 
272 
282  bool hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime, double speed, double decel) const;
283 
288  std::pair<const SUMOVehicle*, const MSLink*> getFirstApproachingFoe(const MSLink* wrapAround) const;
289 
291  return myJunction;
292  }
293 
294 
299  LinkState getState() const {
300  return myState;
301  }
302 
303 
309  return myOffState;
310  }
311 
317  return myLastGreenState;
318  }
319 
320 
321  //@brief Returns the time of the last state change
322  inline SUMOTime getLastStateChange() const {
323  return myLastStateChange;
324  }
325 
326 
331  inline LinkDirection getDirection() const {
332  return myDirection;
333  }
334 
335 
336 
342  void setTLState(LinkState state, SUMOTime t);
343 
344 
349  inline MSLane* getLane() const {
350  return myLane;
351  }
352 
353 
358  inline int getIndex() const {
359  return myIndex;
360  }
361 
363  inline int getTLIndex() const {
364  return myTLIndex;
365  }
366 
368  inline const MSTrafficLightLogic* getTLLogic() const {
369  return myLogic;
370  }
371 
375  inline bool havePriority() const {
376  return myState >= 'A' && myState <= 'Z';
377  }
378 
382  inline bool haveRed() const {
384  }
385 
386  inline bool haveYellow() const {
388  }
389 
390  inline bool haveGreen() const {
392  }
393 
394  inline bool isTLSControlled() const {
395  return myLogic != 0;
396  }
397 
398  inline bool isTurnaround() const {
400  }
401 
406  double getLength() const {
407  return myLength;
408  }
409 
410 
417  double getFoeVisibilityDistance() const {
419  }
420 
421 
426  bool hasFoes() const {
427  return myHasFoes;
428  }
429 
430  // @brief return whether the vehicle may continute past this link to wait within the intersection
431  bool isCont() const;
432 
433 
435  bool keepClear() const {
436  return myKeepClear;
437  }
438 
440  bool isIndirect() const {
441  return myAmIndirect;
442  }
443 
445  bool lastWasContMajor() const;
446 
448  bool lastWasContMajorGreen() const;
449 
453  double getInternalLengthsAfter() const;
454 
458  double getInternalLengthsBefore() const;
459 
465  double getLengthsBeforeCrossing(const MSLane* foeLane) const;
466 
467 
473  double getLengthBeforeCrossing(const MSLane* foeLane) const;
474 
475 
480  inline MSLane* getViaLane() const {
481  return myInternalLane;
482  }
483 
492  const LinkLeaders getLeaderInfo(const MSVehicle* ego, double dist, std::vector<const MSPerson*>* collectBlockers = 0, bool isShadowLink = false) const;
493 
495  double getZipperSpeed(const MSVehicle* ego, const double dist, double vSafe,
496  SUMOTime arrivalTime,
497  BlockingFoes* collectFoes) const;
498 
500  inline MSLane* getViaLaneOrLane() const {
501  return myInternalLane != nullptr ? myInternalLane : myLane;
502  }
503 
504 
506  inline const MSLane* getLaneBefore() const {
507  assert(myInternalLaneBefore == nullptr || myLaneBefore == myInternalLaneBefore); // lane before mismatch!
508  return myLaneBefore;
509  }
510 
512  inline const MSLane* getInternalLaneBefore() const {
513  return myInternalLaneBefore;
514  }
515 
517  SUMOTime getLeaveTime(const SUMOTime arrivalTime, const double arrivalSpeed, const double leaveSpeed, const double vehicleLength) const;
518 
520  void writeApproaching(OutputDevice& od, const std::string fromLaneID) const;
521 
523  MSLink* getParallelLink(int direction) const;
524 
527 
529  inline bool fromInternalLane() const {
530  return myInternalLaneBefore != nullptr;
531  }
532 
534  bool isEntryLink() const;
535 
537  bool isConflictEntryLink() const;
538 
540  bool isExitLink() const;
541 
543  bool isExitLinkAfterInternalJunction() const;
544 
546  const MSLink* getCorrespondingExitLink() const;
547 
549  const MSLink* getCorrespondingEntryLink() const;
550 
552  bool isInternalJunctionLink() const;
553 
556  return myMesoTLSPenalty;
557  }
558 
560  double getGreenFraction() const {
561  return myGreenFraction;
562  }
563 
565  void setMesoTLSPenalty(const SUMOTime penalty) {
566  myMesoTLSPenalty = penalty;
567  }
568 
570  void setGreenFraction(const double fraction) {
571  myGreenFraction = fraction;
572  }
573 
574  const std::vector<const MSLane*>& getFoeLanes() const {
575  return myFoeLanes;
576  }
577 
578  const std::vector<std::pair<double, double> >& getLengthsBehindCrossing() const {
580  }
581 
582  const std::vector<MSLink*>& getFoeLinks() const {
583  return myFoeLinks;
584  }
585 
587  void initParallelLinks();
588 
590  inline double getLateralShift() const {
591  return myLateralShift;
592  }
593 
595  std::string getDescription() const;
596 
597 private:
599  static inline bool unsafeMergeSpeeds(double leaderSpeed, double followerSpeed, double leaderDecel, double followerDecel) {
600  // XXX mismatch between continuous an discrete deceleration
601  return (leaderSpeed * leaderSpeed / leaderDecel) <= (followerSpeed * followerSpeed / followerDecel);
602  }
603 
605  static bool couldBrakeForLeader(double followDist, double leaderDist, const MSVehicle* follow, const MSVehicle* leader);
606 
607  MSLink* computeParallelLink(int direction);
608 
610  void checkWalkingAreaFoe(const MSVehicle* ego, const MSLane* foeLane, std::vector<const MSPerson*>* collectBlockers, LinkLeaders& result) const;
611 
613  bool isInFront(const MSVehicle* ego, const PositionVector& egoPath, const MSPerson* p) const;
614 
615  bool blockedByFoe(const SUMOVehicle* veh, const ApproachingVehicleInformation& avi,
616  SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
617  bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
618  const SUMOTrafficObject* ego) const;
619 
621  bool checkContOff() const;
622 
624  bool contIntersect(const MSLane* lane, const MSLane* foe);
625 
627  double computeDistToDivergence(const MSLane* lane, const MSLane* sibling, double minDist, bool sameSource) const;
628 
629 
631  static bool lateralOverlap(double posLat, double width, double posLat2, double width2);
632 
633  static bool ignoreFoe(const SUMOTrafficObject* ego, const SUMOVehicle* foe);
634 
635 private:
638 
641 
643  std::set<MSLink*> myBlockedFoeLinks;
644 
646  int myIndex;
647 
649  const int myTLIndex;
650 
653 
660 
663 
666 
669  double myLength;
670 
676 
678  bool myHasFoes;
679 
680  // @brief whether vehicles may continue past this link to wait within the intersection
681  bool myAmCont;
682  // @brief whether vehicles may continue past this link to wait within the intersection after switching of the traffic light at this intersection
684 
685  // @brief whether vehicles must keep the intersection clear if there is a downstream jam
687 
690 
691  /* @brief The preceding junction-internal lane, only used at
692  * - exit links (from internal lane to normal lane)
693  * - internal junction links (from internal lane to internal lane)
694  */
696 
701 
704 
705  /* @brief lengths after the crossing point with foeLane
706  * (lengthOnThis, lengthOnFoe)
707  * (index corresponds to myFoeLanes)
708  * empty vector for entry links
709  * */
710  std::vector<std::pair<double, double> > myLengthsBehindCrossing;
711 
712  // TODO: documentation
713  std::vector<MSLink*> myFoeLinks;
714  std::vector<const MSLane*> myFoeLanes;
715 
720 
723 
724  /* @brief Links with the same origin lane and the same destination edge that may
725  be in conflict for sublane simulation */
726  std::vector<MSLink*> mySublaneFoeLinks;
727  /* @brief Links with the same origin lane and different destination edge that may
728  be in conflict for sublane simulation */
729  std::vector<MSLink*> mySublaneFoeLinks2;
730 
731  /* @brief Internal Lanes with the same origin lane and the same destination edge that may
732  be in conflict for sublane simulation */
733  std::vector<MSLane*> mySublaneFoeLanes;
734 
735  static const SUMOTime myLookaheadTime;
737 
740 
742  const bool myAmIndirect;
743 
745  double myRadius;
746 
749 
751  MSLink(const MSLink& s);
752 
754  MSLink& operator=(const MSLink& s);
755 
756 };
long long int SUMOTime
Definition: SUMOTime.h:32
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_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:82
The parent class for traffic light logics.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:75
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
A list of positions.
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition: SUMOVehicle.h:60