CARLA
 
载入中...
搜索中...
未找到
LaneCrossingCalculator.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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/road/element/LaneCrossingCalculator.h" // 导入车道交叉计算器头文件
8#include "carla/road/element/LaneMarking.h" // 导入车道标记头文件
9
10#include "carla/geom/Location.h" // 导入位置相关头文件
11#include "carla/road/Map.h" // 导入地图相关头文件
12
13namespace carla {
14namespace road {
15namespace element {
16
17 /// @todo 临时标志,用于搜索可以找到道路标记的车道。
18 /// 这需要扩展以搜索肩道,但由于肩道宽度较小,
19 /// 在给定位置的最近车道中心查找时可能会导致问题。
20 static constexpr uint32_t FLAGS =
21 static_cast<uint32_t>(Lane::LaneType::Driving) | // 驾驶车道类型
22 static_cast<uint32_t>(Lane::LaneType::Bidirectional) | // 双向车道类型
23 static_cast<uint32_t>(Lane::LaneType::Biking) | // 自行车车道类型
24 static_cast<uint32_t>(Lane::LaneType::Parking); // 停车车道类型
25
26 /// 从 @a lane_id_origin 到 @a lane_id_destination 计算需要跨越的车道标记。
27 static std::vector<LaneMarking> CrossingAtSameSection(
28 const Map &map, // 地图引用
29 const Waypoint *w0, // 起始点航路点
30 const Waypoint *w1, // 目标点航路点
31 const bool w0_is_offroad, // 起始点是否在路外
32 const bool dest_is_at_right) { // 目标点是否在右侧
33 auto w0_marks = map.GetMarkRecord(*w0); // 获取起始点的标记记录
34 auto w1_marks = map.GetMarkRecord(*w1); // 获取目标点的标记记录
35
36 if (dest_is_at_right) { // 如果目标点在右侧
37 if (w0_is_offroad) { // 如果起始点在路外
38 return { LaneMarking(*w1_marks.second) }; // 返回目标点的第二个标记
39 } else {
40 return { LaneMarking(*w0_marks.first) }; // 返回起始点的第一个标记
41 }
42 } else { // 如果目标点在左侧
43 if (w0_is_offroad) { // 如果起始点在路外
44 return { LaneMarking(*w1_marks.first) }; // 返回目标点的第一个标记
45 } else {
46 return { LaneMarking(*w0_marks.second) }; // 返回起始点的第二个标记
47 }
48 }
49
50 return {}; // 默认返回空向量
51 }
52
53 static bool IsOffRoad(const Map &map, const geom::Location &location) {
54 return !map.GetWaypoint(location, FLAGS).has_value(); // 判断位置是否在路外
55 }
56
57 std::vector<LaneMarking> LaneCrossingCalculator::Calculate(
58 const Map &map, // 地图引用
59 const geom::Location &origin, // 起始位置
60 const geom::Location &destination) { // 目标位置
61 auto w0 = map.GetClosestWaypointOnRoad(origin, FLAGS); // 获取起始位置的最近航路点
62 auto w1 = map.GetClosestWaypointOnRoad(destination, FLAGS); // 获取目标位置的最近航路点
63
64 if (!w0.has_value() || !w1.has_value()) { // 如果任一航路点无效
65 return {}; // 返回空向量
66 }
67
68 if (w0->road_id != w1->road_id || w0->section_id != w1->section_id) { // 如果不在同一条道路或段落
69 /// @todo 这个情况也应该被处理。
70 return {}; // 返回空向量
71 }
72
73 if (map.IsJunction(w0->road_id) || map.IsJunction(w1->road_id)) { // 如果任一航路点在交叉口
74 return {}; // 返回空向量
75 }
76
77 const auto w0_is_offroad = IsOffRoad(map, origin); // 检查起始位置是否在路外
78 const auto w1_is_offroad = IsOffRoad(map, destination); // 检查目标位置是否在路外
79
80 if (w0_is_offroad && w1_is_offroad) { // 如果两者都在路外
81 // outside the road
82 return {}; // 返回空向量
83 }
84
85 if ((w0->lane_id == w1->lane_id) && !w0_is_offroad && !w1_is_offroad) { // 如果在同一车道且都在路内
86 // both at the same lane and inside the road
87 return {}; // 返回空向量
88 }
89
90 const auto transform = map.ComputeTransform(*w0); // 计算起始航路点的转换
91 geom::Vector3D orig_vec = transform.GetForwardVector(); // 获取起始点的前向向量
92 geom::Vector3D dest_vec = (destination - origin).MakeSafeUnitVector(2 * std::numeric_limits<float>::epsilon()); // 计算目标向量并标准化
93
94 // cross product
95 const auto dest_is_at_right =
96 (-orig_vec.x * dest_vec.y + orig_vec.y * dest_vec.x) < 0; // 判断目标点是否在右侧
97
98 return CrossingAtSameSection( // 返回同一段的交叉车道标记
99 map,
100 &*w0,
101 &*w1,
102 w0_is_offroad,
103 dest_is_at_right);
104 }
105
106} // namespace element
107} // namespace road
108} // namespace carla
bool IsJunction(RoadId road_id) const
boost::optional< element::Waypoint > GetClosestWaypointOnRoad(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
========================================================================
std::pair< const element::RoadInfoMarkRecord *, const element::RoadInfoMarkRecord * > GetMarkRecord(Waypoint waypoint) const
boost::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
geom::Transform ComputeTransform(Waypoint waypoint) const
static std::vector< LaneMarking > Calculate(const Map &map, const geom::Location &origin, const geom::Location &destination)
static constexpr uint32_t FLAGS
static std::vector< LaneMarking > CrossingAtSameSection(const Map &map, const Waypoint *w0, const Waypoint *w1, const bool w0_is_offroad, const bool dest_is_at_right)
从 lane_id_origin 到 lane_id_destination 计算需要跨越的车道标记。
static bool IsOffRoad(const Map &map, const geom::Location &location)
CARLA模拟器的主命名空间。
Definition Carla.cpp:139