Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSLink.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
21// A connection between lanes
22/****************************************************************************/
23#include <config.h>
24
25#include <iostream>
26#include <algorithm>
27#include <limits>
31#ifdef HAVE_FOX
33#endif
34#include "MSNet.h"
35#include "MSJunction.h"
36#include "MSJunctionLogic.h"
37#include "MSLink.h"
38#include "MSLane.h"
41#include "MSEdge.h"
42#include "MSGlobals.h"
43#include "MSVehicle.h"
46
47//#define MSLink_DEBUG_CROSSING_POINTS
48//#define MSLink_DEBUG_CROSSING_POINTS_DETAILS
49//#define MSLink_DEBUG_OPENED
50//#define DEBUG_APPROACHING
51//#define DEBUG_ZIPPER
52//#define DEBUG_WALKINGAREA
53//#define DEBUG_COND (myLane->getID()=="43[0]_0" && myLaneBefore->getID()==":33_0_0")
54//#define DEBUG_COND (myLane->getID()=="end_0")
55//#define DEBUG_COND (true)
56#define DEBUG_COND2(obj) (obj->isSelected())
57//#define DEBUG_COND2(obj) (obj->getID() == "train2")
58//#define DEBUG_COND2(obj) (true)
59//#define DEBUG_COND_ZIPPER (gDebugFlag1)
60//#define DEBUG_COND_ZIPPER (true)
61#define DEBUG_COND_ZIPPER (ego->isSelected())
62
63// ===========================================================================
64// static member variables
65// ===========================================================================
66
67#define INVALID_TIME -1000
68
69// the default safety gap when passing before oncoming pedestrians
70#define JM_CROSSING_GAP_DEFAULT 10
71
72// minimim width between sibling lanes to qualify as non-overlapping
73#define DIVERGENCE_MIN_WIDTH 2.5
74
76// additional caution is needed when approaching a zipper link
78std::set<std::pair<MSLink*, MSLink*> > MSLink::myRecheck;
79const double MSLink::NO_INTERSECTION(10000);
80
81// ===========================================================================
82// ConflictInfo member method definitions
83// ===========================================================================
84
85double
88 return 0;
89 } else if (foeConflictIndex >= 0) {
90 return foeExitLink->myConflicts[foeConflictIndex].lengthBehindCrossing;
91 } else {
92 return -NO_INTERSECTION;
93 }
94}
95
96double
98 if (foeConflictIndex >= 0) {
99 return foeExitLink->myConflicts[foeConflictIndex].conflictSize;
100 } else {
101 return 0;
102 }
103}
104
105double
108 return exitLink->getInternalLaneBefore()->getLength();
109 } else {
110 return lengthBehindCrossing;
111 }
112}
113
114// ===========================================================================
115// member method definitions
116// ===========================================================================
117MSLink::MSLink(MSLane* predLane, MSLane* succLane, MSLane* via, LinkDirection dir, LinkState state,
118 double length, double foeVisibilityDistance, bool keepClear,
119 MSTrafficLightLogic* logic, int tlIndex,
120 bool indirect) :
121 myLane(succLane),
122 myLaneBefore(predLane),
123 myApproachingPersons(nullptr),
124 myIndex(-1),
125 myTLIndex(tlIndex),
126 myLogic(logic),
127 myState(state),
129 myOffState(state),
130 myLastStateChange(SUMOTime_MIN / 2), // a large negative value, but avoid overflows when subtracting
131 myDirection(dir),
132 myLength(length),
133 myFoeVisibilityDistance(foeVisibilityDistance),
134 myDistToFoePedCrossing(std::numeric_limits<double>::max()),
135 myHasFoes(false),
136 myAmCont(false),
137 myAmContOff(false),
139 myInternalLane(via),
140 myInternalLaneBefore(nullptr),
144 myOffFoeLinks(nullptr),
145 myWalkingAreaFoe(nullptr),
146 myWalkingAreaFoeExit(nullptr),
148 myParallelRight(nullptr),
149 myParallelLeft(nullptr),
150 myAmIndirect(indirect),
151 myRadius(std::numeric_limits<double>::max()),
152 myPermissions(0),
153 myJunction(nullptr)
154{
157 // detect lateral shift from lane geometries
158 //std::cout << "DEBUG link=" << myLaneBefore->getID() << "->" << getViaLaneOrLane()->getID() << " hasInternal=" << MSNet::getInstance()->hasInternalLinks() << " shapeBefore=" << myLaneBefore->getShape().back() << " shapeFront=" << getViaLaneOrLane()->getShape().front() << "\n";
159 if ((myInternalLane != nullptr || predLane->isInternal())
160 && myLaneBefore->getShape().back() != getViaLaneOrLane()->getShape().front()) {
163 const double dist = from.back().distanceTo2D(to.front());
164 // figure out direction of shift
165 try {
166 from.move2side(dist);
167 } catch (InvalidArgument&) {
168 }
169 myLateralShift = (from.back().distanceTo2D(to.front()) < dist) ? dist : -dist;
171 myLateralShift *= -1;
172 }
173 //std::cout << " lateral shift link=" << myLaneBefore->getID() << "->" << getViaLaneOrLane()->getID() << " dist=" << dist << " shift=" << myLateralShift << "\n";
174 }
175 }
176}
177
178
180 delete myOffFoeLinks;
182}
183
184
185void
187 // we only ever increase permission because transient permission reductions lead to invalid bestLanes assignment otherwise
189}
190
191
192void
193MSLink::addCustomConflict(const MSLane* from, const MSLane* to, double startPos, double endPos) {
194 myCustomConflicts.push_back(CustomConflict(from, to, startPos, endPos));
195}
196
198MSLink::getCustomConflict(const MSLane* foeLane) const {
199 if (myCustomConflicts.size() > 0) {
200 const MSLane* foeFrom = foeLane->getNormalPredecessorLane();
201 const MSLane* foeTo = foeLane->getNormalSuccessorLane();
202 for (const CustomConflict& cc : myCustomConflicts) {
203 if (cc.from == foeFrom && cc.to == foeTo) {
204 return &cc;
205 }
206 }
207
208 }
209 return nullptr;
210}
211
212void
213MSLink::setRequestInformation(int index, bool hasFoes, bool isCont,
214 const std::vector<MSLink*>& foeLinks,
215 const std::vector<MSLane*>& foeLanes,
216 MSLane* internalLaneBefore) {
217//#ifdef MSLink_DEBUG_CROSSING_POINTS
218// std::cout << " setRequestInformation() for junction " << getViaLaneOrLane()->getEdge().getFromJunction()->getID()
219// << "\nInternalLanes = " << toString(getViaLaneOrLane()->getEdge().getFromJunction()->getInternalLanes())
220// << std::endl;
221//#endif
222 myIndex = index;
225 myFoeLinks = foeLinks;
226 for (MSLane* foeLane : foeLanes) {
227 // cannot assign vector due to const-ness
228 myFoeLanes.push_back(foeLane);
229 }
230 myJunction = const_cast<MSJunction*>(myLane->getEdge().getFromJunction()); // junctionGraph is initialized after the whole network is loaded
231 myAmContOff = isCont && myLogic != nullptr && internalLaneBefore == nullptr && checkContOff();
232 myInternalLaneBefore = internalLaneBefore;
233 MSLane* lane = nullptr;
234 if (internalLaneBefore != nullptr) {
235 // this is an exit link. compute crossing points with all foeLanes
236 lane = internalLaneBefore;
237 //} else if (myLane->isCrossing()) {
238 // // this is the link to a pedestrian crossing. compute crossing points with all foeLanes
239 // // @note not currently used by pedestrians
240 // lane = myLane;
241 }
242 const MSLink* entryLink = getCorrespondingEntryLink();
243 if (entryLink->getOffState() == LinkState::LINKSTATE_ALLWAY_STOP && entryLink->getTLLogic() != nullptr) {
244 // TLS has "normal" right of way rules but all conflicting links are foes when switching TLS off
245 // (unless it's an internal junction link which should ignore all foes and should be ignored by all foes
246 myOffFoeLinks = new std::vector<MSLink*>();
247 if (isEntryLink()) {
248 for (MSLane* foeLane : foeLanes) {
249 assert(foeLane->isInternal() || foeLane->isCrossing());
250 MSLink* viaLink = foeLane->getIncomingLanes().front().viaLink;
251 if (viaLink->getLaneBefore()->isNormal()) {
252 myOffFoeLinks->push_back(viaLink);
253 }
254 }
255 }
256 }
257#ifdef MSLink_DEBUG_CROSSING_POINTS
258 std::cout << "link " << myIndex << " to " << getViaLaneOrLane()->getID() << " internalLaneBefore=" << (lane == 0 ? "NULL" : lane->getID()) << " has foes: " << toString(foeLanes) << "\n";
259#endif
260 if (lane != nullptr) {
261 const bool beforeInternalJunction = lane->getLinkCont()[0]->getViaLaneOrLane()->getEdge().isInternal();
262 if (lane->getIncomingLanes().size() != 1) {
263 throw ProcessError(TLF("Internal lane '%' has % predecessors", lane->getID(), toString(lane->getIncomingLanes().size())));
264 }
265 const MSLink* junctionEntryLink = lane->getEntryLink();
266 const bool isSecondPart = isExitLinkAfterInternalJunction();
267 // compute crossing points
268 for (const MSLane* foeLane : myFoeLanes) {
269 const CustomConflict* cc = junctionEntryLink != nullptr ? junctionEntryLink->getCustomConflict(foeLane) : nullptr;
270 if (cc != nullptr) {
271 // handle custom conflict definition
272 double startPos = cc->startPos;
273 const double conflictSize = cc->endPos - cc->startPos;
274 if (isSecondPart) {
275 startPos -= junctionEntryLink->getViaLane()->getLength();
276 }
277 // the foe connection may be split at an internal
278 // junction, we need to figure out whether the current
279 // foeLane is the intended target for the custom conflict
280 // There are two possibilities:
281 // a) We have no custom conflict for the reverse pair of connections
282 // -> just check whether lane and foeLane intersect
283 // b) We have a "reverse" custom conflict
284 // -> check whether it covers the foeLane
285 const CustomConflict* rcc = foeLane->getEntryLink()->getCustomConflict(lane);
286 bool haveIntersection = false;
287 if (rcc == nullptr) {
288 // a)
289 haveIntersection = lane->getShape().intersectsAtLengths2D(foeLane->getShape()).size() > 0;
290 } else {
291 // b)
292 const bool foeIsSecondPart = foeLane->getLogicalPredecessorLane()->isInternal();
293 double foeStartPos = rcc->startPos;
294 const double foeConflictSize = rcc->endPos - rcc->startPos;
295 if (foeIsSecondPart) {
296 foeStartPos -= foeLane->getLogicalPredecessorLane()->getLength();
297 }
298 const double foeEndPos = foeStartPos + foeConflictSize;
299 haveIntersection = ((foeStartPos > 0 && foeStartPos < foeLane->getLength())
300 || (foeEndPos > 0 && foeEndPos < foeLane->getLength()));
301 }
302 if (haveIntersection) {
303 myConflicts.push_back(ConflictInfo(lane->getLength() - startPos, conflictSize));
304 } else {
306 }
307#ifdef MSLink_DEBUG_CROSSING_POINTS
308 std::cout << " " << lane->getID() << " custom conflict with " << foeLane->getID() << " customReverse=" << (rcc != nullptr)
309 << " haveIntersection=" << haveIntersection
310 << " startPos=" << startPos << " conflictSize=" << conflictSize
311 << " lbc=" << myConflicts.back().lengthBehindCrossing
312 << "\n";
313#endif
314 continue;
315 }
317 const bool sameTarget = myLane == foeLane->getLinkCont()[0]->getLane();
318 if (sameTarget && !beforeInternalJunction && !contIntersect(lane, foeLane)) {
319 //if (myLane == foeLane->getLinkCont()[0]->getLane()) {
320 // this foeLane has the same target and merges at the end (lane exits the junction)
321 const double minDist = MIN2(DIVERGENCE_MIN_WIDTH, 0.5 * (lane->getWidth() + foeLane->getWidth()));
322 if (lane->getShape().back().distanceTo2D(foeLane->getShape().back()) >= minDist) {
323 // account for lateral shift by the entry links
324 if (foeLane->getEntryLink()->isIndirect()) {
325 myConflicts.push_back(ConflictInfo(-NO_INTERSECTION, 0)); // dummy value, never used
326#ifdef MSLink_DEBUG_CROSSING_POINTS
327 std::cout << " " << lane->getID() << " dummy merge with indirect" << foeLane->getID() << "\n";
328#endif
329 } else {
330 myConflicts.push_back(ConflictInfo(0, foeLane->getWidth(), CONFLICT_DUMMY_MERGE)); // dummy value, never used
331#ifdef MSLink_DEBUG_CROSSING_POINTS
332 std::cout << " " << lane->getID() << " dummy merge with " << foeLane->getID() << "\n";
333#endif
334 }
335 } else {
336 const double distAfterDivergence = computeDistToDivergence(lane, foeLane, minDist, false);
337 const double lbcLane = lane->interpolateGeometryPosToLanePos(distAfterDivergence);
338 myConflicts.push_back(ConflictInfo(lbcLane, foeLane->getWidth()));
339#ifdef MSLink_DEBUG_CROSSING_POINTS
340 std::cout
341 << " " << lane->getID()
342 << " merges with " << foeLane->getID()
343 << " nextLane " << lane->getLinkCont()[0]->getViaLaneOrLane()->getID()
344 << " dist1=" << myConflicts.back().lengthBehindCrossing
345 << "\n";
346#endif
347 }
348 } else {
349 std::vector<double> intersections1 = lane->getShape().intersectsAtLengths2D(foeLane->getShape());
350#ifdef MSLink_DEBUG_CROSSING_POINTS_DETAILS
351 std::cout << " intersections1=" << toString(intersections1) << "\n";
352#endif
353 bool haveIntersection = true;
354 if (intersections1.size() == 0) {
355 intersections1.push_back(-NO_INTERSECTION); // disregard this foe (using maxdouble leads to nasty problems down the line)
356 haveIntersection = false;
357 } else if (intersections1.size() > 1) {
358 std::sort(intersections1.begin(), intersections1.end());
359 }
360 std::vector<double> intersections2 = foeLane->getShape().intersectsAtLengths2D(lane->getShape());
361#ifdef MSLink_DEBUG_CROSSING_POINTS_DETAILS
362 std::cout << " intersections2=" << toString(intersections2) << "\n";
363#endif
364 if (intersections2.size() == 0) {
365 intersections2.push_back(0);
366 } else if (intersections2.size() > 1) {
367 std::sort(intersections2.begin(), intersections2.end());
368 }
369
370 // check for near-intersection (internal junctions for a side road which are only relevant when they have stranded vehicles))
371 if (!haveIntersection && foeLane->getLinkCont()[0]->getViaLane() != nullptr) {
372 const Position waitPos = foeLane->getShape().back();
373 const double dist = lane->getShape().distance2D(waitPos, true);
374 if (dist != GeomHelper::INVALID_OFFSET && dist < lane->getWidth() / 2) {
375 // risk of collision
376 intersections1.clear();
377 intersections2.clear();
378 intersections1.push_back(lane->getShape().nearest_offset_to_point2D(waitPos));
379 intersections2.push_back(foeLane->getShape().length());
380 haveIntersection = true;
381#ifdef MSLink_DEBUG_CROSSING_POINTS_DETAILS
382 std::cout << " link=" << myIndex << " " << getDescription() << " almostIntersection with foeLane " << foeLane->getID() << " offset=" << intersections1.back() << "\n";
383#endif
384 }
385 }
386
387 double conflictSize = foeLane->getWidth();
389 if (haveIntersection) {
390 flag = CONFLICT_DEFAULT;
391 const double angle1 = GeomHelper::naviDegree(lane->getShape().rotationAtOffset(intersections1.back()));
392 const double angle2 = GeomHelper::naviDegree(foeLane->getShape().rotationAtOffset(intersections2.back()));
393 const double angleDiff = GeomHelper::getMinAngleDiff(angle1, angle2);
394 //const double angleDiff = MIN2(GeomHelper::getMinAngleDiff(angle1, angle2),
395 // GeomHelper::getMinAngleDiff(angle1, angle2 + 180));
396 const double widthFactor = 1 / MAX2(sin(DEG2RAD(angleDiff)), 0.2) * 2 - 1;
397 //std::cout << " intersection of " << lane->getID() << " with " << foeLane->getID() << " angle1=" << angle1 << " angle2=" << angle2 << " angleDiff=" << angleDiff << " widthFactor=" << widthFactor << "\n";
398 conflictSize *= widthFactor;
399 conflictSize = MIN2(conflictSize, lane->getLength());
400 // lane width affects the crossing point
401 intersections1.back() -= conflictSize / 2;
402 // ensure non-negative offset for weird geometries
403 intersections1.back() = MAX2(0.0, intersections1.back());
404
405 // also length/geometry factor. (XXX: Why subtract width/2 *before* converting geometric position to lane pos? refs #3031)
406 intersections1.back() = lane->interpolateGeometryPosToLanePos(intersections1.back());
407
408 if (internalLaneBefore->getLogicalPredecessorLane()->getEdge().isInternal() && !foeLane->isCrossing()) {
410 }
411
412 if (foeLane->isCrossing()) {
414 const_cast<MSLink*>(before)->updateDistToFoePedCrossing(intersections1.back());
415 };
416 }
417
418 myConflicts.push_back(ConflictInfo(
419 lane->getLength() - intersections1.back(),
420 conflictSize, flag));
421
422#ifdef MSLink_DEBUG_CROSSING_POINTS
423 std::cout
424 << " intersection of " << lane->getID()
425 << " totalLength=" << lane->getLength()
426 << " with " << foeLane->getID()
427 << " totalLength=" << foeLane->getLength()
428 << " dist1=" << myConflicts.back().lengthBehindCrossing
429 << " widthFactor=" << myConflicts.back().conflictSize / foeLane->getWidth()
430 << "\n";
431#endif
432 }
433 }
434 // check for overlap with internal lanes from the same source lane
435 const MSLane* pred = lane->getLogicalPredecessorLane();
436 // to avoid overlap with vehicles that came from pred (especially when pred has endOffset > 0)
437 // we add all other internal lanes from pred as foeLanes
438 for (const MSLink* const link : pred->getLinkCont()) {
439 const MSLane* const sibling = link->getViaLane();
440 if (sibling != lane && sibling != nullptr) {
441 const double minDist = MIN2(DIVERGENCE_MIN_WIDTH, 0.5 * (lane->getWidth() + sibling->getWidth()));
442 if (lane->getShape().front().distanceTo2D(sibling->getShape().front()) >= minDist) {
443 // account for lateral shift by the entry links
444 continue;
445 }
446 const double distToDivergence = computeDistToDivergence(lane, sibling, minDist, true);
447 double lbcLane;
448 if (lane->getLength() == sibling->getLength() && &lane->getEdge() == &sibling->getEdge()) {
449 // for parallel lanes, avoid inconsistency in distance estimation (#10988)
450 // between forward distance (getLeaderInfo)
451 // and backward distance used in lane-changing (getFollowersOnConsecutive)
452 lbcLane = lane->getLength() - distToDivergence;
453 } else {
454 lbcLane = MAX2(0.0, lane->getLength() - lane->interpolateGeometryPosToLanePos(distToDivergence));
455 }
456 ConflictInfo ci = ConflictInfo(lbcLane, sibling->getWidth());
457 auto it = std::find(myFoeLanes.begin(), myFoeLanes.end(), sibling);
458 if (it != myFoeLanes.end()) {
459 // avoid duplicate foeLane
460 const int replacedIndex = (int)(it - myFoeLanes.begin());
461 myConflicts[replacedIndex] = ci;
462 } else {
463 myConflicts.push_back(ci);
464 myFoeLanes.push_back(sibling);
465 }
466#ifdef MSLink_DEBUG_CROSSING_POINTS
467 std::cout << " adding same-origin foe" << sibling->getID()
468 << " dist1=" << myConflicts.back().lengthBehindCrossing
469 << "\n";
470#endif
471 const MSLane* const siblingCont = sibling->getLinkCont().front()->getViaLaneOrLane();
472 if (siblingCont->isInternal() && lane->getShape().distance2D(siblingCont->getShape().front()) < minDist) {
473 // there may still be overlap with siblingCont (when considering vehicle widths)
474 const double maxCommonLength = MIN2(lane->getLength(), sibling->getLength() + siblingCont->getLength());
475 const double lengthBehindDivergence = MAX2(0.0, lane->getLength() - maxCommonLength);
476 ConflictInfo ci2 = ConflictInfo(lengthBehindDivergence, siblingCont->getWidth(), CONFLICT_SIBLING_CONTINUATION);
477 myConflicts.push_back(ci2);
478 myFoeLanes.push_back(siblingCont);
479 myRecheck.insert({this, siblingCont->getLinkCont().front()});
480
481#ifdef MSLink_DEBUG_CROSSING_POINTS
482 std::cout << " adding same-origin foeContinuation" << siblingCont->getID()
483 << " dist1=" << myConflicts.back().lengthBehindCrossing
484 << "\n";
485#endif
486 }
487 }
488 }
489 // init points for the symmetrical conflict
490 // for each pair of conflicting lanes, the link that gets second, sets the pointers
491 for (int i = 0; i < (int)myFoeLanes.size(); i++) {
492 const MSLane* foeLane = myFoeLanes[i];
493 MSLink* foeExitLink = foeLane->getLinkCont()[0];
494 int foundIndex = -1;
495 for (int i2 = 0; i2 < (int)foeExitLink->myFoeLanes.size(); i2++) {
496 if (foeExitLink->myFoeLanes[i2] == lane) {
497 myConflicts[i].foeConflictIndex = i2;
498 foeExitLink->myConflicts[i2].foeConflictIndex = i;
499 myRecheck.erase({foeExitLink, this});
500 foundIndex = i2;
501 break;
502 }
503 }
504#ifdef MSLink_DEBUG_CROSSING_POINTS
505 std::cout << lane->getID() << " foeLane=" << foeLane->getID() << " index=" << i << " foundIndex=" << foundIndex << "\n";
506#endif
507 if (foundIndex < 0) {
508 if (myConflicts[i].flag != CONFLICT_NO_INTERSECTION) {
509 myRecheck.insert({this, foeExitLink});
510 }
511 }
512 }
513 }
515 // check for links with the same origin lane and the same destination edge
516 const MSEdge* myTarget = &myLane->getEdge();
517 // save foes for entry links
518 for (MSLink* const it : myLaneBefore->getLinkCont()) {
519 const MSEdge* target = &(it->getLane()->getEdge());
520 if (it == this) {
521 continue;
522 }
523 if (target == myTarget) {
524 mySublaneFoeLinks.push_back(it);
525#ifdef MSLink_DEBUG_CROSSING_POINTS
526 std::cout << " sublaneFoeLink (same target): " << it->getViaLaneOrLane()->getID() << "\n";
527#endif
528 } else if (myDirection != LinkDirection::STRAIGHT && it->getDirection() == LinkDirection::STRAIGHT) {
529 // potential turn conflict
530 mySublaneFoeLinks2.push_back(it);
531#ifdef MSLink_DEBUG_CROSSING_POINTS
532 std::cout << " sublaneFoeLink2 (other target: " << it->getViaLaneOrLane()->getID() << "\n";
533#endif
534 }
535 }
536 // save foes for exit links
537 if (fromInternalLane()) {
538 //std::cout << " setRequestInformation link=" << getViaLaneOrLane()->getID() << " before=" << myLaneBefore->getID() << " before2=" << myLaneBefore->getIncomingLanes().front().lane->getID() << "\n";
539 for (const MSLink* const link : myLaneBefore->getIncomingLanes().front().lane->getLinkCont()) {
540 if (link->getViaLane() != myInternalLaneBefore && &link->getLane()->getEdge() == myTarget) {
541 //std::cout << " add sublaneFoe=" << (*it)->getViaLane()->getID() << "\n";
542 mySublaneFoeLanes.push_back(link->getViaLane());
543 }
544 }
545 }
546 }
547 if (myInternalLaneBefore != nullptr
549 // for right turns, the curvature helps rather than restricts the linkLeader check
550 && (
553 const double angle = fabs(GeomHelper::angleDiff(
555 myLane->getShape().angleAt2D(0)));
556 if (angle > 0) {
557 double length = myInternalLaneBefore->getShape().length2D();
558 if (myInternalLaneBefore->getIncomingLanes().size() == 1 &&
559 myInternalLaneBefore->getIncomingLanes()[0].lane->isInternal()) {
560 length += myInternalLaneBefore->getIncomingLanes()[0].lane->getShape().length2D();
561 } else if (myInternalLane != nullptr) {
562 length += myInternalLane->getShape().length2D();
563 }
564 myRadius = length / angle;
565 //std::cout << getDescription() << " a=" << RAD2DEG(angle) << " l=" << length << " r=" << myRadius << "\n";
566 }
567 }
568}
569
570
571void
573 for (auto item : myRecheck) {
574#ifdef MSLink_DEBUG_CROSSING_POINTS
575 std::cout << " recheck l1=" << item.first->getDescription() << " l2=" << item.second->getDescription() << "\n";
576#endif
577 MSLink* const link = item.first;
578 MSLink* const foeExitLink = item.second;
579 const MSLane* const lane = link->getInternalLaneBefore();
580 const MSLane* const foeLane = foeExitLink->getInternalLaneBefore();
581 int conflictIndex = -1;
582 for (int i = 0; i < (int)link->myFoeLanes.size(); i++) {
583 if (link->myFoeLanes[i] == foeLane) {
584 conflictIndex = i;
585 break;
586 }
587 }
588 if (conflictIndex == -1) {
589 WRITE_WARNING("Could not recheck ConflictInfo for " + link->getDescription() + " and " + foeExitLink->getDescription() + "\n");
590 continue;
591 }
592 ConflictInfo& ci = link->myConflicts[conflictIndex];
594 const MSLane* const intLane = link->getInternalLaneBefore();
595 const MSLane* const siblingCont = foeExitLink->getInternalLaneBefore();
596 const MSLane* const sibling = siblingCont->getLogicalPredecessorLane();
597 // this is an approximation because intLane and sibling+siblingCont are still close to each other but may have different curvature
598 const double distToDivergence = intLane->getLength() - ci.lengthBehindCrossing;
599 double lbcSibCont = MIN2(siblingCont->getLength(), MAX2(0.0, sibling->getLength() + siblingCont->getLength() - distToDivergence));
600#ifdef MSLink_DEBUG_CROSSING_POINTS
601 std::cout << " siblingContinuation: distToDivergence=" << distToDivergence << " lbcSibCont=" << lbcSibCont << "\n";
602 std::cout << " conflictIndex=" << conflictIndex << " foeLane=" << foeLane->getID() << " foeExitLink=" << foeExitLink->getDescription() << " intLane=" << intLane->getID() << "\n";
603#endif
604 ConflictInfo ci2 = ConflictInfo(lbcSibCont, intLane->getWidth());
605 ci2.foeConflictIndex = conflictIndex;
606 ci.foeConflictIndex = (int)foeExitLink->myConflicts.size();
607 foeExitLink->myFoeLanes.push_back(intLane);
608 foeExitLink->myConflicts.push_back(ci2);
609 continue;
610 }
611
612 std::vector<double> intersections1 = foeLane->getShape().intersectsAtLengths2D(lane->getShape());
613 if (intersections1.size() == 0) {
614#ifdef MSLink_DEBUG_CROSSING_POINTS
615 std::cout << " no intersection\n";
616#endif
617 continue;
618 }
619 const double widthFactor = ci.conflictSize / foeLane->getWidth();
620 const double conflictSize2 = lane->getWidth() * widthFactor;
621 std::sort(intersections1.begin(), intersections1.end());
622 intersections1.back() -= conflictSize2 / 2;
623 intersections1.back() = MAX2(0.0, intersections1.back());
624 ci.foeConflictIndex = (int)foeExitLink->myConflicts.size();
625 foeExitLink->myConflicts.push_back(ConflictInfo(foeLane->getLength() - intersections1.back(), conflictSize2));
626#ifdef MSLink_DEBUG_CROSSING_POINTS
627 std::cout << " ci=" << conflictIndex << " wf=" << widthFactor << " flag=" << ci.flag << " flbc=" << foeExitLink->myConflicts.back().lengthBehindCrossing << "\n";
628#endif
629 }
630 myRecheck.clear();
631}
632
633double
634MSLink::computeDistToDivergence(const MSLane* lane, const MSLane* sibling, double minDist, bool sameSource, double siblingPredLength) const {
635 double lbcSibling = 0;
636 double lbcLane = 0;
637
638 PositionVector l = lane->getShape();
639 PositionVector s = sibling->getShape();
640 double length = l.length2D();
641 double sibLength = s.length2D();
642 if (!sameSource) {
643 l = l.reverse();
644 s = s.reverse();
645 } else if (sibling->getEntryLink()->myAmIndirect) {
646 // ignore final waiting position since it may be quite close to the lane
647 // shape but the waiting position is perpendicular (so the minDist
648 // requirement is not necessary
649 lbcSibling += s[-1].distanceTo2D(s[-2]);
650 s.pop_back();
651 } else if (lane->getEntryLink()->myAmIndirect) {
652 // ignore final waiting position since it may be quite close to the lane
653 // shape but the waiting position is perpendicular (so the minDist
654 // requirement is not necessary
655 lbcLane += l[-1].distanceTo2D(l[-2]);
656 l.pop_back();
657 }
658
659#ifdef MSLink_DEBUG_CROSSING_POINTS_DETAILS
660 std::cout << " sameSource=" << sameSource << " lane=" << lane->getID() << " sib=" << sibling->getID() << " minDist=" << minDist << " backDist=" << l.back().distanceTo2D(s.back()) << "\n";
661#endif
662 if (l.back().distanceTo2D(s.back()) > minDist) {
663 // compute the final divergence point
664 // this position serves two purposes:
665 // 1) once the foe vehicle back (on sibling) has passed this point, we can safely ignore it
666 // 2) both vehicles are put into a cf-relationship while before the point.
667 // Since the actual crossing point is at the start of the junction,
668 // we want to make sure that both vehicles have the same distance to the crossing point and thus follow each other naturally
669 std::vector<double> distances = l.distances(s);
670#ifdef MSLink_DEBUG_CROSSING_POINTS
671 std::cout << " distances=" << toString(distances) << "\n";
672#endif
673 assert(distances.size() == l.size() + s.size());
674 if (distances.back() > minDist && distances[l.size() - 1] > minDist) {
675 // do a pairwise check between lane and sibling to make because we do not know which of them bends more
676 for (int j = (int)s.size() - 2; j >= 0; j--) {
677 const int i = j + (int)l.size();
678 const double segLength = s[j].distanceTo2D(s[j + 1]);
679 if (distances[i] > minDist) {
680 lbcSibling += segLength;
681 } else {
682 // assume no sharp bends and just interpolate the last segment
683 lbcSibling += segLength - (minDist - distances[i]) * segLength / (distances[i + 1] - distances[i]);
684 break;
685 }
686 }
687 for (int i = (int)l.size() - 2; i >= 0; i--) {
688 const double segLength = l[i].distanceTo2D(l[i + 1]);
689 if (distances[i] > minDist) {
690 lbcLane += segLength;
691 } else {
692 // assume no sharp bends and just interpolate the last segment
693 lbcLane += segLength - (minDist - distances[i]) * segLength / (distances[i + 1] - distances[i]);
694 break;
695 }
696 }
697 }
698 assert(lbcSibling >= -NUMERICAL_EPS);
699 assert(lbcLane >= -NUMERICAL_EPS);
700 }
701 const double distToDivergence1 = sibling->getLength() + siblingPredLength - lbcSibling;
702 const double distToDivergence2 = lane->getLength() - lbcLane;
703 const double distToDivergence = MIN3(
704 MAX2(distToDivergence1, distToDivergence2),
705 sibLength, length);
706#ifdef MSLink_DEBUG_CROSSING_POINTS
707 std::cout << " distToDivergence=" << distToDivergence
708 << " distTD1=" << distToDivergence1
709 << " distTD2=" << distToDivergence2
710 << " length=" << length
711 << " sibLength=" << sibLength
712 << "\n";
713#endif
714 return distToDivergence;
715}
716
717
718bool
719MSLink::contIntersect(const MSLane* lane, const MSLane* foe) {
720 if (foe->getLinkCont()[0]->getViaLane() != nullptr) {
721 std::vector<double> intersections = lane->getShape().intersectsAtLengths2D(foe->getShape());
722 return intersections.size() > 0;
723 }
724 return false;
725}
726
727
728void
729MSLink::setApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime, const double arrivalSpeed, const double leaveSpeed,
730 const bool setRequest, const double arrivalSpeedBraking, const SUMOTime waitingTime, double dist, double latOffset) {
731 const SUMOTime leaveTime = getLeaveTime(arrivalTime, arrivalSpeed, leaveSpeed, approaching->getVehicleType().getLength());
732#ifdef DEBUG_APPROACHING
733 if (DEBUG_COND2(approaching)) {
734 std::cout << SIMTIME << " link=" << getDescription() << " setApproaching veh=" << approaching->getID();
735 if (myApproachingVehicles.size() > 0) {
736 std::cout << " curApproaching=";
737 for (auto i = myApproachingVehicles.begin(); i != myApproachingVehicles.end(); ++i) {
738 std::cout << i->first->getID() << " ";
739 }
740 }
741 std::cout << "\n";
742 }
743#endif
744#ifdef HAVE_FOX
745 ScopedLocker<> lock(myApproachingVehiclesMutex, MSGlobals::gNumSimThreads > 1);
746#endif
748 // - in meso, setApproaching may be called multiple times without intermediate removeApproaching (whenever a vehicle is blocked in MELoop::checkCar)
749 // explicit erasure is necessary because emplace does nothing if the key already exists
750 // - in micro, double registration only happens on looped routes. Here, we only wish to keep the first arrival and thus emplace has the correct behavior
751 // (on meso, only the next upcoming link is registered so nothing gets overwritten on looped routes)
752 myApproachingVehicles.erase(approaching);
753 }
754 myApproachingVehicles.emplace(approaching,
755 ApproachingVehicleInformation(arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, setRequest,
756 arrivalSpeedBraking, waitingTime, dist, approaching->getSpeed(), latOffset));
757}
758
759
760void
761MSLink::updateApproaching(const SUMOVehicle* approaching, const SUMOTime arrivalTime, const double arrivalSpeed, const double leaveSpeed,
762 const bool setRequest, const double arrivalSpeedBraking, const SUMOTime waitingTime, double dist, double latOffset) {
763 {
764#ifdef HAVE_FOX
765 ScopedLocker<> lock(myApproachingVehiclesMutex, MSGlobals::gNumSimThreads > 1);
766#endif
767 auto it = myApproachingVehicles.find(approaching);
768 if (it != myApproachingVehicles.end()) {
769 const SUMOTime leaveTime = getLeaveTime(arrivalTime, arrivalSpeed, leaveSpeed, approaching->getVehicleType().getLength());
770 it->second = ApproachingVehicleInformation(arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, setRequest,
771 arrivalSpeedBraking, waitingTime, dist, approaching->getSpeed(), latOffset);
772 return;
773 }
774 }
775 // setApproaching locks itself
776 setApproaching(approaching, arrivalTime, arrivalSpeed, leaveSpeed, setRequest, arrivalSpeedBraking, waitingTime, dist, latOffset);
777}
778
779
780void
782#ifdef DEBUG_APPROACHING
783 if (DEBUG_COND2(approaching)) {
784 std::cout << SIMTIME << " link=" << getDescription() << " setApproaching veh=" << approaching->getID();
785 if (myApproachingVehicles.size() > 0) {
786 std::cout << " curApproaching=";
787 for (auto i = myApproachingVehicles.begin(); i != myApproachingVehicles.end(); ++i) {
788 std::cout << i->first->getID() << " ";
789 }
790 }
791 std::cout << "\n";
792 }
793#endif
794#ifdef HAVE_FOX
795 ScopedLocker<> lock(myApproachingVehiclesMutex, MSGlobals::gNumSimThreads > 1);
796#endif
797 myApproachingVehicles.emplace(approaching, ai);
798}
799
800void
801MSLink::setApproachingPerson(const MSPerson* approaching, const SUMOTime arrivalTime, const SUMOTime leaveTime) {
802 if (myApproachingPersons == nullptr) {
804 }
805 myApproachingPersons->emplace(approaching, ApproachingPersonInformation(arrivalTime, leaveTime));
806}
807
808void
810#ifdef DEBUG_APPROACHING
811 if (DEBUG_COND2(veh)) {
812 std::cout << SIMTIME << " link=" << getDescription() << " removeApproaching veh=" << veh->getID();
813 if (myApproachingVehicles.size() > 0) {
814 std::cout << " curApproaching=";
815 for (auto i = myApproachingVehicles.begin(); i != myApproachingVehicles.end(); ++i) {
816 std::cout << i->first->getID() << " ";
817 }
818 }
819 std::cout << "\n";
820 }
821#endif
822#ifdef HAVE_FOX
823 ScopedLocker<> lock(myApproachingVehiclesMutex, MSGlobals::gNumSimThreads > 1);
824#endif
825 myApproachingVehicles.erase(veh);
826}
827
828
829void
831 if (myApproachingPersons == nullptr) {
832 WRITE_WARNINGF("Person '%' entered crossing lane '%' without registering approach, time=%", person->getID(), myLane->getID(), time2string(SIMSTEP));
833 return;
834 }
835#ifdef DEBUG_APPROACHING
836 if (DEBUG_COND2(person)) {
837 std::cout << SIMTIME << " Link '" << (myLaneBefore == 0 ? "NULL" : myLaneBefore->getID()) << "'->'" << (myLane == 0 ? "NULL" : myLane->getID()) << std::endl;
838 std::cout << "' Removing approaching person '" << person->getID() << "'\nCurrently registered persons:" << std::endl;
839 for (auto i = myApproachingPersons->begin(); i != myApproachingPersons->end(); ++i) {
840 std::cout << "'" << i->first->getID() << "'" << std::endl;
841 }
842 }
843#endif
844 myApproachingPersons->erase(person);
845}
846
847
850 auto i = myApproachingVehicles.find(veh);
851 if (i != myApproachingVehicles.end()) {
852 return i->second;
853 } else {
854 return ApproachingVehicleInformation(INVALID_TIME, INVALID_TIME, 0, 0, false, 0, 0, 0, 0, 0);
855 }
856}
857
858
861 auto i = myApproachingVehicles.find(veh);
862 if (i != myApproachingVehicles.end()) {
863 return &i->second;
864 } else {
865 return nullptr;
866 }
867}
868
869
870void
874
875
877MSLink::getLeaveTime(const SUMOTime arrivalTime, const double arrivalSpeed,
878 const double leaveSpeed, const double vehicleLength) const {
879 return arrivalTime == SUMOTime_MAX ? SUMOTime_MAX : arrivalTime + TIME2STEPS((getLength() + vehicleLength) / MAX2(0.5 * (arrivalSpeed + leaveSpeed), NUMERICAL_EPS));
880}
881
882
883bool
884MSLink::opened(SUMOTime arrivalTime, double arrivalSpeed, double leaveSpeed, double vehicleLength,
885 double impatience, double decel, SUMOTime waitingTime, double posLat,
886 BlockingFoes* collectFoes, bool ignoreRed, const SUMOTrafficObject* ego, double dist) const {
887#ifdef MSLink_DEBUG_OPENED
888 if (gDebugFlag1) {
889 std::cout << SIMTIME << " opened? link=" << getDescription() << " red=" << haveRed() << " cont=" << isCont() << " numFoeLinks=" << myFoeLinks.size() << " havePrio=" << havePriority() << " lastWasContMajorGreen=" << lastWasContState(LINKSTATE_TL_GREEN_MAJOR) << "\n";
890 }
891#endif
892 if (haveRed() && !ignoreRed) {
893 return false;
894 }
896 return true;
897 }
898 const SUMOTime leaveTime = getLeaveTime(arrivalTime, arrivalSpeed, leaveSpeed, vehicleLength);
900 // check for foes on the same lane with the same target edge
901 for (const MSLink* foeLink : mySublaneFoeLinks) {
902 assert(myLane != foeLink->getLane());
903 for (const auto& it : foeLink->myApproachingVehicles) {
904 const SUMOVehicle* foe = it.first;
905 if (
906 // there only is a conflict if the paths cross
907 ((posLat < foe->getLateralPositionOnLane() + it.second.latOffset && myLane->getIndex() > foeLink->myLane->getIndex())
908 || (posLat > foe->getLateralPositionOnLane() + it.second.latOffset && myLane->getIndex() < foeLink->myLane->getIndex()))
909 // the vehicle that arrives later must yield
910 && (arrivalTime > it.second.arrivalTime
911 // if both vehicles arrive at the same time, the one
912 // to the left must yield
913 || (arrivalTime == it.second.arrivalTime && posLat > foe->getLateralPositionOnLane()))) {
914 if (blockedByFoe(foe, it.second, arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, false,
915 impatience, decel, waitingTime, ego)) {
916#ifdef MSLink_DEBUG_OPENED
917 if (gDebugFlag1) {
918 std::cout << SIMTIME << " blocked by " << foe->getID() << " arrival=" << arrivalTime << " foeArrival=" << it.second.arrivalTime << "\n";
919 }
920#endif
921 if (collectFoes == nullptr) {
922#ifdef MSLink_DEBUG_OPENED
923 if (gDebugFlag1) {
924 std::cout << " link=" << getViaLaneOrLane()->getID() << " blocked by sublaneFoe=" << foe->getID() << " foeLink=" << foeLink->getViaLaneOrLane()->getID() << " posLat=" << posLat << "\n";
925 }
926#endif
927 return false;
928 } else {
929 collectFoes->push_back(it.first);
930 }
931 }
932 }
933 }
934 }
935 // check for foes on the same lane with a different target edge
936 // (straight movers take precedence if the paths cross)
937 const int lhSign = MSGlobals::gLefthand ? -1 : 1;
938 for (const MSLink* foeLink : mySublaneFoeLinks2) {
940 for (const auto& it : foeLink->myApproachingVehicles) {
941 const SUMOVehicle* foe = it.first;
942 // there only is a conflict if the paths cross
943 // and if the vehicles are not currently in a car-following relationship
944 const double egoWidth = ego == nullptr ? 1.8 : ego->getVehicleType().getWidth();
945 if (!lateralOverlap(posLat, egoWidth, foe->getLateralPositionOnLane() + it.second.latOffset, foe->getVehicleType().getWidth())
947 && (posLat * lhSign > (foe->getLateralPositionOnLane() + it.second.latOffset) * lhSign))
949 && (posLat * lhSign < (foe->getLateralPositionOnLane() + it.second.latOffset) * lhSign)))) {
950 if (blockedByFoe(foe, it.second, arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, false,
951 impatience, decel, waitingTime, ego)) {
952#ifdef MSLink_DEBUG_OPENED
953 if (gDebugFlag1) {
954 std::cout << SIMTIME << " blocked by sublane foe " << foe->getID() << " arrival=" << arrivalTime << " foeArrival=" << it.second.arrivalTime << "\n";
955 }
956#endif
957 if (collectFoes == nullptr) {
958#ifdef MSLink_DEBUG_OPENED
959 if (gDebugFlag1) {
960 std::cout << " link=" << getViaLaneOrLane()->getID() << " blocked by sublaneFoe2=" << foe->getID() << " foeLink=" << foeLink->getViaLaneOrLane()->getID() << " posLat=" << posLat << "\n";
961 }
962#endif
963 return false;
964 } else {
965 collectFoes->push_back(it.first);
966 }
967 }
968 }
969 }
970 }
971 }
972#ifdef MSLink_DEBUG_OPENED
973 /*
974 if (gDebugFlag1) {
975 std::cout << SIMTIME << " isExitLinkAfterInternalJunction=" << isExitLinkAfterInternalJunction()
976 << " entryLink=" << getCorrespondingEntryLink()->getDescription()
977 << " entryState=" << getCorrespondingEntryLink()->getState()
978 << "\n";
979 }
980 */
981#endif
982 if ((havePriority()
986 // priority usually means the link is open but there are exceptions:
987 // zipper still needs to collect foes
988 // sublane model could have detected a conflict
989 return collectFoes == nullptr || collectFoes->size() == 0;
990 }
991 if (myState == LINKSTATE_ALLWAY_STOP && waitingTime < TIME2STEPS(ego == nullptr ? TS : ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_ALLWAYSTOP_WAIT, TS))) {
992 return false;
993 } else if (myState == LINKSTATE_STOP && waitingTime < TIME2STEPS(ego == nullptr ? TS : ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPSIGN_WAIT, TS))) {
994 return false;
995 }
996
997 const std::vector<MSLink*>& foeLinks = (myOffFoeLinks == nullptr || getCorrespondingEntryLink()->getState() != LINKSTATE_ALLWAY_STOP) ? myFoeLinks : *myOffFoeLinks;
998
999 if (MSGlobals::gUseMesoSim && impatience == 1 && !myLane->getEdge().isRoundabout()) {
1000 return true;
1001 }
1002 if (myLane->getBidiLane() != nullptr) {
1003 MSLane* bidi = myLane->getBidiLane();
1004 if (bidi->getVehicleNumber() > 0) {
1005 if (ego == nullptr) {
1006 return false;
1007 }
1008 double maxOncomingWidth = 0;
1009 const MSLane::VehCont& vehs = bidi->getVehiclesSecure();
1010 for (MSVehicle* foe : vehs) {
1011 maxOncomingWidth = MAX2(maxOncomingWidth, foe->getVehicleType().getWidth());
1012 }
1013 bidi->releaseVehicles();
1015 if (ego->getVehicleType().getWidth() + maxOncomingWidth + MSGlobals::gLateralResolution < myLane->getWidth()) {
1016 return false;
1017 }
1018 } else if (maxOncomingWidth > 0) {
1019 // do not enter a lane that has any oncoming vehicles
1020 return false;
1021 }
1022 }
1023 for (auto ili : bidi->getIncomingLanes()) {
1024 if (ili.lane->getEdge().getPriority() > myLaneBefore->getEdge().getPriority()
1025 || (ili.lane->getEdge().getPriority() == myLaneBefore->getEdge().getPriority()
1026 && ili.lane->getID() > myLaneBefore->getID())) {
1027 BlockingFoes bidiApproachFoes;
1028 double maxOncomingWidth = 0;
1029 if (ili.viaLink->blockedAtTime(arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, false, 0, decel, 0,
1030 MSGlobals::gLateralResolution ? &bidiApproachFoes : nullptr, ego) || bidiApproachFoes.size() > 0) {
1032 for (const SUMOTrafficObject* foe : bidiApproachFoes) {
1033 maxOncomingWidth = MAX2(maxOncomingWidth, foe->getVehicleType().getWidth());
1034 }
1035 if (ego->getVehicleType().getWidth() + maxOncomingWidth + MSGlobals::gLateralResolution < myLane->getWidth()) {
1036 return false;
1037 }
1038 } else {
1039 return false;
1040 }
1041 }
1042 }
1043 }
1044 }
1045 const bool lastWasContRed = lastWasContState(LINKSTATE_TL_RED);
1046 for (const MSLink* const link : foeLinks) {
1048 if (link->haveRed()) {
1049 continue;
1050 }
1051 }
1052#ifdef MSLink_DEBUG_OPENED
1053 if (gDebugFlag1) {
1054 std::cout << SIMTIME << " foeLink=" << link->getViaLaneOrLane()->getID() << " numApproaching=" << link->getApproaching().size() << "\n";
1055 if (link->getLane()->isCrossing()) {
1056 std::cout << SIMTIME << " approachingPersons=" << (link->myApproachingPersons == nullptr ? "NULL" : toString(link->myApproachingPersons->size())) << "\n";
1057 }
1058 }
1059#endif
1060 if (link->blockedAtTime(arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, myLane == link->getLane(),
1061 impatience, decel, waitingTime, collectFoes, ego, lastWasContRed, dist)) {
1062 return false;
1063 }
1064 }
1065 if (collectFoes != nullptr && collectFoes->size() > 0) {
1066 return false;
1067 }
1068 return true;
1069}
1070
1071
1072bool
1073MSLink::blockedAtTime(SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
1074 bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
1075 BlockingFoes* collectFoes, const SUMOTrafficObject* ego, bool lastWasContRed, double dist) const {
1076 for (const auto& it : myApproachingVehicles) {
1077#ifdef MSLink_DEBUG_OPENED
1078 if (gDebugFlag1) {
1079 if (ego != nullptr
1080 && ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_FOE_SPEED, 0) >= it.second.speed
1082 std::stringstream stream; // to reduce output interleaving from different threads
1083 stream << SIMTIME << " " << myApproachingVehicles.size() << " foe link=" << getViaLaneOrLane()->getID()
1084 << " foeVeh=" << it.first->getID() << " (below ignore speed)"
1085 << " ignoreFoeProb=" << ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_FOE_PROB, 0)
1086 << "\n";
1087 std::cout << stream.str();
1088 }
1089 }
1090#endif
1091 if (it.first != ego
1092 && (ego == nullptr
1096 && !ignoreFoe(ego, it.first)
1097 && (!lastWasContRed || it.first->getSpeed() > SUMO_const_haltingSpeed)
1098 && blockedByFoe(it.first, it.second, arrivalTime, leaveTime, arrivalSpeed, leaveSpeed, sameTargetLane,
1099 impatience, decel, waitingTime, ego)) {
1100 if (collectFoes == nullptr) {
1101 return true;
1102 } else {
1103 collectFoes->push_back(it.first);
1104 }
1105 }
1106 }
1107 if (myApproachingPersons != nullptr && !haveRed()) {
1108 const SUMOTime lookAhead = (ego == nullptr
1111 for (const auto& it : *myApproachingPersons) {
1112#ifdef MSLink_DEBUG_OPENED
1113 if (gDebugFlag1) {
1114 std::cout << SIMTIME << ": " << ego->getID() << " check person " << it.first->getID() << " aTime=" << arrivalTime << " foeATime=" << it.second.arrivalTime
1115 << " lTime=" << leaveTime << " foeLTime=" << it.second.leavingTime
1116 << " dist=" << dist << "\n";
1117 }
1118#endif
1119 if ((ego == nullptr
1121 || ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_FOE_SPEED, 0) < it.first->getSpeed()
1123 && !ignoreFoe(ego, it.first)
1124 && !((arrivalTime > it.second.leavingTime + lookAhead) || (leaveTime + lookAhead < it.second.arrivalTime))) {
1125 if (ego == nullptr) {
1126 // during insertion
1128 continue;
1129 } else {
1130 return true;
1131 }
1132 }
1133 // check whether braking is feasible (ego might have started to accelerate already)
1134 const auto& cfm = ego->getVehicleType().getCarFollowModel();
1135#ifdef MSLink_DEBUG_OPENED
1136 if (gDebugFlag1) {
1137 std::cout << SIMTIME << ": " << ego->getID() << " conflict with person " << it.first->getID() << " aTime=" << arrivalTime << " foeATime=" << it.second.arrivalTime << " dist=" << dist << " bGap=" << cfm.brakeGap(ego->getSpeed(), cfm.getMaxDecel(), 0) << "\n";
1138 }
1139#endif
1140 if (dist > cfm.brakeGap(ego->getSpeed(), cfm.getMaxDecel(), 0)) {
1141#ifdef MSLink_DEBUG_OPENED
1142 if (gDebugFlag1) {
1143 std::cout << SIMTIME << ": " << ego->getID() << " blocked by person " << it.first->getID() << "\n";
1144 }
1145#endif
1146 if (collectFoes == nullptr) {
1147 return true;
1148 } else {
1149 collectFoes->push_back(it.first);
1150 }
1151 }
1152 }
1153 }
1154 }
1155 return false;
1156}
1157
1158
1159bool
1161 SUMOTime arrivalTime, SUMOTime leaveTime, double arrivalSpeed, double leaveSpeed,
1162 bool sameTargetLane, double impatience, double decel, SUMOTime waitingTime,
1163 const SUMOTrafficObject* ego) const {
1164#ifdef MSLink_DEBUG_OPENED
1165 if (gDebugFlag1) {
1166 std::stringstream stream; // to reduce output interleaving from different threads
1167 stream << " link=" << getDescription()
1168 << " foeVeh=" << veh->getID()
1169 << " req=" << avi.willPass
1170 << " aT=" << avi.arrivalTime
1171 << " lT=" << avi.leavingTime
1172 << "\n";
1173 std::cout << stream.str();
1174 }
1175#endif
1176 if (!avi.willPass) {
1177 return false;
1178 }
1180 assert(waitingTime > 0);
1181#ifdef MSLink_DEBUG_OPENED
1182 if (gDebugFlag1) {
1183 std::stringstream stream; // to reduce output interleaving from different threads
1184 stream << " foeDist=" << avi.dist
1185 << " foeBGap=" << veh->getBrakeGap(false)
1186 << " foeWait=" << avi.waitingTime
1187 << " wait=" << waitingTime
1188 << "\n";
1189 std::cout << stream.str();
1190 }
1191#endif
1192 // when using actionSteps, the foe waiting time may be outdated
1193 const SUMOTime actionDelta = SIMSTEP - veh->getLastActionTime();
1194 if (waitingTime > avi.waitingTime + actionDelta) {
1195 return false;
1196 }
1197 if (waitingTime == (avi.waitingTime + actionDelta) && arrivalTime < avi.arrivalTime + actionDelta) {
1198 return false;
1199 }
1200 }
1201 SUMOTime foeArrivalTime = avi.arrivalTime;
1202 double foeArrivalSpeedBraking = avi.arrivalSpeedBraking;
1203 if (impatience > 0 && arrivalTime < avi.arrivalTime) {
1204#ifdef MSLink_DEBUG_OPENED
1205 gDebugFlag6 = ((ego == nullptr || ego->isSelected()) && (veh == nullptr || veh->isSelected()));
1206#endif
1207 const SUMOTime fatb = computeFoeArrivalTimeBraking(arrivalTime, veh, avi.arrivalTime, impatience, avi.dist, foeArrivalSpeedBraking);
1208 foeArrivalTime = (SUMOTime)((1. - impatience) * (double)avi.arrivalTime + impatience * (double)fatb);
1209#ifdef MSLink_DEBUG_OPENED
1210 if (gDebugFlag6) {
1211 std::cout << SIMTIME << " link=" << getDescription() << " ego=" << ego->getID() << " foe=" << veh->getID()
1212 << " at=" << STEPS2TIME(arrivalTime)
1213 << " fat=" << STEPS2TIME(avi.arrivalTime)
1214 << " fatb=" << STEPS2TIME(fatb)
1215 << " fat2=" << STEPS2TIME(foeArrivalTime)
1216 << "\n";
1217 }
1218#endif
1219 }
1220
1221
1222 const SUMOTime lookAhead = (myState == LINKSTATE_ZIPPER
1224 : (ego == nullptr
1227 //if (ego != 0) std::cout << SIMTIME << " ego=" << ego->getID() << " jmTimegapMinor=" << ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_TIMEGAP_MINOR, -1) << " lookAhead=" << lookAhead << "\n";
1228#ifdef MSLink_DEBUG_OPENED
1229 if (gDebugFlag1 || gDebugFlag6) {
1230 std::stringstream stream; // to reduce output interleaving from different threads
1231 stream << " imp=" << impatience << " fAT2=" << foeArrivalTime << " fASb=" << foeArrivalSpeedBraking << " lA=" << lookAhead << " egoAT=" << arrivalTime << " egoLT=" << leaveTime << " egoLS=" << leaveSpeed << "\n";
1232 std::cout << stream.str();
1233 }
1234#endif
1235 if (avi.leavingTime < arrivalTime) {
1236 // ego wants to be follower
1237 if (sameTargetLane && (arrivalTime - avi.leavingTime < lookAhead
1238 || unsafeMergeSpeeds(avi.leaveSpeed, arrivalSpeed,
1239 veh->getVehicleType().getCarFollowModel().getMaxDecel(), decel))) {
1240#ifdef MSLink_DEBUG_OPENED
1241 if (gDebugFlag1 || gDebugFlag6) {
1242 std::cout << " blocked (cannot follow)\n";
1243 }
1244#endif
1245 return true;
1246 }
1247 } else if (foeArrivalTime > leaveTime + lookAhead) {
1248 // ego wants to be leader.
1249 if (sameTargetLane && unsafeMergeSpeeds(leaveSpeed, foeArrivalSpeedBraking,
1250 decel, veh->getVehicleType().getCarFollowModel().getMaxDecel())) {
1251#ifdef MSLink_DEBUG_OPENED
1252 if (gDebugFlag1 || gDebugFlag6) {
1253 std::cout << " blocked (cannot lead)\n";
1254 }
1255#endif
1256 return true;
1257 }
1258 } else {
1259 // even without considering safeHeadwayTime there is already a conflict
1260#ifdef MSLink_DEBUG_OPENED
1261 if (gDebugFlag1 || gDebugFlag6) {
1262 std::cout << " blocked (hard conflict)\n";
1263 }
1264#endif
1265 return true;
1266 }
1267 return false;
1268}
1269
1270
1272MSLink::computeFoeArrivalTimeBraking(SUMOTime arrivalTime, const SUMOVehicle* foe, SUMOTime foeArrivalTime, double impatience, double dist, double& fasb) {
1273 // a: distance saved when foe brakes from arrivalTime to foeArrivalTime
1274 // b: distance driven past foeArrivalTime
1275 // m: permitted decceleration
1276 // d: total deceleration until foeArrivalTime
1277 // dist2: distance of foe at arrivalTime
1278 // actual arrivalTime must fall on a simulation step
1279 if (arrivalTime - arrivalTime % DELTA_T == foeArrivalTime - foeArrivalTime % DELTA_T) {
1280 // foe enters the junction in the same step
1281#ifdef MSLink_DEBUG_OPENED
1282 if (gDebugFlag6) {
1283 std::cout << " foeAT before egoAT\n";
1284 }
1285#endif
1286 return foeArrivalTime;
1287 }
1288 if (arrivalTime % DELTA_T > 0) {
1289 arrivalTime = arrivalTime - (arrivalTime % DELTA_T) + DELTA_T;
1290 }
1291 //arrivalTime += DELTA_T - arrivalTime % DELTA_T;
1292 const double m = foe->getVehicleType().getCarFollowModel().getMaxDecel() * impatience;
1293 const double dt = STEPS2TIME(foeArrivalTime - arrivalTime);
1294 const double d = dt * m;
1295 const double a = dt * d / 2;
1296 const double v = dist / STEPS2TIME(foeArrivalTime - SIMSTEP + DELTA_T);
1297 const double dist2 = dist - v * STEPS2TIME(arrivalTime - SIMSTEP);
1298#ifdef MSLink_DEBUG_OPENED
1299 if (gDebugFlag6) {
1300 std::cout << " dist=" << dist << " dist2=" << dist2
1301 << " at=" << STEPS2TIME(arrivalTime)
1302 << " fat=" << STEPS2TIME(foeArrivalTime)
1303 << " dt=" << dt << " v=" << v << " m=" << m << " d=" << d << " a=" << a << "\n";
1304 }
1305#endif
1306 if (0.5 * v * v / m <= dist2) {
1307#ifdef MSLink_DEBUG_OPENED
1308 if (gDebugFlag6) {
1309 std::cout << " canBrakeToStop\n";
1310 }
1311#endif
1312 fasb = 0;
1313 return foeArrivalTime + TIME2STEPS(30);
1314 }
1315 // a = b (foe reaches the original distance to the stop line)
1316 // x: time driven past foeArrivalTime
1317 // v: foe speed without braking
1318 // v2: average foe speed after foeArrivalTime (braking continues for time x)
1319 // v2 = (v - d - x * m / 2)
1320 // b = v2 * x
1321 // solving for x gives:
1322 const double x = (sqrt(4 * (v - d) * (v - d) - 8 * m * a) * -0.5 - d + v) / m;
1323
1324#ifdef MSLink_DEBUG_OPENED
1325 const double x2 = (sqrt(4 * (v - d) * (v - d) - 8 * m * a) * 0.5 - d + v) / m;
1326 if (gDebugFlag6 || std::isnan(x)) {
1327 std::cout << SIMTIME << " dist=" << dist << " dist2=" << dist2 << " at=" << STEPS2TIME(arrivalTime) << " m=" << m << " d=" << d << " v=" << v << " a=" << a << " x=" << x << " x2=" << x2 << "\n";
1328 }
1329#endif
1330 fasb = v - (dt + x) * m;
1331 return foeArrivalTime + TIME2STEPS(x);
1332}
1333
1334
1335bool
1336MSLink::hasApproachingFoe(SUMOTime arrivalTime, SUMOTime leaveTime, double speed, double decel) const {
1337 for (const MSLink* const link : myFoeLinks) {
1338 if (link->blockedAtTime(arrivalTime, leaveTime, speed, speed, myLane == link->getLane(), 0, decel, 0)) {
1339 return true;
1340 }
1341 }
1342 for (const MSLane* const lane : myFoeLanes) {
1343 if (lane->getVehicleNumberWithPartials() > 0) {
1344 return true;
1345 }
1346 }
1347 return false;
1348}
1349
1350
1351std::pair<const SUMOVehicle*, const MSLink*>
1352MSLink::getFirstApproachingFoe(const MSLink* wrapAround) const {
1353 double closetDist = std::numeric_limits<double>::max();
1354 const SUMOVehicle* closest = nullptr;
1355 const MSLink* foeLink = nullptr;
1356 for (MSLink* link : myFoeLinks) {
1357 for (const auto& it : link->myApproachingVehicles) {
1358 //std::cout << " link=" << getDescription() << " foeLink_in=" << link->getLaneBefore()->getID() << " wrapAround=" << wrapAround->getDescription() << "\n";
1359 if (link->getLaneBefore() == wrapAround->getLaneBefore()) {
1360 return std::make_pair(nullptr, wrapAround);
1361 } else if (it.second.dist < closetDist) {
1362 closetDist = it.second.dist;
1363 if (it.second.willPass) {
1364 closest = it.first;
1365 foeLink = link;
1366 }
1367 }
1368 }
1369 }
1370 return std::make_pair(closest, foeLink);
1371}
1372
1373
1374void
1376 if (myState != state) {
1378 }
1379 myState = state;
1380 if (haveGreen()) {
1382 }
1383}
1384
1385
1386void
1388 myLogic = logic;
1389}
1390
1391
1392bool
1394 // when a traffic light is switched off minor roads have their cont status revoked
1396}
1397
1398
1399bool
1402 return myInternalLaneBefore->getIncomingLanes()[0].viaLink->lastWasContMajor();
1403 }
1404 if (myInternalLane == nullptr || myAmCont) {
1405 return false;
1406 } else {
1408 if (!pred->getEdge().isInternal()) {
1409 return false;
1410 } else {
1411 const MSLane* const pred2 = pred->getLogicalPredecessorLane();
1412 assert(pred2 != nullptr);
1413 const MSLink* const predLink = pred2->getLinkTo(pred);
1414 assert(predLink != nullptr);
1415 if (predLink->havePriority()) {
1416 return true;
1417 }
1419 return predLink->getLastGreenState() == LINKSTATE_TL_GREEN_MAJOR;
1420 } else {
1421 return predLink->haveYellow();
1422 }
1423 }
1424 }
1425}
1426
1427
1428bool
1431 return false;
1432 } else {
1434 if (!pred->getEdge().isInternal()) {
1435 return false;
1436 } else {
1437 const MSLane* const pred2 = pred->getLogicalPredecessorLane();
1438 assert(pred2 != nullptr);
1439 const MSLink* const predLink = pred2->getLinkTo(pred);
1440 assert(predLink != nullptr);
1441 return predLink->getState() == linkState;
1442 }
1443 }
1444}
1445
1446
1447void
1448MSLink::writeApproaching(OutputDevice& od, const std::string fromLaneID) const {
1449 if (myApproachingVehicles.size() > 0) {
1450 od.openTag("link");
1451 od.writeAttr(SUMO_ATTR_FROM, fromLaneID);
1452 const std::string via = getViaLane() == nullptr ? "" : getViaLane()->getID();
1453 od.writeAttr(SUMO_ATTR_VIA, via);
1454 od.writeAttr(SUMO_ATTR_TO, getLane() == nullptr ? "" : getLane()->getID());
1455 std::vector<std::pair<SUMOTime, const SUMOVehicle*> > toSort; // stabilize output
1456 for (auto it : myApproachingVehicles) {
1457 toSort.push_back(std::make_pair(it.second.arrivalTime, it.first));
1458 }
1459 std::sort(toSort.begin(), toSort.end(), [](const std::pair<SUMOTime, const SUMOVehicle*>& a, const std::pair<SUMOTime, const SUMOVehicle*>& b) {
1460 return a.first < b.first || (a.first == b.first && a.second->getNumericalID() < b.second->getNumericalID());
1461 });
1462 for (std::vector<std::pair<SUMOTime, const SUMOVehicle*> >::const_iterator it = toSort.begin(); it != toSort.end(); ++it) {
1463 od.openTag("approaching");
1464 const ApproachingVehicleInformation& avi = myApproachingVehicles.find(it->second)->second;
1465 od.writeAttr(SUMO_ATTR_ID, it->second->getID());
1466 od.writeAttr(SUMO_ATTR_IMPATIENCE, it->second->getImpatience());
1467 od.writeAttr("arrivalTime", time2string(avi.arrivalTime));
1468 od.writeAttr("leaveTime", time2string(avi.leavingTime));
1469 od.writeAttr("arrivalSpeed", toString(avi.arrivalSpeed));
1470 od.writeAttr("arrivalSpeedBraking", toString(avi.arrivalSpeedBraking));
1471 od.writeAttr("leaveSpeed", toString(avi.leaveSpeed));
1472 od.writeAttr("willPass", toString(avi.willPass));
1473 od.closeTag();
1474 }
1475 od.closeTag();
1476 }
1477}
1478
1479
1480double
1482 double len = 0.;
1483 MSLane* lane = myInternalLane;
1484
1485 while (lane != nullptr && lane->isInternal()) {
1486 len += lane->getLength();
1487 lane = lane->getLinkCont()[0]->getViaLane();
1488 }
1489 return len;
1490}
1491
1492double
1494 double len = 0.;
1495 const MSLane* lane = myInternalLane;
1496
1497 while (lane != nullptr && lane->isInternal()) {
1498 len += lane->getLength();
1499 if (lane->getIncomingLanes().size() == 1) {
1500 lane = lane->getIncomingLanes()[0].lane;
1501 } else {
1502 break;
1503 }
1504 }
1505 return len;
1506}
1507
1508
1509double
1511 MSLane* via = myInternalLane;
1512 double totalDist = 0.;
1513 bool foundCrossing = false;
1514 while (via != nullptr) {
1515 MSLink* link = via->getLinkCont()[0];
1516 double dist = link->getLengthBeforeCrossing(foeLane);
1517 if (dist != INVALID_DOUBLE) {
1518 // found conflicting lane
1519 totalDist += dist;
1520 foundCrossing = true;
1521 break;
1522 } else {
1523 totalDist += via->getLength();
1524 via = link->getViaLane();
1525 }
1526 }
1527 if (foundCrossing) {
1528 return totalDist;
1529 } else {
1530 return INVALID_DOUBLE;
1531 }
1532}
1533
1534
1535double
1537 int foe_ix;
1538 for (foe_ix = 0; foe_ix != (int)myFoeLanes.size(); ++foe_ix) {
1539 if (myFoeLanes[foe_ix] == foeLane) {
1540 break;
1541 }
1542 }
1543 if (foe_ix == (int)myFoeLanes.size()) {
1544 // no conflict with the given lane, indicate by returning -1
1545#ifdef MSLink_DEBUG_CROSSING_POINTS
1546 std::cout << "No crossing of lanes '" << foeLane->getID() << "' and '" << myInternalLaneBefore->getID() << "'" << std::endl;
1547#endif
1548 return INVALID_DOUBLE;
1549 } else {
1550 // found conflicting lane index
1551 double dist = myInternalLaneBefore->getLength() - myConflicts[foe_ix].getLengthBehindCrossing(this);
1552 if (dist == -10000.) {
1553 // this is the value in myConflicts, if the relation allows intersection but none is present for the actual geometry.
1554 return INVALID_DOUBLE;
1555 }
1556#ifdef MSLink_DEBUG_CROSSING_POINTS
1557 std::cout << "Crossing of lanes '" << myInternalLaneBefore->getID() << "' and '" << foeLane->getID()
1558 << "' at distance " << dist << " (approach along '"
1559 << myInternalLaneBefore->getEntryLink()->getLaneBefore()->getID() << "')" << std::endl;
1560#endif
1561 return dist;
1562 }
1563}
1564
1565
1566bool
1569 return myInternalLane != nullptr && myInternalLaneBefore == nullptr;
1570 } else {
1571 return false;
1572 }
1573}
1574
1575bool
1577 // either a non-cont entry link or the link after a cont-link
1578 return !myAmCont && (isEntryLink() || (myInternalLaneBefore != nullptr && myInternalLane != nullptr));
1579}
1580
1581bool
1584 return myInternalLaneBefore != nullptr && myInternalLane == nullptr;
1585 } else {
1586 return false;
1587 }
1588}
1589
1590bool
1593 return (getInternalLaneBefore() != nullptr
1594 && myInternalLaneBefore->getIncomingLanes().size() == 1
1595 && myInternalLaneBefore->getIncomingLanes().front().viaLink->isInternalJunctionLink());
1596 } else {
1597 return false;
1598 }
1599}
1600
1601
1602const MSLink*
1604 MSLane* lane = myInternalLane;
1605 const MSLink* link = this;
1606 while (lane != nullptr) {
1607 link = lane->getLinkCont()[0];
1608 lane = link->getViaLane();
1609 }
1610 return link;
1611}
1612
1613
1614const MSLink*
1616 const MSLink* link = this;
1617 while (link->myLaneBefore->isInternal()) {
1618 assert(myLaneBefore->getIncomingLanes().size() == 1);
1619 link = link->myLaneBefore->getIncomingLanes().front().viaLink;
1620 }
1621 return link;
1622}
1623
1624
1625bool
1627 return getInternalLaneBefore() != nullptr && myInternalLane != nullptr;
1628}
1629
1630
1632MSLink::getLeaderInfo(const MSVehicle* ego, double dist, std::vector<const MSPerson*>* collectBlockers, bool isShadowLink) const {
1633 LinkLeaders result;
1634 // this link needs to start at an internal lane (either an exit link or between two internal lanes)
1635 // or it must be queried by the pedestrian model (ego == 0)
1636 if (ego != nullptr && (!fromInternalLane() || ego->getLaneChangeModel().isOpposite())) {
1637 // ignore link leaders
1638 return result;
1639 }
1640 //gDebugFlag1 = true;
1641 if (gDebugFlag1) {
1642 std::cout << SIMTIME << " getLeaderInfo link=" << getDescription() << " dist=" << dist << " isShadowLink=" << isShadowLink << "\n";
1643 }
1644 if (MSGlobals::gComputeLC && ego != nullptr && ego->getLane()->isNormal()) {
1645 const MSLink* junctionEntry = getLaneBefore()->getEntryLink();
1646 if (junctionEntry->haveRed() && !ego->ignoreRed(junctionEntry, true)
1647 // check oncoming on bidiLane during laneChanging
1648 && (!MSGlobals::gComputeLC || junctionEntry->getLaneBefore()->getBidiLane() == nullptr)) {
1649 if (gDebugFlag1) {
1650 std::cout << " ignore linkLeaders beyond red light\n";
1651 }
1652 return result;
1653 }
1654 }
1655 // this is an exit link
1656 const double extraGap = ego != nullptr ? ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_EXTRA_GAP, 0) : 0;
1657 const bool checkPersons = ego != nullptr && MSNet::getInstance()->hasPersons();
1658 for (int i = 0; i < (int)myFoeLanes.size(); ++i) {
1659 const MSLane* foeLane = myFoeLanes[i];
1660 if (!checkPersons && foeLane->anyVehiclesBegin() == foeLane->anyVehiclesEnd()) {
1661 // no vehicle and no person can be a leader here
1662 continue;
1663 }
1664 const MSLink* foeExitLink = foeLane->getLinkCont()[0];
1665 // distance from the querying vehicle to the crossing point with foeLane
1666 double distToCrossing = dist - myConflicts[i].getLengthBehindCrossing(this);
1667 const double foeDistToCrossing = foeLane->getLength() - myConflicts[i].getFoeLengthBehindCrossing(foeExitLink);
1668 const bool sameTarget = (myLane == foeExitLink->getLane()) && !isInternalJunctionLink() && !foeExitLink->isInternalJunctionLink();
1669 const bool sameSource = (myInternalLaneBefore != nullptr && myInternalLaneBefore->getNormalPredecessorLane() == foeLane->getNormalPredecessorLane());
1670 const double crossingWidth = (sameTarget || sameSource) ? 0 : myConflicts[i].conflictSize;
1671 const double foeCrossingWidth = (sameTarget || sameSource) ? 0 : myConflicts[i].getFoeConflictSize(foeExitLink);
1672 // special treatment of contLane foe only applies if this lane is not a contLane or contLane follower itself
1673 const bool contLane = (foeExitLink->getViaLaneOrLane()->getEdge().isInternal() && !(
1675 if (gDebugFlag1) {
1676 std::cout << " distToCrossing=" << distToCrossing << " foeLane=" << foeLane->getID() << " cWidth=" << crossingWidth
1677 << " flag=" << myConflicts[i].flag << " i=" << i << " fcIndex=" << myConflicts[i].foeConflictIndex
1678 << " ijl=" << isInternalJunctionLink() << " sT=" << sameTarget << " sS=" << sameSource
1679 << " lbc=" << myConflicts[i].getLengthBehindCrossing(this)
1680 << " flbc=" << myConflicts[i].getFoeLengthBehindCrossing(foeExitLink)
1681 << " cw=" << crossingWidth
1682 << " fcw=" << foeCrossingWidth
1683 << " contLane=" << contLane
1684 << " state=" << toString(myState)
1685 << " foeState=" << toString(foeExitLink->getState())
1686 << "\n";
1687 }
1688 if (distToCrossing + crossingWidth < 0 && !sameTarget
1689 && (ego == nullptr || !MSGlobals::gComputeLC || distToCrossing + crossingWidth + ego->getVehicleType().getLength() < 0)) {
1690 if (gDebugFlag1) {
1691 std::cout << " ignore:egoBeyondCrossingPoint\n";
1692 }
1693 continue; // vehicle is behind the crossing point, continue with next foe lane
1694 }
1695 bool ignoreGreenCont = false;
1696 bool foeIndirect = false;
1697 if (contLane) {
1698 const MSLink* entry = getLaneBefore()->getEntryLink();
1699 const MSLink* foeEntry = foeLane->getEntryLink();
1700 foeIndirect = foeEntry->myAmIndirect;
1701 if (entry != nullptr && entry->haveGreen()
1702 && foeEntry != nullptr && foeEntry->haveGreen()
1703 && entry->myLaneBefore != foeEntry->myLaneBefore) {
1704 // ignore vehicles before an internaljunction as long as they are still in green minor mode
1705 ignoreGreenCont = true;
1706 }
1707 }
1708 if (foeIndirect && distToCrossing >= NO_INTERSECTION) {
1709 if (gDebugFlag1) {
1710 std::cout << " ignore:noIntersection\n";
1711 }
1712 continue;
1713 }
1714 // it is not sufficient to return the last vehicle on the foeLane because ego might be its leader
1715 // therefore we return all vehicles on the lane
1716 //
1717 // special care must be taken for continuation lanes. (next lane is also internal)
1718 // vehicles on cont. lanes or on internal lanes with the same target as this link can not be ignored
1719 // and should block (gap = -1) unless they are part of an indirect turn
1721 for (MSLane::AnyVehicleIterator it_veh = foeLane->anyVehiclesBegin(); it_veh != end; ++it_veh) {
1722 MSVehicle* leader = (MSVehicle*)*it_veh;
1723 const double leaderBack = leader->getBackPositionOnLane(foeLane) - extraGap;
1724 const double leaderBackDist = foeDistToCrossing - leaderBack;
1725 const double l2 = ego != nullptr ? ego->getLength() + 2 : 0; // add some slack to account for further meeting-angle effects
1726 const double sagitta = ego != nullptr && myRadius != std::numeric_limits<double>::max() ? myRadius - sqrt(myRadius * myRadius - 0.25 * l2 * l2) : 0;
1727 const bool pastTheCrossingPoint = leaderBackDist + foeCrossingWidth + sagitta < 0;
1728 const bool enteredTheCrossingPoint = leaderBackDist < leader->getVehicleType().getLength();
1729 const bool foeIsBicycleTurn = (leader->getVehicleType().getVehicleClass() == SVC_BICYCLE
1730 && foeLane->getIncomingLanes().front().viaLink->getDirection() == LinkDirection::LEFT);
1731 const bool ignoreIndirectBicycleTurn = pastTheCrossingPoint && foeIsBicycleTurn;
1732 const bool cannotIgnore = ((contLane && !ignoreIndirectBicycleTurn) || sameTarget || (sameSource && !MSGlobals::gComputeLC)) && ego != nullptr;
1733 const bool inTheWay = ((((!pastTheCrossingPoint && distToCrossing > 0) || (sameTarget && distToCrossing > leaderBackDist - leader->getLength()))
1734 && (enteredTheCrossingPoint || (sameSource && !enteredTheCrossingPoint && foeDistToCrossing < distToCrossing))
1735 && (!(myConflicts[i].flag == CONFLICT_DUMMY_MERGE) || foeIsBicycleTurn || sameSource))
1736 || foeExitLink->getLaneBefore()->getNormalPredecessorLane() == myLane->getBidiLane());
1737 const bool isOpposite = leader->getLaneChangeModel().isOpposite();
1738 const auto avi = foeExitLink->getApproaching(leader);
1739 // if leader is not found, assume that it performed a lane change in the last step
1740 const bool willPass = avi.willPass || (avi.arrivalTime == INVALID_TIME && sameTarget);
1741 if (gDebugFlag1) {
1742 std::cout << " candidate leader=" << leader->getID()
1743 << " cannotIgnore=" << cannotIgnore
1744 << " fdtc=" << foeDistToCrossing
1745 << " lb=" << leaderBack
1746 << " lbd=" << leaderBackDist
1747 << " fcwidth=" << foeCrossingWidth
1748 << " r=" << myRadius
1749 << " sagitta=" << sagitta
1750 << " foePastCP=" << pastTheCrossingPoint
1751 << " foeEnteredCP=" << enteredTheCrossingPoint
1752 << " inTheWay=" << inTheWay
1753 << " willPass=" << willPass
1754 << " isFrontOnLane=" << leader->isFrontOnLane(foeLane)
1755 << " ignoreGreenCont=" << ignoreGreenCont
1756 << " foeIndirect=" << foeIndirect
1757 << " foeBikeTurn=" << foeIsBicycleTurn
1758 << " isOpposite=" << isOpposite << "\n";
1759 }
1760 if (leader == ego) {
1761 continue;
1762 }
1763 // ignore greenCont foe vehicles that are not in the way
1764 if (!inTheWay && ignoreGreenCont) {
1765 if (gDebugFlag1) {
1766 std::cout << " ignoreGreenCont\n";
1767 }
1768 continue;
1769 }
1770 // after entering the conflict area, ignore foe vehicles that are not in the way
1771 if ((!MSGlobals::gComputeLC || (ego != nullptr && ego->getLane() == foeLane) || (MSGlobals::gSublane && !MSGlobals::gComputeLC))
1772 && distToCrossing < -POSITION_EPS && !inTheWay
1773 && (ego == nullptr || !MSGlobals::gComputeLC || distToCrossing < -ego->getVehicleType().getLength())) {
1774 if (gDebugFlag1) {
1775 std::cout << " ego entered conflict area\n";
1776 }
1777 continue;
1778 }
1780 && sameSource
1781 && &ego->getLane()->getEdge() == &myInternalLaneBefore->getEdge()
1782 && leaderBack + leader->getLength() < ego->getPositionOnLane() - ego->getLength()) {
1783 // ego is already on the junction and clearly ahead of foe
1784 if (gDebugFlag1) {
1785 std::cout << " ego ahead of same-source foe\n";
1786 }
1787 continue;
1788 }
1789
1790 // ignore foe vehicles that will not pass
1791 if ((!cannotIgnore || leader->isStopped() || sameTarget)
1792 && !willPass
1793 && (avi.arrivalTime == INVALID_TIME || leader->getSpeed() < SUMO_const_haltingSpeed)
1794 && leader->isFrontOnLane(foeLane)
1795 && !isOpposite
1796 && !inTheWay
1797 // willPass is false if the vehicle is already on the stopping edge
1798 && !leader->willStop()) {
1799 if (gDebugFlag1) {
1800 std::cout << " foe will not pass\n";
1801 }
1802 continue;
1803 }
1804 if (leader->isBidiOn(foeLane)) {
1805 // conflict resolved via forward lane of the foe
1806 continue;
1807 }
1808 // check whether foe is blocked and might need to change before leaving the junction
1809 const bool foeStrategicBlocked = (leader->getLaneChangeModel().isStrategicBlocked() &&
1810 leader->getCarFollowModel().brakeGap(leader->getSpeed()) <= foeLane->getLength() - leaderBack);
1811 const bool sameInternalEdge = &myInternalLaneBefore->getEdge() == &foeExitLink->getInternalLaneBefore()->getEdge();
1812
1813 const bool foeLaneIsBidi = myInternalLaneBefore->getBidiLane() == foeLane;
1814 if (MSGlobals::gSublane && ego != nullptr && (sameSource || sameTarget || foeLaneIsBidi)
1815 && (!foeStrategicBlocked || sameInternalEdge)) {
1816 if (ego->getLane() == leader->getLane()) {
1817 continue;
1818 }
1819 // ignore vehicles if not in conflict sublane-wise
1820 const double egoLatOffset = isShadowLink ? ego->getLatOffset(ego->getLaneChangeModel().getShadowLane()) : 0;
1821 const double posLat = ego->getLateralPositionOnLane() + egoLatOffset;
1822 double posLatLeader = leader->getLateralPositionOnLane() + leader->getLatOffset(foeLane);
1823 if (foeLaneIsBidi) {
1824 // leader is oncoming
1825 posLatLeader = foeLane->getWidth() - posLatLeader;
1826 }
1827 const double latGap = (fabs(posLat - posLatLeader)
1828 - 0.5 * (ego->getVehicleType().getWidth() + leader->getVehicleType().getWidth()));
1829 const double maneuverDist = leader->getLaneChangeModel().getManeuverDist() * (posLat < posLatLeader ? -1 : 1);
1830 if (gDebugFlag1) {
1831 std::cout << " checkIgnore sublaneFoe lane=" << myInternalLaneBefore->getID()
1832 << " sameSource=" << sameSource
1833 << " sameTarget=" << sameTarget
1834 << " foeLaneIsBidi=" << foeLaneIsBidi
1835 << " foeLane=" << foeLane->getID()
1836 << " leader=" << leader->getID()
1837 << " egoLane=" << ego->getLane()->getID()
1838 << " leaderLane=" << leader->getLane()->getID()
1839 << " egoLat=" << posLat
1840 << " egoLatOffset=" << egoLatOffset
1841 << " leaderLat=" << posLatLeader
1842 << " leaderLatOffset=" << leader->getLatOffset(foeLane)
1843 << " latGap=" << latGap
1844 << " maneuverDist=" << maneuverDist
1845 << " computeLC=" << MSGlobals::gComputeLC
1846 << " egoMaxSpeedLat=" << ego->getVehicleType().getMaxSpeedLat()
1847 << "\n";
1848 }
1849 if (latGap > 0 && (latGap > maneuverDist || !sameTarget || !MSGlobals::gComputeLC)
1850 // do not perform sublane changes that interfere with the leader vehicle
1851 && (!MSGlobals::gComputeLC || latGap > ego->getVehicleType().getMaxSpeedLat())) {
1852 const MSLink* foeEntryLink = foeLane->getIncomingLanes().front().viaLink;
1853 if (sameSource) {
1854 // for lanes from the same edge, higer index implies a
1855 // connection further to the left
1856 const bool leaderFromRight = (myIndex > foeEntryLink->getIndex());
1857 if ((posLat > posLatLeader) == leaderFromRight) {
1858 // ignore speed since lanes diverge
1859 if (gDebugFlag1) {
1860 std::cout << " ignored (same source) leaderFromRight=" << leaderFromRight << "\n";
1861 }
1862 continue;
1863 }
1864 } else if (sameTarget) {
1865 // for lanes from different edges we cannot rely on the
1866 // index due to wrap-around issues
1867 if (myDirection != foeEntryLink->getDirection()) {
1868 bool leaderFromRight = foeEntryLink->getDirection() < myDirection;
1869 // leader vehicle should not move towards ego
1871 leaderFromRight = !leaderFromRight;
1872 }
1873 if (gDebugFlag1) {
1874 std::cout << " leaderFromRight=" << leaderFromRight << "\n";
1875 }
1876 if ((posLat > posLatLeader) == leaderFromRight
1877 // leader should keep lateral position or move away from ego
1878 && (leader->getLaneChangeModel().getSpeedLat() == 0 || leader->getLaneChangeModel().getManeuverDist() == 0
1879 || leaderFromRight == (leader->getLaneChangeModel().getSpeedLat() < latGap))
1880 && (ego->getLaneChangeModel().getSpeedLat() == 0 || ego->getLaneChangeModel().getManeuverDist() == 0
1881 || leaderFromRight == (ego->getLaneChangeModel().getSpeedLat() > -latGap))) {
1882 if (gDebugFlag1) {
1883 std::cout << " ignored (different source) leaderFromRight=" << leaderFromRight << "\n";
1884 }
1885 continue;
1886 }
1887 } else {
1888 // XXX figure out relative direction somehow
1889 }
1890 } else {
1891 if (gDebugFlag1) {
1892 std::cout << " ignored oncoming bidi leader\n";
1893 }
1894 continue;
1895 }
1896 }
1897 }
1899 // compute distance between vehicles on the superimposition of both lanes
1900 // where the crossing point is the common point
1901 double gap;
1902 bool fromLeft = true;
1903 if (ego == nullptr) {
1904 // request from pedestrian model. return distance between leaderBack and crossing point
1905 //std::cout << " foeLane=" << foeLane->getID() << " leaderBack=" << leaderBack << " foeDistToCrossing=" << foeDistToCrossing << " foeLength=" << foeLane->getLength() << " foebehind=" << myConflicts[i].second << " dist=" << dist << " behind=" << myConflicts[i].first << "\n";
1906 gap = leaderBackDist;
1907 // distToCrossing should not take into account the with of the foe lane
1908 // (which was subtracted in setRequestInformation)
1909 // Instead, the width of the foe vehicle is used directly by the caller.
1910 distToCrossing += myConflicts[i].conflictSize / 2;
1911 if (gap + foeCrossingWidth < 0) {
1912 // leader is completely past the crossing point
1913 // or there is no crossing point
1914 continue; // next vehicle
1915 }
1916 // we need to determine whether the vehicle passes the
1917 // crossing from the left or the right (heuristic)
1918 fromLeft = foeDistToCrossing > 0.5 * foeLane->getLength();
1919 } else if ((contLane && !sameSource && !ignoreIndirectBicycleTurn) || isOpposite) {
1920 gap = -std::numeric_limits<double>::max(); // always break for vehicles which are on a continuation lane or for opposite-direction vehicles
1921 } else {
1922 if (pastTheCrossingPoint && !sameTarget) {
1923 // leader is completely past the crossing point
1924 // or there is no crossing point
1925 if (gDebugFlag1) {
1926 std::cout << " foePastCP ignored\n";
1927 }
1928 continue;
1929 }
1930 double leaderBackDist2 = leaderBackDist;
1931 if (sameTarget && leaderBackDist2 < 0) {
1932 const double mismatch = myConflicts[i].getFoeLengthBehindCrossing(foeExitLink) - myConflicts[i].getLengthBehindCrossing(this);
1933 if (mismatch > 0) {
1934 leaderBackDist2 += mismatch;
1935 }
1936 }
1937 if (gDebugFlag1) {
1938 std::cout << " distToCrossing=" << distToCrossing << " leaderBack=" << leaderBack
1939 << " backDist=" << leaderBackDist
1940 << " backDist2=" << leaderBackDist2
1941 << " blockedStrategic=" << leader->getLaneChangeModel().isStrategicBlocked()
1942 << "\n";
1943 }
1944 gap = distToCrossing - ego->getVehicleType().getMinGap() - leaderBackDist2 - foeCrossingWidth;
1945 }
1946 // if the foe is already moving off the intersection, we may
1947 // advance up to the crossing point unless we have the same target or same source
1948 // (for sameSource, the crossing point indicates the point of divergence)
1949 const bool stopAsap = ((leader->isFrontOnLane(foeLane) ? cannotIgnore : (sameTarget || sameSource))
1950 || (ego != nullptr && ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_ADVANCE, 1.0) == 0.0));
1951 if (gDebugFlag1) {
1952 std::cout << " leader=" << leader->getID() << " contLane=" << contLane << " cannotIgnore=" << cannotIgnore << " stopAsap=" << stopAsap << " gap=" << gap << "\n";
1953 }
1954 if (ignoreFoe(ego, leader)) {
1955 continue;
1956 }
1957 const int llFlags = ((fromLeft ? LL_FROM_LEFT : 0) |
1958 (inTheWay ? LL_IN_THE_WAY : 0) |
1959 (sameSource ? LL_SAME_SOURCE : 0) |
1960 (sameTarget ? LL_SAME_TARGET : 0));
1961 result.emplace_back(leader, gap, stopAsap ? -1 : distToCrossing, llFlags, leader->getLatOffset(foeLane));
1962 }
1963
1964 }
1965 if (ego != nullptr && MSNet::getInstance()->hasPersons()) {
1966 // check for crossing pedestrians (keep driving if already on top of the crossing
1967 const double distToPeds = distToCrossing - ego->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPLINE_CROSSING_GAP, MSPModel::SAFETY_GAP);
1968 const double vehWidth = ego->getVehicleType().getWidth() + MSPModel::SAFETY_GAP; // + configurable safety gap
1970 // @check lefthand?!
1971 const bool wayIn = myConflicts[i].lengthBehindCrossing < myLaneBefore->getLength() * 0.5;
1972 const double vehCenter = (foeDistToCrossing + myLaneBefore->getWidth() * 0.5
1973 + ego->getLateralPositionOnLane() * (wayIn ? -1 : 1));
1974 // can access the movement model here since we already checked for existing persons above
1975 if (distToPeds >= -MSPModel::SAFETY_GAP && MSNet::getInstance()->getPersonControl().getMovementModel()->blockedAtDist(ego, foeLane, vehCenter, vehWidth,
1977 collectBlockers)) {
1978 result.emplace_back(nullptr, -1, distToPeds);
1979 } else if (foeLane->isCrossing() && ego->getLane()->isInternal() && ego->getLane()->getEdge().getToJunction() == myJunction) {
1980 const MSLink* crossingLink = foeLane->getIncomingLanes()[0].viaLink;
1981 if (distToCrossing > 0 && crossingLink->havePriority() && crossingLink->myApproachingPersons != nullptr) {
1982 // a person might step on the crossing at any moment, since ego
1983 // is already on the junction, the opened() check is not done anymore
1984 const double timeToEnterCrossing = distToCrossing / MAX2(ego->getSpeed(), 1.0);
1985 for (const auto& item : (*crossingLink->myApproachingPersons)) {
1986 if (!ignoreFoe(ego, item.first) && timeToEnterCrossing > STEPS2TIME(item.second.arrivalTime - SIMSTEP)) {
1987 if (gDebugFlag1) {
1988 std::cout << SIMTIME << ": " << ego->getID() << " breaking for approaching person " << item.first->getID()
1989 //<< " dtc=" << distToCrossing << " ttc=" << distToCrossing / MAX2(ego->getSpeed(), 1.0) << " foeAT=" << item.second.arrivalTime << " foeTTC=" << STEPS2TIME(item.second.arrivalTime - SIMSTEP)
1990 << "\n";
1991 }
1992 result.emplace_back(nullptr, -1, distToPeds);
1993 break;
1994 //} else {
1995 // if (gDebugFlag1) {
1996 // std::cout << SIMTIME << ": " << ego->getID() << " notBreaking for approaching person " << item.first->getID()
1997 // << " dtc=" << distToCrossing << " ttc=" << distToCrossing / MAX2(ego->getSpeed(), 1.0) << " foeAT=" << item.second.arrivalTime << " foeTTC=" << STEPS2TIME(item.second.arrivalTime - SIMSTEP)
1998 // << "\n";
1999 // }
2000 }
2001 }
2002 }
2003 }
2004 }
2005 }
2006
2007 //std::cout << SIMTIME << " ego=" << Named::getIDSecure(ego) << " link=" << getViaLaneOrLane()->getID() << " myWalkingAreaFoe=" << Named::getIDSecure(myWalkingAreaFoe) << "\n";
2008 if (ego != nullptr) {
2009 checkWalkingAreaFoe(ego, myWalkingAreaFoe, collectBlockers, result);
2010 checkWalkingAreaFoe(ego, myWalkingAreaFoeExit, collectBlockers, result);
2011 }
2012
2013 if (MSGlobals::gLateralResolution > 0 && ego != nullptr && !isShadowLink) {
2014 // check for foes on the same edge
2015 for (std::vector<MSLane*>::const_iterator it = mySublaneFoeLanes.begin(); it != mySublaneFoeLanes.end(); ++it) {
2016 const MSLane* foeLane = *it;
2018 for (MSLane::AnyVehicleIterator it_veh = foeLane->anyVehiclesBegin(); it_veh != end; ++it_veh) {
2019 MSVehicle* leader = (MSVehicle*)*it_veh;
2020 if (leader == ego) {
2021 continue;
2022 }
2023 if (leader->getLane()->isNormal()) {
2024 // leader is past the conflict point
2025 continue;
2026 }
2027 const double maxLength = MAX2(myInternalLaneBefore->getLength(), foeLane->getLength());
2028 const double gap = dist - maxLength - ego->getVehicleType().getMinGap() + leader->getBackPositionOnLane(foeLane) - extraGap;
2029 if (gap < -(ego->getVehicleType().getMinGap() + leader->getLength())) {
2030 // ego is ahead of leader
2031 continue;
2032 }
2033 const double posLat = ego->getLateralPositionOnLane();
2034 const double posLatLeader = leader->getLateralPositionOnLane() + leader->getLatOffset(foeLane);
2035 if (gDebugFlag1) {
2036 std::cout << " sublaneFoe lane=" << myInternalLaneBefore->getID()
2037 << " foeLane=" << foeLane->getID()
2038 << " leader=" << leader->getID()
2039 << " egoLane=" << ego->getLane()->getID()
2040 << " leaderLane=" << leader->getLane()->getID()
2041 << " gap=" << gap
2042 << " egoLat=" << posLat
2043 << " leaderLat=" << posLatLeader
2044 << " leaderLatOffset=" << leader->getLatOffset(foeLane)
2045 << " egoIndex=" << myInternalLaneBefore->getIndex()
2046 << " foeIndex=" << foeLane->getIndex()
2047 << " dist=" << dist
2048 << " leaderBack=" << leader->getBackPositionOnLane(foeLane)
2049 << "\n";
2050 }
2051 // there only is a conflict if the paths cross
2052 if ((posLat < posLatLeader && myInternalLaneBefore->getIndex() > foeLane->getIndex())
2053 || (posLat > posLatLeader && myInternalLaneBefore->getIndex() < foeLane->getIndex())) {
2054 if (gDebugFlag1) {
2055 std::cout << SIMTIME << " blocked by " << leader->getID() << " (sublane split) foeLane=" << foeLane->getID() << "\n";
2056 }
2057 if (ignoreFoe(ego, leader)) {
2058 continue;
2059 }
2060 result.emplace_back(leader, gap, -1, LL_SAME_SOURCE);
2061 }
2062 }
2063 }
2064 }
2065 return result;
2066}
2067
2068
2069void
2070MSLink::checkWalkingAreaFoe(const MSVehicle* ego, const MSLane* foeLane, std::vector<const MSPerson*>* collectBlockers, LinkLeaders& result) const {
2071 if (foeLane != nullptr && foeLane->getEdge().getPersons().size() > 0) {
2072 // pedestrians may be on an arbitrary path across this
2073 // walkingarea. make sure to keep enough distance.
2074 // This is a simple but conservative solution that could be improved
2075 // by ignoring pedestrians that are "obviously" not on a collision course
2076 double distToPeds = std::numeric_limits<double>::max();
2077 assert(myInternalLaneBefore != nullptr);
2079 if (ego->getLateralPositionOnLane() != 0) {
2080 egoPath.move2side((MSGlobals::gLefthand ? 1 : -1) * ego->getLateralPositionOnLane());
2081 }
2082 for (MSTransportable* t : foeLane->getEdge().getPersons()) {
2083 MSPerson* p = static_cast<MSPerson*>(t);
2084 double dist = ego->getPosition().distanceTo2D(p->getPosition()) - p->getVehicleType().getLength();
2085 const bool inFront = isInFront(ego, egoPath, p->getPosition()) || isInFront(ego, egoPath, getFuturePosition(p));
2086 if (inFront) {
2088 }
2089#ifdef DEBUG_WALKINGAREA
2090 if (ego->isSelected()) {
2091 std::cout << SIMTIME << " veh=" << ego->getID() << " ped=" << p->getID()
2092 << " pos=" << ego->getPosition() << " pedPos=" << p->getPosition()
2093 << " futurePedPos=" << getFuturePosition(p)
2094 << " rawDist=" << ego->getPosition().distanceTo2D(p->getPosition())
2095 << " inFront=" << inFront
2096 << " dist=" << dist << "\n";
2097 }
2098#endif
2099 if (dist < ego->getVehicleType().getWidth() / 2 || inFront) {
2100 if (inFront) {
2101 const double oncomingFactor = isOnComingPed(ego, p);
2102 if (oncomingFactor > 0) {
2103 // account for pedestrian movement while closing in
2104 const double timeToStop = sqrt(dist) / 2;
2105 const double pedDist = p->getMaxSpeed() * MAX2(timeToStop, TS) * oncomingFactor;
2106 dist = MAX2(0.0, dist - pedDist);
2107#ifdef DEBUG_WALKINGAREA
2108 if (ego->isSelected()) {
2109 std::cout << " timeToStop=" << timeToStop << " pedDist=" << pedDist << " factor=" << oncomingFactor << " dist2=" << dist << "\n";
2110 }
2111#endif
2112 }
2113 }
2114 if (ignoreFoe(ego, p)) {
2115 continue;
2116 }
2117 distToPeds = MIN2(distToPeds, dist);
2118 if (collectBlockers != nullptr) {
2119 collectBlockers->push_back(p);
2120 }
2121 }
2122 }
2123 if (distToPeds != std::numeric_limits<double>::max()) {
2124 // leave extra space in front
2125 result.emplace_back(nullptr, -1, distToPeds);
2126 }
2127 }
2128}
2129
2130bool
2131MSLink::isInFront(const MSVehicle* ego, const PositionVector& egoPath, const Position& pPos) const {
2132 const double pedAngle = ego->getPosition().angleTo2D(pPos);
2133 const double angleDiff = fabs(GeomHelper::angleDiff(ego->getAngle(), pedAngle));
2134#ifdef DEBUG_WALKINGAREA
2135 if (ego->isSelected()) {
2136 std::cout << " angleDiff=" << RAD2DEG(angleDiff) << "\n";
2137 }
2138#endif
2139 if (angleDiff < DEG2RAD(75)) {
2140 return egoPath.distance2D(pPos) < ego->getVehicleType().getWidth() + MSPModel::SAFETY_GAP;
2141 }
2142 return false;
2143}
2144
2145
2146double
2147MSLink::isOnComingPed(const MSVehicle* ego, const MSPerson* p) const {
2148 const double pedToEgoAngle = p->getPosition().angleTo2D(ego->getPosition());
2149 const double angleDiff = fabs(GeomHelper::angleDiff(p->getAngle(), pedToEgoAngle));
2150#ifdef DEBUG_WALKINGAREA
2151 if (ego->isSelected()) {
2152 std::cout << " ped-angleDiff=" << RAD2DEG(angleDiff) << " res=" << cos(angleDiff) << "\n";
2153 }
2154#endif
2155 if (angleDiff <= DEG2RAD(90)) {
2156 ;
2157 return cos(angleDiff);
2158 } else {
2159 return 0;
2160 }
2161}
2162
2163
2165MSLink::getFuturePosition(const MSPerson* p, double timeHorizon) const {
2166 const double a = p->getAngle();
2167 const double dist = timeHorizon * p->getMaxSpeed();
2168
2169 const Position offset(cos(a) * dist, sin(a) * dist);
2170 return p->getPosition() + offset;
2171}
2172
2173
2174MSLink*
2175MSLink::getParallelLink(int direction) const {
2176 if (direction == -1) {
2177 return myParallelRight;
2178 } else if (direction == 1) {
2179 return myParallelLeft;
2180 } else {
2181 assert(false || myLane->getOpposite() != nullptr || MSGlobals::gComputeLC);
2182 return nullptr;
2183 }
2184}
2185
2186MSLink*
2188 if (myLane->getOpposite() != nullptr && myLaneBefore->getOpposite() != nullptr) {
2189 for (MSLink* cand : myLane->getOpposite()->getLinkCont()) {
2190 if (cand->getLane() == myLaneBefore->getOpposite()) {
2191 return cand;
2192 }
2193 }
2194 }
2195 return nullptr;
2196}
2197
2198
2199MSLink*
2201 const MSLane* const before = getLaneBefore()->getParallelLane(direction, false);
2202 const MSLane* const after = getLane()->getParallelLane(direction, false);
2203 if (before != nullptr && after != nullptr) {
2204 for (MSLink* const link : before->getLinkCont()) {
2205 if (link->getLane() == after) {
2206 return link;
2207 }
2208 }
2209 }
2210 return nullptr;
2211}
2212
2213
2214double
2215MSLink::getZipperSpeed(const MSVehicle* ego, const double dist, double vSafe,
2216 SUMOTime arrivalTime,
2217 const BlockingFoes* foes) const {
2218 if (myFoeLinks.size() == 0) {
2219 // link should have LINKSTATE_MAJOR in this case
2220 assert(false);
2221 return vSafe;
2222 }
2223 const double brakeGap = ego->getCarFollowModel().brakeGap(vSafe, ego->getCarFollowModel().getMaxDecel(), TS);
2224 if (dist > MAX2(myFoeVisibilityDistance, brakeGap)) {
2225#ifdef DEBUG_ZIPPER
2227 DEBUGOUT(DEBUG_COND_ZIPPER, SIMTIME << " getZipperSpeed ego=" << ego->getID()
2228 << " dist=" << dist << " bGap=" << brakeGap << " ignoring foes (arrival in " << STEPS2TIME(arrivalTime - now) << ")\n")
2229#endif
2230 return vSafe;
2231 }
2232#ifdef DEBUG_ZIPPER
2233 DEBUGOUT(DEBUG_COND_ZIPPER, SIMTIME << " getZipperSpeed ego=" << ego->getID()
2234 << " egoAT=" << arrivalTime
2235 << " dist=" << dist
2236 << " brakeGap=" << brakeGap
2237 << " vSafe=" << vSafe
2238 << " numFoes=" << foes->size()
2239 << "\n")
2240#endif
2241 const bool uniqueFoeLink = myFoeLinks.size() == 1;
2242 MSLink* foeLink = myFoeLinks[0];
2243 for (const auto& item : *foes) {
2244 if (!item->isVehicle()) {
2245 continue;
2246 }
2247 const MSVehicle* foe = dynamic_cast<const MSVehicle*>(item);
2248 assert(foe != 0);
2249 const ApproachingVehicleInformation* aviPtr = nullptr;
2250 if (uniqueFoeLink) {
2251 aviPtr = foeLink->getApproachingPtr(foe);
2252 } else {
2253 // figure out which link is approached by the current foe
2254 for (MSLink* fl : myFoeLinks) {
2255 aviPtr = fl->getApproachingPtr(foe);
2256 if (aviPtr != nullptr) {
2257 break;
2258 }
2259 }
2260 }
2261 if (aviPtr == nullptr) {
2262 continue;
2263 }
2264 const ApproachingVehicleInformation& avi = *aviPtr;
2265 const double foeDist = (foe->isActive() ? avi.dist : MAX2(0.0, avi.dist -
2266 STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - foe->getLastActionTime()) * avi.speed));
2267
2268 if ( // ignore vehicles that arrive after us (unless they are ahead and we could easily brake for them)
2269 ((avi.arrivalTime > arrivalTime) && !couldBrakeForLeader(dist, foeDist, ego, foe)) ||
2270 // also ignore vehicles that are behind us and are able to brake for us
2271 couldBrakeForLeader(foeDist, dist, foe, ego) ||
2272 // resolve ties by lane index
2273 (avi.arrivalTime == arrivalTime && foeDist == dist && ego->getLane()->getIndex() < foe->getLane()->getIndex())) {
2274#ifdef DEBUG_ZIPPER
2275 if (DEBUG_COND_ZIPPER) std::cout
2276 << " ignoring foe=" << foe->getID()
2277 << " foeAT=" << avi.arrivalTime
2278 << " foeDist=" << avi.dist
2279 << " foeDist2=" << foeDist
2280 << " foeSpeed=" << avi.speed
2281 << " egoSpeed=" << ego->getSpeed()
2282 << " deltaDist=" << foeDist - dist
2283 << " delteSpeed=" << avi.speed - foe->getCarFollowModel().getMaxDecel() - ego->getSpeed()
2284 << " egoCouldBrake=" << couldBrakeForLeader(dist, foeDist, ego, foe)
2285 << " foeCouldBrake=" << couldBrakeForLeader(foeDist, dist, foe, ego)
2286 << "\n";
2287#endif
2288 continue;
2289 }
2290 // the idea behind speed adaption is three-fold:
2291 // 1) ego needs to be in a car-following relationship with foe eventually
2292 // thus, the ego speed should be equal to the follow speed once the foe enters
2293 // the zipper junction
2294 // 2) ego vehicle needs to put a certain distance beteen himself and foe (safeGap)
2295 // achieving this distance can be spread over time but computing
2296 // safeGap is subject to estimation errors of future speeds
2297 // 3) deceleration can be spread out over the time until true
2298 // car-following happens, at the start of speed adaptions, smaller
2299 // decelerations should be sufficient
2300
2301 // we cannot trust avi.arrivalSpeed if the foe has leader vehicles that are accelerating
2302 // lets try to extrapolate
2303 const double uMax = foe->getLane()->getVehicleMaxSpeed(foe);
2304 const double uAccel = foe->getCarFollowModel().estimateSpeedAfterDistance(foeDist, avi.speed, foe->getCarFollowModel().getMaxAccel());
2305 const double uEnd = MIN2(uMax, uAccel);
2306 const double uAvg = (avi.speed + uEnd) / 2;
2307 const double tf0 = foeDist / MAX2(NUMERICAL_EPS, uAvg);
2308 const double tf = MAX2(1.0, ceil((tf0) / TS) * TS);
2309
2310 const double vMax = ego->getLane()->getVehicleMaxSpeed(ego);
2311 const double vAccel = ego->getCarFollowModel().estimateSpeedAfterDistance(dist, ego->getSpeed(), ego->getCarFollowModel().getMaxAccel());
2312 const double vDecel = ego->getCarFollowModel().estimateSpeedAfterDistance(dist, ego->getSpeed(), -ego->getCarFollowModel().getMaxDecel());
2313 const double vEnd = MIN3(vMax, vAccel, MAX2(uEnd, vDecel));
2314 const double vAvg = (ego->getSpeed() + vEnd) / 2;
2315 const double te0 = dist / MAX2(NUMERICAL_EPS, vAvg);
2316 const double te = MAX2(1.0, ceil((te0) / TS) * TS);
2317
2318 const double tTarget = tf + ego->getCarFollowModel().getHeadwayTime();
2319 const double a = ego->getCarFollowModel().avoidArrivalAccel(dist, tTarget, vSafe, ego->getCarFollowModel().getMaxDecel());
2320
2321 const double gap = dist - foe->getVehicleType().getLength() - ego->getVehicleType().getMinGap() - foeDist;
2322 const double vFollow = ego->getCarFollowModel().followSpeed(
2323 ego, ego->getSpeed(), gap, avi.speed, foe->getCarFollowModel().getMaxDecel(), foe);
2324 const double vSafeGap = MAX2(vFollow, ego->getSpeed() + ACCEL2SPEED(a));
2325
2326 // scale behavior based on ego time to link (te)
2327 const double w = MIN2(1.0, te / 10);
2328 const double maxDecel = w * ego->getCarFollowModel().getMaxDecel() + (1 - w) * ego->getCarFollowModel().getEmergencyDecel();
2329 const double vZipper = MAX3(vFollow, ego->getSpeed() - ACCEL2SPEED(maxDecel), vSafeGap);
2330
2331 vSafe = MIN2(vSafe, vZipper);
2332#ifdef DEBUG_ZIPPER
2333 if (DEBUG_COND_ZIPPER) std::cout << " adapting to foe=" << foe->getID()
2334 << " foeDist=" << foeDist
2335 << " foeSpeed=" << avi.speed
2336 << " foeAS=" << avi.arrivalSpeed
2337 << " egoSpeed=" << ego->getSpeed()
2338 << " uMax=" << uMax
2339 << " uAccel=" << uAccel
2340 << " uEnd=" << uEnd
2341 << " uAvg=" << uAvg
2342 << " gap=" << gap
2343 << "\n "
2344 << " tf=" << tf
2345 << " te=" << te
2346 << " aSafeGap=" << a
2347 << " vMax=" << vMax
2348 << " vAccel=" << vAccel
2349 << " vDecel=" << vDecel
2350 << " vEnd=" << vEnd
2351 << " vSafeGap=" << vSafeGap
2352 << " vFollow=" << vFollow
2353 << " w=" << w
2354 << " maxDecel=" << maxDecel
2355 << " vZipper=" << vZipper
2356 << " vSafe=" << vSafe
2357 << "\n";
2358#endif
2359 }
2360 return vSafe;
2361}
2362
2363
2364bool
2365MSLink::couldBrakeForLeader(double followDist, double leaderDist, const MSVehicle* follow, const MSVehicle* leader) {
2366 return (// leader is ahead of follower
2367 followDist > leaderDist &&
2368 // and follower could brake for 1 s to stay behind leader
2369 followDist - leaderDist > follow->getSpeed() - follow->getCarFollowModel().getMaxDecel() - leader->getSpeed());
2370}
2371
2372
2373void
2378
2379bool
2381 // check whether this link gets to keep its cont status switching the tls off
2382 // @note: this could also be pre-computed in netconvert
2383 // we check whether there is any major link from this edge
2384 for (const MSLane* cand : myLaneBefore->getEdge().getLanes()) {
2385 for (const MSLink* link : cand->getLinkCont()) {
2386 if (link->getOffState() == LINKSTATE_TL_OFF_NOSIGNAL) {
2387 return true;
2388 }
2389 }
2390 }
2391 return false;
2392}
2393
2394bool
2395MSLink::lateralOverlap(double posLat, double width, double posLat2, double width2) {
2396 return fabs(posLat2 - posLat) < (width + width2) / 2;
2397}
2398
2399std::string
2401 return myLaneBefore->getID() + "->" + getViaLaneOrLane()->getID();
2402}
2403
2404
2405bool
2407 if (ego == nullptr || !ego->getParameter().wasSet(VEHPARS_JUNCTIONMODEL_PARAMS_SET)) {
2408 return false;
2409 }
2410 const SUMOVehicleParameter& param = ego->getParameter();
2411 for (const std::string& typeID : StringTokenizer(param.getParameter(toString(SUMO_ATTR_JM_IGNORE_TYPES), "")).getVector()) {
2412 if (typeID == foe->getVehicleType().getID()) {
2413 return true;
2414 }
2415 }
2416 for (const std::string& id : StringTokenizer(param.getParameter(toString(SUMO_ATTR_JM_IGNORE_IDS), "")).getVector()) {
2417 if (id == foe->getID()) {
2418 return true;
2419 }
2420 }
2421 return false;
2422}
2423
2424
2425void
2429
2430
2431std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation>
2433 assert(getApproaching().size() > 0);
2434 double minDist = std::numeric_limits<double>::max();
2435 auto closestIt = getApproaching().begin();
2436 for (auto apprIt = getApproaching().begin(); apprIt != getApproaching().end(); apprIt++) {
2437 if (apprIt->second.dist < minDist) {
2438 minDist = apprIt->second.dist;
2439 closestIt = apprIt;
2440 }
2441 }
2442 // maybe a parallel link has a closer vehicle
2443 /*
2444 for (MSLink* link2 : link->getLaneBefore()->getLinkCont()) {
2445 if (link2 != link) {
2446 for (auto apprIt2 = link2->getApproaching().begin(); apprIt2 != link2->getApproaching().end(); apprIt2++) {
2447 if (apprIt2->second.dist < minDist) {
2448 minDist = apprIt2->second.dist;
2449 closestIt = apprIt2;
2450 }
2451 }
2452 }
2453 }
2454 */
2455 return *closestIt;
2456}
2457
2458
2459bool
2462 for (const auto& item : myApproachingVehicles) {
2463 if (item.second.dist < SPEED2DIST(item.first->getSpeed())) {
2464 return true;
2465 }
2466 }
2467 }
2468 return false;
2469}
2470
2471/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define DEG2RAD(x)
Definition GeomHelper.h:35
#define RAD2DEG(x)
Definition GeomHelper.h:36
#define DEBUG_COND2(obj)
#define WRITE_WARNINGF(...)
Definition MsgHandler.h:299
#define WRITE_WARNING(msg)
Definition MsgHandler.h:298
#define TLF(string,...)
Definition MsgHandler.h:318
SUMOTime DELTA_T
Definition SUMOTime.cpp:39
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:107
#define STEPS2TIME(x)
Definition SUMOTime.h:58
#define SPEED2DIST(x)
Definition SUMOTime.h:48
#define SIMSTEP
Definition SUMOTime.h:69
#define ACCEL2SPEED(x)
Definition SUMOTime.h:54
constexpr SUMOTime TIME2STEPS(double x)
Definition SUMOTime.h:60
#define SUMOTime_MAX
Definition SUMOTime.h:34
#define SUMOTime_MIN
Definition SUMOTime.h:35
#define TS
Definition SUMOTime.h:45
#define SIMTIME
Definition SUMOTime.h:70
const SVCPermissions SVCAll
all VClasses are allowed
@ SVC_BICYCLE
vehicle is a bicycle
const long long int VEHPARS_JUNCTIONMODEL_PARAMS_SET
LinkDirection
The different directions a link between two lanes may take (or a stream between two edges)....
@ PARTLEFT
The link is a partial left direction.
@ RIGHT
The link is a (hard) right direction.
@ LEFT
The link is a (hard) left direction.
@ STRAIGHT
The link is a straight direction.
@ PARTRIGHT
The link is a partial right direction.
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
@ LINKSTATE_ALLWAY_STOP
This is an uncontrolled, all-way stop link.
@ LINKSTATE_STOP
This is an uncontrolled, minor link, has to stop.
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
@ LINKSTATE_ZIPPER
This is an uncontrolled, zipper-merge link.
@ LINKSTATE_TL_OFF_BLINKING
The link is controlled by a tls which is off and blinks, has to brake.
@ LINKSTATE_TL_RED
The link has red light (must brake)
@ LINKSTATE_TL_GREEN_MINOR
The link has green light, has to brake.
@ LINKSTATE_TL_OFF_NOSIGNAL
The link is controlled by a tls which is off, not blinking, may pass.
@ SUMO_ATTR_JM_IGNORE_FOE_SPEED
@ SUMO_ATTR_JM_STOPLINE_CROSSING_GAP
@ SUMO_ATTR_JM_STOPSIGN_WAIT
@ SUMO_ATTR_VIA
@ SUMO_ATTR_JM_IGNORE_IDS
@ SUMO_ATTR_JM_IGNORE_TYPES
@ SUMO_ATTR_JM_ALLWAYSTOP_WAIT
@ SUMO_ATTR_JM_EXTRA_GAP
@ SUMO_ATTR_JM_IGNORE_FOE_PROB
@ SUMO_ATTR_TO
@ SUMO_ATTR_FROM
@ SUMO_ATTR_JM_CROSSING_GAP
@ SUMO_ATTR_IMPATIENCE
@ SUMO_ATTR_ID
@ SUMO_ATTR_JM_TIMEGAP_MINOR
@ SUMO_ATTR_JM_ADVANCE
bool gDebugFlag6
Definition StdDefs.cpp:49
bool gDebugFlag1
global utility flags for debugging
Definition StdDefs.cpp:44
const double INVALID_DOUBLE
invalid double
Definition StdDefs.h:68
T MIN3(T a, T b, T c)
Definition StdDefs.h:93
T MIN2(T a, T b)
Definition StdDefs.h:80
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition StdDefs.h:62
T MAX2(T a, T b)
Definition StdDefs.h:86
T MAX3(T a, T b, T c)
Definition StdDefs.h:100
#define DEBUGOUT(cond, msg)
Definition StdDefs.h:151
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
static const double INVALID_OFFSET
a value to signify offsets outside the range of [0, Line.length()]
Definition GeomHelper.h:50
static double naviDegree(const double angle)
static double angleDiff(const double angle1, const double angle2)
Returns the difference of the second angle to the first angle in radiants.
static double getMinAngleDiff(double angle1, double angle2)
Returns the minimum distance (clockwise/counter-clockwise) between both angles.
double getManeuverDist() const
Returns the remaining unblocked distance for the current maneuver. (only used by sublane model)
MSLane * getShadowLane() const
Returns the lane the vehicle's shadow is on during continuous/sublane lane change.
double getSpeedLat() const
return the lateral speed of the current lane change maneuver
virtual bool isSelected() const
whether this vehicle is selected in the GUI
double getLength() const
Returns the vehicle's length.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
bool isStopped() const
Returns whether the vehicle is at a stop.
double estimateSpeedAfterDistance(const double dist, const double v, const double accel) const
double getEmergencyDecel() const
Get the vehicle type's maximal physically possible deceleration [m/s^2].
Definition MSCFModel.h:293
static double avoidArrivalAccel(double dist, double time, double speed, double maxDecel)
Computes the acceleration needed to arrive not before the given time.
double getMaxAccel() const
Get the vehicle type's maximum acceleration [m/s^2].
Definition MSCFModel.h:277
double brakeGap(const double speed) const
Returns the distance the vehicle needs to halt including driver's reaction time tau (i....
Definition MSCFModel.h:424
double getMaxDecel() const
Get the vehicle type's maximal comfortable deceleration [m/s^2].
Definition MSCFModel.h:285
virtual double followSpeed(const MSVehicle *const veh, double speed, double gap2pred, double predSpeed, double predMaxDecel, const MSVehicle *const pred=0, const CalcReason usage=CalcReason::CURRENT) const =0
Computes the vehicle's follow speed (no dawdling)
virtual double getHeadwayTime() const
Get the driver's desired headway [s].
Definition MSCFModel.h:355
A road/street connecting two junctions.
Definition MSEdge.h:78
int getPriority() const
Returns the priority of the edge.
Definition MSEdge.h:338
const std::set< MSTransportable *, ComparatorNumericalIdLess > & getPersons() const
Returns this edge's persons set.
Definition MSEdge.h:205
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition MSEdge.h:169
const MSJunction * getToJunction() const
Definition MSEdge.h:428
const MSJunction * getFromJunction() const
Definition MSEdge.h:424
bool isRoundabout() const
Definition MSEdge.h:743
bool isInternal() const
return whether this edge is an internal edge
Definition MSEdge.h:270
static bool gUseMesoSim
Definition MSGlobals.h:106
static double gLateralResolution
Definition MSGlobals.h:100
static bool gComputeLC
whether the simulationLoop is in the lane changing phase
Definition MSGlobals.h:143
static int gNumSimThreads
how many threads to use for simulation
Definition MSGlobals.h:149
static bool gLefthand
Whether lefthand-drive is being simulated.
Definition MSGlobals.h:177
static SUMOTime gIgnoreJunctionBlocker
Definition MSGlobals.h:85
static bool gSublane
whether sublane simulation is enabled (sublane model or continuous lanechanging)
Definition MSGlobals.h:168
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition MSGlobals.h:81
The base class for an intersection.
Definition MSJunction.h:58
SumoXMLNodeType getType() const
return the type of this Junction
Definition MSJunction.h:133
AnyVehicleIterator is a structure, which manages the iteration through all vehicles on the lane,...
Definition MSLane.h:129
Representation of a lane in the micro simulation.
Definition MSLane.h:84
MSLane * getParallelLane(int offset, bool includeOpposite=true) const
Returns the lane with the given offset parallel to this one or 0 if it does not exist.
Definition MSLane.cpp:2905
const MSLane * getNormalSuccessorLane() const
get normal lane following this internal lane, for normal lanes, the lane itself is returned
Definition MSLane.cpp:3306
int getVehicleNumber() const
Returns the number of vehicles on this lane (for which this lane is responsible)
Definition MSLane.h:457
AnyVehicleIterator anyVehiclesEnd() const
end iterator for iterating over all vehicles touching this lane in downstream direction
Definition MSLane.h:496
const MSLink * getEntryLink() const
Returns the entry link if this is an internal lane, else nullptr.
Definition MSLane.cpp:2824
const MSLink * getLinkTo(const MSLane *const) const
returns the link to the given lane or nullptr, if it is not connected
Definition MSLane.cpp:2801
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition MSLane.h:119
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition MSLane.h:640
const std::vector< IncomingLaneInfo > & getIncomingLanes() const
Definition MSLane.h:984
MSLane * getCanonicalPredecessorLane() const
Definition MSLane.cpp:3327
double getLength() const
Returns the lane's length.
Definition MSLane.h:632
double getVehicleMaxSpeed(const SUMOTrafficObject *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
Definition MSLane.h:575
int getIndex() const
Returns the lane's index.
Definition MSLane.h:668
MSLane * getLogicalPredecessorLane() const
get the most likely predecessor lane (sorted using by_connections_to_sorter). The result is cached in...
Definition MSLane.cpp:3271
bool isNormal() const
Definition MSLane.cpp:2675
bool isCrossing() const
Definition MSLane.cpp:2681
bool isInternal() const
Definition MSLane.cpp:2669
double interpolateGeometryPosToLanePos(double geometryPos) const
Definition MSLane.h:567
AnyVehicleIterator anyVehiclesBegin() const
begin iterator for iterating over all vehicles touching this lane in downstream direction
Definition MSLane.h:490
MSLane * getOpposite() const
return the neighboring opposite direction lane for lane changing or nullptr
Definition MSLane.cpp:4465
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition MSLane.h:484
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition MSLane.h:514
MSLane * getBidiLane() const
retrieve bidirectional lane or nullptr
Definition MSLane.cpp:4782
virtual const PositionVector & getShape(bool) const
Definition MSLane.h:294
MSEdge & getEdge() const
Returns the lane's edge.
Definition MSLane.h:790
const MSLane * getNormalPredecessorLane() const
get normal lane leading to this internal lane, for normal lanes, the lane itself is returned
Definition MSLane.cpp:3296
double getWidth() const
Returns the lane's width.
Definition MSLane.h:661
const std::vector< MSLink * > & getLinkCont() const
returns the container with all links !!!
Definition MSLane.h:750
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:201
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:335
bool hasPersons() const
Returns whether persons are simulated.
Definition MSNet.h:420
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition MSNet.cpp:1317
virtual bool blockedAtDist(const SUMOTrafficObject *ego, const MSLane *lane, double vehCenter, double vehWidth, double oncomingGap, std::vector< const MSPerson * > *collectBlockers)
whether a pedestrian is blocking the crossing of lane for the given vehicle bondaries
Definition MSPModel.h:98
static const double SAFETY_GAP
Definition MSPModel.h:59
The parent class for traffic light logics.
MSPModel * getMovementModel()
Returns the default movement model for this kind of transportables.
Position getPosition(const double) const override
Return current position (x/y, cartesian)
double getMaxSpeed() const override
Returns the maximum speed (the minimum of desired and physical maximum speed)
const MSVehicleType & getVehicleType() const override
Returns the object's "vehicle" type.
virtual double getAngle() const override
return the current angle of the transportable
Representation of a vehicle in the micro simulation.
Definition MSVehicle.h:77
bool willStop() const
Returns whether the vehicle will stop on the current edge.
SUMOTime getLastActionTime() const
Returns the time of the vehicle's last action point.
Definition MSVehicle.h:541
bool isActive() const
Returns whether the current simulation step is an action point for the vehicle.
Definition MSVehicle.h:628
SUMOTime getWaitingTime(const bool accumulated=false) const
Returns the SUMOTime waited (speed was lesser than 0.1m/s)
Definition MSVehicle.h:670
bool isFrontOnLane(const MSLane *lane) const
Returns the information whether the front of the vehicle is on the given lane.
MSAbstractLaneChangeModel & getLaneChangeModel()
Position getPosition(const double offset=0) const
Return current position (x/y, cartesian)
double getBackPositionOnLane(const MSLane *lane) const
Get the vehicle's position relative to the given lane.
Definition MSVehicle.h:398
double getLatOffset(const MSLane *lane) const
Get the offset that that must be added to interpret myState.myPosLat for the given lane.
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition MSVehicle.h:581
bool isBidiOn(const MSLane *lane) const
whether this vehicle is driving against lane
double getLateralPositionOnLane() const
Get the vehicle's lateral position on the lane.
Definition MSVehicle.h:413
double getSpeed() const
Returns the vehicle's current speed.
Definition MSVehicle.h:490
const MSCFModel & getCarFollowModel() const
Returns the vehicle's car following model definition.
Definition MSVehicle.h:973
bool ignoreRed(const MSLink *link, bool canBrake) const
decide whether a red (or yellow light) may be ignored
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition MSVehicle.h:374
double getAngle() const
Returns the vehicle's direction in radians.
Definition MSVehicle.h:735
double getWidth() const
Get the width which vehicles of this class shall have when being drawn.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type's vehicle class.
const std::string & getID() const
Returns the name of the vehicle type.
double getMinGap() const
Get the free space in front of vehicles of this class.
double getMaxSpeedLat() const
Get vehicle's maximum lateral speed [m/s].
const MSCFModel & getCarFollowModel() const
Returns the vehicle type's car following model definition (const version)
double getLength() const
Get vehicle's length [m].
const SUMOVTypeParameter & getParameter() const
const std::string & getID() const
Returns the id.
Definition Named.h:73
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
A point in 2D or 3D with translation and scaling methods.
Definition Position.h:37
double distanceTo2D(const Position &p2) const
returns the euclidean distance in the x-y-plane
Definition Position.h:273
double angleTo2D(const Position &other) const
returns the angle in the plane of the vector pointing from here to the other position (in radians bet...
Definition Position.h:283
A list of positions.
double length2D() const
Returns the length.
double rotationAtOffset(double pos) const
Returns the rotation at the given length.
std::vector< double > intersectsAtLengths2D(const PositionVector &other) const
For all intersections between this vector and other, return the 2D-length of the subvector from this ...
double distance2D(const Position &p, bool perpendicular=false) const
closest 2D-distance to point p (or -1 if perpendicular is true and the point is beyond this vector)
double nearest_offset_to_point2D(const Position &p, bool perpendicular=true) const
return the nearest offest to point 2D
std::vector< double > distances(const PositionVector &s, bool perpendicular=false) const
distances of all my points to s and all of s points to myself
void move2side(double amount, double maxExtension=100)
move position vector to side using certain amount
double angleAt2D(int pos) const
get angle in certain position of position vector (in radians between -M_PI and M_PI)
PositionVector reverse() const
reverse position vector
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Representation of a vehicle, person, or container.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
virtual SumoRNG * getRNG() const =0
Returns the associated RNG for this object.
virtual bool isSelected() const =0
whether this object is selected in the GUI
double getJMParam(const SumoXMLAttr attr, const double defaultValue) const
Returns the named value from the map, or the default if it is not contained there.
Representation of a vehicle.
Definition SUMOVehicle.h:63
virtual double getLateralPositionOnLane() const =0
Get the vehicle's lateral position on the lane.
virtual SUMOTime getLastActionTime() const =0
virtual double getBrakeGap(bool delayed=false) const =0
get distance for coming to a stop (used for rerouting checks)
Structure representing possible vehicle parameter.
bool wasSet(long long int what) const
Returns whether the given parameter was set.
A scoped lock which only triggers on condition.
std::vector< std::string > getVector()
return vector of strings
Definition json.hpp:5639