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