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()
43 {
44  GENERATED_BODY()
45 
46  UPROPERTY(VisibleAnywhere, Category = "Carla Map Tile")
47  FString Name; // Tile_{TileID_X}_{TileID_Y}
48 
49  // Absolute location, does not depend on rebasing
50  UPROPERTY(VisibleAnywhere, Category = "Carla Map Tile")
51  FVector Location{0.0f};
52  // TODO: not FVector
53 
54  UPROPERTY(VisibleAnywhere, 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, Category = "Large Map Manager")
149  FString LargeMapTilePath = "/Game/Carla/Maps/testmap";
150  UPROPERTY(EditAnywhere, 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  FIntVector GetTileVectorID(FVector TileLocation) const;
170 
171  FIntVector GetTileVectorID(FDVector TileLocation) const;
172 
173  FIntVector GetTileVectorID(TileID TileID) const;
174 
175  FVector GetTileLocation(TileID TileID) const;
176 
177  FVector GetTileLocation(FIntVector TileVectorID) const;
178 
179  FDVector GetTileLocationD(TileID TileID) const;
180 
181  FDVector GetTileLocationD(FIntVector TileVectorID) const;
182 
183  /// From a given location it retrieves the TileID that covers that area
184  TileID GetTileID(FVector TileLocation) const;
185 
186  TileID GetTileID(FDVector TileLocation) const;
187 
188  TileID GetTileID(FIntVector TileVectorID) const;
189 public:
190  FCarlaMapTile& GetCarlaMapTile(FVector Location);
191 
192  FCarlaMapTile& GetCarlaMapTile(ULevel* InLevel);
193 
194  FCarlaMapTile* GetCarlaMapTile(FIntVector TileVectorID);
195 
196  FCarlaMapTile* GetCarlaMapTile(TileID TileID);
197 
198  FCarlaMapTile& LoadCarlaMapTile(FString TileMapPath, TileID TileId);
199 
200  ACarlaWheeledVehicle* GetHeroVehicle();
201 
202 protected:
203 
204  void RemoveLandscapeCollisionIfHaveTerraMechanics(ULevel* InLevel);
205 
206  void UpdateTilesState();
207 
208  void RemovePendingActorsToRemove();
209 
210  // Check if any active actor has to be converted into dormant actor
211  // because it went out of range (ActorStreamingDistance)
212  // Just stores the array of selected actors
213  void CheckActiveActors();
214 
215  // Converts active actors that went out of range to dormant actors
216  void ConvertActiveToDormantActors();
217 
218  // Check if any dormant actor has to be converted into active actor
219  // because it enter in range (ActorStreamingDistance)
220  // Just stores the array of selected actors
221  void CheckDormantActors();
222 
223  // Converts active actors that went out of range to dormant actors
224  void ConvertDormantToActiveActors();
225 
226  void CheckIfRebaseIsNeeded();
227 
228  void GetTilesToConsider(
229  const AActor* ActorToConsider,
230  TSet<TileID>& OutTilesToConsider);
231 
232  void GetTilesThatNeedToChangeState(
233  const TSet<TileID>& InTilesToConsider,
234  TSet<TileID>& OutTilesToBeVisible,
235  TSet<TileID>& OutTilesToHidde);
236 
237  void UpdateTileState(
238  const TSet<TileID>& InTilesToUpdate,
239  bool InShouldBlockOnLoad,
240  bool InShouldBeLoaded,
241  bool InShouldBeVisible);
242 
243  void UpdateCurrentTilesLoaded(
244  const TSet<TileID>& InTilesToBeVisible,
245  const TSet<TileID>& InTilesToHidde);
246 
247  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
248  TMap<uint64, FCarlaMapTile> MapTiles;
249 
250  // All actors to be consider for tile loading (all hero vehicles)
251  // The first actor in the array is the one selected for rebase
252  // TODO: support rebase in more than one hero vehicle
253  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
254  TArray<AActor*> ActorsToConsider;
255  //UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
256  TArray<FCarlaActor::IdType> ActiveActors;
257  TArray<FCarlaActor::IdType> DormantActors;
258 
259  // Temporal sets to remove actors. Just to avoid removing them in the update loop
260  TSet<AActor*> ActorsToRemove;
261  TSet<FCarlaActor::IdType> ActivesToRemove;
262  TSet<FCarlaActor::IdType> DormantsToRemove;
263 
264  // Helpers to move Actors from one array to another.
265  TSet<FCarlaActor::IdType> ActiveToDormantActors;
266  TSet<FCarlaActor::IdType> DormantToActiveActors;
267 
268  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
269  TSet<uint64> CurrentTilesLoaded;
270 
271  // Current Origin after rebase
272  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
273  FIntVector CurrentOriginInt{ 0 };
274 
276 
277  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
278  FVector Tile0Offset = FVector(0,0,0);
279 
280  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
281  float TickInterval = 0.0f;
282 
283  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
284  float LayerStreamingDistance = 3.0f * 1000.0f * 100.0f;
285 
286  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
287  float ActorStreamingDistance = 2.0f * 1000.0f * 100.0f;
288 
289  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
290  float RebaseOriginDistance = 2.0f * 1000.0f * 100.0f;
291 
292  float LayerStreamingDistanceSquared = LayerStreamingDistance * LayerStreamingDistance;
293  float ActorStreamingDistanceSquared = ActorStreamingDistance * ActorStreamingDistance;
294  float RebaseOriginDistanceSquared = RebaseOriginDistance * RebaseOriginDistance;
295 
296  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
297  float TileSide = 2.0f * 1000.0f * 100.0f; // 2km
298 
299  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
300  FVector LocalTileOffset = FVector(0,0,0);
301 
302  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
303  bool ShouldTilesBlockOnLoad = false;
304 
305 
306  void RegisterTilesInWorldComposition();
307 
308  FString GenerateTileName(TileID TileID);
309 
310  FString TileIDToString(TileID TileID);
311 
312  void DumpTilesTable() const;
313 
314  void PrintMapInfo();
315 
316  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
317  FString AssetsPath = "";
318 
319  FString BaseTileMapPath = "/Game/Carla/Maps/LargeMap/EmptyTileBase";
320 
321  FColor PositonMsgColor = FColor::Purple;
322 
323  const int32 TilesDistMsgIndex = 100;
324  const int32 MaxTilesDistMsgIndex = TilesDistMsgIndex + 10;
325 
326  const int32 ClientLocMsgIndex = 200;
327  const int32 MaxClientLocMsgIndex = ClientLocMsgIndex + 10;
328 
329  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
330  float MsgTime = 1.0f;
331 
332  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
333  bool bPrintMapInfo = true;
334 
335  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
336  bool bPrintErrors = false;
337 
338  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
339  bool bHasTerramechanics = false;
340 
341 };
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