Line data Source code
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 : /****************************************************************************/
14 : /// @file MSLane.cpp
15 : /// @author Christian Roessel
16 : /// @author Jakob Erdmann
17 : /// @author Daniel Krajzewicz
18 : /// @author Tino Morenz
19 : /// @author Axel Wegener
20 : /// @author Michael Behrisch
21 : /// @author Christoph Sommer
22 : /// @author Mario Krumnow
23 : /// @author Leonhard Luecken
24 : /// @author Mirko Barthauer
25 : /// @date Mon, 05 Mar 2001
26 : ///
27 : // Representation of a lane in the micro simulation
28 : /****************************************************************************/
29 : #include <config.h>
30 :
31 : #include <cmath>
32 : #include <bitset>
33 : #include <iostream>
34 : #include <cassert>
35 : #include <functional>
36 : #include <algorithm>
37 : #include <iterator>
38 : #include <exception>
39 : #include <climits>
40 : #include <set>
41 : #include <utils/common/UtilExceptions.h>
42 : #include <utils/common/StdDefs.h>
43 : #include <utils/common/MsgHandler.h>
44 : #include <utils/common/ToString.h>
45 : #ifdef HAVE_FOX
46 : #include <utils/common/ScopedLocker.h>
47 : #endif
48 : #include <utils/options/OptionsCont.h>
49 : #include <utils/emissions/HelpersHarmonoise.h>
50 : #include <utils/geom/GeomHelper.h>
51 : #include <libsumo/TraCIConstants.h>
52 : #include <microsim/transportables/MSPModel.h>
53 : #include <microsim/transportables/MSTransportableControl.h>
54 : #include <microsim/traffic_lights/MSRailSignal.h>
55 : #include <microsim/traffic_lights/MSRailSignalControl.h>
56 : #include <microsim/traffic_lights/MSDriveWay.h>
57 : #include <microsim/lcmodels/MSAbstractLaneChangeModel.h>
58 : #include <microsim/devices/MSDevice_Taxi.h>
59 : #include <microsim/trigger/MSTriggeredRerouter.h>
60 : #include <mesosim/MELoop.h>
61 : #include "MSNet.h"
62 : #include "MSVehicleType.h"
63 : #include "MSEdge.h"
64 : #include "MSEdgeControl.h"
65 : #include "MSJunction.h"
66 : #include "MSLogicJunction.h"
67 : #include "MSLink.h"
68 : #include "MSLane.h"
69 : #include "MSVehicleTransfer.h"
70 : #include "MSGlobals.h"
71 : #include "MSVehicleControl.h"
72 : #include "MSInsertionControl.h"
73 : #include "MSVehicleControl.h"
74 : #include "MSLeaderInfo.h"
75 : #include "MSVehicle.h"
76 : #include "MSStop.h"
77 :
78 : //#define DEBUG_INSERTION
79 : //#define DEBUG_PLAN_MOVE
80 : //#define DEBUG_EXEC_MOVE
81 : //#define DEBUG_CONTEXT
82 : //#define DEBUG_PARTIALS
83 : //#define DEBUG_MANEUVER_RESERVATIONS
84 : //#define DEBUG_OPPOSITE
85 : //#define DEBUG_VEHICLE_CONTAINER
86 : //#define DEBUG_COLLISIONS
87 : //#define DEBUG_JUNCTION_COLLISIONS
88 : //#define DEBUG_PEDESTRIAN_COLLISIONS
89 : //#define DEBUG_LANE_SORTER
90 : //#define DEBUG_NO_CONNECTION
91 : //#define DEBUG_SURROUNDING
92 : //#define DEBUG_EXTRAPOLATE_DEPARTPOS
93 : //#define DEBUG_ITERATOR
94 :
95 : //#define DEBUG_COND (false)
96 : //#define DEBUG_COND (true)
97 : #define DEBUG_COND (isSelected())
98 : #define DEBUG_COND2(obj) ((obj != nullptr && (obj)->isSelected()))
99 : //#define DEBUG_COND (getID() == "ego")
100 : //#define DEBUG_COND2(obj) ((obj != 0 && (obj)->getID() == "ego"))
101 : //#define DEBUG_COND2(obj) (true)
102 :
103 :
104 : // ===========================================================================
105 : // static member definitions
106 : // ===========================================================================
107 : MSLane::DictType MSLane::myDict;
108 : MSLane::CollisionAction MSLane::myCollisionAction(MSLane::COLLISION_ACTION_TELEPORT);
109 : MSLane::CollisionAction MSLane::myIntermodalCollisionAction(MSLane::COLLISION_ACTION_WARN);
110 : bool MSLane::myCheckJunctionCollisions(false);
111 : double MSLane::myCheckJunctionCollisionMinGap(0);
112 : SUMOTime MSLane::myCollisionStopTime(0);
113 : SUMOTime MSLane::myIntermodalCollisionStopTime(0);
114 : double MSLane::myCollisionMinGapFactor(1.0);
115 : bool MSLane::myExtrapolateSubstepDepart(false);
116 : std::vector<SumoRNG> MSLane::myRNGs;
117 : DepartSpeedDefinition MSLane::myDefaultDepartSpeedDefinition(DepartSpeedDefinition::DEFAULT);
118 : double MSLane::myDefaultDepartSpeed(0);
119 :
120 :
121 : // ===========================================================================
122 : // internal class method definitions
123 : // ===========================================================================
124 : void
125 1956610 : MSLane::StoringVisitor::add(const MSLane* const l) const {
126 1956610 : switch (myDomain) {
127 843562 : case libsumo::CMD_GET_VEHICLE_VARIABLE: {
128 1527263 : for (const MSVehicle* veh : l->getVehiclesSecure()) {
129 683701 : if (myShape.distance2D(veh->getPosition()) <= myRange) {
130 339433 : myObjects.insert(veh);
131 : }
132 : }
133 843737 : for (const MSBaseVehicle* veh : l->getParkingVehicles()) {
134 175 : if (myShape.distance2D(veh->getPosition()) <= myRange) {
135 36 : myObjects.insert(veh);
136 : }
137 : }
138 843562 : l->releaseVehicles();
139 : }
140 843562 : break;
141 834857 : case libsumo::CMD_GET_PERSON_VARIABLE: {
142 834857 : l->getVehiclesSecure();
143 834857 : std::vector<MSTransportable*> persons = l->getEdge().getSortedPersons(MSNet::getInstance()->getCurrentTimeStep(), true);
144 954064 : for (auto p : persons) {
145 119207 : if (myShape.distance2D(p->getPosition()) <= myRange) {
146 62310 : myObjects.insert(p);
147 : }
148 : }
149 834857 : l->releaseVehicles();
150 834857 : }
151 834857 : break;
152 216272 : case libsumo::CMD_GET_EDGE_VARIABLE: {
153 216272 : if (myShape.size() != 1 || l->getShape().distance2D(myShape[0]) <= myRange) {
154 202806 : myObjects.insert(&l->getEdge());
155 : }
156 : }
157 : break;
158 61919 : case libsumo::CMD_GET_LANE_VARIABLE: {
159 61919 : if (myShape.size() != 1 || l->getShape().distance2D(myShape[0]) <= myRange) {
160 61097 : myObjects.insert(l);
161 : }
162 : }
163 : break;
164 : default:
165 : break;
166 :
167 : }
168 1956610 : }
169 :
170 :
171 : MSLane::AnyVehicleIterator&
172 12118919007 : MSLane::AnyVehicleIterator::operator++() {
173 12118919007 : if (nextIsMyVehicles()) {
174 11736291366 : if (myI1 != myI1End) {
175 8608537506 : myI1 += myDirection;
176 3127753860 : } else if (myI3 != myI3End) {
177 3127753860 : myI3 += myDirection;
178 : }
179 : // else: already at end
180 : } else {
181 382627641 : myI2 += myDirection;
182 : }
183 : //if (DEBUG_COND2(myLane)) std::cout << SIMTIME << " AnyVehicleIterator::operator++ lane=" << myLane->getID() << " myI1=" << myI1 << " myI2=" << myI2 << "\n";
184 12118919007 : return *this;
185 : }
186 :
187 :
188 : const MSVehicle*
189 12791348985 : MSLane::AnyVehicleIterator::operator*() {
190 12791348985 : if (nextIsMyVehicles()) {
191 12401166798 : if (myI1 != myI1End) {
192 8752341894 : return myLane->myVehicles[myI1];
193 3648824904 : } else if (myI3 != myI3End) {
194 3208340327 : return myLane->myTmpVehicles[myI3];
195 : } else {
196 : assert(myI2 == myI2End);
197 : return nullptr;
198 : }
199 : } else {
200 390182187 : return myLane->myPartialVehicles[myI2];
201 : }
202 : }
203 :
204 :
205 : bool
206 24910267992 : MSLane::AnyVehicleIterator::nextIsMyVehicles() const {
207 : #ifdef DEBUG_ITERATOR
208 : if (DEBUG_COND2(myLane)) std::cout << SIMTIME << " AnyVehicleIterator::nextIsMyVehicles lane=" << myLane->getID()
209 : << " myI1=" << myI1
210 : << " myI1End=" << myI1End
211 : << " myI2=" << myI2
212 : << " myI2End=" << myI2End
213 : << " myI3=" << myI3
214 : << " myI3End=" << myI3End
215 : << "\n";
216 : #endif
217 24910267992 : if (myI1 == myI1End && myI3 == myI3End) {
218 693420364 : if (myI2 != myI2End) {
219 : return false;
220 : } else {
221 440484577 : return true; // @note. must be caught
222 : }
223 : } else {
224 24216847628 : if (myI2 == myI2End) {
225 : return true;
226 : } else {
227 6885471433 : MSVehicle* cand = myI1 == myI1End ? myLane->myTmpVehicles[myI3] : myLane->myVehicles[myI1];
228 : #ifdef DEBUG_ITERATOR
229 : if (DEBUG_COND2(myLane)) std::cout << " "
230 : << " veh1=" << cand->getID()
231 : << " isTmp=" << (myI1 == myI1End)
232 : << " veh2=" << myLane->myPartialVehicles[myI2]->getID()
233 : << " pos1=" << cand->getPositionOnLane(myLane)
234 : << " pos2=" << myLane->myPartialVehicles[myI2]->getPositionOnLane(myLane)
235 : << "\n";
236 : #endif
237 6885471433 : if (cand->getPositionOnLane() < myLane->myPartialVehicles[myI2]->getPositionOnLane(myLane)) {
238 6412649781 : return myDownstream;
239 : } else {
240 472821652 : return !myDownstream;
241 : }
242 : }
243 : }
244 : }
245 :
246 :
247 : // ===========================================================================
248 : // member method definitions
249 : // ===========================================================================
250 : #ifdef _MSC_VER
251 : #pragma warning(push)
252 : #pragma warning(disable: 4355) // mask warning about "this" in initializers
253 : #endif
254 2151381 : MSLane::MSLane(const std::string& id, double maxSpeed, double friction, double length, MSEdge* const edge,
255 : int numericalID, const PositionVector& shape, double width,
256 : SVCPermissions permissions,
257 : SVCPermissions changeLeft, SVCPermissions changeRight,
258 : int index, bool isRampAccel,
259 : const std::string& type,
260 2151381 : const PositionVector& outlineShape) :
261 : Named(id),
262 4302762 : myNumericalID(numericalID), myShape(shape), myIndex(index),
263 2151381 : myVehicles(), myLength(length), myWidth(width),
264 2151381 : myEdge(edge), myMaxSpeed(maxSpeed),
265 2151381 : myFrictionCoefficient(friction),
266 2151381 : mySpeedModified(false),
267 2151381 : myPermissions(permissions),
268 2151381 : myChangeLeft(changeLeft),
269 2151381 : myChangeRight(changeRight),
270 2151381 : myOriginalPermissions(permissions),
271 2151381 : myLogicalPredecessorLane(nullptr),
272 2151381 : myCanonicalPredecessorLane(nullptr),
273 2151381 : myCanonicalSuccessorLane(nullptr),
274 2151381 : myBruttoVehicleLengthSum(0), myNettoVehicleLengthSum(0),
275 2151381 : myBruttoVehicleLengthSumToRemove(0), myNettoVehicleLengthSumToRemove(0),
276 2151381 : myRecalculateBruttoSum(false),
277 2151381 : myLeaderInfo(width, nullptr, 0.),
278 2151381 : myFollowerInfo(width, nullptr, 0.),
279 2151381 : myLeaderInfoTime(SUMOTime_MIN),
280 2151381 : myFollowerInfoTime(SUMOTime_MIN),
281 2151381 : myLengthGeometryFactor(MAX2(POSITION_EPS, myShape.length()) / myLength), // factor should not be 0
282 2151381 : myIsRampAccel(isRampAccel),
283 2151381 : myLaneType(type),
284 2151381 : myRightSideOnEdge(0), // initialized in MSEdge::initialize
285 2151381 : myRightmostSublane(0),
286 2151381 : myNeedsCollisionCheck(false),
287 2151381 : myOpposite(nullptr),
288 2151381 : myBidiLane(nullptr),
289 : #ifdef HAVE_FOX
290 : mySimulationTask(*this, 0),
291 : #endif
292 4302762 : myStopWatch(3) {
293 : // initialized in MSEdge::initialize
294 2151381 : initRestrictions();// may be reloaded again from initialized in MSEdge::closeBuilding
295 : assert(myRNGs.size() > 0);
296 2151381 : myRNGIndex = numericalID % myRNGs.size();
297 2151381 : if (outlineShape.size() > 0) {
298 23936 : myOutlineShape = new PositionVector(outlineShape);
299 : }
300 2151381 : }
301 : #ifdef _MSC_VER
302 : #pragma warning(pop)
303 : #endif
304 :
305 :
306 5951654 : MSLane::~MSLane() {
307 5025053 : for (MSLink* const l : myLinks) {
308 2892886 : delete l;
309 : }
310 2132167 : delete myOutlineShape;
311 12348155 : }
312 :
313 :
314 : void
315 2154045 : MSLane::initRestrictions() {
316 : // simplify unit testing without MSNet instance
317 2154045 : myRestrictions = MSGlobals::gUnitTests ? nullptr : MSNet::getInstance()->getRestrictions(myEdge->getEdgeType());
318 2154045 : }
319 :
320 :
321 : void
322 2148348 : MSLane::checkBufferType() {
323 2148348 : if (MSGlobals::gNumSimThreads <= 1) {
324 : myVehBuffer.unsetCondition();
325 : // } else {
326 : // this is an idea for better memory locality, lanes with nearby numerical ids get the same rng and thus the same thread
327 : // first tests show no visible effect though
328 : // myRNGIndex = myNumericalID * myRNGs.size() / dictSize();
329 : }
330 2148348 : }
331 :
332 :
333 : void
334 2920605 : MSLane::addLink(MSLink* link) {
335 2920605 : myLinks.push_back(link);
336 2920605 : }
337 :
338 :
339 : void
340 8928 : MSLane::setOpposite(MSLane* oppositeLane) {
341 8928 : myOpposite = oppositeLane;
342 8928 : if (myOpposite != nullptr && getLength() > myOpposite->getLength()) {
343 15 : WRITE_WARNINGF(TL("Unequal lengths of neigh lane '%' and lane '%' (% != %)."), getID(), myOpposite->getID(), getLength(), myOpposite->getLength());
344 : }
345 8928 : }
346 :
347 : void
348 33060 : MSLane::setBidiLane(MSLane* bidiLane) {
349 33060 : myBidiLane = bidiLane;
350 33060 : if (myBidiLane != nullptr && getLength() > myBidiLane->getLength()) {
351 66 : if (isNormal() || MSGlobals::gUsingInternalLanes) {
352 198 : WRITE_WARNINGF(TL("Unequal lengths of bidi lane '%' and lane '%' (% != %)."), getID(), myBidiLane->getID(), getLength(), myBidiLane->getLength());
353 : }
354 : }
355 33060 : }
356 :
357 :
358 :
359 : // ------ interaction with MSMoveReminder ------
360 : void
361 2612518 : MSLane::addMoveReminder(MSMoveReminder* rem, bool addToVehicles) {
362 2612518 : myMoveReminders.push_back(rem);
363 2612518 : if (addToVehicles) {
364 2619672 : for (MSVehicle* const veh : myVehicles) {
365 15980 : veh->addReminder(rem);
366 : }
367 : }
368 : // XXX: Here, the partial occupators are ignored!? Refs. #3255
369 2612518 : }
370 :
371 :
372 : void
373 0 : MSLane::removeMoveReminder(MSMoveReminder* rem) {
374 0 : auto it = std::find(myMoveReminders.begin(), myMoveReminders.end(), rem);
375 0 : if (it != myMoveReminders.end()) {
376 0 : myMoveReminders.erase(it);
377 0 : for (MSVehicle* const veh : myVehicles) {
378 0 : veh->removeReminder(rem);
379 : }
380 : }
381 0 : }
382 :
383 :
384 : double
385 18087931 : MSLane::setPartialOccupation(MSVehicle* v) {
386 : // multithreading: there are concurrent writes to myNeedsCollisionCheck but all of them should set it to true
387 18087931 : myNeedsCollisionCheck = true; // always check
388 : #ifdef DEBUG_PARTIALS
389 : if (DEBUG_COND2(v)) {
390 : std::cout << SIMTIME << " setPartialOccupation. lane=" << getID() << " veh=" << v->getID() << "\n";
391 : }
392 : #endif
393 : // XXX update occupancy here?
394 : #ifdef HAVE_FOX
395 18087931 : ScopedLocker<> lock(myPartialOccupatorMutex, MSGlobals::gNumSimThreads > 1);
396 : #endif
397 : //assert(std::find(myPartialVehicles.begin(), myPartialVehicles.end(), v) == myPartialVehicles.end());
398 18087931 : myPartialVehicles.push_back(v);
399 20671004 : return myLength;
400 : }
401 :
402 :
403 : void
404 18087981 : MSLane::resetPartialOccupation(MSVehicle* v) {
405 : #ifdef HAVE_FOX
406 18087981 : ScopedLocker<> lock(myPartialOccupatorMutex, MSGlobals::gNumSimThreads > 1);
407 : #endif
408 : #ifdef DEBUG_PARTIALS
409 : if (DEBUG_COND2(v)) {
410 : std::cout << SIMTIME << " resetPartialOccupation. lane=" << getID() << " veh=" << v->getID() << "\n";
411 : }
412 : #endif
413 19289635 : for (VehCont::iterator i = myPartialVehicles.begin(); i != myPartialVehicles.end(); ++i) {
414 19289577 : if (v == *i) {
415 18087923 : myPartialVehicles.erase(i);
416 : // XXX update occupancy here?
417 : //std::cout << " removed from myPartialVehicles\n";
418 : return;
419 : }
420 : }
421 : // bluelight eqipped vehicle can teleport onto the intersection without using a connection
422 : assert(false || MSGlobals::gClearState || v->getLaneChangeModel().hasBlueLight());
423 : }
424 :
425 :
426 : void
427 232016 : MSLane::setManeuverReservation(MSVehicle* v) {
428 : #ifdef DEBUG_MANEUVER_RESERVATIONS
429 : if (DEBUG_COND2(v)) {
430 : std::cout << SIMTIME << " setManeuverReservation. lane=" << getID() << " veh=" << v->getID() << "\n";
431 : }
432 : #endif
433 232016 : myManeuverReservations.push_back(v);
434 232016 : }
435 :
436 :
437 : void
438 232016 : MSLane::resetManeuverReservation(MSVehicle* v) {
439 : #ifdef DEBUG_MANEUVER_RESERVATIONS
440 : if (DEBUG_COND2(v)) {
441 : std::cout << SIMTIME << " resetManeuverReservation(): lane=" << getID() << " veh=" << v->getID() << "\n";
442 : }
443 : #endif
444 285691 : for (VehCont::iterator i = myManeuverReservations.begin(); i != myManeuverReservations.end(); ++i) {
445 285691 : if (v == *i) {
446 232016 : myManeuverReservations.erase(i);
447 : return;
448 : }
449 : }
450 : assert(false);
451 : }
452 :
453 :
454 : // ------ Vehicle emission ------
455 : void
456 3546004 : MSLane::incorporateVehicle(MSVehicle* veh, double pos, double speed, double posLat, const MSLane::VehCont::iterator& at, MSMoveReminder::Notification notification) {
457 3546004 : myNeedsCollisionCheck = true;
458 : assert(pos <= myLength);
459 : bool wasInactive = myVehicles.size() == 0;
460 3546004 : veh->enterLaneAtInsertion(this, pos, speed, posLat, notification);
461 3546004 : if (at == myVehicles.end()) {
462 : // vehicle will be the first on the lane
463 675723 : myVehicles.push_back(veh);
464 : } else {
465 2870281 : myVehicles.insert(at, veh);
466 : }
467 3546004 : myBruttoVehicleLengthSum += veh->getVehicleType().getLengthWithGap();
468 3546004 : myNettoVehicleLengthSum += veh->getVehicleType().getLength();
469 3546004 : myEdge->markDelayed();
470 3546004 : if (wasInactive) {
471 658129 : MSNet::getInstance()->getEdgeControl().gotActive(this);
472 : }
473 3546004 : if (getBidiLane() != nullptr && (!isRailway(veh->getVClass()) || (getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
474 : // railways don't need to "see" each other when moving in opposite directions on the same track (efficiency)
475 1458 : getBidiLane()->setPartialOccupation(veh);
476 : }
477 3546004 : }
478 :
479 :
480 : bool
481 753736 : MSLane::lastInsertion(MSVehicle& veh, double mspeed, double posLat, bool patchSpeed) {
482 753736 : double pos = getLength() - POSITION_EPS;
483 753736 : MSVehicle* leader = getLastAnyVehicle();
484 : // back position of leader relative to this lane
485 : double leaderBack;
486 753736 : if (leader == nullptr) {
487 : /// look for a leaders on consecutive lanes
488 3809 : veh.setTentativeLaneAndPosition(this, pos, posLat);
489 3809 : veh.updateBestLanes(false, this);
490 3809 : std::pair<MSVehicle* const, double> leaderInfo = getLeader(&veh, pos, veh.getBestLanesContinuation(), veh.getCarFollowModel().brakeGap(mspeed));
491 3809 : leader = leaderInfo.first;
492 3809 : leaderBack = pos + leaderInfo.second + veh.getVehicleType().getMinGap();
493 : } else {
494 749927 : leaderBack = leader->getBackPositionOnLane(this);
495 : //std::cout << " leaderPos=" << leader->getPositionOnLane(this) << " leaderBack=" << leader->getBackPositionOnLane(this) << " leaderLane=" << leader->getLane()->getID() << "\n";
496 : }
497 3809 : if (leader == nullptr) {
498 : // insert at the end of this lane
499 2632 : return isInsertionSuccess(&veh, mspeed, pos, posLat, patchSpeed, MSMoveReminder::NOTIFICATION_DEPARTED);
500 : } else {
501 : // try to insert behind the leader
502 751104 : const double frontGapNeeded = veh.getCarFollowModel().getSecureGap(&veh, leader, mspeed, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel()) + veh.getVehicleType().getMinGap() + POSITION_EPS;
503 751104 : if (leaderBack >= frontGapNeeded) {
504 422488 : pos = MIN2(pos, leaderBack - frontGapNeeded);
505 422488 : bool result = isInsertionSuccess(&veh, mspeed, pos, posLat, patchSpeed, MSMoveReminder::NOTIFICATION_DEPARTED);
506 : //if (!result) std::cout << " insertLast failed for " << veh.getID() << " pos=" << pos << " leaderBack=" << leaderBack << " frontGapNeeded=" << frontGapNeeded << "\n";
507 422488 : return result;
508 : }
509 : //std::cout << " insertLast failed for " << veh.getID() << " pos=" << pos << " leaderBack=" << leaderBack << " frontGapNeeded=" << frontGapNeeded << "\n";
510 : }
511 : return false;
512 : }
513 :
514 :
515 : bool
516 590187 : MSLane::freeInsertion(MSVehicle& veh, double mspeed, double posLat,
517 : MSMoveReminder::Notification notification) {
518 : // try to insert teleporting vehicles fully on this lane
519 590187 : double maxPos = myLength;
520 590187 : if (veh.hasStops() && veh.getNextStop().edge == veh.getCurrentRouteEdge()) {
521 8109 : maxPos = MAX2(0.0, veh.getNextStop().getEndPos(veh));
522 : }
523 590187 : const double minPos = (notification == MSMoveReminder::NOTIFICATION_TELEPORT ?
524 288471 : MIN2(maxPos, veh.getVehicleType().getLength()) : 0);
525 590187 : veh.setTentativeLaneAndPosition(this, minPos, 0);
526 590187 : if (myVehicles.size() == 0) {
527 : // ensure sufficient gap to followers on predecessor lanes
528 14070 : const double backOffset = minPos - veh.getVehicleType().getLength();
529 14070 : const double missingRearGap = getMissingRearGap(&veh, backOffset, mspeed);
530 14070 : if (missingRearGap > 0) {
531 1097 : if (minPos + missingRearGap <= maxPos) {
532 : // @note. The rear gap is tailored to mspeed. If it changes due
533 : // to a leader vehicle (on subsequent lanes) insertion will
534 : // still fail. Under the right combination of acceleration and
535 : // deceleration values there might be another insertion
536 : // positions that would be successful be we do not look for it.
537 : //std::cout << SIMTIME << " freeInsertion lane=" << getID() << " veh=" << veh.getID() << " unclear @(340)\n";
538 817 : return isInsertionSuccess(&veh, mspeed, minPos + missingRearGap, posLat, true, notification);
539 : }
540 : return false;
541 : } else {
542 12973 : return isInsertionSuccess(&veh, mspeed, minPos, posLat, true, notification);
543 : }
544 :
545 : } else {
546 : // check whether the vehicle can be put behind the last one if there is such
547 576117 : const MSVehicle* const leader = myVehicles.back(); // @todo reproduction of bogus old behavior. see #1961
548 576117 : const double leaderPos = leader->getBackPositionOnLane(this);
549 576117 : const double speed = leader->getSpeed();
550 576117 : const double frontGapNeeded = veh.getCarFollowModel().getSecureGap(&veh, leader, speed, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel()) + veh.getVehicleType().getMinGap();
551 576117 : if (leaderPos >= frontGapNeeded) {
552 562489 : const double tspeed = MIN2(veh.getCarFollowModel().insertionFollowSpeed(&veh, mspeed, frontGapNeeded, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel(), leader), mspeed);
553 : // check whether we can insert our vehicle behind the last vehicle on the lane
554 562489 : if (isInsertionSuccess(&veh, tspeed, minPos, posLat, true, notification)) {
555 : //std::cout << SIMTIME << " freeInsertion lane=" << getID() << " veh=" << veh.getID() << " pos=" << minPos<< " speed=" << speed << " tspeed=" << tspeed << " frontGapNeeded=" << frontGapNeeded << " lead=" << leader->getID() << " lPos=" << leaderPos << "\n vehsOnLane=" << toString(myVehicles) << " @(358)\n";
556 : return true;
557 : }
558 : }
559 : }
560 : // go through the lane, look for free positions (starting after the last vehicle)
561 : MSLane::VehCont::iterator predIt = myVehicles.begin();
562 13404463 : while (predIt != myVehicles.end()) {
563 : // get leader (may be zero) and follower
564 : // @todo compute secure position in regard to sublane-model
565 12960031 : const MSVehicle* leader = predIt != myVehicles.end() - 1 ? *(predIt + 1) : nullptr;
566 12960031 : if (leader == nullptr && myPartialVehicles.size() > 0) {
567 73893 : leader = myPartialVehicles.front();
568 : }
569 12960031 : const MSVehicle* follower = *predIt;
570 :
571 : // patch speed if allowed
572 : double speed = mspeed;
573 12960031 : if (leader != nullptr) {
574 12586311 : speed = MIN2(leader->getSpeed(), mspeed);
575 : }
576 :
577 : // compute the space needed to not collide with leader
578 : double frontMax = maxPos;
579 : if (leader != nullptr) {
580 12586311 : double leaderRearPos = leader->getBackPositionOnLane(this);
581 12586311 : double frontGapNeeded = veh.getCarFollowModel().getSecureGap(&veh, leader, speed, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel()) + veh.getVehicleType().getMinGap();
582 12586311 : frontMax = MIN2(maxPos, leaderRearPos - frontGapNeeded);
583 : }
584 : // compute the space needed to not let the follower collide
585 12960031 : const double followPos = follower->getPositionOnLane() + follower->getVehicleType().getMinGap();
586 12960031 : const double backGapNeeded = follower->getCarFollowModel().getSecureGap(follower, &veh, follower->getSpeed(), veh.getSpeed(), veh.getCarFollowModel().getMaxDecel());
587 12960031 : const double backMin = followPos + backGapNeeded + veh.getVehicleType().getLength();
588 :
589 : // check whether there is enough room (given some extra space for rounding errors)
590 12960031 : if (frontMax > minPos && backMin + POSITION_EPS < frontMax) {
591 : // try to insert vehicle (should be always ok)
592 22186 : if (isInsertionSuccess(&veh, speed, backMin + POSITION_EPS, posLat, true, notification)) {
593 : //std::cout << SIMTIME << " freeInsertion lane=" << getID() << " veh=" << veh.getID() << " @(393)\n";
594 : return true;
595 : }
596 : }
597 : ++predIt;
598 : }
599 : // first check at lane's begin
600 : //std::cout << SIMTIME << " freeInsertion lane=" << getID() << " veh=" << veh.getID() << " fail final\n";
601 : return false;
602 : }
603 :
604 :
605 : double
606 13566340 : MSLane::getDepartSpeed(const MSVehicle& veh, bool& patchSpeed) {
607 : double speed = 0;
608 13566340 : const SUMOVehicleParameter& pars = veh.getParameter();
609 13566340 : DepartSpeedDefinition dsd = pars.departSpeedProcedure;
610 13566340 : if (dsd == DepartSpeedDefinition::DEFAULT) {
611 7115190 : dsd = myDefaultDepartSpeedDefinition;
612 7115190 : if (dsd == DepartSpeedDefinition::GIVEN) {
613 3916164 : speed = myDefaultDepartSpeed;
614 : }
615 6451150 : } else if (dsd == DepartSpeedDefinition::GIVEN) {
616 1375867 : speed = pars.departSpeed;;
617 : }
618 13566340 : switch (dsd) {
619 5292031 : case DepartSpeedDefinition::GIVEN:
620 5292031 : patchSpeed = false;
621 5292031 : break;
622 53183 : case DepartSpeedDefinition::RANDOM:
623 106366 : speed = roundDecimal(RandHelper::rand(getVehicleMaxSpeed(&veh)), gPrecisionRandom);
624 53183 : patchSpeed = true;
625 53183 : break;
626 2925500 : case DepartSpeedDefinition::MAX:
627 2925500 : speed = getVehicleMaxSpeed(&veh);
628 2925500 : patchSpeed = true;
629 2925500 : break;
630 376234 : case DepartSpeedDefinition::DESIRED:
631 376234 : speed = getVehicleMaxSpeed(&veh);
632 376234 : patchSpeed = false;
633 376234 : break;
634 137501 : case DepartSpeedDefinition::LIMIT:
635 137501 : speed = getVehicleMaxSpeed(&veh) / veh.getChosenSpeedFactor();
636 137501 : patchSpeed = false;
637 137501 : break;
638 8177 : case DepartSpeedDefinition::LAST: {
639 8177 : MSVehicle* last = getLastAnyVehicle();
640 8177 : speed = getVehicleMaxSpeed(&veh);
641 8177 : if (last != nullptr) {
642 7845 : speed = MIN2(speed, last->getSpeed());
643 7845 : patchSpeed = false;
644 : }
645 : break;
646 : }
647 4773714 : case DepartSpeedDefinition::AVG: {
648 4773714 : speed = MIN2(getVehicleMaxSpeed(&veh), getMeanSpeed());
649 4773714 : if (getLastAnyVehicle() != nullptr) {
650 4475583 : patchSpeed = false;
651 : }
652 : break;
653 : }
654 0 : case DepartSpeedDefinition::DEFAULT:
655 : default:
656 : // speed = 0 was set before
657 0 : patchSpeed = false; // @todo check
658 0 : break;
659 : }
660 13566340 : return speed;
661 : }
662 :
663 :
664 : double
665 14051065 : MSLane::getDepartPosLat(const MSVehicle& veh) {
666 14051065 : const SUMOVehicleParameter& pars = veh.getParameter();
667 14051065 : switch (pars.departPosLatProcedure) {
668 107654 : case DepartPosLatDefinition::GIVEN:
669 107654 : return pars.departPosLat;
670 : case DepartPosLatDefinition::RIGHT:
671 36054 : return -getWidth() * 0.5 + veh.getVehicleType().getWidth() * 0.5;
672 : case DepartPosLatDefinition::LEFT:
673 35843 : return getWidth() * 0.5 - veh.getVehicleType().getWidth() * 0.5;
674 : case DepartPosLatDefinition::RANDOM: {
675 235419 : const double raw = RandHelper::rand(getWidth() - veh.getVehicleType().getWidth()) - getWidth() * 0.5 + veh.getVehicleType().getWidth() * 0.5;
676 235419 : return roundDecimal(raw, gPrecisionRandom);
677 : }
678 : case DepartPosLatDefinition::CENTER:
679 : case DepartPosLatDefinition::DEFAULT:
680 : // @note:
681 : // case DepartPosLatDefinition::FREE
682 : // case DepartPosLatDefinition::RANDOM_FREE
683 : // are not handled here because they involve multiple insertion attempts
684 : default:
685 : return 0;
686 : }
687 : }
688 :
689 :
690 : bool
691 13566278 : MSLane::insertVehicle(MSVehicle& veh) {
692 : double pos = 0;
693 13566278 : bool patchSpeed = true; // whether the speed shall be adapted to infrastructure/traffic in front
694 13566278 : const SUMOVehicleParameter& pars = veh.getParameter();
695 13566278 : double speed = getDepartSpeed(veh, patchSpeed);
696 13566278 : double posLat = getDepartPosLat(veh);
697 :
698 : // determine the position
699 13566278 : switch (pars.departPosProcedure) {
700 976065 : case DepartPosDefinition::GIVEN:
701 976065 : pos = pars.departPos;
702 976065 : if (pos < 0.) {
703 146651 : pos += myLength;
704 : }
705 : break;
706 239150 : case DepartPosDefinition::RANDOM:
707 239150 : pos = roundDecimal(RandHelper::rand(getLength()), gPrecisionRandom);
708 : break;
709 : case DepartPosDefinition::RANDOM_FREE: {
710 532296 : for (int i = 0; i < 10; i++) {
711 : // we will try some random positions ...
712 : pos = RandHelper::rand(getLength());
713 484787 : posLat = getDepartPosLat(veh); // could be random as well
714 484787 : if (isInsertionSuccess(&veh, speed, pos, posLat, patchSpeed, MSMoveReminder::NOTIFICATION_DEPARTED)) {
715 2151 : MSNet::getInstance()->getInsertionControl().retractDescheduleDeparture(&veh);
716 2151 : return true;
717 : }
718 : }
719 : // ... and if that doesn't work, we put the vehicle to the free position
720 47509 : bool success = freeInsertion(veh, speed, posLat);
721 47509 : if (success) {
722 12922 : MSNet::getInstance()->getInsertionControl().retractDescheduleDeparture(&veh);
723 : }
724 : return success;
725 : }
726 254207 : case DepartPosDefinition::FREE:
727 254207 : return freeInsertion(veh, speed, posLat);
728 753736 : case DepartPosDefinition::LAST:
729 753736 : return lastInsertion(veh, speed, posLat, patchSpeed);
730 3842 : case DepartPosDefinition::STOP:
731 3842 : if (veh.hasStops() && veh.getNextStop().edge == veh.getCurrentRouteEdge()) {
732 : // getLastFreePos of stopping place could return negative position to avoid blocking the stop
733 3836 : pos = MAX2(0.0, veh.getNextStop().getEndPos(veh));
734 : break;
735 : }
736 : FALLTHROUGH;
737 : case DepartPosDefinition::BASE:
738 : case DepartPosDefinition::DEFAULT:
739 : case DepartPosDefinition::SPLIT_FRONT:
740 : default:
741 11289624 : if (pars.departProcedure == DepartDefinition::SPLIT) {
742 : pos = getLength();
743 : // find the vehicle from which we are splitting off (should only be a single lane to check)
744 : AnyVehicleIterator end = anyVehiclesEnd();
745 3 : for (AnyVehicleIterator it = anyVehiclesBegin(); it != end; ++it) {
746 24 : const MSVehicle* cand = *it;
747 24 : if (cand->isStopped() && cand->getNextStopParameter()->split == veh.getID()) {
748 21 : if (pars.departPosProcedure == DepartPosDefinition::SPLIT_FRONT) {
749 3 : pos = cand->getPositionOnLane() + cand->getVehicleType().getMinGap() + veh.getLength();
750 : } else {
751 18 : pos = cand->getBackPositionOnLane() - veh.getVehicleType().getMinGap();
752 : }
753 : break;
754 : }
755 : }
756 : } else {
757 11289603 : pos = veh.basePos(myEdge);
758 : }
759 : break;
760 : }
761 : // determine the lateral position for special cases
762 12508675 : if (MSGlobals::gLateralResolution > 0) {
763 1561258 : switch (pars.departPosLatProcedure) {
764 : case DepartPosLatDefinition::RANDOM_FREE: {
765 0 : for (int i = 0; i < 10; i++) {
766 : // we will try some random positions ...
767 0 : posLat = RandHelper::rand(getWidth()) - getWidth() * 0.5;
768 0 : if (isInsertionSuccess(&veh, speed, pos, posLat, patchSpeed, MSMoveReminder::NOTIFICATION_DEPARTED)) {
769 : return true;
770 : }
771 : }
772 : FALLTHROUGH;
773 : }
774 : // no break! continue with DepartPosLatDefinition::FREE
775 : case DepartPosLatDefinition::FREE: {
776 : // systematically test all positions until a free lateral position is found
777 5790 : double posLatMin = -getWidth() * 0.5 + veh.getVehicleType().getWidth() * 0.5;
778 5790 : double posLatMax = getWidth() * 0.5 - veh.getVehicleType().getWidth() * 0.5;
779 19291 : for (posLat = posLatMin; posLat < posLatMax; posLat += MSGlobals::gLateralResolution) {
780 17275 : if (isInsertionSuccess(&veh, speed, pos, posLat, patchSpeed, MSMoveReminder::NOTIFICATION_DEPARTED)) {
781 : return true;
782 : }
783 : }
784 : return false;
785 : }
786 : default:
787 : break;
788 : }
789 : }
790 : // try to insert
791 12502885 : const bool success = isInsertionSuccess(&veh, speed, pos, posLat, patchSpeed, MSMoveReminder::NOTIFICATION_DEPARTED);
792 : #ifdef DEBUG_EXTRAPOLATE_DEPARTPOS
793 : if (DEBUG_COND2(&veh)) {
794 : std::cout << SIMTIME << " veh=" << veh.getID() << " success=" << success << " extrapolate=" << myExtrapolateSubstepDepart << " delay=" << veh.getDepartDelay() << " speed=" << speed << "\n";
795 : }
796 : #endif
797 12502884 : if (success && myExtrapolateSubstepDepart && veh.getDepartDelay() > 0) {
798 220873 : SUMOTime relevantDelay = MIN2(DELTA_T, veh.getDepartDelay());
799 : // try to compensate sub-step depart delay by moving the vehicle forward
800 220873 : speed = veh.getSpeed(); // may have been adapted in isInsertionSuccess
801 220873 : double dist = speed * STEPS2TIME(relevantDelay);
802 220873 : std::pair<MSVehicle* const, double> leaderInfo = getLeader(&veh, pos, veh.getBestLanesContinuation());
803 220873 : if (leaderInfo.first != nullptr) {
804 : MSVehicle* leader = leaderInfo.first;
805 220615 : const double frontGapNeeded = veh.getCarFollowModel().getSecureGap(&veh, leader, speed, leader->getSpeed(),
806 : leader->getCarFollowModel().getMaxDecel());
807 220615 : dist = MIN2(dist, leaderInfo.second - frontGapNeeded);
808 : }
809 220873 : if (dist > 0) {
810 215282 : veh.executeFractionalMove(dist);
811 : }
812 : }
813 : return success;
814 : }
815 :
816 :
817 : bool
818 7722917 : MSLane::checkFailure(const MSVehicle* aVehicle, double& speed, double& dist, const double nspeed, const bool patchSpeed, const std::string errorMsg, InsertionCheck check) const {
819 7722917 : if (nspeed < speed) {
820 3335918 : if (patchSpeed) {
821 690384 : speed = MIN2(nspeed, speed);
822 690384 : dist = aVehicle->getCarFollowModel().brakeGap(speed) + aVehicle->getVehicleType().getMinGap();
823 2645534 : } else if (speed > 0) {
824 2645534 : if ((aVehicle->getInsertionChecks() & (int)check) == 0) {
825 : return false;
826 : }
827 2645494 : if (MSGlobals::gEmergencyInsert) {
828 : // Check whether vehicle can stop at the given distance when applying emergency braking
829 47 : double emergencyBrakeGap = 0.5 * speed * speed / aVehicle->getCarFollowModel().getEmergencyDecel();
830 47 : if (emergencyBrakeGap <= dist) {
831 : // Vehicle may stop in time with emergency deceleration
832 : // still, emit a warning
833 141 : WRITE_WARNINGF(TL("Vehicle '%' is inserted in an emergency situation, time=%."), aVehicle->getID(), time2string(SIMSTEP));
834 47 : return false;
835 : }
836 : }
837 :
838 2645447 : if (errorMsg != "") {
839 156 : WRITE_ERRORF(TL("Vehicle '%' will not be able to depart on lane '%' with speed % (%), time=%."),
840 : aVehicle->getID(), getID(), speed, errorMsg, time2string(SIMSTEP));
841 39 : MSNet::getInstance()->getInsertionControl().descheduleDeparture(aVehicle);
842 : }
843 2645447 : return true;
844 : }
845 : }
846 : return false;
847 : }
848 :
849 :
850 : bool
851 14108031 : MSLane::isInsertionSuccess(MSVehicle* aVehicle,
852 : double speed, double pos, double posLat, bool patchSpeed,
853 : MSMoveReminder::Notification notification) {
854 14108031 : int insertionChecks = aVehicle->getInsertionChecks();
855 14108031 : if (pos < 0 || pos > myLength) {
856 : // we may not start there
857 11202 : WRITE_WARNINGF(TL("Invalid departPos % given for vehicle '%', time=%. Inserting at lane end instead."),
858 : pos, aVehicle->getID(), time2string(SIMSTEP));
859 3734 : pos = myLength;
860 : }
861 :
862 : #ifdef DEBUG_INSERTION
863 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
864 : std::cout << "\nIS_INSERTION_SUCCESS\n"
865 : << SIMTIME << " lane=" << getID()
866 : << " veh '" << aVehicle->getID()
867 : << " bestLanes=" << toString(aVehicle->getBestLanesContinuation(this))
868 : << " pos=" << pos
869 : << " speed=" << speed
870 : << " patchSpeed=" << patchSpeed
871 : << "'\n";
872 : }
873 : #endif
874 :
875 14108031 : aVehicle->setTentativeLaneAndPosition(this, pos, posLat);
876 14108031 : aVehicle->updateBestLanes(false, this);
877 : const MSCFModel& cfModel = aVehicle->getCarFollowModel();
878 14108031 : const std::vector<MSLane*>& bestLaneConts = aVehicle->getBestLanesContinuation(this);
879 : std::vector<MSLane*>::const_iterator ri = bestLaneConts.begin();
880 14108031 : double seen = getLength() - pos; // == distance from insertion position until the end of the currentLane
881 14108031 : double dist = cfModel.brakeGap(speed) + aVehicle->getVehicleType().getMinGap();
882 14108031 : const bool isRail = aVehicle->isRail();
883 14108031 : if (isRail && insertionChecks != (int)InsertionCheck::NONE
884 72005 : && aVehicle->getParameter().departProcedure != DepartDefinition::SPLIT
885 71981 : && MSRailSignalControl::isSignalized(aVehicle->getVClass())
886 14169770 : && isRailwayOrShared(myPermissions)) {
887 61608 : const MSDriveWay* dw = MSDriveWay::getDepartureDriveway(aVehicle);
888 : MSEdgeVector occupied;
889 : #ifdef DEBUG_INSERTION
890 : gDebugFlag4 = DEBUG_COND2(aVehicle) || DEBUG_COND;
891 : #endif
892 61608 : if (dw->foeDriveWayOccupied(false, aVehicle, occupied)) {
893 52830 : setParameter("insertionBlocked:" + aVehicle->getID(), dw->getID());
894 : #ifdef DEBUG_INSERTION
895 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
896 : std::cout << " foe of driveway " + dw->getID() + " has occupied edges " + toString(occupied) << "\n";
897 : }
898 : gDebugFlag4 = false;
899 : #endif
900 : return false;
901 : }
902 : #ifdef DEBUG_INSERTION
903 : gDebugFlag4 = false;
904 : #endif
905 61608 : }
906 14055201 : if (getBidiLane() != nullptr && isRail) {
907 : // do not insert if the bidirectional edge is occupied
908 1456 : if (getBidiLane()->getVehicleNumberWithPartials() > 0 && (insertionChecks & (int)InsertionCheck::BIDI) != 0) {
909 : #ifdef DEBUG_INSERTION
910 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
911 : std::cout << " bidi-lane occupied\n";
912 : }
913 : #endif
914 : return false;
915 : }
916 : // do not insert the back of the train would be put onto an occupied bidi-lane
917 1450 : double backLength = aVehicle->getLength() - pos;
918 1450 : if (backLength > 0 && (insertionChecks & (int)InsertionCheck::BIDI) != 0) {
919 236 : MSLane* pred = getLogicalPredecessorLane();
920 236 : MSLane* bidi = pred == nullptr ? nullptr : pred->getBidiLane();
921 382 : while (backLength > 0 && bidi != nullptr) {
922 194 : if (bidi->getVehicleNumberWithPartials() > 0) {
923 : #ifdef DEBUG_INSERTION
924 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
925 : std::cout << " bidi-lane furtherLanes occupied\n";
926 : }
927 : #endif
928 : return false;
929 : }
930 146 : backLength -= bidi->getLength();
931 146 : pred = pred->getLogicalPredecessorLane();
932 146 : bidi = pred == nullptr ? nullptr : pred->getBidiLane();
933 : }
934 : }
935 : }
936 : MSLink* firstRailSignal = nullptr;
937 : double firstRailSignalDist = -1;
938 : // whether speed may be patched for unavoidable reasons (stops, speedLimits, ...)
939 14055147 : const bool patchSpeedSpecial = patchSpeed || aVehicle->getParameter().departSpeedProcedure != DepartSpeedDefinition::GIVEN;
940 :
941 : // before looping through the continuation lanes, check if a stop is scheduled on this lane
942 : // (the code is duplicated in the loop)
943 14055147 : if (aVehicle->hasStops()) {
944 443773 : const MSStop& nextStop = aVehicle->getNextStop();
945 443773 : if (nextStop.lane == this) {
946 108588 : std::stringstream msg;
947 : double distToStop, safeSpeed;
948 108588 : if (nextStop.pars.speed > 0) {
949 1630 : msg << "scheduled waypoint on lane '" << myID << "' too close";
950 1630 : distToStop = MAX2(0.0, nextStop.pars.startPos - pos);
951 1630 : safeSpeed = cfModel.freeSpeed(aVehicle, speed, distToStop, nextStop.pars.speed, true, MSCFModel::CalcReason::FUTURE);
952 : } else {
953 106958 : msg << "scheduled stop on lane '" << myID << "' too close";
954 106958 : distToStop = nextStop.pars.endPos - pos;
955 106958 : safeSpeed = cfModel.stopSpeed(aVehicle, speed, distToStop, MSCFModel::CalcReason::FUTURE);
956 : }
957 325747 : if (checkFailure(aVehicle, speed, dist, MAX2(0.0, safeSpeed), patchSpeedSpecial, msg.str(), InsertionCheck::STOP)) {
958 : // we may not drive with the given velocity - we cannot stop at the stop
959 : return false;
960 : }
961 108588 : }
962 : }
963 : // check leader vehicle first because it could have influenced the departSpeed (for departSpeed=avg)
964 : // get the pointer to the vehicle next in front of the given position
965 14055139 : const MSLeaderInfo leaders = getLastVehicleInformation(aVehicle, 0, pos);
966 : //if (aVehicle->getID() == "disabled") std::cout << " leaders=" << leaders.toString() << "\n";
967 14055139 : const double nspeed = safeInsertionSpeed(aVehicle, -pos, leaders, speed);
968 21185958 : if (nspeed == INVALID_SPEED || checkFailure(aVehicle, speed, dist, nspeed, patchSpeed, "", InsertionCheck::LEADER_GAP)) {
969 : // we may not drive with the given velocity - we crash into the leader
970 : #ifdef DEBUG_INSERTION
971 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
972 : std::cout << SIMTIME << " isInsertionSuccess lane=" << getID()
973 : << " veh=" << aVehicle->getID()
974 : << " pos=" << pos
975 : << " posLat=" << posLat
976 : << " patchSpeed=" << patchSpeed
977 : << " speed=" << speed
978 : << " nspeed=" << nspeed
979 : << " leaders=" << leaders.toString()
980 : << " failed (@700)!\n";
981 : }
982 : #endif
983 : return false;
984 : }
985 : #ifdef DEBUG_INSERTION
986 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
987 : std::cout << SIMTIME << " speed = " << speed << " nspeed = " << nspeed << " leaders=" << leaders.toString() << "\n";
988 : }
989 : #endif
990 :
991 4536933 : const MSRoute& r = aVehicle->getRoute();
992 4536933 : MSRouteIterator ce = r.begin();
993 : int nRouteSuccs = 1;
994 : MSLane* currentLane = this;
995 : MSLane* nextLane = this;
996 6028790 : SUMOTime arrivalTime = MSNet::getInstance()->getCurrentTimeStep() + TIME2STEPS(seen / MAX2(speed, SUMO_const_haltingSpeed));
997 4947498 : while ((seen < dist || (isRail && firstRailSignal == nullptr)) && ri != bestLaneConts.end()) {
998 : // get the next link used...
999 514837 : std::vector<MSLink*>::const_iterator link = succLinkSec(*aVehicle, nRouteSuccs, *currentLane, bestLaneConts);
1000 : // get the next used lane (including internal)
1001 514837 : if (currentLane->isLinkEnd(link)) {
1002 13038 : if (¤tLane->getEdge() == r.getLastEdge()) {
1003 : // reached the end of the route
1004 11268 : if (aVehicle->getParameter().arrivalSpeedProcedure == ArrivalSpeedDefinition::GIVEN) {
1005 98 : const double remaining = seen + aVehicle->getArrivalPos() - currentLane->getLength();
1006 98 : const double fspeed = cfModel.freeSpeed(aVehicle, speed, remaining, aVehicle->getParameter().arrivalSpeed, true, MSCFModel::CalcReason::FUTURE);
1007 196 : if (checkFailure(aVehicle, speed, dist, fspeed,
1008 : patchSpeedSpecial, "arrival speed too low", InsertionCheck::ARRIVAL_SPEED)) {
1009 : // we may not drive with the given velocity - we cannot match the specified arrival speed
1010 : return false;
1011 : }
1012 : }
1013 11268 : if (mayContinue(aVehicle) && hasUnsafeLink()) {
1014 : // since the route is likely to continue we must be prepared for braking
1015 1543 : if (checkFailure(aVehicle, speed, dist, cfModel.insertionStopSpeed(aVehicle, speed, seen),
1016 3086 : patchSpeedSpecial, "junction '" + currentLane->getEdge().getToJunction()->getID() + "' too close", InsertionCheck::JUNCTION)) {
1017 : // we may not drive with the given velocity - we cannot stop at the junction
1018 : return false;
1019 : }
1020 : }
1021 : } else {
1022 : // lane does not continue
1023 1770 : if (checkFailure(aVehicle, speed, dist, cfModel.insertionStopSpeed(aVehicle, speed, seen),
1024 3540 : patchSpeedSpecial, "junction '" + currentLane->getEdge().getToJunction()->getID() + "' too close", InsertionCheck::JUNCTION)) {
1025 : // we may not drive with the given velocity - we cannot stop at the junction
1026 : return false;
1027 : }
1028 : }
1029 : break;
1030 : }
1031 501799 : if (isRail && firstRailSignal == nullptr) {
1032 : std::string constraintInfo;
1033 : bool isInsertionOrder;
1034 18114 : if (MSRailSignal::hasInsertionConstraint(*link, aVehicle, constraintInfo, isInsertionOrder)) {
1035 6216 : setParameter((isInsertionOrder ? "insertionOrder" : "insertionConstraint:")
1036 9261 : + aVehicle->getID(), constraintInfo);
1037 : #ifdef DEBUG_INSERTION
1038 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1039 : std::cout << " insertion constraint at link " << (*link)->getDescription() << " not cleared \n";
1040 : }
1041 : #endif
1042 : return false;
1043 : }
1044 : }
1045 :
1046 : // might also by a regular traffic_light instead of a rail_signal
1047 498691 : if (firstRailSignal == nullptr && (*link)->getTLLogic() != nullptr) {
1048 : firstRailSignal = *link;
1049 : firstRailSignalDist = seen;
1050 : }
1051 498691 : nextLane = (*link)->getViaLaneOrLane();
1052 498691 : if (!(*link)->opened(arrivalTime, speed, speed, aVehicle->getVehicleType().getLength(), aVehicle->getImpatience(),
1053 : cfModel.getMaxDecel(), 0, posLat, nullptr, false, aVehicle)
1054 473250 : || (*link)->railSignalWasPassed()
1055 473247 : || !(*link)->havePriority()
1056 948954 : || (*link)->getState() == LINKSTATE_ZIPPER) {
1057 : // have to stop at junction
1058 50874 : std::string errorMsg = "";
1059 50874 : const LinkState state = (*link)->getState();
1060 50874 : if (state == LINKSTATE_MINOR
1061 50874 : || state == LINKSTATE_EQUAL
1062 : || state == LINKSTATE_STOP
1063 : || state == LINKSTATE_ALLWAY_STOP) {
1064 : // no sense in trying later
1065 : errorMsg = "unpriorised junction too close";
1066 27166 : } else if ((*link)->getTLLogic() != nullptr && !(*link)->getTLLogic()->getsMajorGreen((*link)->getTLIndex())) {
1067 : // traffic light never turns 'G'?
1068 19460 : errorMsg = "tlLogic '" + (*link)->getTLLogic()->getID() + "' link " + toString((*link)->getTLIndex()) + " never switches to 'G'";
1069 : }
1070 50874 : const double laneStopOffset = MAX2(getVehicleStopOffset(aVehicle),
1071 50874 : aVehicle->getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPLINE_CROSSING_GAP, MSPModel::SAFETY_GAP) - (*link)->getDistToFoePedCrossing());
1072 50874 : const double remaining = seen - laneStopOffset;
1073 101748 : if (checkFailure(aVehicle, speed, dist, cfModel.insertionStopSpeed(aVehicle, speed, remaining),
1074 : patchSpeedSpecial, errorMsg, InsertionCheck::JUNCTION)) {
1075 : // we may not drive with the given velocity - we cannot stop at the junction in time
1076 : #ifdef DEBUG_INSERTION
1077 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1078 : std::cout << SIMTIME << " isInsertionSuccess lane=" << getID()
1079 : << " veh=" << aVehicle->getID()
1080 : << " patchSpeed=" << patchSpeed
1081 : << " speed=" << speed
1082 : << " remaining=" << remaining
1083 : << " leader=" << currentLane->getLastVehicleInformation(aVehicle, 0, 0).toString()
1084 : << " last=" << Named::getIDSecure(getLastAnyVehicle())
1085 : << " meanSpeed=" << getMeanSpeed()
1086 : << " failed (@926)!\n";
1087 : }
1088 : #endif
1089 : return false;
1090 : }
1091 : #ifdef DEBUG_INSERTION
1092 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1093 : std::cout << "trying insertion before minor link: "
1094 : << "insertion speed = " << speed << " dist=" << dist
1095 : << "\n";
1096 : }
1097 : #endif
1098 37865 : if (seen >= aVehicle->getVehicleType().getMinGap()) {
1099 : break;
1100 : }
1101 447817 : } else if (nextLane->isInternal()) {
1102 246451 : double tmp = 0;
1103 246451 : bool dummyReq = true;
1104 : #ifdef DEBUG_INSERTION
1105 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1106 : std::cout << "checking linkLeader for lane '" << nextLane->getID() << "'\n";
1107 : gDebugFlag1 = true;
1108 : }
1109 : #endif
1110 246451 : double nSpeed = speed;
1111 246451 : aVehicle->checkLinkLeader(nextLane->getLinkCont()[0], nextLane, seen + nextLane->getLength(), nullptr, nSpeed, tmp, tmp, dummyReq);
1112 : #ifdef DEBUG_INSERTION
1113 : gDebugFlag1 = false;
1114 : #endif
1115 492902 : if (checkFailure(aVehicle, speed, dist, nSpeed, patchSpeed, "", InsertionCheck::LEADER_GAP)) {
1116 : // we may not drive with the given velocity - there is a junction leader
1117 : #ifdef DEBUG_INSERTION
1118 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1119 : std::cout << " linkLeader nSpeed=" << nSpeed << " failed (@1058)!\n";
1120 : }
1121 : #endif
1122 2907 : return false;
1123 : }
1124 : }
1125 : // check how next lane affects the journey
1126 454835 : if (nextLane != nullptr) {
1127 :
1128 : // do not insert if the bidirectional edge is occupied before a railSignal has been encountered
1129 454835 : if (firstRailSignal == nullptr && nextLane->getBidiLane() != nullptr && nextLane->getBidiLane()->getVehicleNumberWithPartials() > 0) {
1130 0 : if ((insertionChecks & (int)InsertionCheck::ONCOMING_TRAIN) != 0) {
1131 : #ifdef DEBUG_INSERTION
1132 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1133 : std::cout << " nextLane=" << nextLane->getID() << " occupiedBidi\n";
1134 : }
1135 : #endif
1136 44269 : return false;
1137 : }
1138 : }
1139 :
1140 : // check if there are stops on the next lane that should be regarded
1141 : // (this block is duplicated before the loop to deal with the insertion lane)
1142 454835 : if (aVehicle->hasStops()) {
1143 17344 : const MSStop& nextStop = aVehicle->getNextStop();
1144 17344 : if (nextStop.lane == nextLane) {
1145 538 : std::stringstream msg;
1146 538 : msg << "scheduled stop on lane '" << nextStop.lane->getID() << "' too close";
1147 538 : const double distToStop = seen + nextStop.pars.endPos;
1148 538 : if (checkFailure(aVehicle, speed, dist, cfModel.insertionStopSpeed(aVehicle, speed, distToStop),
1149 538 : patchSpeedSpecial, msg.str(), InsertionCheck::STOP)) {
1150 : // we may not drive with the given velocity - we cannot stop at the stop
1151 : return false;
1152 : }
1153 538 : }
1154 : }
1155 :
1156 : // check leader on next lane
1157 454835 : const MSLeaderInfo nextLeaders = nextLane->getLastVehicleInformation(aVehicle, 0, 0);
1158 454835 : if (nextLeaders.hasVehicles()) {
1159 158401 : const double nextLaneSpeed = nextLane->safeInsertionSpeed(aVehicle, seen, nextLeaders, speed);
1160 : #ifdef DEBUG_INSERTION
1161 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1162 : std::cout << SIMTIME << " leader on lane '" << nextLane->getID() << "': " << nextLeaders.toString() << " nspeed=" << nextLaneSpeed << "\n";
1163 : }
1164 : #endif
1165 308367 : if (nextLaneSpeed == INVALID_SPEED || checkFailure(aVehicle, speed, dist, nextLaneSpeed, patchSpeed, "", InsertionCheck::LEADER_GAP)) {
1166 : // we may not drive with the given velocity - we crash into the leader
1167 : #ifdef DEBUG_INSERTION
1168 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1169 : std::cout << " isInsertionSuccess lane=" << getID()
1170 : << " veh=" << aVehicle->getID()
1171 : << " pos=" << pos
1172 : << " posLat=" << posLat
1173 : << " patchSpeed=" << patchSpeed
1174 : << " speed=" << speed
1175 : << " nspeed=" << nextLaneSpeed
1176 : << " nextLane=" << nextLane->getID()
1177 : << " lead=" << nextLeaders.toString()
1178 : << " failed (@641)!\n";
1179 : }
1180 : #endif
1181 : return false;
1182 : }
1183 : }
1184 413069 : if (!nextLane->checkForPedestrians(aVehicle, speed, dist, -seen, patchSpeed)) {
1185 : return false;
1186 : }
1187 : // check next lane's maximum velocity
1188 413045 : const double freeSpeed = cfModel.freeSpeed(aVehicle, speed, seen, nextLane->getVehicleMaxSpeed(aVehicle), true, MSCFModel::CalcReason::FUTURE);
1189 413044 : if (freeSpeed < speed) {
1190 59852 : if (patchSpeedSpecial) {
1191 59442 : speed = freeSpeed;
1192 59442 : dist = cfModel.brakeGap(speed) + aVehicle->getVehicleType().getMinGap();
1193 : } else {
1194 410 : if ((insertionChecks & (int)InsertionCheck::SPEED_LIMIT) != 0) {
1195 410 : if (!MSGlobals::gCheckRoutes) {
1196 15 : WRITE_WARNINGF(TL("Vehicle '%' is inserted too fast and will violate the speed limit on a lane '%', time=%."),
1197 : aVehicle->getID(), nextLane->getID(), time2string(SIMSTEP));
1198 : } else {
1199 : // we may not drive with the given velocity - we would be too fast on the next lane
1200 1215 : WRITE_ERRORF(TL("Vehicle '%' will not be able to depart using the given velocity (slow lane ahead), time=%."), aVehicle->getID(), time2string(SIMSTEP));
1201 405 : MSNet::getInstance()->getInsertionControl().descheduleDeparture(aVehicle);
1202 : return false;
1203 : }
1204 : }
1205 : }
1206 : }
1207 : // check traffic on next junction
1208 : // we cannot use (*link)->opened because a vehicle without priority
1209 : // may already be comitted to blocking the link and unable to stop
1210 412639 : const SUMOTime leaveTime = (*link)->getLeaveTime(arrivalTime, speed, speed, aVehicle->getVehicleType().getLength());
1211 412639 : if ((*link)->hasApproachingFoe(arrivalTime, leaveTime, speed, cfModel.getMaxDecel())) {
1212 18814 : if (checkFailure(aVehicle, speed, dist, cfModel.insertionStopSpeed(aVehicle, speed, seen), patchSpeed, "", InsertionCheck::JUNCTION)) {
1213 : // we may not drive with the given velocity - we crash at the junction
1214 : return false;
1215 : }
1216 : }
1217 446705 : arrivalTime += TIME2STEPS(nextLane->getLength() / MAX2(speed, NUMERICAL_EPS));
1218 410565 : seen += nextLane->getLength();
1219 : currentLane = nextLane;
1220 410565 : if ((*link)->getViaLane() == nullptr) {
1221 190504 : nRouteSuccs++;
1222 : ++ce;
1223 : ++ri;
1224 : }
1225 454835 : }
1226 : }
1227 :
1228 4473631 : const MSLeaderDistanceInfo& followers = getFollowersOnConsecutive(aVehicle, aVehicle->getBackPositionOnLane(), false);
1229 9763992 : for (int i = 0; i < followers.numSublanes(); ++i) {
1230 6267264 : const MSVehicle* follower = followers[i].first;
1231 6267264 : if (follower != nullptr) {
1232 1278697 : const double backGapNeeded = follower->getCarFollowModel().getSecureGap(follower, aVehicle, follower->getSpeed(), speed, cfModel.getMaxDecel());
1233 1278697 : if (followers[i].second < backGapNeeded
1234 1278697 : && ((insertionChecks & (int)InsertionCheck::FOLLOWER_GAP) != 0
1235 80 : || (followers[i].second < 0 && (insertionChecks & (int)InsertionCheck::COLLISION) != 0))) {
1236 : // too close to the follower on this lane
1237 : #ifdef DEBUG_INSERTION
1238 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1239 : std::cout << SIMTIME << " isInsertionSuccess lane=" << getID()
1240 : << " veh=" << aVehicle->getID()
1241 : << " pos=" << pos
1242 : << " posLat=" << posLat
1243 : << " speed=" << speed
1244 : << " nspeed=" << nspeed
1245 : << " follower=" << follower->getID()
1246 : << " backGapNeeded=" << backGapNeeded
1247 : << " gap=" << followers[i].second
1248 : << " failure (@719)!\n";
1249 : }
1250 : #endif
1251 976903 : return false;
1252 : }
1253 : }
1254 : }
1255 :
1256 3496728 : if (!checkForPedestrians(aVehicle, speed, dist, pos, patchSpeed)) {
1257 : return false;
1258 : }
1259 :
1260 3496222 : MSLane* shadowLane = aVehicle->getLaneChangeModel().getShadowLane(this);
1261 : #ifdef DEBUG_INSERTION
1262 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1263 : std::cout << " shadowLane=" << Named::getIDSecure(shadowLane) << "\n";
1264 : }
1265 : #endif
1266 3496222 : if (shadowLane != nullptr) {
1267 874 : const MSLeaderDistanceInfo& shadowFollowers = shadowLane->getFollowersOnConsecutive(aVehicle, aVehicle->getBackPositionOnLane(), false);
1268 3824 : for (int i = 0; i < shadowFollowers.numSublanes(); ++i) {
1269 2967 : const MSVehicle* follower = shadowFollowers[i].first;
1270 2967 : if (follower != nullptr) {
1271 24 : const double backGapNeeded = follower->getCarFollowModel().getSecureGap(follower, aVehicle, follower->getSpeed(), speed, cfModel.getMaxDecel());
1272 24 : if (shadowFollowers[i].second < backGapNeeded
1273 24 : && ((insertionChecks & (int)InsertionCheck::FOLLOWER_GAP) != 0
1274 0 : || (shadowFollowers[i].second < 0 && (insertionChecks & (int)InsertionCheck::COLLISION) != 0))) {
1275 : // too close to the follower on this lane
1276 : #ifdef DEBUG_INSERTION
1277 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1278 : std::cout << SIMTIME
1279 : << " isInsertionSuccess shadowlane=" << shadowLane->getID()
1280 : << " veh=" << aVehicle->getID()
1281 : << " pos=" << pos
1282 : << " posLat=" << posLat
1283 : << " speed=" << speed
1284 : << " nspeed=" << nspeed
1285 : << " follower=" << follower->getID()
1286 : << " backGapNeeded=" << backGapNeeded
1287 : << " gap=" << shadowFollowers[i].second
1288 : << " failure (@812)!\n";
1289 : }
1290 : #endif
1291 17 : return false;
1292 : }
1293 : }
1294 : }
1295 857 : const MSLeaderInfo& ahead = shadowLane->getLastVehicleInformation(nullptr, 0, aVehicle->getPositionOnLane(), false);
1296 2843 : for (int i = 0; i < ahead.numSublanes(); ++i) {
1297 2080 : const MSVehicle* veh = ahead[i];
1298 2080 : if (veh != nullptr) {
1299 343 : const double gap = veh->getBackPositionOnLane(shadowLane) - aVehicle->getPositionOnLane() - aVehicle->getVehicleType().getMinGap();
1300 343 : const double gapNeeded = aVehicle->getCarFollowModel().getSecureGap(aVehicle, veh, speed, veh->getSpeed(), veh->getCarFollowModel().getMaxDecel());
1301 343 : if (gap < gapNeeded
1302 94 : && ((insertionChecks & (int)InsertionCheck::LEADER_GAP) != 0
1303 0 : || (gap < 0 && (insertionChecks & (int)InsertionCheck::COLLISION) != 0))) {
1304 : // too close to the shadow leader
1305 : #ifdef DEBUG_INSERTION
1306 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1307 : std::cout << SIMTIME
1308 : << " isInsertionSuccess shadowlane=" << shadowLane->getID()
1309 : << " veh=" << aVehicle->getID()
1310 : << " pos=" << pos
1311 : << " posLat=" << posLat
1312 : << " speed=" << speed
1313 : << " nspeed=" << nspeed
1314 : << " leader=" << veh->getID()
1315 : << " gapNeeded=" << gapNeeded
1316 : << " gap=" << gap
1317 : << " failure (@842)!\n";
1318 : }
1319 : #endif
1320 : return false;
1321 : }
1322 : }
1323 : }
1324 874 : }
1325 3496111 : if (followers.numFreeSublanes() > 0) {
1326 : // check approaching vehicles to prevent rear-end collisions
1327 3317044 : const double backOffset = pos - aVehicle->getVehicleType().getLength();
1328 3317044 : const double missingRearGap = getMissingRearGap(aVehicle, backOffset, speed);
1329 3317044 : if (missingRearGap > 0
1330 0 : && (insertionChecks & (int)InsertionCheck::LEADER_GAP) != 0) {
1331 : // too close to a follower
1332 : #ifdef DEBUG_INSERTION
1333 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1334 : std::cout << SIMTIME
1335 : << " isInsertionSuccess lane=" << getID()
1336 : << " veh=" << aVehicle->getID()
1337 : << " pos=" << pos
1338 : << " posLat=" << posLat
1339 : << " speed=" << speed
1340 : << " nspeed=" << nspeed
1341 : << " missingRearGap=" << missingRearGap
1342 : << " failure (@728)!\n";
1343 : }
1344 : #endif
1345 : return false;
1346 : }
1347 : }
1348 3496111 : if (insertionChecks == (int)InsertionCheck::NONE) {
1349 2332 : speed = MAX2(0.0, speed);
1350 : }
1351 : // may got negative while adaptation
1352 3496111 : if (speed < 0) {
1353 : #ifdef DEBUG_INSERTION
1354 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1355 : std::cout << SIMTIME
1356 : << " isInsertionSuccess lane=" << getID()
1357 : << " veh=" << aVehicle->getID()
1358 : << " pos=" << pos
1359 : << " posLat=" << posLat
1360 : << " speed=" << speed
1361 : << " nspeed=" << nspeed
1362 : << " failed (@733)!\n";
1363 : }
1364 : #endif
1365 : return false;
1366 : }
1367 3496111 : const int bestLaneOffset = aVehicle->getBestLaneOffset();
1368 3496111 : const double extraReservation = aVehicle->getLaneChangeModel().getExtraReservation(bestLaneOffset);
1369 3496111 : if (extraReservation > 0) {
1370 23262 : std::stringstream msg;
1371 23262 : msg << "too many lane changes required on lane '" << myID << "'";
1372 : // we need to take into acount one extra actionStep of delay due to #3665
1373 23262 : double distToStop = aVehicle->getBestLaneDist() - pos - extraReservation - speed * aVehicle->getActionStepLengthSecs();
1374 23262 : if (distToStop >= 0) {
1375 : double stopSpeed = cfModel.stopSpeed(aVehicle, speed, distToStop, MSCFModel::CalcReason::FUTURE);
1376 : #ifdef DEBUG_INSERTION
1377 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1378 : std::cout << "\nIS_INSERTION_SUCCESS\n"
1379 : << SIMTIME << " veh=" << aVehicle->getID() << " bestLaneOffset=" << bestLaneOffset << " bestLaneDist=" << aVehicle->getBestLaneDist() << " extraReservation=" << extraReservation
1380 : << " distToStop=" << distToStop << " v=" << speed << " v2=" << stopSpeed << "\n";
1381 : }
1382 : #endif
1383 44748 : if (checkFailure(aVehicle, speed, distToStop, MAX2(0.0, stopSpeed),
1384 22378 : patchSpeedSpecial, msg.str(), InsertionCheck::LANECHANGE)) {
1385 : // we may not drive with the given velocity - we cannot reserve enough space for lane changing
1386 : return false;
1387 : }
1388 : }
1389 23262 : }
1390 : // enter
1391 3496101 : incorporateVehicle(aVehicle, pos, speed, posLat, find_if(myVehicles.begin(), myVehicles.end(), [&](MSVehicle * const v) {
1392 2943512 : return v->getPositionOnLane() >= pos;
1393 : }), notification);
1394 : #ifdef DEBUG_INSERTION
1395 : if (DEBUG_COND2(aVehicle) || DEBUG_COND) {
1396 : std::cout << SIMTIME
1397 : << " isInsertionSuccess lane=" << getID()
1398 : << " veh=" << aVehicle->getID()
1399 : << " pos=" << pos
1400 : << " posLat=" << posLat
1401 : << " speed=" << speed
1402 : << " nspeed=" << nspeed
1403 : << "\n myVehicles=" << toString(myVehicles)
1404 : << " myPartial=" << toString(myPartialVehicles)
1405 : << " myManeuverReservations=" << toString(myManeuverReservations)
1406 : << "\n success!\n";
1407 : }
1408 : #endif
1409 3496101 : if (isRail) {
1410 5758 : unsetParameter("insertionConstraint:" + aVehicle->getID());
1411 5758 : unsetParameter("insertionOrder:" + aVehicle->getID());
1412 5758 : unsetParameter("insertionBlocked:" + aVehicle->getID());
1413 : // rail_signal (not traffic_light) requires approach information for
1414 : // switching correctly at the start of the next simulation step
1415 5758 : if (firstRailSignal != nullptr && firstRailSignal->getJunction()->getType() == SumoXMLNodeType::RAIL_SIGNAL) {
1416 1854 : aVehicle->registerInsertionApproach(firstRailSignal, firstRailSignalDist);
1417 : }
1418 : }
1419 : return true;
1420 14055139 : }
1421 :
1422 :
1423 : void
1424 48456 : MSLane::forceVehicleInsertion(MSVehicle* veh, double pos, MSMoveReminder::Notification notification, double posLat) {
1425 48456 : veh->updateBestLanes(true, this);
1426 : bool dummy;
1427 48456 : const double speed = veh->hasDeparted() ? veh->getSpeed() : getDepartSpeed(*veh, dummy);
1428 48456 : incorporateVehicle(veh, pos, speed, posLat, find_if(myVehicles.begin(), myVehicles.end(), [&](MSVehicle * const v) {
1429 126195 : return v->getPositionOnLane() >= pos;
1430 : }), notification);
1431 48456 : }
1432 :
1433 :
1434 : double
1435 14213540 : MSLane::safeInsertionSpeed(const MSVehicle* veh, double seen, const MSLeaderInfo& leaders, double speed) {
1436 : double nspeed = speed;
1437 : #ifdef DEBUG_INSERTION
1438 : if (DEBUG_COND2(veh)) {
1439 : std::cout << SIMTIME << " safeInsertionSpeed veh=" << veh->getID() << " speed=" << speed << "\n";
1440 : }
1441 : #endif
1442 25024158 : for (int i = 0; i < leaders.numSublanes(); ++i) {
1443 17743373 : const MSVehicle* leader = leaders[i];
1444 17743373 : if (leader != nullptr) {
1445 15483961 : double gap = leader->getBackPositionOnLane(this) + seen - veh->getVehicleType().getMinGap();
1446 15483961 : if (leader->getLane() == getBidiLane()) {
1447 : // use distance to front position and account for movement
1448 1273 : gap -= (leader->getLength() + leader->getBrakeGap(true));
1449 : }
1450 15483961 : if (gap < 0) {
1451 : #ifdef DEBUG_INSERTION
1452 : if (DEBUG_COND2(veh)) {
1453 : std::cout << " leader=" << leader->getID() << " bPos=" << leader->getBackPositionOnLane(this) << " gap=" << gap << "\n";
1454 : }
1455 : #endif
1456 6932755 : if ((veh->getInsertionChecks() & (int)InsertionCheck::COLLISION) != 0) {
1457 : return INVALID_SPEED;
1458 : } else {
1459 0 : return 0;
1460 : }
1461 : }
1462 8551206 : nspeed = MIN2(nspeed,
1463 8551206 : veh->getCarFollowModel().insertionFollowSpeed(veh, speed, gap, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel(), leader));
1464 : #ifdef DEBUG_INSERTION
1465 : if (DEBUG_COND2(veh)) {
1466 : std::cout << " leader=" << leader->getID() << " bPos=" << leader->getBackPositionOnLane(this) << " gap=" << gap << " nspeed=" << nspeed << "\n";
1467 : }
1468 : #endif
1469 : }
1470 : }
1471 : return nspeed;
1472 : }
1473 :
1474 :
1475 : // ------ Handling vehicles lapping into lanes ------
1476 : const MSLeaderInfo
1477 748469885 : MSLane::getLastVehicleInformation(const MSVehicle* ego, double latOffset, double minPos, bool allowCached) const {
1478 748469885 : if (myLeaderInfoTime < MSNet::getInstance()->getCurrentTimeStep() || ego != nullptr || minPos > 0 || !allowCached) {
1479 265846919 : MSLeaderInfo leaderTmp(myWidth, ego, latOffset);
1480 : AnyVehicleIterator last = anyVehiclesBegin();
1481 : int freeSublanes = 1; // number of sublanes for which no leader was found
1482 : //if (ego->getID() == "disabled" && SIMTIME == 58) {
1483 : // std::cout << "DEBUG\n";
1484 : //}
1485 265846919 : const MSVehicle* veh = *last;
1486 1918263478 : while (freeSublanes > 0 && veh != nullptr) {
1487 : #ifdef DEBUG_PLAN_MOVE
1488 : if (DEBUG_COND2(ego) || DEBUG_COND) {
1489 : gDebugFlag1 = true;
1490 : std::cout << " getLastVehicleInformation lane=" << getID() << " minPos=" << minPos << " veh=" << veh->getID() << " pos=" << veh->getPositionOnLane(this) << "\n";
1491 : }
1492 : #endif
1493 3302366822 : if (veh != ego && MAX2(0.0, veh->getPositionOnLane(this)) >= minPos) {
1494 270291806 : const double vehLatOffset = veh->getLatOffset(this);
1495 270291806 : freeSublanes = leaderTmp.addLeader(veh, true, vehLatOffset);
1496 : #ifdef DEBUG_PLAN_MOVE
1497 : if (DEBUG_COND2(ego) || DEBUG_COND) {
1498 : std::cout << " latOffset=" << vehLatOffset << " newLeaders=" << leaderTmp.toString() << "\n";
1499 : }
1500 : #endif
1501 : }
1502 1652416559 : veh = *(++last);
1503 : }
1504 265846919 : if (ego == nullptr && minPos == 0) {
1505 : #ifdef HAVE_FOX
1506 121359523 : ScopedLocker<> lock(myLeaderInfoMutex, MSGlobals::gNumSimThreads > 1);
1507 : #endif
1508 : // update cached value
1509 : myLeaderInfo = leaderTmp;
1510 121359523 : myLeaderInfoTime = MSNet::getInstance()->getCurrentTimeStep();
1511 : }
1512 : #ifdef DEBUG_PLAN_MOVE
1513 : //if (DEBUG_COND2(ego)) std::cout << SIMTIME
1514 : // << " getLastVehicleInformation lane=" << getID()
1515 : // << " ego=" << Named::getIDSecure(ego)
1516 : // << "\n"
1517 : // << " vehicles=" << toString(myVehicles)
1518 : // << " partials=" << toString(myPartialVehicles)
1519 : // << "\n"
1520 : // << " result=" << leaderTmp.toString()
1521 : // << " cached=" << myLeaderInfo.toString()
1522 : // << " myLeaderInfoTime=" << myLeaderInfoTime
1523 : // << "\n";
1524 : gDebugFlag1 = false;
1525 : #endif
1526 : return leaderTmp;
1527 265846919 : }
1528 : return myLeaderInfo;
1529 : }
1530 :
1531 :
1532 : const MSLeaderInfo
1533 393718128 : MSLane::getFirstVehicleInformation(const MSVehicle* ego, double latOffset, bool onlyFrontOnLane, double maxPos, bool allowCached) const {
1534 : #ifdef HAVE_FOX
1535 393718128 : ScopedLocker<> lock(myFollowerInfoMutex, MSGlobals::gNumSimThreads > 1);
1536 : #endif
1537 393718128 : if (myFollowerInfoTime < MSNet::getInstance()->getCurrentTimeStep() || ego != nullptr || maxPos < myLength || !allowCached || onlyFrontOnLane) {
1538 : // XXX separate cache for onlyFrontOnLane = true
1539 393718128 : MSLeaderInfo followerTmp(myWidth, ego, latOffset);
1540 : AnyVehicleIterator first = anyVehiclesUpstreamBegin();
1541 : int freeSublanes = 1; // number of sublanes for which no leader was found
1542 393718128 : const MSVehicle* veh = *first;
1543 657417653 : while (freeSublanes > 0 && veh != nullptr) {
1544 : #ifdef DEBUG_PLAN_MOVE
1545 : if (DEBUG_COND2(ego)) {
1546 : std::cout << " veh=" << veh->getID() << " pos=" << veh->getPositionOnLane(this) << " maxPos=" << maxPos << "\n";
1547 : }
1548 : #endif
1549 263699525 : if (veh != ego && veh->getPositionOnLane(this) <= maxPos
1550 527399050 : && (!onlyFrontOnLane || veh->isFrontOnLane(this))) {
1551 : //const double vehLatOffset = veh->getLane()->getRightSideOnEdge() - getRightSideOnEdge();
1552 241040026 : const double vehLatOffset = veh->getLatOffset(this);
1553 : #ifdef DEBUG_PLAN_MOVE
1554 : if (DEBUG_COND2(ego)) {
1555 : std::cout << " veh=" << veh->getID() << " latOffset=" << vehLatOffset << "\n";
1556 : }
1557 : #endif
1558 241040026 : freeSublanes = followerTmp.addLeader(veh, true, vehLatOffset);
1559 : }
1560 263699525 : veh = *(++first);
1561 : }
1562 393718128 : if (ego == nullptr && maxPos == std::numeric_limits<double>::max()) {
1563 : // update cached value
1564 : myFollowerInfo = followerTmp;
1565 393718128 : myFollowerInfoTime = MSNet::getInstance()->getCurrentTimeStep();
1566 : }
1567 : #ifdef DEBUG_PLAN_MOVE
1568 : //if (DEBUG_COND2(ego)) std::cout << SIMTIME
1569 : // << " getFirstVehicleInformation lane=" << getID()
1570 : // << " ego=" << Named::getIDSecure(ego)
1571 : // << "\n"
1572 : // << " vehicles=" << toString(myVehicles)
1573 : // << " partials=" << toString(myPartialVehicles)
1574 : // << "\n"
1575 : // << " result=" << followerTmp.toString()
1576 : // //<< " cached=" << myFollowerInfo.toString()
1577 : // << " myLeaderInfoTime=" << myLeaderInfoTime
1578 : // << "\n";
1579 : #endif
1580 : return followerTmp;
1581 393718128 : }
1582 : return myFollowerInfo;
1583 : }
1584 :
1585 :
1586 : // ------ ------
1587 : void
1588 96771953 : MSLane::planMovements(SUMOTime t) {
1589 : assert(myVehicles.size() != 0);
1590 : double cumulatedVehLength = 0.;
1591 96771953 : MSLeaderInfo leaders(myWidth);
1592 :
1593 : // iterate over myVehicles, myPartialVehicles, and myManeuverReservations merge-sort style
1594 : VehCont::reverse_iterator veh = myVehicles.rbegin();
1595 : VehCont::reverse_iterator vehPart = myPartialVehicles.rbegin();
1596 : VehCont::reverse_iterator vehRes = myManeuverReservations.rbegin();
1597 : #ifdef DEBUG_PLAN_MOVE
1598 : if (DEBUG_COND) std::cout
1599 : << "\n"
1600 : << SIMTIME
1601 : << " planMovements() lane=" << getID()
1602 : << "\n vehicles=" << toString(myVehicles)
1603 : << "\n partials=" << toString(myPartialVehicles)
1604 : << "\n reservations=" << toString(myManeuverReservations)
1605 : << "\n";
1606 : #endif
1607 : assert(MSGlobals::gLateralResolution || myManeuverReservations.size() == 0);
1608 801344380 : for (; veh != myVehicles.rend(); ++veh) {
1609 : #ifdef DEBUG_PLAN_MOVE
1610 : if (DEBUG_COND2((*veh))) {
1611 : std::cout << " plan move for: " << (*veh)->getID();
1612 : }
1613 : #endif
1614 704572427 : updateLeaderInfo(*veh, vehPart, vehRes, leaders); // 36ns with 8 threads, 9ns with 1
1615 : #ifdef DEBUG_PLAN_MOVE
1616 : if (DEBUG_COND2((*veh))) {
1617 : std::cout << " leaders=" << leaders.toString() << "\n";
1618 : }
1619 : #endif
1620 704572427 : (*veh)->planMove(t, leaders, cumulatedVehLength); // 4800ns with 8 threads, 3100 with 1
1621 704572427 : cumulatedVehLength += (*veh)->getVehicleType().getLengthWithGap();
1622 704572427 : leaders.addLeader(*veh, false, 0);
1623 : }
1624 96771953 : }
1625 :
1626 :
1627 : void
1628 96771953 : MSLane::setJunctionApproaches() const {
1629 801344380 : for (MSVehicle* const veh : myVehicles) {
1630 704572427 : veh->setApproachingForAllLinks();
1631 : }
1632 96771953 : }
1633 :
1634 :
1635 : void
1636 704572427 : MSLane::updateLeaderInfo(const MSVehicle* veh, VehCont::reverse_iterator& vehPart, VehCont::reverse_iterator& vehRes, MSLeaderInfo& ahead) const {
1637 : bool morePartialVehsAhead = vehPart != myPartialVehicles.rend();
1638 : bool moreReservationsAhead = vehRes != myManeuverReservations.rend();
1639 : bool nextToConsiderIsPartial;
1640 :
1641 : // Determine relevant leaders for veh
1642 715313997 : while (moreReservationsAhead || morePartialVehsAhead) {
1643 1259112 : if ((!moreReservationsAhead || (*vehRes)->getPositionOnLane(this) <= veh->getPositionOnLane())
1644 20904059 : && (!morePartialVehsAhead || (*vehPart)->getPositionOnLane(this) <= veh->getPositionOnLane())) {
1645 : // All relevant downstream vehicles have been collected.
1646 : break;
1647 : }
1648 :
1649 : // Check whether next farthest relevant vehicle downstream is a partial vehicle or a maneuver reservation
1650 10741570 : if (moreReservationsAhead && !morePartialVehsAhead) {
1651 : nextToConsiderIsPartial = false;
1652 10645916 : } else if (morePartialVehsAhead && !moreReservationsAhead) {
1653 : nextToConsiderIsPartial = true;
1654 : } else {
1655 : assert(morePartialVehsAhead && moreReservationsAhead);
1656 : // Add farthest downstream vehicle first
1657 162362 : nextToConsiderIsPartial = (*vehPart)->getPositionOnLane(this) > (*vehRes)->getPositionOnLane(this);
1658 : }
1659 : // Add appropriate leader information
1660 162362 : if (nextToConsiderIsPartial) {
1661 10597194 : const double latOffset = (*vehPart)->getLatOffset(this);
1662 : #ifdef DEBUG_PLAN_MOVE
1663 : if (DEBUG_COND) {
1664 : std::cout << " partial ahead: " << (*vehPart)->getID() << " latOffset=" << latOffset << "\n";
1665 : }
1666 : #endif
1667 10664289 : if (!(MSGlobals::gLaneChangeDuration > 0 && (*vehPart)->getLaneChangeModel().isOpposite()
1668 67095 : && !(*vehPart)->getLaneChangeModel().isChangingLanes())) {
1669 10547431 : ahead.addLeader(*vehPart, false, latOffset);
1670 : }
1671 : ++vehPart;
1672 : morePartialVehsAhead = vehPart != myPartialVehicles.rend();
1673 : } else {
1674 144376 : const double latOffset = (*vehRes)->getLatOffset(this);
1675 : #ifdef DEBUG_PLAN_MOVE
1676 : if (DEBUG_COND) {
1677 : std::cout << " reservation ahead: " << (*vehRes)->getID() << " latOffset=" << latOffset << "\n";
1678 : }
1679 : #endif
1680 144376 : ahead.addLeader(*vehRes, false, latOffset);
1681 : ++vehRes;
1682 : moreReservationsAhead = vehRes != myManeuverReservations.rend();
1683 : }
1684 : }
1685 704572427 : }
1686 :
1687 :
1688 : void
1689 105012133 : MSLane::detectCollisions(SUMOTime timestep, const std::string& stage) {
1690 105012133 : myNeedsCollisionCheck = false;
1691 : #ifdef DEBUG_COLLISIONS
1692 : if (DEBUG_COND) {
1693 : std::vector<const MSVehicle*> all;
1694 : for (AnyVehicleIterator last = anyVehiclesBegin(); last != anyVehiclesEnd(); ++last) {
1695 : all.push_back(*last);
1696 : }
1697 : std::cout << SIMTIME << " detectCollisions stage=" << stage << " lane=" << getID() << ":\n"
1698 : << " vehs=" << toString(myVehicles) << "\n"
1699 : << " part=" << toString(myPartialVehicles) << "\n"
1700 : << " all=" << toString(all) << "\n"
1701 : << "\n";
1702 : }
1703 : #endif
1704 :
1705 105012133 : if (myCollisionAction == COLLISION_ACTION_NONE) {
1706 943157 : return;
1707 : }
1708 :
1709 : std::set<const MSVehicle*, ComparatorNumericalIdLess> toRemove;
1710 : std::set<const MSVehicle*, ComparatorNumericalIdLess> toTeleport;
1711 104999265 : if (mustCheckJunctionCollisions()) {
1712 1464525 : myNeedsCollisionCheck = true; // always check
1713 : #ifdef DEBUG_JUNCTION_COLLISIONS
1714 : if (DEBUG_COND) {
1715 : std::cout << SIMTIME << " detect junction Collisions stage=" << stage << " lane=" << getID() << ":\n"
1716 : << " vehs=" << toString(myVehicles) << "\n"
1717 : << " part=" << toString(myPartialVehicles) << "\n"
1718 : << "\n";
1719 : }
1720 : #endif
1721 : assert(myLinks.size() == 1);
1722 1464525 : const std::vector<const MSLane*>& foeLanes = myLinks.front()->getFoeLanes();
1723 : // save the iterator, it might get modified, see #8842
1724 : MSLane::AnyVehicleIterator end = anyVehiclesEnd();
1725 2589318 : for (AnyVehicleIterator veh = anyVehiclesBegin(); veh != end; ++veh) {
1726 2589318 : const MSVehicle* const collider = *veh;
1727 : //std::cout << " collider " << collider->getID() << "\n";
1728 2589318 : PositionVector colliderBoundary = collider->getBoundingBox(myCheckJunctionCollisionMinGap);
1729 35074592 : for (const MSLane* const foeLane : foeLanes) {
1730 : #ifdef DEBUG_JUNCTION_COLLISIONS
1731 : if (DEBUG_COND) {
1732 : std::cout << " foeLane " << foeLane->getID()
1733 : << " foeVehs=" << toString(foeLane->myVehicles)
1734 : << " foePart=" << toString(foeLane->myPartialVehicles) << "\n";
1735 : }
1736 : #endif
1737 : MSLane::AnyVehicleIterator foeEnd = foeLane->anyVehiclesEnd();
1738 4340444 : for (MSLane::AnyVehicleIterator it_veh = foeLane->anyVehiclesBegin(); it_veh != foeEnd; ++it_veh) {
1739 4340444 : const MSVehicle* const victim = *it_veh;
1740 4340444 : if (victim == collider) {
1741 : // may happen if the vehicles lane and shadow lane are siblings
1742 37518 : continue;
1743 : }
1744 : #ifdef DEBUG_JUNCTION_COLLISIONS
1745 : if (DEBUG_COND && DEBUG_COND2(collider)) {
1746 : std::cout << SIMTIME << " foe=" << victim->getID()
1747 : << " bound=" << colliderBoundary << " foeBound=" << victim->getBoundingBox()
1748 : << " overlaps=" << colliderBoundary.overlapsWith(victim->getBoundingBox())
1749 : << " poly=" << collider->getBoundingPoly()
1750 : << " foePoly=" << victim->getBoundingPoly()
1751 : << " overlaps2=" << collider->getBoundingPoly().overlapsWith(victim->getBoundingPoly())
1752 : << "\n";
1753 : }
1754 : #endif
1755 4302926 : if (MSGlobals::gIgnoreJunctionBlocker < std::numeric_limits<SUMOTime>::max()) {
1756 7280 : if (collider->getWaitingTime() >= MSGlobals::gIgnoreJunctionBlocker
1757 7280 : || victim->getWaitingTime() >= MSGlobals::gIgnoreJunctionBlocker) {
1758 : // ignored vehicles should not tigger collision
1759 5904 : continue;
1760 : }
1761 : }
1762 :
1763 4297022 : if (colliderBoundary.overlapsWith(victim->getBoundingBox())) {
1764 : // make a detailed check
1765 23009 : PositionVector boundingPoly = collider->getBoundingPoly();
1766 23009 : if (collider->getBoundingPoly(myCheckJunctionCollisionMinGap).overlapsWith(victim->getBoundingPoly())) {
1767 : // junction leader is the victim (collider must still be on junction)
1768 : assert(isInternal());
1769 14291 : if (victim->getLane()->isInternal() && victim->isLeader(myLinks.front(), collider, -1)) {
1770 6322 : foeLane->handleCollisionBetween(timestep, stage, victim, collider, -1, 0, toRemove, toTeleport);
1771 : } else {
1772 7969 : handleCollisionBetween(timestep, stage, collider, victim, -1, 0, toRemove, toTeleport);
1773 : }
1774 : }
1775 23009 : }
1776 : }
1777 32485274 : detectPedestrianJunctionCollision(collider, colliderBoundary, foeLane, timestep, stage, toRemove, toTeleport);
1778 : }
1779 2589318 : if (myLinks.front()->getWalkingAreaFoe() != nullptr) {
1780 59508 : detectPedestrianJunctionCollision(collider, colliderBoundary, myLinks.front()->getWalkingAreaFoe(), timestep, stage, toRemove, toTeleport);
1781 : }
1782 2589318 : if (myLinks.front()->getWalkingAreaFoeExit() != nullptr) {
1783 55556 : detectPedestrianJunctionCollision(collider, colliderBoundary, myLinks.front()->getWalkingAreaFoeExit(), timestep, stage, toRemove, toTeleport);
1784 : }
1785 2589318 : }
1786 : }
1787 :
1788 :
1789 104999265 : if (myIntermodalCollisionAction != COLLISION_ACTION_NONE && myEdge->getPersons().size() > 0 && hasPedestrians()) {
1790 : #ifdef DEBUG_PEDESTRIAN_COLLISIONS
1791 : if (DEBUG_COND) {
1792 : std::cout << SIMTIME << " detect pedestrian collisions stage=" << stage << " lane=" << getID() << "\n";
1793 : }
1794 : #endif
1795 : AnyVehicleIterator v_end = anyVehiclesEnd();
1796 147194 : for (AnyVehicleIterator it_v = anyVehiclesBegin(); it_v != v_end; ++it_v) {
1797 147194 : const MSVehicle* v = *it_v;
1798 147194 : double back = v->getBackPositionOnLane(this);
1799 147194 : const double length = v->getVehicleType().getLength();
1800 147194 : const double right = v->getRightSideOnEdge(this) - getRightSideOnEdge();
1801 147194 : if (v->getLane() == getBidiLane()) {
1802 : // use the front position for checking
1803 611 : back -= length;
1804 : }
1805 147194 : PersonDist leader = nextBlocking(back, right, right + v->getVehicleType().getWidth());
1806 : #ifdef DEBUG_PEDESTRIAN_COLLISIONS
1807 : if (DEBUG_COND && DEBUG_COND2(v)) {
1808 : std::cout << SIMTIME << " back=" << back << " right=" << right << " person=" << Named::getIDSecure(leader.first)
1809 : << " dist=" << leader.second << " jammed=" << (leader.first == nullptr ? false : leader.first->isJammed()) << "\n";
1810 : }
1811 : #endif
1812 147194 : if (leader.first != 0 && leader.second < length && !leader.first->isJammed()) {
1813 94 : if (v->getVehicleType().getGuiShape() == SUMOVehicleShape::AIRCRAFT) {
1814 : // aircraft wings and body are above walking level
1815 : continue;
1816 : }
1817 94 : const double gap = leader.second - length;
1818 188 : handleIntermodalCollisionBetween(timestep, stage, v, leader.first, gap, "sharedLane", toRemove, toTeleport);
1819 : }
1820 : }
1821 : }
1822 :
1823 104999265 : if (myVehicles.size() == 0) {
1824 : return;
1825 : }
1826 104068976 : if (!MSGlobals::gSublane) {
1827 : // no sublanes
1828 : VehCont::reverse_iterator lastVeh = myVehicles.rend() - 1;
1829 640088038 : for (VehCont::reverse_iterator pred = myVehicles.rbegin(); pred != lastVeh; ++pred) {
1830 : VehCont::reverse_iterator veh = pred + 1;
1831 557389542 : detectCollisionBetween(timestep, stage, *veh, *pred, toRemove, toTeleport);
1832 : }
1833 82698496 : if (myPartialVehicles.size() > 0) {
1834 7032252 : detectCollisionBetween(timestep, stage, *lastVeh, myPartialVehicles.front(), toRemove, toTeleport);
1835 : }
1836 82698496 : if (getBidiLane() != nullptr) {
1837 : // bidirectional railway
1838 481664 : MSLane* bidiLane = getBidiLane();
1839 481664 : if (bidiLane->getVehicleNumberWithPartials() > 0) {
1840 320065 : for (auto veh = myVehicles.begin(); veh != myVehicles.end(); ++veh) {
1841 221885 : double high = (*veh)->getPositionOnLane(this);
1842 221885 : double low = (*veh)->getBackPositionOnLane(this);
1843 221885 : if (stage == MSNet::STAGE_MOVEMENTS) {
1844 : // use previous back position to catch trains that
1845 : // "jump" through each other
1846 199733 : low -= SPEED2DIST((*veh)->getSpeed());
1847 : }
1848 968207 : for (AnyVehicleIterator veh2 = bidiLane->anyVehiclesBegin(); veh2 != bidiLane->anyVehiclesEnd(); ++veh2) {
1849 : // self-collisions might legitemately occur when a long train loops back on itself
1850 968207 : if (*veh == *veh2 && !(*veh)->isRail()) {
1851 220980 : continue;
1852 : }
1853 868075 : if ((*veh)->getLane() == (*veh2)->getLane() ||
1854 835384 : (*veh)->getLane() == (*veh2)->getBackLane() ||
1855 88157 : (*veh)->getBackLane() == (*veh2)->getLane()) {
1856 : // vehicles are not in a bidi relation
1857 659070 : continue;
1858 : }
1859 88157 : double low2 = myLength - (*veh2)->getPositionOnLane(bidiLane);
1860 88157 : double high2 = myLength - (*veh2)->getBackPositionOnLane(bidiLane);
1861 88157 : if (stage == MSNet::STAGE_MOVEMENTS) {
1862 : // use previous back position to catch trains that
1863 : // "jump" through each other
1864 74654 : high2 += SPEED2DIST((*veh2)->getSpeed());
1865 : }
1866 88157 : if (!(high < low2 || high2 < low)) {
1867 : #ifdef DEBUG_COLLISIONS
1868 : if (DEBUG_COND) {
1869 : std::cout << SIMTIME << " bidi-collision veh=" << (*veh)->getID() << " bidiVeh=" << (*veh2)->getID()
1870 : << " vehFurther=" << toString((*veh)->getFurtherLanes())
1871 : << " high=" << high << " low=" << low << " high2=" << high2 << " low2=" << low2 << "\n";
1872 : }
1873 : #endif
1874 : // the faster vehicle is at fault
1875 41 : MSVehicle* collider = const_cast<MSVehicle*>(*veh);
1876 41 : MSVehicle* victim = const_cast<MSVehicle*>(*veh2);
1877 41 : if (collider->getSpeed() < victim->getSpeed()) {
1878 : std::swap(victim, collider);
1879 : }
1880 41 : handleCollisionBetween(timestep, stage, collider, victim, -1, 0, toRemove, toTeleport);
1881 : }
1882 : }
1883 : }
1884 : }
1885 : }
1886 : } else {
1887 : // in the sublane-case it is insufficient to check the vehicles ordered
1888 : // by their front position as there might be more than 2 vehicles next to each
1889 : // other on the same lane
1890 : // instead, a moving-window approach is used where all vehicles that
1891 : // overlap in the longitudinal direction receive pairwise checks
1892 : // XXX for efficiency, all lanes of an edge should be checked together
1893 : // (lanechanger-style)
1894 :
1895 : // XXX quick hack: check each in myVehicles against all others
1896 143837994 : for (AnyVehicleIterator veh = anyVehiclesBegin(); veh != anyVehiclesEnd(); ++veh) {
1897 143837994 : MSVehicle* follow = (MSVehicle*)*veh;
1898 3774073155 : for (AnyVehicleIterator veh2 = anyVehiclesBegin(); veh2 != anyVehiclesEnd(); ++veh2) {
1899 3774136131 : MSVehicle* lead = (MSVehicle*)*veh2;
1900 3774136131 : if (lead == follow) {
1901 143834170 : continue;
1902 : }
1903 3630301961 : if (lead->getPositionOnLane(this) < follow->getPositionOnLane(this)) {
1904 1814082895 : continue;
1905 : }
1906 1816219066 : if (detectCollisionBetween(timestep, stage, follow, lead, toRemove, toTeleport)) {
1907 : // XXX what about collisions with multiple leaders at once?
1908 : break;
1909 : }
1910 : }
1911 : }
1912 : }
1913 :
1914 :
1915 104072894 : for (std::set<const MSVehicle*, ComparatorNumericalIdLess>::iterator it = toRemove.begin(); it != toRemove.end(); ++it) {
1916 3918 : MSVehicle* veh = const_cast<MSVehicle*>(*it);
1917 : MSLane* vehLane = veh->getMutableLane();
1918 3918 : vehLane->removeVehicle(veh, MSMoveReminder::NOTIFICATION_TELEPORT, false);
1919 : if (toTeleport.count(veh) > 0) {
1920 3533 : MSVehicleTransfer::getInstance()->add(timestep, veh);
1921 : } else {
1922 385 : veh->onRemovalFromNet(MSMoveReminder::NOTIFICATION_VAPORIZED_COLLISION);
1923 385 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
1924 : }
1925 : }
1926 : }
1927 :
1928 :
1929 : void
1930 32600338 : MSLane::detectPedestrianJunctionCollision(const MSVehicle* collider, const PositionVector& colliderBoundary, const MSLane* foeLane,
1931 : SUMOTime timestep, const std::string& stage,
1932 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toRemove,
1933 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toTeleport) {
1934 32600338 : if (myIntermodalCollisionAction != COLLISION_ACTION_NONE && foeLane->getEdge().getPersons().size() > 0 && foeLane->hasPedestrians()) {
1935 : #ifdef DEBUG_PEDESTRIAN_COLLISIONS
1936 : if (DEBUG_COND) {
1937 : std::cout << SIMTIME << " detect pedestrian junction collisions stage=" << stage << " lane=" << getID() << " foeLane=" << foeLane->getID() << "\n";
1938 : }
1939 : #endif
1940 112340 : const std::vector<MSTransportable*>& persons = foeLane->getEdge().getSortedPersons(timestep);
1941 1267624 : for (std::vector<MSTransportable*>::const_iterator it_p = persons.begin(); it_p != persons.end(); ++it_p) {
1942 : #ifdef DEBUG_PEDESTRIAN_COLLISIONS
1943 : if (DEBUG_COND) {
1944 : std::cout << " collider=" << collider->getID()
1945 : << " ped=" << (*it_p)->getID()
1946 : << " jammed=" << (*it_p)->isJammed()
1947 : << " colliderBoundary=" << colliderBoundary
1948 : << " pedBoundary=" << (*it_p)->getBoundingBox()
1949 : << "\n";
1950 : }
1951 : #endif
1952 1155284 : if ((*it_p)->isJammed()) {
1953 876 : continue;
1954 : }
1955 2308816 : if (colliderBoundary.overlapsWith((*it_p)->getBoundingBox())
1956 1154408 : && collider->getBoundingPoly().overlapsWith((*it_p)->getBoundingBox())) {
1957 3870 : std::string collisionType = "junctionPedestrian";
1958 3870 : if (foeLane->isCrossing()) {
1959 : collisionType = "crossing";
1960 3654 : } else if (foeLane->isWalkingArea()) {
1961 : collisionType = "walkingarea";
1962 : }
1963 3870 : handleIntermodalCollisionBetween(timestep, stage, collider, *it_p, 0, collisionType, toRemove, toTeleport);
1964 : }
1965 : }
1966 112340 : }
1967 32600338 : }
1968 :
1969 :
1970 : bool
1971 2380640860 : MSLane::detectCollisionBetween(SUMOTime timestep, const std::string& stage, MSVehicle* collider, MSVehicle* victim,
1972 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toRemove,
1973 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toTeleport) const {
1974 4732695745 : if (myCollisionAction == COLLISION_ACTION_TELEPORT && ((victim->hasInfluencer() && victim->getInfluencer().isRemoteAffected(timestep)) ||
1975 2352305194 : (collider->hasInfluencer() && collider->getInfluencer().isRemoteAffected(timestep)))) {
1976 37 : return false;
1977 : }
1978 :
1979 : // No self-collisions! (This is assumed to be ensured at caller side)
1980 2380640823 : if (collider == victim) {
1981 : return false;
1982 : }
1983 :
1984 2380639707 : const bool colliderOpposite = collider->getLaneChangeModel().isOpposite() || collider->isBidiOn(this);
1985 2380639707 : const bool victimOpposite = victim->getLaneChangeModel().isOpposite() || victim->isBidiOn(this);
1986 2380639707 : const bool bothOpposite = victimOpposite && colliderOpposite;
1987 2380639707 : if (bothOpposite) {
1988 : std::swap(victim, collider);
1989 : }
1990 2380639707 : const double colliderPos = colliderOpposite && !bothOpposite ? collider->getBackPositionOnLane(this) : collider->getPositionOnLane(this);
1991 2380639707 : const double minGapFactor = myCollisionMinGapFactor >= 0 ? myCollisionMinGapFactor : collider->getCarFollowModel().getCollisionMinGapFactor();
1992 2380639707 : double victimBack = victimOpposite && !bothOpposite ? victim->getPositionOnLane(this) : victim->getBackPositionOnLane(this);
1993 2380639707 : if (victim->getLateralOverlap() > 0 || collider->getLateralOverlap() > 0) {
1994 143389184 : if (&collider->getLane()->getEdge() == myEdge && collider->getLane()->getLength() > getLength()) {
1995 : // interpret victim position on the longer lane
1996 782 : victimBack *= collider->getLane()->getLength() / getLength();
1997 : }
1998 : }
1999 2380639707 : double gap = victimBack - colliderPos - minGapFactor * collider->getVehicleType().getMinGap();
2000 2380639707 : if (bothOpposite) {
2001 1839496 : gap = colliderPos - victimBack - minGapFactor * collider->getVehicleType().getMinGap();
2002 2378800211 : } else if (colliderOpposite) {
2003 : // vehicles are back to back so (frontal) minGap doesn't apply
2004 3630777 : gap += minGapFactor * collider->getVehicleType().getMinGap();
2005 : }
2006 : #ifdef DEBUG_COLLISIONS
2007 : if (DEBUG_COND && (DEBUG_COND2(collider) || DEBUG_COND2(victim))) {
2008 : std::cout << SIMTIME
2009 : << " thisLane=" << getID()
2010 : << " collider=" << collider->getID()
2011 : << " victim=" << victim->getID()
2012 : << " colOpposite=" << colliderOpposite
2013 : << " vicOpposite=" << victimOpposite
2014 : << " colLane=" << collider->getLane()->getID()
2015 : << " vicLane=" << victim->getLane()->getID()
2016 : << " colPos=" << colliderPos
2017 : << " vicBack=" << victimBack
2018 : << " colLat=" << collider->getCenterOnEdge(this)
2019 : << " vicLat=" << victim->getCenterOnEdge(this)
2020 : << " minGap=" << collider->getVehicleType().getMinGap()
2021 : << " minGapFactor=" << minGapFactor
2022 : << " gap=" << gap
2023 : << "\n";
2024 : }
2025 : #endif
2026 2380639707 : if (victimOpposite && gap < -(collider->getLength() + victim->getLength())) {
2027 : // already past each other
2028 : return false;
2029 : }
2030 2380621558 : if (gap < -NUMERICAL_EPS) {
2031 : double latGap = 0;
2032 11860491 : if (MSGlobals::gSublane) {
2033 11854006 : latGap = (fabs(victim->getCenterOnEdge(this) - collider->getCenterOnEdge(this))
2034 11854006 : - 0.5 * fabs(victim->getVehicleType().getWidth() + collider->getVehicleType().getWidth()));
2035 11854006 : if (latGap + NUMERICAL_EPS > 0) {
2036 : return false;
2037 : }
2038 : // account for ambiguous gap computation related to partial
2039 : // occupation of lanes with different lengths
2040 62978 : if (isInternal() && getEdge().getNumLanes() > 1 && victim->getLane() != collider->getLane()) {
2041 : double gapDelta = 0;
2042 2013 : const MSVehicle* otherLaneVeh = collider->getLane() == this ? victim : collider;
2043 2013 : if (otherLaneVeh->getLaneChangeModel().getShadowLane() == this) {
2044 639 : gapDelta = getLength() - otherLaneVeh->getLane()->getLength();
2045 : } else {
2046 1374 : for (const MSLane* cand : otherLaneVeh->getFurtherLanes()) {
2047 1374 : if (&cand->getEdge() == &getEdge()) {
2048 1374 : gapDelta = getLength() - cand->getLength();
2049 1374 : break;
2050 : }
2051 : }
2052 : }
2053 2013 : if (gap + gapDelta >= 0) {
2054 : return false;
2055 : }
2056 : }
2057 : }
2058 69461 : if (MSGlobals::gLaneChangeDuration > DELTA_T
2059 53 : && collider->getLaneChangeModel().isChangingLanes()
2060 36 : && victim->getLaneChangeModel().isChangingLanes()
2061 69461 : && victim->getLane() != this) {
2062 : // synchroneous lane change maneuver
2063 : return false;
2064 : }
2065 : #ifdef DEBUG_COLLISIONS
2066 : if (DEBUG_COND && (DEBUG_COND2(collider) || DEBUG_COND2(victim))) {
2067 : std::cout << SIMTIME << " detectedCollision gap=" << gap << " latGap=" << latGap << "\n";
2068 : }
2069 : #endif
2070 69461 : handleCollisionBetween(timestep, stage, collider, victim, gap, latGap, toRemove, toTeleport);
2071 69461 : return true;
2072 : }
2073 : return false;
2074 : }
2075 :
2076 :
2077 : void
2078 83793 : MSLane::handleCollisionBetween(SUMOTime timestep, const std::string& stage, const MSVehicle* collider, const MSVehicle* victim,
2079 : double gap, double latGap, std::set<const MSVehicle*, ComparatorNumericalIdLess>& toRemove,
2080 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toTeleport) const {
2081 83793 : if (collider->ignoreCollision() || victim->ignoreCollision()) {
2082 66773 : return;
2083 : }
2084 : std::string collisionType;
2085 : std::string collisionText;
2086 80875 : if (isFrontalCollision(collider, victim)) {
2087 : collisionType = "frontal";
2088 6846 : collisionText = TL("frontal collision");
2089 74029 : } else if (stage == MSNet::STAGE_LANECHANGE) {
2090 : collisionType = "side";
2091 12273 : collisionText = TL("side collision");
2092 61756 : } else if (isInternal()) {
2093 : collisionType = "junction";
2094 13562 : collisionText = TL("junction collision");
2095 : } else {
2096 : collisionType = "collision";
2097 48194 : collisionText = TL("collision");
2098 : }
2099 :
2100 : // in frontal collisions the opposite vehicle is the collider
2101 80875 : if (victim->getLaneChangeModel().isOpposite() && !collider->getLaneChangeModel().isOpposite()) {
2102 : std::swap(collider, victim);
2103 : }
2104 323500 : std::string prefix = TLF("Vehicle '%'; % with vehicle '%", collider->getID(), collisionText, victim->getID());
2105 80875 : if (myCollisionStopTime > 0) {
2106 66072 : if (collider->collisionStopTime() >= 0 && victim->collisionStopTime() >= 0) {
2107 63855 : return;
2108 : }
2109 : std::string dummyError;
2110 2217 : SUMOVehicleParameter::Stop stop;
2111 2217 : stop.duration = myCollisionStopTime;
2112 2217 : stop.parametersSet |= STOP_DURATION_SET;
2113 2217 : const double collisionAngle = RAD2DEG(fabs(GeomHelper::angleDiff(victim->getAngle(), collider->getAngle())));
2114 : // determine new speeds from collision angle (@todo account for vehicle mass)
2115 2217 : double victimSpeed = victim->getSpeed();
2116 2217 : double colliderSpeed = collider->getSpeed();
2117 : // double victimOrigSpeed = victim->getSpeed();
2118 : // double colliderOrigSpeed = collider->getSpeed();
2119 2217 : if (collisionAngle < 45) {
2120 : // rear-end collisions
2121 : colliderSpeed = MIN2(colliderSpeed, victimSpeed);
2122 292 : } else if (collisionAngle < 135) {
2123 : // side collision
2124 288 : colliderSpeed /= 2;
2125 288 : victimSpeed /= 2;
2126 : } else {
2127 : // frontal collision
2128 : colliderSpeed = 0;
2129 : victimSpeed = 0;
2130 : }
2131 2217 : const double victimStopPos = MIN2(victim->getLane()->getLength(),
2132 2217 : victim->getPositionOnLane() + victim->getCarFollowModel().brakeGap(victimSpeed, victim->getCarFollowModel().getEmergencyDecel(), 0));
2133 2217 : if (victim->collisionStopTime() < 0) {
2134 1432 : stop.collision = true;
2135 1432 : stop.lane = victim->getLane()->getID();
2136 : // @todo: push victim forward?
2137 1432 : stop.startPos = victimStopPos;
2138 1432 : stop.endPos = stop.startPos;
2139 1432 : stop.parametersSet |= STOP_START_SET | STOP_END_SET;
2140 1432 : ((MSBaseVehicle*)victim)->addStop(stop, dummyError, 0);
2141 : }
2142 2217 : if (collider->collisionStopTime() < 0) {
2143 1758 : stop.collision = true;
2144 1758 : stop.lane = collider->getLane()->getID();
2145 1758 : stop.startPos = MIN2(collider->getPositionOnLane() + collider->getCarFollowModel().brakeGap(colliderSpeed, collider->getCarFollowModel().getEmergencyDecel(), 0),
2146 1758 : MAX3(0.0, victimStopPos - 0.75 * victim->getVehicleType().getLength(),
2147 1758 : collider->getPositionOnLane() - SPEED2DIST(collider->getSpeed())));
2148 1758 : stop.endPos = stop.startPos;
2149 1758 : stop.parametersSet |= STOP_START_SET | STOP_END_SET;
2150 1758 : ((MSBaseVehicle*)collider)->addStop(stop, dummyError, 0);
2151 : }
2152 : //std::cout << " collisionAngle=" << collisionAngle
2153 : // << "\n vPos=" << victim->getPositionOnLane() << " vStop=" << victimStopPos << " vSpeed=" << victimOrigSpeed << " vSpeed2=" << victimSpeed << " vSpeed3=" << victim->getSpeed()
2154 : // << "\n cPos=" << collider->getPositionOnLane() << " cStop=" << stop.startPos << " cSpeed=" << colliderOrigSpeed << " cSpeed2=" << colliderSpeed << " cSpeed3=" << collider->getSpeed()
2155 : // << "\n";
2156 2217 : } else {
2157 14803 : switch (myCollisionAction) {
2158 : case COLLISION_ACTION_WARN:
2159 : break;
2160 3534 : case COLLISION_ACTION_TELEPORT:
2161 7068 : prefix = TLF("Teleporting vehicle '%'; % with vehicle '%", collider->getID(), collisionText, victim->getID());
2162 : toRemove.insert(collider);
2163 : toTeleport.insert(collider);
2164 : break;
2165 210 : case COLLISION_ACTION_REMOVE: {
2166 420 : prefix = TLF("Removing % participants: vehicle '%', vehicle '%", collisionText, collider->getID(), victim->getID());
2167 : bool removeCollider = true;
2168 : bool removeVictim = true;
2169 210 : removeVictim = !(victim->hasInfluencer() && victim->getInfluencer()->isRemoteAffected(timestep));
2170 210 : removeCollider = !(collider->hasInfluencer() && collider->getInfluencer()->isRemoteAffected(timestep));
2171 210 : if (removeVictim) {
2172 : toRemove.insert(victim);
2173 : }
2174 210 : if (removeCollider) {
2175 : toRemove.insert(collider);
2176 : }
2177 210 : if (!removeVictim) {
2178 0 : if (!removeCollider) {
2179 0 : prefix = TLF("Keeping remote-controlled % participants: vehicle '%', vehicle '%", collisionText, collider->getID(), victim->getID());
2180 : } else {
2181 0 : prefix = TLF("Removing % participant: vehicle '%', keeping remote-controlled vehicle '%", collisionText, collider->getID(), victim->getID());
2182 : }
2183 210 : } else if (!removeCollider) {
2184 0 : prefix = TLF("Keeping remote-controlled % participant: vehicle '%', removing vehicle '%", collisionText, collider->getID(), victim->getID());
2185 : }
2186 : break;
2187 : }
2188 : default:
2189 : break;
2190 : }
2191 : }
2192 17020 : const bool newCollision = MSNet::getInstance()->registerCollision(collider, victim, collisionType, this, collider->getPositionOnLane(this));
2193 17020 : if (newCollision) {
2194 30766 : WRITE_WARNINGF(prefix + "', lane='%', gap=%%, time=%, stage=%.",
2195 : getID(), toString(gap), (MSGlobals::gSublane ? TL(", latGap=") + toString(latGap) : ""),
2196 : time2string(timestep), stage);
2197 6198 : MSNet::getInstance()->informVehicleStateListener(victim, MSNet::VehicleState::COLLISION);
2198 6198 : MSNet::getInstance()->informVehicleStateListener(collider, MSNet::VehicleState::COLLISION);
2199 6198 : MSNet::getInstance()->getVehicleControl().countCollision(myCollisionAction == COLLISION_ACTION_TELEPORT);
2200 : }
2201 : #ifdef DEBUG_COLLISIONS
2202 : if (DEBUG_COND2(collider)) {
2203 : toRemove.erase(collider);
2204 : toTeleport.erase(collider);
2205 : }
2206 : if (DEBUG_COND2(victim)) {
2207 : toRemove.erase(victim);
2208 : toTeleport.erase(victim);
2209 : }
2210 : #endif
2211 : }
2212 :
2213 :
2214 : void
2215 3964 : MSLane::handleIntermodalCollisionBetween(SUMOTime timestep, const std::string& stage, const MSVehicle* collider, const MSTransportable* victim,
2216 : double gap, const std::string& collisionType,
2217 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toRemove,
2218 : std::set<const MSVehicle*, ComparatorNumericalIdLess>& toTeleport) const {
2219 3964 : if (collider->ignoreCollision()) {
2220 3304 : return;
2221 : }
2222 7928 : std::string prefix = TLF("Vehicle '%'", collider->getID());
2223 3964 : if (myIntermodalCollisionStopTime > 0) {
2224 3344 : if (collider->collisionStopTime() >= 0) {
2225 3304 : return;
2226 : }
2227 : std::string dummyError;
2228 40 : SUMOVehicleParameter::Stop stop;
2229 40 : stop.duration = myIntermodalCollisionStopTime;
2230 40 : stop.parametersSet |= STOP_DURATION_SET;
2231 : // determine new speeds from collision angle (@todo account for vehicle mass)
2232 40 : double colliderSpeed = collider->getSpeed();
2233 40 : const double victimStopPos = victim->getEdgePos();
2234 : // double victimOrigSpeed = victim->getSpeed();
2235 : // double colliderOrigSpeed = collider->getSpeed();
2236 40 : if (collider->collisionStopTime() < 0) {
2237 40 : stop.collision = true;
2238 40 : stop.lane = collider->getLane()->getID();
2239 40 : stop.startPos = MIN2(collider->getPositionOnLane() + collider->getCarFollowModel().brakeGap(colliderSpeed, collider->getCarFollowModel().getEmergencyDecel(), 0),
2240 40 : MAX3(0.0, victimStopPos - 0.75 * victim->getVehicleType().getLength(),
2241 40 : collider->getPositionOnLane() - SPEED2DIST(collider->getSpeed())));
2242 40 : stop.endPos = stop.startPos;
2243 40 : stop.parametersSet |= STOP_START_SET | STOP_END_SET;
2244 40 : ((MSBaseVehicle*)collider)->addStop(stop, dummyError, 0);
2245 : }
2246 40 : } else {
2247 620 : switch (myIntermodalCollisionAction) {
2248 : case COLLISION_ACTION_WARN:
2249 : break;
2250 15 : case COLLISION_ACTION_TELEPORT:
2251 30 : prefix = TLF("Teleporting vehicle '%' after", collider->getID());
2252 : toRemove.insert(collider);
2253 : toTeleport.insert(collider);
2254 : break;
2255 15 : case COLLISION_ACTION_REMOVE: {
2256 30 : prefix = TLF("Removing vehicle '%' after", collider->getID());
2257 : bool removeCollider = true;
2258 15 : removeCollider = !(collider->hasInfluencer() && collider->getInfluencer()->isRemoteAffected(timestep));
2259 : if (!removeCollider) {
2260 0 : prefix = TLF("Keeping remote-controlled vehicle '%' after", collider->getID());
2261 : } else {
2262 : toRemove.insert(collider);
2263 : }
2264 : break;
2265 : }
2266 : default:
2267 : break;
2268 : }
2269 : }
2270 660 : const bool newCollision = MSNet::getInstance()->registerCollision(collider, victim, collisionType, this, victim->getEdgePos());
2271 660 : if (newCollision) {
2272 272 : if (gap != 0) {
2273 455 : WRITE_WARNING(prefix + TLF(" collision with person '%', lane='%', gap=%, time=%, stage=%.",
2274 : victim->getID(), getID(), gap, time2string(timestep), stage));
2275 : } else {
2276 905 : WRITE_WARNING(prefix + TLF(" collision with person '%', lane='%', time=%, stage=%.",
2277 : victim->getID(), getID(), time2string(timestep), stage));
2278 : }
2279 272 : MSNet::getInstance()->informVehicleStateListener(collider, MSNet::VehicleState::COLLISION);
2280 272 : MSNet::getInstance()->getVehicleControl().countCollision(myIntermodalCollisionAction == COLLISION_ACTION_TELEPORT);
2281 : }
2282 : #ifdef DEBUG_COLLISIONS
2283 : if (DEBUG_COND2(collider)) {
2284 : toRemove.erase(collider);
2285 : toTeleport.erase(collider);
2286 : }
2287 : #endif
2288 : }
2289 :
2290 :
2291 : bool
2292 80875 : MSLane::isFrontalCollision(const MSVehicle* collider, const MSVehicle* victim) {
2293 80875 : if (collider->getLaneChangeModel().isOpposite() != victim->getLaneChangeModel().isOpposite()) {
2294 : return true;
2295 : } else {
2296 80857 : const MSEdge* victimBidi = victim->getLane()->getEdge().getBidiEdge();
2297 80857 : if (&collider->getLane()->getEdge() == victimBidi) {
2298 : return true;
2299 : } else {
2300 108108 : for (MSLane* further : collider->getFurtherLanes()) {
2301 34079 : if (&further->getEdge() == victimBidi) {
2302 : return true;
2303 : }
2304 : }
2305 : }
2306 : }
2307 : return false;
2308 : }
2309 :
2310 : void
2311 96771953 : MSLane::executeMovements(const SUMOTime t) {
2312 : // multithreading: there are concurrent writes to myNeedsCollisionCheck but all of them should set it to true
2313 96771953 : myNeedsCollisionCheck = true;
2314 96771953 : MSLane* bidi = getBidiLane();
2315 96771953 : if (bidi != nullptr && bidi->getVehicleNumber() == 0) {
2316 716795 : MSNet::getInstance()->getEdgeControl().checkCollisionForInactive(bidi);
2317 : }
2318 96771953 : MSVehicle* firstNotStopped = nullptr;
2319 : // iterate over vehicles in reverse so that move reminders will be called in the correct order
2320 797578244 : for (VehCont::reverse_iterator i = myVehicles.rbegin(); i != myVehicles.rend();) {
2321 704572414 : MSVehicle* veh = *i;
2322 : // length is needed later when the vehicle may not exist anymore
2323 704572414 : const double length = veh->getVehicleType().getLengthWithGap();
2324 704572414 : const double nettoLength = veh->getVehicleType().getLength();
2325 704572414 : const bool moved = veh->executeMove();
2326 : MSLane* const target = veh->getMutableLane();
2327 700806291 : if (veh->hasArrived()) {
2328 : // vehicle has reached its arrival position
2329 : #ifdef DEBUG_EXEC_MOVE
2330 : if DEBUG_COND2(veh) {
2331 : std::cout << SIMTIME << " veh " << veh->getID() << " has arrived." << std::endl;
2332 : }
2333 : #endif
2334 3322579 : veh->onRemovalFromNet(MSMoveReminder::NOTIFICATION_ARRIVED);
2335 3322579 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
2336 697483712 : } else if (target != nullptr && moved) {
2337 16824994 : if (target->getEdge().isVaporizing()) {
2338 : // vehicle has reached a vaporizing edge
2339 756 : veh->onRemovalFromNet(MSMoveReminder::NOTIFICATION_VAPORIZED_VAPORIZER);
2340 756 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
2341 : } else {
2342 : // vehicle has entered a new lane (leaveLane and workOnMoveReminders were already called in MSVehicle::executeMove)
2343 16824238 : target->myVehBuffer.push_back(veh);
2344 16824238 : MSNet::getInstance()->getEdgeControl().needsVehicleIntegration(target);
2345 16824238 : if (MSGlobals::gSublane && veh->getLaneChangeModel().getShadowLane() != nullptr) {
2346 : // trigger sorting of partial vehicles as their order may have changed (lane might not be active and only contain partial vehicles)
2347 58720 : MSNet::getInstance()->getEdgeControl().needsVehicleIntegration(veh->getLaneChangeModel().getShadowLane());
2348 : }
2349 : }
2350 680658718 : } else if (veh->isParking()) {
2351 : // vehicle started to park
2352 16480 : MSVehicleTransfer::getInstance()->add(t, veh);
2353 16480 : myParkingVehicles.insert(veh);
2354 680642238 : } else if (veh->brokeDown()) {
2355 12 : veh->resumeFromStopping();
2356 36 : WRITE_WARNINGF(TL("Removing vehicle '%' after breaking down, lane='%', time=%."),
2357 : veh->getID(), veh->getLane()->getID(), time2string(t));
2358 12 : veh->onRemovalFromNet(MSMoveReminder::NOTIFICATION_VAPORIZED_BREAKDOWN);
2359 12 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
2360 680642226 : } else if (veh->isJumping()) {
2361 : // vehicle jumps to next route edge
2362 953 : MSVehicleTransfer::getInstance()->add(t, veh);
2363 680641273 : } else if (veh->getPositionOnLane() > getLength()) {
2364 : // for any reasons the vehicle is beyond its lane...
2365 : // this should never happen because it is handled in MSVehicle::executeMove
2366 : assert(false);
2367 0 : WRITE_WARNINGF(TL("Teleporting vehicle '%'; beyond end of lane, target lane='%', time=%."),
2368 : veh->getID(), getID(), time2string(t));
2369 0 : MSNet::getInstance()->getVehicleControl().countCollision(true);
2370 0 : MSVehicleTransfer::getInstance()->add(t, veh);
2371 :
2372 680641273 : } else if (veh->collisionStopTime() == 0) {
2373 3422 : veh->resumeFromStopping();
2374 3422 : if (getCollisionAction() == COLLISION_ACTION_REMOVE) {
2375 531 : WRITE_WARNINGF(TL("Removing vehicle '%' after earlier collision, lane='%', time=%."),
2376 : veh->getID(), veh->getLane()->getID(), time2string(t));
2377 177 : veh->onRemovalFromNet(MSMoveReminder::NOTIFICATION_VAPORIZED_COLLISION);
2378 177 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(veh);
2379 3245 : } else if (getCollisionAction() == COLLISION_ACTION_TELEPORT) {
2380 7236 : WRITE_WARNINGF(TL("Teleporting vehicle '%' after earlier collision, lane='%', time=%."),
2381 : veh->getID(), veh->getLane()->getID(), time2string(t));
2382 2412 : MSVehicleTransfer::getInstance()->add(t, veh);
2383 : } else {
2384 833 : if (firstNotStopped == nullptr && !(*i)->isStopped() && (*i)->getLane() == this) {
2385 557 : firstNotStopped = *i;
2386 : }
2387 : ++i;
2388 833 : continue;
2389 : }
2390 : } else {
2391 680637851 : if (firstNotStopped == nullptr && !(*i)->isStopped() && (*i)->getLane() == this) {
2392 81177969 : firstNotStopped = *i;
2393 : }
2394 : ++i;
2395 680637851 : continue;
2396 : }
2397 20167607 : myBruttoVehicleLengthSumToRemove += length;
2398 20167607 : myNettoVehicleLengthSumToRemove += nettoLength;
2399 : ++i;
2400 20167607 : i = VehCont::reverse_iterator(myVehicles.erase(i.base()));
2401 : }
2402 93005830 : if (firstNotStopped != nullptr) {
2403 81178526 : const SUMOTime ttt = firstNotStopped->getVehicleType().getParameter().getTimeToTeleport(MSGlobals::gTimeToGridlock);
2404 81178526 : const SUMOTime tttb = firstNotStopped->getVehicleType().getParameter().getTimeToTeleportBidi(MSGlobals::gTimeToTeleportBidi);
2405 81178526 : if (ttt > 0 || MSGlobals::gTimeToGridlockHighways > 0 || MSGlobals::gTimeToTeleportDisconnected >= 0 || tttb > 0 || MSGlobals::gTimeToTeleportRSDeadlock > 0) {
2406 78155958 : const bool wrongLane = !appropriate(firstNotStopped);
2407 78155958 : const bool disconnected = (MSGlobals::gTimeToTeleportDisconnected >= 0
2408 40907 : && firstNotStopped->succEdge(1) != nullptr
2409 78193030 : && firstNotStopped->getEdge()->allowedLanes(*firstNotStopped->succEdge(1), firstNotStopped->getVClass()) == nullptr);
2410 :
2411 78146868 : const bool r1 = ttt > 0 && firstNotStopped->getWaitingTime() > ttt && !disconnected
2412 : // never teleport a taxi on the last edge of it's route (where it would exit the simulation)
2413 78164552 : && (firstNotStopped->getDevice(typeid(MSDevice_Taxi)) == nullptr || firstNotStopped->getRoutePosition() < (firstNotStopped->getRoute().size() - 1));
2414 78147748 : const bool r2 = !r1 && MSGlobals::gTimeToGridlockHighways > 0
2415 1011 : && firstNotStopped->getWaitingTime() > MSGlobals::gTimeToGridlockHighways
2416 602 : && getSpeedLimit() > MSGlobals::gGridlockHighwaysSpeed && wrongLane
2417 602 : && !disconnected;
2418 78155946 : const bool r3 = disconnected && firstNotStopped->getWaitingTime() > MSGlobals::gTimeToTeleportDisconnected;
2419 78147736 : const bool r4 = !r1 && !r2 && !r3 && tttb > 0
2420 78156402 : && firstNotStopped->getWaitingTime() > tttb && getBidiLane() && !disconnected;
2421 539924 : const bool r5 = MSGlobals::gTimeToTeleportRSDeadlock > 0 && MSRailSignalControl::hasInstance() && !r1 && !r2 && !r3 && !r4
2422 78647556 : && firstNotStopped->getWaitingTime() > MSGlobals::gTimeToTeleportRSDeadlock && MSRailSignalControl::getInstance().haveDeadlock(firstNotStopped);
2423 78155958 : if (r1 || r2 || r3 || r4 || r5) {
2424 8330 : const std::vector<MSLink*>::const_iterator link = succLinkSec(*firstNotStopped, 1, *this, firstNotStopped->getBestLanesContinuation());
2425 8330 : const bool minorLink = !wrongLane && (link != myLinks.end()) && !((*link)->havePriority());
2426 10425 : std::string reason = (wrongLane ? " (wrong lane" : (minorLink ? " (yield" : " (jam"));
2427 8330 : myBruttoVehicleLengthSumToRemove += firstNotStopped->getVehicleType().getLengthWithGap();
2428 8330 : myNettoVehicleLengthSumToRemove += firstNotStopped->getVehicleType().getLength();
2429 8330 : if (firstNotStopped == myVehicles.back()) {
2430 : myVehicles.pop_back();
2431 : } else {
2432 306 : myVehicles.erase(std::find(myVehicles.begin(), myVehicles.end(), firstNotStopped));
2433 : reason = " (blocked";
2434 : }
2435 74850 : WRITE_WARNINGF("Teleporting vehicle '%'; waited too long" + reason
2436 : + (r2 ? ", highway" : "")
2437 : + (r3 ? ", disconnected" : "")
2438 : + (r4 ? ", bidi" : "")
2439 : + (r5 ? ", railSignal" : "")
2440 : + "), lane='%', time=%.", firstNotStopped->getID(), getID(), time2string(t));
2441 8330 : if (wrongLane) {
2442 1023 : MSNet::getInstance()->getVehicleControl().registerTeleportWrongLane();
2443 7307 : } else if (minorLink) {
2444 5212 : MSNet::getInstance()->getVehicleControl().registerTeleportYield();
2445 : } else {
2446 2095 : MSNet::getInstance()->getVehicleControl().registerTeleportJam();
2447 : }
2448 8330 : if (MSGlobals::gRemoveGridlocked) {
2449 14 : firstNotStopped->onRemovalFromNet(MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED);
2450 14 : MSNet::getInstance()->getVehicleControl().scheduleVehicleRemoval(firstNotStopped);
2451 : } else {
2452 8316 : MSVehicleTransfer::getInstance()->add(t, firstNotStopped);
2453 : }
2454 : }
2455 : }
2456 : }
2457 93005830 : if (MSGlobals::gSublane) {
2458 : // trigger sorting of vehicles as their order may have changed
2459 17408990 : MSNet::getInstance()->getEdgeControl().needsVehicleIntegration(this);
2460 : }
2461 93005830 : }
2462 :
2463 :
2464 : void
2465 249 : MSLane::markRecalculateBruttoSum() {
2466 249 : myRecalculateBruttoSum = true;
2467 249 : }
2468 :
2469 :
2470 : void
2471 96771950 : MSLane::updateLengthSum() {
2472 96771950 : myBruttoVehicleLengthSum -= myBruttoVehicleLengthSumToRemove;
2473 96771950 : myNettoVehicleLengthSum -= myNettoVehicleLengthSumToRemove;
2474 96771950 : myBruttoVehicleLengthSumToRemove = 0;
2475 96771950 : myNettoVehicleLengthSumToRemove = 0;
2476 96771950 : if (myVehicles.empty()) {
2477 : // avoid numerical instability
2478 7802555 : myBruttoVehicleLengthSum = 0;
2479 7802555 : myNettoVehicleLengthSum = 0;
2480 88969395 : } else if (myRecalculateBruttoSum) {
2481 184 : myBruttoVehicleLengthSum = 0;
2482 677 : for (VehCont::const_iterator i = myVehicles.begin(); i != myVehicles.end(); ++i) {
2483 493 : myBruttoVehicleLengthSum += (*i)->getVehicleType().getLengthWithGap();
2484 : }
2485 184 : myRecalculateBruttoSum = false;
2486 : }
2487 96771950 : }
2488 :
2489 :
2490 : void
2491 0 : MSLane::changeLanes(const SUMOTime t) {
2492 0 : myEdge->changeLanes(t);
2493 0 : }
2494 :
2495 :
2496 : const MSEdge*
2497 5541216 : MSLane::getNextNormal() const {
2498 5541216 : return myEdge->getNormalSuccessor();
2499 : }
2500 :
2501 :
2502 : const MSLane*
2503 129774 : MSLane::getFirstInternalInConnection(double& offset) const {
2504 129774 : if (!this->isInternal()) {
2505 : return nullptr;
2506 : }
2507 129774 : offset = 0.;
2508 : const MSLane* firstInternal = this;
2509 129774 : MSLane* pred = getCanonicalPredecessorLane();
2510 129822 : while (pred != nullptr && pred->isInternal()) {
2511 : firstInternal = pred;
2512 48 : offset += pred->getLength();
2513 48 : pred = firstInternal->getCanonicalPredecessorLane();
2514 : }
2515 : return firstInternal;
2516 : }
2517 :
2518 :
2519 : // ------ Static (sic!) container methods ------
2520 : bool
2521 2151379 : MSLane::dictionary(const std::string& id, MSLane* ptr) {
2522 : const DictType::iterator it = myDict.lower_bound(id);
2523 2151379 : if (it == myDict.end() || it->first != id) {
2524 : // id not in myDict
2525 2151371 : myDict.emplace_hint(it, id, ptr);
2526 2151371 : return true;
2527 : }
2528 : return false;
2529 : }
2530 :
2531 :
2532 : MSLane*
2533 9253174 : MSLane::dictionary(const std::string& id) {
2534 : const DictType::iterator it = myDict.find(id);
2535 9253174 : if (it == myDict.end()) {
2536 : // id not in myDict
2537 : return nullptr;
2538 : }
2539 9252605 : return it->second;
2540 : }
2541 :
2542 :
2543 : void
2544 42571 : MSLane::clear() {
2545 2174730 : for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
2546 2132159 : delete (*i).second;
2547 : }
2548 : myDict.clear();
2549 42571 : }
2550 :
2551 :
2552 : void
2553 242 : MSLane::insertIDs(std::vector<std::string>& into) {
2554 8196 : for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
2555 7954 : into.push_back((*i).first);
2556 : }
2557 242 : }
2558 :
2559 :
2560 : template<class RTREE> void
2561 610 : MSLane::fill(RTREE& into) {
2562 25261 : for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
2563 24651 : MSLane* l = (*i).second;
2564 24651 : Boundary b = l->getShape().getBoxBoundary();
2565 24651 : b.grow(3.);
2566 24651 : const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
2567 24651 : const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
2568 24651 : into.Insert(cmin, cmax, l);
2569 : }
2570 610 : }
2571 :
2572 : template void MSLane::fill<NamedRTree>(NamedRTree& into);
2573 : template void MSLane::fill<LANE_RTREE_QUAL>(LANE_RTREE_QUAL& into);
2574 :
2575 : // ------ ------
2576 : bool
2577 78155958 : MSLane::appropriate(const MSVehicle* veh) const {
2578 78155958 : if (veh->getLaneChangeModel().isOpposite()) {
2579 : return false;
2580 : }
2581 78045097 : if (myEdge->isInternal()) {
2582 : return true;
2583 : }
2584 72707699 : if (veh->succEdge(1) == nullptr) {
2585 : assert((int)veh->getBestLanes().size() > veh->getLaneIndex());
2586 19638808 : if (veh->getBestLanes()[veh->getLaneIndex()].bestLaneOffset == 0) {
2587 : return true;
2588 : } else {
2589 : return false;
2590 : }
2591 : }
2592 53068891 : std::vector<MSLink*>::const_iterator link = succLinkSec(*veh, 1, *this, veh->getBestLanesContinuation());
2593 : return (link != myLinks.end());
2594 : }
2595 :
2596 :
2597 : void
2598 34291948 : MSLane::integrateNewVehicles() {
2599 34291948 : myNeedsCollisionCheck = true;
2600 : std::vector<MSVehicle*>& buffered = myVehBuffer.getContainer();
2601 34291948 : sort(buffered.begin(), buffered.end(), vehicle_position_sorter(this));
2602 51116186 : for (MSVehicle* const veh : buffered) {
2603 : assert(veh->getLane() == this);
2604 16824238 : myVehicles.insert(myVehicles.begin(), veh);
2605 16824238 : myBruttoVehicleLengthSum += veh->getVehicleType().getLengthWithGap();
2606 16824238 : myNettoVehicleLengthSum += veh->getVehicleType().getLength();
2607 : //if (true) std::cout << SIMTIME << " integrateNewVehicle lane=" << getID() << " veh=" << veh->getID() << " (on lane " << veh->getLane()->getID() << ") into lane=" << getID() << " myBrutto=" << myBruttoVehicleLengthSum << "\n";
2608 16824238 : myEdge->markDelayed();
2609 : }
2610 : buffered.clear();
2611 : myVehBuffer.unlock();
2612 : //std::cout << SIMTIME << " integrateNewVehicle lane=" << getID() << " myVehicles1=" << toString(myVehicles);
2613 34291948 : if (MSGlobals::gLateralResolution > 0 || myOpposite != nullptr) {
2614 19963315 : sort(myVehicles.begin(), myVehicles.end(), vehicle_natural_position_sorter(this));
2615 : }
2616 34291948 : sortPartialVehicles();
2617 : #ifdef DEBUG_VEHICLE_CONTAINER
2618 : if (DEBUG_COND) std::cout << SIMTIME << " integrateNewVehicle lane=" << getID()
2619 : << " vehicles=" << toString(myVehicles) << " partials=" << toString(myPartialVehicles) << "\n";
2620 : #endif
2621 34291948 : }
2622 :
2623 :
2624 : void
2625 120105821 : MSLane::sortPartialVehicles() {
2626 120105821 : if (myPartialVehicles.size() > 1) {
2627 2039876 : sort(myPartialVehicles.begin(), myPartialVehicles.end(), vehicle_natural_position_sorter(this));
2628 : }
2629 120105821 : }
2630 :
2631 :
2632 : void
2633 20725011 : MSLane::sortManeuverReservations() {
2634 20725011 : if (myManeuverReservations.size() > 1) {
2635 : #ifdef DEBUG_CONTEXT
2636 : if (DEBUG_COND) {
2637 : std::cout << "sortManeuverReservations on lane " << getID()
2638 : << "\nBefore sort: " << toString(myManeuverReservations) << std::endl;
2639 : }
2640 : #endif
2641 23916 : sort(myManeuverReservations.begin(), myManeuverReservations.end(), vehicle_natural_position_sorter(this));
2642 : #ifdef DEBUG_CONTEXT
2643 : if (DEBUG_COND) {
2644 : std::cout << "After sort: " << toString(myManeuverReservations) << std::endl;
2645 : }
2646 : #endif
2647 : }
2648 20725011 : }
2649 :
2650 :
2651 : bool
2652 7137622604 : MSLane::isInternal() const {
2653 7137622604 : return myEdge->isInternal();
2654 : }
2655 :
2656 :
2657 : bool
2658 54267475 : MSLane::isNormal() const {
2659 54267475 : return myEdge->isNormal();
2660 : }
2661 :
2662 :
2663 : bool
2664 101809157 : MSLane::isCrossing() const {
2665 101809157 : return myEdge->isCrossing();
2666 : }
2667 :
2668 :
2669 : bool
2670 845643 : MSLane::isPriorityCrossing() const {
2671 845643 : return isCrossing() && getIncomingLanes()[0].viaLink->getOffState() == LINKSTATE_MAJOR;
2672 : }
2673 :
2674 :
2675 : bool
2676 99461926 : MSLane::isWalkingArea() const {
2677 99461926 : return myEdge->isWalkingArea();
2678 : }
2679 :
2680 :
2681 : MSVehicle*
2682 1088656186 : MSLane::getLastFullVehicle() const {
2683 1088656186 : if (myVehicles.size() == 0) {
2684 : return nullptr;
2685 : }
2686 1070761744 : return myVehicles.front();
2687 : }
2688 :
2689 :
2690 : MSVehicle*
2691 490745 : MSLane::getFirstFullVehicle() const {
2692 490745 : if (myVehicles.size() == 0) {
2693 : return nullptr;
2694 : }
2695 334689 : return myVehicles.back();
2696 : }
2697 :
2698 :
2699 : MSVehicle*
2700 414214404 : MSLane::getLastAnyVehicle() const {
2701 : // all vehicles in myVehicles should have positions smaller or equal to
2702 : // those in myPartialVehicles (unless we're on a bidi-lane)
2703 414214404 : if (myVehicles.size() > 0) {
2704 324165970 : if (myBidiLane != nullptr && myPartialVehicles.size() > 0) {
2705 363562 : if (myVehicles.front()->getPositionOnLane() > myPartialVehicles.front()->getPositionOnLane(this)) {
2706 13136 : return myPartialVehicles.front();
2707 : }
2708 : }
2709 324152834 : return myVehicles.front();
2710 : }
2711 90048434 : if (myPartialVehicles.size() > 0) {
2712 4904940 : return myPartialVehicles.front();
2713 : }
2714 : return nullptr;
2715 : }
2716 :
2717 :
2718 : MSVehicle*
2719 137047 : MSLane::getFirstAnyVehicle() const {
2720 : MSVehicle* result = nullptr;
2721 137047 : if (myVehicles.size() > 0) {
2722 137047 : result = myVehicles.back();
2723 : }
2724 : if (myPartialVehicles.size() > 0
2725 137047 : && (result == nullptr || result->getPositionOnLane(this) < myPartialVehicles.back()->getPositionOnLane(this))) {
2726 135 : result = myPartialVehicles.back();
2727 : }
2728 137047 : return result;
2729 : }
2730 :
2731 :
2732 : std::vector<MSLink*>::const_iterator
2733 2020402066 : MSLane::succLinkSec(const SUMOVehicle& veh, int nRouteSuccs,
2734 : const MSLane& succLinkSource, const std::vector<MSLane*>& conts) {
2735 2020402066 : const MSEdge* nRouteEdge = veh.succEdge(nRouteSuccs);
2736 : // check whether the vehicle tried to look beyond its route
2737 2020402066 : if (nRouteEdge == nullptr) {
2738 : // return end (no succeeding link) if so
2739 : return succLinkSource.myLinks.end();
2740 : }
2741 : // if we are on an internal lane there should only be one link and it must be allowed
2742 1417962922 : if (succLinkSource.isInternal()) {
2743 : assert(succLinkSource.myLinks.size() == 1);
2744 : // could have been disallowed dynamically with a rerouter or via TraCI
2745 : // assert(succLinkSource.myLinks[0]->getLane()->allowsVehicleClass(veh.getVehicleType().getVehicleClass()));
2746 : return succLinkSource.myLinks.begin();
2747 : }
2748 : // a link may be used if
2749 : // 1) there is a destination lane ((*link)->getLane()!=0)
2750 : // 2) the destination lane belongs to the next edge in route ((*link)->getLane()->myEdge == nRouteEdge)
2751 : // 3) the destination lane allows the vehicle's class ((*link)->getLane()->allowsVehicleClass(veh.getVehicleClass()))
2752 :
2753 : // there should be a link which leads to the next desired lane our route in "conts" (built in "getBestLanes")
2754 : // "conts" stores the best continuations of our current lane
2755 : // we should never return an arbitrary link since this may cause collisions
2756 :
2757 1110757334 : if (nRouteSuccs < (int)conts.size()) {
2758 : // we go through the links in our list and return the matching one
2759 1240419092 : for (std::vector<MSLink*>::const_iterator link = succLinkSource.myLinks.begin(); link != succLinkSource.myLinks.end(); ++link) {
2760 1239682796 : if ((*link)->getLane() != nullptr && (*link)->getLane()->myEdge == nRouteEdge
2761 1105677272 : && (*link)->getLane()->allowsVehicleClass(veh.getVClass())
2762 2344918332 : && ((*link)->getViaLane() == nullptr || (*link)->getViaLane()->allowsVehicleClass(veh.getVClass()))) {
2763 : // we should use the link if it connects us to the best lane
2764 1105235450 : if ((*link)->getLane() == conts[nRouteSuccs]) {
2765 1090622496 : return link;
2766 : }
2767 : }
2768 : }
2769 : } else {
2770 : // the source lane is a dead end (no continuations exist)
2771 : return succLinkSource.myLinks.end();
2772 : }
2773 : // the only case where this should happen is for a disconnected route (deliberately ignored)
2774 : #ifdef DEBUG_NO_CONNECTION
2775 : // the "'" around the ids are missing intentionally in the message below because it slows messaging down, resulting in test timeouts
2776 : WRITE_WARNING("Could not find connection between lane " + succLinkSource.getID() + " and lane " + conts[nRouteSuccs]->getID() +
2777 : " for vehicle " + veh.getID() + ", time=" + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
2778 : #endif
2779 : return succLinkSource.myLinks.end();
2780 : }
2781 :
2782 :
2783 : const MSLink*
2784 629255547 : MSLane::getLinkTo(const MSLane* const target) const {
2785 629255547 : const bool internal = target->isInternal();
2786 840670482 : for (const MSLink* const l : myLinks) {
2787 806483751 : if ((internal && l->getViaLane() == target) || (!internal && l->getLane() == target)) {
2788 : return l;
2789 : }
2790 : }
2791 : return nullptr;
2792 : }
2793 :
2794 :
2795 : const MSLane*
2796 77318 : MSLane::getInternalFollowingLane(const MSLane* const target) const {
2797 126925 : for (const MSLink* const l : myLinks) {
2798 117903 : if (l->getLane() == target) {
2799 : return l->getViaLane();
2800 : }
2801 : }
2802 : return nullptr;
2803 : }
2804 :
2805 :
2806 : const MSLink*
2807 115422148 : MSLane::getEntryLink() const {
2808 115422148 : if (!isInternal()) {
2809 : return nullptr;
2810 : }
2811 : const MSLane* internal = this;
2812 115122716 : const MSLane* lane = this->getCanonicalPredecessorLane();
2813 : assert(lane != nullptr);
2814 116388340 : while (lane->isInternal()) {
2815 : internal = lane;
2816 1265624 : lane = lane->getCanonicalPredecessorLane();
2817 : assert(lane != nullptr);
2818 : }
2819 115122716 : return lane->getLinkTo(internal);
2820 : }
2821 :
2822 :
2823 : void
2824 1030 : MSLane::setMaxSpeed(const double val, const bool modified, const double jamThreshold) {
2825 1030 : myMaxSpeed = val;
2826 1030 : mySpeedModified = modified;
2827 1030 : myEdge->recalcCache();
2828 1030 : if (MSGlobals::gUseMesoSim) {
2829 222 : MESegment* first = MSGlobals::gMesoNet->getSegmentForEdge(*myEdge);
2830 1266 : while (first != nullptr) {
2831 1044 : first->setSpeed(val, SIMSTEP, jamThreshold, myIndex);
2832 : first = first->getNextSegment();
2833 : }
2834 : }
2835 1030 : }
2836 :
2837 :
2838 : void
2839 85 : MSLane::setFrictionCoefficient(double val) {
2840 85 : myFrictionCoefficient = val;
2841 85 : myEdge->recalcCache();
2842 85 : }
2843 :
2844 :
2845 : void
2846 15 : MSLane::setLength(double val) {
2847 15 : myLength = val;
2848 15 : myEdge->recalcCache();
2849 15 : }
2850 :
2851 :
2852 : void
2853 84671040 : MSLane::swapAfterLaneChange(SUMOTime) {
2854 : //if (getID() == "disabled_lane") std::cout << SIMTIME << " swapAfterLaneChange lane=" << getID() << " myVehicles=" << toString(myVehicles) << " myTmpVehicles=" << toString(myTmpVehicles) << "\n";
2855 84671040 : myVehicles = myTmpVehicles;
2856 : myTmpVehicles.clear();
2857 : // this needs to be done after finishing lane-changing for all lanes on the
2858 : // current edge (MSLaneChanger::updateLanes())
2859 84671040 : sortPartialVehicles();
2860 84671040 : if (MSGlobals::gSublane && getOpposite() != nullptr) {
2861 425444 : getOpposite()->sortPartialVehicles();
2862 : }
2863 84671040 : if (myBidiLane != nullptr) {
2864 717389 : myBidiLane->sortPartialVehicles();
2865 : }
2866 84671040 : }
2867 :
2868 :
2869 : MSVehicle*
2870 25732 : MSLane::removeVehicle(MSVehicle* remVehicle, MSMoveReminder::Notification notification, bool notify) {
2871 : assert(remVehicle->getLane() == this);
2872 54788 : for (MSLane::VehCont::iterator it = myVehicles.begin(); it < myVehicles.end(); it++) {
2873 54772 : if (remVehicle == *it) {
2874 25716 : if (notify) {
2875 17565 : remVehicle->leaveLane(notification);
2876 : }
2877 25716 : myVehicles.erase(it);
2878 25716 : myBruttoVehicleLengthSum -= remVehicle->getVehicleType().getLengthWithGap();
2879 25716 : myNettoVehicleLengthSum -= remVehicle->getVehicleType().getLength();
2880 25716 : break;
2881 : }
2882 : }
2883 25732 : return remVehicle;
2884 : }
2885 :
2886 :
2887 : MSLane*
2888 84740867 : MSLane::getParallelLane(int offset, bool includeOpposite) const {
2889 84740867 : return myEdge->parallelLane(this, offset, includeOpposite);
2890 : }
2891 :
2892 :
2893 : void
2894 2920605 : MSLane::addIncomingLane(MSLane* lane, MSLink* viaLink) {
2895 : IncomingLaneInfo ili;
2896 2920605 : ili.lane = lane;
2897 2920605 : ili.viaLink = viaLink;
2898 2920605 : ili.length = lane->getLength();
2899 2920605 : myIncomingLanes.push_back(ili);
2900 2920605 : }
2901 :
2902 :
2903 : void
2904 2920605 : MSLane::addApproachingLane(MSLane* lane, bool warnMultiCon) {
2905 2920605 : MSEdge* approachingEdge = &lane->getEdge();
2906 2920605 : if (myApproachingLanes.find(approachingEdge) == myApproachingLanes.end()) {
2907 2888975 : myApproachingLanes[approachingEdge] = std::vector<MSLane*>();
2908 31630 : } else if (!approachingEdge->isInternal() && warnMultiCon) {
2909 : // whenever a normal edge connects twice, there is a corresponding
2910 : // internal edge wich connects twice, one warning is sufficient
2911 18 : WRITE_WARNINGF(TL("Lane '%' is approached multiple times from edge '%'. This may cause collisions."),
2912 : getID(), approachingEdge->getID());
2913 : }
2914 2920605 : myApproachingLanes[approachingEdge].push_back(lane);
2915 2920605 : }
2916 :
2917 :
2918 : bool
2919 102791199 : MSLane::isApproachedFrom(MSLane* const lane, SUMOVehicleClass svc) {
2920 151977141 : for (MSLink* link : lane->getLinkCont()) {
2921 115508941 : if (link->getLane() == this && (link->getPermissions() & svc) == svc) {
2922 : return true;
2923 : }
2924 : }
2925 : return false;
2926 : }
2927 :
2928 :
2929 3331114 : double MSLane::getMissingRearGap(const MSVehicle* leader, double backOffset, double leaderSpeed) const {
2930 : // this follows the same logic as getFollowerOnConsecutive. we do a tree
2931 : // search and check for the vehicle with the largest missing rear gap within
2932 : // relevant range
2933 : double result = 0;
2934 : const double leaderDecel = leader->getCarFollowModel().getMaxDecel();
2935 3331114 : CLeaderDist followerInfo = getFollowersOnConsecutive(leader, backOffset, false)[0];
2936 3331114 : const MSVehicle* v = followerInfo.first;
2937 3331114 : if (v != nullptr) {
2938 3651 : result = v->getCarFollowModel().getSecureGap(v, leader, v->getSpeed(), leaderSpeed, leaderDecel) - followerInfo.second;
2939 : }
2940 3331114 : return result;
2941 : }
2942 :
2943 :
2944 : double
2945 339306453 : MSLane::getMaximumBrakeDist() const {
2946 339306453 : const MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
2947 339306453 : const double maxSpeed = getSpeedLimit() * vc.getMaxSpeedFactor();
2948 : // NOTE: For the euler update this is an upper bound on the actual braking distance (see ticket #860)
2949 : // impose a hard bound due to visibility / common sense to avoid unnecessary computation if there are strange vehicles in the fleet
2950 339306453 : const double minDecel = isRailway(myPermissions) ? vc.getMinDecelerationRail() : vc.getMinDeceleration();
2951 339306453 : return MIN2(maxSpeed * maxSpeed * 0.5 / minDecel + vc.getMaxMinGap(),
2952 339306453 : myPermissions == SVC_SHIP ? 10000.0 : 1000.0);
2953 : }
2954 :
2955 :
2956 : std::pair<MSVehicle* const, double>
2957 39705140 : MSLane::getLeader(const MSVehicle* veh, const double vehPos, const std::vector<MSLane*>& bestLaneConts, double dist, bool checkTmpVehicles) const {
2958 : // get the leading vehicle for (shadow) veh
2959 : // XXX this only works as long as all lanes of an edge have equal length
2960 : #ifdef DEBUG_CONTEXT
2961 : if (DEBUG_COND2(veh)) {
2962 : std::cout << " getLeader lane=" << getID() << " ego=" << veh->getID() << " vehs=" << toString(myVehicles) << " tmpVehs=" << toString(myTmpVehicles) << "\n";
2963 : }
2964 : #endif
2965 39705140 : if (checkTmpVehicles) {
2966 241380993 : for (VehCont::const_iterator last = myTmpVehicles.begin(); last != myTmpVehicles.end(); ++last) {
2967 : // XXX refactor leaderInfo to use a const vehicle all the way through the call hierarchy
2968 239035086 : MSVehicle* pred = (MSVehicle*)*last;
2969 239035086 : if (pred == veh) {
2970 : continue;
2971 : }
2972 : #ifdef DEBUG_CONTEXT
2973 : if (DEBUG_COND2(veh)) {
2974 : std::cout << std::setprecision(gPrecision) << " getLeader lane=" << getID() << " ego=" << veh->getID() << " egoPos=" << vehPos << " pred=" << pred->getID() << " predPos=" << pred->getPositionOnLane() << "\n";
2975 : }
2976 : #endif
2977 204191433 : if (pred->getPositionOnLane() >= vehPos) {
2978 32565533 : return std::pair<MSVehicle* const, double>(pred, pred->getBackPositionOnLane(this) - veh->getVehicleType().getMinGap() - vehPos);
2979 : }
2980 : }
2981 : } else {
2982 46834455 : for (AnyVehicleIterator last = anyVehiclesBegin(); last != anyVehiclesEnd(); ++last) {
2983 : // XXX refactor leaderInfo to use a const vehicle all the way through the call hierarchy
2984 50779562 : MSVehicle* pred = (MSVehicle*)*last;
2985 50779562 : if (pred == veh) {
2986 3787526 : continue;
2987 : }
2988 : #ifdef DEBUG_CONTEXT
2989 : if (DEBUG_COND2(veh)) {
2990 : std::cout << " getLeader lane=" << getID() << " ego=" << veh->getID() << " egoPos=" << vehPos
2991 : << " pred=" << pred->getID() << " predPos=" << pred->getPositionOnLane(this) << " predBack=" << pred->getBackPositionOnLane(this) << "\n";
2992 : }
2993 : #endif
2994 46992036 : if (pred->getPositionOnLane(this) >= vehPos) {
2995 4061903 : if (MSGlobals::gLaneChangeDuration > 0
2996 332912 : && pred->getLaneChangeModel().isOpposite()
2997 89310 : && !pred->getLaneChangeModel().isChangingLanes()
2998 4074720 : && pred->getLaneChangeModel().getShadowLane() == this) {
2999 : // skip non-overlapping shadow
3000 58398 : continue;
3001 : }
3002 3945107 : return std::pair<MSVehicle* const, double>(pred, pred->getBackPositionOnLane(this) - veh->getVehicleType().getMinGap() - vehPos);
3003 : }
3004 : }
3005 : }
3006 : // XXX from here on the code mirrors MSLaneChanger::getRealLeader
3007 3194500 : if (bestLaneConts.size() > 0) {
3008 2881071 : double seen = getLength() - vehPos;
3009 2881071 : double speed = veh->getSpeed();
3010 2881071 : if (dist < 0) {
3011 32541 : dist = veh->getCarFollowModel().brakeGap(speed) + veh->getVehicleType().getMinGap();
3012 : }
3013 : #ifdef DEBUG_CONTEXT
3014 : if (DEBUG_COND2(veh)) {
3015 : std::cout << " getLeader lane=" << getID() << " seen=" << seen << " dist=" << dist << "\n";
3016 : }
3017 : #endif
3018 2881071 : if (seen > dist) {
3019 1102933 : return std::pair<MSVehicle* const, double>(static_cast<MSVehicle*>(nullptr), -1);
3020 : }
3021 1778138 : return getLeaderOnConsecutive(dist, seen, speed, *veh, bestLaneConts);
3022 : } else {
3023 313429 : return std::make_pair(static_cast<MSVehicle*>(nullptr), -1);
3024 : }
3025 : }
3026 :
3027 :
3028 : std::pair<MSVehicle* const, double>
3029 39299628 : MSLane::getLeaderOnConsecutive(double dist, double seen, double speed, const MSVehicle& veh,
3030 : const std::vector<MSLane*>& bestLaneConts, bool considerCrossingFoes) const {
3031 : #ifdef DEBUG_CONTEXT
3032 : if (DEBUG_COND2(&veh)) {
3033 : std::cout << " getLeaderOnConsecutive lane=" << getID() << " ego=" << veh.getID() << " seen=" << seen << " dist=" << dist << " conts=" << toString(bestLaneConts) << "\n";
3034 : }
3035 : #endif
3036 39299628 : if (seen > dist && !isInternal()) {
3037 123372 : return std::make_pair(static_cast<MSVehicle*>(nullptr), -1);
3038 : }
3039 : int view = 1;
3040 : // loop over following lanes
3041 39176256 : if (myPartialVehicles.size() > 0) {
3042 : // XXX
3043 1132960 : MSVehicle* pred = myPartialVehicles.front();
3044 1132960 : const double gap = seen - (getLength() - pred->getBackPositionOnLane(this)) - veh.getVehicleType().getMinGap();
3045 : #ifdef DEBUG_CONTEXT
3046 : if (DEBUG_COND2(&veh)) {
3047 : std::cout << " predGap=" << gap << " partials=" << toString(myPartialVehicles) << "\n";
3048 : }
3049 : #endif
3050 : // make sure pred is really a leader and not doing continous lane-changing behind ego
3051 1132960 : if (gap > 0) {
3052 767687 : return std::pair<MSVehicle* const, double>(pred, gap);
3053 : }
3054 : }
3055 : #ifdef DEBUG_CONTEXT
3056 : if (DEBUG_COND2(&veh)) {
3057 : gDebugFlag1 = true;
3058 : }
3059 : #endif
3060 : const MSLane* nextLane = this;
3061 : do {
3062 54990590 : nextLane->getVehiclesSecure(); // lock against running sim when called from GUI for time gap coloring
3063 : // get the next link used
3064 54990590 : std::vector<MSLink*>::const_iterator link = succLinkSec(veh, view, *nextLane, bestLaneConts);
3065 54990590 : if (nextLane->isLinkEnd(link) && view < veh.getRoute().size() - veh.getRoutePosition()) {
3066 5165063 : const MSEdge* nextEdge = *(veh.getCurrentRouteEdge() + view);
3067 5165063 : if (nextEdge->getNumLanes() == 1) {
3068 : // lanes are unambiguous on the next route edge, continue beyond bestLaneConts
3069 5590973 : for (link = nextLane->getLinkCont().begin(); link < nextLane->getLinkCont().end(); link++) {
3070 3479568 : if ((*link)->getLane() == nextEdge->getLanes().front()) {
3071 : break;
3072 : }
3073 : }
3074 : }
3075 : }
3076 54990590 : if (nextLane->isLinkEnd(link)) {
3077 : #ifdef DEBUG_CONTEXT
3078 : if (DEBUG_COND2(&veh)) {
3079 : std::cout << " cannot continue after nextLane=" << nextLane->getID() << "\n";
3080 : }
3081 : #endif
3082 14738333 : nextLane->releaseVehicles();
3083 14738333 : break;
3084 : }
3085 : // check for link leaders
3086 40252257 : const bool laneChanging = veh.getLane() != this;
3087 40252257 : const MSLink::LinkLeaders linkLeaders = (*link)->getLeaderInfo(&veh, seen);
3088 40252256 : nextLane->releaseVehicles();
3089 40252256 : if (linkLeaders.size() > 0) {
3090 : std::pair<MSVehicle*, double> result;
3091 : double shortestGap = std::numeric_limits<double>::max();
3092 2366188 : for (auto ll : linkLeaders) {
3093 : double gap = ll.vehAndGap.second;
3094 : MSVehicle* lVeh = ll.vehAndGap.first;
3095 1311549 : if (lVeh != nullptr) {
3096 : // leader is a vehicle, not a pedestrian
3097 1215769 : gap += lVeh->getCarFollowModel().brakeGap(lVeh->getSpeed(), lVeh->getCarFollowModel().getMaxDecel(), 0);
3098 : }
3099 : #ifdef DEBUG_CONTEXT
3100 : if (DEBUG_COND2(&veh)) {
3101 : std::cout << " linkLeader candidate " << Named::getIDSecure(lVeh)
3102 : << " isLeader=" << veh.isLeader(*link, lVeh, ll.vehAndGap.second)
3103 : << " gap=" << ll.vehAndGap.second
3104 : << " gap+brakeing=" << gap
3105 : << "\n";
3106 : }
3107 : #endif
3108 : // skip vehicles which do not share the outgoing edge (to get only real leader vehicles in TraCI #13842)
3109 1311549 : if (!considerCrossingFoes && !ll.sameTarget()) {
3110 35 : continue;
3111 : }
3112 : // in the context of lane-changing, all candidates are leaders
3113 1311514 : if (lVeh != nullptr && !laneChanging && !veh.isLeader(*link, lVeh, ll.vehAndGap.second)) {
3114 11668 : continue;
3115 : }
3116 1299846 : if (gap < shortestGap) {
3117 : shortestGap = gap;
3118 1097043 : if (ll.vehAndGap.second < 0 && !MSGlobals::gComputeLC) {
3119 : // can always continue up to the stop line or crossing point
3120 : // @todo: figure out whether this should also impact lane changing
3121 74089 : ll.vehAndGap.second = MAX2(seen - nextLane->getLength(), ll.distToCrossing);
3122 : }
3123 : result = ll.vehAndGap;
3124 : }
3125 : }
3126 1054639 : if (shortestGap != std::numeric_limits<double>::max()) {
3127 : #ifdef DEBUG_CONTEXT
3128 : if (DEBUG_COND2(&veh)) {
3129 : std::cout << " found linkLeader after nextLane=" << nextLane->getID() << "\n";
3130 : gDebugFlag1 = false;
3131 : }
3132 : #endif
3133 1045456 : return result;
3134 : }
3135 : }
3136 39206800 : bool nextInternal = (*link)->getViaLane() != nullptr;
3137 : nextLane = (*link)->getViaLaneOrLane();
3138 19432492 : if (nextLane == nullptr) {
3139 : break;
3140 : }
3141 39206800 : nextLane->getVehiclesSecure(); // lock against running sim when called from GUI for time gap coloring
3142 39206800 : MSVehicle* leader = nextLane->getLastAnyVehicle();
3143 39206800 : if (leader != nullptr) {
3144 : #ifdef DEBUG_CONTEXT
3145 : if (DEBUG_COND2(&veh)) {
3146 : std::cout << " found leader " << leader->getID() << " on nextLane=" << nextLane->getID() << "\n";
3147 : }
3148 : #endif
3149 15340497 : const double leaderDist = seen + leader->getBackPositionOnLane(nextLane) - veh.getVehicleType().getMinGap();
3150 15340497 : nextLane->releaseVehicles();
3151 15340497 : return std::make_pair(leader, leaderDist);
3152 : }
3153 23866303 : nextLane->releaseVehicles();
3154 23866303 : if (nextLane->getVehicleMaxSpeed(&veh) < speed) {
3155 2359567 : dist = veh.getCarFollowModel().brakeGap(nextLane->getVehicleMaxSpeed(&veh));
3156 : }
3157 23866303 : seen += nextLane->getLength();
3158 23866303 : if (!nextInternal) {
3159 8050979 : view++;
3160 : }
3161 56834277 : } while (seen <= dist || nextLane->isInternal());
3162 : #ifdef DEBUG_CONTEXT
3163 : gDebugFlag1 = false;
3164 : #endif
3165 22022615 : return std::make_pair(static_cast<MSVehicle*>(nullptr), -1);
3166 : }
3167 :
3168 :
3169 : std::pair<MSVehicle* const, double>
3170 106689 : MSLane::getCriticalLeader(double dist, double seen, double speed, const MSVehicle& veh) const {
3171 : #ifdef DEBUG_CONTEXT
3172 : if (DEBUG_COND2(&veh)) {
3173 : std::cout << SIMTIME << " getCriticalLeader. lane=" << getID() << " veh=" << veh.getID() << "\n";
3174 : }
3175 : #endif
3176 106689 : const std::vector<MSLane*>& bestLaneConts = veh.getBestLanesContinuation(this);
3177 : std::pair<MSVehicle*, double> result = std::make_pair(static_cast<MSVehicle*>(nullptr), -1);
3178 : double safeSpeed = std::numeric_limits<double>::max();
3179 : int view = 1;
3180 : // loop over following lanes
3181 : // @note: we don't check the partial occupator for this lane since it was
3182 : // already checked in MSLaneChanger::getRealLeader()
3183 : const MSLane* nextLane = this;
3184 213378 : SUMOTime arrivalTime = MSNet::getInstance()->getCurrentTimeStep() + TIME2STEPS(seen / MAX2(speed, NUMERICAL_EPS));
3185 : do {
3186 : // get the next link used
3187 190453 : std::vector<MSLink*>::const_iterator link = succLinkSec(veh, view, *nextLane, bestLaneConts);
3188 187672 : if (nextLane->isLinkEnd(link) || !(*link)->opened(arrivalTime, speed, speed, veh.getVehicleType().getLength(),
3189 373148 : veh.getImpatience(), veh.getCarFollowModel().getMaxDecel(), 0, veh.getLateralPositionOnLane(), nullptr, false, &veh) || (*link)->haveRed()) {
3190 7758 : return result;
3191 : }
3192 : // check for link leaders
3193 : #ifdef DEBUG_CONTEXT
3194 : if (DEBUG_COND2(&veh)) {
3195 : gDebugFlag1 = true; // See MSLink::getLeaderInfo
3196 : }
3197 : #endif
3198 182695 : const MSLink::LinkLeaders linkLeaders = (*link)->getLeaderInfo(&veh, seen);
3199 : #ifdef DEBUG_CONTEXT
3200 : if (DEBUG_COND2(&veh)) {
3201 : gDebugFlag1 = false; // See MSLink::getLeaderInfo
3202 : }
3203 : #endif
3204 213723 : for (MSLink::LinkLeaders::const_iterator it = linkLeaders.begin(); it != linkLeaders.end(); ++it) {
3205 31028 : const MSVehicle* leader = (*it).vehAndGap.first;
3206 31028 : if (leader != nullptr && leader != result.first) {
3207 : // XXX ignoring pedestrians here!
3208 : // XXX ignoring the fact that the link leader may alread by following us
3209 : // XXX ignoring the fact that we may drive up to the crossing point
3210 30954 : double tmpSpeed = safeSpeed;
3211 30954 : veh.adaptToJunctionLeader((*it).vehAndGap, seen, nullptr, nextLane, tmpSpeed, tmpSpeed, (*it).distToCrossing);
3212 : #ifdef DEBUG_CONTEXT
3213 : if (DEBUG_COND2(&veh)) {
3214 : std::cout << " linkLeader=" << leader->getID() << " gap=" << result.second << " tmpSpeed=" << tmpSpeed << " safeSpeed=" << safeSpeed << "\n";
3215 : }
3216 : #endif
3217 30954 : if (tmpSpeed < safeSpeed) {
3218 : safeSpeed = tmpSpeed;
3219 : result = (*it).vehAndGap;
3220 : }
3221 : }
3222 : }
3223 182695 : bool nextInternal = (*link)->getViaLane() != nullptr;
3224 : nextLane = (*link)->getViaLaneOrLane();
3225 102845 : if (nextLane == nullptr) {
3226 : break;
3227 : }
3228 182695 : MSVehicle* leader = nextLane->getLastAnyVehicle();
3229 182695 : if (leader != nullptr && leader != result.first) {
3230 103915 : const double gap = seen + leader->getBackPositionOnLane(nextLane) - veh.getVehicleType().getMinGap();
3231 103915 : const double tmpSpeed = veh.getCarFollowModel().insertionFollowSpeed(&veh, speed, gap, leader->getSpeed(), leader->getCarFollowModel().getMaxDecel(), leader);
3232 103915 : if (tmpSpeed < safeSpeed) {
3233 : safeSpeed = tmpSpeed;
3234 : result = std::make_pair(leader, gap);
3235 : }
3236 : }
3237 182695 : if (nextLane->getVehicleMaxSpeed(&veh) < speed) {
3238 28054 : dist = veh.getCarFollowModel().brakeGap(nextLane->getVehicleMaxSpeed(&veh));
3239 : }
3240 182695 : seen += nextLane->getLength();
3241 182695 : if (seen <= dist) {
3242 : // delaying the update of arrivalTime and making it conditional to avoid possible integer overflows
3243 37685 : arrivalTime += TIME2STEPS(nextLane->getLength() / MAX2(speed, NUMERICAL_EPS));
3244 : }
3245 182695 : if (!nextInternal) {
3246 102845 : view++;
3247 : }
3248 266459 : } while (seen <= dist || nextLane->isInternal());
3249 98931 : return result;
3250 : }
3251 :
3252 :
3253 : MSLane*
3254 1315643476 : MSLane::getLogicalPredecessorLane() const {
3255 1315643476 : if (myLogicalPredecessorLane == nullptr) {
3256 5413752 : MSEdgeVector pred = myEdge->getPredecessors();
3257 : // get only those edges which connect to this lane
3258 10835021 : for (MSEdgeVector::iterator i = pred.begin(); i != pred.end();) {
3259 5421269 : std::vector<IncomingLaneInfo>::const_iterator j = find_if(myIncomingLanes.begin(), myIncomingLanes.end(), edge_finder(*i));
3260 5421269 : if (j == myIncomingLanes.end()) {
3261 : i = pred.erase(i);
3262 : } else {
3263 : ++i;
3264 : }
3265 : }
3266 : // get the lane with the "straightest" connection
3267 5413752 : if (pred.size() != 0) {
3268 2054912 : std::sort(pred.begin(), pred.end(), by_connections_to_sorter(&getEdge()));
3269 1027456 : MSEdge* best = *pred.begin();
3270 1027456 : std::vector<IncomingLaneInfo>::const_iterator j = find_if(myIncomingLanes.begin(), myIncomingLanes.end(), edge_finder(best));
3271 1027456 : myLogicalPredecessorLane = j->lane;
3272 : }
3273 5413752 : }
3274 1315643476 : return myLogicalPredecessorLane;
3275 : }
3276 :
3277 :
3278 : const MSLane*
3279 980135256 : MSLane::getNormalPredecessorLane() const {
3280 2119896111 : if (isInternal()) {
3281 1139760855 : return getLogicalPredecessorLane()->getNormalPredecessorLane();
3282 : } else {
3283 : return this;
3284 : }
3285 : }
3286 :
3287 :
3288 : const MSLane*
3289 591869 : MSLane::getNormalSuccessorLane() const {
3290 813309 : if (isInternal()) {
3291 221440 : return getCanonicalSuccessorLane()->getNormalSuccessorLane();
3292 : } else {
3293 : return this;
3294 : }
3295 : }
3296 :
3297 :
3298 : MSLane*
3299 84052 : MSLane::getLogicalPredecessorLane(const MSEdge& fromEdge) const {
3300 147341 : for (const IncomingLaneInfo& cand : myIncomingLanes) {
3301 99292 : if (&(cand.lane->getEdge()) == &fromEdge) {
3302 : return cand.lane;
3303 : }
3304 : }
3305 : return nullptr;
3306 : }
3307 :
3308 :
3309 : MSLane*
3310 118166382 : MSLane::getCanonicalPredecessorLane() const {
3311 118166382 : if (myCanonicalPredecessorLane != nullptr) {
3312 : return myCanonicalPredecessorLane;
3313 : }
3314 992756 : if (myIncomingLanes.empty()) {
3315 : return nullptr;
3316 : }
3317 : // myCanonicalPredecessorLane has not yet been determined and there exist incoming lanes
3318 : // get the lane with the priorized (or if this does not apply the "straightest") connection
3319 992424 : const auto bestLane = std::min_element(myIncomingLanes.begin(), myIncomingLanes.end(), incoming_lane_priority_sorter(this));
3320 : {
3321 : #ifdef HAVE_FOX
3322 992424 : ScopedLocker<> lock(myLeaderInfoMutex, MSGlobals::gNumSimThreads > 1);
3323 : #endif
3324 992424 : myCanonicalPredecessorLane = bestLane->lane;
3325 : }
3326 : #ifdef DEBUG_LANE_SORTER
3327 : std::cout << "\nBest predecessor lane for lane '" << myID << "': '" << myCanonicalPredecessorLane->getID() << "'" << std::endl;
3328 : #endif
3329 992424 : return myCanonicalPredecessorLane;
3330 : }
3331 :
3332 :
3333 : MSLane*
3334 2554795 : MSLane::getCanonicalSuccessorLane() const {
3335 2554795 : if (myCanonicalSuccessorLane != nullptr) {
3336 : return myCanonicalSuccessorLane;
3337 : }
3338 68435 : if (myLinks.empty()) {
3339 : return nullptr;
3340 : }
3341 : // myCanonicalSuccessorLane has not yet been determined and there exist outgoing links
3342 8000 : std::vector<MSLink*> candidateLinks = myLinks;
3343 : // get the lane with the priorized (or if this does not apply the "straightest") connection
3344 16000 : std::sort(candidateLinks.begin(), candidateLinks.end(), outgoing_lane_priority_sorter(this));
3345 8000 : MSLane* best = (*candidateLinks.begin())->getViaLaneOrLane();
3346 : #ifdef DEBUG_LANE_SORTER
3347 : std::cout << "\nBest successor lane for lane '" << myID << "': '" << best->getID() << "'" << std::endl;
3348 : #endif
3349 8000 : myCanonicalSuccessorLane = best;
3350 : return myCanonicalSuccessorLane;
3351 8000 : }
3352 :
3353 :
3354 : LinkState
3355 4730107 : MSLane::getIncomingLinkState() const {
3356 4730107 : const MSLane* const pred = getLogicalPredecessorLane();
3357 4730107 : if (pred == nullptr) {
3358 : return LINKSTATE_DEADEND;
3359 : } else {
3360 4730107 : return pred->getLinkTo(this)->getState();
3361 : }
3362 : }
3363 :
3364 :
3365 : const std::vector<std::pair<const MSLane*, const MSEdge*> >
3366 290339 : MSLane::getOutgoingViaLanes() const {
3367 : std::vector<std::pair<const MSLane*, const MSEdge*> > result;
3368 652900 : for (const MSLink* link : myLinks) {
3369 : assert(link->getLane() != nullptr);
3370 725122 : result.push_back(std::make_pair(link->getLane(), link->getViaLane() == nullptr ? nullptr : &link->getViaLane()->getEdge()));
3371 : }
3372 290339 : return result;
3373 0 : }
3374 :
3375 : std::vector<const MSLane*>
3376 76 : MSLane::getNormalIncomingLanes() const {
3377 76 : std::vector<const MSLane*> result = {};
3378 302 : for (std::map<MSEdge*, std::vector<MSLane*> >::const_iterator it = myApproachingLanes.begin(); it != myApproachingLanes.end(); ++it) {
3379 488 : for (std::vector<MSLane*>::const_iterator it_lane = (*it).second.begin(); it_lane != (*it).second.end(); ++it_lane) {
3380 262 : if (!((*it_lane)->isInternal())) {
3381 146 : result.push_back(*it_lane);
3382 : }
3383 : }
3384 : }
3385 76 : return result;
3386 0 : }
3387 :
3388 :
3389 : void
3390 1138173 : MSLane::leftByLaneChange(MSVehicle* v) {
3391 1138173 : myBruttoVehicleLengthSum -= v->getVehicleType().getLengthWithGap();
3392 1138173 : myNettoVehicleLengthSum -= v->getVehicleType().getLength();
3393 1138173 : }
3394 :
3395 :
3396 : void
3397 1093860 : MSLane::enteredByLaneChange(MSVehicle* v) {
3398 1093860 : myBruttoVehicleLengthSum += v->getVehicleType().getLengthWithGap();
3399 1093860 : myNettoVehicleLengthSum += v->getVehicleType().getLength();
3400 1093860 : }
3401 :
3402 :
3403 : int
3404 0 : MSLane::getCrossingIndex() const {
3405 0 : for (std::vector<MSLink*>::const_iterator i = myLinks.begin(); i != myLinks.end(); ++i) {
3406 0 : if ((*i)->getLane()->isCrossing()) {
3407 0 : return (int)(i - myLinks.begin());
3408 : }
3409 : }
3410 : return -1;
3411 : }
3412 :
3413 : // ------------ Current state retrieval
3414 : double
3415 2107089454 : MSLane::getFractionalVehicleLength(bool brutto) const {
3416 : double sum = 0;
3417 2107089454 : if (myPartialVehicles.size() > 0) {
3418 415236172 : const MSLane* bidi = getBidiLane();
3419 847983904 : for (MSVehicle* cand : myPartialVehicles) {
3420 432747732 : if (MSGlobals::gSublane && cand->getLaneChangeModel().getShadowLane() == this) {
3421 25318111 : continue;
3422 : }
3423 407429621 : if (cand->getLane() == bidi) {
3424 237728 : sum += (brutto ? cand->getVehicleType().getLengthWithGap() : cand->getVehicleType().getLength());
3425 : } else {
3426 407310757 : sum += myLength - cand->getBackPositionOnLane(this);
3427 : }
3428 : }
3429 : }
3430 2107089454 : return sum;
3431 : }
3432 :
3433 : double
3434 2107032734 : MSLane::getBruttoOccupancy() const {
3435 2107032734 : getVehiclesSecure();
3436 2107032734 : double fractions = getFractionalVehicleLength(true);
3437 2107032734 : if (myVehicles.size() != 0) {
3438 1564741929 : MSVehicle* lastVeh = myVehicles.front();
3439 1564741929 : if (lastVeh->getPositionOnLane() < lastVeh->getVehicleType().getLength()) {
3440 49190378 : fractions -= (lastVeh->getVehicleType().getLength() - lastVeh->getPositionOnLane());
3441 : }
3442 : }
3443 2107032734 : releaseVehicles();
3444 2107032734 : return MIN2(1., (myBruttoVehicleLengthSum + fractions) / myLength);
3445 : }
3446 :
3447 :
3448 : double
3449 56720 : MSLane::getNettoOccupancy() const {
3450 56720 : getVehiclesSecure();
3451 56720 : double fractions = getFractionalVehicleLength(false);
3452 56720 : if (myVehicles.size() != 0) {
3453 504 : MSVehicle* lastVeh = myVehicles.front();
3454 504 : if (lastVeh->getPositionOnLane() < lastVeh->getVehicleType().getLength()) {
3455 4 : fractions -= (lastVeh->getVehicleType().getLength() - lastVeh->getPositionOnLane());
3456 : }
3457 : }
3458 56720 : releaseVehicles();
3459 56720 : return (myNettoVehicleLengthSum + fractions) / myLength;
3460 : }
3461 :
3462 :
3463 : double
3464 46 : MSLane::getWaitingSeconds() const {
3465 46 : if (myVehicles.size() == 0) {
3466 : return 0;
3467 : }
3468 : double wtime = 0;
3469 48 : for (VehCont::const_iterator i = myVehicles.begin(); i != myVehicles.end(); ++i) {
3470 24 : wtime += (*i)->getWaitingSeconds();
3471 : }
3472 : return wtime;
3473 : }
3474 :
3475 :
3476 : double
3477 166613601 : MSLane::getMeanSpeed() const {
3478 166613601 : if (myVehicles.size() == 0) {
3479 138023627 : return myMaxSpeed;
3480 : }
3481 : double v = 0;
3482 : int numVehs = 0;
3483 174528143 : for (const MSVehicle* const veh : getVehiclesSecure()) {
3484 145938169 : if (!veh->isStopped() || !myEdge->hasLaneChanger()) {
3485 145429841 : v += veh->getSpeed();
3486 145429841 : numVehs++;
3487 : }
3488 : }
3489 28589974 : releaseVehicles();
3490 28589974 : if (numVehs == 0) {
3491 243431 : return myMaxSpeed;
3492 : }
3493 28346543 : return v / numVehs;
3494 : }
3495 :
3496 :
3497 : double
3498 2095 : MSLane::getMeanSpeedBike() const {
3499 : // @note: redundant code with getMeanSpeed to avoid extra checks in a function that is called very often
3500 2095 : if (myVehicles.size() == 0) {
3501 1480 : return myMaxSpeed;
3502 : }
3503 : double v = 0;
3504 : int numBikes = 0;
3505 2260 : for (MSVehicle* veh : getVehiclesSecure()) {
3506 1645 : if (veh->getVClass() == SVC_BICYCLE) {
3507 1150 : v += veh->getSpeed();
3508 1150 : numBikes++;
3509 : }
3510 : }
3511 : double ret;
3512 615 : if (numBikes > 0) {
3513 335 : ret = v / (double) myVehicles.size();
3514 : } else {
3515 280 : ret = myMaxSpeed;
3516 : }
3517 615 : releaseVehicles();
3518 615 : return ret;
3519 : }
3520 :
3521 :
3522 : double
3523 56723 : MSLane::getHarmonoise_NoiseEmissions() const {
3524 : double ret = 0;
3525 56723 : const MSLane::VehCont& vehs = getVehiclesSecure();
3526 56723 : if (vehs.size() == 0) {
3527 56223 : releaseVehicles();
3528 56223 : return 0;
3529 : }
3530 1216 : for (MSLane::VehCont::const_iterator i = vehs.begin(); i != vehs.end(); ++i) {
3531 716 : double sv = (*i)->getHarmonoise_NoiseEmissions();
3532 716 : ret += (double) pow(10., (sv / 10.));
3533 : }
3534 500 : releaseVehicles();
3535 500 : return HelpersHarmonoise::sum(ret);
3536 : }
3537 :
3538 :
3539 : int
3540 275513 : MSLane::vehicle_position_sorter::operator()(MSVehicle* v1, MSVehicle* v2) const {
3541 275513 : const double pos1 = v1->getBackPositionOnLane(myLane);
3542 275513 : const double pos2 = v2->getBackPositionOnLane(myLane);
3543 275513 : if (pos1 != pos2) {
3544 271565 : return pos1 > pos2;
3545 : } else {
3546 3948 : return v1->getNumericalID() > v2->getNumericalID();
3547 : }
3548 : }
3549 :
3550 :
3551 : int
3552 359715786 : MSLane::vehicle_natural_position_sorter::operator()(MSVehicle* v1, MSVehicle* v2) const {
3553 359715786 : const double pos1 = v1->getBackPositionOnLane(myLane);
3554 359715786 : const double pos2 = v2->getBackPositionOnLane(myLane);
3555 359715786 : if (pos1 != pos2) {
3556 359169600 : return pos1 < pos2;
3557 : } else {
3558 546186 : return v1->getLateralPositionOnLane() < v2->getLateralPositionOnLane();
3559 : }
3560 : }
3561 :
3562 :
3563 1027456 : MSLane::by_connections_to_sorter::by_connections_to_sorter(const MSEdge* const e) :
3564 1027456 : myEdge(e),
3565 1027456 : myLaneDir(e->getLanes()[0]->getShape().angleAt2D(0)) {
3566 1027456 : }
3567 :
3568 :
3569 : int
3570 22149 : MSLane::by_connections_to_sorter::operator()(const MSEdge* const e1, const MSEdge* const e2) const {
3571 : // std::cout << "\nby_connections_to_sorter()";
3572 :
3573 22149 : const std::vector<MSLane*>* ae1 = e1->allowedLanes(*myEdge);
3574 22149 : const std::vector<MSLane*>* ae2 = e2->allowedLanes(*myEdge);
3575 : double s1 = 0;
3576 22149 : if (ae1 != nullptr && ae1->size() != 0) {
3577 : // std::cout << "\nsize 1 = " << ae1->size()
3578 : // << " anglediff 1 = " << fabs(GeomHelper::angleDiff((*ae1)[0]->getShape().angleAt2D(0), myLaneDir)) / M_PI / 2.
3579 : // << "\nallowed lanes: ";
3580 : // for (std::vector<MSLane*>::const_iterator j = ae1->begin(); j != ae1->end(); ++j){
3581 : // std::cout << "\n" << (*j)->getID();
3582 : // }
3583 22149 : s1 = (double) ae1->size() + fabs(GeomHelper::angleDiff((*ae1)[0]->getShape().angleAt2D(0), myLaneDir)) / M_PI / 2.;
3584 : }
3585 : double s2 = 0;
3586 22149 : if (ae2 != nullptr && ae2->size() != 0) {
3587 : // std::cout << "\nsize 2 = " << ae2->size()
3588 : // << " anglediff 2 = " << fabs(GeomHelper::angleDiff((*ae2)[0]->getShape().angleAt2D(0), myLaneDir)) / M_PI / 2.
3589 : // << "\nallowed lanes: ";
3590 : // for (std::vector<MSLane*>::const_iterator j = ae2->begin(); j != ae2->end(); ++j){
3591 : // std::cout << "\n" << (*j)->getID();
3592 : // }
3593 22149 : s2 = (double) ae2->size() + fabs(GeomHelper::angleDiff((*ae2)[0]->getShape().angleAt2D(0), myLaneDir)) / M_PI / 2.;
3594 : }
3595 :
3596 : // std::cout << "\ne1 = " << e1->getID() << " e2 = " << e2->getID()
3597 : // << "\ns1 = " << s1 << " s2 = " << s2
3598 : // << std::endl;
3599 :
3600 22149 : return s1 < s2;
3601 : }
3602 :
3603 :
3604 992424 : MSLane::incoming_lane_priority_sorter::incoming_lane_priority_sorter(const MSLane* const targetLane) :
3605 992424 : myLane(targetLane),
3606 992424 : myLaneDir(targetLane->getShape().angleAt2D(0)) {}
3607 :
3608 : int
3609 1370 : MSLane::incoming_lane_priority_sorter::operator()(const IncomingLaneInfo& laneInfo1, const IncomingLaneInfo& laneInfo2) const {
3610 1370 : const MSLane* noninternal1 = laneInfo1.lane;
3611 3357 : while (noninternal1->isInternal()) {
3612 : assert(noninternal1->getIncomingLanes().size() == 1);
3613 1987 : noninternal1 = noninternal1->getIncomingLanes()[0].lane;
3614 : }
3615 1370 : MSLane* noninternal2 = laneInfo2.lane;
3616 3033 : while (noninternal2->isInternal()) {
3617 : assert(noninternal2->getIncomingLanes().size() == 1);
3618 1663 : noninternal2 = noninternal2->getIncomingLanes()[0].lane;
3619 : }
3620 :
3621 1370 : const MSLink* link1 = noninternal1->getLinkTo(myLane);
3622 1370 : const MSLink* link2 = noninternal2->getLinkTo(myLane);
3623 :
3624 : #ifdef DEBUG_LANE_SORTER
3625 : std::cout << "\nincoming_lane_priority sorter()\n"
3626 : << "noninternal predecessor for lane '" << laneInfo1.lane->getID()
3627 : << "': '" << noninternal1->getID() << "'\n"
3628 : << "noninternal predecessor for lane '" << laneInfo2.lane->getID()
3629 : << "': '" << noninternal2->getID() << "'\n";
3630 : #endif
3631 :
3632 : assert(laneInfo1.lane->isInternal() || link1 == laneInfo1.viaLink);
3633 : assert(link1 != 0);
3634 : assert(link2 != 0);
3635 :
3636 : // check priority between links
3637 : bool priorized1 = true;
3638 : bool priorized2 = true;
3639 :
3640 : #ifdef DEBUG_LANE_SORTER
3641 : std::cout << "FoeLinks of '" << noninternal1->getID() << "'" << std::endl;
3642 : #endif
3643 2705 : for (const MSLink* const foeLink : link1->getFoeLinks()) {
3644 : #ifdef DEBUG_LANE_SORTER
3645 : std::cout << foeLink->getLaneBefore()->getID() << std::endl;
3646 : #endif
3647 2408 : if (foeLink == link2) {
3648 : priorized1 = false;
3649 : break;
3650 : }
3651 : }
3652 :
3653 : #ifdef DEBUG_LANE_SORTER
3654 : std::cout << "FoeLinks of '" << noninternal2->getID() << "'" << std::endl;
3655 : #endif
3656 3714 : for (const MSLink* const foeLink : link2->getFoeLinks()) {
3657 : #ifdef DEBUG_LANE_SORTER
3658 : std::cout << foeLink->getLaneBefore()->getID() << std::endl;
3659 : #endif
3660 : // either link1 is priorized, or it should not appear in link2's foes
3661 2858 : if (foeLink == link1) {
3662 : priorized2 = false;
3663 : break;
3664 : }
3665 : }
3666 : // if one link is subordinate, the other must be priorized (except for
3667 : // traffic lights where mutual response is permitted to handle stuck-on-red
3668 : // situation)
3669 1370 : if (priorized1 != priorized2) {
3670 1145 : return priorized1;
3671 : }
3672 :
3673 : // both are priorized, compare angle difference
3674 225 : double d1 = fabs(GeomHelper::angleDiff(noninternal1->getShape().angleAt2D(0), myLaneDir));
3675 225 : double d2 = fabs(GeomHelper::angleDiff(noninternal2->getShape().angleAt2D(0), myLaneDir));
3676 :
3677 225 : return d2 > d1;
3678 : }
3679 :
3680 :
3681 :
3682 8000 : MSLane::outgoing_lane_priority_sorter::outgoing_lane_priority_sorter(const MSLane* const sourceLane) :
3683 8000 : myLaneDir(sourceLane->getShape().angleAt2D(0)) {}
3684 :
3685 : int
3686 13641 : MSLane::outgoing_lane_priority_sorter::operator()(const MSLink* link1, const MSLink* link2) const {
3687 : const MSLane* target1 = link1->getLane();
3688 : const MSLane* target2 = link2->getLane();
3689 13641 : if (target2 == nullptr) {
3690 : return true;
3691 : }
3692 13641 : if (target1 == nullptr) {
3693 : return false;
3694 : }
3695 :
3696 : #ifdef DEBUG_LANE_SORTER
3697 : std::cout << "\noutgoing_lane_priority sorter()\n"
3698 : << "noninternal successors for lane '" << myLane->getID()
3699 : << "': '" << target1->getID() << "' and "
3700 : << "'" << target2->getID() << "'\n";
3701 : #endif
3702 :
3703 : // priority of targets
3704 : int priority1 = target1->getEdge().getPriority();
3705 : int priority2 = target2->getEdge().getPriority();
3706 :
3707 13641 : if (priority1 != priority2) {
3708 123 : return priority1 > priority2;
3709 : }
3710 :
3711 : // if priority of targets coincides, use angle difference
3712 :
3713 : // both are priorized, compare angle difference
3714 13518 : double d1 = fabs(GeomHelper::angleDiff(target1->getShape().angleAt2D(0), myLaneDir));
3715 13518 : double d2 = fabs(GeomHelper::angleDiff(target2->getShape().angleAt2D(0), myLaneDir));
3716 :
3717 13518 : return d2 > d1;
3718 : }
3719 :
3720 : void
3721 6287 : MSLane::addParking(MSBaseVehicle* veh) {
3722 : myParkingVehicles.insert(veh);
3723 6287 : }
3724 :
3725 :
3726 : void
3727 18380 : MSLane::removeParking(MSBaseVehicle* veh) {
3728 : myParkingVehicles.erase(veh);
3729 18380 : }
3730 :
3731 : bool
3732 69 : MSLane::hasApproaching() const {
3733 138 : for (const MSLink* link : myLinks) {
3734 75 : if (link->getApproaching().size() > 0) {
3735 : return true;
3736 : }
3737 : }
3738 : return false;
3739 : }
3740 :
3741 : void
3742 12665 : MSLane::saveState(OutputDevice& out) {
3743 12665 : const bool toRailJunction = myLinks.size() > 0 && (
3744 12208 : myEdge->getToJunction()->getType() == SumoXMLNodeType::RAIL_SIGNAL
3745 12128 : || myEdge->getToJunction()->getType() == SumoXMLNodeType::RAIL_CROSSING);
3746 : const bool hasVehicles = myVehicles.size() > 0;
3747 12665 : if (hasVehicles || (toRailJunction && hasApproaching())) {
3748 520 : out.openTag(SUMO_TAG_LANE);
3749 520 : out.writeAttr(SUMO_ATTR_ID, getID());
3750 520 : if (hasVehicles) {
3751 514 : out.openTag(SUMO_TAG_VIEWSETTINGS_VEHICLES);
3752 514 : out.writeAttr(SUMO_ATTR_VALUE, myVehicles);
3753 1028 : out.closeTag();
3754 : }
3755 520 : if (toRailJunction) {
3756 37 : for (const MSLink* link : myLinks) {
3757 20 : if (link->getApproaching().size() > 0) {
3758 17 : out.openTag(SUMO_TAG_LINK);
3759 17 : out.writeAttr(SUMO_ATTR_TO, link->getViaLaneOrLane()->getID());
3760 34 : for (auto item : link->getApproaching()) {
3761 17 : out.openTag(SUMO_TAG_APPROACHING);
3762 17 : out.writeAttr(SUMO_ATTR_ID, item.first->getID());
3763 17 : out.writeAttr(SUMO_ATTR_ARRIVALTIME, item.second.arrivalTime);
3764 17 : out.writeAttr(SUMO_ATTR_ARRIVALSPEED, item.second.arrivalSpeed);
3765 17 : out.writeAttr(SUMO_ATTR_DEPARTSPEED, item.second.leaveSpeed);
3766 17 : out.writeAttr(SUMO_ATTR_REQUEST, item.second.willPass);
3767 17 : out.writeAttr(SUMO_ATTR_ARRIVALSPEEDBRAKING, item.second.arrivalSpeedBraking);
3768 17 : out.writeAttr(SUMO_ATTR_WAITINGTIME, item.second.waitingTime);
3769 17 : out.writeAttr(SUMO_ATTR_DISTANCE, item.second.dist);
3770 17 : if (item.second.latOffset != 0) {
3771 0 : out.writeAttr(SUMO_ATTR_POSITION_LAT, item.second.latOffset);
3772 : }
3773 34 : out.closeTag();
3774 : }
3775 34 : out.closeTag();
3776 : }
3777 : }
3778 : }
3779 1040 : out.closeTag();
3780 : }
3781 12665 : }
3782 :
3783 : void
3784 8535 : MSLane::clearState() {
3785 : myVehicles.clear();
3786 : myParkingVehicles.clear();
3787 : myPartialVehicles.clear();
3788 : myManeuverReservations.clear();
3789 8535 : myBruttoVehicleLengthSum = 0;
3790 8535 : myNettoVehicleLengthSum = 0;
3791 8535 : myBruttoVehicleLengthSumToRemove = 0;
3792 8535 : myNettoVehicleLengthSumToRemove = 0;
3793 8535 : myLeaderInfoTime = SUMOTime_MIN;
3794 8535 : myFollowerInfoTime = SUMOTime_MIN;
3795 19823 : for (MSLink* link : myLinks) {
3796 11288 : link->clearState();
3797 : }
3798 8535 : }
3799 :
3800 : void
3801 588 : MSLane::loadState(const std::vector<SUMOVehicle*>& vehs) {
3802 2035 : for (SUMOVehicle* veh : vehs) {
3803 1447 : MSVehicle* v = dynamic_cast<MSVehicle*>(veh);
3804 1447 : v->updateBestLanes(false, this);
3805 : // incorporateVehicle resets the lastActionTime (which has just been loaded from state) so we must restore it
3806 1447 : const SUMOTime lastActionTime = v->getLastActionTime();
3807 1447 : incorporateVehicle(v, v->getPositionOnLane(), v->getSpeed(), v->getLateralPositionOnLane(), myVehicles.end(),
3808 : MSMoveReminder::NOTIFICATION_LOAD_STATE);
3809 1447 : v->resetActionOffset(lastActionTime - MSNet::getInstance()->getCurrentTimeStep());
3810 : }
3811 588 : }
3812 :
3813 :
3814 : double
3815 2451422030 : MSLane::getVehicleStopOffset(const MSVehicle* veh) const {
3816 2451422030 : if (!myLaneStopOffset.isDefined()) {
3817 : return 0;
3818 : }
3819 93531 : if ((myLaneStopOffset.getPermissions() & veh->getVClass()) != 0) {
3820 31232 : return myLaneStopOffset.getOffset();
3821 : } else {
3822 : return 0;
3823 : }
3824 : }
3825 :
3826 :
3827 : const StopOffset&
3828 2980 : MSLane::getLaneStopOffsets() const {
3829 2980 : return myLaneStopOffset;
3830 : }
3831 :
3832 :
3833 : void
3834 2152 : MSLane::setLaneStopOffset(const StopOffset& stopOffset) {
3835 2152 : myLaneStopOffset = stopOffset;
3836 2152 : }
3837 :
3838 :
3839 : MSLeaderDistanceInfo
3840 310249004 : MSLane::getFollowersOnConsecutive(const MSVehicle* ego, double backOffset,
3841 : bool allSublanes, double searchDist, MinorLinkMode mLinkMode, bool maxSearchDist) const {
3842 : assert(ego != 0);
3843 : // get the follower vehicle on the lane to change to
3844 310249004 : const double egoPos = backOffset + ego->getVehicleType().getLength();
3845 310249004 : const double egoLatDist = ego->getLane()->getRightSideOnEdge() - getRightSideOnEdge();
3846 311198516 : const bool getOppositeLeaders = ((ego->getLaneChangeModel().isOpposite() && ego->getLane() == this)
3847 310378794 : || (!ego->getLaneChangeModel().isOpposite() && &ego->getLane()->getEdge() != &getEdge()));
3848 : #ifdef DEBUG_CONTEXT
3849 : if (DEBUG_COND2(ego)) {
3850 : std::cout << SIMTIME << " getFollowers lane=" << getID() << " ego=" << ego->getID()
3851 : << " backOffset=" << backOffset << " pos=" << egoPos
3852 : << " allSub=" << allSublanes << " searchDist=" << searchDist << " ignoreMinor=" << mLinkMode
3853 : << " maxSearchDist=" << maxSearchDist
3854 : << " egoLatDist=" << egoLatDist
3855 : << " getOppositeLeaders=" << getOppositeLeaders
3856 : << "\n";
3857 : }
3858 : #endif
3859 318054623 : MSCriticalFollowerDistanceInfo result(myWidth, allSublanes ? nullptr : ego, allSublanes ? 0 : egoLatDist, getOppositeLeaders);
3860 310249004 : if (MSGlobals::gLateralResolution > 0 && egoLatDist == 0) {
3861 : // check whether ego is outside lane bounds far enough so that another vehicle might
3862 : // be between itself and the first "actual" sublane
3863 : // shift the offset so that we "see" this vehicle
3864 200152220 : if (ego->getLeftSideOnLane() < -MSGlobals::gLateralResolution) {
3865 34971 : result.setSublaneOffset(int(-ego->getLeftSideOnLane() / MSGlobals::gLateralResolution));
3866 200117249 : } else if (ego->getRightSideOnLane() > getWidth() + MSGlobals::gLateralResolution) {
3867 106939 : result.setSublaneOffset(-int((ego->getRightSideOnLane() - getWidth()) / MSGlobals::gLateralResolution));
3868 : }
3869 : #ifdef DEBUG_CONTEXT
3870 : if (DEBUG_COND2(ego)) {
3871 : std::cout << SIMTIME << " getFollowers lane=" << getID() << " ego=" << ego->getID()
3872 : << " egoPosLat=" << ego->getLateralPositionOnLane()
3873 : << " egoLatDist=" << ego->getLane()->getRightSideOnEdge() - getRightSideOnEdge()
3874 : << " extraOffset=" << result.getSublaneOffset()
3875 : << "\n";
3876 : }
3877 : #endif
3878 : }
3879 : /// XXX iterate in reverse and abort when there are no more freeSublanes
3880 6160823035 : for (AnyVehicleIterator last = anyVehiclesBegin(); last != anyVehiclesEnd(); ++last) {
3881 6160823035 : const MSVehicle* veh = *last;
3882 : #ifdef DEBUG_CONTEXT
3883 : if (DEBUG_COND2(ego)) {
3884 : std::cout << " veh=" << veh->getID() << " lane=" << veh->getLane()->getID() << " pos=" << veh->getPositionOnLane(this) << "\n";
3885 : }
3886 : #endif
3887 6160823035 : if (veh != ego && veh->getPositionOnLane(this) < egoPos) {
3888 : //const double latOffset = veh->getLane()->getRightSideOnEdge() - getRightSideOnEdge();
3889 2715805359 : const double latOffset = veh->getLatOffset(this);
3890 2715805359 : double dist = backOffset - veh->getPositionOnLane(this) - veh->getVehicleType().getMinGap();
3891 2715805359 : if (veh->isBidiOn(this)) {
3892 180634 : dist -= veh->getLength();
3893 : }
3894 2715805359 : result.addFollower(veh, ego, dist, latOffset);
3895 : #ifdef DEBUG_CONTEXT
3896 : if (DEBUG_COND2(ego)) {
3897 : std::cout << " (1) added veh=" << veh->getID() << " latOffset=" << latOffset << " result=" << result.toString() << "\n";
3898 : }
3899 : #endif
3900 : }
3901 : }
3902 : #ifdef DEBUG_CONTEXT
3903 : if (DEBUG_COND2(ego)) {
3904 : std::cout << " result.numFreeSublanes=" << result.numFreeSublanes() << "\n";
3905 : }
3906 : #endif
3907 310249004 : if (result.numFreeSublanes() > 0) {
3908 : // do a tree search among all follower lanes and check for the most
3909 : // important vehicle (the one requiring the largest reargap)
3910 : // to get a safe bound on the necessary search depth, we need to consider the maximum speed and minimum
3911 : // deceleration of potential follower vehicles
3912 143155719 : if (searchDist == -1) {
3913 141746616 : searchDist = getMaximumBrakeDist() - backOffset;
3914 : #ifdef DEBUG_CONTEXT
3915 : if (DEBUG_COND2(ego)) {
3916 : std::cout << " computed searchDist=" << searchDist << "\n";
3917 : }
3918 : #endif
3919 : }
3920 : std::set<const MSEdge*> egoFurther;
3921 155365771 : for (MSLane* further : ego->getFurtherLanes()) {
3922 12210052 : egoFurther.insert(&further->getEdge());
3923 : }
3924 154547205 : if (ego->getPositionOnLane() < ego->getVehicleType().getLength() && egoFurther.size() == 0
3925 144112892 : && ego->getLane()->getLogicalPredecessorLane() != nullptr) {
3926 : // on insertion
3927 464914 : egoFurther.insert(&ego->getLane()->getLogicalPredecessorLane()->getEdge());
3928 : }
3929 :
3930 : // avoid loops
3931 143155719 : std::set<const MSLane*> visited(myEdge->getLanes().begin(), myEdge->getLanes().end());
3932 143155719 : if (myEdge->getBidiEdge() != nullptr) {
3933 : visited.insert(myEdge->getBidiEdge()->getLanes().begin(), myEdge->getBidiEdge()->getLanes().end());
3934 : }
3935 : std::vector<MSLane::IncomingLaneInfo> newFound;
3936 143155719 : std::vector<MSLane::IncomingLaneInfo> toExamine = myIncomingLanes;
3937 304269499 : while (toExamine.size() != 0) {
3938 357972844 : for (std::vector<MSLane::IncomingLaneInfo>::iterator it = toExamine.begin(); it != toExamine.end(); ++it) {
3939 196859064 : MSLane* next = (*it).lane;
3940 : searchDist = maxSearchDist
3941 196859064 : ? MAX2(searchDist, next->getMaximumBrakeDist() - backOffset)
3942 190755748 : : MIN2(searchDist, next->getMaximumBrakeDist() - backOffset);
3943 196859064 : MSLeaderInfo first = next->getFirstVehicleInformation(nullptr, 0, false, std::numeric_limits<double>::max(), false);
3944 196859064 : MSLeaderInfo firstFront = next->getFirstVehicleInformation(nullptr, 0, true);
3945 : #ifdef DEBUG_CONTEXT
3946 : if (DEBUG_COND2(ego)) {
3947 : std::cout << " next=" << next->getID() << " seen=" << (*it).length << " first=" << first.toString() << " firstFront=" << firstFront.toString() << " backOffset=" << backOffset << "\n";
3948 : gDebugFlag1 = true; // for calling getLeaderInfo
3949 : }
3950 : #endif
3951 196859064 : if (backOffset + (*it).length - next->getLength() < 0
3952 196859064 : && egoFurther.count(&next->getEdge()) != 0
3953 : ) {
3954 : // check for junction foes that would interfere with lane changing
3955 : // @note: we are passing the back of ego as its front position so
3956 : // we need to add this back to the returned gap
3957 12219829 : const MSLink::LinkLeaders linkLeaders = (*it).viaLink->getLeaderInfo(ego, -backOffset);
3958 12508689 : for (const auto& ll : linkLeaders) {
3959 288860 : if (ll.vehAndGap.first != nullptr) {
3960 288848 : const bool bidiFoe = (*it).viaLink->getLane() == ll.vehAndGap.first->getLane()->getNormalPredecessorLane()->getBidiLane();
3961 288848 : const bool egoIsLeader = !bidiFoe && ll.vehAndGap.first->isLeader((*it).viaLink, ego, ll.vehAndGap.second);
3962 : // if ego is leader the returned gap still assumes that ego follows the leader
3963 : // if the foe vehicle follows ego we need to deduce that gap
3964 : const double gap = (egoIsLeader
3965 288848 : ? -ll.vehAndGap.second - ll.vehAndGap.first->getVehicleType().getLengthWithGap() - ego->getVehicleType().getMinGap()
3966 371 : : ll.vehAndGap.second + ego->getVehicleType().getLength());
3967 288848 : result.addFollower(ll.vehAndGap.first, ego, gap);
3968 : #ifdef DEBUG_CONTEXT
3969 : if (DEBUG_COND2(ego)) {
3970 : std::cout << SIMTIME << " ego=" << ego->getID() << " link=" << (*it).viaLink->getViaLaneOrLane()->getID()
3971 : << " (3) added veh=" << Named::getIDSecure(ll.vehAndGap.first)
3972 : << " gap=" << ll.vehAndGap.second << " dtC=" << ll.distToCrossing
3973 : << " bidiFoe=" << bidiFoe
3974 : << " egoIsLeader=" << egoIsLeader << " gap2=" << gap
3975 : << "\n";
3976 : }
3977 : #endif
3978 : }
3979 : }
3980 12219829 : }
3981 : #ifdef DEBUG_CONTEXT
3982 : if (DEBUG_COND2(ego)) {
3983 : gDebugFlag1 = false;
3984 : }
3985 : #endif
3986 :
3987 789574032 : for (int i = 0; i < first.numSublanes(); ++i) {
3988 592714968 : const MSVehicle* v = first[i] == ego ? firstFront[i] : first[i];
3989 : double agap = 0;
3990 :
3991 592714968 : if (v != nullptr && v != ego) {
3992 195973186 : if (!v->isFrontOnLane(next)) {
3993 : // the front of v is already on divergent trajectory from the ego vehicle
3994 : // for which this method is called (in the context of MSLaneChanger).
3995 : // Therefore, technically v is not a follower but only an obstruction and
3996 : // the gap is not between the front of v and the back of ego
3997 : // but rather between the flank of v and the back of ego.
3998 28362481 : agap = (*it).length - next->getLength() + backOffset;
3999 28362481 : if (MSGlobals::gUsingInternalLanes) {
4000 : // ego should have left the intersection still occupied by v
4001 28353359 : agap -= v->getVehicleType().getMinGap();
4002 : }
4003 : #ifdef DEBUG_CONTEXT
4004 : if (DEBUG_COND2(ego)) {
4005 : std::cout << " agap1=" << agap << "\n";
4006 : }
4007 : #endif
4008 28362481 : const bool differentEdge = &v->getLane()->getEdge() != &ego->getLane()->getEdge();
4009 28362481 : if (agap > 0 && differentEdge) {
4010 : // Only if ego overlaps we treat v as if it were a real follower
4011 : // Otherwise we ignore it and look for another follower
4012 17294851 : if (!getOppositeLeaders) {
4013 : // even if the vehicle is not a real
4014 : // follower, it still forms a real
4015 : // obstruction in opposite direction driving
4016 17116019 : v = firstFront[i];
4017 17116019 : if (v != nullptr && v != ego) {
4018 14166745 : agap = (*it).length - v->getPositionOnLane() + backOffset - v->getVehicleType().getMinGap();
4019 : } else {
4020 : v = nullptr;
4021 : }
4022 : }
4023 11067630 : } else if (differentEdge && result.hasVehicle(v)) {
4024 : // ignore this vehicle as it was already seen on another lane
4025 : agap = 0;
4026 : }
4027 : } else {
4028 167610705 : if (next->getBidiLane() != nullptr && v->isBidiOn(next)) {
4029 121626 : agap = v->getPositionOnLane() + backOffset - v->getVehicleType().getLengthWithGap();
4030 : } else {
4031 167489079 : agap = (*it).length - v->getPositionOnLane() + backOffset - v->getVehicleType().getMinGap();
4032 : }
4033 167610705 : if (!(*it).viaLink->havePriority() && egoFurther.count(&(*it).lane->getEdge()) == 0
4034 8133540 : && ego->isOnRoad() // during insertion, this can lead to collisions because ego's further lanes are not set (see #3053)
4035 8070788 : && !ego->getLaneChangeModel().isOpposite()
4036 175681061 : && v->getSpeed() < SUMO_const_haltingSpeed
4037 : ) {
4038 : // if v is stopped on a minor side road it should not block lane changing
4039 : agap = MAX2(agap, 0.0);
4040 : }
4041 : }
4042 195973186 : result.addFollower(v, ego, agap, 0, i);
4043 : #ifdef DEBUG_CONTEXT
4044 : if (DEBUG_COND2(ego)) {
4045 : std::cout << " (2) added veh=" << Named::getIDSecure(v) << " agap=" << agap << " next=" << next->getID() << " result=" << result.toString() << "\n";
4046 : }
4047 : #endif
4048 : }
4049 : }
4050 196859064 : if ((*it).length < searchDist) {
4051 : const std::vector<MSLane::IncomingLaneInfo>& followers = next->getIncomingLanes();
4052 139448872 : for (std::vector<MSLane::IncomingLaneInfo>::const_iterator j = followers.begin(); j != followers.end(); ++j) {
4053 70642941 : if (visited.find((*j).lane) == visited.end() && (((*j).viaLink->havePriority() && !(*j).viaLink->isTurnaround())
4054 12654411 : || mLinkMode == MinorLinkMode::FOLLOW_ALWAYS
4055 1499310 : || (mLinkMode == MinorLinkMode::FOLLOW_ONCOMING && (*j).viaLink->getDirection() == LinkDirection::STRAIGHT))) {
4056 67237217 : visited.insert((*j).lane);
4057 : MSLane::IncomingLaneInfo ili;
4058 67237217 : ili.lane = (*j).lane;
4059 67237217 : ili.length = (*j).length + (*it).length;
4060 67237217 : ili.viaLink = (*j).viaLink;
4061 67237217 : newFound.push_back(ili);
4062 : }
4063 : }
4064 : }
4065 196859064 : }
4066 : toExamine.clear();
4067 : swap(newFound, toExamine);
4068 : }
4069 : //return result;
4070 :
4071 143155719 : }
4072 620498008 : return result;
4073 310249004 : }
4074 :
4075 :
4076 : void
4077 16982930 : MSLane::getLeadersOnConsecutive(double dist, double seen, double speed, const MSVehicle* ego,
4078 : const std::vector<MSLane*>& bestLaneConts, MSLeaderDistanceInfo& result,
4079 : bool oppositeDirection) const {
4080 16982930 : if (seen > dist && !(isInternal() && MSGlobals::gComputeLC)) {
4081 : return;
4082 : }
4083 : // check partial vehicles (they might be on a different route and thus not
4084 : // found when iterating along bestLaneConts)
4085 17804613 : for (VehCont::const_iterator it = myPartialVehicles.begin(); it != myPartialVehicles.end(); ++it) {
4086 2885197 : MSVehicle* veh = *it;
4087 2885197 : if (!veh->isFrontOnLane(this)) {
4088 821683 : result.addLeader(veh, seen, veh->getLatOffset(this));
4089 : } else {
4090 : break;
4091 : }
4092 : }
4093 : #ifdef DEBUG_CONTEXT
4094 : if (DEBUG_COND2(ego)) {
4095 : gDebugFlag1 = true;
4096 : }
4097 : #endif
4098 : const MSLane* nextLane = this;
4099 : int view = 1;
4100 : // loop over following lanes
4101 36317031 : while ((seen < dist || nextLane->isInternal()) && result.numFreeSublanes() > 0) {
4102 : // get the next link used
4103 : bool nextInternal = false;
4104 24763544 : if (oppositeDirection) {
4105 7775 : if (view >= (int)bestLaneConts.size()) {
4106 : break;
4107 : }
4108 2835 : nextLane = bestLaneConts[view];
4109 : } else {
4110 24755769 : std::vector<MSLink*>::const_iterator link = succLinkSec(*ego, view, *nextLane, bestLaneConts);
4111 24755769 : if (nextLane->isLinkEnd(link)) {
4112 : break;
4113 : }
4114 : // check for link leaders
4115 20287953 : const MSLink::LinkLeaders linkLeaders = (*link)->getLeaderInfo(ego, seen);
4116 20287953 : if (linkLeaders.size() > 0) {
4117 1070185 : const MSLink::LinkLeader ll = linkLeaders[0];
4118 1070185 : MSVehicle* veh = ll.vehAndGap.first;
4119 : // in the context of lane changing all junction leader candidates must be respected
4120 1070185 : if (veh != 0 && (ego->isLeader(*link, veh, ll.vehAndGap.second)
4121 101333 : || (MSGlobals::gComputeLC
4122 1171518 : && veh->getPosition().distanceTo2D(ego->getPosition()) - veh->getVehicleType().getMinGap() - ego->getVehicleType().getLength()
4123 101333 : < veh->getCarFollowModel().brakeGap(veh->getSpeed())))) {
4124 : #ifdef DEBUG_CONTEXT
4125 : if (DEBUG_COND2(ego)) {
4126 : std::cout << " linkleader=" << veh->getID() << " gap=" << ll.vehAndGap.second << " leaderOffset=" << ll.latOffset << " flags=" << ll.llFlags << "\n";
4127 : }
4128 : #endif
4129 956687 : if (ll.sameTarget() || ll.sameSource()) {
4130 593190 : result.addLeader(veh, ll.vehAndGap.second, ll.latOffset);
4131 : } else {
4132 : // add link leader to all sublanes and return
4133 2362163 : for (int i = 0; i < result.numSublanes(); ++i) {
4134 1998666 : result.addLeader(veh, ll.vehAndGap.second, 0, i);
4135 : }
4136 : }
4137 : #ifdef DEBUG_CONTEXT
4138 : gDebugFlag1 = false;
4139 : #endif
4140 956687 : return;
4141 : } // XXX else, deal with pedestrians
4142 : }
4143 19331266 : nextInternal = (*link)->getViaLane() != nullptr;
4144 : nextLane = (*link)->getViaLaneOrLane();
4145 11204006 : if (nextLane == nullptr) {
4146 : break;
4147 : }
4148 20287953 : }
4149 :
4150 19334101 : MSLeaderInfo leaders = nextLane->getLastVehicleInformation(nullptr, 0, 0, false);
4151 : #ifdef DEBUG_CONTEXT
4152 : if (DEBUG_COND2(ego)) {
4153 : std::cout << SIMTIME << " getLeadersOnConsecutive lane=" << getID() << " nextLane=" << nextLane->getID() << " leaders=" << leaders.toString() << "\n";
4154 : }
4155 : #endif
4156 : // @todo check alignment issues if the lane width changes
4157 : const int iMax = MIN2(leaders.numSublanes(), result.numSublanes());
4158 102871549 : for (int i = 0; i < iMax; ++i) {
4159 83537448 : const MSVehicle* veh = leaders[i];
4160 83537448 : if (veh != nullptr) {
4161 : #ifdef DEBUG_CONTEXT
4162 : if (DEBUG_COND2(ego)) std::cout << " lead=" << veh->getID()
4163 : << " seen=" << seen
4164 : << " minGap=" << ego->getVehicleType().getMinGap()
4165 : << " backPos=" << veh->getBackPositionOnLane(nextLane)
4166 : << " gap=" << seen - ego->getVehicleType().getMinGap() + veh->getBackPositionOnLane(nextLane)
4167 : << "\n";
4168 : #endif
4169 33564191 : result.addLeader(veh, seen - ego->getVehicleType().getMinGap() + veh->getBackPositionOnLane(nextLane), 0, i);
4170 : }
4171 : }
4172 :
4173 19334101 : if (nextLane->getVehicleMaxSpeed(ego) < speed) {
4174 936974 : dist = ego->getCarFollowModel().brakeGap(nextLane->getVehicleMaxSpeed(ego));
4175 : }
4176 19334101 : seen += nextLane->getLength();
4177 19334101 : if (!nextInternal) {
4178 11206841 : view++;
4179 : }
4180 19334101 : }
4181 : #ifdef DEBUG_CONTEXT
4182 : gDebugFlag1 = false;
4183 : #endif
4184 : }
4185 :
4186 :
4187 : void
4188 125554314 : MSLane::addLeaders(const MSVehicle* vehicle, double vehPos, MSLeaderDistanceInfo& result, bool opposite) {
4189 : // if there are vehicles on the target lane with the same position as ego,
4190 : // they may not have been added to 'ahead' yet
4191 : #ifdef DEBUG_SURROUNDING
4192 : if (DEBUG_COND || DEBUG_COND2(vehicle)) {
4193 : std::cout << " addLeaders lane=" << getID() << " veh=" << vehicle->getID() << " vehPos=" << vehPos << " opposite=" << opposite << "\n";
4194 : }
4195 : #endif
4196 125554314 : const MSLeaderInfo& aheadSamePos = getLastVehicleInformation(nullptr, 0, vehPos, false);
4197 651549585 : for (int i = 0; i < aheadSamePos.numSublanes(); ++i) {
4198 525995271 : const MSVehicle* veh = aheadSamePos[i];
4199 525995271 : if (veh != nullptr && veh != vehicle) {
4200 141393386 : const double gap = veh->getBackPositionOnLane(this) - vehPos - vehicle->getVehicleType().getMinGap();
4201 : #ifdef DEBUG_SURROUNDING
4202 : if (DEBUG_COND || DEBUG_COND2(vehicle)) {
4203 : std::cout << " further lead=" << veh->getID() << " leadBack=" << veh->getBackPositionOnLane(this) << " gap=" << gap << "\n";
4204 : }
4205 : #endif
4206 141393386 : result.addLeader(veh, gap, 0, i);
4207 : }
4208 : }
4209 :
4210 125554314 : if (result.numFreeSublanes() > 0) {
4211 37455161 : double seen = vehicle->getLane()->getLength() - vehPos;
4212 37455161 : double speed = vehicle->getSpeed();
4213 : // leader vehicle could be link leader on the next junction
4214 37455161 : double dist = MAX2(vehicle->getCarFollowModel().brakeGap(speed), 10.0) + vehicle->getVehicleType().getMinGap();
4215 37455161 : if (getBidiLane() != nullptr) {
4216 477856 : dist = MAX2(dist, myMaxSpeed * 20);
4217 : }
4218 : // check for link leaders when on internal
4219 37455161 : if (seen > dist && !(isInternal() && MSGlobals::gComputeLC)) {
4220 : #ifdef DEBUG_SURROUNDING
4221 : if (DEBUG_COND || DEBUG_COND2(vehicle)) {
4222 : std::cout << " aborting forward search. dist=" << dist << " seen=" << seen << "\n";
4223 : }
4224 : #endif
4225 : return;
4226 : }
4227 : #ifdef DEBUG_SURROUNDING
4228 : if (DEBUG_COND || DEBUG_COND2(vehicle)) {
4229 : std::cout << " add consecutive before=" << result.toString() << " seen=" << seen << " dist=" << dist;
4230 : }
4231 : #endif
4232 16982930 : if (opposite) {
4233 6358 : const std::vector<MSLane*> bestLaneConts = vehicle->getUpstreamOppositeLanes();
4234 : #ifdef DEBUG_SURROUNDING
4235 : if (DEBUG_COND || DEBUG_COND2(vehicle)) {
4236 : std::cout << " upstreamOpposite=" << toString(bestLaneConts);
4237 : }
4238 : #endif
4239 6358 : getLeadersOnConsecutive(dist, seen, speed, vehicle, bestLaneConts, result, opposite);
4240 6358 : } else {
4241 16976572 : const std::vector<MSLane*>& bestLaneConts = vehicle->getBestLanesContinuation(this);
4242 16976572 : getLeadersOnConsecutive(dist, seen, speed, vehicle, bestLaneConts, result);
4243 : }
4244 : #ifdef DEBUG_SURROUNDING
4245 : if (DEBUG_COND || DEBUG_COND2(vehicle)) {
4246 : std::cout << " after=" << result.toString() << "\n";
4247 : }
4248 : #endif
4249 : }
4250 125554314 : }
4251 :
4252 :
4253 : MSVehicle*
4254 486000386 : MSLane::getPartialBehind(const MSVehicle* ego) const {
4255 512745171 : for (VehCont::const_reverse_iterator i = myPartialVehicles.rbegin(); i != myPartialVehicles.rend(); ++i) {
4256 28427753 : MSVehicle* veh = *i;
4257 28427753 : if (veh->isFrontOnLane(this)
4258 3285007 : && veh != ego
4259 31712760 : && veh->getPositionOnLane() <= ego->getPositionOnLane()) {
4260 : #ifdef DEBUG_CONTEXT
4261 : if (DEBUG_COND2(ego)) {
4262 : std::cout << SIMTIME << " getPartialBehind lane=" << getID() << " ego=" << ego->getID() << " found=" << veh->getID() << "\n";
4263 : }
4264 : #endif
4265 : return veh;
4266 : }
4267 : }
4268 : #ifdef DEBUG_CONTEXT
4269 : if (DEBUG_COND2(ego)) {
4270 : std::cout << SIMTIME << " getPartialBehind lane=" << getID() << " ego=" << ego->getID() << " nothing found. partials=" << toString(myPartialVehicles) << "\n";
4271 : }
4272 : #endif
4273 : return nullptr;
4274 : }
4275 :
4276 : MSLeaderInfo
4277 20725011 : MSLane::getPartialBeyond() const {
4278 20725011 : MSLeaderInfo result(myWidth);
4279 22243128 : for (VehCont::const_iterator it = myPartialVehicles.begin(); it != myPartialVehicles.end(); ++it) {
4280 3197569 : MSVehicle* veh = *it;
4281 3197569 : if (!veh->isFrontOnLane(this)) {
4282 1518117 : result.addLeader(veh, false, veh->getLatOffset(this));
4283 : } else {
4284 : break;
4285 : }
4286 : }
4287 20725011 : return result;
4288 0 : }
4289 :
4290 :
4291 : std::set<MSVehicle*>
4292 11179 : MSLane::getSurroundingVehicles(double startPos, double downstreamDist, double upstreamDist, std::shared_ptr<LaneCoverageInfo> checkedLanes) const {
4293 : assert(checkedLanes != nullptr);
4294 11179 : if (checkedLanes->find(this) != checkedLanes->end()) {
4295 : #ifdef DEBUG_SURROUNDING
4296 : std::cout << "Skipping previously scanned lane: " << getID() << std::endl;
4297 : #endif
4298 2266 : return std::set<MSVehicle*>();
4299 : } else {
4300 : // Add this lane's coverage to the lane coverage info
4301 18839 : (*checkedLanes)[this] = std::make_pair(MAX2(0.0, startPos - upstreamDist), MIN2(startPos + downstreamDist, getLength()));
4302 : }
4303 : #ifdef DEBUG_SURROUNDING
4304 : std::cout << "Scanning on lane " << myID << "(downstr. " << downstreamDist << ", upstr. " << upstreamDist << ", startPos " << startPos << "): " << std::endl;
4305 : #endif
4306 14490 : std::set<MSVehicle*> foundVehicles = getVehiclesInRange(MAX2(0., startPos - upstreamDist), MIN2(myLength, startPos + downstreamDist));
4307 8913 : if (startPos < upstreamDist) {
4308 : // scan incoming lanes
4309 10886 : for (const IncomingLaneInfo& incomingInfo : getIncomingLanes()) {
4310 5412 : MSLane* incoming = incomingInfo.lane;
4311 : #ifdef DEBUG_SURROUNDING
4312 : std::cout << "Checking on incoming: " << incoming->getID() << std::endl;
4313 : if (checkedLanes->find(incoming) != checkedLanes->end()) {
4314 : std::cout << "Skipping previous: " << incoming->getID() << std::endl;
4315 : }
4316 : #endif
4317 10824 : std::set<MSVehicle*> newVehs = incoming->getSurroundingVehicles(incoming->getLength(), 0.0, upstreamDist - startPos, checkedLanes);
4318 5412 : foundVehicles.insert(newVehs.begin(), newVehs.end());
4319 : }
4320 : }
4321 :
4322 8913 : if (getLength() < startPos + downstreamDist) {
4323 : // scan successive lanes
4324 : const std::vector<MSLink*>& lc = getLinkCont();
4325 6130 : for (MSLink* l : lc) {
4326 : #ifdef DEBUG_SURROUNDING
4327 : std::cout << "Checking on outgoing: " << l->getViaLaneOrLane()->getID() << std::endl;
4328 : #endif
4329 5588 : std::set<MSVehicle*> newVehs = l->getViaLaneOrLane()->getSurroundingVehicles(0.0, downstreamDist - (myLength - startPos), upstreamDist, checkedLanes);
4330 2794 : foundVehicles.insert(newVehs.begin(), newVehs.end());
4331 : }
4332 : }
4333 : #ifdef DEBUG_SURROUNDING
4334 : std::cout << "On lane (2) " << myID << ": \nFound vehicles: " << std::endl;
4335 : for (MSVehicle* v : foundVehicles) {
4336 : std::cout << v->getID() << " pos = " << v->getPositionOnLane() << std::endl;
4337 : }
4338 : #endif
4339 : return foundVehicles;
4340 : }
4341 :
4342 :
4343 : std::set<MSVehicle*>
4344 11748 : MSLane::getVehiclesInRange(const double a, const double b) const {
4345 : std::set<MSVehicle*> res;
4346 11748 : const VehCont& vehs = getVehiclesSecure();
4347 :
4348 11748 : if (!vehs.empty()) {
4349 12542 : for (MSVehicle* const veh : vehs) {
4350 8354 : if (veh->getPositionOnLane() >= a) {
4351 7010 : if (veh->getBackPositionOnLane() > b) {
4352 : break;
4353 : }
4354 : res.insert(veh);
4355 : }
4356 : }
4357 : }
4358 11748 : releaseVehicles();
4359 11748 : return res;
4360 : }
4361 :
4362 :
4363 : std::vector<const MSJunction*>
4364 0 : MSLane::getUpcomingJunctions(double pos, double range, const std::vector<MSLane*>& contLanes) const {
4365 : // set of upcoming junctions and the corresponding conflict links
4366 : std::vector<const MSJunction*> junctions;
4367 0 : for (auto l : getUpcomingLinks(pos, range, contLanes)) {
4368 0 : junctions.insert(junctions.end(), l->getJunction());
4369 0 : }
4370 0 : return junctions;
4371 0 : }
4372 :
4373 :
4374 : std::vector<const MSLink*>
4375 734 : MSLane::getUpcomingLinks(double pos, double range, const std::vector<MSLane*>& contLanes) const {
4376 : #ifdef DEBUG_SURROUNDING
4377 : std::cout << "getUpcoming links on lane '" << getID() << "' with pos=" << pos
4378 : << " range=" << range << std::endl;
4379 : #endif
4380 : // set of upcoming junctions and the corresponding conflict links
4381 : std::vector<const MSLink*> links;
4382 :
4383 : // Currently scanned lane
4384 : const MSLane* lane = this;
4385 :
4386 : // continuation lanes for the vehicle
4387 : std::vector<MSLane*>::const_iterator contLanesIt = contLanes.begin();
4388 : // scanned distance so far
4389 : double dist = 0.0;
4390 : // link to be crossed by the vehicle
4391 734 : const MSLink* link = nullptr;
4392 734 : if (lane->isInternal()) {
4393 : assert(*contLanesIt == nullptr); // is called with vehicle's bestLane structure
4394 113 : link = lane->getEntryLink();
4395 113 : links.insert(links.end(), link);
4396 113 : dist += link->getInternalLengthsAfter();
4397 : // next non-internal lane behind junction
4398 : lane = link->getLane();
4399 : pos = 0.0;
4400 : assert(*(contLanesIt + 1) == lane);
4401 : }
4402 1224 : while (++contLanesIt != contLanes.end()) {
4403 : assert(!lane->isInternal());
4404 934 : dist += lane->getLength() - pos;
4405 : pos = 0.;
4406 : #ifdef DEBUG_SURROUNDING
4407 : std::cout << "Distance until end of lane '" << lane->getID() << "' is " << dist << "." << std::endl;
4408 : #endif
4409 934 : if (dist > range) {
4410 : break;
4411 : }
4412 490 : link = lane->getLinkTo(*contLanesIt);
4413 490 : if (link != nullptr) {
4414 478 : links.insert(links.end(), link);
4415 : }
4416 490 : lane = *contLanesIt;
4417 : }
4418 734 : return links;
4419 0 : }
4420 :
4421 :
4422 : MSLane*
4423 250944158 : MSLane::getOpposite() const {
4424 250944158 : return myOpposite;
4425 : }
4426 :
4427 :
4428 : MSLane*
4429 190685828 : MSLane::getParallelOpposite() const {
4430 190685828 : return myEdge->getLanes().back()->getOpposite();
4431 : }
4432 :
4433 :
4434 : double
4435 7555211 : MSLane::getOppositePos(double pos) const {
4436 7555211 : return MAX2(0., myLength - pos);
4437 : }
4438 :
4439 : std::pair<MSVehicle* const, double>
4440 9333174 : MSLane::getFollower(const MSVehicle* ego, double egoPos, double dist, MinorLinkMode mLinkMode, bool maxSearchDist) const {
4441 33720914 : for (AnyVehicleIterator first = anyVehiclesUpstreamBegin(); first != anyVehiclesUpstreamEnd(); ++first) {
4442 : // XXX refactor leaderInfo to use a const vehicle all the way through the call hierarchy
4443 41370499 : MSVehicle* pred = (MSVehicle*)*first;
4444 : #ifdef DEBUG_CONTEXT
4445 : if (DEBUG_COND2(ego)) {
4446 : std::cout << " getFollower lane=" << getID() << " egoPos=" << egoPos << " pred=" << pred->getID() << " predPos=" << pred->getPositionOnLane(this) << "\n";
4447 : }
4448 : #endif
4449 41370499 : if (pred != ego && pred->getPositionOnLane(this) < egoPos) {
4450 7649585 : return std::pair<MSVehicle* const, double>(pred, egoPos - pred->getPositionOnLane(this) - ego->getVehicleType().getLength() - pred->getVehicleType().getMinGap());
4451 : }
4452 : }
4453 1683589 : const double backOffset = egoPos - ego->getVehicleType().getLength();
4454 1683589 : if (dist > 0 && backOffset > dist) {
4455 322743 : return std::make_pair(nullptr, -1);
4456 : }
4457 1360846 : const MSLeaderDistanceInfo followers = getFollowersOnConsecutive(ego, backOffset, true, dist, mLinkMode, maxSearchDist);
4458 1360846 : CLeaderDist result = followers.getClosest();
4459 1360846 : return std::make_pair(const_cast<MSVehicle*>(result.first), result.second);
4460 1360846 : }
4461 :
4462 : std::pair<MSVehicle* const, double>
4463 5128844 : MSLane::getOppositeLeader(const MSVehicle* ego, double dist, bool oppositeDir, MinorLinkMode mLinkMode) const {
4464 : #ifdef DEBUG_OPPOSITE
4465 : if (DEBUG_COND2(ego)) std::cout << SIMTIME << " getOppositeLeader lane=" << getID()
4466 : << " ego=" << ego->getID()
4467 : << " pos=" << ego->getPositionOnLane()
4468 : << " posOnOpposite=" << getOppositePos(ego->getPositionOnLane())
4469 : << " dist=" << dist
4470 : << " oppositeDir=" << oppositeDir
4471 : << "\n";
4472 : #endif
4473 5128844 : if (!oppositeDir) {
4474 386058 : return getLeader(ego, getOppositePos(ego->getPositionOnLane()), ego->getBestLanesContinuation(this));
4475 : } else {
4476 4742786 : const double egoLength = ego->getVehicleType().getLength();
4477 4742786 : const double egoPos = ego->getLaneChangeModel().isOpposite() ? ego->getPositionOnLane() : getOppositePos(ego->getPositionOnLane());
4478 4742786 : std::pair<MSVehicle* const, double> result = getFollower(ego, egoPos + egoLength, dist, mLinkMode, true);
4479 4742786 : if (result.first != nullptr) {
4480 4100769 : result.second -= ego->getVehicleType().getMinGap();
4481 4100769 : if (result.first->getLaneChangeModel().isOpposite()) {
4482 1312407 : result.second -= result.first->getVehicleType().getLength();
4483 : }
4484 : }
4485 4742786 : return result;
4486 : }
4487 : }
4488 :
4489 :
4490 : std::pair<MSVehicle* const, double>
4491 700010 : MSLane::getOppositeFollower(const MSVehicle* ego) const {
4492 : #ifdef DEBUG_OPPOSITE
4493 : if (DEBUG_COND2(ego)) std::cout << SIMTIME << " getOppositeFollower lane=" << getID()
4494 : << " ego=" << ego->getID()
4495 : << " backPos=" << ego->getBackPositionOnLane()
4496 : << " posOnOpposite=" << getOppositePos(ego->getBackPositionOnLane())
4497 : << "\n";
4498 : #endif
4499 700010 : if (ego->getLaneChangeModel().isOpposite()) {
4500 416772 : std::pair<MSVehicle* const, double> result = getFollower(ego, getOppositePos(ego->getPositionOnLane()), -1, MinorLinkMode::FOLLOW_NEVER);
4501 416772 : return result;
4502 : } else {
4503 283238 : double vehPos = getOppositePos(ego->getPositionOnLane() - ego->getVehicleType().getLength());
4504 283238 : std::pair<MSVehicle*, double> result = getLeader(ego, vehPos, std::vector<MSLane*>());
4505 283238 : double dist = getMaximumBrakeDist() + getOppositePos(ego->getPositionOnLane() - getLength());
4506 : MSLane* next = const_cast<MSLane*>(this);
4507 498485 : while (result.first == nullptr && dist > 0) {
4508 : // cannot call getLeadersOnConsecutive because succLinkSec doesn't
4509 : // uses the vehicle's route and doesn't work on the opposite side
4510 275619 : vehPos -= next->getLength();
4511 275619 : next = next->getCanonicalSuccessorLane();
4512 275619 : if (next == nullptr) {
4513 : break;
4514 : }
4515 215247 : dist -= next->getLength();
4516 215247 : result = next->getLeader(ego, vehPos, std::vector<MSLane*>());
4517 : }
4518 283238 : if (result.first != nullptr) {
4519 218635 : if (result.first->getLaneChangeModel().isOpposite()) {
4520 81834 : result.second -= result.first->getVehicleType().getLength();
4521 : } else {
4522 136801 : if (result.second > POSITION_EPS) {
4523 : // follower can be safely ignored since it is going the other way
4524 128807 : return std::make_pair(static_cast<MSVehicle*>(nullptr), -1);
4525 : }
4526 : }
4527 : }
4528 154431 : return result;
4529 : }
4530 : }
4531 :
4532 : void
4533 85452 : MSLane::initCollisionAction(const OptionsCont& oc, const std::string& option, CollisionAction& myAction) {
4534 85452 : const std::string action = oc.getString(option);
4535 85452 : if (action == "none") {
4536 15 : myAction = COLLISION_ACTION_NONE;
4537 85437 : } else if (action == "warn") {
4538 43533 : myAction = COLLISION_ACTION_WARN;
4539 41904 : } else if (action == "teleport") {
4540 41869 : myAction = COLLISION_ACTION_TELEPORT;
4541 35 : } else if (action == "remove") {
4542 35 : myAction = COLLISION_ACTION_REMOVE;
4543 : } else {
4544 0 : WRITE_ERROR(TLF("Invalid % '%'.", option, action));
4545 : }
4546 85452 : }
4547 :
4548 : void
4549 42726 : MSLane::initCollisionOptions(const OptionsCont& oc) {
4550 42726 : initCollisionAction(oc, "collision.action", myCollisionAction);
4551 42726 : initCollisionAction(oc, "intermodal-collision.action", myIntermodalCollisionAction);
4552 42726 : myCheckJunctionCollisions = oc.getBool("collision.check-junctions");
4553 42726 : myCheckJunctionCollisionMinGap = oc.getFloat("collision.check-junctions.mingap");
4554 42726 : myCollisionStopTime = string2time(oc.getString("collision.stoptime"));
4555 42726 : myIntermodalCollisionStopTime = string2time(oc.getString("intermodal-collision.stoptime"));
4556 42726 : myCollisionMinGapFactor = oc.getFloat("collision.mingap-factor");
4557 42726 : myExtrapolateSubstepDepart = oc.getBool("extrapolate-departpos");
4558 42726 : }
4559 :
4560 :
4561 : void
4562 1329 : MSLane::setPermissions(SVCPermissions permissions, long long transientID) {
4563 1329 : if (transientID == CHANGE_PERMISSIONS_PERMANENT) {
4564 83 : myPermissions = permissions;
4565 83 : myOriginalPermissions = permissions;
4566 : } else {
4567 1246 : myPermissionChanges[transientID] = permissions;
4568 1246 : resetPermissions(CHANGE_PERMISSIONS_PERMANENT);
4569 : }
4570 1329 : }
4571 :
4572 :
4573 : void
4574 1554 : MSLane::resetPermissions(long long transientID) {
4575 : myPermissionChanges.erase(transientID);
4576 1554 : if (myPermissionChanges.empty()) {
4577 294 : myPermissions = myOriginalPermissions;
4578 : } else {
4579 : // combine all permission changes
4580 1260 : myPermissions = SVCAll;
4581 2534 : for (const auto& item : myPermissionChanges) {
4582 1274 : myPermissions &= item.second;
4583 : }
4584 : }
4585 1554 : }
4586 :
4587 :
4588 : bool
4589 12937181 : MSLane::hadPermissionChanges() const {
4590 12937181 : return !myPermissionChanges.empty();
4591 : }
4592 :
4593 :
4594 : void
4595 9 : MSLane::setChangeLeft(SVCPermissions permissions) {
4596 9 : myChangeLeft = permissions;
4597 9 : }
4598 :
4599 :
4600 : void
4601 9 : MSLane::setChangeRight(SVCPermissions permissions) {
4602 9 : myChangeRight = permissions;
4603 9 : }
4604 :
4605 :
4606 : bool
4607 52778162 : MSLane::hasPedestrians() const {
4608 52778162 : MSNet* const net = MSNet::getInstance();
4609 52778162 : return net->hasPersons() && net->getPersonControl().getMovementModel()->hasPedestrians(this);
4610 : }
4611 :
4612 :
4613 : PersonDist
4614 699005 : MSLane::nextBlocking(double minPos, double minRight, double maxLeft, double stopTime, bool bidi) const {
4615 699005 : return MSNet::getInstance()->getPersonControl().getMovementModel()->nextBlocking(this, minPos, minRight, maxLeft, stopTime, bidi);
4616 : }
4617 :
4618 :
4619 : bool
4620 3909797 : MSLane::checkForPedestrians(const MSVehicle* aVehicle, double& speed, double& dist, double pos, bool patchSpeed) const {
4621 3909797 : if (getEdge().getPersons().size() > 0 && hasPedestrians()) {
4622 : #ifdef DEBUG_INSERTION
4623 : if (DEBUG_COND2(aVehicle)) {
4624 : std::cout << SIMTIME << " check for pedestrians on lane=" << getID() << " pos=" << pos << "\n";
4625 : }
4626 : #endif
4627 3818 : PersonDist leader = nextBlocking(pos - aVehicle->getVehicleType().getLength(),
4628 1909 : aVehicle->getRightSideOnLane(), aVehicle->getRightSideOnLane() + aVehicle->getVehicleType().getWidth(), ceil(speed / aVehicle->getCarFollowModel().getMaxDecel()));
4629 1909 : if (leader.first != 0) {
4630 769 : const double gap = leader.second - aVehicle->getVehicleType().getLengthWithGap();
4631 769 : const double stopSpeed = aVehicle->getCarFollowModel().stopSpeed(aVehicle, speed, gap, MSCFModel::CalcReason::FUTURE);
4632 284 : if ((gap < 0 && (aVehicle->getInsertionChecks() & ((int)InsertionCheck::COLLISION | (int)InsertionCheck::PEDESTRIAN)) != 0)
4633 1538 : || checkFailure(aVehicle, speed, dist, stopSpeed, patchSpeed, "", InsertionCheck::PEDESTRIAN)) {
4634 : // we may not drive with the given velocity - we crash into the pedestrian
4635 : #ifdef DEBUG_INSERTION
4636 : if (DEBUG_COND2(aVehicle)) std::cout << SIMTIME
4637 : << " isInsertionSuccess lane=" << getID()
4638 : << " veh=" << aVehicle->getID()
4639 : << " pos=" << pos
4640 : << " posLat=" << aVehicle->getLateralPositionOnLane()
4641 : << " patchSpeed=" << patchSpeed
4642 : << " speed=" << speed
4643 : << " stopSpeed=" << stopSpeed
4644 : << " pedestrianLeader=" << leader.first->getID()
4645 : << " failed (@796)!\n";
4646 : #endif
4647 498 : return false;
4648 : }
4649 : }
4650 : }
4651 3909299 : double backLength = aVehicle->getLength() - pos;
4652 3909299 : if (backLength > 0 && MSNet::getInstance()->hasPersons()) {
4653 : // look upstream for pedestrian crossings
4654 6948 : const MSLane* prev = getLogicalPredecessorLane();
4655 : const MSLane* cur = this;
4656 98868 : while (backLength > 0 && prev != nullptr) {
4657 91952 : const MSLink* link = prev->getLinkTo(cur);
4658 91952 : if (link->hasFoeCrossing()) {
4659 2274 : for (const MSLane* foe : link->getFoeLanes()) {
4660 1760 : if (foe->isCrossing() && (foe->hasPedestrians() ||
4661 707 : (foe->getIncomingLanes()[0].viaLink->getApproachingPersons() != nullptr
4662 47 : && foe->getIncomingLanes()[0].viaLink->getApproachingPersons()->size() > 0))) {
4663 : #ifdef DEBUG_INSERTION
4664 : if (DEBUG_COND2(aVehicle)) std::cout << SIMTIME
4665 : << " isInsertionSuccess lane=" << getID()
4666 : << " veh=" << aVehicle->getID()
4667 : << " pos=" << pos
4668 : << " backCrossing=" << foe->getID()
4669 : << " peds=" << joinNamedToString(foe->getEdge().getPersons(), " ")
4670 : << " approaching=" << foe->getIncomingLanes()[0].viaLink->getApproachingPersons()->size()
4671 : << " failed (@4550)!\n";
4672 : #endif
4673 : return false;
4674 : }
4675 : }
4676 : }
4677 91920 : backLength -= prev->getLength();
4678 : cur = prev;
4679 91920 : prev = prev->getLogicalPredecessorLane();
4680 : }
4681 : }
4682 : return true;
4683 : }
4684 :
4685 :
4686 : void
4687 42728 : MSLane::initRNGs(const OptionsCont& oc) {
4688 : myRNGs.clear();
4689 42728 : const int numRNGs = oc.getInt("thread-rngs");
4690 42728 : const bool random = oc.getBool("random");
4691 42728 : int seed = oc.getInt("seed");
4692 42728 : myRNGs.reserve(numRNGs); // this is needed for stable pointers on debugging
4693 2777320 : for (int i = 0; i < numRNGs; i++) {
4694 5469184 : myRNGs.push_back(SumoRNG("lanes_" + toString(i)));
4695 2734592 : RandHelper::initRand(&myRNGs.back(), random, seed++);
4696 : }
4697 42728 : }
4698 :
4699 : void
4700 46 : MSLane::saveRNGStates(OutputDevice& out) {
4701 2990 : for (int i = 0; i < getNumRNGs(); i++) {
4702 2944 : out.openTag(SUMO_TAG_RNGLANE);
4703 2944 : out.writeAttr(SUMO_ATTR_INDEX, i);
4704 2944 : out.writeAttr(SUMO_ATTR_STATE, RandHelper::saveState(&myRNGs[i]));
4705 5888 : out.closeTag();
4706 : }
4707 46 : }
4708 :
4709 : void
4710 2944 : MSLane::loadRNGState(int index, const std::string& state) {
4711 2944 : if (index >= getNumRNGs()) {
4712 0 : throw ProcessError(TLF("State was saved with more than % threads. Change the number of threads or do not load RNG state", toString(getNumRNGs())));
4713 : }
4714 2944 : RandHelper::loadState(state, &myRNGs[index]);
4715 2944 : }
4716 :
4717 :
4718 : MSLane*
4719 17595554984 : MSLane::getBidiLane() const {
4720 17595554984 : return myBidiLane;
4721 : }
4722 :
4723 :
4724 : bool
4725 124521011 : MSLane::mustCheckJunctionCollisions() const {
4726 124521011 : return myCheckJunctionCollisions && myEdge->isInternal() && (
4727 1537459 : myLinks.front()->getFoeLanes().size() > 0
4728 7313 : || myLinks.front()->getWalkingAreaFoe() != nullptr
4729 6405 : || myLinks.front()->getWalkingAreaFoeExit() != nullptr);
4730 : }
4731 :
4732 :
4733 : double
4734 609455013 : MSLane::getSpaceTillLastStanding(const MSVehicle* ego, bool& foundStopped) const {
4735 : /// @todo if ego isn't on this lane, we could use a cached value
4736 : double lengths = 0;
4737 4978259796 : for (const MSVehicle* last : myVehicles) {
4738 4424312526 : if (last->getSpeed() < SUMO_const_haltingSpeed && !last->getLane()->getEdge().isRoundabout()
4739 27130762 : && last != ego
4740 : // @todo recheck
4741 4423040199 : && last->isFrontOnLane(this)) {
4742 27117708 : foundStopped = true;
4743 27117708 : const double lastBrakeGap = last->getCarFollowModel().brakeGap(last->getSpeed());
4744 27117708 : const double ret = last->getBackPositionOnLane() + lastBrakeGap - lengths;
4745 : return ret;
4746 : }
4747 4368804783 : if (MSGlobals::gSublane && ego->getVehicleType().getWidth() + last->getVehicleType().getWidth() < getWidth()) {
4748 32040830 : lengths += last->getVehicleType().getLengthWithGap() * (last->getVehicleType().getWidth() + last->getVehicleType().getMinGapLat()) / getWidth();
4749 : } else {
4750 4336763953 : lengths += last->getVehicleType().getLengthWithGap();
4751 : }
4752 : }
4753 582337305 : return getLength() - lengths;
4754 : }
4755 :
4756 :
4757 : bool
4758 498583740 : MSLane::allowsVehicleClass(SUMOVehicleClass vclass, int routingMode) const {
4759 498583740 : return (((routingMode & libsumo::ROUTING_MODE_IGNORE_TRANSIENT_PERMISSIONS) ? myOriginalPermissions : myPermissions) & vclass) == vclass;
4760 : }
4761 :
4762 :
4763 : const MSJunction*
4764 393328 : MSLane::getFromJunction() const {
4765 393328 : return myEdge->getFromJunction();
4766 : }
4767 :
4768 :
4769 : const MSJunction*
4770 492146 : MSLane::getToJunction() const {
4771 492146 : return myEdge->getToJunction();
4772 : }
4773 :
4774 :
4775 : bool
4776 11268 : MSLane::mayContinue(const MSVehicle* veh) const {
4777 11268 : if (veh->getDevice(typeid(MSDevice_Taxi)) != nullptr) {
4778 : // taxi device may assign a new route that continues past the end of the initial route
4779 : return true;
4780 : }
4781 22091 : for (const MSMoveReminder* rem : myMoveReminders) {
4782 12530 : if (dynamic_cast<const MSTriggeredRerouter*>(rem) != nullptr) {
4783 : return true;
4784 : }
4785 : }
4786 : return false;
4787 : }
4788 :
4789 :
4790 : bool
4791 1707 : MSLane::hasUnsafeLink() const {
4792 3362 : for (const MSLink* link : myLinks) {
4793 3198 : if (!link->havePriority() || link->getState() == LINKSTATE_ZIPPER) {
4794 : return true;
4795 : }
4796 : }
4797 : return false;
4798 : }
4799 :
4800 : /****************************************************************************/
|