CARLA
 
载入中...
搜索中...
未找到
OpenDriveMap.cpp
浏览该文件的文档.
1// Copyright (c) 2019 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.h"
9
12#include <carla/rpc/String.h>
14
15// 定义一个私有命名空间UOpenDriveMap_Private,用于存放模板函数
17
18 // 模板函数,用于获取任意范围类型的尺寸
19 template <typename RangeT>
20 static auto GetSize(const RangeT &Range)
21 {
22 return Range.size();
23 }
24
25 // 模板函数,专门用于获取TArray的尺寸
26 template <typename T>
27 static auto GetSize(const TArray<T> &Array)
28 {
29 return Array.Num();
30 }
31
32
33 // 模板函数,用于将任意范围转换为TArray,应用给定的转换函数
34 template <typename T, typename RangeT, typename FuncT>
35 static TArray<T> TransformToTArray(RangeT &&Range, FuncT &&TransformFunction)
36 {
37 TArray<T> Result;
38 Result.Reserve(GetSize(Range));
39 for (auto &&Item : Range)
40 {
41 Result.Emplace(TransformFunction(Item));
42 }
43 return Result;
44 }
45
46 // 模板函数,用于将任意范围转换为TArray,使用默认的转换函数
47 template <typename T, typename RangeT>
48 static TArray<T> TransformToTArray(RangeT &&Range)
49 {
50 return TransformToTArray<T>(
51 std::forward<RangeT>(Range),
52 [](auto &&Item) { return T{Item}; });
53 }
54
55} // 以UOpenDriveMap_Private命名的命名空间,命名空间作用是防止同名函数的冲突,并且使程序员更好的区分同名函数。
56
57// UOpenDriveMap类的构造函数
58UOpenDriveMap::UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
59 : Super(ObjectInitializer) {}
60
61// UOpenDriveMap类的成员函数,用于加载OpenDrive内容
62bool UOpenDriveMap::Load(const FString &XODRContent)
63{
64 // 使用carla的OpenDriveParser加载OpenDrive内容
66 carla::rpc::FromLongFString(XODRContent));
67 if (ResultMap)
68 {
69 // 如果加载成功,移动结果到Map成员变量
70 Map = std::move(*ResultMap);
71 }
72 // 返回是否有地图数据
73 return HasMap();
74}
75
76// UOpenDriveMap类的成员函数,用于获取道路上最近的路点
78{
79 // 检查是否有地图数据
80 check(HasMap());
81 // 获取最近的路点
82 auto Result = Map->GetClosestWaypointOnRoad(Location);
83 // 设置是否成功的标记
84 Success = Result.has_value();
85 // 返回路点,如果没有找到则返回空的FWaypoint
86 return Result.has_value() ? FWaypoint{*Result} : FWaypoint{};
87}
88
89// UOpenDriveMap类的成员函数,用于生成路点数组
90TArray<FWaypoint> UOpenDriveMap::GenerateWaypoints(float ApproxDistance) const
91{
92 // 如果ApproxDistance小于1.0f,记录错误日志并返回空数组
93 if (ApproxDistance < 1.0f)
94 {
95 UE_LOG(LogCarla, Error, TEXT("GenerateWaypoints: Please provide an ApproxDistance greater than 1 centimetre."));
96 return {};
97 }
98 // 检查是否有地图数据
99 check(HasMap());
100 // 使用私有命名空间中的模板函数生成路点数组
101 using namespace UOpenDriveMap_Private;
102 return TransformToTArray<FWaypoint>(Map->GenerateWaypoints(ApproxDistance / 1e2f));
103}
104
105/// 根据OpenDrive生成地图的拓扑
106TArray<FWaypointConnection> UOpenDriveMap::GenerateTopology() const
107{
108 check(HasMap());
109 // 使用私有命名空间
110 using namespace UOpenDriveMap_Private;
111 // 将Map的拓扑信息转换为FWaypointConnection数组
112 return TransformToTArray<FWaypointConnection>(Map->GenerateTopology(), [](auto &&Item) {
113 return FWaypointConnection{FWaypoint{Item.first}, FWaypoint{Item.second}};
114 });
115}
116
117/// 基于道路的入口生成航点
119{
120 check(HasMap());
121 using namespace UOpenDriveMap_Private;
122 // 将Map的道路入口信息转换为FWaypoint数组
123 return TransformToTArray<FWaypoint>(Map->GenerateWaypointsOnRoadEntries());
124}
125
126/// 计算航点的位置。
128{
129 // 返回航点变换的位置
130 return ComputeTransform(Waypoint).GetLocation();
131}
132
133/// 计算航点数组的位置。
134TArray<FVector> UOpenDriveMap::ComputeLocations(const TArray<FWaypoint> &Waypoints) const
135{
136 using namespace UOpenDriveMap_Private;
137 // 将航点数组转换为位置数组
138 return TransformToTArray<FVector>(Waypoints, [this](auto &&Waypoint) {
139 return ComputeLocation(Waypoint);
140 });
141}
142
143/// 计算航点的变换。x轴指向该航点的道路方向。
145{
146 check(HasMap());
147 using namespace UOpenDriveMap_Private;
148 // 计算航点的变换
149 return Map->ComputeTransform(Waypoint.Waypoint);
150}
151
152/// 计算航点数组的变换。
153TArray<FTransform> UOpenDriveMap::ComputeTransforms(const TArray<FWaypoint> &Waypoints) const
154{
155 using namespace UOpenDriveMap_Private;
156 return TransformToTArray<FTransform>(Waypoints, [this](auto &&Waypoint) {
157 return ComputeTransform(Waypoint);
158 });
159}
160
161/// 返回给定距离上的航点列表,以便位于航点的车辆可以行驶得到。
162TArray<FWaypoint> UOpenDriveMap::GetNext(FWaypoint Waypoint, float Distance) const
163{
164 // 距离必须大于1厘米
165 if (Distance < 1.0f)
166 {
167 UE_LOG(LogCarla, Error, TEXT("GetNext: Please provide a Distance greater than 1 centimetre."));
168 return {};
169 }
170 check(HasMap());
171 using namespace UOpenDriveMap_Private;
172 // 获取给定距离上的航点列表
173 return TransformToTArray<FWaypoint>(Map->GetNext(Waypoint.Waypoint, Distance / 1e2f));
174}
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
地图类的前向声明,用于在LaneInvasionSensor类中可能的引用。
TArray< FWaypointConnection > GenerateTopology() const
生成定义此拓扑的最小航路点集地图。航点位于每条车道的入口处。
bool Load(const FString &XODRContent)
使用OpenDrive(XODR)文件加载此映射。
TArray< FWaypoint > GenerateWaypointsOnRoadEntries() const
在每条道路的起点,在每条车道上生成航点。
bool HasMap() const
返回此映射是否已初始化。
TArray< FVector > ComputeLocations(const TArray< FWaypoint > &Waypoints) const
计算航点数组的位置。
TArray< FTransform > ComputeTransforms(const TArray< FWaypoint > &Waypoints) const
计算航点数组的变换。
FVector ComputeLocation(FWaypoint Waypoint) const
计算航点的位置。
FTransform ComputeTransform(FWaypoint Waypoint) const
计算航点的变换。x轴指向该航点的道路方向。
TArray< FWaypoint > GetNext(FWaypoint Waypoint, float Distance=100.0f) const
返回给定距离上的航点列表,以便位于航点的车辆可以行驶得到。
FWaypoint GetClosestWaypointOnRoad(FVector Location, bool &Success) const
给定一个位置,返回车道中心最近的点。
TArray< FWaypoint > GenerateWaypoints(float ApproxDistance=100.0f) const
在地图上以近似距离生成航路点。
UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
static boost::optional< road::Map > Load(const std::string &opendrive)
static auto GetSize(const RangeT &Range)
static TArray< T > TransformToTArray(RangeT &&Range, FuncT &&TransformFunction)
carla::road::element::Waypoint Waypoint