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