CARLA
OpenDriveMap.cpp
Go to the documentation of this file.
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 
52 UOpenDriveMap::UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
53  : Super(ObjectInitializer) {}
54 
55 bool 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 
74 TArray<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 
86 TArray<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 
107 TArray<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 
115 FTransform UOpenDriveMap::ComputeTransform(FWaypoint Waypoint) const
116 {
117  check(HasMap());
118  using namespace UOpenDriveMap_Private;
119  return Map->ComputeTransform(Waypoint.Waypoint);
120 }
121 
122 TArray<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 
130 TArray<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 }
FTransform ComputeTransform(FWaypoint Waypoint) const
Compute the transform of a waypoint.
FWaypoint GetClosestWaypointOnRoad(FVector Location, bool &Success) const
Given a location, return the closest point on the centre of a lane.
TArray< FWaypoint > GenerateWaypointsOnRoadEntries() const
Generate waypoints on each lane at the start of each road.
TOptional< carla::road::Map > Map
Definition: OpenDriveMap.h:97
TArray< FWaypointConnection > GenerateTopology() const
Generate the minimum set of waypoints that define the topology of this map.
carla::road::element::Waypoint Waypoint
Definition: OpenDriveMap.h:20
static boost::optional< road::Map > Load(const std::string &opendrive)
bool HasMap() const
Return whether this map has been initialized.
Definition: OpenDriveMap.h:47
geom::Location Location
Definition: rpc/Location.h:14
static TArray< T > TransformToTArray(RangeT &&Range, FuncT &&TransformFunction)
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...
FVector ComputeLocation(FWaypoint Waypoint) const
Compute the location of a waypoint.
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.
UOpenDriveMap(const FObjectInitializer &ObjectInitializer)
bool Load(const FString &XODRContent)
Load this map with an OpenDrive (XODR) file.
TArray< FWaypoint > GenerateWaypoints(float ApproxDistance=100.0f) const
Generate waypoints all over the map at an approximated distance.
static auto GetSize(const RangeT &Range)