CARLA
Loading...
Searching...
No Matches
LaneParser.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
8
10
12
13namespace carla {
14namespace opendrive {
15namespace parser {
16
17 static void ParseLanes(
19 double s,
20 const pugi::xml_node &parent_node,
21 carla::road::MapBuilder &map_builder) {
22 for (pugi::xml_node lane_node : parent_node.children("lane")) {
23
24 road::LaneId lane_id = lane_node.attribute("id").as_int();
25
26 road::Lane *lane = map_builder.GetLane(road_id, lane_id, s);
27
28 // Lane Width
29 int width_count = 0;
30 for (pugi::xml_node lane_width_node : lane_node.children("width")) {
31 const double s_offset = lane_width_node.attribute("sOffset").as_double();
32 const double a = lane_width_node.attribute("a").as_double();
33 const double b = lane_width_node.attribute("b").as_double();
34 const double c = lane_width_node.attribute("c").as_double();
35 const double d = lane_width_node.attribute("d").as_double();
36
37 // Call Map builder create Lane Width function
38 map_builder.CreateLaneWidth(lane, s_offset + s, a, b, c, d);
39 width_count++;
40 }
41 if (width_count == 0) {
42 map_builder.CreateLaneWidth(lane, s, 0.0, 0.0, 0.0, 0.0);
43 if (lane->GetId() != 0) {
44 std::cout << "WARNING: In road " << lane->GetRoad()->GetId() << " lane " << lane->GetId() <<
45 " no \"<width>\" parameter found under \"<lane>\" tag. Using default values." << std::endl;
46 }
47 }
48
49 // Lane Border
50 for (pugi::xml_node lane_border_node : lane_node.children("border")) {
51 const double s_offset = lane_border_node.attribute("sOffset").as_double();
52 const double a = lane_border_node.attribute("a").as_double();
53 const double b = lane_border_node.attribute("b").as_double();
54 const double c = lane_border_node.attribute("c").as_double();
55 const double d = lane_border_node.attribute("d").as_double();
56
57 // Call Map builder create Lane Border function
58 map_builder.CreateLaneBorder(lane, s_offset + s, a, b, c, d);
59 }
60
61 // Lane Road Mark
62 int road_mark_id = 0;
63 for (pugi::xml_node lane_road_mark : lane_node.children("roadMark")) {
64 pugi::xml_node road_mark_type;
65 {
66 const double s_offset = lane_road_mark.attribute("sOffset").as_double();
67 const std::string type = lane_road_mark.attribute("type").value();
68 const std::string weight = lane_road_mark.attribute("weight").value();
69 const std::string color = lane_road_mark.attribute("color").value();
70 const std::string material = lane_road_mark.attribute("material").value();
71 const double width = lane_road_mark.attribute("width").as_double();
72 const std::string lane_change = lane_road_mark.attribute("laneChange").value();
73 const double height = lane_road_mark.attribute("height").as_double();
74
75 // Call map builder for LaneRoadMarkType
76
77 std::string type_name = "";
78 double type_width = 0.0;
79 road_mark_type = lane_road_mark.child("type");
80 if (road_mark_type) {
81 type_name = road_mark_type.attribute("name").value();
82 type_width = road_mark_type.attribute("width").as_double();
83 }
84
85 bool is_rht = lane->GetRoad()->IsRHT();
86 // Call map builder for LaneRoadMark
87 map_builder.CreateRoadMark(
88 lane,
89 road_mark_id,
90 s_offset + s,
91 type,
92 weight,
93 color,
94 material,
95 width,
96 lane_change,
97 height,
98 type_name,
99 type_width,
100 is_rht);
101 }
102
103 for (pugi::xml_node road_mark_type_line_node : road_mark_type.children("line")) {
104
105 const double length = road_mark_type_line_node.attribute("length").as_double();
106 const double space = road_mark_type_line_node.attribute("space").as_double();
107 const double t = road_mark_type_line_node.attribute("tOffset").as_double();
108 const double s_offset = road_mark_type_line_node.attribute("sOffset").as_double();
109 const std::string rule = road_mark_type_line_node.attribute("rule").value();
110 const double width = road_mark_type_line_node.attribute("width").as_double();
111
112 // Call map builder for LaneRoadMarkType LaneRoadMarkTypeLine
113 map_builder.CreateRoadMarkTypeLine(
114 lane,
115 road_mark_id,
116 length,
117 space,
118 t,
119 s_offset + s,
120 rule,
121 width);
122 }
123 ++road_mark_id;
124 }
125
126 // Lane Material
127 for (pugi::xml_node lane_material_node : lane_node.children("material")) {
128
129 const double s_offset = lane_material_node.attribute("sOffset").as_double();
130 const std::string surface = lane_material_node.attribute("surface").value();
131 const double friction = lane_material_node.attribute("friction").as_double();
132 const double roughness = lane_material_node.attribute("roughness").as_double();
133
134 // Create map builder for Lane Material
135 map_builder.CreateLaneMaterial(lane, s_offset + s, surface, friction, roughness);
136 }
137
138 // Lane Visibility
139 for (pugi::xml_node lane_visibility_node : lane_node.children("visibility")) {
140 const double s_offset = lane_visibility_node.attribute("sOffset").as_double();
141 const double forward = lane_visibility_node.attribute("forward").as_double();
142 const double back = lane_visibility_node.attribute("back").as_double();
143 const double left = lane_visibility_node.attribute("left").as_double();
144 const double right = lane_visibility_node.attribute("right").as_double();
145
146 // Create map builder for Lane Visibility
147 map_builder.CreateLaneVisibility(lane, s_offset + s, forward, back, left, right);
148 }
149
150 // Lane Speed
151 for (pugi::xml_node lane_speed_node : lane_node.children("speed")) {
152 const double s_offset = lane_speed_node.attribute("sOffset").as_double();
153 const double max = lane_speed_node.attribute("max").as_double();
154 std::string unit = lane_speed_node.attribute("unit").value();
155
156 // Create map builder for Lane Speed
157 map_builder.CreateLaneSpeed(lane, s_offset + s, max, unit);
158 }
159
160 // Lane Access
161 for (pugi::xml_node lane_access_node : lane_node.children("access")) {
162 const double s_offset = lane_access_node.attribute("sOffset").as_double();
163 const std::string restriction = lane_access_node.attribute("restriction").value();
164
165 // Create map builder for Lane Access
166 map_builder.CreateLaneAccess(lane, s_offset + s, restriction);
167 }
168
169 // Lane Height
170 for (pugi::xml_node lane_height_node : lane_node.children("height")) {
171 const double s_offset = lane_height_node.attribute("sOffset").as_double();
172 const double inner = lane_height_node.attribute("inner").as_double();
173 const double outer = lane_height_node.attribute("outer").as_double();
174
175 // Create map builder for Lane Height
176 map_builder.CreateLaneHeight(lane, s_offset + s, inner, outer);
177 }
178
179 // Lane Rule
180 for (pugi::xml_node lane_rule_node : lane_node.children("rule")) {
181 const double s_offset = lane_rule_node.attribute("sOffset").as_double();
182 const std::string value = lane_rule_node.attribute("value").value();
183
184 // Create map builder for Lane Height
185 map_builder.CreateLaneRule(lane, s_offset + s, value);
186 }
187
188 }
189 }
190
192 const pugi::xml_document &xml,
193 carla::road::MapBuilder &map_builder) {
194
195 pugi::xml_node open_drive_node = xml.child("OpenDRIVE");
196
197 // Lanes
198 for (pugi::xml_node road_node : open_drive_node.children("road")) {
199 road::RoadId road_id = road_node.attribute("id").as_uint();
200
201 for (pugi::xml_node lanes_node : road_node.children("lanes")) {
202
203 for (pugi::xml_node lane_section_node : lanes_node.children("laneSection")) {
204 double s = lane_section_node.attribute("s").as_double();
205 pugi::xml_node left_node = lane_section_node.child("left");
206 if (left_node) {
207 ParseLanes(road_id, s, left_node, map_builder);
208 }
209
210 pugi::xml_node center_node = lane_section_node.child("center");
211 if (center_node) {
212 ParseLanes(road_id, s, center_node, map_builder);
213 }
214
215 pugi::xml_node right_node = lane_section_node.child("right");
216 if (right_node) {
217 ParseLanes(road_id, s, right_node, map_builder);
218 }
219 }
220 }
221 }
222 }
223
224} // namespace parser
225} // namespace opendrive
226} // namespace carla
std::string road_id
Definition LaneGraph.cpp:39
static void Parse(const pugi::xml_document &xml, carla::road::MapBuilder &map_builder)
Road * GetRoad() const
Definition road/Lane.cpp:29
LaneId GetId() const
Definition road/Lane.cpp:34
void CreateRoadMark(Lane *lane, const int road_mark_id, const double s, const std::string type, const std::string weight, const std::string color, const std::string material, const double width, const std::string lane_change, const double height, const std::string type_name, const double type_width, const bool is_rht)
void CreateLaneAccess(Lane *lane, const double s, const std::string restriction)
void CreateLaneHeight(Lane *lane, const double s, const double inner, const double outer)
Lane * GetLane(const RoadId road_id, const LaneId lane_id, const double s)
void CreateLaneBorder(Lane *lane, const double s, const double a, const double b, const double c, const double d)
void CreateLaneRule(Lane *lane, const double s, const std::string value)
void CreateLaneSpeed(Lane *lane, const double s, const double max, const std::string unit)
void CreateRoadMarkTypeLine(Lane *lane, const int road_mark_id, const double length, const double space, const double tOffset, const double s, const std::string rule, const double width)
void CreateLaneWidth(Lane *lane, const double s, const double a, const double b, const double c, const double d)
void CreateLaneVisibility(Lane *lane, const double s, const double forward, const double back, const double left, const double right)
void CreateLaneMaterial(Lane *lane, const double s, const std::string surface, const double friction, const double roughness)
double as_double(double def=0) const
Definition pugixml.cpp:5178
unsigned int as_uint(unsigned int def=0) const
Definition pugixml.cpp:5173
const char_t * value() const
Definition pugixml.cpp:5215
xml_node child(const char_t *name) const
Definition pugixml.cpp:5490
xml_object_range< xml_node_iterator > children() const
Definition pugixml.cpp:5425
xml_attribute attribute(const char_t *name) const
Definition pugixml.cpp:5500
static void ParseLanes(road::RoadId road_id, double s, const pugi::xml_node &parent_node, carla::road::MapBuilder &map_builder)
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.