CARLA
Loading...
Searching...
No Matches
road/Map.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/geom/Rtree.h"
12#include "carla/NonCopyable.h"
16#include "carla/road/MapData.h"
19#include "carla/geom/Vector3D.h"
21
22#include <optional>
23
24#include <vector>
25
26namespace carla {
27namespace road {
28
29 class Map : private MovableNonCopyable {
30 public:
31
33
34 /// ========================================================================
35 /// -- Constructor ---------------------------------------------------------
36 /// ========================================================================
37
38 Map(MapData m) : _data(std::move(m)) {
40 }
41
42 /// ========================================================================
43 /// -- Georeference --------------------------------------------------------
44 /// ========================================================================
45
47 return _data.GetGeoReference();
48 }
49
51 return _data.GetGeoProjection();
52 }
53
54 /// ========================================================================
55 /// -- Geometry ------------------------------------------------------------
56 /// ========================================================================
57
58 std::optional<element::Waypoint> GetClosestWaypointOnRoad(
59 const geom::Location &location,
60 int32_t lane_type = static_cast<int32_t>(Lane::LaneType::Driving)) const;
61
62 std::optional<element::Waypoint> GetWaypoint(
63 const geom::Location &location,
64 int32_t lane_type = static_cast<int32_t>(Lane::LaneType::Driving)) const;
65
66 std::optional<element::Waypoint> GetWaypoint(
68 LaneId lane_id,
69 float s) const;
70
72
73 /// ========================================================================
74 /// -- Road information ----------------------------------------------------
75 /// ========================================================================
76
77 const Lane &GetLane(Waypoint waypoint) const;
78
79 Lane::LaneType GetLaneType(Waypoint waypoint) const;
80
81 double GetLaneWidth(Waypoint waypoint) const;
82
84
85 bool IsJunction(RoadId road_id) const;
86
87 std::pair<const element::RoadInfoMarkRecord *, const element::RoadInfoMarkRecord *>
88 GetMarkRecord(Waypoint waypoint) const;
89
90 std::vector<element::LaneMarking> CalculateCrossedLanes(
91 const geom::Location &origin,
92 const geom::Location &destination) const;
93
94 /// Returns a list of locations defining 2d areas,
95 /// when a location is repeated an area is finished
96 std::vector<geom::Location> GetAllCrosswalkZones() const;
97
98 /// Data structure for the signal search
104
105 /// Searches signals from an initial waypoint until the defined distance.
106 std::vector<SignalSearchData> GetSignalsInDistance(
107 Waypoint waypoint, double distance, bool stop_at_junction = false) const;
108
109 /// Return all RoadInfoSignal in the map
110 std::vector<const element::RoadInfoSignal*>
112
113 /// ========================================================================
114 /// -- Waypoint generation -------------------------------------------------
115 /// ========================================================================
116
117 /// Return the list of waypoints placed at the entrance of each drivable
118 /// successor lane; i.e., the list of each waypoint in the next road segment
119 /// that a vehicle could drive from @a waypoint.
120 std::vector<Waypoint> GetSuccessors(Waypoint waypoint) const;
121 std::vector<Waypoint> GetPredecessors(Waypoint waypoint) const;
122
123 /// Return the list of waypoints at @a distance such that a vehicle at @a
124 /// waypoint could drive to.
125 std::vector<Waypoint> GetNext(Waypoint waypoint, double distance) const;
126 /// Return the list of waypoints at @a distance in the reversed direction
127 /// that a vehicle at @a waypoint could drive to.
128 std::vector<Waypoint> GetPrevious(Waypoint waypoint, double distance) const;
129
130 /// Return a waypoint at the lane of @a waypoint's right lane.
131 std::optional<Waypoint> GetRight(Waypoint waypoint) const;
132
133 /// Return a waypoint at the lane of @a waypoint's left lane.
134 std::optional<Waypoint> GetLeft(Waypoint waypoint) const;
135
136 /// Generate all the waypoints in @a map separated by @a approx_distance.
137 std::vector<Waypoint> GenerateWaypoints(double approx_distance) const;
138
139 /// Generate waypoints on each @a lane at the start of each @a road
140 std::vector<Waypoint> GenerateWaypointsOnRoadEntries(Lane::LaneType lane_type = Lane::LaneType::Driving) const;
141
142 /// Generate waypoints at the entry of each lane of the specified road
143 std::vector<Waypoint> GenerateWaypointsInRoad(RoadId road_id, Lane::LaneType lane_type = Lane::LaneType::Driving) const;
144
145 /// Generate the minimum set of waypoints that define the topology of @a
146 /// map. The waypoints are placed at the entrance of each lane.
147 std::vector<std::pair<Waypoint, Waypoint>> GenerateTopology() const;
148
149 /// Generate waypoints of the junction
150 std::vector<std::pair<Waypoint, Waypoint>> GetJunctionWaypoints(JuncId id, Lane::LaneType lane_type) const;
151
153
154 const Junction* GetJunction(JuncId id) const;
155
156 std::unordered_map<road::RoadId, std::unordered_set<road::RoadId>>
158
159 /// Buids a mesh based on the OpenDRIVE
161 const double distance,
162 const float extra_width = 0.6f,
163 const bool smooth_junctions = true) const;
164
165 std::vector<std::unique_ptr<geom::Mesh>> GenerateChunkedMesh(
166 const rpc::OpendriveGenerationParameters& params) const;
167
168 /// Roads excluded from mesh/marking generation: link-less sub-2m
169 /// slivers and link-less duplicate traces overlapping a connected
170 /// road (DeepMap/NuRec export artifacts that render as floating
171 /// slabs of road stacked on the real carriageway).
172 std::unordered_set<road::RoadId> ComputeSkippedGenerationRoads() const;
173
174 std::map<road::Lane::LaneType , std::vector<std::unique_ptr<geom::Mesh>>>
176 const geom::Vector3D& minpos,
177 const geom::Vector3D& maxpos) const;
178
179 /// Buids a mesh of all crosswalks based on the OpenDRIVE
181
182 std::vector<std::pair<geom::Transform, std::string>> GetTreesTransform(
183 const geom::Vector3D& minpos,
184 const geom::Vector3D& maxpos,
185 float distancebetweentrees,
186 float distancefromdrivinglineborder,
187 float s_offset = 0,
188 bool measure_from_curb = false,
189 bool keep_on_sidewalk = false) const;
190
191 geom::Mesh GenerateWalls(const double distance, const float wall_height) const;
192
193 /// Buids a list of meshes related with LineMarkings
194 std::vector<std::unique_ptr<geom::Mesh>> GenerateLineMarkings(
196 const geom::Vector3D& minpos,
197 const geom::Vector3D& maxpos,
198 std::vector<std::string>& outinfo ) const;
199
200 const std::unordered_map<SignId, std::unique_ptr<Signal>>& GetSignals() const {
201 return _data.GetSignals();
202 }
203
204 const std::unordered_map<ContId, std::unique_ptr<Controller>>& GetControllers() const {
205 return _data.GetControllers();
206 }
207
208 const std::unordered_map<StencilId, std::unique_ptr<Stencil>>& GetStencils() const {
209 return _data.GetStencils();
210 }
211 /// Data structure for the stencil search
217
218 /// Searches stencils from an initial waypoint until the defined distance.
219 std::vector<StencilSearchData> GetStencilsInDistance(
220 Waypoint waypoint, double distance, bool stop_at_junction = false) const;
221
222 /// Return all RoadInfoStencil in the map
223 std::vector<const element::RoadInfoStencil*>
225
226 std::vector<carla::geom::BoundingBox> GetJunctionsBoundingBoxes() const;
227
228 /// Read-only access to the parsed OpenDRIVE data: roads, junctions,
229 /// signals and controllers. The client-side carla::client::Road,
230 /// LaneSection and Lane wrappers resolve their ids through this.
231 const MapData &GetData() const {
232 return _data;
233 }
234
235#ifdef LIBCARLA_WITH_GTEST
236 MapData &GetMap() {
237 return _data;
238 }
239#endif // LIBCARLA_WITH_GTEST
240
241private:
242
245
248
249 void CreateRtree();
250
251 /// Helper Functions for constructing the rtree element list
253 std::vector<Rtree::TreeElement> &rtree_elements,
254 geom::Transform &current_transform,
255 geom::Transform &next_transform,
256 Waypoint &current_waypoint,
257 Waypoint &next_waypoint);
258
260 std::vector<Rtree::TreeElement> &rtree_elements,
261 geom::Transform &current_transform,
262 Waypoint &current_waypoint,
263 Waypoint &next_waypoint);
264
265public:
266 inline float GetZPosInDeformation(float posx, float posy) const;
267
268 std::map<road::Lane::LaneType, std::vector<std::unique_ptr<geom::Mesh>>>
270 const std::vector<RoadId>& RoadsID,
271 const size_t index,
272 const size_t number_of_roads_per_thread) const;
273
274 void GenerateJunctions(const carla::geom::MeshFactory& mesh_factory,
276 const geom::Vector3D& minpos,
277 const geom::Vector3D& maxpos,
278 std::map<road::Lane::LaneType, std::vector<std::unique_ptr<geom::Mesh>>>*
279 juntion_out_mesh_list) const;
280
281 void GenerateSingleJunction(const carla::geom::MeshFactory& mesh_factory,
282 const JuncId Id,
283 std::map<road::Lane::LaneType, std::vector<std::unique_ptr<geom::Mesh>>>*
284 junction_out_mesh_list) const;
285
286 // Return list of junction ID which are between those positions
287 std::vector<JuncId> FilterJunctionsByPosition(
288 const geom::Vector3D& minpos,
289 const geom::Vector3D& maxpos) const;
290 // Return list of roads ID which are between those positions
291 std::vector<RoadId> FilterRoadsByPosition(
292 const geom::Vector3D& minpos,
293 const geom::Vector3D& maxpos ) const;
294
295 std::unique_ptr<geom::Mesh> SDFToMesh(const road::Junction& jinput, const std::vector<geom::Vector3D>& sdfinput, int grid_cells_per_dim) const;
296
297 /// Paves the regions a junction's lane corridors enclose but don't
298 /// cover (see GenerateChunkedMesh). Returns nullptr when there is
299 /// nothing to fill.
300 std::unique_ptr<geom::Mesh> GenerateJunctionFill(
302 const geom::Mesh &corridor_mesh) const;
303 };
304
305} // namespace road
306} // namespace carla
float distance
std::string road_id
Definition LaneGraph.cpp:39
std::map< std::string, unsigned int > road
std::map< std::string, unsigned int > junction
Inherit (privately) to suppress copy construction and assignment.
Mesh helper generator.
Definition MeshFactory.h:22
Mesh data container, validator and exporter.
Definition Mesh.h:44
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
LaneType
Can be used as flags.
Definition road/Lane.h:29
const geom::GeoProjection & GetGeoProjection() const
Definition MapData.h:38
const geom::GeoLocation & GetGeoReference() const
Definition MapData.h:34
const std::unordered_map< StencilId, std::unique_ptr< Stencil > > & GetStencils() const
Definition MapData.h:92
const std::unordered_map< SignId, std::unique_ptr< Signal > > & GetSignals() const
Definition MapData.h:84
const std::unordered_map< ContId, std::unique_ptr< Controller > > & GetControllers() const
Definition MapData.h:88
std::vector< std::pair< Waypoint, Waypoint > > GenerateTopology() const
Generate the minimum set of waypoints that define the topology of map.
Definition road/Map.cpp:884
std::vector< SignalSearchData > GetSignalsInDistance(Waypoint waypoint, double distance, bool stop_at_junction=false) const
Searches signals from an initial waypoint until the defined distance.
Definition road/Map.cpp:335
const std::unordered_map< StencilId, std::unique_ptr< Stencil > > & GetStencils() const
Definition road/Map.h:208
JuncId GetJunctionId(RoadId road_id) const
Definition road/Map.cpp:303
std::vector< Waypoint > GetPrevious(Waypoint waypoint, double distance) const
Return the list of waypoints at distance in the reversed direction that a vehicle at waypoint could d...
Definition road/Map.cpp:718
bool IsJunction(RoadId road_id) const
Definition road/Map.cpp:307
const geom::GeoProjection & GetGeoProjection() const
Definition road/Map.h:50
std::optional< Waypoint > GetLeft(Waypoint waypoint) const
Return a waypoint at the lane of waypoint's left lane.
Definition road/Map.cpp:789
Lane::LaneType GetLaneType(Waypoint waypoint) const
Definition road/Map.cpp:286
std::unique_ptr< geom::Mesh > SDFToMesh(const road::Junction &jinput, const std::vector< geom::Vector3D > &sdfinput, int grid_cells_per_dim) const
double GetLaneWidth(Waypoint waypoint) const
Definition road/Map.cpp:290
std::vector< std::pair< geom::Transform, std::string > > GetTreesTransform(const geom::Vector3D &minpos, const geom::Vector3D &maxpos, float distancebetweentrees, float distancefromdrivinglineborder, float s_offset=0, bool measure_from_curb=false, bool keep_on_sidewalk=false) const
std::vector< RoadId > FilterRoadsByPosition(const geom::Vector3D &minpos, const geom::Vector3D &maxpos) const
void AddElementToRtreeAndUpdateTransforms(std::vector< Rtree::TreeElement > &rtree_elements, geom::Transform &current_transform, Waypoint &current_waypoint, Waypoint &next_waypoint)
std::vector< Waypoint > GenerateWaypointsOnRoadEntries(Lane::LaneType lane_type=Lane::LaneType::Driving) const
Generate waypoints on each lane at the start of each road.
Definition road/Map.cpp:825
std::pair< const element::RoadInfoMarkRecord *, const element::RoadInfoMarkRecord * > GetMarkRecord(Waypoint waypoint) const
Definition road/Map.cpp:312
friend MapBuilder
Definition road/Map.h:243
std::vector< element::LaneMarking > CalculateCrossedLanes(const geom::Location &origin, const geom::Location &destination) const
Definition road/Map.cpp:522
std::vector< const element::RoadInfoStencil * > GetAllStencilReferences() const
Return all RoadInfoStencil in the map.
Definition road/Map.cpp:456
std::unordered_set< road::RoadId > ComputeSkippedGenerationRoads() const
Roads excluded from mesh/marking generation: link-less sub-2m slivers and link-less duplicate traces ...
geom::Mesh GenerateWalls(const double distance, const float wall_height) const
std::vector< carla::geom::BoundingBox > GetJunctionsBoundingBoxes() const
std::vector< JuncId > FilterJunctionsByPosition(const geom::Vector3D &minpos, const geom::Vector3D &maxpos) const
std::map< road::Lane::LaneType, std::vector< std::unique_ptr< geom::Mesh > > > GenerateRoadsMultithreaded(const carla::geom::MeshFactory &mesh_factory, const std::vector< RoadId > &RoadsID, const size_t index, const size_t number_of_roads_per_thread) const
std::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
Definition road/Map.cpp:217
geom::Mesh GetAllCrosswalkMesh() const
Buids a mesh of all crosswalks based on the OpenDRIVE.
std::vector< Waypoint > GetNext(Waypoint waypoint, double distance) const
Return the list of waypoints at distance such that a vehicle at waypoint could drive to.
Definition road/Map.cpp:669
std::optional< Waypoint > GetRight(Waypoint waypoint) const
Return a waypoint at the lane of waypoint's right lane.
Definition road/Map.cpp:767
Junction * GetJunction(JuncId id)
Map(MapData m)
========================================================================
Definition road/Map.h:38
const geom::GeoLocation & GetGeoReference() const
========================================================================
Definition road/Map.h:46
const MapData & GetData() const
Read-only access to the parsed OpenDRIVE data: roads, junctions, signals and controllers.
Definition road/Map.h:231
std::optional< element::Waypoint > GetClosestWaypointOnRoad(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
========================================================================
Definition road/Map.cpp:170
void GenerateJunctions(const carla::geom::MeshFactory &mesh_factory, const rpc::OpendriveGenerationParameters &params, const geom::Vector3D &minpos, const geom::Vector3D &maxpos, std::map< road::Lane::LaneType, std::vector< std::unique_ptr< geom::Mesh > > > *juntion_out_mesh_list) const
const std::unordered_map< SignId, std::unique_ptr< Signal > > & GetSignals() const
Definition road/Map.h:200
std::vector< Waypoint > GenerateWaypointsInRoad(RoadId road_id, Lane::LaneType lane_type=Lane::LaneType::Driving) const
Generate waypoints at the entry of each lane of the specified road.
Definition road/Map.cpp:854
const std::unordered_map< ContId, std::unique_ptr< Controller > > & GetControllers() const
Definition road/Map.h:204
std::vector< std::unique_ptr< geom::Mesh > > GenerateChunkedMesh(const rpc::OpendriveGenerationParameters &params) const
geom::Transform ComputeTransform(Waypoint waypoint) const
Definition road/Map.cpp:278
std::vector< StencilSearchData > GetStencilsInDistance(Waypoint waypoint, double distance, bool stop_at_junction=false) const
Searches stencils from an initial waypoint until the defined distance.
Definition road/Map.cpp:468
std::vector< Waypoint > GenerateWaypoints(double approx_distance) const
Generate all the waypoints in map separated by approx_distance.
Definition road/Map.cpp:811
std::map< road::Lane::LaneType, std::vector< std::unique_ptr< geom::Mesh > > > GenerateOrderedChunkedMeshInLocations(const rpc::OpendriveGenerationParameters &params, const geom::Vector3D &minpos, const geom::Vector3D &maxpos) const
void GenerateSingleJunction(const carla::geom::MeshFactory &mesh_factory, const JuncId Id, std::map< road::Lane::LaneType, std::vector< std::unique_ptr< geom::Mesh > > > *junction_out_mesh_list) const
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
Definition road/Map.cpp:643
std::vector< Waypoint > GetSuccessors(Waypoint waypoint) const
========================================================================
Definition road/Map.cpp:617
std::vector< geom::Location > GetAllCrosswalkZones() const
Returns a list of locations defining 2d areas, when a location is repeated an area is finished.
Definition road/Map.cpp:528
std::vector< std::unique_ptr< geom::Mesh > > GenerateLineMarkings(const rpc::OpendriveGenerationParameters &params, const geom::Vector3D &minpos, const geom::Vector3D &maxpos, std::vector< std::string > &outinfo) const
Buids a list of meshes related with LineMarkings.
float GetZPosInDeformation(float posx, float posy) const
void AddElementToRtree(std::vector< Rtree::TreeElement > &rtree_elements, geom::Transform &current_transform, geom::Transform &next_transform, Waypoint &current_waypoint, Waypoint &next_waypoint)
Helper Functions for constructing the rtree element list.
std::vector< const element::RoadInfoSignal * > GetAllSignalReferences() const
Return all RoadInfoSignal in the map.
Definition road/Map.cpp:443
std::vector< std::pair< Waypoint, Waypoint > > GetJunctionWaypoints(JuncId id, Lane::LaneType lane_type) const
Generate waypoints of the junction.
Definition road/Map.cpp:907
std::unordered_map< road::RoadId, std::unordered_set< road::RoadId > > ComputeJunctionConflicts(JuncId id) const
Definition road/Map.cpp:924
std::unique_ptr< geom::Mesh > GenerateJunctionFill(const road::Junction &junction, const geom::Mesh &corridor_mesh) const
Paves the regions a junction's lane corridors enclose but don't cover (see GenerateChunkedMesh).
const Lane & GetLane(Waypoint waypoint) const
========================================================================
Definition road/Map.cpp:994
geom::Mesh GenerateMesh(const double distance, const float extra_width=0.6f, const bool smooth_junctions=true) const
Buids a mesh based on the OpenDRIVE.
int32_t JuncId
Definition RoadTypes.h:17
uint32_t RoadId
Definition RoadTypes.h:15
int32_t LaneId
Definition RoadTypes.h:19
This file contains definitions of common data structures used in traffic manager.
Data structure for the signal search.
Definition road/Map.h:99
const element::RoadInfoSignal * signal
Definition road/Map.h:100
Data structure for the stencil search.
Definition road/Map.h:212
const element::RoadInfoStencil * stencil
Definition road/Map.h:213
Seting for map generation from opendrive without additional geometry.