CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
OpenDrive
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
7
#include "
Carla/OpenDrive/OpenDriveMap.h
"
8
#include "
Carla.h
"
9
10
#include <
util/disable-ue4-macros.h
>
11
#include <
carla/opendrive/OpenDriveParser.h
>
12
#include <
carla/rpc/String.h
>
13
#include <
util/enable-ue4-macros.h
>
14
15
16
namespace
UOpenDriveMap_Private
{
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
53
UOpenDriveMap::UOpenDriveMap
(
const
FObjectInitializer &ObjectInitializer)
54
: Super(ObjectInitializer) {}
55
56
bool
UOpenDriveMap::Load
(
const
FString &XODRContent)
57
{
58
auto
ResultMap =
carla::opendrive::OpenDriveParser::Load
(
59
carla::rpc::FromLongFString(XODRContent));
60
if
(ResultMap)
61
{
62
Map
= std::move(*ResultMap);
63
}
64
return
HasMap
();
65
}
66
67
FWaypoint
UOpenDriveMap::GetClosestWaypointOnRoad
(FVector
Location
,
bool
&Success)
const
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
75
TArray<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
87
TArray<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
96
TArray<FWaypoint>
UOpenDriveMap::GenerateWaypointsOnRoadEntries
()
const
97
{
98
check(
HasMap
());
99
using namespace
UOpenDriveMap_Private
;
100
return
TransformToTArray<FWaypoint>
(
Map
->GenerateWaypointsOnRoadEntries());
101
}
102
103
FVector
UOpenDriveMap::ComputeLocation
(
FWaypoint
Waypoint)
const
104
{
105
return
ComputeTransform
(Waypoint).GetLocation();
106
}
107
108
TArray<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
116
FTransform
UOpenDriveMap::ComputeTransform
(
FWaypoint
Waypoint)
const
117
{
118
check(
HasMap
());
119
using namespace
UOpenDriveMap_Private
;
120
return
Map
->ComputeTransform(Waypoint.
Waypoint
);
121
}
122
123
TArray<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
131
TArray<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
}
ECarlaServerResponse::Success
@ Success
Definition
CarlaServerResponse.h:11
Carla.h
OpenDriveMap.h
OpenDriveParser.h
UOpenDriveMap::Map
TOptional< carla::road::Map > Map
Definition
OpenDriveMap.h:97
UOpenDriveMap::GenerateTopology
TArray< FWaypointConnection > GenerateTopology() const
Generate the minimum set of waypoints that define the topology of this map.
Definition
OpenDriveMap.cpp:87
UOpenDriveMap::Load
bool Load(const FString &XODRContent)
Load this map with an OpenDrive (XODR) file.
Definition
OpenDriveMap.cpp:56
UOpenDriveMap::GenerateWaypointsOnRoadEntries
TArray< FWaypoint > GenerateWaypointsOnRoadEntries() const
Generate waypoints on each lane at the start of each road.
Definition
OpenDriveMap.cpp:96
UOpenDriveMap::HasMap
bool HasMap() const
Return whether this map has been initialized.
Definition
OpenDriveMap.h:47
UOpenDriveMap::ComputeLocations
TArray< FVector > ComputeLocations(const TArray< FWaypoint > &Waypoints) const
Compute the locations of an array of waypoints.
Definition
OpenDriveMap.cpp:108
UOpenDriveMap::ComputeTransforms
TArray< FTransform > ComputeTransforms(const TArray< FWaypoint > &Waypoints) const
Compute the transforms of an array of waypoints.
Definition
OpenDriveMap.cpp:123
UOpenDriveMap::ComputeLocation
FVector ComputeLocation(FWaypoint Waypoint) const
Compute the location of a waypoint.
Definition
OpenDriveMap.cpp:103
UOpenDriveMap::ComputeTransform
FTransform ComputeTransform(FWaypoint Waypoint) const
Compute the transform of a waypoint.
Definition
OpenDriveMap.cpp:116
UOpenDriveMap::GetNext
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.
Definition
OpenDriveMap.cpp:131
UOpenDriveMap::GetClosestWaypointOnRoad
FWaypoint GetClosestWaypointOnRoad(FVector Location, bool &Success) const
Given a location, return the closest point on the centre of a lane.
Definition
OpenDriveMap.cpp:67
UOpenDriveMap::GenerateWaypoints
TArray< FWaypoint > GenerateWaypoints(float ApproxDistance=100.0f) const
Generate waypoints all over the map at an approximated distance.
Definition
OpenDriveMap.cpp:75
UOpenDriveMap::UOpenDriveMap
UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
Definition
OpenDriveMap.cpp:53
carla::opendrive::OpenDriveParser::Load
static std::optional< road::Map > Load(const std::string &opendrive)
Definition
OpenDriveParser.cpp:27
disable-ue4-macros.h
enable-ue4-macros.h
UOpenDriveMap_Private
Definition
OpenDriveMap.cpp:16
UOpenDriveMap_Private::GetSize
static auto GetSize(const RangeT &Range)
Definition
OpenDriveMap.cpp:19
UOpenDriveMap_Private::TransformToTArray
static TArray< T > TransformToTArray(RangeT &&Range, FuncT &&TransformFunction)
Definition
OpenDriveMap.cpp:32
String.h
FWaypoint
Definition
OpenDriveMap.h:17
FWaypoint::Waypoint
carla::road::element::Waypoint Waypoint
Definition
OpenDriveMap.h:20
Location
Definition
geom/Location.h:23
Generated by
1.18.0