CARLA
Loading...
Searching...
No Matches
LargeMapManager.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2// This work is licensed under the terms of the MIT license.
3// For a copy, see <https://opensource.org/licenses/MIT>.
4
5#pragma once
6
7#include "Actor/CarlaActor.h"
8#include "Math/DVector.h"
9
10#include <util/ue-header-guard-begin.h>
11#include "CoreMinimal.h"
12#include "GameFramework/Actor.h"
13#include "Engine/LevelStreamingDynamic.h"
14#include <util/ue-header-guard-end.h>
15
16#include "LargeMapManager.generated.h"
17
18// TODO: Cache CarlaEpisode
19
20USTRUCT()
22{
23 GENERATED_BODY()
24
26
28 const FCarlaActor* InCarlaActor,
29 const FTransform& InTransform)
30 : CarlaActor(InCarlaActor),
31 WorldLocation(FDVector(InTransform.GetTranslation())),
32 Rotation(InTransform.GetRotation()) {}
33
35
37
38 FQuat Rotation;
39};
40
41USTRUCT(BlueprintType)
43{
44 GENERATED_BODY()
45
46 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Carla Map Tile")
47 FString Name; // Tile_{TileID_X}_{TileID_Y}
48
49 // Absolute location, does not depend on rebasing
50 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Carla Map Tile")
51 FVector Location{0.0f};
52 // TODO: not FVector
53
54 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Carla Map Tile")
55 ULevelStreamingDynamic* StreamingLevel = nullptr;
56
57 bool TilesSpawned = false;
58};
59
60UCLASS()
61class CARLA_API ALargeMapManager : public AActor
62{
63 GENERATED_BODY()
64
65public:
66
67 using TileID = uint64;
68
69 // Sets default values for this actor's properties
71
73
74protected:
75 // Called when the game starts or when spawned
76 virtual void BeginPlay() override;
77
78 void PreWorldOriginOffset(UWorld* InWorld, FIntVector InSrcOrigin, FIntVector InDstOrigin);
79 void PostWorldOriginOffset(UWorld* InWorld, FIntVector InSrcOrigin, FIntVector InDstOrigin);
80
81 void OnLevelAddedToWorld(ULevel* InLevel, UWorld* InWorld);
82 void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld);
83
84 // Snap the generated signs whose ground has streamed in (used after tiles
85 // stream in), if the traffic light manager has the flag on.
86 void AdjustSignsHeightToGround();
87
88public:
89
90 void RegisterInitialObjects();
91
92 void OnActorSpawned(const FCarlaActor& CarlaActor);
93
94 UFUNCTION(Category="Large Map Manager")
95 void OnActorDestroyed(AActor* DestroyedActor);
96
97 // Called every frame
98 void Tick(float DeltaTime) override;
99
100 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
101 void GenerateMap(FString InAssetsPath);
102
103 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
104 void GenerateLargeMap();
105
106 void GenerateMap(TArray<TPair<FString, FIntVector>> MapPathsIds);
107
108 UFUNCTION(BlueprintCallable, CallInEditor, Category = "Large Map Manager")
109 void ClearWorldAndTiles();
110
111 UFUNCTION(BlueprintCallable, CallInEditor, Category = "Large Map Manager")
112 void GenerateMap_Editor()
113 {
114 if (!LargeMapTilePath.IsEmpty()) GenerateMap(LargeMapTilePath);
115 }
116
117 void AddActorToUnloadedList(const FCarlaActor& CarlaActor, const FTransform& Transform);
118
119 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
120 FIntVector GetNumTilesInXY() const;
121
122 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
123 int GetNumTiles() const
124 {
125 return MapTiles.Num();
126 }
127
128 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
129 bool IsLevelOfTileLoaded(FIntVector InTileID) const;
130
131 bool IsTileLoaded(TileID TileId) const
132 {
133 return CurrentTilesLoaded.Contains(TileId);
134 }
135
136 bool IsTileLoaded(FVector Location) const
137 {
138 return IsTileLoaded(GetTileID(Location));
139 }
140
141 bool IsTileLoaded(FDVector Location) const
142 {
143 return IsTileLoaded(GetTileID(Location));
144 }
145
146 FTransform GlobalToLocalTransform(const FTransform& InTransform) const;
147 FVector GlobalToLocalLocation(const FVector& InLocation) const;
148
149 FTransform LocalToGlobalTransform(const FTransform& InTransform) const;
150 FVector LocalToGlobalLocation(const FVector& InLocation) const;
151
152 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
153 FString LargeMapTilePath = "/Game/Carla/Maps/testmap";
154 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
155 FString LargeMapName = "testmap";
156
157 void SetTile0Offset(const FVector& Offset);
158
159 void SetTileSize(float Size);
160
161 float GetTileSize();
162
163 FVector GetTile0Offset();
164
165 void SetLayerStreamingDistance(float Distance);
166
167 void SetActorStreamingDistance(float Distance);
168
169 float GetLayerStreamingDistance() const;
170
171 float GetActorStreamingDistance() const;
172
173 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
174 FIntVector GetTileVectorID(FVector TileLocation) const;
175
176 FIntVector GetTileVectorID(FDVector TileLocation) const;
177
178 FIntVector GetTileVectorID(TileID TileID) const;
179
180 FVector GetTileLocation(TileID TileID) const;
181
182 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
183 FVector GetTileLocation(FIntVector TileVectorID) const;
184
185 FDVector GetTileLocationD(TileID TileID) const;
186
187 FDVector GetTileLocationD(FIntVector TileVectorID) const;
188
189 /// From a given location it retrieves the TileID that covers that area
190 TileID GetTileID(FVector TileLocation) const;
191
192 TileID GetTileID(FDVector TileLocation) const;
193
194 TileID GetTileID(FIntVector TileVectorID) const;
195public:
196 FCarlaMapTile* GetCarlaMapTile(FVector Location);
197
198 FCarlaMapTile& GetCarlaMapTile(ULevel* InLevel);
199
200 UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
201 FCarlaMapTile& GetCarlaMapTile(FIntVector TileVectorID);
202
203 FCarlaMapTile* GetCarlaMapTilePointer(FIntVector TileVectorID);
204
205 FCarlaMapTile* GetCarlaMapTile(TileID TileID);
206
207 FCarlaMapTile& LoadCarlaMapTile(FString TileMapPath, TileID TileId);
208
209 ACarlaWheeledVehicle* GetHeroVehicle();
210
211 // The spectator is treated as an ego vehicle by default when no other egos are around,
212 // but this can be changed from the Python API, which ends up in this function.
213 bool SpectatorAsEgo = false;
214 void ConsiderSpectatorAsEgo(bool _SpectatorAsEgo);
215
216protected:
217
218 void RemoveLandscapeCollisionIfHaveTerraMechanics(ULevel* InLevel);
219
220 void UpdateTilesState();
221
222 void RemovePendingActorsToRemove();
223
224 // Check if any active actor has to be converted into dormant actor
225 // because it went out of range (ActorStreamingDistance)
226 // Just stores the array of selected actors
227 void CheckActiveActors();
228
229 // Converts active actors that went out of range to dormant actors
230 void ConvertActiveToDormantActors();
231
232 // Check if any dormant actor has to be converted into active actor
233 // because it enter in range (ActorStreamingDistance)
234 // Just stores the array of selected actors
235 void CheckDormantActors();
236
237 // Converts active actors that went out of range to dormant actors
238 void ConvertDormantToActiveActors();
239
240 void CheckIfRebaseIsNeeded();
241
242 void GetTilesToConsider(
243 const AActor* ActorToConsider,
244 TSet<TileID>& OutTilesToConsider);
245
246 void GetTilesThatNeedToChangeState(
247 const TSet<TileID>& InTilesToConsider,
248 TSet<TileID>& OutTilesToBeVisible,
249 TSet<TileID>& OutTilesToHidde);
250
251 void UpdateTileState(
252 const TSet<TileID>& InTilesToUpdate,
253 bool InShouldBlockOnLoad,
254 bool InShouldBeLoaded,
255 bool InShouldBeVisible);
256
257 void UpdateCurrentTilesLoaded(
258 const TSet<TileID>& InTilesToBeVisible,
259 const TSet<TileID>& InTilesToHidde);
260
261 UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
262 TMap<uint64, FCarlaMapTile> MapTiles;
263
264 // All actors to be consider for tile loading (all hero vehicles)
265 // The first actor in the array is the one selected for rebase
266 // TODO: support rebase in more than one hero vehicle
267 UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
268 TArray<AActor*> ActorsToConsider;
269
270 UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
271 AActor* Spectator = nullptr;
272 //UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
273 TArray<FCarlaActor::IdType> ActiveActors;
274 TArray<FCarlaActor::IdType> DormantActors;
275
276 // Temporal sets to remove actors. Just to avoid removing them in the update loop
277 TSet<AActor*> ActorsToRemove;
278 TSet<FCarlaActor::IdType> ActivesToRemove;
279 TSet<FCarlaActor::IdType> DormantsToRemove;
280
281 // Helpers to move Actors from one array to another.
282 TSet<FCarlaActor::IdType> ActiveToDormantActors;
283 TSet<FCarlaActor::IdType> DormantToActiveActors;
284
285 UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
286 TSet<uint64> CurrentTilesLoaded;
287
288 // Current Origin after rebase
289 UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
290 FIntVector CurrentOriginInt{ 0 };
291
293
294 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
295 FVector Tile0Offset = FVector(0,0,0);
296
297 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
298 float TickInterval = 0.0f;
299
300 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
301 float LayerStreamingDistance = 3.0f * 1000.0f * 100.0f;
302
303 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
304 float ActorStreamingDistance = 2.0f * 1000.0f * 100.0f;
305
306 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
307 float RebaseOriginDistance = 2.0f * 1000.0f * 100.0f;
308
309 float LayerStreamingDistanceSquared = LayerStreamingDistance * LayerStreamingDistance;
310 float ActorStreamingDistanceSquared = ActorStreamingDistance * ActorStreamingDistance;
311 float RebaseOriginDistanceSquared = RebaseOriginDistance * RebaseOriginDistance;
312
313 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
314 float TileSide = 2.0f * 1000.0f * 100.0f; // 2km
315
316 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
317 FVector LocalTileOffset = FVector(0,0,0);
318
319 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
320 bool ShouldTilesBlockOnLoad = false;
321
322
323 void RegisterTilesInWorldComposition();
324
325 FString GenerateTileName(TileID TileID);
326
327 FString TileIDToString(TileID TileID);
328
329 void DumpTilesTable() const;
330
331 void PrintMapInfo();
332
333 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Large Map Manager")
334 FString AssetsPath = "";
335
336 FString BaseTileMapPath = "/Game/Carla/Maps/LargeMap/EmptyTileBase";
337
338 FColor PositonMsgColor = FColor::Purple;
339
340 const int32 TilesDistMsgIndex = 100;
341 const int32 MaxTilesDistMsgIndex = TilesDistMsgIndex + 10;
342
343 const int32 ClientLocMsgIndex = 200;
344 const int32 MaxClientLocMsgIndex = ClientLocMsgIndex + 10;
345
346 UPROPERTY(EditAnywhere, Category = "Large Map Manager")
347 float MsgTime = 1.0f;
348
349 UPROPERTY(EditAnywhere, Category = "Large Map Manager")
350 bool bPrintMapInfo = true;
351
352 UPROPERTY(EditAnywhere, Category = "Large Map Manager")
353 bool bPrintErrors = false;
354
355 UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
356 bool bHasTerramechanics = false;
357};
Base class for CARLA wheeled vehicles.
bool IsTileLoaded(FDVector Location) const
bool IsTileLoaded(FVector Location) const
A view over an actor and its properties.
Definition CarlaActor.h:25
FActiveActor(const FCarlaActor *InCarlaActor, const FTransform &InTransform)
const FCarlaActor * CarlaActor
FDVector WorldLocation