LCOV - code coverage report
Current view: top level - src/mesosim - MEVehicle.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 84.8 % 46 39
Test Date: 2026-10-12 15:53:19 Functions: 64.3 % 14 9

            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    MEVehicle.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @date    Tue, May 2005
      17              : ///
      18              : // A vehicle from the mesoscopic point of view
      19              : /****************************************************************************/
      20              : #pragma once
      21              : #include <config.h>
      22              : 
      23              : #include <iostream>
      24              : #include <cassert>
      25              : #include <map>
      26              : #include <vector>
      27              : #include <microsim/MSBaseVehicle.h>
      28              : #include <microsim/MSEdge.h>
      29              : #include <utils/common/StdDefs.h>
      30              : #include "MESegment.h"
      31              : 
      32              : class MSLane;
      33              : class MSLink;
      34              : 
      35              : // ===========================================================================
      36              : // class definitions
      37              : // ===========================================================================
      38              : /**
      39              :  * @class MEVehicle
      40              :  * @brief A vehicle from the mesoscopic point of view
      41              :  */
      42       374592 : class MEVehicle : public MSBaseVehicle {
      43              : public:
      44              :     /** @brief Constructor
      45              :      * @param[in] pars The vehicle description
      46              :      * @param[in] route The vehicle's route
      47              :      * @param[in] type The vehicle's type
      48              :      * @param[in] speedFactor The factor for driven lane's speed limits
      49              :      * @exception ProcessError If a value is wrong
      50              :      */
      51              :     MEVehicle(SUMOVehicleParameter* pars, ConstMSRoutePtr route,
      52              :               MSVehicleType* type, const double speedFactor);
      53              : 
      54              : 
      55              :     /** @brief Get the vehicle's position along the lane
      56              :      * @return The position of the vehicle (in m from the lane's begin)
      57              :      */
      58              :     double getPositionOnLane() const;
      59              : 
      60              : 
      61              :     /** @brief Get the vehicle's position relative to the given lane
      62              :      *  @return The back position of the vehicle (in m from the given lane's begin)
      63              :      */
      64              :     double getBackPositionOnLane(const MSLane* lane) const;
      65              : 
      66              : 
      67              :     /** @brief Returns the vehicle's direction in degrees
      68              :      * @return The vehicle's current angle
      69              :      */
      70              :     double getAngle() const;
      71              : 
      72              : 
      73              :     /** @brief Returns the slope of the road at vehicle's position in degrees
      74              :      * @return The slope
      75              :      */
      76              :     double getSlope() const;
      77              : 
      78              :     /** @brief Returns the edge the vehicle is currently at (possibly an
      79              :      * internal edge)
      80              :      */
      81              :     const MSEdge* getCurrentEdge() const;
      82              : 
      83              :     /** @brief Returns the lane the vehicle is on
      84              :     * @return The vehicle's current lane
      85              :     */
      86     16363409 :     const MSLane* getLane() const {
      87     16363409 :         return nullptr;
      88              :     }
      89              : 
      90         5143 :     const MSLane* getBackLane() const {
      91         5143 :         return nullptr;
      92              :     }
      93              : 
      94              :     /** @brief Return current position (x/y, cartesian)
      95              :      *
      96              :      * If the vehicle's myLane is 0, Position::INVALID.
      97              :      * @param[in] offset optional offset in longitudinal direction
      98              :      * @return The current position (in cartesian coordinates)
      99              :      * @see myLane
     100              :      */
     101              :     Position getPosition(const double offset = 0) const;
     102              : 
     103              :     /// @brief get bounding rectangle
     104              :     PositionVector getBoundingBox(double offset = 0) const;
     105              : 
     106              :     /** @brief Returns the vehicle's estimated speed assuming no delays
     107              :      * @return The vehicle's estimated speed
     108              :      * @note This is only an upper bound. The speed will be lower if the preceding vehicle is delayed
     109              :      */
     110              :     double getSpeed() const;
     111              : 
     112              :     /** @brief Returns the vehicle's estimated average speed on the segment assuming no further delays
     113              :      * @return The vehicle's estimated average speed
     114              :      * @note This is only an upper bound. The speed will be lower if the preceding vehicle is delayed
     115              :      */
     116              :     double getAverageSpeed() const;
     117              : 
     118              :     /// @brief Returns the vehicle's estimated speed after driving across the link
     119              :     double estimateLeaveSpeed(const MSLink* link) const;
     120              : 
     121              : 
     122              :     /** @brief Returns the vehicle's estimated speed taking into account delays
     123              :      * @return The vehicle's estimated speed
     124              :      * @param[in, out] earliestArrival A lower bound on leaveTime, modified to contain new lower bound on leave Time
     125              :      * @note This is only an upper bound. The speed may be even lower if there are further delays downstream
     126              :      */
     127              :     double getConservativeSpeed(SUMOTime& earliestArrival) const;
     128              : 
     129              :     /// @name insertion/removal
     130              :     //@{
     131              : 
     132              :     /** @brief Called when the vehicle is removed from the network.
     133              :      *
     134              :      * Moves along work reminders and
     135              :      *  informs all devices about quitting. Calls "leaveLane" then.
     136              :      *
     137              :      * @param[in] reason why the vehicle leaves (reached its destination, parking, teleport)
     138              :      */
     139              :     void onRemovalFromNet(const MSMoveReminder::Notification reason);
     140              :     //@}
     141              : 
     142              : 
     143              :     /** @brief Update when the vehicle enters a new edge in the move step.
     144              :      * @return Whether the vehicle's route has ended (due to vaporization, or because the destination was reached)
     145              :      */
     146              :     bool moveRoutePointer();
     147              : 
     148              :     /** @brief Returns whether this vehicle has already arrived
     149              :      * (reached the arrivalPosition on its final edge)
     150              :      */
     151              :     bool hasArrived() const;
     152              : 
     153              :     /** @brief Returns the information whether the vehicle is on a road (is simulated)
     154              :      * @return Whether the vehicle is simulated
     155              :      */
     156              :     bool isOnRoad() const;
     157              : 
     158              :     /** @brief Returns whether the vehicle is trying to re-enter the net
     159              :      * @return true if the vehicle is trying to enter the net (eg after parking)
     160              :      */
     161              :     virtual bool isIdling() const;
     162              : 
     163              : 
     164              :     /** @brief registers vehicle with the given link
     165              :      *
     166              :      * @param[in] link the link on which the car shall register its approach
     167              :      */
     168              :     void setApproaching(MSLink* link);
     169              : 
     170              :     /// @brief Returns the remaining stop duration for a stopped vehicle or 0
     171            0 :     SUMOTime remainingStopDuration() const {
     172            0 :         return 0;
     173              :     }
     174              : 
     175         5518 :     bool instantStopping() const {
     176         5518 :         return true;
     177              :     }
     178              : 
     179              :     /// @brief process triggered stops and return true if all stops on the current segment are completed
     180              :     bool endTriggeredStop();
     181              : 
     182              :     ///@brief ends the current stop and performs loading/unloading
     183              :     void processStop();
     184              : 
     185              :     /** @brief Returns until when to stop at the current segment and sets the information that the stop has been reached
     186              :      * @param[in] time the current time
     187              :      * @return stop time for the segment
     188              :      */
     189              :     SUMOTime checkStop(SUMOTime time);
     190              : 
     191              :     /**
     192              :     * resumes a vehicle from stopping
     193              :     * @return true on success, the resuming fails if the vehicle wasn't parking in the first place
     194              :     */
     195              :     bool resumeFromStopping();
     196              : 
     197              :     /// @brief get distance for coming to a stop (used for rerouting checks)
     198       158614 :     double getBrakeGap(bool delayed = false) const {
     199              :         UNUSED_PARAMETER(delayed);
     200       158614 :         return mySegment == nullptr || myQueIndex == MESegment::PARKING_QUEUE ? 0 : mySegment->getLength();
     201              :     }
     202              : 
     203              :     /** @brief Sets the (planned) time at which the vehicle leaves its current segment
     204              :      * @param[in] t The leaving time
     205              :      */
     206              :     inline void setEventTime(SUMOTime t, bool hasDelay = true) {
     207              :         assert(t > myLastEntryTime);
     208    558767784 :         if (hasDelay && mySegment != nullptr) {
     209              :             mySegment->getEdge().markDelayed();
     210              :         }
     211     18334481 :         myEventTime = t;
     212    513636145 :     }
     213              : 
     214              : 
     215              :     /** @brief Returns the (planned) time at which the vehicle leaves its current segment
     216              :      * @return The time the vehicle thinks it leaves its segment at
     217              :      */
     218              :     inline SUMOTime getEventTime() const {
     219   1122357022 :         return myEventTime;
     220              :     }
     221              : 
     222              :     /** @brief Sets the minimum time at which the vehicle leaves its current segment (this includes planned stops)
     223              :      * @param[in] t The leaving time
     224              :      */
     225              :     inline void setUnqueuedEventTime(SUMOTime t) {
     226     26797158 :         myUnqueuedEventTime = t;
     227              :     }
     228              :     /** @brief Returns the minimum time at which the vehicle leaves its current segment
     229              :      * @return The minimum time the vehicle could leave its segment at
     230              :      */
     231              :     inline SUMOTime getUnqueuedEventTime() const {
     232              :         return myUnqueuedEventTime;
     233              :     }
     234              : 
     235              : 
     236              :     /** @brief Sets the current segment the vehicle is at together with its que
     237              :      * @param[in] s The current segment
     238              :      * @param[in] q The current que
     239              :      */
     240     54446391 :     inline virtual void setSegment(MESegment* s, int idx = 0) {
     241     54446391 :         mySegment = s;
     242     54446391 :         myQueIndex = idx;
     243     54446391 :     }
     244              : 
     245              : 
     246              :     /** @brief Returns the current segment the vehicle is on
     247              :      * @return The segment the vehicle is on
     248              :      */
     249              :     inline MESegment* getSegment() const {
     250   1049184098 :         return mySegment;
     251              :     }
     252              : 
     253              :     int getSegmentIndex() const;
     254              : 
     255              :     /** @brief Returns the index of the que the vehicle is in
     256              :      * @return The que index
     257              :      */
     258            0 :     inline int getQueIndex() const {
     259   1682134727 :         return myQueIndex;
     260              :     }
     261              : 
     262              :     /** @brief Get the vehicle's lateral position on the edge of the given lane
     263              :      * (or its current edge if lane == 0)
     264              :      * @return The lateral position of the vehicle (in m distance between right
     265              :      * side of vehicle and ride side of edge
     266              :      */
     267              :     double getRightSideOnEdge(const MSLane* /*lane*/) const;
     268              : 
     269              :     /** @brief Sets the entry time for the current segment
     270              :      * @param[in] t The entry time
     271              :      */
     272              :     inline void setLastEntryTime(SUMOTime t) {
     273     26818375 :         myLastEntryTime = t;
     274              :     }
     275              : 
     276              : 
     277              :     /** @brief Returns the time the vehicle entered the current segment
     278              :      * @return The entry time
     279              :      */
     280              :     SUMOTime getLastEntryTime() const {
     281     40334416 :         return myLastEntryTime;
     282              :     }
     283              : 
     284              :     /** @brief Returns the time of the vehicle's last action point.
     285              :      * @return The time of the last action point
     286              :      */
     287        10656 :     SUMOTime getLastActionTime() const {
     288        10656 :         return SIMSTEP;
     289              :     }
     290              : 
     291              :     /** @brief Sets the time at which the vehicle was blocked
     292              :      * @param[in] t The blocking time
     293              :      */
     294              :     inline void setBlockTime(const SUMOTime t) {
     295              :         assert(t > myLastEntryTime);
     296     27220228 :         myBlockTime = t;
     297       401853 :     }
     298              : 
     299              : 
     300              :     /** @brief Returns the time at which the vehicle was blocked
     301              :      * @return The blocking time
     302              :      */
     303              :     inline SUMOTime getBlockTime() const {
     304   1025744252 :         return myBlockTime;
     305              :     }
     306              : 
     307              : 
     308              :     /// @brief Returns the duration for which the vehicle was blocked
     309   2117850736 :     inline SUMOTime getWaitingTime(const bool accumulated = false) const {
     310              :         UNUSED_PARAMETER(accumulated);
     311   2117850736 :         return MAX2(SUMOTime(0), myEventTime - myBlockTime);
     312              :     }
     313              : 
     314              :     /// @brief Returns the time lost compare to free flow
     315              :     inline SUMOTime getQueuingTimeLoss() const {
     316            0 :         return MAX2(SUMOTime(0), myEventTime - myUnqueuedEventTime);
     317              :     }
     318              : 
     319        64309 :     inline SUMOTime getTimeLoss() const {
     320              :         // slow-downs while driving are not modelled
     321        64309 :         return getWaitingTime();
     322              :     }
     323              : 
     324              :     /// @brief Returns the earliest leave time for the current segment
     325            0 :     double getEventTimeSeconds() const {
     326         5732 :         return STEPS2TIME(getEventTime());
     327              :     }
     328              : 
     329              :     /// @brief Returns the entry time for the current segment
     330            0 :     double getLastEntryTimeSeconds() const {
     331      4990444 :         return STEPS2TIME(getLastEntryTime());
     332              :     }
     333              : 
     334              :     /// @brief Returns the time at which the vehicle was blocked on the current segment
     335            0 :     double getBlockTimeSeconds() const {
     336           20 :         return STEPS2TIME(getBlockTime());
     337              :     }
     338              : 
     339              :     /// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty
     340              :     double getCurrentLinkPenaltySeconds() const;
     341              : 
     342              :     /// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty
     343              :     double getCurrentStoppingTimeSeconds() const;
     344              : 
     345              :     /// Replaces the current route by the given one
     346              :     bool replaceRoute(ConstMSRoutePtr route,  const std::string& info, bool onInit = false, int offset = 0, bool addRouteStops = true, bool removeStops = true, std::string* msgReturn = nullptr);
     347              : 
     348              :     /** @brief Returns whether the vehicle is allowed to pass the next junction, checks also for triggered stops
     349              :      * @return true iff the vehicle may drive over the next junction
     350              :      */
     351              :     bool mayProceed();
     352              : 
     353              :     /** @brief Updates a single vehicle detector if present
     354              :      */
     355              :     void updateDetectorForWriting(MSMoveReminder* rem, SUMOTime currentTime, SUMOTime exitTime);
     356              : 
     357              :     /** @brief Updates all vehicle detectors
     358              :      */
     359              :     void updateDetectors(const SUMOTime currentTime, const SUMOTime exitTime, const bool isLeave,
     360              :                          const MSMoveReminder::Notification reason = MSMoveReminder::NOTIFICATION_JUNCTION);
     361              : 
     362              :     /** @brief Returns the velocity/lane influencer
     363              :      *
     364              :      * If no influencer was existing before, one is built, first
     365              :      * @return Reference to this vehicle's speed influencer
     366              :      */
     367              :     BaseInfluencer& getBaseInfluencer();
     368              : 
     369              :     const BaseInfluencer* getBaseInfluencer() const;
     370              : 
     371      1334622 :     bool hasInfluencer() const {
     372      1334622 :         return myInfluencer != nullptr;
     373              :     }
     374              : 
     375              :     void markJammed(bool jammed) {
     376     20480837 :         myWasJammed = jammed;
     377     20480837 :     }
     378              : 
     379              :     bool wasJammed() const {
     380     25967770 :         return myWasJammed;
     381              :     }
     382              : 
     383              :     /// @name state io
     384              :     //@{
     385              : 
     386              :     /// Saves the states of a vehicle
     387              :     void saveState(OutputDevice& out);
     388              : 
     389              :     /** @brief Loads the state of this vehicle from the given description
     390              :      */
     391              :     void loadState(const SUMOSAXAttributes& attrs, const SUMOTime offset);
     392              :     //@}
     393              : 
     394              : 
     395              : protected:
     396              :     /// @brief The segment the vehicle is at
     397              :     MESegment* mySegment;
     398              : 
     399              :     /// @brief Index of the que the vehicle is in (important for multiqueue extension)
     400              :     int myQueIndex;
     401              : 
     402              :     /// @brief The (planned) time of leaving the segment (cell)
     403              :     SUMOTime myEventTime;
     404              : 
     405              :     /// @brief The time the vehicle entered its current segment
     406              :     SUMOTime myLastEntryTime;
     407              : 
     408              :     /// @brief The time at which the vehicle was blocked on its current segment
     409              :     SUMOTime myBlockTime;
     410              : 
     411              :     /// @brief The expected time of leaving the segment when following the leader at free-flow headway
     412              :     SUMOTime myUnqueuedEventTime;
     413              : 
     414              :     /// @brief Whether this vehicle was jammed on it's previous normal segment
     415              :     bool myWasJammed;
     416              : 
     417              :     /// @brief An instance of a velocity/lane influencing instance; built in "getInfluencer"
     418              :     BaseInfluencer* myInfluencer;
     419              : 
     420              : };
        

Generated by: LCOV version 2.0-1