SUMO - Simulation of Urban MObility
MSLink.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A connnection between lanes
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <algorithm>
35 #include <limits>
37 #include "MSNet.h"
38 #include "MSJunction.h"
39 #include "MSLink.h"
40 #include "MSLane.h"
42 #include "MSEdge.h"
43 #include "MSGlobals.h"
44 #include "MSVehicle.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 //#define MSLink_DEBUG_CROSSING_POINTS
52 
53 // ===========================================================================
54 // static member variables
55 // ===========================================================================
57 // additional caution is needed when approaching a zipper link
59 
60 // time to link in seconds below which adaptation should take place
61 #define ZIPPER_ADAPT_TIME 10
62 // distance to link in m below which adaptation should take place
63 #define ZIPPER_ADAPT_DIST 100
64 
65 // ===========================================================================
66 // member method definitions
67 // ===========================================================================
68 #ifndef HAVE_INTERNAL_LANES
69 MSLink::MSLink(MSLane* succLane, LinkDirection dir, LinkState state, SUMOReal length, bool keepClear) :
70  myLane(succLane),
71  myIndex(-1),
72  myState(state),
73  myLastStateChange(-1),
74  myDirection(dir),
75  myLength(length),
76  myHasFoes(false),
77  myAmCont(false),
78  myKeepClear(keepClear),
79  myJunction(0)
80 #else
81 MSLink::MSLink(MSLane* succLane, MSLane* via, LinkDirection dir, LinkState state, SUMOReal length, bool keepClear) :
82  myLane(succLane),
83  myIndex(-1),
84  myState(state),
85  myLastStateChange(-1),
86  myDirection(dir),
87  myLength(length),
88  myHasFoes(false),
89  myAmCont(false),
90  myKeepClear(keepClear),
91  myJunctionInlane(via),
92  myInternalLaneBefore(0),
93  myJunction(0)
94 #endif
95 {}
96 
97 
99 
100 
101 void
103  const std::vector<MSLink*>& foeLinks,
104  const std::vector<MSLane*>& foeLanes,
105  MSLane* internalLaneBefore) {
106  myIndex = index;
107  myHasFoes = hasFoes;
108  myAmCont = isCont;
109  myFoeLinks = foeLinks;
110  for (std::vector<MSLane*>::const_iterator it_lane = foeLanes.begin(); it_lane != foeLanes.end(); ++it_lane) {
111  // cannot assign vector due to const-ness
112  myFoeLanes.push_back(*it_lane);
113  }
114  myJunction = const_cast<MSJunction*>(myLane->getEdge().getFromJunction()); // junctionGraph is initialized after the whole network is loaded
115 #ifdef HAVE_INTERNAL_LANES
116  myInternalLaneBefore = internalLaneBefore;
117  MSLane* lane = 0;
118  if (internalLaneBefore != 0) {
119  // this is an exit link. compute crossing points with all foeLanes
120  lane = internalLaneBefore;
121  //} else if (myLane->getEdge().isCrossing()) {
122  // // this is the link to a pedestrian crossing. compute crossing points with all foeLanes
123  // // @note not currently used by pedestrians
124  // lane = myLane;
125  }
126 #ifdef MSLink_DEBUG_CROSSING_POINTS
127  std::cout << " link " << myIndex << " to " << getViaLaneOrLane()->getID() << " internalLane=" << (lane == 0 ? "NULL" : lane->getID()) << " has foes: " << toString(foeLanes) << "\n";
128 #endif
129  if (lane != 0) {
130  const bool beforeInternalJunction = lane->getLinkCont()[0]->getViaLaneOrLane()->getEdge().isInternal();
131  assert(lane->getIncomingLanes().size() == 1);
132  // compute crossing points
133  for (std::vector<const MSLane*>::const_iterator it_lane = myFoeLanes.begin(); it_lane != myFoeLanes.end(); ++it_lane) {
134  const bool sameTarget = myLane == (*it_lane)->getLinkCont()[0]->getLane();
135  if (sameTarget && !beforeInternalJunction) {
136  //if (myLane == (*it_lane)->getLinkCont()[0]->getLane()) {
137  // this foeLane has the same target and merges at the end (lane exits the junction)
138  myLengthsBehindCrossing.push_back(std::make_pair(0, 0)); // dummy value, never used
139 #ifdef MSLink_DEBUG_CROSSING_POINTS
140  std::cout
141  << " " << lane->getID()
142  << " merges with " << (*it_lane)->getID()
143  << " nextLane " << lane->getLinkCont()[0]->getViaLaneOrLane()->getID()
144  << " dist1=" << myLengthsBehindCrossing.back().first
145  << " dist2=" << myLengthsBehindCrossing.back().second
146  << "\n";
147 #endif
148  } else {
149  std::vector<SUMOReal> intersections1 = lane->getShape().intersectsAtLengths2D((*it_lane)->getShape());
150 #ifdef MSLink_DEBUG_CROSSING_POINTS
151  //std::cout << " intersections1=" << toString(intersections1) << "\n";
152 #endif
153  bool haveIntersection = true;
154  if (intersections1.size() == 0) {
155  intersections1.push_back(-10000.0); // disregard this foe (using maxdouble leads to nasty problems down the line)
156  haveIntersection = false;
157  } else if (intersections1.size() > 1) {
158  std::sort(intersections1.begin(), intersections1.end());
159  }
160  std::vector<SUMOReal> intersections2 = (*it_lane)->getShape().intersectsAtLengths2D(lane->getShape());
161 #ifdef MSLink_DEBUG_CROSSING_POINTS
162  //std::cout << " intersections2=" << toString(intersections2) << "\n";
163 #endif
164  if (intersections2.size() == 0) {
165  intersections2.push_back(0);
166  } else if (intersections2.size() > 1) {
167  std::sort(intersections2.begin(), intersections2.end());
168  }
169  if (haveIntersection) {
170  // lane width affects the crossing point
171  intersections1.back() -= (*it_lane)->getWidth() / 2;
172  intersections2.back() -= lane->getWidth() / 2;
173  // also length/geometry factor
174  intersections1.back() = lane->interpolateGeometryPosToLanePos(intersections1.back());
175  intersections2.back() = (*it_lane)->interpolateGeometryPosToLanePos(intersections2.back());
176 
177  if (internalLaneBefore->getLogicalPredecessorLane()->getEdge().isInternal() && !(*it_lane)->getEdge().isCrossing()) {
178  // wait at the internal junction
179  // (except for foes that are crossings since there is no internal junction)
180  intersections1.back() = 0;
181  }
182  }
183 
184  myLengthsBehindCrossing.push_back(std::make_pair(
185  lane->getLength() - intersections1.back(),
186  (*it_lane)->getLength() - intersections2.back()));
187 
188 #ifdef MSLink_DEBUG_CROSSING_POINTS
189  std::cout
190  << " intersection of " << lane->getID()
191  << " totalLength=" << lane->getLength()
192  << " with " << (*it_lane)->getID()
193  << " totalLength=" << (*it_lane)->getLength()
194  << " dist1=" << myLengthsBehindCrossing.back().first
195  << " dist2=" << myLengthsBehindCrossing.back().second
196  << "\n";
197 #endif
198  }
199  }
200  // check for overlap with internal lanes from the same source lane
201  const MSLane* pred = lane->getLogicalPredecessorLane();
202  // to avoid overlap with vehicles that came from pred (especially when pred has endOffset > 0)
203  // we add all other internal lanes from pred as foeLanes
204  const MSLinkCont& predLinks = pred->getLinkCont();
205  for (MSLinkCont::const_iterator it = predLinks.begin(); it != predLinks.end(); ++it) {
206  const MSLane* sibling = (*it)->getViaLane();
207  if (sibling != lane && sibling != 0) {
208  std::vector<SUMOReal> intersections1 = lane->getShape().intersectsAtLengths2D(sibling->getShape());
209 #ifdef MSLink_DEBUG_CROSSING_POINTS
210  //std::cout << " intersections1=" << toString(intersections1) << "\n";
211 #endif
212  if (intersections1.size() > 0) {
213  std::sort(intersections1.begin(), intersections1.end());
214  if (intersections1.back() > NUMERICAL_EPS) {
215  // siblings share a common shape up to the last crossing point so intersections are identical and only need to be computed once
216  myLengthsBehindCrossing.push_back(std::make_pair(
217  lane->getLength() - intersections1.back(),
218  sibling->getLength() - intersections1.back()));
219  myFoeLanes.push_back(sibling);
220 #ifdef MSLink_DEBUG_CROSSING_POINTS
221  std::cout << " adding same-origin foe" << sibling->getID()
222  << " dist1=" << myLengthsBehindCrossing.back().first
223  << " dist2=" << myLengthsBehindCrossing.back().second
224  << "\n";
225 #endif
226  }
227  }
228  }
229  }
230  }
231 #else
232  UNUSED_PARAMETER(internalLaneBefore);
233 #endif
234 }
235 
236 
237 std::pair<SUMOReal, SUMOReal>
238 getLastIntersections(const MSLane* lane, const MSLane* foe);
239 
240 void
241 MSLink::setApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime, const SUMOReal arrivalSpeed, const SUMOReal leaveSpeed,
242  const bool setRequest, const SUMOTime arrivalTimeBraking, const SUMOReal arrivalSpeedBraking, const SUMOTime waitingTime, SUMOReal dist) {
243  const SUMOTime leaveTime = getLeaveTime(arrivalTime, arrivalSpeed, leaveSpeed, approaching->getVehicleType().getLength());
244  myApproachingVehicles.insert(std::make_pair(approaching,
245  ApproachingVehicleInformation(arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, setRequest,
246  arrivalTimeBraking, arrivalSpeedBraking, waitingTime, dist)));
247 }
248 
249 
250 void
252  myBlockedFoeLinks.insert(link);
253 }
254 
255 
256 
257 bool
259  for (std::set<MSLink*>::const_iterator i = myBlockedFoeLinks.begin(); i != myBlockedFoeLinks.end(); ++i) {
260  if ((*i)->isBlockingAnyone()) {
261  return true;
262  }
263  }
264  return false;
265 }
266 
267 
268 void
270  myApproachingVehicles.erase(veh);
271 }
272 
273 
276  std::map<const SUMOVehicle*, ApproachingVehicleInformation>::const_iterator i = myApproachingVehicles.find(veh);
277  if (i != myApproachingVehicles.end()) {
278  return i->second;
279  } else {
280  return ApproachingVehicleInformation(-1000, -1000, 0, 0, false, -1000, 0, 0, 0);
281  }
282 }
283 
284 
285 SUMOTime
286 MSLink::getLeaveTime(const SUMOTime arrivalTime, const SUMOReal arrivalSpeed,
287  const SUMOReal leaveSpeed, const SUMOReal vehicleLength) const {
288  return arrivalTime + TIME2STEPS((getLength() + vehicleLength) / MAX2((SUMOReal)0.5 * (arrivalSpeed + leaveSpeed), NUMERICAL_EPS));
289 }
290 
291 
292 bool
293 MSLink::opened(SUMOTime arrivalTime, SUMOReal arrivalSpeed, SUMOReal leaveSpeed, SUMOReal vehicleLength,
294  SUMOReal impatience, SUMOReal decel, SUMOTime waitingTime,
295  std::vector<const SUMOVehicle*>* collectFoes) const {
296  if (haveRed()) {
297  return false;
298  }
300  return true;
301  }
302  if (havePriority() && myState != LINKSTATE_ZIPPER) {
303  // zipper still needs to collect foes
304  return true;
305  }
306  if ((myState == LINKSTATE_STOP || myState == LINKSTATE_ALLWAY_STOP) && waitingTime == 0) {
307  return false;
308  }
309  const SUMOTime leaveTime = getLeaveTime(arrivalTime, arrivalSpeed, leaveSpeed, vehicleLength);
310  for (std::vector<MSLink*>::const_iterator i = myFoeLinks.begin(); i != myFoeLinks.end(); ++i) {
311 #ifdef HAVE_INTERNAL
313  if ((*i)->haveRed()) {
314  continue;
315  }
316  }
317 #endif
318  if ((*i)->blockedAtTime(arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, myLane == (*i)->getLane(),
319  impatience, decel, waitingTime, collectFoes)) {
320  return false;
321  }
322  }
323  if (collectFoes != 0 && collectFoes->size() > 0) {
324  return false;
325  }
326  return true;
327 }
328 
329 
330 bool
331 MSLink::blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime, SUMOReal arrivalSpeed, SUMOReal leaveSpeed,
332  bool sameTargetLane, SUMOReal impatience, SUMOReal decel, SUMOTime waitingTime,
333  std::vector<const SUMOVehicle*>* collectFoes) const {
334  for (std::map<const SUMOVehicle*, ApproachingVehicleInformation>::const_iterator i = myApproachingVehicles.begin(); i != myApproachingVehicles.end(); ++i) {
335  if (!i->second.willPass) {
336  continue;
337  }
339  assert(waitingTime > 0);
340  if (waitingTime > i->second.waitingTime) {
341  continue;
342  }
343  if (waitingTime == i->second.waitingTime && arrivalTime < i->second.arrivalTime) {
344  continue;
345  }
346  }
347  const SUMOTime foeArrivalTime = (SUMOTime)((1.0 - impatience) * i->second.arrivalTime + impatience * i->second.arrivalTimeBraking);
349  if (i->second.leavingTime < arrivalTime) {
350  // ego wants to be follower
351  if (sameTargetLane && (arrivalTime - i->second.leavingTime < lookAhead
352  || unsafeMergeSpeeds(i->second.leaveSpeed, arrivalSpeed,
353  i->first->getVehicleType().getCarFollowModel().getMaxDecel(), decel))) {
354  if (collectFoes == 0) {
355  return true;
356  } else {
357  collectFoes->push_back(i->first);
358  }
359  }
360  } else if (foeArrivalTime > leaveTime) {
361  // ego wants to be leader.
362  if (sameTargetLane && (foeArrivalTime - leaveTime < lookAhead
363  || unsafeMergeSpeeds(leaveSpeed, i->second.arrivalSpeedBraking,
364  decel, i->first->getVehicleType().getCarFollowModel().getMaxDecel()))) {
365  if (collectFoes == 0) {
366  return true;
367  } else {
368  collectFoes->push_back(i->first);
369  }
370  }
371  } else {
372  // even without considering safeHeadwayTime there is already a conflict
373  if (collectFoes == 0) {
374  return true;
375  } else {
376  collectFoes->push_back(i->first);
377  }
378  }
379  }
380  return false;
381 }
382 
383 
384 bool
386  MSVehicle* veh = lane->getLastVehicle();
387  SUMOReal distLeft = 0;
388  if (veh == 0) {
389  veh = lane->getPartialOccupator();
390  distLeft = lane->getLength() - lane->getPartialOccupatorEnd();
391  } else {
392  distLeft = lane->getLength() - veh->getPositionOnLane() + veh->getVehicleType().getLength();
393  }
394  if (veh == 0) {
395  return false;
396  } else {
397  assert(distLeft > 0);
398  // can we be sure that the vehicle leaves this lane in the next step?
399  bool result = distLeft > (veh->getSpeed() - veh->getCarFollowModel().getMaxDecel());
400  return result;
401  }
402 }
403 
404 
405 bool
406 MSLink::hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime, SUMOReal speed, SUMOReal decel) const {
407  for (std::vector<MSLink*>::const_iterator i = myFoeLinks.begin(); i != myFoeLinks.end(); ++i) {
408  if ((*i)->blockedAtTime(arrivalTime, leaveTime, speed, speed, myLane == (*i)->getLane(), 0, decel, 0)) {
409  return true;
410  }
411  }
412  for (std::vector<const MSLane*>::const_iterator i = myFoeLanes.begin(); i != myFoeLanes.end(); ++i) {
413  if ((*i)->getVehicleNumber() > 0 || (*i)->getPartialOccupator() != 0) {
414  return true;
415  }
416  }
417  return false;
418 }
419 
420 
423  return myDirection;
424 }
425 
426 
427 void
429  if (myState != state) {
430  myLastStateChange = t;
431  }
432  myState = state;
433 }
434 
435 
436 MSLane*
438  return myLane;
439 }
440 
441 MSLane*
443  MSLane* approachedLane; //the lane approached by this link; this lane may be an internal lane
444 #ifdef HAVE_INTERNAL_LANES
445  if (myJunctionInlane != 0) { // if there is an internal lane
446  approachedLane = myJunctionInlane; //consider the internal lane as the approached lane
447  } else { //if ther is no internal lane
448  approachedLane = myLane;
449  }
450 #else
451  approachedLane = myLane;
452 #endif
453  const std::vector<MSLane::IncomingLaneInfo> possibleLanes = approachedLane->getIncomingLanes();
454  std::vector<MSLane::IncomingLaneInfo>::const_iterator i;
455  for (i = possibleLanes.begin(); i != possibleLanes.end(); i++) {
456  MSLane* lane = (*i).lane;
457  MSLinkCont outgoingLinks = lane->getLinkCont(); //the links outgoing from lane
458  for (MSLinkCont::const_iterator j = outgoingLinks.begin(); j != outgoingLinks.end(); j++) {
459  if ((*j) == this) {
460  return lane;
461  }
462  }
463  }
464  WRITE_WARNING("No approaching lane found for the link with the index " + toString(this->getIndex()) + ".");
465  return 0;
466 }
467 
468 
469 bool
471 #ifdef HAVE_INTERNAL_LANES
472  if (myJunctionInlane == 0 || myAmCont) {
473  return false;
474  } else {
475  MSLane* pred = myJunctionInlane->getLogicalPredecessorLane();
477  return false;
478  } else {
479  MSLane* pred2 = pred->getLogicalPredecessorLane();
480  assert(pred2 != 0);
481  MSLink* predLink = MSLinkContHelper::getConnectingLink(*pred2, *pred);
482  assert(predLink != 0);
483  return predLink->havePriority();
484  }
485  }
486 #else
487  return false;
488 #endif
489 }
490 
491 
492 void
493 MSLink::writeApproaching(OutputDevice& od, const std::string fromLaneID) const {
494  if (myApproachingVehicles.size() > 0) {
495  od.openTag("link");
496  od.writeAttr(SUMO_ATTR_FROM, fromLaneID);
497 #ifdef HAVE_INTERNAL_LANES
498  const std::string via = getViaLane() == 0 ? "" : getViaLane()->getID();
499 #else
500  const std::string via = "";
501 #endif
502  od.writeAttr(SUMO_ATTR_VIA, via);
503  od.writeAttr(SUMO_ATTR_TO, getLane() == 0 ? "" : getLane()->getID());
504  std::vector<std::pair<SUMOTime, const SUMOVehicle*> > toSort; // stabilize output
505  for (std::map<const SUMOVehicle*, ApproachingVehicleInformation>::const_iterator it = myApproachingVehicles.begin(); it != myApproachingVehicles.end(); ++it) {
506  toSort.push_back(std::make_pair(it->second.arrivalTime, it->first));
507  }
508  std::sort(toSort.begin(), toSort.end());
509  for (std::vector<std::pair<SUMOTime, const SUMOVehicle*> >::const_iterator it = toSort.begin(); it != toSort.end(); ++it) {
510  od.openTag("approaching");
511  const ApproachingVehicleInformation& avi = myApproachingVehicles.find(it->second)->second;
512  od.writeAttr(SUMO_ATTR_ID, it->second->getID());
513  od.writeAttr(SUMO_ATTR_IMPATIENCE, it->second->getImpatience());
514  od.writeAttr("arrivalTime", time2string(avi.arrivalTime));
515  od.writeAttr("arrivalTimeBraking", time2string(avi.arrivalTimeBraking));
516  od.writeAttr("leaveTime", time2string(avi.leavingTime));
517  od.writeAttr("arrivalSpeed", toString(avi.arrivalSpeed));
518  od.writeAttr("arrivalSpeedBraking", toString(avi.arrivalSpeedBraking));
519  od.writeAttr("leaveSpeed", toString(avi.leaveSpeed));
520  od.writeAttr("willPass", toString(avi.willPass));
521  od.closeTag();
522  }
523  od.closeTag();
524  }
525 }
526 
527 
528 #ifdef HAVE_INTERNAL_LANES
529 MSLane*
530 MSLink::getViaLane() const {
531  return myJunctionInlane;
532 }
533 
534 
536 MSLink::getLeaderInfo(SUMOReal dist, SUMOReal minGap, std::vector<const MSPerson*>* collectBlockers) const {
537  LinkLeaders result;
538  //gDebugFlag1 = true;
539  // this link needs to start at an internal lane (either an exit link or between two internal lanes)
541  (myJunctionInlane == 0 && getLane()->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_NORMAL)
542  || (myJunctionInlane != 0 && myJunctionInlane->getLogicalPredecessorLane()->getEdge().isInternal()))) {
543  //if (gDebugFlag1) std::cout << SIMTIME << " getLeaderInfo link=" << getViaLaneOrLane()->getID() << "\n";
544  // this is an exit link
545  for (size_t i = 0; i < myFoeLanes.size(); ++i) {
546  const MSLane* foeLane = myFoeLanes[i];
547  // distance from the querying vehicle to the crossing point with foeLane
548  const SUMOReal distToCrossing = dist - myLengthsBehindCrossing[i].first;
549  const bool sameTarget = (myLane == foeLane->getLinkCont()[0]->getLane());
550  const bool sameSource = (myInternalLaneBefore != 0 && myInternalLaneBefore->getLogicalPredecessorLane() == foeLane->getLogicalPredecessorLane());
551  const SUMOReal crossingWidth = (sameTarget || sameSource) ? 0 : foeLane->getWidth();
552  const SUMOReal foeCrossingWidth = (sameTarget || sameSource) ? 0 : myInternalLaneBefore->getWidth();
553  //if (gDebugFlag1) std::cout << " distToCrossing=" << distToCrossing << " foeLane=" << foeLane->getID() << "\n";
554  if (distToCrossing + crossingWidth < 0) {
555  continue; // vehicle is behind the crossing point, continue with next foe lane
556  }
557  const SUMOReal foeDistToCrossing = foeLane->getLength() - myLengthsBehindCrossing[i].second;
558  // it is not sufficient to return the last vehicle on the foeLane because ego might be its leader
559  // therefore we return all vehicles on the lane
560  //
561  // special care must be taken for continuation lanes. (next lane is also internal)
562  // vehicles on these lanes should always block (gap = -1)
563  const bool contLane = (foeLane->getLinkCont()[0]->getViaLaneOrLane()->getEdge().getPurpose() == MSEdge::EDGEFUNCTION_INTERNAL);
564  // vehicles on cont. lanes or on internal lanes with the same target as this link can never be ignored
565  const bool cannotIgnore = contLane || sameTarget || sameSource;
566  const MSLane::VehCont& vehicles = foeLane->getVehiclesSecure();
567  foeLane->releaseVehicles();
568  for (MSLane::VehCont::const_iterator it_veh = vehicles.begin(); it_veh != vehicles.end(); ++it_veh) {
569  MSVehicle* leader = *it_veh;
570  if (!cannotIgnore && !foeLane->getLinkCont()[0]->getApproaching(leader).willPass) {
571  continue;
572  }
573  if (cannotIgnore || leader->getWaitingTime() < MSGlobals::gIgnoreJunctionBlocker) {
574  // compute distance between vehicles on the the superimposition of both lanes
575  // where the crossing point is the common point
576  SUMOReal gap;
577  if (contLane && !sameSource) {
578  gap = -1; // always break for vehicles which are on a continuation lane
579  } else {
580  const SUMOReal leaderBack = leader->getPositionOnLane() - leader->getVehicleType().getLength();
581  const SUMOReal leaderBackDist = foeDistToCrossing - leaderBack;
582  //if (gDebugFlag1) std::cout << " distToCrossing=" << distToCrossing << " leader back=" << leaderBack << " backDist=" << leaderBackDist << "\n";
583  if (leaderBackDist + foeCrossingWidth < 0) {
584  // leader is completely past the crossing point
585  // or there is no crossing point
586  continue; // next vehicle
587  }
588  gap = distToCrossing - leaderBackDist - (sameTarget ? minGap : 0);
589  }
590  result.push_back(LinkLeader(leader, gap, cannotIgnore ? -1 : distToCrossing));
591  }
592 
593  }
594  MSVehicle* leader = foeLane->getPartialOccupator();
595  if (leader != 0) {
596  if (cannotIgnore || leader->getWaitingTime() < MSGlobals::gIgnoreJunctionBlocker) {
597  // compute distance between vehicles on the the superimposition of both lanes
598  // where the crossing point is the common point
599  SUMOReal gap;
600  if (contLane && !sameSource) {
601  gap = -1; // always break for vehicles which are on a continuation lane
602  } else {
603  const SUMOReal leaderBackDist = foeDistToCrossing - foeLane->getPartialOccupatorEnd();
604  //if (gDebugFlag1) std::cout << " distToCrossing=" << distToCrossing << " leader (partialOccupator) backDist=" << leaderBackDist << "\n";
605  if (leaderBackDist + foeCrossingWidth < 0) {
606  // leader is completely past the crossing point
607  // or there is no crossing point
608  continue; // next lane
609  }
610  gap = distToCrossing - leaderBackDist - (sameTarget ? minGap : 0);
611  }
612  result.push_back(LinkLeader(leader, gap, sameTarget ? -1 : distToCrossing));
613  }
614  }
615  // check for crossing pedestrians (keep driving if already on top of the crossing
616  const SUMOReal distToPeds = distToCrossing - MSPModel::SAFETY_GAP;
617  if (distToPeds >= -MSPModel::SAFETY_GAP && MSPModel::getModel()->blockedAtDist(foeLane, foeDistToCrossing, collectBlockers)) {
618  result.push_back(LinkLeader((MSVehicle*)0, -1, distToPeds));
619  }
620  }
621  }
622  return result;
623 }
624 #endif
625 
626 
627 MSLane*
629 #ifdef HAVE_INTERNAL_LANES
630  if (myJunctionInlane != 0) {
631  return myJunctionInlane;
632  }
633 #endif
634  return myLane;
635 }
636 
637 
638 void
640  if (myJunction != 0) {
641  myJunction->passedJunction(vehicle);
642  }
643 }
644 
645 
646 bool
647 MSLink::isLeader(const MSVehicle* ego, const MSVehicle* foe) {
648  if (myJunction != 0) {
649  return myJunction->isLeader(ego, foe);
650  } else {
651  // unregulated junction
652  return false;
653  }
654 }
655 
656 const MSLane*
658 #ifdef HAVE_INTERNAL_LANES
659  return myInternalLaneBefore;
660 #endif
661  return 0;
662 }
663 
664 
665 SUMOReal
666 MSLink::getZipperSpeed(const MSVehicle* ego, const SUMOReal dist, SUMOReal vSafe,
667  SUMOTime arrivalTime,
668  std::vector<const SUMOVehicle*>* collectFoes) const {
669  //gDebugFlag1 = ego->getID() == "left.7";
670  if (myFoeLinks.size() == 0) {
671  // link should have LINKSTATE_MAJOR in this case
672  assert(false);
673  return vSafe;
674  } else if (myFoeLinks.size() > 1) {
675  throw ProcessError("Zipper junctions with more than two conflicting lanes are not supported (at junction '"
676  + myJunction->getID() + "')");
677  }
679  const SUMOReal secondsToArrival = STEPS2TIME(arrivalTime - now);
680  if (secondsToArrival > ZIPPER_ADAPT_TIME && dist > ZIPPER_ADAPT_DIST) {
681  //if (gDebugFlag1) std::cout << SIMTIME << " getZipperSpeed ego=" << ego->getID()
682  // << " dist=" << dist
683  // << " ignoring foes (arrival in " << STEPS2TIME(arrivalTime - now) << ")\n";
684  return vSafe;
685  }
686  //if (gDebugFlag1) std::cout << SIMTIME << " getZipperSpeed ego=" << ego->getID()
687  // << " egoAT=" << arrivalTime
688  // << " dist=" << dist
689  // << " vSafe=" << vSafe
690  // << " numFoes=" << collectFoes->size()
691  // << "\n";
692  MSLink* foeLink = myFoeLinks[0];
693  for (std::vector<const SUMOVehicle*>::const_iterator i = collectFoes->begin(); i != collectFoes->end(); ++i) {
694  const MSVehicle* foe = dynamic_cast<const MSVehicle*>(*i);
695  assert(foe != 0);
696  const ApproachingVehicleInformation& avi = foeLink->getApproaching(foe);
697  if ( // ignore vehicles that arrive after us (unless they are ahead and we could easily brake for them)
698  ((avi.arrivalTime > arrivalTime) && !couldBrakeForLeader(dist, avi.dist, ego, foe)) ||
699  // also ignore vehicles that are behind us and are able to brake for us
700  couldBrakeForLeader(avi.dist, dist, foe, ego)) {
701  //if (gDebugFlag1) std::cout
702  // << " ignoring foe=" << foe->getID()
703  // << " foeAT=" << avi.arrivalTime
704  // << " foeDist=" << avi.dist
705  // << " foeSpeed=" << foe->getSpeed()
706  // << " egoSpeed=" << ego->getSpeed()
707  // << " deltaDist=" << avi.dist - dist
708  // << " delteSpeed=" << foe->getSpeed() - foe->getCarFollowModel().getMaxDecel() - ego->getSpeed()
709  // << "\n";
710  continue;
711  }
712  const SUMOReal gap = dist - foe->getVehicleType().getLength() - ego->getVehicleType().getMinGap() - avi.dist;
713  const SUMOReal follow = ego->getCarFollowModel().followSpeed(
714  ego, ego->getSpeed(), gap, foe->getSpeed(), foe->getCarFollowModel().getMaxDecel());
715  // speed adaption to follow the foe can be spread over secondsToArrival
716  const SUMOReal followInTime = vSafe + (follow - vSafe) / MAX2((SUMOReal)1, secondsToArrival / TS);
717  vSafe = MIN2(vSafe, followInTime);
718  //if (gDebugFlag1) std::cout << " adapting to foe=" << foe->getID()
719  // << " foeDist=" << avi.dist
720  // << " follow=" << follow
721  // << " followInTime=" << followInTime
722  // << " gap=" << gap
723  // << " foeSpeed=" << foe->getSpeed()
724  // << " follow=" << follow
725  // << " foeAT=" << avi.arrivalTime
726  // << " foeLT=" << avi.leavingTime
727  // << " foeAS=" << avi.arrivalSpeed
728  // << " vSafe=" << vSafe
729  // << "\n";
730  }
731  return vSafe;
732 }
733 
734 
735 bool
736 MSLink::couldBrakeForLeader(SUMOReal followDist, SUMOReal leaderDist, const MSVehicle* follow, const MSVehicle* leader) {
737  return (// leader is ahead of follower
738  followDist > leaderDist &&
739  // and follower could brake for 1 s to stay behind leader
740  followDist - leaderDist > follow->getSpeed() - follow->getCarFollowModel().getMaxDecel() - leader->getSpeed());
741 }
742 
743 
744 /****************************************************************************/
745 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:461
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:43
static const SUMOReal SAFETY_GAP
Definition: MSPModel.h:81
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:303
const MSCFModel & getCarFollowModel() const
Returns the vehicle&#39;s car following model definition.
Definition: MSVehicle.h:544
SUMOTime getWaitingTime() const
Returns the SUMOTime waited (speed was lesser than 0.1m/s)
Definition: MSVehicle.h:359
virtual SUMOReal followSpeed(const MSVehicle *const veh, SUMOReal speed, SUMOReal gap2pred, SUMOReal predSpeed, SUMOReal predMaxDecel) const =0
Computes the vehicle&#39;s follow speed (no dawdling)
This is an uncontrolled, minor link, has to stop.
SUMOReal getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:376
The base class for an intersection.
Definition: MSJunction.h:61
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:88
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
SUMOReal getLength() const
Get vehicle&#39;s length [m].
SUMOReal getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:392
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
T MAX2(T a, T b)
Definition: StdDefs.h:79
SUMOReal getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:286
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
#define TS
Definition: SUMOTime.h:52
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
This is an uncontrolled, all-way stop link.
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:39
This is an uncontrolled, zipper-merge link.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:296
SUMOReal getPartialOccupatorEnd() const
Returns the position of the in-lapping vehicle&#39;s end.
Definition: MSLane.h:261
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)...
const std::string & getID() const
Returns the id.
Definition: Named.h:65
MSLane * getLogicalPredecessorLane() const
get the most likely precedecessor lane (sorted using by_connections_to_sorter). The result is cached ...
Definition: MSLane.cpp:1420
void passedJunction(const MSVehicle *vehicle)
erase vehicle from myLinkLeaders
Definition: MSJunction.cpp:77
Representation of a vehicle.
Definition: SUMOVehicle.h:65
SUMOReal getMinGap() const
Get the free space in front of vehicles of this class.
static MSPModel * getModel()
Definition: MSPModel.cpp:65
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
T MIN2(T a, T b)
Definition: StdDefs.h:73
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:71
SUMOReal getMaxDecel() const
Get the vehicle type&#39;s maximum deceleration [m/s^2].
Definition: MSCFModel.h:184
MSVehicle * getLastVehicle() const
returns the last vehicle
Definition: MSLane.cpp:992
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:240
EdgeBasicFunction getPurpose() const
Returns the edge type (EdgeBasicFunction)
Definition: MSEdge.h:235
The edge is a normal street.
Definition: MSEdge.h:94
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
Definition: MSBaseVehicle.h:93
SUMOReal getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:294
static SUMOTime gIgnoreJunctionBlocker
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:74
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:323
MSVehicle * getPartialOccupator() const
Returns the vehicle which laps into this lane.
Definition: MSLane.h:253
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:214
static const bool gUseMesoSim
Definition: MSGlobals.h:102
const MSJunction * getFromJunction() const
Definition: MSEdge.h:345
#define NUMERICAL_EPS
Definition: config.h:161
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1059
const std::vector< IncomingLaneInfo > & getIncomingLanes() const
Definition: MSLane.h:590
The edge is an internal edge.
Definition: MSEdge.h:98
SUMOReal interpolateGeometryPosToLanePos(SUMOReal geometryPos) const
Definition: MSLane.h:346
std::vector< SUMOReal > intersectsAtLengths2D(const PositionVector &other) const
For all intersections between this vector and other, return the 2D-length of the subvector from this ...
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
bool isLeader(const MSVehicle *ego, const MSVehicle *foe)
Definition: MSJunction.cpp:83
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.