CARLA
 
载入中...
搜索中...
未找到
LaneParser.cpp
浏览该文件的文档.
1// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// 车道解析器
5//
6// This work is licensed under the terms of the MIT license.
7// For a copy, see <https://opensource.org/licenses/MIT>.
8
10
12
13#include <pugixml/pugixml.hpp>
14
15namespace carla {
16namespace opendrive {
17namespace parser {
18
19 // 解析车道
20 static void ParseLanes(
21 road::RoadId road_id,
22 double s,
23 const pugi::xml_node &parent_node,
24 carla::road::MapBuilder &map_builder) {
25 for (pugi::xml_node lane_node : parent_node.children("lane")) {
26
27 road::LaneId lane_id = lane_node.attribute("id").as_int();
28
29 road::Lane *lane = map_builder.GetLane(road_id, lane_id, s);
30
31 // 车道宽度
32 int width_count = 0;
33 for (pugi::xml_node lane_width_node : lane_node.children("width")) {
34 const double s_offset = lane_width_node.attribute("sOffset").as_double();
35 const double a = lane_width_node.attribute("a").as_double();
36 const double b = lane_width_node.attribute("b").as_double();
37 const double c = lane_width_node.attribute("c").as_double();
38 const double d = lane_width_node.attribute("d").as_double();
39
40 // 调用地图构建器创建车道宽度函数
41 map_builder.CreateLaneWidth(lane, s_offset + s, a, b, c, d);
42 width_count++;
43 }
44 if (width_count == 0 && lane->GetId() != 0) {
45 map_builder.CreateLaneWidth(lane, s, 0.0, 0.0, 0.0, 0.0);
46 std::cout << "WARNING: In road " << lane->GetRoad()->GetId() << " lane " << lane->GetId() <<
47 " no \"<width>\" parameter found under \"<lane>\" tag. Using default values." << std::endl;
48 }
49
50 // 车道边界
51 for (pugi::xml_node lane_border_node : lane_node.children("border")) {
52 const double s_offset = lane_border_node.attribute("sOffset").as_double();
53 const double a = lane_border_node.attribute("a").as_double();
54 const double b = lane_border_node.attribute("b").as_double();
55 const double c = lane_border_node.attribute("c").as_double();
56 const double d = lane_border_node.attribute("d").as_double();
57
58 // 调用地图构建器创建车道边界函数
59 map_builder.CreateLaneBorder(lane, s_offset + s, a, b, c, d);
60 }
61
62 // Lane Road Mark
63 // 在OpenDrive中,<roadMark>元素定义了车道外边界的车道线样式,即左边车道就定义其左边界,右边车道就定义其右边界。
64 int road_mark_id = 0;
65 for (pugi::xml_node lane_road_mark : lane_node.children("roadMark")) {
66 pugi::xml_node road_mark_type;
67 {
68 const double s_offset = lane_road_mark.attribute("sOffset").as_double();
69 const std::string type = lane_road_mark.attribute("type").value();
70 const std::string weight = lane_road_mark.attribute("weight").value();
71 const std::string color = lane_road_mark.attribute("color").value();
72 const std::string material = lane_road_mark.attribute("material").value();
73 const double width = lane_road_mark.attribute("width").as_double();
74 const std::string lane_change = lane_road_mark.attribute("laneChange").value();
75 const double height = lane_road_mark.attribute("height").as_double();
76
77 // 为 LaneRoadMarkType 调用地图构建器
78
79 std::string type_name = "";
80 double type_width = 0.0;
81 road_mark_type = lane_road_mark.child("type");
82 if (road_mark_type) {
83 type_name = road_mark_type.attribute("name").value();
84 type_width = road_mark_type.attribute("width").as_double();
85 }
86
87 // 为 LaneRoadMark 调用地图构建器
88 map_builder.CreateRoadMark(
89 lane,
90 road_mark_id,
91 s_offset + s,
92 type,
93 weight,
94 color,
95 material,
96 width,
97 lane_change,
98 height,
99 type_name,
100 type_width);
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 // 为 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 // 车道材质
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 // 为车道材质 Lane Material 创建地图构建器
135 map_builder.CreateLaneMaterial(lane, s_offset + s, surface, friction, roughness);
136 }
137
138 // 车道可见性
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 // 为车道可见性 Lane Visibility 创建地图构建器
147 map_builder.CreateLaneVisibility(lane, s_offset + s, forward, back, left, right);
148 }
149
150 // Lane Speed
151 // 单独车道可以拥有不同于所属道路的速度限制,其将被定义为<laneSpeed>。
152 for (pugi::xml_node lane_speed_node : lane_node.children("speed")) {
153 const double s_offset = lane_speed_node.attribute("sOffset").as_double();
154 const double max = lane_speed_node.attribute("max").as_double();
155 std::string unit = lane_speed_node.attribute("unit").value();
156
157 // 为车道速度限制 Lane Speed 创建地图构建器
158 map_builder.CreateLaneSpeed(lane, s_offset + s, max, unit);
159 }
160
161 // Lane Access
162 // 在 <lane> 元素内提供了 <access> 元素,以便描述车道使用规则
163 for (pugi::xml_node lane_access_node : lane_node.children("access")) {
164 const double s_offset = lane_access_node.attribute("sOffset").as_double();
165 const std::string restriction = lane_access_node.attribute("restriction").value();
166
167 // 为车道使用 Lane Access 创建地图构建器
168 map_builder.CreateLaneAccess(lane, s_offset + s, restriction);
169 }
170
171 // Lane Height
172 for (pugi::xml_node lane_height_node : lane_node.children("height")) {
173 const double s_offset = lane_height_node.attribute("sOffset").as_double();
174 const double inner = lane_height_node.attribute("inner").as_double();
175 const double outer = lane_height_node.attribute("outer").as_double();
176
177 // Create map builder for Lane Height
178 map_builder.CreateLaneHeight(lane, s_offset + s, inner, outer);
179 }
180
181 // Lane Rule
182 for (pugi::xml_node lane_rule_node : lane_node.children("rule")) {
183 const double s_offset = lane_rule_node.attribute("sOffset").as_double();
184 const std::string value = lane_rule_node.attribute("value").value();
185
186 // Create map builder for Lane Height
187 map_builder.CreateLaneRule(lane, s_offset + s, value);
188 }
189
190 }
191 }
192
194 const pugi::xml_document &xml,
195 carla::road::MapBuilder &map_builder) {
196
197 pugi::xml_node open_drive_node = xml.child("OpenDRIVE"); // 获取OpenDRIVE根节点
198
199 // 车道
200 for (pugi::xml_node road_node : open_drive_node.children("road")) { // 遍历每个道路节点
201 road::RoadId road_id = road_node.attribute("id").as_uint(); // 获取道路ID
202
203 for (pugi::xml_node lanes_node : road_node.children("lanes")) { // 遍历每个车道节点
204
205 for (pugi::xml_node lane_section_node : lanes_node.children("laneSection")) { // 遍历每个车道段节点
206 double s = lane_section_node.attribute("s").as_double(); // 获取车道段的s属性,表示沿道路的距离
207 pugi::xml_node left_node = lane_section_node.child("left"); // 解析左侧车道
208 if (left_node) {
209 ParseLanes(road_id, s, left_node, map_builder);
210 }
211
212 pugi::xml_node center_node = lane_section_node.child("center"); // 解析中间车道
213 if (center_node) {
214 ParseLanes(road_id, s, center_node, map_builder);
215 }
216
217 pugi::xml_node right_node = lane_section_node.child("right"); // 解析右侧车道
218 if (right_node) {
219 ParseLanes(road_id, s, right_node, map_builder);
220 }
221 }
222 }
223 }
224 }
225
226} // namespace parser
227} // namespace opendrive
228} // namespace carla
static void Parse(const pugi::xml_document &xml, carla::road::MapBuilder &map_builder)
解析XML文档中的车道信息,并构建道路地图中的车道部分 该函数读取XML文档中的车道数据,如车道宽度、车道类型、车道方向等 并使用这些数据来构建或更新道路地图中的车道部分
Road * GetRoad() const
LaneId GetId() const
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 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)
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:5373
unsigned int as_uint(unsigned int def=0) const
Definition pugixml.cpp:5368
const char_t * value() const
Definition pugixml.cpp:5410
xml_node child(const char_t *name) const
Definition pugixml.cpp:5685
xml_object_range< xml_node_iterator > children() const
Definition pugixml.cpp:5620
xml_attribute attribute(const char_t *name) const
Definition pugixml.cpp:5695
static void ParseLanes(road::RoadId road_id, double s, const pugi::xml_node &parent_node, carla::road::MapBuilder &map_builder)
int32_t LaneId
Definition RoadTypes.h:26
uint32_t RoadId
Definition RoadTypes.h:20
CARLA模拟器的主命名空间。
Definition Carla.cpp:139