CARLA
Loading...
Searching...
No Matches
road/Road.h
Go to the documentation of this file.
1// Copyright (c) 2026 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"
21
22#include <unordered_map>
23#include <vector>
24
25namespace carla {
26namespace 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 bool IsRHT() const;
50
51 Lane &GetLaneByDistance(double s, LaneId lane_id);
52
53 const Lane &GetLaneByDistance(double s, LaneId lane_id) const;
54
55 /// Get all lanes from all lane sections in a specific s
56 std::vector<Lane*> GetLanesByDistance(double s);
57
58 std::vector<const Lane*> GetLanesByDistance(double s) const;
59
60 RoadId GetSuccessor() const;
61
62 RoadId GetPredecessor() const;
63
64 Lane &GetLaneById(SectionId section_id, LaneId lane_id);
65
66 const Lane &GetLaneById(SectionId section_id, LaneId lane_id) const;
67
68 Lane *GetNextLane(const double s, const LaneId lane_id);
69
70 Lane *GetPrevLane(const double s, const LaneId lane_id);
71
72 /// Get the start section (from road coordinates s) given a @a lane id
74
75 /// Get the end section (from road coordinates s) given a @a lane id
77
78 std::vector<Road *> GetNexts() const;
79
80 std::vector<Road *> GetPrevs() const;
81
82 const geom::CubicPolynomial &GetElevationOn(const double s) const;
83
84 /// Surface height offset from the <lateralProfile><shape> records at
85 /// (s, t). Returns 0 when the road has no shape records. Interpolates
86 /// linearly between the two bracketing s-stations; within a station the
87 /// record with the greatest t <= query t applies (clamped to the first
88 /// record below its t range).
89 double GetLateralShapeZ(const double s, const double t) const;
90
91 /// Returns a directed point on the center of the road (lane 0),
92 /// with the corresponding laneOffset and elevation records applied,
93 /// on distance "s".
94 /// - @ param s distance regarding the road to compute the point
95 element::DirectedPoint GetDirectedPointIn(const double s) const;
96 /// Returns a directed point on the center of the road (lane 0),
97 /// with the corresponding laneOffset and elevation records applied,
98 /// on distance "s".
99 /// - @ param s distance regarding the road to compute the point
101
102 /// Returns a pair containing:
103 /// - @b first: distance to the nearest point on the center in
104 /// this road segment from the begining of it (s).
105 /// - @b second: Euclidean distance from the nearest point in
106 /// this road segment to p.
107 /// @param loc point to calculate the distance
108 const std::pair<double, double> GetNearestPoint(
109 const geom::Location &loc) const;
110
111 /// Returns a pointer to the nearest lane, given s relative to Road and
112 /// a location
113 /// @param s distance from the begining of the road to the point you
114 /// want to calculate the distance
115 /// @param loc point to calculate the distance
116 const std::pair<const Lane *, double> GetNearestLane(
117 const double s,
118 const geom::Location &loc,
119 uint32_t type = static_cast<uint32_t>(Lane::LaneType::Any)) const;
120
121 template <typename T>
122 const T *GetInfo(const double s) const {
123 return _info.GetInfo<T>(s);
124 }
125
126 template <typename T>
127 std::vector<const T*> GetInfos() const {
128 return _info.GetInfos<T>();
129 }
130
131 template <typename T>
132 std::vector<const T*> GetInfosInRange(const double min_s, const double max_s) const {
133 return _info.GetInfos<T>(min_s, max_s);
134 }
135
141
142 private:
143
144 template <typename MultiMapT>
145 static auto GetLessEqualRange(MultiMapT &map, double s) {
146 if (map.find(s) == map.end()) {
147 auto it = map.lower_bound(s);
148 if (it == map.begin()) {
149 return std::make_pair(map.end(), map.end());
150 }
151 s = (--it)->first;
152 }
153 return std::make_pair(map.lower_bound(s), map.upper_bound(s));
154 }
155
156 public:
157
158 auto GetLaneSectionsAt(const double s) {
159 auto pair = GetLessEqualRange(_lane_sections, s);
160 return MakeListView(
163 }
164
165 auto GetLaneSectionsAt(const double s) const {
166 auto pair = GetLessEqualRange(_lane_sections, s);
167 return MakeListView(
170 }
171
175
177 return _lane_sections.GetById(id);
178 }
179
180 /// Return the upper bound "s", i.e., the end distance of the lane section
181 /// at @a s (clamped at road's length).
182 double UpperBound(double s) const {
183 auto it = _lane_sections.upper_bound(s);
184 return it != _lane_sections.end() ? it->first : _length;
185 }
186
187 /// Get all lanes at a given s
188 std::map<LaneId, const Lane *> GetLanesAt(const double s) const;
189
190 private:
191
193
194 MapData *_map_data { nullptr };
195
196 RoadId _id { 0 };
197
198 std::string _name;
199
200 double _length { 0.0 };
201
202 bool _is_junction { false };
203
205
206 bool _is_rht { true };
207
209
211
213
215
216 std::vector<Road *> _nexts;
217
218 std::vector<Road *> _prevs;
219 };
220
221} // road
222} // carla
long long first
Definition LaneGraph.cpp:34
std::map< std::string, unsigned int > road
Inherit (privately) to suppress copy construction and assignment.
Describes a Cubic Polynomial so: f(x) = a + bx + cx^2 + dx^3.
const T * GetInfo(const double s) const
Returns single info given a type and a distance (s) from the start of the road.
std::vector< const T * > GetInfos() const
Return all infos given a type from the start of the road.
LaneSection & GetById(SectionId id)
std::vector< const T * > GetInfos() const
Definition road/Road.h:127
MapData * _map_data
Definition road/Road.h:194
std::vector< const T * > GetInfosInRange(const double min_s, const double max_s) const
Definition road/Road.h:132
RoadId GetPredecessor() const
Definition road/Road.cpp:60
double GetLength() const
Definition road/Road.cpp:40
const std::pair< double, double > GetNearestPoint(const geom::Location &loc) const
Returns a pair containing:
std::vector< Road * > GetPrevs() const
Definition road/Road.cpp:68
static auto GetLessEqualRange(MultiMapT &map, double s)
Definition road/Road.h:145
Lane * GetPrevLane(const double s, const LaneId lane_id)
std::vector< Road * > _nexts
Definition road/Road.h:216
const T * GetInfo(const double s) const
Definition road/Road.h:122
Lane & GetLaneById(SectionId section_id, LaneId lane_id)
InformationSet _info
Definition road/Road.h:214
double UpperBound(double s) const
Return the upper bound "s", i.e., the end distance of the lane section at s (clamped at road's length...
Definition road/Road.h:182
LaneSectionMap _lane_sections
Definition road/Road.h:208
RoadId GetSuccessor() const
Definition road/Road.cpp:56
double GetLateralShapeZ(const double s, const double t) const
Surface height offset from the <lateralProfile><shape> records at (s, t).
RoadId GetId() const
Definition road/Road.cpp:32
std::map< LaneId, const Lane * > GetLanesAt(const double s) const
Get all lanes at a given s.
std::vector< Road * > GetNexts() const
Definition road/Road.cpp:64
LaneSection * GetStartSection(LaneId id)
Get the start section (from road coordinates s) given a lane id.
Lane * GetNextLane(const double s, const LaneId lane_id)
auto GetLaneSections() const
Definition road/Road.h:136
const geom::CubicPolynomial & GetElevationOn(const double s) const
Definition road/Road.cpp:72
Lane & GetLaneByDistance(double s, LaneId lane_id)
Definition road/Road.cpp:80
std::vector< Road * > _prevs
Definition road/Road.h:218
auto GetLaneSectionsAt(const double s)
Definition road/Road.h:158
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.
bool IsRHT() const
Definition road/Road.cpp:52
auto GetLaneSectionsAt(const double s) const
Definition road/Road.h:165
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...
std::vector< Lane * > GetLanesByDistance(double s)
Get all lanes from all lane sections in a specific s.
Definition road/Road.cpp:94
const LaneSection & GetLaneSectionById(SectionId id) const
Definition road/Road.h:176
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...
JuncId GetJunctionId() const
Definition road/Road.cpp:48
LaneSection * GetEndSection(LaneId id)
Get the end section (from road coordinates s) given a lane id.
std::string GetName() const
Definition road/Road.cpp:36
std::string _name
Definition road/Road.h:198
const MapData * GetMap() const
Definition road/Road.cpp:28
bool IsJunction() const
Definition road/Road.cpp:44
LaneSection & GetLaneSectionById(SectionId id)
Definition road/Road.h:172
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
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 JuncId
Definition RoadTypes.h:17
uint32_t RoadId
Definition RoadTypes.h:15
int32_t LaneId
Definition RoadTypes.h:19
uint32_t SectionId
Definition RoadTypes.h:21
This file contains definitions of common data structures used in traffic manager.