/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file MEVehicle.h14/// @author Daniel Krajzewicz15/// @date Tue, May 200516///17// A vehicle from the mesoscopic point of view18/****************************************************************************/19#pragma once20#include <config.h>2122#include <iostream>23#include <cassert>24#include <map>25#include <vector>26#include <microsim/MSBaseVehicle.h>27#include <microsim/MSEdge.h>28#include <utils/common/StdDefs.h>29#include "MESegment.h"3031class MSLane;32class MSLink;3334// ===========================================================================35// class definitions36// ===========================================================================37/**38* @class MEVehicle39* @brief A vehicle from the mesoscopic point of view40*/41class MEVehicle : public MSBaseVehicle {42public:43/** @brief Constructor44* @param[in] pars The vehicle description45* @param[in] route The vehicle's route46* @param[in] type The vehicle's type47* @param[in] speedFactor The factor for driven lane's speed limits48* @exception ProcessError If a value is wrong49*/50MEVehicle(SUMOVehicleParameter* pars, ConstMSRoutePtr route,51MSVehicleType* type, const double speedFactor);525354/** @brief Get the vehicle's position along the lane55* @return The position of the vehicle (in m from the lane's begin)56*/57double getPositionOnLane() const;585960/** @brief Get the vehicle's position relative to the given lane61* @return The back position of the vehicle (in m from the given lane's begin)62*/63double getBackPositionOnLane(const MSLane* lane) const;646566/** @brief Returns the vehicle's direction in degrees67* @return The vehicle's current angle68*/69double getAngle() const;707172/** @brief Returns the slope of the road at vehicle's position in degrees73* @return The slope74*/75double getSlope() const;7677/** @brief Returns the lane the vehicle is on78* @return The vehicle's current lane79*/80const MSLane* getLane() const {81return nullptr;82}8384const MSLane* getBackLane() const {85return nullptr;86}8788/** @brief Return current position (x/y, cartesian)89*90* If the vehicle's myLane is 0, Position::INVALID.91* @param[in] offset optional offset in longitudinal direction92* @return The current position (in cartesian coordinates)93* @see myLane94*/95Position getPosition(const double offset = 0) const;9697/// @brief get bounding rectangle98PositionVector getBoundingBox(double offset = 0) const;99100/** @brief Returns the vehicle's estimated speed assuming no delays101* @return The vehicle's estimated speed102* @note This is only an upper bound. The speed will be lower if the preceding vehicle is delayed103*/104double getSpeed() const;105106/** @brief Returns the vehicle's estimated average speed on the segment assuming no further delays107* @return The vehicle's estimated average speed108* @note This is only an upper bound. The speed will be lower if the preceding vehicle is delayed109*/110double getAverageSpeed() const;111112/// @brief Returns the vehicle's estimated speed after driving across the link113double estimateLeaveSpeed(const MSLink* link) const;114115116/** @brief Returns the vehicle's estimated speed taking into account delays117* @return The vehicle's estimated speed118* @param[in, out] earliestArrival A lower bound on leaveTime, modified to contain new lower bound on leave Time119* @note This is only an upper bound. The speed may be even lower if there are further delays downstream120*/121double getConservativeSpeed(SUMOTime& earliestArrival) const;122123/// @name insertion/removal124//@{125126/** @brief Called when the vehicle is removed from the network.127*128* Moves along work reminders and129* informs all devices about quitting. Calls "leaveLane" then.130*131* @param[in] reason why the vehicle leaves (reached its destination, parking, teleport)132*/133void onRemovalFromNet(const MSMoveReminder::Notification reason);134//@}135136137/** @brief Update when the vehicle enters a new edge in the move step.138* @return Whether the vehicle's route has ended (due to vaporization, or because the destination was reached)139*/140bool moveRoutePointer();141142/** @brief Returns whether this vehicle has already arrived143* (reached the arrivalPosition on its final edge)144*/145bool hasArrived() const;146147/** @brief Returns the information whether the vehicle is on a road (is simulated)148* @return Whether the vehicle is simulated149*/150bool isOnRoad() const;151152/** @brief Returns whether the vehicle is trying to re-enter the net153* @return true if the vehicle is trying to enter the net (eg after parking)154*/155virtual bool isIdling() const;156157158/** @brief registers vehicle with the given link159*160* @param[in] link the link on which the car shall register its approach161*/162void setApproaching(MSLink* link);163164/// @brief Returns the remaining stop duration for a stopped vehicle or 0165SUMOTime remainingStopDuration() const {166return 0;167}168169///@brief ends the current stop and performs loading/unloading170void processStop();171172/** @brief Returns until when to stop at the current segment and sets the information that the stop has been reached173* @param[in] time the current time174* @return stop time for the segment175*/176SUMOTime checkStop(SUMOTime time);177178/**179* resumes a vehicle from stopping180* @return true on success, the resuming fails if the vehicle wasn't parking in the first place181*/182bool resumeFromStopping();183184/// @brief get distance for coming to a stop (used for rerouting checks)185double getBrakeGap(bool delayed = false) const {186UNUSED_PARAMETER(delayed);187return mySegment == nullptr || myQueIndex == MESegment::PARKING_QUEUE ? 0 : mySegment->getLength();188}189190/** @brief Sets the (planned) time at which the vehicle leaves its current segment191* @param[in] t The leaving time192*/193inline void setEventTime(SUMOTime t, bool hasDelay = true) {194assert(t > myLastEntryTime);195if (hasDelay && mySegment != nullptr) {196mySegment->getEdge().markDelayed();197}198myEventTime = t;199}200201202/** @brief Returns the (planned) time at which the vehicle leaves its current segment203* @return The time the vehicle thinks it leaves its segment at204*/205inline SUMOTime getEventTime() const {206return myEventTime;207}208209210/** @brief Sets the current segment the vehicle is at together with its que211* @param[in] s The current segment212* @param[in] q The current que213*/214inline virtual void setSegment(MESegment* s, int idx = 0) {215mySegment = s;216myQueIndex = idx;217}218219220/** @brief Returns the current segment the vehicle is on221* @return The segment the vehicle is on222*/223inline MESegment* getSegment() const {224return mySegment;225}226227int getSegmentIndex() const;228229/** @brief Returns the index of the que the vehicle is in230* @return The que index231*/232inline int getQueIndex() const {233return myQueIndex;234}235236/** @brief Get the vehicle's lateral position on the edge of the given lane237* (or its current edge if lane == 0)238* @return The lateral position of the vehicle (in m distance between right239* side of vehicle and ride side of edge240*/241double getRightSideOnEdge(const MSLane* /*lane*/) const;242243/** @brief Sets the entry time for the current segment244* @param[in] t The entry time245*/246inline void setLastEntryTime(SUMOTime t) {247myLastEntryTime = t;248}249250251/** @brief Returns the time the vehicle entered the current segment252* @return The entry time253*/254SUMOTime getLastEntryTime() const {255return myLastEntryTime;256}257258/** @brief Returns the time of the vehicle's last action point.259* @return The time of the last action point260*/261SUMOTime getLastActionTime() const {262return SIMSTEP;263}264265/** @brief Sets the time at which the vehicle was blocked266* @param[in] t The blocking time267*/268inline void setBlockTime(const SUMOTime t) {269assert(t > myLastEntryTime);270myBlockTime = t;271}272273274/** @brief Returns the time at which the vehicle was blocked275* @return The blocking time276*/277inline SUMOTime getBlockTime() const {278return myBlockTime;279}280281282/// @brief Returns the duration for which the vehicle was blocked283inline SUMOTime getWaitingTime(const bool accumulated = false) const {284UNUSED_PARAMETER(accumulated);285return MAX2(SUMOTime(0), myEventTime - myBlockTime);286}287288inline SUMOTime getTimeLoss() const {289// slow-downs while driving are not modelled290return getWaitingTime();291}292293/// @brief Returns the earliest leave time for the current segment294double getEventTimeSeconds() const {295return STEPS2TIME(getEventTime());296}297298/// @brief Returns the entry time for the current segment299double getLastEntryTimeSeconds() const {300return STEPS2TIME(getLastEntryTime());301}302303/// @brief Returns the time at which the vehicle was blocked on the current segment304double getBlockTimeSeconds() const {305return STEPS2TIME(getBlockTime());306}307308/// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty309double getCurrentLinkPenaltySeconds() const;310311/// @brief Returns the delay that is accrued due to option --meso-tls-penalty or --meso-minor-penalty312double getCurrentStoppingTimeSeconds() const;313314/// Replaces the current route by the given one315bool replaceRoute(ConstMSRoutePtr route, const std::string& info, bool onInit = false, int offset = 0, bool addRouteStops = true, bool removeStops = true, std::string* msgReturn = nullptr);316317/** @brief Returns whether the vehicle is allowed to pass the next junction, checks also for triggered stops318* @return true iff the vehicle may drive over the next junction319*/320bool mayProceed();321322/** @brief Updates a single vehicle detector if present323*/324void updateDetectorForWriting(MSMoveReminder* rem, SUMOTime currentTime, SUMOTime exitTime);325326/** @brief Updates all vehicle detectors327*/328void updateDetectors(SUMOTime currentTime, const bool isLeave,329const MSMoveReminder::Notification reason = MSMoveReminder::NOTIFICATION_JUNCTION);330331/** @brief Returns the velocity/lane influencer332*333* If no influencer was existing before, one is built, first334* @return Reference to this vehicle's speed influencer335*/336BaseInfluencer& getBaseInfluencer();337338const BaseInfluencer* getBaseInfluencer() const;339340bool hasInfluencer() const {341return myInfluencer != nullptr;342}343344/// @name state io345//@{346347/// Saves the states of a vehicle348void saveState(OutputDevice& out);349350/** @brief Loads the state of this vehicle from the given description351*/352void loadState(const SUMOSAXAttributes& attrs, const SUMOTime offset);353//@}354355356protected:357/// @brief The segment the vehicle is at358MESegment* mySegment;359360/// @brief Index of the que the vehicle is in (important for multiqueue extension)361int myQueIndex;362363/// @brief The (planned) time of leaving the segment (cell)364SUMOTime myEventTime;365366/// @brief The time the vehicle entered its current segment367SUMOTime myLastEntryTime;368369/// @brief The time at which the vehicle was blocked on its current segment370SUMOTime myBlockTime;371372/// @brief An instance of a velocity/lane influencing instance; built in "getInfluencer"373BaseInfluencer* myInfluencer;374375};376377378