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-2020 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  // distance to link in m below which adaptation for zipper-merging should take place
71  static const double ZIPPER_ADAPT_DIST;
72 
73  struct LinkLeader {
74  LinkLeader(MSVehicle* _veh, double _gap, double _distToCrossing, bool _fromLeft = true, bool _inTheWay = false) :
75  vehAndGap(std::make_pair(_veh, _gap)),
76  distToCrossing(_distToCrossing),
77  fromLeft(_fromLeft),
78  inTheWay(_inTheWay) {
79  }
80 
81  std::pair<MSVehicle*, double> vehAndGap;
83  bool fromLeft;
84  bool inTheWay;
85  };
86 
87  typedef std::vector<LinkLeader> LinkLeaders;
88 
98  ApproachingVehicleInformation(const SUMOTime _arrivalTime, const SUMOTime _leavingTime,
99  const double _arrivalSpeed, const double _leaveSpeed,
100  const bool _willPass,
101  const SUMOTime _arrivalTimeBraking,
102  const double _arrivalSpeedBraking,
103  const SUMOTime _waitingTime,
104  const double _dist,
105  const double _speed
106  ) :
107  arrivalTime(_arrivalTime), leavingTime(_leavingTime),
108  arrivalSpeed(_arrivalSpeed), leaveSpeed(_leaveSpeed),
109  willPass(_willPass),
110  arrivalTimeBraking(_arrivalTimeBraking),
111  arrivalSpeedBraking(_arrivalSpeedBraking),
112  waitingTime(_waitingTime),
113  dist(_dist),
114  speed(_speed) {
115  }
116 
122  const double arrivalSpeed;
124  const double leaveSpeed;
126  const bool willPass;
130  const double arrivalSpeedBraking;
134  const double dist;
136  const double speed;
137 
138  private:
141 
142  };
143 
144  typedef std::map<const SUMOVehicle*, const ApproachingVehicleInformation, ComparatorNumericalIdLess> ApproachInfos;
145  typedef std::vector<const SUMOVehicle*> BlockingFoes;
146 
155  MSLink(MSLane* predLane, MSLane* succLane, MSLane* via, LinkDirection dir, LinkState state, double length, double foeVisibilityDistance, bool keepClear, MSTrafficLightLogic* logic, int tlLinkIdx);
156 
157 
159  ~MSLink();
160 
161 
169  void setRequestInformation(int index, bool hasFoes, bool isCont,
170  const std::vector<MSLink*>& foeLinks, const std::vector<MSLane*>& foeLanes,
171  MSLane* internalLaneBefore = 0);
172 
174  void addWalkingAreaFoe(const MSLane* lane) {
175  myWalkingAreaFoe = lane;
176  }
177 
179  void addWalkingAreaFoeExit(const MSLane* lane) {
180  myWalkingAreaFoeExit = lane;
181  }
182 
185  return myWalkingAreaFoe;
186  }
188  return myWalkingAreaFoeExit;
189  }
190 
195  void setApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime,
196  const double arrivalSpeed, const double leaveSpeed, const bool setRequest,
197  const SUMOTime arrivalTimeBraking, const double arrivalSpeedBraking,
198  const SUMOTime waitingTime, double dist);
199 
201  void setApproaching(const SUMOVehicle* approaching, ApproachingVehicleInformation ai);
202 
204  void removeApproaching(const SUMOVehicle* veh);
205 
206  void addBlockedLink(MSLink* link);
207 
208  /* @brief return information about this vehicle if it is registered as
209  * approaching (dummy values otherwise)
210  * @note used for visualisation of link items */
211  ApproachingVehicleInformation getApproaching(const SUMOVehicle* veh) const;
212 
214  const ApproachInfos& getApproaching() const {
215  return myApproachingVehicles;
216  }
217 
219  void clearState();
220 
228  bool opened(SUMOTime arrivalTime, double arrivalSpeed, double leaveSpeed, double vehicleLength,
229  double impatience, double decel, SUMOTime waitingTime,
230  double posLat = 0,
231  BlockingFoes* collectFoes = nullptr,
232  bool ignoreRed = false,
233  const SUMOTrafficObject* ego = nullptr) const;
234 
249  bool blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
250  bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
251  BlockingFoes* collectFoes = nullptr, const SUMOTrafficObject* ego = nullptr) const;
252 
253 
254  bool isBlockingAnyone() const {
255  return myApproachingVehicles.size() != 0;
256  }
257 
258  bool willHaveBlockedFoe() const;
259 
260 
261 
271  bool hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime, double speed, double decel) const;
272 
277  std::pair<const SUMOVehicle*, const MSLink*> getFirstApproachingFoe(const MSLink* wrapAround) const;
278 
280  return myJunction;
281  }
282 
283 
288  LinkState getState() const {
289  return myState;
290  }
291 
292 
298  return myOffState;
299  }
300 
301 
302  //@brief Returns the time of the last state change
303  inline SUMOTime getLastStateChange() const {
304  return myLastStateChange;
305  }
306 
307 
312  inline LinkDirection getDirection() const {
313  return myDirection;
314  }
315 
316 
317 
323  void setTLState(LinkState state, SUMOTime t);
324 
325 
330  inline MSLane* getLane() const {
331  return myLane;
332  }
333 
334 
339  inline int getIndex() const {
340  return myIndex;
341  }
342 
344  inline int getTLIndex() const {
345  return myTLIndex;
346  }
347 
349  inline const MSTrafficLightLogic* getTLLogic() const {
350  return myLogic;
351  }
352 
356  inline bool havePriority() const {
357  return myState >= 'A' && myState <= 'Z';
358  }
359 
363  inline bool haveRed() const {
365  }
366 
367  inline bool haveYellow() const {
369  }
370 
371  inline bool haveGreen() const {
373  }
374 
375  inline bool isTLSControlled() const {
376  return myLogic != 0;
377  }
378 
379  inline bool isTurnaround() const {
381  }
382 
387  double getLength() const {
388  return myLength;
389  }
390 
391 
398  double getFoeVisibilityDistance() const {
400  }
401 
402 
407  bool hasFoes() const {
408  return myHasFoes;
409  }
410 
411  // @brief return whether the vehicle may continute past this link to wait within the intersection
412  bool isCont() const;
413 
414 
416  bool keepClear() const {
417  return myKeepClear;
418  }
419 
421  bool lastWasContMajor() const;
422 
424  bool lastWasContMajorGreen() const;
425 
429  double getInternalLengthsAfter() const;
430 
434  double getInternalLengthsBefore() const;
435 
441  double getLengthsBeforeCrossing(const MSLane* foeLane) const;
442 
443 
449  double getLengthBeforeCrossing(const MSLane* foeLane) const;
450 
451 
456  inline MSLane* getViaLane() const {
457  return myInternalLane;
458  }
459 
468  const LinkLeaders getLeaderInfo(const MSVehicle* ego, double dist, std::vector<const MSPerson*>* collectBlockers = 0, bool isShadowLink = false) const;
469 
471  double getZipperSpeed(const MSVehicle* ego, const double dist, double vSafe,
472  SUMOTime arrivalTime,
473  BlockingFoes* collectFoes) const;
474 
476  inline MSLane* getViaLaneOrLane() const {
477  return myInternalLane != nullptr ? myInternalLane : myLane;
478  }
479 
480 
482  inline const MSLane* getLaneBefore() const {
483  assert(myInternalLaneBefore == nullptr || myLaneBefore == myInternalLaneBefore); // lane before mismatch!
484  return myLaneBefore;
485  }
486 
488  inline const MSLane* getInternalLaneBefore() const {
489  return myInternalLaneBefore;
490  }
491 
493  SUMOTime getLeaveTime(const SUMOTime arrivalTime, const double arrivalSpeed, const double leaveSpeed, const double vehicleLength) const;
494 
496  void writeApproaching(OutputDevice& od, const std::string fromLaneID) const;
497 
499  MSLink* getParallelLink(int direction) const;
500 
502  inline bool fromInternalLane() const {
503  return myInternalLaneBefore != nullptr;
504  }
505 
507  bool isEntryLink() const;
508 
510  bool isConflictEntryLink() const;
511 
513  bool isExitLink() const;
514 
516  bool isExitLinkAfterInternalJunction() const;
517 
519  const MSLink* getCorrespondingExitLink() const;
520 
522  const MSLink* getCorrespondingEntryLink() const;
523 
525  bool isInternalJunctionLink() const;
526 
529  return myMesoTLSPenalty;
530  }
531 
533  double getGreenFraction() const {
534  return myGreenFraction;
535  }
536 
538  void setMesoTLSPenalty(const SUMOTime penalty) {
539  myMesoTLSPenalty = penalty;
540  }
541 
543  void setGreenFraction(const double fraction) {
544  myGreenFraction = fraction;
545  }
546 
547  const std::vector<const MSLane*>& getFoeLanes() const {
548  return myFoeLanes;
549  }
550 
551  const std::vector<std::pair<double, double> >& getLengthsBehindCrossing() const {
553  }
554 
555  const std::vector<MSLink*>& getFoeLinks() const {
556  return myFoeLinks;
557  }
558 
560  void initParallelLinks();
561 
563  inline double getLateralShift() const {
564  return myLateralShift;
565  }
566 
568  std::string getDescription() const;
569 
570 private:
572  static inline bool unsafeMergeSpeeds(double leaderSpeed, double followerSpeed, double leaderDecel, double followerDecel) {
573  // XXX mismatch between continuous an discrete deceleration
574  return (leaderSpeed * leaderSpeed / leaderDecel) <= (followerSpeed * followerSpeed / followerDecel);
575  }
576 
578  static bool couldBrakeForLeader(double followDist, double leaderDist, const MSVehicle* follow, const MSVehicle* leader);
579 
580  MSLink* computeParallelLink(int direction);
581 
583  void checkWalkingAreaFoe(const MSVehicle* ego, const MSLane* foeLane, std::vector<const MSPerson*>* collectBlockers, LinkLeaders& result) const;
584 
585  bool blockedByFoe(const SUMOVehicle* veh, const ApproachingVehicleInformation& avi,
586  SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
587  bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
588  const SUMOTrafficObject* ego) const;
589 
591  bool checkContOff() const;
592 
594  bool contIntersect(const MSLane* lane, const MSLane* foe);
595 
597  double computeDistToDivergence(const MSLane* lane, const MSLane* sibling, double minDist, bool sameSource) const;
598 
599 
600 private:
603 
606 
608  std::set<MSLink*> myBlockedFoeLinks;
609 
611  int myIndex;
612 
614  const int myTLIndex;
615 
618 
623 
626 
629 
632  double myLength;
633 
638 
640  bool myHasFoes;
641 
642  // @brief whether vehicles may continue past this link to wait within the intersection
643  bool myAmCont;
644  // @brief whether vehicles may continue past this link to wait within the intersection after switching of the traffic light at this intersection
646 
647  // @brief whether vehicles must keep the intersection clear if there is a downstream jam
649 
652 
653  /* @brief The preceding junction-internal lane, only used at
654  * - exit links (from internal lane to normal lane)
655  * - internal junction links (from internal lane to internal lane)
656  */
658 
663 
666 
667  /* @brief lengths after the crossing point with foeLane
668  * (lengthOnThis, lengthOnFoe)
669  * (index corresponds to myFoeLanes)
670  * empty vector for entry links
671  * */
672  std::vector<std::pair<double, double> > myLengthsBehindCrossing;
673 
674  // TODO: documentation
675  std::vector<MSLink*> myFoeLinks;
676  std::vector<const MSLane*> myFoeLanes;
677 
682 
685 
686  /* @brief Links with the same origin lane and the same destination edge that may
687  be in conflict for sublane simulation */
688  std::vector<MSLink*> mySublaneFoeLinks;
689  /* @brief Links with the same origin lane and different destination edge that may
690  be in conflict for sublane simulation */
691  std::vector<MSLink*> mySublaneFoeLinks2;
692 
693  /* @brief Internal Lanes with the same origin lane and the same destination edge that may
694  be in conflict for sublane simulation */
695  std::vector<MSLane*> mySublaneFoeLanes;
696 
697  static const SUMOTime myLookaheadTime;
699 
702 
705 
707  MSLink(const MSLink& s);
708 
710  MSLink& operator=(const MSLink& s);
711 
712 };
long long int SUMOTime
Definition: SUMOTime.h:31
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:77
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
Representation of a vehicle, person, or container.
Representation of a vehicle.
Definition: SUMOVehicle.h:58