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
16
17 template <typename RangeT>
18 static auto GetSize(const RangeT &Range)
19 {
20 return Range.size();
21 }
22
23 template <typename T>
24 static auto GetSize(const TArray<T> &Array)
25 {
26 return Array.Num();
27 }
28
29
30 template <typename T, typename RangeT, typename FuncT>
31 static TArray<T> TransformToTArray(RangeT &&Range, FuncT &&TransformFunction)
32 {
33 TArray<T> Result;
34 Result.Reserve(GetSize(Range));
35 for (auto &&Item : Range)
36 {
37 Result.Emplace(TransformFunction(Item));
38 }
39 return Result;
40 }
41
42 template <typename T, typename RangeT>
43 static TArray<T> TransformToTArray(RangeT &&Range)
44 {
45 return TransformToTArray<T>(
46 std::forward<RangeT>(Range),
47 [](auto &&Item) { return T{Item}; });
48 }
49
50} // namespace UOpenDriveMap_Private
51
52UOpenDriveMap::UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
53 : Super(ObjectInitializer) {}
54
55bool UOpenDriveMap::Load(const FString &XODRContent)
56{
58 carla::rpc::FromLongFString(XODRContent));
59 if (ResultMap)
60 {
61 Map = std::move(*ResultMap);
62 }
63 return HasMap();
64}
65
67{
68 check(HasMap());
69 auto Result = Map->GetClosestWaypointOnRoad(Location);
70 Success = Result.has_value();
71 return Result.has_value() ? FWaypoint{*Result} : FWaypoint{};
72}
73
74TArray<FWaypoint> UOpenDriveMap::GenerateWaypoints(float ApproxDistance) const
75{
76 if (ApproxDistance < 1.0f)
77 {
78 UE_LOG(LogCarla, Error, TEXT("GenerateWaypoints: Please provide an ApproxDistance greater than 1 centimetre."));
79 return {};
80 }
81 check(HasMap());
82 using namespace UOpenDriveMap_Private;
83 return TransformToTArray<FWaypoint>(Map->GenerateWaypoints(ApproxDistance / 1e2f));
84}
85
86TArray<FWaypointConnection> UOpenDriveMap::GenerateTopology() const
87{
88 check(HasMap());
89 using namespace UOpenDriveMap_Private;
90 return TransformToTArray<FWaypointConnection>(Map->GenerateTopology(), [](auto &&Item) {
91 return FWaypointConnection{FWaypoint{Item.first}, FWaypoint{Item.second}};
92 });
93}
94
96{
97 check(HasMap());
98 using namespace UOpenDriveMap_Private;
99 return TransformToTArray<FWaypoint>(Map->GenerateWaypointsOnRoadEntries());
100}
101
103{
104 return ComputeTransform(Waypoint).GetLocation();
105}
106
107TArray<FVector> UOpenDriveMap::ComputeLocations(const TArray<FWaypoint> &Waypoints) const
108{
109 using namespace UOpenDriveMap_Private;
110 return TransformToTArray<FVector>(Waypoints, [this](auto &&Waypoint) {
111 return ComputeLocation(Waypoint);
112 });
113}
114
116{
117 check(HasMap());
118 using namespace UOpenDriveMap_Private;
119 return Map->ComputeTransform(Waypoint.Waypoint);
120}
121
122TArray<FTransform> UOpenDriveMap::ComputeTransforms(const TArray<FWaypoint> &Waypoints) const
123{
124 using namespace UOpenDriveMap_Private;
125 return TransformToTArray<FTransform>(Waypoints, [this](auto &&Waypoint) {
126 return ComputeTransform(Waypoint);
127 });
128}
129
130TArray<FWaypoint> UOpenDriveMap::GetNext(FWaypoint Waypoint, float Distance) const
131{
132 if (Distance < 1.0f)
133 {
134 UE_LOG(LogCarla, Error, TEXT("GetNext: Please provide a Distance greater than 1 centimetre."));
135 return {};
136 }
137 check(HasMap());
138 using namespace UOpenDriveMap_Private;
139 return TransformToTArray<FWaypoint>(Map->GetNext(Waypoint.Waypoint, Distance / 1e2f));
140}
TOptional< carla::road::Map > Map
TArray< FWaypointConnection > GenerateTopology() const
Generate the minimum set of waypoints that define the topology of this map.
bool Load(const FString &XODRContent)
Load this map with an OpenDrive (XODR) file.
TArray< FWaypoint > GenerateWaypointsOnRoadEntries() const
Generate waypoints on each lane at the start of each road.
bool HasMap() const
Return whether this map has been initialized.
TArray< FVector > ComputeLocations(const TArray< FWaypoint > &Waypoints) const
Compute the locations of an array of waypoints.
TArray< FTransform > ComputeTransforms(const TArray< FWaypoint > &Waypoints) const
Compute the transforms of an array of waypoints.
FVector ComputeLocation(FWaypoint Waypoint) const
Compute the location of a waypoint.
FTransform ComputeTransform(FWaypoint Waypoint) const
Compute the transform of a waypoint.
TArray< FWaypoint > GetNext(FWaypoint Waypoint, float Distance=100.0f) const
Return the list of waypoints at a given distance such that a vehicle at waypoint could drive to.
FWaypoint GetClosestWaypointOnRoad(FVector Location, bool &Success) const
Given a location, return the closest point on the centre of a lane.
TArray< FWaypoint > GenerateWaypoints(float ApproxDistance=100.0f) const
Generate waypoints all over the map at an approximated distance.
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