CARLA
Loading...
Searching...
No Matches
client/Map.cpp
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#include "carla/client/Map.h"
8
11#include "carla/client/Lane.h"
13#include "carla/client/Road.h"
16#include "carla/road/Road.h"
18#include "carla/road/Map.h"
21
22#include <sstream>
23
24namespace carla {
25namespace client {
26
27 static auto MakeMap(const std::string &opendrive_contents) {
28 auto stream = std::istringstream(opendrive_contents);
29 auto map = opendrive::OpenDriveParser::Load(stream.str());
30 if (!map.has_value()) {
31 throw_exception(std::runtime_error("failed to generate map"));
32 }
33 return std::move(*map);
34 }
35
36 Map::Map(rpc::MapInfo description, std::string xodr_content)
37 : _description(std::move(description)),
38 _map(MakeMap(xodr_content)){
39 open_drive_file = xodr_content;
40 }
41 Map::Map(std::string name, std::string xodr_content)
42 : Map(rpc::MapInfo{
43 std::move(name),
44 std::vector<geom::Transform>{}}, xodr_content) {
45 open_drive_file = xodr_content;
46 }
47
48 Map::~Map() = default;
49
50 SharedPtr<Waypoint> Map::GetWaypoint(
51 const geom::Location &location,
52 bool project_to_road,
53 int32_t lane_type) const {
54 std::optional<road::element::Waypoint> waypoint;
55 if (project_to_road) {
56 waypoint = _map.GetClosestWaypointOnRoad(location, lane_type);
57 } else {
58 waypoint = _map.GetWaypoint(location, lane_type);
59 }
60 return waypoint.has_value() ?
61 SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
62 nullptr;
63 }
64
65 SharedPtr<Waypoint> Map::GetWaypointXODR(
67 carla::road::LaneId lane_id,
68 float s) const {
69 std::optional<road::element::Waypoint> waypoint;
70 waypoint = _map.GetWaypoint(road_id, lane_id, s);
71 return waypoint.has_value() ?
72 SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
73 nullptr;
74 }
75
76 Map::TopologyList Map::GetTopology() const {
77 namespace re = carla::road::element;
78 std::unordered_map<re::Waypoint, SharedPtr<Waypoint>> waypoints;
79
80 auto get_or_make_waypoint = [&](const auto &waypoint) {
81 auto it = waypoints.find(waypoint);
82 if (it == waypoints.end()) {
83 it = waypoints.emplace(
84 waypoint,
85 SharedPtr<Waypoint>(new Waypoint{shared_from_this(), waypoint})).first;
86 }
87 return it->second;
88 };
89
90 TopologyList result;
91 auto topology = _map.GenerateTopology();
92 result.reserve(topology.size());
93 for (const auto &pair : topology) {
94 result.emplace_back(
95 get_or_make_waypoint(pair.first),
96 get_or_make_waypoint(pair.second));
97 }
98 return result;
99 }
100
101 std::vector<SharedPtr<Waypoint>> Map::GenerateWaypoints(double distance) const {
102 std::vector<SharedPtr<Waypoint>> result;
103 const auto waypoints = _map.GenerateWaypoints(distance);
104 result.reserve(waypoints.size());
105 for (const auto &waypoint : waypoints) {
106 result.emplace_back(SharedPtr<Waypoint>(new Waypoint{shared_from_this(), waypoint}));
107 }
108 return result;
109 }
110
111 std::vector<road::element::LaneMarking> Map::CalculateCrossedLanes(
112 const geom::Location &origin,
113 const geom::Location &destination) const {
114 return _map.CalculateCrossedLanes(origin, destination);
115 }
116
117 const geom::GeoLocation &Map::GetGeoReference() const {
118 return _map.GetGeoReference();
119 }
120
121 const geom::GeoProjection &Map::GetGeoProjection() const {
122 return _map.GetGeoProjection();
123 }
124
125 std::vector<geom::Location> Map::GetAllCrosswalkZones() const {
126 return _map.GetAllCrosswalkZones();
127 }
128
129 SharedPtr<Junction> Map::GetJunction(const Waypoint &waypoint) const {
130 const road::Junction *juncptr = GetMap().GetJunction(waypoint.GetJunctionId());
131 auto junction = SharedPtr<Junction>(new Junction(shared_from_this(), juncptr));
132 return junction;
133 }
134
135 std::vector<std::pair<SharedPtr<Waypoint>, SharedPtr<Waypoint>>> Map::GetJunctionWaypoints(
136 road::JuncId id,
137 road::Lane::LaneType lane_type) const {
138 std::vector<std::pair<SharedPtr<Waypoint>, SharedPtr<Waypoint>>> result;
139 auto junction_waypoints = GetMap().GetJunctionWaypoints(id, lane_type);
140 for (auto &waypoint_pair : junction_waypoints) {
141 result.emplace_back(
142 std::make_pair(SharedPtr<Waypoint>(new Waypoint(shared_from_this(), waypoint_pair.first)),
143 SharedPtr<Waypoint>(new Waypoint(shared_from_this(), waypoint_pair.second))));
144 }
145 return result;
146 }
147
148 SharedPtr<Junction> Map::GetJunctionById(road::JuncId id) const {
149 const road::Junction *junction = _map.GetJunction(id);
150 if (junction == nullptr) {
151 return nullptr;
152 }
153 return SharedPtr<Junction>(new Junction(shared_from_this(), junction));
154 }
155
156 std::vector<SharedPtr<Junction>> Map::GetJunctions() const {
157 std::vector<SharedPtr<Junction>> result;
158 for (const auto &pair : _map.GetData().GetJunctions()) {
159 result.emplace_back(SharedPtr<Junction>(new Junction(shared_from_this(), &pair.second)));
160 }
161 return result;
162 }
163
164 std::vector<SharedPtr<Road>> Map::GetRoads() const {
165 std::vector<SharedPtr<Road>> result;
166 const auto &roads = _map.GetData().GetRoads();
167 result.reserve(roads.size());
168 for (const auto &pair : roads) {
169 result.emplace_back(SharedPtr<Road>(new Road(shared_from_this(), pair.first)));
170 }
171 return result;
172 }
173
175 if (!_map.GetData().ContainsRoad(road_id)) {
176 return nullptr;
177 }
178 return SharedPtr<Road>(new Road(shared_from_this(), road_id));
179 }
180
181 SharedPtr<LaneSection> Map::GetLaneSection(
183 road::SectionId section_id) const {
184 if (!_map.GetData().ContainsRoad(road_id)) {
185 return nullptr;
186 }
187 const road::Road &road = _map.GetData().GetRoad(road_id);
188 for (const road::LaneSection &section : road.GetLaneSections()) {
189 if (section.GetId() == section_id) {
190 return SharedPtr<LaneSection>(new LaneSection(shared_from_this(), road_id, section_id));
191 }
192 }
193 return nullptr;
194 }
195
196 SharedPtr<Lane> Map::GetLane(
198 road::SectionId section_id,
199 road::LaneId lane_id) const {
200 if (!_map.GetData().ContainsRoad(road_id)) {
201 return nullptr;
202 }
203 const road::Road &road = _map.GetData().GetRoad(road_id);
204 for (const road::LaneSection &section : road.GetLaneSections()) {
205 if (section.GetId() == section_id) {
206 if (!section.ContainsLane(lane_id)) {
207 return nullptr;
208 }
209 return SharedPtr<Lane>(new Lane(shared_from_this(), road_id, section_id, lane_id));
210 }
211 }
212 return nullptr;
213 }
214
215 SharedPtr<Waypoint> Map::MakeWaypoint(road::element::Waypoint waypoint) const {
216 return SharedPtr<Waypoint>(new Waypoint(shared_from_this(), std::move(waypoint)));
217 }
218
219 std::vector<SharedPtr<Landmark>> Map::GetAllLandmarks() const {
220 std::vector<SharedPtr<Landmark>> result;
221 auto signal_references = _map.GetAllSignalReferences();
222 for(auto* signal_reference : signal_references) {
223 result.emplace_back(
224 new Landmark(nullptr, shared_from_this(), signal_reference, 0));
225 }
226 return result;
227 }
228
229 std::vector<SharedPtr<Landmark>> Map::GetLandmarksFromId(std::string id) const {
230 std::vector<SharedPtr<Landmark>> result;
231 auto signal_references = _map.GetAllSignalReferences();
232 for(auto* signal_reference : signal_references) {
233 if(signal_reference->GetSignalId() == id) {
234 result.emplace_back(
235 new Landmark(nullptr, shared_from_this(), signal_reference, 0));
236 }
237 }
238 return result;
239 }
240
241 std::vector<SharedPtr<Landmark>> Map::GetAllLandmarksOfType(std::string type) const {
242 std::vector<SharedPtr<Landmark>> result;
243 auto signal_references = _map.GetAllSignalReferences();
244 for(auto* signal_reference : signal_references) {
245 if(signal_reference->GetSignal()->GetType() == type) {
246 result.emplace_back(
247 new Landmark(nullptr, shared_from_this(), signal_reference, 0));
248 }
249 }
250 return result;
251 }
252
253 std::vector<SharedPtr<Landmark>>
254 Map::GetLandmarkGroup(const Landmark &landmark) const {
255 std::vector<SharedPtr<Landmark>> result;
256 auto &controllers = landmark._signal->GetSignal()->GetControllers();
257 for (auto& controller_id : controllers) {
258 const auto &controller = _map.GetControllers().at(controller_id);
259 for(auto& signal_id : controller->GetSignals()) {
260 auto& signal = _map.GetSignals().at(signal_id);
261 auto new_landmarks = GetLandmarksFromId(signal->GetSignalId());
262 result.insert(result.end(), new_landmarks.begin(), new_landmarks.end());
263 }
264 }
265 return result;
266 }
267
268 void Map::CookInMemoryMap(const std::string& path) const {
269 traffic_manager::InMemoryMap::Cook(shared_from_this(), path);
270 }
271
272} // namespace client
273} // 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
Class containing a reference to RoadInfoSignal.
Definition Landmark.h:22
const road::element::RoadInfoSignal * _signal
Definition Landmark.h:137
A stretch of road over which the lane layout does not change.
A single OpenDRIVE lane, identified by (road id, lane section id, lane id).
Definition client/Lane.h:37
std::string open_drive_file
Definition client/Map.h:134
Map(rpc::MapInfo description, std::string xodr_content)
SharedPtr< Waypoint > GetWaypoint(const geom::Location &location, bool project_to_road=true, int32_t lane_type=static_cast< uint32_t >(road::Lane::LaneType::Driving)) const
std::vector< std::pair< SharedPtr< Waypoint >, SharedPtr< Waypoint > > > TopologyList
Definition client/Map.h:67
An OpenDRIVE road: a run of carriageway divided along s into lane sections, each holding the lanes.
Definition client/Road.h:36
road::JuncId GetJunctionId() const
static std::optional< road::Map > Load(const std::string &opendrive)
LaneType
Can be used as flags.
Definition road/Lane.h:29
const std::set< ContId > & GetControllers() const
Definition Signal.h:176
static void Cook(WorldMap world_map, const std::string &path)
static auto MakeMap(const std::string &opendrive_contents)
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.
std::shared_ptr< T > SharedPtr
Definition Memory.h:15
void throw_exception(const ExceptionT &e)
Templated on the static type so the thrown object keeps its dynamic type: taking a const std::excepti...
Definition Exception.h:44