CARLA
 
载入中...
搜索中...
未找到
Road.cpp
浏览该文件的文档.
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#include "carla/Exception.h" // 引入异常处理头文件
8#include "carla/geom/CubicPolynomial.h" // 引入三次多项式几何头文件
9#include "carla/geom/Location.h" // 引入位置几何头文件
10#include "carla/geom/Math.h" // 引入数学运算头文件
11#include "carla/ListView.h" // 引入列表视图头文件
12#include "carla/Logging.h" // 引入日志记录头文件
13#include "carla/road/element/RoadInfoElevation.h" // 引入道路信息的高度头文件
14#include "carla/road/element/RoadInfoGeometry.h" // 引入道路几何信息头文件
15#include "carla/road/element/RoadInfoLaneOffset.h" // 引入车道偏移信息头文件
16#include "carla/road/element/RoadInfoLaneWidth.h" // 引入车道宽度信息头文件
17#include "carla/road/Lane.h" // 引入车道头文件
18#include "carla/road/MapData.h" // 引入地图数据头文件
19#include "carla/road/Road.h" // 引入道路头文件
20
21#include <stdexcept> // 引入标准异常处理头文件
22
23namespace carla {
24namespace road {
25
26 // 获取与该道路相关联的地图数据
27 const MapData *Road::GetMap() const {
28 return _map_data; // 返回地图数据指针
29 }
30
31 // 获取道路的ID
32 RoadId Road::GetId() const {
33 return _id; // 返回道路ID
34 }
35
36 // 获取道路名称
37 std::string Road::GetName() const {
38 return _name; // 返回道路名称
39 }
40
41 // 获取道路长度
42 double Road::GetLength() const {
43 return _length; // 返回道路长度
44 }
45
46 // 判断该道路是否为交叉口
47 bool Road::IsJunction() const {
48 return _is_junction; // 返回是否为交叉口
49 }
50
51 // 获取交叉口ID
53 return _junction_id; // 返回交叉口ID
54 }
55
56 // 获取下一条道路的ID
58 return _successor; // 返回下一条道路ID
59 }
60
61 // 获取前一条道路的ID
63 return _predecessor; // 返回前一条道路ID
64 }
65
66 // 获取下一条道路的指针列表
67 std::vector<Road *> Road::GetNexts() const {
68 return _nexts; // 返回下一条道路的指针列表
69 }
70
71 // 获取前一条道路的指针列表
72 std::vector<Road *> Road::GetPrevs() const {
73 return _prevs; // 返回前一条道路的指针列表
74 }
75
76 // 获取给定距离s处的道路高度多项式
77 const geom::CubicPolynomial &Road::GetElevationOn(const double s) const {
78 auto info = GetInfo<element::RoadInfoElevation>(s); // 获取道路高度信息
79 if (info == nullptr) { // 如果没有找到高度信息
80 throw_exception(std::runtime_error("failed to find road elevation.")); // 抛出异常
81 }
82 return info->GetPolynomial(); // 返回高度多项式
83 }
84
85 // 根据距离s和车道ID获取车道的引用
86 Lane &Road::GetLaneByDistance(double s, LaneId lane_id) {
87 for (auto &section : GetLaneSectionsAt(s)) { // 遍历在距离s处的车道段
88 auto *lane = section.GetLane(lane_id); // 获取指定ID的车道
89 if (lane != nullptr) { // 如果找到了车道
90 return *lane; // 返回车道引用
91 }
92 }
93 throw_exception(std::runtime_error("lane not found")); // 抛出异常,表示未找到车道
94 }
95
96 // 获取给定距离s和车道ID的常量车道引用
97 const Lane &Road::GetLaneByDistance(double s, LaneId lane_id) const {
98 return const_cast<Road *>(this)->GetLaneByDistance(s, lane_id); // 调用非常量版本
99 }
100
101 // 根据距离s获取所有车道的指针列表
102 std::vector<Lane*> Road::GetLanesByDistance(double s) {
103 std::vector<Lane*> result; // 创建结果列表
104 auto lane_sections = GetLaneSectionsAt(s); // 获取在距离s处的车道段
105 for (auto &lane_section : lane_sections) { // 遍历每个车道段
106 for (auto & lane_pair : lane_section.GetLanes()) { // 遍历车道段中的每个车道
107 result.emplace_back(&lane_pair.second); // 将车道指针添加到结果列表
108 }
109 }
110 return result; // 返回车道指针列表
111 }
112
113 // 根据距离s获取所有车道的常量指针列表
114 std::vector<const Lane*> Road::GetLanesByDistance(double s) const {
115 std::vector<const Lane*> result; // 创建结果列表
116 const auto lane_sections = GetLaneSectionsAt(s); // 获取在距离s处的车道段
117 for (const auto &lane_section : lane_sections) { // 遍历每个车道段
118 for (const auto & lane_pair : lane_section.GetLanes()) { // 遍历车道段中的每个车道
119 result.emplace_back(&lane_pair.second); // 将车道指针添加到结果列表
120 }
121 }
122 return result; // 返回车道指针列表
123 }
124
125 // 根据车道段ID和车道ID获取车道的引用
126 Lane &Road::GetLaneById(SectionId section_id, LaneId lane_id) {
127 return GetLaneSectionById(section_id).GetLanes().at(lane_id); // 获取指定车道段中的车道引用
128 }
129
130 // 根据车道段ID和车道ID获取车道的常量引用
131 const Lane &Road::GetLaneById(SectionId section_id, LaneId lane_id) const {
132 return const_cast<Road *>(this)->GetLaneById(section_id, lane_id); // 调用非常量版本
133 }
134
135 // 获取给定距离s处的下一个车道
136 Lane *Road::GetNextLane(const double s, const LaneId lane_id) {
137 auto upper = _lane_sections.upper_bound(s); // 获取距离s的上界车道段
138
139 while (upper != _lane_sections.end()) { // 当上界车道段不为空时
140 // 检查ID
141 Lane *ptr = upper->second.GetLane(lane_id); // 获取与 lane_id 相关联的车道指针
142if (ptr != nullptr) { // 如果车道指针不为空
143 return ptr; // 返回该车道指针
144}
145++upper; // 移动到下一个元素
146
147return nullptr; // 如果没有找到,返回空指针
148}
149
150// 获取在 's' 之前的车道
151Lane *Road::GetPrevLane(const double s, const LaneId lane_id) {
152
153 auto lower = _lane_sections.lower_bound(s); // 找到第一个大于或等于 s 的元素
154 auto rlower = std::make_reverse_iterator(lower); // 创建反向迭代器
155
156 while (rlower != _lane_sections.rend()) { // 遍历直到结束
157 Lane *ptr = rlower->second.GetLane(lane_id); // 获取与 lane_id 相关联的车道指针
158 if (ptr != nullptr) { // 如果车道指针不为空
159 return ptr; // 返回该车道指针
160 }
161 ++rlower; // 移动到前一个元素
162 }
163
164 return nullptr; // 如果没有找到,返回空指针
165}
166
167// 获取具有指定 lane id 的开始段
168LaneSection *Road::GetStartSection(LaneId id) {
169 auto it = _lane_sections.begin(); // 从车道段的开始位置迭代
170 while (it != _lane_sections.end()) { // 遍历直到结束
171 Lane *ptr = it->second.GetLane(id); // 获取与 id 相关联的车道指针
172 if (ptr != nullptr) { // 如果车道指针不为空
173 return &(it->second); // 返回该车道段的地址
174 }
175 ++it; // 移动到下一个元素
176 }
177 return nullptr; // 如果没有找到,返回空指针
178}
179
180// 获取具有指定 lane id 的结束段
181LaneSection *Road::GetEndSection(LaneId id) {
182 auto it = _lane_sections.rbegin(); // 从车道段的结束位置反向迭代
183 while (it != _lane_sections.rend()) { // 遍历直到结束
184 Lane *ptr = it->second.GetLane(id); // 获取与 id 相关联的车道指针
185 if (ptr != nullptr) { // 如果车道指针不为空
186 return &(it->second); // 返回该车道段的地址
187 }
188 ++it; // 移动到前一个元素
189 }
190 return nullptr; // 如果没有找到,返回空指针
191}
192
193// 根据给定的 s 获取方向点
194element::DirectedPoint Road::GetDirectedPointIn(const double s) const {
195 const auto clamped_s = geom::Math::Clamp(s, 0.0, _length); // 将 s 限制在有效范围内
196 const auto geometry = _info.GetInfo<element::RoadInfoGeometry>(clamped_s); // 获取几何信息
197
198 const auto lane_offset = _info.GetInfo<element::RoadInfoLaneOffset>(clamped_s); // 获取车道偏移信息
199 float offset = 0; // 初始化偏移量
200 if(lane_offset){ // 如果存在车道偏移信息
201 offset = static_cast<float>(lane_offset->GetPolynomial().Evaluate(clamped_s)); // 计算偏移量
202 }
203 // 应用道路的车道偏移记录
204 element::DirectedPoint p = geometry->GetGeometry().PosFromDist(clamped_s - geometry->GetDistance()); // 计算位置
205 // Unreal 的 Y 轴偏移(偏移量取负)
206 p.ApplyLateralOffset(-offset); // 应用横向偏移
207
208 // 应用道路的高程记录
209 const auto elevation_info = GetElevationOn(s); // 获取高程信息
210 p.location.z = static_cast<float>(elevation_info.Evaluate(s)); // 设置 z 坐标
211 p.pitch = elevation_info.Tangent(s); // 设置俯仰角
212
213 return p; // 返回方向点
214}
215
216// 根据给定的 s 获取方向点,不考虑车道偏移
217element::DirectedPoint Road::GetDirectedPointInNoLaneOffset(const double s) const {
218 const auto clamped_s = geom::Math::Clamp(s, 0.0, _length); // 将 s 限制在有效范围内
219 const auto geometry = _info.GetInfo<element::RoadInfoGeometry>(clamped_s); // 获取几何信息
220
221 element::DirectedPoint p = geometry->GetGeometry().PosFromDist(clamped_s - geometry->GetDistance()); // 计算位置
222
223 // 应用道路的高程记录
224 const auto elevation_info = GetElevationOn(s); // 获取高程信息
225 p.location.z = static_cast<float>(elevation_info.Evaluate(s)); // 设置 z 坐标
226 p.pitch = elevation_info.Tangent(s); // 设置俯仰角
227
228 return p; // 返回方向点
229}
230
231// 获取距离给定位置最近的点
232const std::pair<double, double> Road::GetNearestPoint(const geom::Location &loc) const {
233 std::pair<double, double> last = { 0.0, std::numeric_limits<double>::max() }; // 初始化最近点和距离
234
235 auto geom_info_list = _info.GetInfos<element::RoadInfoGeometry>(); // 获取几何信息列表
236 decltype(geom_info_list)::iterator nearest_geom = geom_info_list.end(); // 初始化最近的几何信息指针
237
238 for (auto g = geom_info_list.begin(); g != geom_info_list.end(); ++g) { // 遍历几何信息列表
239 DEBUG_ASSERT(*g != nullptr); // 确保指针不为空
240 auto dist = (*g)->GetGeometry().DistanceTo(loc); // 计算与位置的距离
241 if (dist.second < last.second) { // 如果当前距离小于最近距离
242 last = dist; // 更新最近距离
243 nearest_geom = g; // 更新最近的几何信息指针
244 }
245 }
246
247 for (auto g = geom_info_list.begin();
248 g != geom_info_list.end() && g != nearest_geom;
249 ++g) { // 遍历直到最近的几何信息
250 DEBUG_ASSERT(*g != nullptr); // 确保指针不为空
251 last.first += (*g)->GetGeometry().GetLength(); // 累加长度
252 }
253
254 return last; // 返回最近点和距离
255}
256 const std::pair<const Lane *, double> Road::GetNearestLane(
257 const double s,
258 const geom::Location &loc,
259 uint32_t lane_type) const {
260 using namespace carla::road::element; // 使用 carla::road::element 命名空间
261 std::map<LaneId, const Lane *> lanes(GetLanesAt(s)); // 获取在 s 位置的所有车道
262
263 // 获取右侧车道(负值车道)
264 auto right_lanes = MakeListView(
265 std::make_reverse_iterator(lanes.lower_bound(0)), lanes.rend()); // 创建右侧车道的视图
266 // 获取左侧车道(正值车道)
267 auto left_lanes = MakeListView(
268 lanes.lower_bound(1), lanes.end()); // 创建左侧车道的视图
269
270 const DirectedPoint dp_lane_zero = GetDirectedPointIn(s); // 获取在 s 处的方向点
271 std::pair<const Lane *, double> result =
272 std::make_pair(nullptr, std::numeric_limits<double>::max()); // 初始化结果为 null 和最大距离
273
274 DirectedPoint current_dp = dp_lane_zero; // 当前方向点初始化为 dp_lane_zero
275 for (const auto &lane : right_lanes) { // 遍历右侧车道
276 const auto lane_width_info = lane.second->GetInfo<RoadInfoLaneWidth>(s); // 获取车道宽度信息
277 const auto half_width = static_cast<float>(lane_width_info->GetPolynomial().Evaluate(s)) * 0.5f; // 计算半宽度
278
279 current_dp.ApplyLateralOffset(half_width); // 应用半宽度偏移到当前方向点
280 const auto current_dist = geom::Math::Distance(current_dp.location, loc); // 计算当前方向点到目标位置的距离
281
282 // 如果当前方向点接近目标位置,说明我们在正确的方向上
283 if (current_dist <= result.second) {
284 // 仅考虑与类型标志匹配的车道作为候选结果
285 if ((static_cast<uint32_t>(lane.second->GetType()) & lane_type) > 0) {
286 result.first = &(*lane.second); // 更新结果中的车道指针
287 result.second = current_dist; // 更新结果中的距离
288 }
289 } else {
290 // 如果距离变远,说明我们可能在偏离方向
291 break; // 退出循环
292 }
293 current_dp.ApplyLateralOffset(half_width); // 再次应用半宽度偏移
294 }
295
296 current_dp = dp_lane_zero; // 重置当前方向点为初始值
297 for (const auto &lane : left_lanes) { // 遍历左侧车道
298 const auto lane_width_info = lane.second->GetInfo<RoadInfoLaneWidth>(s); // 获取车道宽度信息
299 const auto half_width = -static_cast<float>(lane_width_info->GetPolynomial().Evaluate(s)) * 0.5f; // 计算负半宽度
300
301 current_dp.ApplyLateralOffset(half_width); // 应用负半宽度偏移到当前方向点
302 const auto current_dist = geom::Math::Distance(current_dp.location, loc); // 计算当前方向点到目标位置的距离
303
304 // 如果当前方向点接近目标位置,说明我们在正确的方向上
305 if (current_dist <= result.second) {
306 // 仅考虑与类型标志匹配的车道作为候选结果
307 if ((static_cast<uint32_t>(lane.second->GetType()) & lane_type) > 0) {
308 result.first = &(*lane.second); // 更新结果中的车道指针
309 result.second = current_dist; // 更新结果中的距离
310 }
311 } else {
312 // 如果距离变远,说明我们可能在偏离方向
313 break; // 退出循环
314 }
315 current_dp.ApplyLateralOffset(half_width); // 再次应用负半宽度偏移
316 }
317
318 return result; // 返回最近的车道和距离
319}
320
321std::map<LaneId, const Lane *> Road::GetLanesAt(const double s) const {
322 std::map<LaneId, const Lane *> map; // 初始化一个车道映射
323 for (auto &&lane_section : GetLaneSectionsAt(s)) { // 遍历在 s 位置的车道段
324 for (auto &&lane : lane_section.GetLanes()) { // 遍历车道段中的所有车道
325 map[lane.first] = &(lane.second); // 将车道 ID 和车道指针存入映射
326 }
327 }
328 return map; // 返回车道映射
329}
330
331} // road
332} // carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:68
static T Clamp(T a, T min=T(0), T max=T(1))
Definition Math.h:54
static auto Distance(const Vector3D &a, const Vector3D &b)
Definition Math.h:90
const T * GetInfo(const double s) const
返回给定类型和距离s的单一信息
std::vector< const T * > GetInfos() const
返回从道路起点给定类型的所有信息
std::map< LaneId, Lane > & GetLanes()
MapData * _map_data
Definition Road.h:183
RoadId GetPredecessor() const
double GetLength() const
const std::pair< double, double > GetNearestPoint(const geom::Location &loc) const
返回一个包含以下内容的对:
std::vector< Road * > GetPrevs() const
Lane * GetPrevLane(const double s, const LaneId lane_id)
double _length
Definition Road.h:189
JuncId _junction_id
Definition Road.h:193
Lane & GetLaneById(SectionId section_id, LaneId lane_id)
InformationSet _info
Definition Road.h:201
LaneSectionMap _lane_sections
Definition Road.h:195
RoadId GetSuccessor() const
std::vector< Road * > _nexts
Definition Road.h:203
std::vector< Road * > _prevs
Definition Road.h:205
RoadId GetId() const
std::map< LaneId, const Lane * > GetLanesAt(const double s) const
获取在给定 s 位置的所有车道
std::vector< Road * > GetNexts() const
LaneSection * GetStartSection(LaneId id)
获取给定车道 ID 的起始车道段
Lane * GetNextLane(const double s, const LaneId lane_id)
bool _is_junction
Definition Road.h:191
const geom::CubicPolynomial & GetElevationOn(const double s) const
Lane & GetLaneByDistance(double s, LaneId lane_id)
auto GetLaneSectionsAt(const double s)
Definition Road.h:144
const std::pair< const Lane *, double > GetNearestLane(const double s, const geom::Location &loc, uint32_t type=static_cast< uint32_t >(Lane::LaneType::Any)) const
返回最近的车道指针,给定相对于路段的 s 值和位置
element::DirectedPoint GetDirectedPointIn(const double s) const
返回指定距离的中心点(车道 0)的导向点
std::vector< Lane * > GetLanesByDistance(double s)
在特定的 s 值获取所有车道
RoadId _successor
Definition Road.h:197
element::DirectedPoint GetDirectedPointInNoLaneOffset(const double s) const
返回指定距离的中心点(车道 0)的导向点,不考虑车道偏移
JuncId GetJunctionId() const
LaneSection * GetEndSection(LaneId id)
获取给定车道 ID 的结束车道段
std::string GetName() const
std::string _name
Definition Road.h:187
RoadId _predecessor
Definition Road.h:199
const MapData * GetMap() const
bool IsJunction() const
LaneSection & GetLaneSectionById(SectionId id)
Definition Road.h:160
车道宽度记录:道路上每个交叉部分的车道可以提供多个宽度条目。 每个车道至少必须定义一个条目,除了按照惯例中心车道宽度为零。 每个条目在定义新条目之前都是有效的。如果为一个车道定义了多个条目, 它们必须按...
uint32_t SectionId
Definition RoadTypes.h:29
int32_t JuncId
Definition RoadTypes.h:23
int32_t LaneId
Definition RoadTypes.h:26
uint32_t RoadId
Definition RoadTypes.h:20
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
void throw_exception(const std::exception &e)
Definition Carla.cpp:142
static auto MakeListView(Iterator begin, Iterator end)
void ApplyLateralOffset(float lateral_offset)
Definition Geometry.cpp:45