LCOV - code coverage report
Current view: top level - src/microsim/transportables - MSPerson.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 88.0 % 142 125
Test Date: 2025-11-13 15:38:19 Functions: 82.1 % 28 23

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2025 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    MSPerson.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @author  Laura Bieker
      19              : /// @date    Mon, 9 Jul 2001
      20              : ///
      21              : // The class for modelling person-movements
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <string>
      26              : #include <vector>
      27              : #include <utils/iodevices/OutputDevice.h>
      28              : #include <utils/options/OptionsCont.h>
      29              : #include <utils/common/ToString.h>
      30              : #include <utils/common/StringUtils.h>
      31              : #include <utils/geom/GeomHelper.h>
      32              : #include <utils/router/IntermodalNetwork.h>
      33              : #include <microsim/MSNet.h>
      34              : #include <microsim/MSEdge.h>
      35              : #include <microsim/MSLane.h>
      36              : #include <microsim/transportables/MSTransportableControl.h>
      37              : #include <microsim/MSInsertionControl.h>
      38              : #include <microsim/MSEventControl.h>
      39              : #include <microsim/MSVehicle.h>
      40              : #include <microsim/MSVehicleControl.h>
      41              : #include <microsim/MSStoppingPlace.h>
      42              : #include <microsim/MSRouteHandler.h>
      43              : #include <microsim/devices/MSDevice_Taxi.h>
      44              : #include <microsim/trigger/MSTriggeredRerouter.h>
      45              : #include "MSPModel_Striping.h"
      46              : #include "MSStageTrip.h"
      47              : #include "MSStageWalking.h"
      48              : #include "MSPerson.h"
      49              : 
      50              : 
      51              : // ===========================================================================
      52              : // method definitions
      53              : // ===========================================================================
      54              : /* -------------------------------------------------------------------------
      55              : * MSPerson::MSPersonStage_Access - methods
      56              : * ----------------------------------------------------------------------- */
      57         4225 : MSPerson::MSPersonStage_Access::MSPersonStage_Access(const MSEdge* destination, MSStoppingPlace* toStop,
      58              :         const double arrivalPos, const double arrivalPosLat, const double dist, const bool isExit,
      59         4225 :         const Position& startPos, const Position& endPos) :
      60              :     MSStage(MSStageType::ACCESS, destination, toStop, arrivalPos, arrivalPosLat),
      61         8450 :     myDist(dist), myAmExit(isExit) {
      62         4225 :     myPath.push_back(startPos);
      63         4225 :     myPath.push_back(endPos);
      64         4225 : }
      65              : 
      66              : 
      67         8450 : MSPerson::MSPersonStage_Access::~MSPersonStage_Access() {}
      68              : 
      69              : MSStage*
      70            0 : MSPerson::MSPersonStage_Access::clone() const {
      71            0 :     return new MSPersonStage_Access(myDestination, myDestinationStop, myArrivalPos, myArrivalPosLat, myDist, myAmExit, myPath.front(), myPath.back());
      72              : }
      73              : 
      74              : void
      75         4225 : MSPerson::MSPersonStage_Access::proceed(MSNet* net, MSTransportable* person, SUMOTime now, MSStage* previous) {
      76         4225 :     myDeparted = now;
      77         4225 :     if (myDist >= 0) {
      78         4211 :         myEstimatedArrival = now + TIME2STEPS(myDist / person->getMaxSpeed());
      79              :     } else {
      80           14 :         myEstimatedArrival = now + previous->getJumpDuration();
      81              :     }
      82              :     // TODO myEstimatedArrival is not a multiple of DELTA_T here. This might give a problem because the destination position will not be reached precisely
      83         4225 :     MSEdge* edge = myDestinationStop != nullptr ? &myDestinationStop->getLane().getEdge() : const_cast<MSEdge*>(myDestination);
      84         4225 :     net->getBeginOfTimestepEvents()->addEvent(new ProceedCmd(person, edge), myEstimatedArrival);
      85         4225 :     net->getPersonControl().startedAccess();
      86         4225 :     edge->addTransportable(person);
      87         4225 : }
      88              : 
      89              : 
      90              : std::string
      91            0 : MSPerson::MSPersonStage_Access::getStageDescription(const bool /* isPerson */) const {
      92            0 :     return "access";
      93              : }
      94              : 
      95              : 
      96              : std::string
      97            0 : MSPerson::MSPersonStage_Access::getStageSummary(const bool /* isPerson */) const {
      98            0 :     if (myDestination == nullptr) {
      99            0 :         return ("jump to edge '") + getDestination()->getID() + "'";
     100              :     } else {
     101            0 :         return (myAmExit ? "access from stop '" : "access to stop '") + getDestinationStop()->getID() + "'";
     102              :     }
     103              : }
     104              : 
     105              : 
     106              : Position
     107        19998 : MSPerson::MSPersonStage_Access::getPosition(SUMOTime now) const {
     108        19998 :     return myPath.positionAtOffset(myPath.length() * (double)(now - myDeparted) / (double)(myEstimatedArrival - myDeparted));
     109              : }
     110              : 
     111              : 
     112              : double
     113        19998 : MSPerson::MSPersonStage_Access::getAngle(SUMOTime /* now */) const {
     114        19998 :     return myPath.angleAt2D(0);
     115              : }
     116              : 
     117              : 
     118              : double
     119        19022 : MSPerson::MSPersonStage_Access::getSpeed() const {
     120        19022 :     return myDist / STEPS2TIME(MAX2((SUMOTime)1, myEstimatedArrival - myDeparted));
     121              : }
     122              : 
     123              : void
     124         2939 : MSPerson::MSPersonStage_Access::tripInfoOutput(OutputDevice& os, const MSTransportable* const) const {
     125         5878 :     os.openTag("access");
     126         2939 :     if (getDestinationStop() != nullptr) {
     127         5862 :         os.writeAttr("stop", getDestinationStop()->getID());
     128              :     }
     129         5878 :     os.writeAttr("depart", time2string(myDeparted));
     130         5878 :     os.writeAttr("arrival", myArrived >= 0 ? time2string(myArrived) : "-1");
     131         5878 :     os.writeAttr("duration", myArrived > 0 ? time2string(getDuration()) : "-1");
     132         2939 :     os.writeAttr("routeLength", myDist);
     133         2939 :     os.closeTag();
     134         2939 : }
     135              : 
     136              : 
     137              : SUMOTime
     138         4193 : MSPerson::MSPersonStage_Access::ProceedCmd::execute(SUMOTime currentTime) {
     139         4193 :     MSNet::getInstance()->getPersonControl().endedAccess();
     140         4193 :     myStopEdge->removeTransportable(myPerson);
     141         4193 :     if (!myPerson->proceed(MSNet::getInstance(), currentTime)) {
     142           53 :         MSNet::getInstance()->getPersonControl().erase(myPerson);
     143              :     }
     144         4193 :     return 0;
     145              : }
     146              : 
     147              : 
     148              : /* -------------------------------------------------------------------------
     149              :  * MSPerson - methods
     150              :  * ----------------------------------------------------------------------- */
     151       314062 : MSPerson::MSPerson(const SUMOVehicleParameter* pars, MSVehicleType* vtype, MSTransportable::MSTransportablePlan* plan, const double speedFactor) :
     152              :     MSTransportable(pars, vtype, plan, true),
     153       314062 :     myInfluencer(nullptr),
     154       628124 :     myChosenSpeedFactor(pars->speedFactor < 0 ? speedFactor : pars->speedFactor),
     155       314129 :     myTimegapCrossing(getFloatParam("pedestrian.timegap-crossing"))
     156       314062 : { }
     157              : 
     158              : 
     159       598925 : MSPerson::~MSPerson() {
     160       314016 :     delete myInfluencer;
     161       598925 : }
     162              : 
     163              : 
     164              : bool
     165       525345 : MSPerson::checkAccess(const MSStage* const prior, const bool waitAtStop) {
     166              :     MSStoppingPlace* prevStop = prior->getDestinationStop();
     167       525345 :     if (!waitAtStop && prior->getStageType() == MSStageType::TRIP) {
     168       203759 :         prevStop = prior->getOriginStop();
     169              :     }
     170       525345 :     if (prevStop != nullptr) {
     171        59322 :         const MSEdge* const accessEdge = waitAtStop ? prior->getDestination() : (*myStep)->getFromEdge();
     172        59322 :         const MSStoppingPlace::Access* const access = prevStop->getAccess(accessEdge);
     173        59322 :         if (access != nullptr) {
     174         4211 :             const MSLane* const lane = accessEdge->getLanes()[0];
     175         4211 :             MSStage* newStage = nullptr;
     176         4211 :             if (waitAtStop) {
     177         2067 :                 const MSEdge* const stopEdge = &prevStop->getLane().getEdge();
     178         2067 :                 const double arrivalAtBs = (prevStop->getBeginLanePosition() + prevStop->getEndLanePosition()) / 2;
     179         4134 :                 newStage = new MSPersonStage_Access(stopEdge, prevStop, arrivalAtBs, 0.0, access->length, false,
     180         2067 :                                                     lane->geometryPositionAtOffset(access->endPos),
     181         4134 :                                                     prevStop->getLane().geometryPositionAtOffset(arrivalAtBs));
     182              :             } else {
     183         3838 :                 const bool useDoors = access->exit == MSStoppingPlace::AccessExit::DOORS ||
     184         7226 :                                       (OptionsCont::getOptions().getString("pedestrian.model") != "jupedsim" && access->exit == MSStoppingPlace::AccessExit::CARRIAGE);
     185         2144 :                 if (access->exit == MSStoppingPlace::AccessExit::CARRIAGE) {
     186            0 :                     const double startPos = prior->getStageType() == MSStageType::TRIP ? prior->getEdgePos(0) : prior->getArrivalPos();
     187            0 :                     const double startPosLat = prior->getStageType() == MSStageType::TRIP ? prior->getEdgePosLat(0) : prior->getArrivalPosLat();
     188              :                     // The start and end attributes of the access stage are equal in this case, but we need to compute the arrival position relatively
     189              :                     // to the current lane and not the lane of the previous stage.
     190            0 :                     const Position start = prevStop->getLane().geometryPositionAtOffset(startPos, startPosLat);
     191            0 :                     const Position end = lane->getShape().transformToVectorCoordinates(start);
     192            0 :                     newStage = new MSPersonStage_Access(accessEdge, prevStop, end.x(), -end.y(), access->length, true, start, start);
     193              :                 } else {
     194         2144 :                     const double startPos = prior->getStageType() == MSStageType::TRIP ? prior->getEdgePos(0) : prior->getArrivalPos();
     195         2144 :                     const Position& trainExit = prevStop->getLane().geometryPositionAtOffset(startPos);
     196         2144 :                     const double arrivalPos = useDoors ? lane->getShape().nearest_offset_to_point2D(trainExit) : access->endPos;
     197              :                     Position platformEntry = lane->geometryPositionAtOffset(arrivalPos);
     198         2144 :                     if (useDoors) {
     199              :                         // find the closer side of the platform to enter
     200          900 :                         const double halfWidth = lane->getWidth() / 2. - MAX2(getVehicleType().getLength(), getVehicleType().getWidth()) / 2. - POSITION_EPS;
     201          450 :                         platformEntry = lane->geometryPositionAtOffset(arrivalPos, halfWidth);
     202          450 :                         const Position& plat2 = lane->geometryPositionAtOffset(arrivalPos, -halfWidth);
     203          450 :                         if (trainExit.distanceSquaredTo2D(plat2) < trainExit.distanceSquaredTo2D(platformEntry)) {
     204          356 :                             platformEntry = plat2;
     205              :                         }
     206              :                     }
     207         2144 :                     newStage = new MSPersonStage_Access(accessEdge, prevStop, arrivalPos, 0.0, access->length, true,
     208         2144 :                                                         trainExit, platformEntry);
     209              :                 }
     210              :             }
     211         4211 :             newStage->setTrip(prior->getTrip());
     212         4211 :             myStep = myPlan->insert(myStep, newStage);
     213              :             return true;
     214              :         }
     215              :     }
     216       521134 :     if (prior->getJumpDuration() > 0) {
     217              :         // negative distance indicates jump
     218              :         MSStage* newStage = new MSPersonStage_Access(getDestination(), nullptr, getArrivalPos(), 0.0, -1, true,
     219           28 :                 prior->getPosition(SIMSTEP) , (*myStep)->getPosition(SIMSTEP));
     220           14 :         myStep = myPlan->insert(myStep, newStage);
     221              :         return true;
     222              :     }
     223              :     return false;
     224              : }
     225              : 
     226              : 
     227              : double
     228            8 : MSPerson::getImpatience() const {
     229            8 :     return MAX2(0., MIN2(1., getVehicleType().getImpatience()
     230            8 :                          + STEPS2TIME((*myStep)->getWaitingTime()) / MSPModel_Striping::MAX_WAIT_TOLERANCE));
     231              : }
     232              : 
     233              : const std::string&
     234         2633 : MSPerson::getNextEdge() const {
     235              : //    if (getCurrentStageType() == WALKING) {
     236              : //        MSStageWalking* walkingStage =  dynamic_cast<MSStageWalking*>(*myStep);
     237              : //        assert(walkingStage != 0);
     238              : //        const MSEdge* nextEdge = walkingStage->getPedestrianState()->getNextEdge(*walkingStage);
     239              : //        if (nextEdge != 0) {
     240              : //            return nextEdge->getID();
     241              : //        }
     242              : //    }
     243              : //    return StringUtils::emptyString;
     244         2633 :     const MSEdge* nextEdge = getNextEdgePtr();
     245         2633 :     if (nextEdge != nullptr) {
     246         2623 :         return nextEdge->getID();
     247              :     }
     248              :     return StringUtils::emptyString;
     249              : }
     250              : 
     251              : 
     252              : const MSEdge*
     253       166321 : MSPerson::getNextEdgePtr() const {
     254       166321 :     if (getCurrentStageType() == MSStageType::WALKING) {
     255       166321 :         MSStageWalking* walkingStage =  dynamic_cast<MSStageWalking*>(*myStep);
     256              :         assert(walkingStage != nullptr);
     257       166321 :         return walkingStage->getPState()->getNextEdge(*walkingStage);
     258              :     }
     259              :     return nullptr;
     260              : }
     261              : 
     262              : 
     263              : 
     264              : void
     265         1507 : MSPerson::replaceWalk(const ConstMSEdgeVector& newEdges, double departPos, int firstIndex, int nextIndex) {
     266              :     assert(nextIndex > firstIndex);
     267              :     //std::cout << SIMTIME << " reroute person " << getID()
     268              :     //    << "  newEdges=" << toString(newEdges)
     269              :     //    << " firstIndex=" << firstIndex
     270              :     //    << " nextIndex=" << nextIndex
     271              :     //    << " departPos=" << getEdgePos()
     272              :     //    << " arrivalPos=" <<  getNextStage(nextIndex - 1)->getArrivalPos()
     273              :     //    << "\n";
     274         1507 :     MSStage* const toBeReplaced = getNextStage(nextIndex - 1);
     275              :     MSStageWalking* newStage = new MSStageWalking(getID(), newEdges,
     276              :             toBeReplaced->getDestinationStop(), -1,
     277              :             -1,
     278              :             departPos,
     279         1507 :             toBeReplaced->getArrivalPos(),
     280         1507 :             MSPModel::UNSPECIFIED_POS_LAT);
     281         1507 :     appendStage(newStage, nextIndex);
     282              :     // remove stages in reverse order so that proceed will only be called at the last removal
     283         3019 :     for (int i = nextIndex - 1; i >= firstIndex; i--) {
     284              :         //std::cout << " removeStage=" << i << "\n";
     285         1512 :         removeStage(i);
     286              :     }
     287         1507 : }
     288              : 
     289              : 
     290              : MSPerson::Influencer&
     291        58455 : MSPerson::getInfluencer() {
     292        58455 :     if (myInfluencer == nullptr) {
     293          227 :         myInfluencer = new Influencer();
     294              :     }
     295        58455 :     return *myInfluencer;
     296              : }
     297              : 
     298              : 
     299              : const MSPerson::Influencer*
     300            0 : MSPerson::getInfluencer() const {
     301            0 :     return myInfluencer;
     302              : }
     303              : 
     304              : 
     305              : 
     306              : /* -------------------------------------------------------------------------
     307              :  * methods of MSPerson::Influencer
     308              :  * ----------------------------------------------------------------------- */
     309          227 : MSPerson::Influencer::Influencer() {}
     310              : 
     311              : 
     312          227 : MSPerson::Influencer::~Influencer() {}
     313              : 
     314              : 
     315              : void
     316        14116 : MSPerson::Influencer::setRemoteControlled(Position xyPos, MSLane* l, double pos, double posLat, double angle, int edgeOffset, const ConstMSEdgeVector& route, SUMOTime t) {
     317        14116 :     myRemoteXYPos = xyPos;
     318        14116 :     myRemoteLane = l;
     319        14116 :     myRemotePos = pos;
     320        14116 :     myRemotePosLat = posLat;
     321        14116 :     myRemoteAngle = angle;
     322        14116 :     myRemoteEdgeOffset = edgeOffset;
     323        14116 :     myRemoteRoute = route;
     324        14116 :     myLastRemoteAccess = t;
     325        14116 : }
     326              : 
     327              : 
     328              : bool
     329        30223 : MSPerson::Influencer::isRemoteControlled() const {
     330        30223 :     return myLastRemoteAccess == MSNet::getInstance()->getCurrentTimeStep();
     331              : }
     332              : 
     333              : 
     334              : bool
     335            0 : MSPerson::Influencer::isRemoteAffected(SUMOTime t) const {
     336            0 :     return myLastRemoteAccess >= t - TIME2STEPS(10);
     337              : }
     338              : 
     339              : 
     340              : void
     341        14116 : MSPerson::Influencer::postProcessRemoteControl(MSPerson* p) {
     342              :     /*
     343              :     std::cout << SIMTIME << " moveToXY person=" << p->getID()
     344              :         << " xyPos=" << myRemoteXYPos
     345              :         << " lane=" << Named::getIDSecure(myRemoteLane)
     346              :         << " pos=" << myRemotePos
     347              :         << " posLat=" << myRemotePosLat
     348              :         << " angle=" << myRemoteAngle
     349              :         << " eOf=" << myRemoteEdgeOffset
     350              :         << " route=" << toString(myRemoteRoute)
     351              :         << " aTime=" << time2string(myLastRemoteAccess)
     352              :         << "\n";
     353              :         */
     354        14116 :     switch (p->getStageType(0)) {
     355              :         case MSStageType::WALKING: {
     356        14076 :             MSStageWalking* s = dynamic_cast<MSStageWalking*>(p->getCurrentStage());
     357              :             assert(s != nullptr);
     358        14076 :             s->getPState()->moveToXY(p, myRemoteXYPos, myRemoteLane, myRemotePos, myRemotePosLat,
     359        14076 :                                      myRemoteAngle, myRemoteEdgeOffset, myRemoteRoute,
     360              :                                      MSNet::getInstance()->getCurrentTimeStep());
     361              :         }
     362        14076 :         break;
     363              :         default:
     364              :             break;
     365              :     }
     366        14116 : }
     367              : 
     368              : 
     369              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1