CARLA
Loading...
Searching...
No Matches
OpenDriveMap.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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
8#include "Carla.h"
9
12#include <carla/rpc/String.h>
14
15
17
18 template <typename RangeT>
19 static auto GetSize(const RangeT &Range)
20 {
21 return Range.size();
22 }
23
24 template <typename T>
25 static auto GetSize(const TArray<T> &Array)
26 {
27 return Array.Num();
28 }
29
30
31 template <typename T, typename RangeT, typename FuncT>
32 static TArray<T> TransformToTArray(RangeT &&Range, FuncT &&TransformFunction)
33 {
34 TArray<T> Result;
35 Result.Reserve(GetSize(Range));
36 for (auto &&Item : Range)
37 {
38 Result.Emplace(TransformFunction(Item));
39 }
40 return Result;
41 }
42
43 template <typename T, typename RangeT>
44 static TArray<T> TransformToTArray(RangeT &&Range)
45 {
46 return TransformToTArray<T>(
47 std::forward<RangeT>(Range),
48 [](auto &&Item) { return T{Item}; });
49 }
50
51} // namespace UOpenDriveMap_Private
52
53UOpenDriveMap::UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
54 : Super(ObjectInitializer) {}
55
56bool UOpenDriveMap::Load(const FString &XODRContent)
57{
59 carla::rpc::FromLongFString(XODRContent));
60 if (ResultMap)
61 {
62 Map = std::move(*ResultMap);
63 }
64 return HasMap();
65}
66
68{
69 check(HasMap());
70 auto Result = Map->GetClosestWaypointOnRoad(Location);
71 Success = Result.has_value();
72 return Result.has_value() ? FWaypoint{*Result} : FWaypoint{};
73}
74
75TArray<FWaypoint> UOpenDriveMap::GenerateWaypoints(float ApproxDistance) const
76{
77 if (ApproxDistance < 1.0f)
78 {
79 UE_LOG(LogCarla, Error, TEXT("GenerateWaypoints: Please provide an ApproxDistance greater than 1 centimetre."));
80 return {};
81 }
82 check(HasMap());
83 using namespace UOpenDriveMap_Private;
84 return TransformToTArray<FWaypoint>(Map->GenerateWaypoints(ApproxDistance / 1e2f));
85}
86
87TArray<FWaypointConnection> UOpenDriveMap::GenerateTopology() const
88{
89 check(HasMap());
90 using namespace UOpenDriveMap_Private;
91 return TransformToTArray<FWaypointConnection>(Map->GenerateTopology(), [](auto &&Item) {
92 return FWaypointConnection{FWaypoint{Item.first}, FWaypoint{Item.second}};
93 });
94}
95
97{
98 check(HasMap());
99 using namespace UOpenDriveMap_Private;
100 return TransformToTArray<FWaypoint>(Map->GenerateWaypointsOnRoadEntries());
101}
102
104{
105 return ComputeTransform(Waypoint).GetLocation();
106}
107
108TArray<FVector> UOpenDriveMap::ComputeLocations(const TArray<FWaypoint> &Waypoints) const
109{
110 using namespace UOpenDriveMap_Private;
111 return TransformToTArray<FVector>(Waypoints, [this](auto &&Waypoint) {
112 return ComputeLocation(Waypoint);
113 });
114}
115
117{
118 check(HasMap());
119 using namespace UOpenDriveMap_Private;
120 return Map->ComputeTransform(Waypoint.Waypoint);
121}
122
123TArray<FTransform> UOpenDriveMap::ComputeTransforms(const TArray<FWaypoint> &Waypoints) const
124{
125 using namespace UOpenDriveMap_Private;
126 return TransformToTArray<FTransform>(Waypoints, [this](auto &&Waypoint) {
127 return ComputeTransform(Waypoint);
128 });
129}
130
131TArray<FWaypoint> UOpenDriveMap::GetNext(FWaypoint Waypoint, float Distance) const
132{
133 if (Distance < 1.0f)
134 {
135 UE_LOG(LogCarla, Error, TEXT("GetNext: Please provide a Distance greater than 1 centimetre."));
136 return {};
137 }
138 check(HasMap());
139 using namespace UOpenDriveMap_Private;
140 return TransformToTArray<FWaypoint>(Map->GetNext(Waypoint.Waypoint, Distance / 1e2f));
141}
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 std::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