LCOV - code coverage report
Current view: top level - src/microsim - MSLeaderInfo.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 40.0 % 10 4
Test Date: 2026-06-15 15:46:12 Functions: 0.0 % 3 0

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2002-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    MSLeaderInfo.h
      15              : /// @author  Jakob Erdmann
      16              : /// @date    Oct 2015
      17              : ///
      18              : // Information about vehicles ahead (may be multiple vehicles if
      19              : // lateral-resolution is active)
      20              : /****************************************************************************/
      21              : #pragma once
      22              : #include <config.h>
      23              : 
      24              : #include <string>
      25              : #include <vector>
      26              : #include <utils/common/StdDefs.h>
      27              : #include <utils/common/Translation.h>
      28              : 
      29              : // ===========================================================================
      30              : // class declarations
      31              : // ===========================================================================
      32              : class MSVehicle;
      33              : class MSLane;
      34              : 
      35              : 
      36              : // ===========================================================================
      37              : // type definitions
      38              : // ===========================================================================
      39              : typedef std::pair<const MSVehicle*, double> CLeaderDist;
      40              : typedef std::pair<MSVehicle*, double> LeaderDist;
      41              : 
      42              : 
      43              : // ===========================================================================
      44              : // class definitions
      45              : // ===========================================================================
      46              : /**
      47              :  * @class MSLeaderInfo
      48              :  */
      49   2893475862 : class MSLeaderInfo {
      50              : public:
      51              :     /// Constructor
      52              :     MSLeaderInfo(const double laneWidth, const MSVehicle* ego = nullptr, const double latOffset = 0.);
      53              : 
      54              :     /// Destructor
      55              :     virtual ~MSLeaderInfo();
      56              : 
      57              :     /* @brief adds this vehicle as a leader in the appropriate sublanes
      58              :      * @param[in] veh The vehicle to add
      59              :      * @param[in] beyond Whether the vehicle is beyond the existing leaders (and thus may be shadowed by them)
      60              :      * @param[in] latOffset The lateral offset that must be added to the position of veh
      61              :      * @return The number of free sublanes
      62              :      */
      63              :     virtual int addLeader(const MSVehicle* veh, bool beyond, double latOffset = 0.);
      64              : 
      65              :     /// @brief discard all information
      66              :     virtual void clear();
      67              : 
      68              :     /* @brief returns sublanes occupied by veh
      69              :      * @param[in] veh The vehicle to check
      70              :      * @param[in] latOffset The offset value to add to the vehicle position
      71              :      * @param[out] rightmost The rightmost sublane occupied by veh
      72              :      * @param[out] leftmost The rightmost sublane occupied by veh
      73              :      */
      74              :     void getSubLanes(const MSVehicle* veh, double latOffset, int& rightmost, int& leftmost) const;
      75              : 
      76              :     /* @brief returns the sublane boundaries of the ith sublane
      77              :      * @param[in] sublane The sublane to check
      78              :      * @param[in] latOffset The offset value to add to the result
      79              :      * @param[out] rightSide The right border of the given sublane
      80              :      * @param[out] leftSide The left border of the given sublane
      81              :      */
      82              :     void getSublaneBorders(int sublane, double latOffset, double& rightSide, double& leftSide) const;
      83              : 
      84              :     /// @brief return the vehicle for the given sublane
      85              :     const MSVehicle* operator[](int sublane) const;
      86              : 
      87              :     int numSublanes() const {
      88   5943773903 :         return (int)myVehicles.size();
      89              :     }
      90              : 
      91              :     int numFreeSublanes() const {
      92    465961897 :         return myFreeSublanes;
      93              :     }
      94              : 
      95              :     bool hasVehicles() const {
      96    490072130 :         return myHasVehicles;
      97              :     }
      98              : 
      99              :     const std::vector<const MSVehicle*>& getVehicles() const {
     100              :         return myVehicles;
     101              :     }
     102              : 
     103              :     int getSublaneOffset() const {
     104              :         return myOffset;
     105              :     }
     106              : 
     107              :     /// @brief set number of sublanes by which to shift positions
     108              :     void setSublaneOffset(int offset);
     109              : 
     110              :     /// @brief whether a stopped vehicle is leader
     111              :     bool hasStoppedVehicle() const;
     112              : 
     113              :     /// @brief whether the given vehicle is part of this leaderInfo
     114              :     bool hasVehicle(const MSVehicle* veh) const;
     115              : 
     116              :     /// @brief remove vehicles that are driving in the opposite direction (fully or partially) on the given lane
     117              :     void removeOpposite(const MSLane* lane);
     118              : 
     119              :     /// @brief print a debugging representation
     120              :     virtual std::string toString() const;
     121              : 
     122              : protected:
     123              : 
     124              :     /// @brief the width of the lane to which this instance applies
     125              :     // @note: not const to simplify assignment
     126              :     double myWidth;
     127              : 
     128              :     /// @brief an extra offset for shifting the interpretation of sublane borders (default [0,myWidth])
     129              :     int myOffset;
     130              : 
     131              :     std::vector<const MSVehicle*> myVehicles;
     132              : 
     133              :     /// @brief the number of free sublanes
     134              :     // if an ego vehicle is given in the constructor, the number of free
     135              :     // sublanes of those covered by ego
     136              :     int myFreeSublanes;
     137              : 
     138              :     /// @brief borders of the ego vehicle for filtering of free sublanes
     139              :     int egoRightMost;
     140              :     int egoLeftMost;
     141              : 
     142              :     bool myHasVehicles;
     143              : 
     144              : };
     145              : 
     146              : 
     147              : /// @brief saves leader/follower vehicles and their distances relative to an ego vehicle
     148              : class MSLeaderDistanceInfo : public MSLeaderInfo {
     149              : public:
     150              :     /// Constructor
     151              :     MSLeaderDistanceInfo(const double laneWidth, const MSVehicle* ego, const double latOffset);
     152              : 
     153              :     /// @brief Construct for the non-sublane-case
     154              :     MSLeaderDistanceInfo(const CLeaderDist& cLeaderDist, const double laneWidth);
     155              : 
     156              :     /// Destructor
     157              :     virtual ~MSLeaderDistanceInfo();
     158              : 
     159              :     /* @brief adds this vehicle as a leader in the appropriate sublanes
     160              :      * @param[in] veh The vehicle to add
     161              :      * @param[in] gap The gap between the egoFront+minGap to the back of veh
     162              :      *   or from the back of ego to the front+minGap of veh
     163              :      * @param[in] latOffset The lateral offset that must be added to the position of veh
     164              :      * @param[in] sublane The single sublane to which this leader shall be checked (-1 means: check for all)
     165              :      * @return The number of free sublanes
     166              :      */
     167              :     virtual int addLeader(const MSVehicle* veh, double gap, double latOffset = 0, int sublane = -1);
     168              : 
     169            0 :     virtual int addLeader(const MSVehicle* veh, bool beyond, double latOffset = 0) {
     170              :         UNUSED_PARAMETER(veh);
     171              :         UNUSED_PARAMETER(beyond);
     172              :         UNUSED_PARAMETER(latOffset);
     173            0 :         throw ProcessError(TL("Method not supported"));
     174              :     }
     175              : 
     176              :     /// @brief updatd empty sublanes with vehicles and gaps from other
     177              :     virtual void addLeaders(MSLeaderDistanceInfo& other);
     178              : 
     179              :     /// @brief discard all information
     180              :     virtual void clear();
     181              : 
     182              :     /// @brief return the vehicle and its distance for the given sublane
     183              :     CLeaderDist operator[](int sublane) const;
     184              : 
     185              :     /// @brief print a debugging representation
     186              :     virtual std::string toString() const;
     187              : 
     188              :     const std::vector<double>& getDistances() const {
     189              :         return myDistances;
     190              :     }
     191              : 
     192              :     /// @brief subtract vehicle length from all gaps if the leader vehicle is driving in the opposite direction
     193              :     void fixOppositeGaps(bool isFollower);
     194              : 
     195              :     /// @brief add given value to all gaps
     196              :     void patchGaps(double amount);
     197              : 
     198              :     /// @brief return vehicle with the smalles gap
     199              :     CLeaderDist getClosest() const;
     200              : 
     201              :     void moveSamePosTo(const MSVehicle* ego, MSLeaderDistanceInfo& other);
     202              : 
     203              :     /// @brief return minimum distance to a stopped vehicle or max double
     204              :     double getMinDistToStopped() const;
     205              : 
     206              : 
     207              : protected:
     208              : 
     209              :     std::vector<double> myDistances;
     210              : 
     211              : };
     212              : 
     213              : 
     214              : /* @brief saves follower vehicles and their distances as well as their required gap relative to an ego vehicle
     215              :  * when adding new followers, the one with the largest required gap is recored
     216              :  * (rather than the one with the smallest gap) */
     217              : class MSCriticalFollowerDistanceInfo : public MSLeaderDistanceInfo {
     218              : public:
     219              :     /// Constructor
     220              :     MSCriticalFollowerDistanceInfo(const double laneWidth, const MSVehicle* ego, const double latOffset, const bool haveOppositeLeaders = false);
     221              : 
     222              :     /// Destructor
     223              :     virtual ~MSCriticalFollowerDistanceInfo();
     224              : 
     225              :     /* @brief adds this vehicle as a follower in the appropriate sublanes
     226              :      * @param[in] veh The vehicle to add
     227              :      * @param[in] ego The vehicle which is being followed
     228              :      * @param[in] gap The distance from the back of ego to the follower
     229              :      * @param[in] latOffset The lateral offset that must be added to the position of veh
     230              :      * @param[in] sublane The single sublane to which this leader shall be checked (-1 means: check for all)
     231              :      * @return The number of free sublanes
     232              :      */
     233              :     int addFollower(const MSVehicle* veh, const MSVehicle* ego, double gap, double latOffset = 0, int sublane = -1);
     234              : 
     235            0 :     virtual int addLeader(const MSVehicle* veh, double gap, double latOffset = 0, int sublane = -1) {
     236              :         UNUSED_PARAMETER(veh);
     237              :         UNUSED_PARAMETER(gap);
     238              :         UNUSED_PARAMETER(latOffset);
     239              :         UNUSED_PARAMETER(sublane);
     240            0 :         throw ProcessError(TL("Method not supported"));
     241              :     }
     242              : 
     243            0 :     virtual int addLeader(const MSVehicle* veh, bool beyond, double latOffset = 0) {
     244              :         UNUSED_PARAMETER(veh);
     245              :         UNUSED_PARAMETER(beyond);
     246              :         UNUSED_PARAMETER(latOffset);
     247            0 :         throw ProcessError(TL("Method not supported"));
     248              :     }
     249              : 
     250              :     /// @brief discard all information
     251              :     void clear();
     252              : 
     253              :     /// @brief print a debugging representation
     254              :     std::string toString() const;
     255              : 
     256              : protected:
     257              : 
     258              :     // @brief the differences between requriedGap and actual gap for each of the followers
     259              :     std::vector<double> myMissingGaps;
     260              : 
     261              :     // @brief whether this Info objects tracks leaders instead of followers
     262              :     bool myHaveOppositeLeaders;
     263              : 
     264              : };
        

Generated by: LCOV version 2.0-1