CARLA
road/Map.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/geom/Rtree.h"
11 #include "carla/geom/Transform.h"
12 #include "carla/NonCopyable.h"
16 #include "carla/road/MapData.h"
17 #include "carla/road/RoadTypes.h"
18 #include "carla/road/MeshFactory.h"
19 #include "carla/geom/Vector3D.h"
21 
22 #include <boost/optional.hpp>
23 
24 #include <vector>
25 
26 namespace carla {
27 namespace road {
28 
29  class Map : private MovableNonCopyable {
30  public:
31 
33 
34  /// ========================================================================
35  /// -- Constructor ---------------------------------------------------------
36  /// ========================================================================
37 
38  Map(MapData m) : _data(std::move(m)) {
39  CreateRtree();
40  }
41 
42  /// ========================================================================
43  /// -- Georeference --------------------------------------------------------
44  /// ========================================================================
45 
47  return _data.GetGeoReference();
48  }
49 
50  /// ========================================================================
51  /// -- Geometry ------------------------------------------------------------
52  /// ========================================================================
53 
54  boost::optional<element::Waypoint> GetClosestWaypointOnRoad(
55  const geom::Location &location,
56  int32_t lane_type = static_cast<int32_t>(Lane::LaneType::Driving)) const;
57 
58  boost::optional<element::Waypoint> GetWaypoint(
59  const geom::Location &location,
60  int32_t lane_type = static_cast<int32_t>(Lane::LaneType::Driving)) const;
61 
62  boost::optional<element::Waypoint> GetWaypoint(
63  RoadId road_id,
64  LaneId lane_id,
65  float s) const;
66 
68 
69  /// ========================================================================
70  /// -- Road information ----------------------------------------------------
71  /// ========================================================================
72 
73  const Lane &GetLane(Waypoint waypoint) const;
74 
75  Lane::LaneType GetLaneType(Waypoint waypoint) const;
76 
77  double GetLaneWidth(Waypoint waypoint) const;
78 
79  JuncId GetJunctionId(RoadId road_id) const;
80 
81  bool IsJunction(RoadId road_id) const;
82 
83  std::pair<const element::RoadInfoMarkRecord *, const element::RoadInfoMarkRecord *>
84  GetMarkRecord(Waypoint waypoint) const;
85 
86  std::vector<element::LaneMarking> CalculateCrossedLanes(
87  const geom::Location &origin,
88  const geom::Location &destination) const;
89 
90  /// Returns a list of locations defining 2d areas,
91  /// when a location is repeated an area is finished
92  std::vector<geom::Location> GetAllCrosswalkZones() const;
93 
94  /// Data structure for the signal search
98  double accumulated_s = 0;
99  };
100 
101  /// Searches signals from an initial waypoint until the defined distance.
102  std::vector<SignalSearchData> GetSignalsInDistance(
103  Waypoint waypoint, double distance, bool stop_at_junction = false) const;
104 
105  /// Return all RoadInfoSignal in the map
106  std::vector<const element::RoadInfoSignal*>
107  GetAllSignalReferences() const;
108 
109  /// ========================================================================
110  /// -- Waypoint generation -------------------------------------------------
111  /// ========================================================================
112 
113  /// Return the list of waypoints placed at the entrance of each drivable
114  /// successor lane; i.e., the list of each waypoint in the next road segment
115  /// that a vehicle could drive from @a waypoint.
116  std::vector<Waypoint> GetSuccessors(Waypoint waypoint) const;
117  std::vector<Waypoint> GetPredecessors(Waypoint waypoint) const;
118 
119  /// Return the list of waypoints at @a distance such that a vehicle at @a
120  /// waypoint could drive to.
121  std::vector<Waypoint> GetNext(Waypoint waypoint, double distance) const;
122  /// Return the list of waypoints at @a distance in the reversed direction
123  /// that a vehicle at @a waypoint could drive to.
124  std::vector<Waypoint> GetPrevious(Waypoint waypoint, double distance) const;
125 
126  /// Return a waypoint at the lane of @a waypoint's right lane.
127  boost::optional<Waypoint> GetRight(Waypoint waypoint) const;
128 
129  /// Return a waypoint at the lane of @a waypoint's left lane.
130  boost::optional<Waypoint> GetLeft(Waypoint waypoint) const;
131 
132  /// Generate all the waypoints in @a map separated by @a approx_distance.
133  std::vector<Waypoint> GenerateWaypoints(double approx_distance) const;
134 
135  /// Generate waypoints on each @a lane at the start of each @a road
136  std::vector<Waypoint> GenerateWaypointsOnRoadEntries(Lane::LaneType lane_type = Lane::LaneType::Driving) const;
137 
138  /// Generate waypoints at the entry of each lane of the specified road
139  std::vector<Waypoint> GenerateWaypointsInRoad(RoadId road_id, Lane::LaneType lane_type = Lane::LaneType::Driving) const;
140 
141  /// Generate the minimum set of waypoints that define the topology of @a
142  /// map. The waypoints are placed at the entrance of each lane.
143  std::vector<std::pair<Waypoint, Waypoint>> GenerateTopology() const;
144 
145  /// Generate waypoints of the junction
146  std::vector<std::pair<Waypoint, Waypoint>> GetJunctionWaypoints(JuncId id, Lane::LaneType lane_type) const;
147 
149 
150  const Junction* GetJunction(JuncId id) const;
151 
152  std::unordered_map<road::RoadId, std::unordered_set<road::RoadId>>
154 
155  /// Buids a mesh based on the OpenDRIVE
157  const double distance,
158  const float extra_width = 0.6f,
159  const bool smooth_junctions = true) const;
160 
161  std::vector<std::unique_ptr<geom::Mesh>> GenerateChunkedMesh(
162  const rpc::OpendriveGenerationParameters& params) const;
163 
164  std::map<road::Lane::LaneType , std::vector<std::unique_ptr<geom::Mesh>>>
166  const geom::Vector3D& minpos,
167  const geom::Vector3D& maxpos) const;
168 
169  /// Buids a mesh of all crosswalks based on the OpenDRIVE
171 
172  std::vector<std::pair<geom::Transform, std::string>> GetTreesTransform(
173  const geom::Vector3D& minpos,
174  const geom::Vector3D& maxpos,
175  float distancebetweentrees,
176  float distancefromdrivinglineborder,
177  float s_offset = 0) const;
178 
179  geom::Mesh GenerateWalls(const double distance, const float wall_height) const;
180 
181  /// Buids a list of meshes related with LineMarkings
182  std::vector<std::unique_ptr<geom::Mesh>> GenerateLineMarkings(
184  const geom::Vector3D& minpos,
185  const geom::Vector3D& maxpos,
186  std::vector<std::string>& outinfo ) const;
187 
188  const std::unordered_map<SignId, std::unique_ptr<Signal>>& GetSignals() const {
189  return _data.GetSignals();
190  }
191 
192  const std::unordered_map<ContId, std::unique_ptr<Controller>>& GetControllers() const {
193  return _data.GetControllers();
194  }
195 
196  std::vector<carla::geom::BoundingBox> GetJunctionsBoundingBoxes() const;
197 
198 #ifdef LIBCARLA_WITH_GTEST
199  MapData &GetMap() {
200  return _data;
201  }
202 #endif // LIBCARLA_WITH_GTEST
203 
204 private:
205 
206  friend MapBuilder;
208 
211 
212  void CreateRtree();
213 
214  /// Helper Functions for constructing the rtree element list
215  void AddElementToRtree(
216  std::vector<Rtree::TreeElement> &rtree_elements,
217  geom::Transform &current_transform,
218  geom::Transform &next_transform,
219  Waypoint &current_waypoint,
220  Waypoint &next_waypoint);
221 
223  std::vector<Rtree::TreeElement> &rtree_elements,
224  geom::Transform &current_transform,
225  Waypoint &current_waypoint,
226  Waypoint &next_waypoint);
227 
228 public:
229  inline float GetZPosInDeformation(float posx, float posy) const;
230 
231  std::map<road::Lane::LaneType, std::vector<std::unique_ptr<geom::Mesh>>>
233  const std::vector<RoadId>& RoadsID,
234  const size_t index,
235  const size_t number_of_roads_per_thread) const;
236 
237  void GenerateJunctions(const carla::geom::MeshFactory& mesh_factory,
239  const geom::Vector3D& minpos,
240  const geom::Vector3D& maxpos,
241  std::map<road::Lane::LaneType, std::vector<std::unique_ptr<geom::Mesh>>>*
242  juntion_out_mesh_list) const;
243 
244  void GenerateSingleJunction(const carla::geom::MeshFactory& mesh_factory,
245  const JuncId Id,
246  std::map<road::Lane::LaneType, std::vector<std::unique_ptr<geom::Mesh>>>*
247  junction_out_mesh_list) const;
248 
249  // Return list of junction ID which are between those positions
250  std::vector<JuncId> FilterJunctionsByPosition(
251  const geom::Vector3D& minpos,
252  const geom::Vector3D& maxpos) const;
253  // Return list of roads ID which are between those positions
254  std::vector<RoadId> FilterRoadsByPosition(
255  const geom::Vector3D& minpos,
256  const geom::Vector3D& maxpos ) const;
257 
258  std::unique_ptr<geom::Mesh> SDFToMesh(const road::Junction& jinput, const std::vector<geom::Vector3D>& sdfinput, int grid_cells_per_dim) const;
259  };
260 
261 } // namespace road
262 } // namespace carla
const geom::GeoLocation & GetGeoReference() const
Definition: MapData.h:32
std::vector< carla::geom::BoundingBox > GetJunctionsBoundingBoxes() const
Definition: road/Map.cpp:1309
std::vector< element::LaneMarking > CalculateCrossedLanes(const geom::Location &origin, const geom::Location &destination) const
Definition: road/Map.cpp:449
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.
Definition: road/Map.cpp:1005
Seting for map generation from opendrive without additional geometry.
const std::unordered_map< ContId, std::unique_ptr< Controller > > & GetControllers() const
Definition: MapData.h:82
std::unordered_map< road::RoadId, std::unordered_set< road::RoadId > > ComputeJunctionConflicts(JuncId id) const
Definition: road/Map.cpp:764
Mesh helper generator.
Definition: MeshFactory.h:22
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:590
uint32_t RoadId
Definition: RoadTypes.h:15
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
Definition: road/Map.cpp:1525
std::vector< std::pair< Waypoint, Waypoint > > GenerateTopology() const
Generate the minimum set of waypoints that define the topology of map.
Definition: road/Map.cpp:724
boost::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
Definition: road/Map.cpp:212
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
Definition: road/Map.cpp:1347
geom::Mesh GetAllCrosswalkMesh() const
Buids a mesh of all crosswalks based on the OpenDRIVE.
Definition: road/Map.cpp:1252
friend MapBuilder
Definition: road/Map.h:206
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:455
const std::unordered_map< ContId, std::unique_ptr< Controller > > & GetControllers() const
Definition: road/Map.h:192
const Lane & GetLane(Waypoint waypoint) const
======================================================================== – Road information --------...
Definition: road/Map.cpp:834
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:662
Junction * GetJunction(JuncId id)
Definition: road/Map.cpp:997
Map(MapData m)
======================================================================== – Constructor -------------...
Definition: road/Map.h:38
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::vector< const element::RoadInfoSignal * > GetAllSignalReferences() const
Return all RoadInfoSignal in the map.
Definition: road/Map.cpp:437
void CreateRtree()
Definition: road/Map.cpp:887
int32_t JuncId
Definition: RoadTypes.h:17
geom::Mesh GenerateWalls(const double distance, const float wall_height) const
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
Definition: road/Map.cpp:1138
boost::optional< element::Waypoint > GetClosestWaypointOnRoad(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
======================================================================== – Geometry ----------------...
Definition: road/Map.cpp:165
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.
Definition: road/Map.cpp:1289
float GetZPosInDeformation(float posx, float posy) const
Definition: road/Map.cpp:1321
Inherit (privately) to suppress copy construction and assignment.
const std::unordered_map< SignId, std::unique_ptr< Signal > > & GetSignals() const
Definition: MapData.h:78
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:554
std::vector< RoadId > FilterRoadsByPosition(const geom::Vector3D &minpos, const geom::Vector3D &maxpos) const
Definition: road/Map.cpp:1427
Data structure for the signal search.
Definition: road/Map.h:95
LaneType
Can be used as flags.
Definition: Lane.h:29
int32_t LaneId
Definition: RoadTypes.h:19
geom::Transform ComputeTransform(Waypoint waypoint) const
Definition: road/Map.cpp:273
std::pair< const element::RoadInfoMarkRecord *, const element::RoadInfoMarkRecord * > GetMarkRecord(Waypoint waypoint) const
Definition: road/Map.cpp:307
std::vector< std::unique_ptr< geom::Mesh > > GenerateChunkedMesh(const rpc::OpendriveGenerationParameters &params) const
Definition: road/Map.cpp:1053
boost::optional< Waypoint > GetLeft(Waypoint waypoint) const
Return a waypoint at the lane of waypoint&#39;s left lane.
Definition: road/Map.cpp:636
Lane::LaneType GetLaneType(Waypoint waypoint) const
Definition: road/Map.cpp:281
Mesh data container, validator and exporter.
Definition: Mesh.h:44
bool IsJunction(RoadId road_id) const
Definition: road/Map.cpp:302
std::vector< Waypoint > GetPredecessors(Waypoint waypoint) const
Definition: road/Map.cpp:536
std::vector< JuncId > FilterJunctionsByPosition(const geom::Vector3D &minpos, const geom::Vector3D &maxpos) const
Definition: road/Map.cpp:1410
std::unique_ptr< geom::Mesh > SDFToMesh(const road::Junction &jinput, const std::vector< geom::Vector3D > &sdfinput, int grid_cells_per_dim) const
Definition: road/Map.cpp:1448
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:330
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) const
Definition: road/Map.cpp:1207
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:692
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.
Definition: road/Map.cpp:844
const geom::GeoLocation & GetGeoReference() const
======================================================================== – Georeference ------------...
Definition: road/Map.h:46
std::vector< Waypoint > GetSuccessors(Waypoint waypoint) const
======================================================================== – Waypoint generation -----...
Definition: road/Map.cpp:518
void AddElementToRtreeAndUpdateTransforms(std::vector< Rtree::TreeElement > &rtree_elements, geom::Transform &current_transform, Waypoint &current_waypoint, Waypoint &next_waypoint)
Definition: road/Map.cpp:865
std::vector< Waypoint > GenerateWaypoints(double approx_distance) const
Generate all the waypoints in map separated by approx_distance.
Definition: road/Map.cpp:648
const element::RoadInfoSignal * signal
Definition: road/Map.h:96
const std::unordered_map< SignId, std::unique_ptr< Signal > > & GetSignals() const
Definition: road/Map.h:188
JuncId GetJunctionId(RoadId road_id) const
Definition: road/Map.cpp:298
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
Definition: road/Map.cpp:1327
boost::optional< Waypoint > GetRight(Waypoint waypoint) const
Return a waypoint at the lane of waypoint&#39;s right lane.
Definition: road/Map.cpp:626
std::vector< std::pair< Waypoint, Waypoint > > GetJunctionWaypoints(JuncId id, Lane::LaneType lane_type) const
Generate waypoints of the junction.
Definition: road/Map.cpp:747
double GetLaneWidth(Waypoint waypoint) const
Definition: road/Map.cpp:285
MapData _data
Definition: road/Map.h:207