CARLA
Loading...
Searching...
No Matches
RoadMap.h
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#pragma once
8
10
11#include <util/ue-header-guard-begin.h>
12#include "UObject/NoExportTypes.h"
13#include <util/ue-header-guard-end.h>
14
15#include "RoadMap.generated.h"
16
17/// Road map intersection result. See URoadMap.
18USTRUCT(BlueprintType)
20{
21 GENERATED_BODY()
22
23 /// Percentage of the box lying off-road.
24 UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
25 float OffRoad = 0.0F;
26
27 /// Percentage of the box invading opposite lane (wrong direction).
28 UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
29 float OppositeLane = 0.0F;
30};
31
32/// Data stored in a road map pixel. See URoadMap.
34{
35 friend class URoadMap;
36
37 constexpr static int IsRoadRow = 15;
38
39 constexpr static int HasDirectionRow = 14;
40
41 constexpr static uint16 MaximumEncodedAngle = (1 << 14) - 1;
42
43 constexpr static uint16 AngleMask = (0xFFFF >> 2);
44
45public:
46
47 explicit FRoadMapPixelData(uint16 inValue) : Value(inValue) {}
48
49 /// Whether this pixel lies in-road.
50 bool IsRoad() const
51 {
52 return (Value & (1 << IsRoadRow)) != 0;
53 }
54
55 /// Whether this pixel has a direction defined (e.g. road intersections are
56 /// not off-road but neither have defined direction).
57 bool HasDirection() const
58 {
59 return (Value & (1 << HasDirectionRow)) != 0;
60 }
61
62 /// Get the azimuth angle [-PI, PI] of the road direction (in spherical
63 /// coordinates) at this pixel.
64 ///
65 /// Undefined if !HasDirection().
67 {
68 const float Angle = AngleMask & Value;
69 // Internally the angle is rotated by PI.
70 return (Angle * 2.0f * PI / MaximumEncodedAngle) - PI;
71 }
72
73 /// Get the road direction at this pixel.
74 ///
75 /// Undefined if !HasDirection().
76 FVector GetDirection() const
77 {
78 const FVector2D SphericalCoords(HALF_PI, GetDirectionAzimuthalAngle());
79 return SphericalCoords.SphericalToUnitCartesian();
80 }
81
82 FColor EncodeAsColor() const;
83
84private:
85
86 static uint16 Encode(bool IsRoad, bool HasDirection, const FVector &Direction);
87
88 uint16 Value;
89};
90
91/// Road map of the level. Contains information in 2D of which areas are road
92/// and lane directions.
93UCLASS()
94class CARLA_API URoadMap : public UObject
95{
96 GENERATED_BODY()
97
98public:
99
100 /// Creates a valid empty map (every point is off-road).
101 URoadMap(const FObjectInitializer& ObjectInitializer);
102
103 /// Resets current map an initializes an empty map of the given size.
104 void Reset(
105 uint32 Width,
106 uint32 Height,
108 const FTransform &WorldToMap,
109 const FVector &MapOffset);
110
111 void SetPixelAt(
112 uint32 PixelX,
113 uint32 PixelY,
114 ECityMapMeshTag Tag,
115 const FTransform &Transform,
116 bool bInvertDirection = false);
117
118 uint32 GetWidth() const
119 {
120 return Width;
121 }
122
123 uint32 GetHeight() const
124 {
125 return Height;
126 }
127
128 /// Return the world location of a given pixel.
129 FVector GetWorldLocation(uint32 PixelX, uint32 PixelY) const;
130
131 /// Retrieve the data stored at a given pixel.
132 FRoadMapPixelData GetDataAt(uint32 PixelX, uint32 PixelY) const
133 {
134 check(IsValid());
135 return FRoadMapPixelData(RoadMapData[GetIndex(PixelX, PixelY)]);
136 }
137
138 /// Clamps value if lies outside map limits.
139 FRoadMapPixelData GetDataAt(const FVector &WorldLocation) const;
140
141 /// Intersect actor bounds with map.
142 ///
143 /// Bounds box is projected to the map and checked against it for possible
144 /// intersections with off-road areas and opposite lanes.
146 const FTransform &BoxTransform,
147 const FVector &BoxExtent,
148 float ChecksPerCentimeter) const;
149
150 /// Save the current map as PNG with the pixel data encoded as color.
151 bool SaveAsPNG(const FString &Folder, const FString &MapName) const;
152
153#if WITH_EDITOR
154
155 /// Log status of the map to the console.
156 void Log() const;
157
158 /// Draw every pixel of the image as debug point.
159 void DrawDebugPixelsToLevel(UWorld *World, bool bJustFlushDoNotDraw = false) const;
160
161#endif // WITH_EDITOR
162
163private:
164
165 int32 GetIndex(uint32 PixelX, uint32 PixelY) const
166 {
167 return PixelX + Width * PixelY;
168 }
169
170 bool IsValid() const
171 {
172 return ((RoadMapData.Num() > 0) && (RoadMapData.Num() == Height * Width));
173 }
174
175 /// World-to-map transform.
176 UPROPERTY(VisibleAnywhere)
177 FTransform WorldToMap;
178
179 /// Offset of the map in map coordinates.
180 UPROPERTY(VisibleAnywhere)
181 FVector MapOffset;
182
183 /// Number of pixels per centimeter.
184 UPROPERTY(VisibleAnywhere)
186
187 /// Width of the map in pixels.
188 UPROPERTY(VisibleAnywhere)
189 uint32 Width;
190
191 /// Height of the map in pixels.
192 UPROPERTY(VisibleAnywhere)
193 uint32 Height;
194
195 UPROPERTY()
196 TArray<uint16> RoadMapData;
197};
ECityMapMeshTag
Tag to identify the meshes used by the ProceduralMapGenerator.
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
Road map of the level.
Definition RoadMap.h:95
uint32 Height
Height of the map in pixels.
Definition RoadMap.h:193
FVector GetWorldLocation(uint32 PixelX, uint32 PixelY) const
Return the world location of a given pixel.
Definition RoadMap.cpp:201
int32 GetIndex(uint32 PixelX, uint32 PixelY) const
Definition RoadMap.h:165
void Reset(uint32 Width, uint32 Height, float PixelsPerCentimeter, const FTransform &WorldToMap, const FVector &MapOffset)
Resets current map an initializes an empty map of the given size.
Definition RoadMap.cpp:83
bool IsValid() const
Definition RoadMap.h:170
void SetPixelAt(uint32 PixelX, uint32 PixelY, ECityMapMeshTag Tag, const FTransform &Transform, bool bInvertDirection=false)
Definition RoadMap.cpp:98
float PixelsPerCentimeter
Number of pixels per centimeter.
Definition RoadMap.h:185
TArray< uint16 > RoadMapData
Definition RoadMap.h:196
FVector MapOffset
Offset of the map in map coordinates.
Definition RoadMap.h:181
uint32 Width
Width of the map in pixels.
Definition RoadMap.h:189
FTransform WorldToMap
World-to-map transform.
Definition RoadMap.h:177
uint32 GetHeight() const
Definition RoadMap.h:123
bool SaveAsPNG(const FString &Folder, const FString &MapName) const
Save the current map as PNG with the pixel data encoded as color.
Definition RoadMap.cpp:251
uint32 GetWidth() const
Definition RoadMap.h:118
FRoadMapIntersectionResult Intersect(const FTransform &BoxTransform, const FVector &BoxExtent, float ChecksPerCentimeter) const
Intersect actor bounds with map.
Definition RoadMap.cpp:219
FRoadMapPixelData GetDataAt(uint32 PixelX, uint32 PixelY) const
Retrieve the data stored at a given pixel.
Definition RoadMap.h:132
Road map intersection result. See URoadMap.
Definition RoadMap.h:20
Data stored in a road map pixel. See URoadMap.
Definition RoadMap.h:34
float GetDirectionAzimuthalAngle() const
Get the azimuth angle [-PI, PI] of the road direction (in spherical coordinates) at this pixel.
Definition RoadMap.h:66
bool HasDirection() const
Whether this pixel has a direction defined (e.g.
Definition RoadMap.h:57
FVector GetDirection() const
Get the road direction at this pixel.
Definition RoadMap.h:76
bool IsRoad() const
Whether this pixel lies in-road.
Definition RoadMap.h:50
FRoadMapPixelData(uint16 inValue)
Definition RoadMap.h:47