CARLA
Road.h
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "carla/geom/Mesh.h"
10 #include "carla/Iterator.h"
11 #include "carla/ListView.h"
12 #include "carla/NonCopyable.h"
16 #include "carla/road/Junction.h"
17 #include "carla/road/LaneSection.h"
20 #include "carla/road/RoadTypes.h"
21 
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace carla {
26 namespace road {
27 
28  class MapData;
29  class Elevation;
30  class MapBuilder;
31 
32  class Road : private MovableNonCopyable {
33  public:
34 
35  Road() = default;
36 
37  const MapData *GetMap() const;
38 
39  RoadId GetId() const;
40 
41  std::string GetName() const;
42 
43  double GetLength() const;
44 
45  bool IsJunction() const;
46 
47  JuncId GetJunctionId() const;
48 
49  Lane &GetLaneByDistance(double s, LaneId lane_id);
50 
51  const Lane &GetLaneByDistance(double s, LaneId lane_id) const;
52 
53  /// Get all lanes from all lane sections in a specific s
54  std::vector<Lane*> GetLanesByDistance(double s);
55 
56  std::vector<const Lane*> GetLanesByDistance(double s) const;
57 
58  RoadId GetSuccessor() const;
59 
60  RoadId GetPredecessor() const;
61 
62  Lane &GetLaneById(SectionId section_id, LaneId lane_id);
63 
64  const Lane &GetLaneById(SectionId section_id, LaneId lane_id) const;
65 
66  Lane *GetNextLane(const double s, const LaneId lane_id);
67 
68  Lane *GetPrevLane(const double s, const LaneId lane_id);
69 
70  /// Get the start section (from road coordinates s) given a @a lane id
72 
73  /// Get the end section (from road coordinates s) given a @a lane id
75 
76  std::vector<Road *> GetNexts() const;
77 
78  std::vector<Road *> GetPrevs() const;
79 
80  const geom::CubicPolynomial &GetElevationOn(const double s) const;
81 
82  /// Returns a directed point on the center of the road (lane 0),
83  /// with the corresponding laneOffset and elevation records applied,
84  /// on distance "s".
85  /// - @ param s distance regarding the road to compute the point
86  element::DirectedPoint GetDirectedPointIn(const double s) const;
87  /// Returns a directed point on the center of the road (lane 0),
88  /// with the corresponding laneOffset and elevation records applied,
89  /// on distance "s".
90  /// - @ param s distance regarding the road to compute the point
92 
93  /// Returns a pair containing:
94  /// - @b first: distance to the nearest point on the center in
95  /// this road segment from the begining of it (s).
96  /// - @b second: Euclidean distance from the nearest point in
97  /// this road segment to p.
98  /// @param loc point to calculate the distance
99  const std::pair<double, double> GetNearestPoint(
100  const geom::Location &loc) const;
101 
102  /// Returns a pointer to the nearest lane, given s relative to Road and
103  /// a location
104  /// @param dist distance from the begining of the road to the point you
105  /// want to calculate the distance
106  /// @param loc point to calculate the distance
107  const std::pair<const Lane *, double> GetNearestLane(
108  const double s,
109  const geom::Location &loc,
110  uint32_t type = static_cast<uint32_t>(Lane::LaneType::Any)) const;
111 
112  template <typename T>
113  const T *GetInfo(const double s) const {
114  return _info.GetInfo<T>(s);
115  }
116 
117  template <typename T>
118  std::vector<const T*> GetInfos() const {
119  return _info.GetInfos<T>();
120  }
121 
122  template <typename T>
123  std::vector<const T*> GetInfosInRange(const double min_s, const double max_s) const {
124  return _info.GetInfos<T>(min_s, max_s);
125  }
126 
127  auto GetLaneSections() const {
128  return MakeListView(
131  }
132 
133  private:
134 
135  template <typename MultiMapT>
136  static auto GetLessEqualRange(MultiMapT &map, double s) {
137  if (map.find(s) == map.end()) {
138  auto it = map.lower_bound(s);
139  if (it == map.begin()) {
140  return std::make_pair(map.end(), map.end());
141  }
142  s = (--it)->first;
143  }
144  return std::make_pair(map.lower_bound(s), map.upper_bound(s));
145  }
146 
147  public:
148 
149  auto GetLaneSectionsAt(const double s) {
150  auto pair = GetLessEqualRange(_lane_sections, s);
151  return MakeListView(
154  }
155 
156  auto GetLaneSectionsAt(const double s) const {
157  auto pair = GetLessEqualRange(_lane_sections, s);
158  return MakeListView(
161  }
162 
164  return _lane_sections.GetById(id);
165  }
166 
168  return _lane_sections.GetById(id);
169  }
170 
171  /// Return the upper bound "s", i.e., the end distance of the lane section
172  /// at @a s (clamped at road's length).
173  double UpperBound(double s) const {
174  auto it = _lane_sections.upper_bound(s);
175  return it != _lane_sections.end() ? it->first : _length;
176  }
177 
178  /// Get all lanes at a given s
179  std::map<LaneId, const Lane *> GetLanesAt(const double s) const;
180 
181  private:
182 
183  friend MapBuilder;
184 
185  MapData *_map_data { nullptr };
186 
187  RoadId _id { 0 };
188 
189  std::string _name;
190 
191  double _length { 0.0 };
192 
193  bool _is_junction { false };
194 
196 
198 
200 
202 
204 
205  std::vector<Road *> _nexts;
206 
207  std::vector<Road *> _prevs;
208  };
209 
210 } // road
211 } // carla
std::vector< const T * > GetInfos() const
Definition: Road.h:118
std::string _name
Definition: Road.h:189
std::vector< Road * > _nexts
Definition: Road.h:205
uint32_t RoadId
Definition: RoadTypes.h:15
double _length
Definition: Road.h:191
Describes a Cubic Polynomial so: f(x) = a + bx + cx^2 + dx^3.
const std::pair< double, double > GetNearestPoint(const geom::Location &loc) const
Returns a pair containing:
Definition: Road.cpp:216
static auto make_map_values_iterator(It it)
Creates an iterator over non-const references to the values of a map.
Definition: Iterator.h:34
std::vector< const T * > GetInfosInRange(const double min_s, const double max_s) const
Definition: Road.h:123
const std::pair< const Lane *, double > GetNearestLane(const double s, const geom::Location &loc, uint32_t type=static_cast< uint32_t >(Lane::LaneType::Any)) const
Returns a pointer to the nearest lane, given s relative to Road and a location.
Definition: Road.cpp:241
RoadId GetSuccessor() const
Definition: Road.cpp:50
std::vector< Lane * > GetLanesByDistance(double s)
Get all lanes from all lane sections in a specific s.
Definition: Road.cpp:88
double UpperBound(double s) const
Return the upper bound "s", i.e., the end distance of the lane section at s (clamped at road&#39;s length...
Definition: Road.h:173
std::vector< Road * > GetNexts() const
Definition: Road.cpp:58
std::vector< Road * > _prevs
Definition: Road.h:207
bool IsJunction() const
Definition: Road.cpp:42
Lane * GetNextLane(const double s, const LaneId lane_id)
Definition: Road.cpp:119
const geom::CubicPolynomial & GetElevationOn(const double s) const
Definition: Road.cpp:66
Lane * GetPrevLane(const double s, const LaneId lane_id)
Definition: Road.cpp:136
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
LaneSection * GetEndSection(LaneId id)
Get the end section (from road coordinates s) given a lane id.
Definition: Road.cpp:167
uint32_t SectionId
Definition: RoadTypes.h:21
static auto GetLessEqualRange(MultiMapT &map, double s)
Definition: Road.h:136
JuncId _junction_id
Definition: Road.h:195
auto GetLaneSectionsAt(const double s)
Definition: Road.h:149
InformationSet _info
Definition: Road.h:203
int32_t JuncId
Definition: RoadTypes.h:17
const T * GetInfo(const double s) const
Definition: Road.h:113
const T * GetInfo(const double s) const
Returns single info given a type and a distance (s) from the start of the road.
auto GetLaneSections() const
Definition: Road.h:127
Inherit (privately) to suppress copy construction and assignment.
auto GetLaneSectionsAt(const double s) const
Definition: Road.h:156
RoadId GetId() const
Definition: Road.cpp:30
MapData * _map_data
Definition: Road.h:185
Lane & GetLaneByDistance(double s, LaneId lane_id)
Definition: Road.cpp:74
static auto make_map_values_const_iterator(It it)
Creates an iterator over const references to the values of a map.
Definition: Iterator.h:43
int32_t LaneId
Definition: RoadTypes.h:19
const LaneSection & GetLaneSectionById(SectionId id) const
Definition: Road.h:167
Lane & GetLaneById(SectionId section_id, LaneId lane_id)
Definition: Road.cpp:110
RoadId _successor
Definition: Road.h:199
LaneSectionMap _lane_sections
Definition: Road.h:197
LaneSection & GetLaneSectionById(SectionId id)
Definition: Road.h:163
RoadId _predecessor
Definition: Road.h:201
JuncId GetJunctionId() const
Definition: Road.cpp:46
RoadId GetPredecessor() const
Definition: Road.cpp:54
RoadId _id
Definition: Road.h:187
std::map< LaneId, const Lane * > GetLanesAt(const double s) const
Get all lanes at a given s.
Definition: Road.cpp:307
std::vector< const T * > GetInfos() const
Return all infos given a type from the start of the road.
LaneSection * GetStartSection(LaneId id)
Get the start section (from road coordinates s) given a lane id.
Definition: Road.cpp:154
std::string GetName() const
Definition: Road.cpp:34
element::DirectedPoint GetDirectedPointInNoLaneOffset(const double s) const
Returns a directed point on the center of the road (lane 0), with the corresponding laneOffset and el...
Definition: Road.cpp:202
bool _is_junction
Definition: Road.h:193
const MapData * GetMap() const
Definition: Road.cpp:26
double GetLength() const
Definition: Road.cpp:38
static auto MakeListView(Iterator begin, Iterator end)
LaneSection & GetById(SectionId id)
friend MapBuilder
Definition: Road.h:183
element::DirectedPoint GetDirectedPointIn(const double s) const
Returns a directed point on the center of the road (lane 0), with the corresponding laneOffset and el...
Definition: Road.cpp:180
std::vector< Road * > GetPrevs() const
Definition: Road.cpp:62