LCOV - code coverage report
Current view: top level - src/microsim - MSEdge.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 95.8 % 801 767
Test Date: 2026-05-24 16:29:35 Functions: 95.5 % 89 85

            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    MSEdge.cpp
      15              : /// @author  Christian Roessel
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Christoph Sommer
      18              : /// @author  Daniel Krajzewicz
      19              : /// @author  Laura Bieker
      20              : /// @author  Michael Behrisch
      21              : /// @author  Sascha Krieg
      22              : /// @date    Tue, 06 Mar 2001
      23              : ///
      24              : // A road/street connecting two junctions
      25              : /****************************************************************************/
      26              : #include <config.h>
      27              : 
      28              : #include <algorithm>
      29              : #include <iostream>
      30              : #include <cassert>
      31              : #ifdef HAVE_FOX
      32              : #include <utils/common/ScopedLocker.h>
      33              : #endif
      34              : #include <utils/common/StringTokenizer.h>
      35              : #include <utils/options/OptionsCont.h>
      36              : #include <microsim/devices/MSRoutingEngine.h>
      37              : #include <mesosim/MELoop.h>
      38              : #include <mesosim/MESegment.h>
      39              : #include <mesosim/MEVehicle.h>
      40              : #include "MSInsertionControl.h"
      41              : #include "MSJunction.h"
      42              : #include "MSLane.h"
      43              : #include "MSLaneChanger.h"
      44              : #include "MSLaneChangerSublane.h"
      45              : #include "MSLink.h"
      46              : #include "MSGlobals.h"
      47              : #include "MSNet.h"
      48              : #include "MSVehicle.h"
      49              : #include "MSLeaderInfo.h"
      50              : #include <microsim/transportables/MSTransportable.h>
      51              : #include "MSEdgeWeightsStorage.h"
      52              : #include "MSEdge.h"
      53              : 
      54              : #define BEST_LANE_LOOKAHEAD 3000.0
      55              : 
      56              : // ===========================================================================
      57              : // static member definitions
      58              : // ===========================================================================
      59              : MSEdge::DictType MSEdge::myDict;
      60              : MSEdgeVector MSEdge::myEdges;
      61              : SVCPermissions MSEdge::myMesoIgnoredVClasses(0);
      62              : DepartLaneDefinition MSEdge::myDefaultDepartLaneDefinition(DepartLaneDefinition::DEFAULT);
      63              : int MSEdge::myDefaultDepartLane(0);
      64              : 
      65              : // ===========================================================================
      66              : // member method definitions
      67              : // ===========================================================================
      68      1974278 : MSEdge::MSEdge(const std::string& id, int numericalID,
      69              :                const SumoXMLEdgeFunc function,
      70              :                const std::string& streetName,
      71              :                const std::string& edgeType,
      72              :                const std::string& routingType,
      73              :                int priority,
      74      1974278 :                double distance) :
      75      1974278 :     Named(id), myNumericalID(numericalID), myLanes(nullptr),
      76      1974278 :     myLaneChanger(nullptr), myFunction(function), myVaporizationRequests(0),
      77      1974278 :     myLastFailedInsertionTime(-1),
      78      1974278 :     myFromJunction(nullptr), myToJunction(nullptr),
      79      1974278 :     myHaveTransientPermissions(false),
      80      1974278 :     myOtherTazConnector(nullptr),
      81      1974278 :     myStreetName(streetName),
      82      1974278 :     myEdgeType(edgeType),
      83      1974278 :     myRoutingType(routingType),
      84      1974278 :     myPriority(priority),
      85      1974278 :     myDistance(distance),
      86      1974278 :     myWidth(0.),
      87      1974278 :     myLength(0.),
      88      1974278 :     myEmptyTraveltime(0.),
      89      1974278 :     myTimePenalty(0.),
      90      1974278 :     myAmDelayed(false),
      91      1974278 :     myAmRoundabout(false),
      92      1974278 :     myAmFringe(true),
      93      3948556 :     myBidiEdge(nullptr)
      94      1974278 : { }
      95              : 
      96              : 
      97      3404118 : MSEdge::~MSEdge() {
      98      1958986 :     delete myLaneChanger;
      99      1958986 :     delete myReversedRoutingEdge;
     100      1958986 :     delete myRailwayRoutingEdge;
     101     13199048 : }
     102              : 
     103              : 
     104              : void
     105      1838796 : MSEdge::initialize(const std::vector<MSLane*>* lanes) {
     106              :     assert(lanes != 0);
     107      1838796 :     myLanes = std::shared_ptr<const std::vector<MSLane*> >(lanes);
     108      1838796 :     if (myFunction == SumoXMLEdgeFunc::CONNECTOR) {
     109        70006 :         myCombinedPermissions = SVCAll;
     110              :     }
     111      3967791 :     for (MSLane* const lane : *lanes) {
     112      2128995 :         lane->setRightSideOnEdge(myWidth, (int)mySublaneSides.size());
     113      2128995 :         MSLeaderInfo ahead(lane->getWidth());
     114      4971581 :         for (int j = 0; j < ahead.numSublanes(); ++j) {
     115      2842586 :             mySublaneSides.push_back(myWidth + j * MSGlobals::gLateralResolution);
     116              :         }
     117      2128995 :         myWidth += lane->getWidth();
     118      2128995 :     }
     119      1838796 : }
     120              : 
     121              : 
     122      1862242 : void MSEdge::recalcCache() {
     123      1862242 :     if (myLanes->empty()) {
     124              :         return;
     125              :     }
     126      1862242 :     myLength = myLanes->front()->getLength();
     127      3724484 :     myEmptyTraveltime = myLength / MAX2(getSpeedLimit(), NUMERICAL_EPS);
     128      1862242 :     if (isNormal() && (MSGlobals::gUseMesoSim || MSGlobals::gTLSPenalty > 0)) {
     129              :         SUMOTime minorPenalty = 0;
     130       186892 :         bool haveTLSPenalty = MSGlobals::gTLSPenalty > 0;
     131       186892 :         if (MSGlobals::gUseMesoSim) {
     132       186777 :             const MESegment::MesoEdgeType& edgeType = MSNet::getInstance()->getMesoType(getEdgeType());
     133       186777 :             minorPenalty = edgeType.minorPenalty;
     134       186777 :             haveTLSPenalty = edgeType.tlsPenalty > 0;
     135              :         }
     136       186892 :         if (haveTLSPenalty || minorPenalty > 0) {
     137              :             // add tls penalties to the minimum travel time
     138              :             SUMOTime minPenalty = -1;
     139         2555 :             for (const MSLane* const l : *myLanes) {
     140         4099 :                 for (const MSLink* const link : l->getLinkCont()) {
     141         2387 :                     if (link->getLane()->isWalkingArea() && link->getLaneBefore()->isNormal()) {
     142           60 :                         continue;
     143              :                     }
     144         2327 :                     SUMOTime linkPenalty = link->isTLSControlled() ? link->getMesoTLSPenalty() : (link->havePriority() ? 0 : minorPenalty);
     145         2327 :                     if (minPenalty == -1) {
     146              :                         minPenalty = linkPenalty;
     147              :                     } else {
     148              :                         minPenalty = MIN2(minPenalty, linkPenalty);
     149              :                     }
     150              :                 }
     151              :             }
     152          843 :             if (minPenalty > 0) {
     153          285 :                 myEmptyTraveltime += STEPS2TIME(minPenalty);
     154          285 :                 myTimePenalty = STEPS2TIME(minPenalty);
     155              :             }
     156              :         }
     157      1675350 :     } else if (isCrossing() && MSGlobals::gTLSPenalty > 0) {
     158              :         // penalties are recorded for the entering link
     159          112 :         for (const auto& ili : myLanes->front()->getIncomingLanes()) {
     160           56 :             double penalty = STEPS2TIME(ili.viaLink->getMesoTLSPenalty());
     161           56 :             if (!ili.viaLink->haveOffPriority()) {
     162            0 :                 penalty = MAX2(penalty, MSGlobals::gMinorPenalty);
     163              :             }
     164           56 :             if (penalty > 0) {
     165           20 :                 myEmptyTraveltime += penalty;
     166           20 :                 myTimePenalty = penalty;
     167              :             }
     168              :         }
     169      1675294 :     } else if (isInternal() && MSGlobals::gUsingInternalLanes) {
     170       975195 :         const MSLink* link = myLanes->front()->getIncomingLanes()[0].viaLink;
     171       975195 :         if (!link->isTLSControlled() && !link->havePriority()) {
     172       485038 :             if (link->isTurnaround()) {
     173       184122 :                 myEmptyTraveltime += MSGlobals::gTurnaroundPenalty;
     174       184122 :                 myTimePenalty = MSGlobals::gTurnaroundPenalty;
     175              :             } else {
     176       300916 :                 myEmptyTraveltime += MSGlobals::gMinorPenalty;
     177       300916 :                 myTimePenalty = MSGlobals::gMinorPenalty;
     178              :             }
     179              :         }
     180              :     }
     181              : }
     182              : 
     183              : 
     184              : void
     185           42 : MSEdge::resetTAZ(MSJunction* junction) {
     186              :     mySuccessors.clear();
     187              :     myPredecessors.clear();
     188          728 :     for (const MSEdge* edge : junction->getIncoming()) {
     189          686 :         if (!edge->isInternal()) {
     190          126 :             MSEdgeVector& succ = const_cast<MSEdgeVector&>(edge->mySuccessors);
     191              :             MSConstEdgePairVector& succVia = const_cast<MSConstEdgePairVector&>(edge->myViaSuccessors);
     192          126 :             MSEdgeVector& pred = const_cast<MSEdgeVector&>(edge->myPredecessors);
     193          126 :             auto it = std::find(succ.begin(), succ.end(), this);
     194          126 :             auto it2 = std::find(succVia.begin(), succVia.end(), std::make_pair(const_cast<const MSEdge*>(this), (const MSEdge*)nullptr));
     195          126 :             auto it3 = std::find(pred.begin(), pred.end(), this);
     196          126 :             if (it != succ.end()) {
     197              :                 succ.erase(it);
     198              :                 succVia.erase(it2);
     199              :             }
     200          126 :             if (it3 != pred.end()) {
     201              :                 pred.erase(it3);
     202              :             }
     203              :         }
     204              :     }
     205           42 : }
     206              : 
     207              : void
     208      1766272 : MSEdge::closeBuilding() {
     209      3892326 :     for (MSLane* const lane : *myLanes) {
     210      5048222 :         for (MSLink* const link : lane->getLinkCont()) {
     211      2922168 :             link->initParallelLinks();
     212              :             MSLane* const toL = link->getLane();
     213              :             MSLane* const viaL = link->getViaLane();
     214      2922168 :             if (toL != nullptr) {
     215              :                 MSEdge& to = toL->getEdge();
     216      2922168 :                 if (std::find(mySuccessors.begin(), mySuccessors.end(), &to) == mySuccessors.end()) {
     217      2722226 :                     mySuccessors.push_back(&to);
     218      5444452 :                     myViaSuccessors.push_back(std::make_pair(&to, (viaL == nullptr ? nullptr : &viaL->getEdge())));
     219              :                 }
     220      2922168 :                 if (std::find(to.myPredecessors.begin(), to.myPredecessors.end(), this) == to.myPredecessors.end()) {
     221      2722226 :                     to.myPredecessors.push_back(this);
     222              :                 }
     223      2922168 :                 if (link->getDirection() != LinkDirection::TURN) {
     224      2280326 :                     myAmFringe = false;
     225              :                 }
     226              :             }
     227      2922168 :             if (viaL != nullptr) {
     228              :                 MSEdge& to = viaL->getEdge();
     229      1020415 :                 if (std::find(to.myPredecessors.begin(), to.myPredecessors.end(), this) == to.myPredecessors.end()) {
     230       939777 :                     to.myPredecessors.push_back(this);
     231              :                 }
     232              :             }
     233              :         }
     234      2126054 :         lane->checkBufferType();
     235              :     }
     236      1766272 :     std::sort(mySuccessors.begin(), mySuccessors.end(), by_id_sorter());
     237      1766272 :     rebuildAllowedLanes(true);
     238      1766272 :     recalcCache();
     239              : 
     240              :     // extend lookup table for sublane model after all edges are read
     241      1766272 :     if (myLanes->back()->getOpposite() != nullptr) {
     242         8270 :         MSLane* opposite = myLanes->back()->getOpposite();
     243         8270 :         MSLeaderInfo ahead(opposite->getWidth());
     244        22558 :         for (int j = 0; j < ahead.numSublanes(); ++j) {
     245        14288 :             mySublaneSides.push_back(myWidth + j * MSGlobals::gLateralResolution);
     246              :         }
     247         8270 :     }
     248      1766272 : }
     249              : 
     250              : 
     251              : void
     252      1766264 : MSEdge::postLoadInitLaneChanger() {
     253      1766264 :     if (myLaneChanger != nullptr) {
     254       442235 :         myLaneChanger->postloadInitLC();
     255              :     }
     256      1766264 : }
     257              : 
     258              : void
     259      1766272 : MSEdge::buildLaneChanger() {
     260      1766272 :     if (!myLanes->empty()) {
     261      1766272 :         const bool allowChanging = allowsLaneChanging();
     262      1766272 :         if (MSGlobals::gLateralResolution > 0) {
     263              :             // may always initiate sublane-change
     264       183546 :             if (!isInternal() || MSGlobals::gUsingInternalLanes) {
     265       183355 :                 myLaneChanger = new MSLaneChangerSublane(myLanes.get(), allowChanging);
     266              :             }
     267              :         } else {
     268      1582726 :             if (MSGlobals::gLaneChangeDuration > 0) {
     269         4981 :                 myLaneChanger = new MSLaneChanger(myLanes.get(), allowChanging);
     270      1577745 :             } else if (myLanes->size() > 1 || canChangeToOpposite()) {
     271       253907 :                 myLaneChanger = new MSLaneChanger(myLanes.get(), allowChanging);
     272              :             }
     273              :         }
     274              :     }
     275      1766272 : }
     276              : 
     277              : 
     278              : bool
     279      1766272 : MSEdge::allowsLaneChanging() const {
     280      1766272 :     if (isInternal() && MSGlobals::gUsingInternalLanes) {
     281              :         // allow changing only if all links leading to this internal lane have priority
     282              :         // or they are controlled by a traffic light
     283      1482719 :         for (const MSLane* const lane : *myLanes) {
     284      1017021 :             const MSLink* const link = lane->getLogicalPredecessorLane()->getLinkTo(lane);
     285              :             assert(link != nullptr);
     286              :             const LinkState state = link->getState();
     287       463412 :             if ((state == LINKSTATE_MINOR && lane->getBidiLane() == nullptr)
     288       554035 :                     || state == LINKSTATE_EQUAL
     289       554035 :                     || state == LINKSTATE_STOP
     290              :                     || state == LINKSTATE_ALLWAY_STOP
     291      1017021 :                     || state == LINKSTATE_DEADEND) {
     292              :                 return false;
     293              :             }
     294              :         }
     295              :     }
     296              :     return true;
     297              : }
     298              : 
     299              : 
     300              : void
     301     18788643 : MSEdge::addToAllowed(const SVCPermissions permissions, std::shared_ptr<const std::vector<MSLane*> > allowedLanes, AllowedLanesCont& laneCont) const {
     302     18788643 :     if (!allowedLanes->empty()) {
     303              :         // recheck whether we had this list to save memory
     304     18524170 :         for (auto& allowed : laneCont) {
     305     17487292 :             if (*allowed.second == *allowedLanes) {
     306     13158932 :                 allowed.first |= permissions;
     307              :                 return;
     308              :             }
     309              :         }
     310      1036878 :         laneCont.push_back(std::make_pair(permissions, allowedLanes));
     311              :     }
     312              : }
     313              : 
     314              : 
     315              : SVCPermissions
     316      2955070 : MSEdge::getMesoPermissions(SVCPermissions p, SVCPermissions ignoreIgnored) {
     317      2955070 :     SVCPermissions ignored = myMesoIgnoredVClasses & ~ignoreIgnored;
     318      2955070 :     return (p | ignored) == ignored ? 0 : p;
     319              : }
     320              : 
     321              : 
     322              : void
     323      1767795 : MSEdge::rebuildAllowedLanes(const bool onInit, bool updateVehicles) {
     324              :     // rebuild myMinimumPermissions and myCombinedPermissions
     325      1767795 :     myMinimumPermissions = SVCAll;
     326      1767795 :     myCombinedPermissions = 0;
     327              :     bool lanesChangedPermission = false;
     328      3895892 :     for (MSLane* const lane : *myLanes) {
     329              :         // same dedicated lanes are ignored in meso to avoid capacity errors.
     330              :         // Here we have to make sure that vehicles which are set to depart on
     331              :         // such lanes trigger an error.
     332      2128097 :         SVCPermissions allow = getMesoPermissions(lane->getPermissions(), SVC_PEDESTRIAN);
     333      2128097 :         myMinimumPermissions &= allow;
     334      2128097 :         myCombinedPermissions |= allow;
     335      2128097 :         lanesChangedPermission |= lane->hadPermissionChanges();
     336              :     }
     337      1767795 :     if (!onInit && !myHaveTransientPermissions && lanesChangedPermission) {
     338         1086 :         myHaveTransientPermissions = true;
     339              :         // backup original structures when first needed
     340         1086 :         myOrigAllowed = myAllowed;
     341              :         myOrigAllowedTargets = myAllowedTargets;
     342              :         myOrigClassesViaSuccessorMap = myClassesViaSuccessorMap;
     343              :     }
     344              :     // rebuild myAllowed
     345              :     myAllowed.clear();
     346      1767795 :     if (myCombinedPermissions != myMinimumPermissions) {
     347       169217 :         myAllowed.push_back(std::make_pair(SVC_IGNORING, myLanes));
     348      5753378 :         for (SVCPermissions vclass = SVC_PRIVATE; vclass <= SUMOVehicleClass_MAX; vclass *= 2) {
     349      5584161 :             if ((myCombinedPermissions & vclass) == vclass) {
     350              :                 std::shared_ptr<std::vector<MSLane*> > allowedLanes = std::make_shared<std::vector<MSLane*> >();
     351     11849055 :                 for (MSLane* const lane : *myLanes) {
     352      8021924 :                     if (lane->allowsVehicleClass((SUMOVehicleClass)vclass)) {
     353      4049064 :                         allowedLanes->push_back(lane);
     354              :                     }
     355              :                 }
     356      7654262 :                 addToAllowed(vclass, allowedLanes, myAllowed);
     357              :             }
     358              :         }
     359              :     }
     360      1767795 :     if (onInit) {
     361      1766272 :         myOriginalMinimumPermissions = myMinimumPermissions;
     362      1766272 :         myOriginalCombinedPermissions = myCombinedPermissions;
     363              :     } else {
     364         1523 :         rebuildAllowedTargets(updateVehicles);
     365         3925 :         for (MSEdge* pred : myPredecessors) {
     366         2402 :             if (myHaveTransientPermissions && !pred->myHaveTransientPermissions) {
     367         1551 :                 pred->myOrigAllowed = pred->myAllowed;
     368              :                 pred->myOrigAllowedTargets = pred->myAllowedTargets;
     369              :                 pred->myOrigClassesViaSuccessorMap = pred->myClassesViaSuccessorMap;
     370         1551 :                 pred->myHaveTransientPermissions = true;
     371              :             }
     372         2402 :             pred->rebuildAllowedTargets(updateVehicles);
     373              :         }
     374         1523 :         if (MSGlobals::gUseMesoSim) {
     375         2625 :             for (MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(*this); s != nullptr; s = s->getNextSegment()) {
     376         2226 :                 s->updatePermissions();
     377              :             }
     378              :         }
     379              :     }
     380      1767795 : }
     381              : 
     382              : 
     383              : void
     384      1771271 : MSEdge::rebuildAllowedTargets(const bool updateVehicles) {
     385              :     myAllowedTargets.clear();
     386      4501041 :     for (const MSEdge* target : mySuccessors) {
     387              :         bool universalMap = true; // whether the mapping for SVC_IGNORING is also valid for all vehicle classes
     388              :         std::shared_ptr<std::vector<MSLane*> > allLanes = std::make_shared<std::vector<MSLane*> >();
     389              :         // compute the mapping for SVC_IGNORING
     390      6225278 :         for (MSLane* const lane : *myLanes) {
     391              :             SVCPermissions combinedTargetPermissions = 0;
     392      9922069 :             for (const MSLink* const link : lane->getLinkCont()) {
     393      6426561 :                 if (&link->getLane()->getEdge() == target) {
     394      2930787 :                     allLanes->push_back(lane);
     395      2930787 :                     combinedTargetPermissions |= link->getLane()->getPermissions();
     396      2930787 :                     if (link->getViaLane() != nullptr &&
     397      1023072 :                             ((lane->getPermissions() & link->getLane()->getPermissions()) != link->getViaLane()->getPermissions())) {
     398              :                         // custom connection permissions
     399              :                         universalMap = false;
     400              :                     }
     401              :                 }
     402              :             }
     403      3495508 :             if (combinedTargetPermissions == 0 || (lane->getPermissions() & combinedTargetPermissions) != lane->getPermissions()) {
     404              :                 universalMap = false;
     405              :             }
     406              :         }
     407      2729770 :         if (universalMap) {
     408      2218430 :             if (myAllowed.empty()) {
     409              :                 // we have no lane specific permissions
     410      4368474 :                 myAllowedTargets[target].push_back(std::make_pair(myMinimumPermissions, myLanes));
     411              :             } else {
     412       134540 :                 for (const auto& i : myAllowed) {
     413       301041 :                     addToAllowed(i.first, i.second, myAllowedTargets[target]);
     414              :                 }
     415              :             }
     416              :         } else {
     417      1022680 :             addToAllowed(SVC_IGNORING, allLanes, myAllowedTargets[target]);
     418              :             // compute the vclass specific mapping
     419     17385560 :             for (SVCPermissions vclass = SVC_PRIVATE; vclass <= SUMOVehicleClass_MAX; vclass *= 2) {
     420     16874220 :                 if ((myCombinedPermissions & vclass) == vclass) {
     421              :                     std::shared_ptr<std::vector<MSLane*> > allowedLanes = std::make_shared<std::vector<MSLane*> >();
     422     44616009 :                     for (MSLane* const lane : *myLanes) {
     423     30266184 :                         if (lane->allowsVehicleClass((SUMOVehicleClass)vclass)) {
     424     57946241 :                             for (const MSLink* const link : lane->getLinkCont()) {
     425     37884639 :                                 if (link->getLane()->allowsVehicleClass((SUMOVehicleClass)vclass) && &link->getLane()->getEdge() == target && (link->getViaLane() == nullptr || link->getViaLane()->allowsVehicleClass((SUMOVehicleClass)vclass))) {
     426     10112588 :                                     allowedLanes->push_back(lane);
     427              :                                 }
     428              :                             }
     429              :                         }
     430              :                     }
     431     43049475 :                     addToAllowed(vclass, allowedLanes, myAllowedTargets[target]);
     432              :                 }
     433              :             }
     434              :         }
     435              :     }
     436      1771271 :     if (updateVehicles) {
     437         3161 :         for (const MSLane* const lane : *myLanes) {
     438         1844 :             const MSLane::VehCont& vehs = lane->getVehiclesSecure();
     439         4275 :             for (MSVehicle* veh : vehs) {
     440         2431 :                 veh->updateBestLanes(true);
     441              :             }
     442         1844 :             lane->releaseVehicles();
     443              :         }
     444              :     }
     445              :     myClassesSuccessorMap.clear();
     446      1771271 : }
     447              : 
     448              : 
     449              : // ------------ Access to the edge's lanes
     450              : MSLane*
     451          774 : MSEdge::leftLane(const MSLane* const lane) const {
     452          774 :     return parallelLane(lane, 1);
     453              : }
     454              : 
     455              : 
     456              : MSLane*
     457          400 : MSEdge::rightLane(const MSLane* const lane) const {
     458          400 :     return parallelLane(lane, -1);
     459              : }
     460              : 
     461              : 
     462              : MSLane*
     463     84192117 : MSEdge::parallelLane(const MSLane* const lane, int offset, bool includeOpposite) const {
     464     84192117 :     const int resultIndex = lane->getIndex() + offset;
     465     84192117 :     if (resultIndex >= getNumLanes() && includeOpposite) {
     466     20526812 :         const MSEdge* opposite = getOppositeEdge();
     467     20526812 :         if (opposite != nullptr && resultIndex < getNumLanes() + opposite->getNumLanes()) {
     468      1379221 :             return opposite->getLanes()[opposite->getNumLanes() + getNumLanes() - resultIndex - 1];
     469              :         }
     470              :         return nullptr;
     471     63665305 :     } else if (resultIndex >= (int)myLanes->size() || resultIndex < 0) {
     472              :         return nullptr;
     473              :     } else {
     474     38781082 :         return (*myLanes)[resultIndex];
     475              :     }
     476              : }
     477              : 
     478              : 
     479              : const std::vector<MSLane*>*
     480     76665889 : MSEdge::allowedLanes(const MSEdge& destination, SUMOVehicleClass vclass, bool ignoreTransientPermissions) const {
     481     76665889 :     const auto& targets = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigAllowedTargets : myAllowedTargets;
     482              :     AllowedLanesByTarget::const_iterator i = targets.find(&destination);
     483     76665889 :     if (i != targets.end()) {
     484     76640124 :         for (const auto& allowed : i->second) {
     485     76516963 :             if ((allowed.first & vclass) == vclass) {
     486              :                 return allowed.second.get();
     487              :             }
     488              :         }
     489              :     }
     490              :     return nullptr;
     491              : }
     492              : 
     493              : 
     494              : const std::vector<MSLane*>*
     495    274488701 : MSEdge::allowedLanes(SUMOVehicleClass vclass) const {
     496    274488701 :     if ((myMinimumPermissions & vclass) == vclass) {
     497     43539127 :         return myLanes.get();
     498              :     } else {
     499    230949574 :         if ((myCombinedPermissions & vclass) == vclass) {
     500    461895693 :             for (const auto& allowed : myAllowed) {
     501    461895693 :                 if ((allowed.first & vclass) == vclass) {
     502              :                     return allowed.second.get();
     503              :                 }
     504              :             }
     505              :         }
     506         1989 :         return nullptr;
     507              :     }
     508              : }
     509              : 
     510              : 
     511              : const std::vector<MSLane*>*
     512   2176082689 : MSEdge::allowedLanes(SUMOVehicleClass vclass, bool ignoreTransientPermissions) const {
     513   2176082689 :     const SVCPermissions& minP = ignoreTransientPermissions ? myOriginalMinimumPermissions : myMinimumPermissions;
     514   2176082689 :     if ((minP & vclass) == vclass) {
     515    638133054 :         return myLanes.get();
     516              :     } else {
     517   1537949635 :         const SVCPermissions comP = ignoreTransientPermissions ? myOriginalCombinedPermissions : myCombinedPermissions;
     518   1537949635 :         if ((comP & vclass) == vclass) {
     519   1537949563 :             const AllowedLanesCont& allowedCont = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigAllowed : myAllowed;
     520   3081597260 :             for (const auto& allowed : allowedCont) {
     521   3081597260 :                 if ((allowed.first & vclass) == vclass) {
     522              :                     return allowed.second.get();
     523              :                 }
     524              :             }
     525              :         }
     526           72 :         return nullptr;
     527              :     }
     528              : }
     529              : 
     530              : 
     531              : // ------------
     532              : SUMOTime
     533          433 : MSEdge::incVaporization(SUMOTime) {
     534          433 :     ++myVaporizationRequests;
     535          433 :     return 0;
     536              : }
     537              : 
     538              : 
     539              : SUMOTime
     540          279 : MSEdge::decVaporization(SUMOTime) {
     541          279 :     --myVaporizationRequests;
     542          279 :     return 0;
     543              : }
     544              : 
     545              : 
     546              : MSLane*
     547    217469968 : MSEdge::getFreeLane(const std::vector<MSLane*>* allowed, const SUMOVehicleClass vclass, double departPos) const {
     548    217469968 :     if (allowed == nullptr) {
     549    184339616 :         allowed = allowedLanes(vclass);
     550              :     }
     551              :     MSLane* res = nullptr;
     552    184339616 :     if (allowed != nullptr) {
     553              :         double largestGap = 0;
     554              :         MSLane* resByGap = nullptr;
     555              :         double leastOccupancy = std::numeric_limits<double>::max();
     556    460327908 :         for (std::vector<MSLane*>::const_iterator i = allowed->begin(); i != allowed->end(); ++i) {
     557    242859921 :             const double occupancy = (*i)->getBruttoOccupancy();
     558    242859921 :             if (occupancy < leastOccupancy) {
     559    225896041 :                 res = (*i);
     560              :                 leastOccupancy = occupancy;
     561              :             }
     562    242859921 :             const MSVehicle* last = (*i)->getLastFullVehicle();
     563    242859921 :             const double lastGap = (last != nullptr ? last->getPositionOnLane() : myLength) - departPos;
     564    242859921 :             if (lastGap > largestGap) {
     565              :                 largestGap = lastGap;
     566     58379342 :                 resByGap = (*i);
     567              :             }
     568              :         }
     569    217467987 :         if (resByGap != nullptr) {
     570              :             //if (res != resByGap) std::cout << SIMTIME << " edge=" << getID() << " departPos=" << departPos << " res=" << Named::getIDSecure(res) << " resByGap=" << Named::getIDSecure(resByGap) << " largestGap=" << largestGap << "\n";
     571              :             res = resByGap;
     572              :         }
     573              :     }
     574    217469968 :     return res;
     575              : }
     576              : 
     577              : 
     578              : MSLane*
     579    839494245 : MSEdge::getProbableLane(const std::vector<MSLane*>* allowed, const SUMOVehicleClass vclass, double departPos, double maxSpeed) const {
     580    839494245 :     if (allowed == nullptr) {
     581            0 :         allowed = allowedLanes(vclass);
     582              :     }
     583              :     MSLane* res = nullptr;
     584            0 :     if (allowed != nullptr) {
     585              :         double largestGap = 0;
     586              :         double largestSpeed = 0;
     587              :         MSLane* resByGap = nullptr;
     588              :         double leastOccupancy = std::numeric_limits<double>::max();
     589              :         int aIndex = 0;
     590   1685049013 :         for (std::vector<MSLane*>::const_iterator i = allowed->begin(); i != allowed->end(); ++i, aIndex++) {
     591    845554768 :             const double occupancy = (*i)->getBruttoOccupancy();
     592    845554768 :             if (occupancy < leastOccupancy) {
     593    844829733 :                 res = (*i);
     594              :                 leastOccupancy = occupancy;
     595              :             }
     596    845554768 :             const MSVehicle* last = (*i)->getLastFullVehicle();
     597    845554768 :             double lastGap = (last != nullptr ? last->getPositionOnLane() : myLength) - departPos;
     598              :             // never insert to the left of a vehicle with a larger speedFactor
     599    845554768 :             if (lastGap > largestGap && maxSpeed >= largestSpeed) {
     600              :                 largestGap = lastGap;
     601    800349882 :                 resByGap = (*i);
     602              :             }
     603    845554768 :             if (last != nullptr) {
     604    843309322 :                 largestSpeed = MAX2(largestSpeed, getVehicleMaxSpeed(last));
     605              :             }
     606              :         }
     607    839494245 :         if (resByGap != nullptr) {
     608              :             //if (res != resByGap) std::cout << SIMTIME << " edge=" << getID() << " departPos=" << departPos << " res=" << Named::getIDSecure(res) << " resByGap=" << Named::getIDSecure(resByGap) << " largestGap=" << largestGap << "\n";
     609              :             res = resByGap;
     610              :         }
     611              :     }
     612    839494245 :     return res;
     613              : }
     614              : 
     615              : 
     616              : double
     617   1058073655 : MSEdge::getDepartPosBound(const MSVehicle& veh, bool upper) const {
     618   1058073655 :     const SUMOVehicleParameter& pars = veh.getParameter();
     619              :     double pos = getLength();
     620              :     // determine the position
     621   1058073655 :     switch (pars.departPosProcedure) {
     622     10036639 :         case DepartPosDefinition::GIVEN:
     623     10036639 :             pos = pars.departPos;
     624     10036639 :             if (pos < 0.) {
     625      2860457 :                 pos += myLength;
     626              :             }
     627              :             break;
     628              :         case DepartPosDefinition::RANDOM:
     629              :             // could be any position on the edge
     630              :             break;
     631              :         case DepartPosDefinition::RANDOM_FREE:
     632              :             // could be any position on the edge due to multiple random attempts
     633              :             break;
     634              :         case DepartPosDefinition::FREE:
     635              :             // many candidate positions, upper bound could be computed exactly
     636              :             // with much effort
     637              :             break;
     638     11726450 :         case DepartPosDefinition::LAST:
     639     11726450 :             if (upper) {
     640       468180 :                 for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
     641       317928 :                     MSVehicle* last = (*i)->getLastFullVehicle();
     642       317928 :                     if (last != nullptr) {
     643       276090 :                         pos = MIN2(pos, last->getPositionOnLane());
     644              :                     }
     645              :                 }
     646              :             } else {
     647              :                 pos = 0;
     648              :             }
     649              :             break;
     650    831002180 :         case DepartPosDefinition::BASE:
     651              :         case DepartPosDefinition::DEFAULT:
     652    831002180 :             if (!upper) {
     653              :                 pos = 0;
     654              :             }
     655              :             break;
     656           28 :         default:
     657           28 :             pos = MIN2(pos, veh.getVehicleType().getLength());
     658              :             break;
     659              :     }
     660   1058073655 :     return pos;
     661              : }
     662              : 
     663              : 
     664              : MSLane*
     665           46 : MSEdge::getDepartLaneMeso(SUMOVehicle& veh) const {
     666           46 :     if (veh.getParameter().departLaneProcedure == DepartLaneDefinition::GIVEN) {
     667            3 :         if ((int) myLanes->size() <= veh.getParameter().departLane || !(*myLanes)[veh.getParameter().departLane]->allowsVehicleClass(veh.getVehicleType().getVehicleClass())) {
     668            0 :             return nullptr;
     669              :         }
     670            3 :         return (*myLanes)[veh.getParameter().departLane];
     671              :     }
     672           43 :     return (*myLanes)[0];
     673              : }
     674              : 
     675              : 
     676              : MSLane*
     677   1746965088 : MSEdge::getDepartLane(MSVehicle& veh) const {
     678   1746965088 :     DepartLaneDefinition dld = veh.getParameter().departLaneProcedure;
     679   1746965088 :     int departLane = veh.getParameter().departLane;
     680   1746965088 :     if (dld == DepartLaneDefinition::DEFAULT) {
     681   1324262044 :         dld = myDefaultDepartLaneDefinition;
     682   1324262044 :         departLane = myDefaultDepartLane;
     683              :     }
     684   1746965088 :     switch (dld) {
     685    108030569 :         case DepartLaneDefinition::GIVEN:
     686    108030569 :             if ((int) myLanes->size() <= departLane || !(*myLanes)[departLane]->allowsVehicleClass(veh.getVehicleType().getVehicleClass())) {
     687           66 :                 return nullptr;
     688              :             }
     689    108030503 :             return (*myLanes)[departLane];
     690     90119700 :         case DepartLaneDefinition::RANDOM:
     691    180239400 :             return RandHelper::getRandomFrom(*allowedLanes(veh.getVehicleType().getVehicleClass()));
     692    184237432 :         case DepartLaneDefinition::FREE:
     693    184237432 :             return getFreeLane(nullptr, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
     694      4971244 :         case DepartLaneDefinition::ALLOWED_FREE:
     695      4971244 :             if (veh.getRoute().size() == 1) {
     696         5960 :                 return getFreeLane(nullptr, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
     697              :             } else {
     698      4965284 :                 return getFreeLane(allowedLanes(**(veh.getRoute().begin() + 1), veh.getVehicleType().getVehicleClass()), veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
     699              :             }
     700    867453016 :         case DepartLaneDefinition::BEST_FREE:
     701              :         case DepartLaneDefinition::BEST_PROB: {
     702    867453016 :             veh.updateBestLanes(false, myLanes->front());
     703    867453016 :             const std::vector<MSVehicle::LaneQ>& bl = veh.getBestLanes();
     704              :             double bestLength = -1;
     705   2561513512 :             for (std::vector<MSVehicle::LaneQ>::const_iterator i = bl.begin(); i != bl.end(); ++i) {
     706   1694060496 :                 if ((*i).length > bestLength) {
     707              :                     bestLength = (*i).length;
     708              :                 }
     709              :             }
     710              :             // beyond a certain length, all lanes are suitable
     711              :             // however, we still need to check departPos to avoid unsuitable insertion
     712              :             // (this is only possible in some cases)
     713              :             double departPos = 0;
     714    867453016 :             if (bestLength > BEST_LANE_LOOKAHEAD) {
     715      1411963 :                 departPos = getDepartPosBound(veh);
     716      1411963 :                 bestLength = MIN2(bestLength - departPos, BEST_LANE_LOOKAHEAD);
     717              :             }
     718    867453016 :             std::vector<MSLane*>* bestLanes = new std::vector<MSLane*>();
     719   2561513512 :             for (std::vector<MSVehicle::LaneQ>::const_iterator i = bl.begin(); i != bl.end(); ++i) {
     720   1694060496 :                 if (((*i).length - departPos) >= bestLength) {
     721    876118640 :                     if (isInternal()) {
     722           32 :                         for (MSLane* lane : *myLanes) {
     723           20 :                             if (lane->getNormalSuccessorLane() == (*i).lane) {
     724           12 :                                 bestLanes->push_back(lane);
     725              :                             }
     726              :                         }
     727              :                     } else {
     728    876118628 :                         bestLanes->push_back((*i).lane);
     729              :                     }
     730              :                 }
     731              :             }
     732              :             MSLane* ret = nullptr;
     733    867453016 :             if (veh.getParameter().departLaneProcedure == DepartLaneDefinition::BEST_FREE) {
     734     27958771 :                 ret = getFreeLane(bestLanes, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false));
     735              :             } else {
     736    839494245 :                 ret = getProbableLane(bestLanes, veh.getVehicleType().getVehicleClass(), getDepartPosBound(veh, false), getVehicleMaxSpeed(&veh));
     737              :             }
     738    867453016 :             delete bestLanes;
     739    867453016 :             return ret;
     740              :         }
     741    492153127 :         case DepartLaneDefinition::DEFAULT:
     742              :         case DepartLaneDefinition::FIRST_ALLOWED:
     743    492153127 :             return getFirstAllowed(veh.getVehicleType().getVehicleClass());
     744              :         default:
     745              :             break;
     746              :     }
     747            0 :     if (!(*myLanes)[0]->allowsVehicleClass(veh.getVehicleType().getVehicleClass())) {
     748              :         return nullptr;
     749              :     }
     750            0 :     return (*myLanes)[0];
     751              : }
     752              : 
     753              : 
     754              : MSLane*
     755    492909568 : MSEdge::getFirstAllowed(SUMOVehicleClass vClass, bool defaultFirst, int routingMode) const {
     756    499787121 :     for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
     757    499782121 :         if ((*i)->allowsVehicleClass(vClass, routingMode)) {
     758    492904568 :             return *i;
     759              :         }
     760              :     }
     761         5000 :     return defaultFirst && !myLanes->empty() ? myLanes->front() : nullptr;
     762              : }
     763              : 
     764              : 
     765              : bool
     766   2756471625 : MSEdge::validateDepartSpeed(SUMOVehicle& v) const {
     767   2756471625 :     const SUMOVehicleParameter& pars = v.getParameter();
     768   2756471625 :     const MSVehicleType& type = v.getVehicleType();
     769   2756471625 :     if (pars.departSpeedProcedure == DepartSpeedDefinition::GIVEN) {
     770              :         // departSpeed could have been rounded down in the output
     771    150375690 :         double vMax = getVehicleMaxSpeed(&v) + SPEED_EPS;
     772    150375690 :         if (pars.departSpeed > vMax) {
     773              :             // check departLane (getVehicleMaxSpeed checks lane 0)
     774        19609 :             MSLane* departLane = MSGlobals::gMesoNet ? getDepartLaneMeso(v) : getDepartLane(dynamic_cast<MSVehicle&>(v));
     775        19609 :             if (departLane != nullptr) {
     776        19609 :                 vMax = departLane->getVehicleMaxSpeed(&v);
     777        19609 :                 if (pars.wasSet(VEHPARS_SPEEDFACTOR_SET)) {
     778              :                     // speedFactor could have been rounded down in the output
     779            7 :                     vMax *= (1 + SPEED_EPS);
     780              :                 }
     781              :                 // additive term must come after multiplication!
     782        19609 :                 vMax += SPEED_EPS;
     783        19609 :                 if (pars.departSpeed > vMax) {
     784        19595 :                     if (type.getSpeedFactor().getParameter(1) > 0.) {
     785        39152 :                         v.setChosenSpeedFactor(type.computeChosenSpeedDeviation(pars.speedFactor, nullptr, pars.departSpeed / MIN2(getSpeedLimit(), type.getDesiredMaxSpeed() - SPEED_EPS)));
     786        19576 :                         if (v.getChosenSpeedFactor() > type.getSpeedFactor().getParameter(0) + 2 * type.getSpeedFactor().getParameter(1)) {
     787              :                             // only warn for significant deviation
     788        33536 :                             WRITE_WARNINGF(TL("Choosing new speed factor % for vehicle '%' to match departure speed % (max %)."),
     789              :                                            toString(v.getChosenSpeedFactor()), pars.id, pars.departSpeed, vMax);
     790              :                         }
     791              :                     } else {
     792              :                         return false;
     793              :                     }
     794              :                 }
     795              :             }
     796              :         }
     797              :     }
     798              :     return true;
     799              : }
     800              : 
     801              : 
     802              : bool
     803   2756933010 : MSEdge::insertVehicle(SUMOVehicle& v, SUMOTime time, const bool checkOnly, const bool forceCheck) const {
     804              :     // when vaporizing, no vehicles are inserted, but checking needs to be successful to trigger removal
     805   2756914224 :     if (isVaporizing() || isTazConnector()
     806   5513381935 :             || v.getRouteValidity(true, checkOnly) != MSBaseVehicle::ROUTE_VALID) {
     807       484152 :         return checkOnly;
     808              :     }
     809   2756448680 :     const SUMOVehicleParameter& pars = v.getParameter();
     810   2756448680 :     if (!validateDepartSpeed(v)) {
     811           14 :         if (MSGlobals::gCheckRoutes) {
     812           21 :             throw ProcessError(TLF("Departure speed for vehicle '%' is too high for the departure edge '%', time=%.",
     813           21 :                                    pars.id, getID(), time2string(time)));
     814              :         } else {
     815           21 :             WRITE_WARNINGF(TL("Departure speed for vehicle '%' is too high for the departure edge '%', time=%."),
     816              :                            pars.id, getID(), time2string(time));
     817              :         }
     818              :     }
     819   2756448673 :     if (MSGlobals::gUseMesoSim) {
     820    527696502 :         if (!forceCheck && myLastFailedInsertionTime == time) {
     821              :             return false;
     822              :         }
     823              :         double pos = 0.0;
     824     76214637 :         switch (pars.departPosProcedure) {
     825       595657 :             case DepartPosDefinition::GIVEN:
     826       595657 :                 if (pars.departPos >= 0.) {
     827              :                     pos = pars.departPos;
     828              :                 } else {
     829         8393 :                     pos = pars.departPos + getLength();
     830              :                 }
     831       595657 :                 if (pos < 0 || pos > getLength()) {
     832            6 :                     WRITE_WARNINGF(TL("Invalid departPos % given for vehicle '%', time=%. Inserting at lane end instead."),
     833              :                                    pos, v.getID(), time2string(time));
     834              :                     pos = getLength();
     835              :                 }
     836              :                 break;
     837              :             case DepartPosDefinition::RANDOM:
     838              :             case DepartPosDefinition::RANDOM_FREE:
     839              :                 pos = RandHelper::rand(getLength());
     840        44186 :                 break;
     841              :             default:
     842              :                 break;
     843              :         }
     844              :         bool result = false;
     845     76214637 :         MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this, pos);
     846              :         MEVehicle* veh = static_cast<MEVehicle*>(&v);
     847              :         int qIdx;
     848     76214637 :         if (pars.departPosProcedure == DepartPosDefinition::FREE) {
     849       486990 :             while (segment != nullptr && !result) {
     850       448198 :                 if (checkOnly) {
     851            6 :                     result = segment->hasSpaceFor(veh, time, qIdx, true) == time;
     852              :                 } else {
     853       448192 :                     result = segment->initialise(veh, time);
     854              :                 }
     855              :                 segment = segment->getNextSegment();
     856              :             }
     857              :         } else {
     858     76175845 :             if (checkOnly) {
     859     67139215 :                 result = segment->hasSpaceFor(veh, time, qIdx, true) == time;
     860              :             } else {
     861      9036630 :                 result = segment->initialise(veh, time);
     862              :             }
     863              :         }
     864     76214634 :         return result;
     865              :     }
     866   2228752171 :     if (checkOnly) {
     867    615910718 :         DepartLaneDefinition dld = v.getParameter().departLaneProcedure;
     868    615910718 :         if (dld == DepartLaneDefinition::DEFAULT) {
     869    561224306 :             dld = myDefaultDepartLaneDefinition;
     870              :         }
     871    615910718 :         switch (dld) {
     872     80077148 :             case DepartLaneDefinition::GIVEN:
     873              :             case DepartLaneDefinition::DEFAULT:
     874              :             case DepartLaneDefinition::FIRST_ALLOWED: {
     875     80077148 :                 MSLane* insertionLane = getDepartLane(static_cast<MSVehicle&>(v));
     876     80077148 :                 if (insertionLane == nullptr) {
     877            0 :                     WRITE_WARNINGF(TL("Could not insert vehicle '%' on any lane of edge '%', time=%."),
     878              :                                    v.getID(), getID(), time2string(time));
     879            0 :                     return false;
     880              :                 }
     881     80077148 :                 const double occupancy = insertionLane->getBruttoOccupancy();
     882     80077148 :                 return (occupancy == 0 || occupancy * myLength + v.getVehicleType().getLengthWithGap() <= myLength ||
     883      5551272 :                         v.getParameter().departProcedure == DepartDefinition::SPLIT);
     884              :             }
     885    535833570 :             default:
     886    553342844 :                 for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
     887    545147969 :                     const double occupancy = (*i)->getBruttoOccupancy();
     888    545147969 :                     if (occupancy == 0 || occupancy * myLength + v.getVehicleType().getLengthWithGap() <= myLength ||
     889     17509274 :                             v.getParameter().departProcedure == DepartDefinition::SPLIT) {
     890              :                         return true;
     891              :                     }
     892              :                 }
     893              :         }
     894              :         return false;
     895              :     }
     896   1612841453 :     MSLane* insertionLane = getDepartLane(static_cast<MSVehicle&>(v));
     897   1612841453 :     if (insertionLane == nullptr) {
     898              :         return false;
     899              :     }
     900              : 
     901   1612836453 :     if (!forceCheck) {
     902   1612836276 :         if (myLastFailedInsertionTime == time) {
     903              :             if (myFailedInsertionMemory.count(insertionLane->getIndex())) {
     904              :                 // A vehicle was already rejected for the proposed insertionLane in this timestep
     905              :                 return false;
     906              :             }
     907              :         } else {
     908              :             // last rejection occurred in a previous timestep, clear cache
     909              :             myFailedInsertionMemory.clear();
     910              :         }
     911              :     }
     912              : 
     913     13529681 :     bool success = insertionLane->insertVehicle(static_cast<MSVehicle&>(v));
     914              : 
     915     13529678 :     if (!success) {
     916              :         // constraints may enforce explicit re-ordering so we need to try other vehicles after failure
     917     20139544 :         if (!insertionLane->hasParameter("insertionOrder" + v.getID())) {
     918     10069625 :             myFailedInsertionMemory.insert(insertionLane->getIndex());
     919              :         }
     920              :     }
     921              :     return success;
     922              : }
     923              : 
     924              : 
     925              : void
     926     42591435 : MSEdge::changeLanes(SUMOTime t) const {
     927     42591435 :     if (myLaneChanger != nullptr) {
     928     42591435 :         myLaneChanger->laneChange(t);
     929              :     }
     930     42591435 : }
     931              : 
     932              : 
     933              : const MSEdge*
     934      5269324 : MSEdge::getInternalFollowingEdge(const MSEdge* followerAfterInternal, SUMOVehicleClass vClass) const {
     935              :     //@todo to be optimized
     936      6066139 :     for (const MSLane* const l : *myLanes) {
     937      8446926 :         for (const MSLink* const link : l->getLinkCont()) {
     938      7650111 :             if (&link->getLane()->getEdge() == followerAfterInternal) {
     939      5002946 :                 if (link->getViaLane() != nullptr) {
     940      3970330 :                     if (link->getViaLane()->allowsVehicleClass(vClass)) {
     941      3932009 :                         return &link->getViaLane()->getEdge();
     942              :                     } else {
     943        38321 :                         continue;
     944              :                     }
     945              :                 } else {
     946              :                     return nullptr; // network without internal links
     947              :                 }
     948              :             }
     949              :         }
     950              :     }
     951              :     return nullptr;
     952              : }
     953              : 
     954              : 
     955              : double
     956      1245805 : MSEdge::getInternalFollowingLengthTo(const MSEdge* followerAfterInternal, SUMOVehicleClass vClass) const {
     957              :     assert(followerAfterInternal != 0);
     958              :     assert(!followerAfterInternal->isInternal());
     959              :     double dist = 0.;
     960      1245805 :     const MSEdge* edge = getInternalFollowingEdge(followerAfterInternal, vClass);
     961              :     // Take into account non-internal lengths until next non-internal edge
     962      2300533 :     while (edge != nullptr && edge->isInternal()) {
     963      1054728 :         dist += edge->getLength();
     964      1054728 :         edge = edge->getInternalFollowingEdge(followerAfterInternal, vClass);
     965              :     }
     966      1245805 :     return dist;
     967              : }
     968              : 
     969              : 
     970              : const MSEdge*
     971       148356 : MSEdge::getNormalBefore() const {
     972              :     const MSEdge* result = this;
     973       157418 :     while (result->isInternal() && MSGlobals::gUsingInternalLanes) {
     974              :         assert(result->getPredecessors().size() == 1);
     975         9062 :         result = result->getPredecessors().front();
     976              :     }
     977       148356 :     return result;
     978              : }
     979              : 
     980              : const MSEdge*
     981      6149655 : MSEdge::getNormalSuccessor() const {
     982              :     const MSEdge* result = this;
     983     11522892 :     while (result->isInternal()) {
     984              :         assert(result->getSuccessors().size() == 1);
     985      5373237 :         result = result->getSuccessors().front();
     986              :     }
     987      6149655 :     return result;
     988              : }
     989              : 
     990              : double
     991    188811999 : MSEdge::getMeanSpeed() const {
     992              :     double v = 0;
     993              :     double totalNumVehs = 0;
     994    188811999 :     if (MSGlobals::gUseMesoSim) {
     995    250084108 :         for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
     996              :             const int numVehs = segment->getCarNumber();
     997    184048787 :             if (numVehs > 0) {
     998     25223479 :                 v += numVehs * segment->getMeanSpeed();
     999     25223479 :                 totalNumVehs += numVehs;
    1000              :             }
    1001              :         }
    1002     66035321 :         if (totalNumVehs == 0) {
    1003     56897400 :             return getLength() / myEmptyTraveltime; // may include tls-penalty
    1004              :         }
    1005              :     } else {
    1006    282676591 :         for (const MSLane* const lane : *myLanes) {
    1007              :             int numVehs = lane->getVehicleNumber();
    1008    159899913 :             if (numVehs == 0) {
    1009              :                 // take speed limit but with lowest possible weight
    1010              :                 numVehs = 1;
    1011              :             }
    1012    159899913 :             v += numVehs * lane->getMeanSpeed();
    1013    159899913 :             totalNumVehs += numVehs;
    1014              :         }
    1015    122776678 :         if (myBidiEdge != nullptr) {
    1016      8758981 :             for (const MSLane* const lane : myBidiEdge->getLanes()) {
    1017      4538234 :                 if (lane->getVehicleNumber() > 0) {
    1018              :                     // do not route across edges which are already occupied in reverse direction
    1019              :                     return 0;
    1020              :                 }
    1021              :             }
    1022              :         }
    1023    122460403 :         if (totalNumVehs == 0) {
    1024            0 :             return getSpeedLimit();
    1025              :         }
    1026              :     }
    1027    131598324 :     return v / totalNumVehs;
    1028              : }
    1029              : 
    1030              : 
    1031              : double
    1032            8 : MSEdge::getMeanFriction() const {
    1033              :     double f = 0.;
    1034           32 :     for (const MSLane* const lane : *myLanes) {
    1035           24 :         f += lane->getFrictionCoefficient();
    1036              :     }
    1037            8 :     if (!myLanes->empty()) {
    1038            8 :         return f / (double)myLanes->size();
    1039              :     }
    1040              :     return 1.;
    1041              : }
    1042              : 
    1043              : 
    1044              : double
    1045         1272 : MSEdge::getMeanSpeedBike() const {
    1046         1272 :     if (MSGlobals::gUseMesoSim) {
    1047              :         // no separate bicycle speeds in meso
    1048          362 :         return getMeanSpeed();
    1049              :     }
    1050              :     double v = 0;
    1051              :     double totalNumVehs = 0;
    1052         3005 :     for (const MSLane* const lane : *myLanes) {
    1053              :         const int numVehs = lane->getVehicleNumber();
    1054         2095 :         v += numVehs * lane->getMeanSpeedBike();
    1055         2095 :         totalNumVehs += numVehs;
    1056              :     }
    1057          910 :     if (totalNumVehs == 0) {
    1058          455 :         return getSpeedLimit();
    1059              :     }
    1060          455 :     return v / totalNumVehs;
    1061              : }
    1062              : 
    1063              : 
    1064              : double
    1065        64548 : MSEdge::getCurrentTravelTime(double minSpeed) const {
    1066              :     assert(minSpeed > 0);
    1067        64548 :     if (!myAmDelayed) {
    1068        44794 :         return myEmptyTraveltime;
    1069              :     }
    1070        39508 :     return getLength() / MAX2(minSpeed, getMeanSpeed());
    1071              : }
    1072              : 
    1073              : 
    1074              : double
    1075            0 : MSEdge::getRoutingSpeed() const {
    1076            0 :     return MSRoutingEngine::getAssumedSpeed(this, nullptr);
    1077              : }
    1078              : 
    1079              : 
    1080              : bool
    1081      1838794 : MSEdge::dictionary(const std::string& id, MSEdge* ptr) {
    1082              :     const DictType::iterator it = myDict.lower_bound(id);
    1083      1838794 :     if (it == myDict.end() || it->first != id) {
    1084              :         // id not in myDict
    1085      1838794 :         myDict.emplace_hint(it, id, ptr);
    1086      3677622 :         while (ptr->getNumericalID() >= (int)myEdges.size()) {
    1087      1838828 :             myEdges.push_back(nullptr);
    1088              :         }
    1089      1838794 :         myEdges[ptr->getNumericalID()] = ptr;
    1090      1838794 :         return true;
    1091              :     }
    1092              :     return false;
    1093              : }
    1094              : 
    1095              : 
    1096              : MSEdge*
    1097      8679016 : MSEdge::dictionary(const std::string& id) {
    1098              :     const DictType::iterator it = myDict.find(id);
    1099      8679016 :     if (it == myDict.end()) {
    1100              :         return nullptr;
    1101              :     }
    1102      6838188 :     return it->second;
    1103              : }
    1104              : 
    1105              : 
    1106              : MSEdge*
    1107      2928662 : MSEdge::dictionaryHint(const std::string& id, const int startIdx) {
    1108              :     // this method is mainly useful when parsing connections from the net.xml which are sorted by "from" id
    1109      2928662 :     if (myEdges[startIdx] != nullptr && myEdges[startIdx]->getID() == id) {
    1110              :         return myEdges[startIdx];
    1111              :     }
    1112      1849638 :     if (startIdx + 1 < (int)myEdges.size() && myEdges[startIdx + 1] != nullptr && myEdges[startIdx + 1]->getID() == id) {
    1113              :         return myEdges[startIdx + 1];
    1114              :     }
    1115       608873 :     return dictionary(id);
    1116              : }
    1117              : 
    1118              : 
    1119              : const MSEdgeVector&
    1120       906629 : MSEdge::getAllEdges() {
    1121       906629 :     return myEdges;
    1122              : }
    1123              : 
    1124              : 
    1125              : void
    1126        42208 : MSEdge::clear() {
    1127      1865712 :     for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
    1128      1823504 :         delete (*i).second;
    1129              :     }
    1130              :     myDict.clear();
    1131              :     myEdges.clear();
    1132        42208 : }
    1133              : 
    1134              : 
    1135              : void
    1136          285 : MSEdge::insertIDs(std::vector<std::string>& into) {
    1137        16016 :     for (DictType::iterator i = myDict.begin(); i != myDict.end(); ++i) {
    1138        15731 :         into.push_back((*i).first);
    1139              :     }
    1140          285 : }
    1141              : 
    1142              : 
    1143              : void
    1144       413827 : MSEdge::parseEdgesList(const std::string& desc, ConstMSEdgeVector& into,
    1145              :                        const std::string& rid) {
    1146       413827 :     StringTokenizer st(desc);
    1147       413827 :     parseEdgesList(st.getVector(), into, rid);
    1148       413827 : }
    1149              : 
    1150              : 
    1151              : void
    1152       414109 : MSEdge::parseEdgesList(const std::vector<std::string>& desc, ConstMSEdgeVector& into,
    1153              :                        const std::string& rid) {
    1154      1567943 :     for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
    1155      1153883 :         const MSEdge* edge = MSEdge::dictionary(*i);
    1156              :         // check whether the edge exists
    1157      1153883 :         if (edge == nullptr) {
    1158           49 :             throw ProcessError("The edge '" + *i + "' within the route " + rid + " is not known."
    1159          147 :                                + "\n The route can not be build.");
    1160              :         }
    1161      1153834 :         into.push_back(edge);
    1162              :     }
    1163       414060 : }
    1164              : 
    1165              : 
    1166              : double
    1167      1869927 : MSEdge::getDistanceTo(const MSEdge* other, const bool doBoundaryEstimate) const {
    1168              :     assert(this != other);
    1169      1869927 :     if (doBoundaryEstimate) {
    1170        19288 :         return myBoundary.distanceTo2D(other->myBoundary);
    1171              :     }
    1172      1850639 :     if (isTazConnector()) {
    1173          564 :         if (other->isTazConnector()) {
    1174          448 :             return myBoundary.distanceTo2D(other->myBoundary);
    1175              :         }
    1176          116 :         return myBoundary.distanceTo2D(other->getLanes()[0]->getShape()[0]);
    1177              :     }
    1178      1850075 :     if (other->isTazConnector()) {
    1179         5405 :         return other->myBoundary.distanceTo2D(getLanes()[0]->getShape()[-1]);
    1180              :     }
    1181      1844670 :     return getLanes()[0]->getShape()[-1].distanceTo2D(other->getLanes()[0]->getShape()[0]);
    1182              : }
    1183              : 
    1184              : 
    1185              : const Position
    1186         2453 : MSEdge::getStopPosition(const SUMOVehicleParameter::Stop& stop) {
    1187         2453 :     return MSLane::dictionary(stop.lane)->geometryPositionAtOffset((stop.endPos + stop.startPos) / 2.);
    1188              : }
    1189              : 
    1190              : 
    1191              : double
    1192     93660798 : MSEdge::getSpeedLimit() const {
    1193              :     // @note lanes might have different maximum speeds in theory
    1194     93660798 :     return myLanes->empty() ? 1 : getLanes()[0]->getSpeedLimit();
    1195              : }
    1196              : 
    1197              : 
    1198              : double
    1199      1922373 : MSEdge::getLengthGeometryFactor() const {
    1200      1922373 :     return myLanes->empty() ? 1 : getLanes()[0]->getLengthGeometryFactor();
    1201              : }
    1202              : 
    1203              : double
    1204   2067780600 : MSEdge::getVehicleMaxSpeed(const SUMOTrafficObject* const veh) const {
    1205              :     // @note lanes might have different maximum speeds in theory
    1206   2067780600 :     return myLanes->empty() ? 1 : getLanes()[0]->getVehicleMaxSpeed(veh);
    1207              : }
    1208              : 
    1209              : 
    1210              : void
    1211          203 : MSEdge::setMaxSpeed(const double val, const bool modified, const double jamThreshold) {
    1212              :     assert(val >= 0);
    1213          203 :     if (myLanes != nullptr) {
    1214          560 :         for (MSLane* const lane : *myLanes) {
    1215          357 :             lane->setMaxSpeed(val, modified, jamThreshold);
    1216              :         }
    1217              :     }
    1218          203 : }
    1219              : 
    1220              : 
    1221              : void
    1222      2933234 : MSEdge::addTransportable(MSTransportable* t) const {
    1223      2933234 :     if (t->isPerson()) {
    1224              :         myPersons.insert(t);
    1225              :     } else {
    1226              :         myContainers.insert(t);
    1227              :     }
    1228      2933234 : }
    1229              : 
    1230              : void
    1231      6445755 : MSEdge::removeTransportable(MSTransportable* t) const {
    1232      6445755 :     std::set<MSTransportable*, ComparatorNumericalIdLess>& tc = t->isPerson() ? myPersons : myContainers;
    1233              :     auto it = tc.find(t);
    1234      6445755 :     if (it != tc.end()) {
    1235              :         tc.erase(it);
    1236              :     }
    1237      6445755 : }
    1238              : 
    1239              : std::vector<MSTransportable*>
    1240    101035451 : MSEdge::getSortedPersons(SUMOTime timestep, bool includeRiding) const {
    1241    101035451 :     std::vector<MSTransportable*> result(myPersons.begin(), myPersons.end());
    1242    101035451 :     if (includeRiding) {
    1243      2896155 :         for (std::vector<MSLane*>::const_iterator i = myLanes->begin(); i != myLanes->end(); ++i) {
    1244      2042878 :             const MSLane::VehCont& vehs = (*i)->getVehiclesSecure();
    1245      3266518 :             for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
    1246      1223640 :                 const std::vector<MSTransportable*>& persons = (*j)->getPersons();
    1247      1223640 :                 result.insert(result.end(), persons.begin(), persons.end());
    1248              :             }
    1249      2042878 :             (*i)->releaseVehicles();
    1250              :         }
    1251              :     }
    1252    101035451 :     sort(result.begin(), result.end(), transportable_by_position_sorter(timestep));
    1253    101035451 :     return result;
    1254            0 : }
    1255              : 
    1256              : 
    1257              : std::vector<MSTransportable*>
    1258     36124008 : MSEdge::getSortedContainers(SUMOTime timestep, bool /* includeRiding */) const {
    1259     36124008 :     std::vector<MSTransportable*> result(myContainers.begin(), myContainers.end());
    1260     36124008 :     sort(result.begin(), result.end(), transportable_by_position_sorter(timestep));
    1261     36124008 :     return result;
    1262            0 : }
    1263              : 
    1264              : 
    1265              : int
    1266      5266434 : MSEdge::transportable_by_position_sorter::operator()(const MSTransportable* const c1, const MSTransportable* const c2) const {
    1267      5266434 :     const double pos1 = c1->getCurrentStage()->getEdgePos(myTime);
    1268      5266434 :     const double pos2 = c2->getCurrentStage()->getEdgePos(myTime);
    1269      5266434 :     if (pos1 != pos2) {
    1270      5108646 :         return pos1 < pos2;
    1271              :     }
    1272       157788 :     return c1->getID() < c2->getID();
    1273              : }
    1274              : 
    1275              : 
    1276              : void
    1277       125229 : MSEdge::addSuccessor(MSEdge* edge, const MSEdge* via) {
    1278       125229 :     mySuccessors.push_back(edge);
    1279       125229 :     myViaSuccessors.push_back(std::make_pair(edge, via));
    1280       125229 :     if (isTazConnector() && edge->getFromJunction() != nullptr) {
    1281        62611 :         myBoundary.add(edge->getFromJunction()->getPosition());
    1282              :     }
    1283              : 
    1284       125229 :     edge->myPredecessors.push_back(this);
    1285       125229 :     if (edge->isTazConnector() && getToJunction() != nullptr) {
    1286        62618 :         edge->myBoundary.add(getToJunction()->getPosition());
    1287              :     }
    1288       125229 : }
    1289              : 
    1290              : 
    1291              : const MSEdgeVector&
    1292     72405138 : MSEdge::getSuccessors(SUMOVehicleClass vClass) const {
    1293     72405138 :     if (vClass == SVC_IGNORING || !MSNet::getInstance()->hasPermissions() || myFunction == SumoXMLEdgeFunc::CONNECTOR) {
    1294     72387936 :         return mySuccessors;
    1295              :     }
    1296              : #ifdef HAVE_FOX
    1297        17202 :     ScopedLocker<> lock(mySuccessorMutex, MSGlobals::gNumThreads > 1);
    1298              : #endif
    1299              :     std::map<SUMOVehicleClass, MSEdgeVector>::iterator i = myClassesSuccessorMap.find(vClass);
    1300        17202 :     if (i == myClassesSuccessorMap.end()) {
    1301              :         // instantiate vector
    1302         2230 :         myClassesSuccessorMap[vClass];
    1303              :         i = myClassesSuccessorMap.find(vClass);
    1304              :         // this vClass is requested for the first time. rebuild all successors
    1305        11467 :         for (MSEdgeVector::const_iterator it = mySuccessors.begin(); it != mySuccessors.end(); ++it) {
    1306         9237 :             if ((*it)->isTazConnector()) {
    1307          288 :                 i->second.push_back(*it);
    1308              :             } else {
    1309         8949 :                 const std::vector<MSLane*>* allowed = allowedLanes(**it, vClass);
    1310         8949 :                 if (allowed != nullptr && allowed->size() > 0) {
    1311         6723 :                     i->second.push_back(*it);
    1312              :                 }
    1313              :             }
    1314              :         }
    1315              :     }
    1316              :     // can use cached value
    1317        17202 :     return i->second;
    1318              : }
    1319              : 
    1320              : 
    1321              : const MSConstEdgePairVector&
    1322    196945592 : MSEdge::getViaSuccessors(SUMOVehicleClass vClass, bool ignoreTransientPermissions) const {
    1323    196945592 :     if (vClass == SVC_IGNORING || !MSNet::getInstance()->hasPermissions() || myFunction == SumoXMLEdgeFunc::CONNECTOR) {
    1324    191394196 :         return myViaSuccessors;
    1325              :     }
    1326              : #ifdef HAVE_FOX
    1327      5551396 :     ScopedLocker<> lock(mySuccessorMutex, MSGlobals::gNumThreads > 1);
    1328              : #endif
    1329      5551396 :     auto& viaMap = ignoreTransientPermissions && myHaveTransientPermissions ? myOrigClassesViaSuccessorMap : myClassesViaSuccessorMap;
    1330              :     auto i = viaMap.find(vClass);
    1331      5551396 :     if (i != viaMap.end()) {
    1332              :         // can use cached value
    1333      5496711 :         return i->second;
    1334              :     }
    1335              :     // instantiate vector
    1336        54685 :     MSConstEdgePairVector& result = viaMap[vClass];
    1337              :     // this vClass is requested for the first time. rebuild all successors
    1338       232707 :     for (const auto& viaPair : myViaSuccessors) {
    1339       178022 :         if (viaPair.first->isTazConnector()) {
    1340         6321 :             result.push_back(viaPair);
    1341              :         } else {
    1342       171701 :             const std::vector<MSLane*>* allowed = allowedLanes(*viaPair.first, vClass, ignoreTransientPermissions);
    1343       171701 :             if (allowed != nullptr && allowed->size() > 0) {
    1344       129029 :                 result.push_back(viaPair);
    1345              :             }
    1346              :         }
    1347              :     }
    1348              :     return result;
    1349              : }
    1350              : 
    1351              : 
    1352              : void
    1353      1768579 : MSEdge::setJunctions(MSJunction* from, MSJunction* to) {
    1354      1768579 :     myFromJunction = from;
    1355      1768579 :     myToJunction = to;
    1356      1768579 :     if (!isTazConnector()) {
    1357      1768579 :         myBoundary.add(from->getPosition());
    1358      1768579 :         myBoundary.add(to->getPosition());
    1359              :     }
    1360      1768579 : }
    1361              : 
    1362              : 
    1363              : bool
    1364      2905177 : MSEdge::canChangeToOpposite() const {
    1365      2905177 :     return (!myLanes->empty() && myLanes->back()->getOpposite() != nullptr &&
    1366              :             // do not change on curved internal lanes
    1367              :             (!isInternal()
    1368         5873 :              || (MSGlobals::gUsingInternalLanes
    1369         5873 :                  && myLanes->back()->getIncomingLanes()[0].viaLink->getDirection() == LinkDirection::STRAIGHT)));
    1370              : }
    1371              : 
    1372              : 
    1373              : const MSEdge*
    1374     22286425 : MSEdge::getOppositeEdge() const {
    1375     22286425 :     if (!myLanes->empty() && myLanes->back()->getOpposite() != nullptr) {
    1376      2989293 :         return &(myLanes->back()->getOpposite()->getEdge());
    1377              :     } else {
    1378     19297132 :         return nullptr;
    1379              :     }
    1380              : }
    1381              : 
    1382              : 
    1383              : bool
    1384          178 : MSEdge::hasMinorLink() const {
    1385          354 :     for (const MSLane* const l : *myLanes) {
    1386          386 :         for (const MSLink* const link : l->getLinkCont()) {
    1387          210 :             if (!link->havePriority()) {
    1388              :                 return true;
    1389              :             }
    1390              :         }
    1391              :     }
    1392              :     return false;
    1393              : }
    1394              : 
    1395              : bool
    1396       208901 : MSEdge::hasChangeProhibitions(SUMOVehicleClass svc, int index) const {
    1397       208901 :     if (myLanes->size() == 1) {
    1398              :         return false;
    1399              :     }
    1400       285581 :     for (const MSLane* const l : *myLanes) {
    1401       193923 :         if (l->getIndex() <= index && !l->allowsChangingRight(svc) && l->getIndex() > 0) {
    1402              :             return true;
    1403       193879 :         } else if (l->getIndex() >= index && !l->allowsChangingLeft(svc) && l->getIndex() < (int)(myLanes->size() - 1)) {
    1404              :             return true;
    1405              :         }
    1406              :     }
    1407              :     return false;
    1408              : }
    1409              : 
    1410              : void
    1411       919078 : MSEdge::checkAndRegisterBiDirEdge(const std::string& bidiID) {
    1412       919078 :     if (bidiID != "") {
    1413        32940 :         myBidiEdge = dictionary(bidiID);
    1414        32940 :         if (myBidiEdge == nullptr) {
    1415            0 :             WRITE_ERRORF(TL("Bidi-edge '%' does not exist"), bidiID);
    1416              :         }
    1417        32940 :         setBidiLanes();
    1418       527999 :         return;
    1419              :     }
    1420       886138 :     if (getFunction() != SumoXMLEdgeFunc::NORMAL) {
    1421              :         return;
    1422              :     }
    1423              :     // legacy networks (no bidi attribute)
    1424       391079 :     ConstMSEdgeVector candidates = myToJunction->getOutgoing();
    1425      3099625 :     for (ConstMSEdgeVector::const_iterator it = candidates.begin(); it != candidates.end(); it++) {
    1426      2708546 :         if ((*it)->getToJunction() == myFromJunction) { //reverse edge
    1427       309582 :             if (myBidiEdge != nullptr && isSuperposable(*it)) {
    1428            0 :                 WRITE_WARNINGF(TL("Ambiguous superposable edges between junction '%' and '%'."), myToJunction->getID(), myFromJunction->getID());
    1429            0 :                 break;
    1430              :             }
    1431       309582 :             if (isSuperposable(*it)) {
    1432           26 :                 myBidiEdge = *it;
    1433           26 :                 setBidiLanes();
    1434              :             }
    1435              :         }
    1436              :     }
    1437       391079 : }
    1438              : 
    1439              : 
    1440              : void
    1441        32966 : MSEdge::setBidiLanes() {
    1442              :     assert(myBidiEdge != nullptr);
    1443        32966 :     if (getNumLanes() == 1 && myBidiEdge->getNumLanes() == 1) {
    1444              :         // the other way round is set when this method runs for the bidiEdge
    1445        31234 :         getLanes()[0]->setBidiLane(myBidiEdge->getLanes()[0]);
    1446              :     } else {
    1447              :         // find lanes with matching reversed shapes
    1448              :         int numBidiLanes = 0;
    1449         5262 :         for (MSLane* l1 : *myLanes) {
    1450        10794 :             for (MSLane* l2 : *myBidiEdge->myLanes) {
    1451         7264 :                 if (l1->getShape().reverse().almostSame(l2->getShape(), POSITION_EPS * 2)) {
    1452         1786 :                     l1->setBidiLane(l2);
    1453         1786 :                     numBidiLanes++;
    1454              :                 }
    1455              :             }
    1456              :         }
    1457              :         // warn only once for each pair
    1458         1732 :         if (numBidiLanes == 0 && getNumericalID() < myBidiEdge->getNumericalID()) {
    1459           15 :             WRITE_WARNINGF(TL("Edge '%' and bidi edge '%' have no matching bidi lanes"), getID(), myBidiEdge->getID());
    1460              :         }
    1461              :     }
    1462        32966 : }
    1463              : 
    1464              : 
    1465              : bool
    1466       309582 : MSEdge::isSuperposable(const MSEdge* other) {
    1467       309582 :     if (other == nullptr || other->getLanes().size() != myLanes->size()) {
    1468              :         return false;
    1469              :     }
    1470              :     std::vector<MSLane*>::const_iterator it1 = myLanes->begin();
    1471              :     std::vector<MSLane*>::const_reverse_iterator it2 = other->getLanes().rbegin();
    1472              :     do {
    1473       304990 :         if ((*it1)->getShape().reverse() != (*it2)->getShape()) {
    1474              :             return false;
    1475              :         }
    1476              :         it1++;
    1477              :         it2++;
    1478           26 :     } while (it1 != myLanes->end());
    1479              : 
    1480              :     return true;
    1481              : }
    1482              : 
    1483              : 
    1484              : void
    1485        73111 : MSEdge::addWaiting(SUMOVehicle* vehicle) const {
    1486              : #ifdef HAVE_FOX
    1487        73111 :     ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
    1488              : #endif
    1489        73111 :     myWaiting.push_back(vehicle);
    1490        73111 : }
    1491              : 
    1492              : 
    1493              : void
    1494        64710 : MSEdge::removeWaiting(const SUMOVehicle* vehicle) const {
    1495              : #ifdef HAVE_FOX
    1496        64710 :     ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
    1497              : #endif
    1498        64710 :     std::vector<SUMOVehicle*>::iterator it = std::find(myWaiting.begin(), myWaiting.end(), vehicle);
    1499        64710 :     if (it != myWaiting.end()) {
    1500        64099 :         myWaiting.erase(it);
    1501              :     }
    1502        64710 : }
    1503              : 
    1504              : 
    1505              : SUMOVehicle*
    1506        77456 : MSEdge::getWaitingVehicle(MSTransportable* transportable, const double position) const {
    1507              : #ifdef HAVE_FOX
    1508        77456 :     ScopedLocker<> lock(myWaitingMutex, MSGlobals::gNumSimThreads > 1);
    1509              : #endif
    1510        77759 :     for (SUMOVehicle* const vehicle : myWaiting) {
    1511         5196 :         if (transportable->isWaitingFor(vehicle)) {
    1512         7375 :             if (vehicle->isStoppedInRange(position, MSGlobals::gStopTolerance) ||
    1513         2269 :                     (!vehicle->hasDeparted() &&
    1514         2060 :                      (vehicle->getParameter().departProcedure == DepartDefinition::TRIGGERED ||
    1515           83 :                       vehicle->getParameter().departProcedure == DepartDefinition::CONTAINER_TRIGGERED))) {
    1516              :                 return vehicle;
    1517              :             }
    1518          213 :             if (!vehicle->isLineStop(position) && vehicle->allowsBoarding(transportable)) {
    1519          228 :                 WRITE_WARNING((transportable->isPerson() ? "Person '" : "Container '")
    1520              :                               + transportable->getID() + "' at edge '" + getID() + "' position " + toString(position) + " cannot use waiting vehicle '"
    1521              :                               + vehicle->getID() + "' at position " + toString(vehicle->getPositionOnLane()) + " because it is too far away.");
    1522              :             }
    1523              :         }
    1524              :     }
    1525              :     return nullptr;
    1526              : }
    1527              : 
    1528              : std::vector<const SUMOVehicle*>
    1529       144177 : MSEdge::getVehicles() const {
    1530              :     std::vector<const SUMOVehicle*> result;
    1531       144177 :     if (MSGlobals::gUseMesoSim) {
    1532          220 :         for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
    1533          122 :             std::vector<const MEVehicle*> segmentVehs = segment->getVehicles();
    1534          122 :             result.insert(result.end(), segmentVehs.begin(), segmentVehs.end());
    1535          122 :         }
    1536              :     } else {
    1537       385943 :         for (MSLane* lane : getLanes()) {
    1538       828374 :             for (auto veh : lane->getVehiclesSecure()) {
    1539       586559 :                 result.push_back(veh);
    1540              :             }
    1541       241815 :             lane->releaseVehicles();
    1542              :         }
    1543              :     }
    1544       144177 :     return result;
    1545            0 : }
    1546              : 
    1547              : int
    1548       622263 : MSEdge::getNumDrivingLanes() const {
    1549              :     int result = 0;
    1550       622263 :     SVCPermissions filter = SVCAll;
    1551       622263 :     if ((myCombinedPermissions & ~(SVC_PEDESTRIAN | SVC_WHEELCHAIR)) != 0) {
    1552              :         filter = ~(SVC_PEDESTRIAN | SVC_WHEELCHAIR);
    1553         2050 :     } else if ((myCombinedPermissions & (SVC_PEDESTRIAN | SVC_WHEELCHAIR)) != 0) {
    1554              :         // filter out green verge
    1555              :         filter = (SVC_PEDESTRIAN | SVC_WHEELCHAIR);
    1556              :     }
    1557      1401827 :     for (const MSLane* const l : *myLanes) {
    1558       779564 :         if ((l->getPermissions() & filter) != 0) {
    1559       690200 :             result++;
    1560              :         }
    1561              :     }
    1562       622263 :     return result;
    1563              : }
    1564              : 
    1565              : int
    1566          399 : MSEdge::getVehicleNumber() const {
    1567          399 :     return (int)getVehicles().size();
    1568              : }
    1569              : 
    1570              : 
    1571              : bool
    1572            0 : MSEdge::isEmpty() const {
    1573              :     /// more efficient than retrieving vehicle number
    1574            0 :     if (MSGlobals::gUseMesoSim) {
    1575            0 :         for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
    1576            0 :             if (segment->getCarNumber() > 0) {
    1577              :                 return false;
    1578              :             }
    1579              :         }
    1580              :     } else {
    1581            0 :         for (MSLane* lane : getLanes()) {
    1582            0 :             if (lane->getVehicleNumber() > 0) {
    1583              :                 return false;
    1584              :             }
    1585              :         }
    1586              :     }
    1587              :     return true;
    1588              : }
    1589              : 
    1590              : 
    1591              : double
    1592           14 : MSEdge::getWaitingSeconds() const {
    1593              :     double wtime = 0;
    1594           14 :     if (MSGlobals::gUseMesoSim) {
    1595           12 :         for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
    1596            9 :             wtime += segment->getWaitingSeconds();
    1597              :         }
    1598              :     } else {
    1599           34 :         for (MSLane* lane : getLanes()) {
    1600           23 :             wtime += lane->getWaitingSeconds();
    1601              :         }
    1602              :     }
    1603           14 :     return wtime;
    1604              : }
    1605              : 
    1606              : 
    1607              : double
    1608           22 : MSEdge::getOccupancy() const {
    1609           22 :     if (myLanes->size() == 0) {
    1610              :         return 0;
    1611              :     }
    1612           22 :     if (MSGlobals::gUseMesoSim) {
    1613              :         /// @note MESegment only tracks brutto occupancy so we compute this from sratch
    1614              :         double sum = 0;
    1615            8 :         for (const SUMOVehicle* veh : getVehicles()) {
    1616            4 :             sum += dynamic_cast<const MEVehicle*>(veh)->getVehicleType().getLength();
    1617            4 :         }
    1618            4 :         return sum / (myLength * (double)myLanes->size());
    1619              :     } else {
    1620              :         double sum = 0;
    1621           48 :         for (auto lane : getLanes()) {
    1622           30 :             sum += lane->getNettoOccupancy();
    1623              :         }
    1624           18 :         return sum / (double)myLanes->size();
    1625              :     }
    1626              : }
    1627              : 
    1628              : 
    1629              : double
    1630            0 : MSEdge::getFlow() const {
    1631            0 :     if (myLanes->size() == 0) {
    1632              :         return 0;
    1633              :     }
    1634              :     double flow = 0;
    1635            0 :     for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
    1636            0 :         flow += (double) segment->getCarNumber() * segment->getMeanSpeed();
    1637              :     }
    1638            0 :     return 3600 * flow / (*myLanes)[0]->getLength();
    1639              : }
    1640              : 
    1641              : 
    1642              : double
    1643            0 : MSEdge::getBruttoOccupancy() const {
    1644            0 :     if (myLanes->size() == 0) {
    1645              :         return 0;
    1646              :     }
    1647              :     double occ = 0;
    1648            0 :     for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this); segment != nullptr; segment = segment->getNextSegment()) {
    1649            0 :         occ += segment->getBruttoOccupancy();
    1650              :     }
    1651            0 :     return occ / (*myLanes)[0]->getLength() / (double)(myLanes->size());
    1652              : }
    1653              : 
    1654              : double
    1655         4240 : MSEdge::getTravelTimeAggregated(const MSEdge* const edge, const SUMOVehicle* const veh, double /*time*/) {
    1656         4240 :     return edge->getLength() / MIN2(MSRoutingEngine::getAssumedSpeed(edge, veh), veh->getMaxSpeed());
    1657              : }
    1658              : 
    1659              : 
    1660              : void
    1661         2098 : MSEdge::inferEdgeType() {
    1662              :     // @note must be called after closeBuilding() to ensure successors and
    1663              :     // predecessors are set
    1664         2098 :     if (isInternal() && myEdgeType == "") {
    1665         1274 :         const std::string typeBefore = getNormalBefore()->getEdgeType();
    1666         1274 :         if (typeBefore != "") {
    1667          694 :             const std::string typeAfter = getNormalSuccessor()->getEdgeType();
    1668          694 :             if (typeBefore == typeAfter) {
    1669              :                 myEdgeType = typeBefore;
    1670          314 :             } else if (typeAfter != "") {
    1671           68 :                 MSNet* net = MSNet::getInstance();
    1672           68 :                 auto resBefore = net->getRestrictions(typeBefore);
    1673           68 :                 auto resAfter = net->getRestrictions(typeAfter);
    1674           68 :                 if (resBefore != nullptr && resAfter != nullptr) {
    1675              :                     // create new restrictions for this type-combination
    1676           96 :                     myEdgeType = typeBefore + "|" + typeAfter;
    1677           48 :                     if (net->getRestrictions(myEdgeType) == nullptr) {
    1678           48 :                         for (const auto& item : *resBefore) {
    1679           24 :                             const SUMOVehicleClass svc = item.first;
    1680           24 :                             const double speed = item.second;
    1681              :                             const auto it = (*resAfter).find(svc);
    1682           24 :                             if (it != (*resAfter).end()) {
    1683           24 :                                 const double speed2 = it->second;
    1684           24 :                                 const double newSpeed = (MSNet::getInstance()->hasJunctionHigherSpeeds()
    1685           24 :                                                          ? MAX2(speed, speed2) : (speed + speed2) / 2);
    1686           24 :                                 net->addRestriction(myEdgeType, svc, newSpeed);
    1687              :                             }
    1688              :                         }
    1689              :                     }
    1690              :                 }
    1691              :             }
    1692              :         }
    1693              :     }
    1694         2098 : }
    1695              : 
    1696              : 
    1697              : double
    1698         2178 : MSEdge::getDistanceAt(double pos) const {
    1699              :     // negative values of myDistances indicate descending kilometrage
    1700         2178 :     return fabs(myDistance + pos);
    1701              : }
    1702              : 
    1703              : 
    1704              : bool
    1705         1333 : MSEdge::hasTransientPermissions() const {
    1706         1333 :     return myHaveTransientPermissions;
    1707              : }
    1708              : 
    1709              : 
    1710              : std::pair<double, SUMOTime>
    1711    592986019 : MSEdge::getLastBlocked(int index) const {
    1712    592986019 :     if (myLaneChanger != nullptr) {
    1713    592986019 :         return myLaneChanger->getLastBlocked(index);
    1714              :     }
    1715            0 :     return std::make_pair(-1, -1);
    1716              : }
    1717              : 
    1718              : 
    1719              : double
    1720         2855 : MSEdge::getPreference(const SUMOVTypeParameter& pars) const {
    1721         5710 :     return MSNet::getInstance()->getPreference(getRoutingType(), pars);
    1722              : }
    1723              : 
    1724              : 
    1725              : void
    1726         7032 : MSEdge::clearState() {
    1727              :     myPersons.clear();
    1728              :     myContainers.clear();
    1729              :     myWaiting.clear();
    1730         7032 : }
    1731              : 
    1732              : 
    1733              : const std::map<const MEVehicle*, std::pair<double, int> >&
    1734       513195 : MSEdge::getMesoPositions() const {
    1735              :     assert(MSGlobals::gUseMesoSim);
    1736       513195 :     if (myLastCacheUpdate < SIMSTEP) {
    1737       480270 :         myLastCacheUpdate = SIMSTEP;
    1738              :         myCachedMesoPos.clear();
    1739              :         int laneIndex = 0;
    1740       480270 :         const double now = SIMTIME;
    1741      1184462 :         for (std::vector<MSLane*>::const_iterator msl = myLanes->begin(); msl != myLanes->end(); ++msl, ++laneIndex) {
    1742              :             // go through the vehicles
    1743              :             double segmentOffset = 0; // offset at start of current segment
    1744       704192 :             for (MESegment* segment = MSGlobals::gMesoNet->getSegmentForEdge(*this);
    1745      1536463 :                     segment != nullptr; segment = segment->getNextSegment()) {
    1746              :                 const double length = segment->getLength();
    1747       832271 :                 if (laneIndex < segment->numQueues()) {
    1748              :                     // make a copy so we don't have to worry about synchronization
    1749       621831 :                     std::vector<MEVehicle*> queue = segment->getQueue(laneIndex);
    1750       621831 :                     const int queueSize = (int)queue.size();
    1751              :                     SUMOTime earliestExitTime = segment->getQueueBlockTime(laneIndex);
    1752              :                     int overlap = 0;
    1753              :                     double prevPos = std::numeric_limits<double>::max();
    1754      1421664 :                     for (int i = 0; i < queueSize; ++i) {
    1755       799833 :                         const MEVehicle* const veh = queue[queueSize - i - 1];
    1756              :                         earliestExitTime = MAX2(earliestExitTime, veh->getEventTime());
    1757       799833 :                         const double vehLength = veh->getVehicleType().getLengthWithGap();
    1758       799833 :                         if (i > 0) {
    1759       714297 :                             earliestExitTime += segment->getMinTauWithVehLength(vehLength, veh->getVehicleType().getCarFollowModel().getHeadwayTime());
    1760              :                         }
    1761       799833 :                         const double entry = veh->getLastEntryTimeSeconds();
    1762       799833 :                         const double pos = segmentOffset + length * (now - entry) / (STEPS2TIME(earliestExitTime) - entry);
    1763              :                         // check if we overlap with the previous vehicle such that the gui has the chance to add some lateral offset
    1764       799833 :                         if (overlap == 0 && prevPos - pos < vehLength) {
    1765              :                             overlap = 1;
    1766              :                         } else {
    1767              :                             overlap = 0;
    1768              :                         }
    1769       799833 :                         myCachedMesoPos[veh] = std::make_pair(pos, overlap);
    1770              :                         prevPos = pos;
    1771              :                     }
    1772       621831 :                 }
    1773       832271 :                 segmentOffset += length;
    1774              :             }
    1775              :         }
    1776              :     }
    1777       513195 :     return myCachedMesoPos;
    1778              : }
    1779              : 
    1780              : 
    1781              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1