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

Generated by: LCOV version 2.0-1