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  void AddActorToUnloadedList(const FCarlaActor& CarlaActor, const FTransform& Transform);
108 
109  UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
110  FIntVector GetNumTilesInXY() const;
111 
112  UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
113  int GetNumTiles() const
114  {
115  return MapTiles.Num();
116  }
117 
118  UFUNCTION(BlueprintCallable, Category = "Large Map Manager")
119  bool IsLevelOfTileLoaded(FIntVector InTileID) const;
120 
121  bool IsTileLoaded(TileID TileId) const
122  {
123  return CurrentTilesLoaded.Contains(TileId);
124  }
125 
126  bool IsTileLoaded(FVector Location) const
127  {
128  return IsTileLoaded(GetTileID(Location));
129  }
130 
132  {
133  return IsTileLoaded(GetTileID(Location));
134  }
135 
136  FTransform GlobalToLocalTransform(const FTransform& InTransform) const;
137  FVector GlobalToLocalLocation(const FVector& InLocation) const;
138 
139  FTransform LocalToGlobalTransform(const FTransform& InTransform) const;
140  FVector LocalToGlobalLocation(const FVector& InLocation) const;
141 
142  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
143  FString LargeMapTilePath = "/Game/Carla/Maps/testmap";
144  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
145  FString LargeMapName = "testmap";
146 
147  void SetTile0Offset(const FVector& Offset);
148 
149  void SetTileSize(float Size);
150 
151  void SetLayerStreamingDistance(float Distance);
152 
153  void SetActorStreamingDistance(float Distance);
154 
155  float GetLayerStreamingDistance() const;
156 
157  float GetActorStreamingDistance() const;
158 
159 protected:
160 
161  FIntVector GetTileVectorID(FVector TileLocation) const;
162 
163  FIntVector GetTileVectorID(FDVector TileLocation) const;
164 
165  FIntVector GetTileVectorID(TileID TileID) const;
166 
167  FVector GetTileLocation(TileID TileID) const;
168 
169  FVector GetTileLocation(FIntVector TileVectorID) const;
170 
171  FDVector GetTileLocationD(TileID TileID) const;
172 
173  FDVector GetTileLocationD(FIntVector TileVectorID) const;
174 
175  /// From a given location it retrieves the TileID that covers that area
176  TileID GetTileID(FVector TileLocation) const;
177 
178  TileID GetTileID(FDVector TileLocation) const;
179 
180  TileID GetTileID(FIntVector TileVectorID) const;
181 
182  FCarlaMapTile& GetCarlaMapTile(FVector Location);
183 
184  FCarlaMapTile& GetCarlaMapTile(ULevel* InLevel);
185 
186  FCarlaMapTile* GetCarlaMapTile(FIntVector TileVectorID);
187 
188  FCarlaMapTile& LoadCarlaMapTile(FString TileMapPath, TileID TileId);
189 
190  void UpdateTilesState();
191 
192  void RemovePendingActorsToRemove();
193 
194  // Check if any active actor has to be converted into dormant actor
195  // because it went out of range (ActorStreamingDistance)
196  // Just stores the array of selected actors
197  void CheckActiveActors();
198 
199  // Converts active actors that went out of range to dormant actors
200  void ConvertActiveToDormantActors();
201 
202  // Check if any dormant actor has to be converted into active actor
203  // because it enter in range (ActorStreamingDistance)
204  // Just stores the array of selected actors
205  void CheckDormantActors();
206 
207  // Converts active actors that went out of range to dormant actors
208  void ConvertDormantToActiveActors();
209 
210  void CheckIfRebaseIsNeeded();
211 
212  void GetTilesToConsider(
213  const AActor* ActorToConsider,
214  TSet<TileID>& OutTilesToConsider);
215 
216  void GetTilesThatNeedToChangeState(
217  const TSet<TileID>& InTilesToConsider,
218  TSet<TileID>& OutTilesToBeVisible,
219  TSet<TileID>& OutTilesToHidde);
220 
221  void UpdateTileState(
222  const TSet<TileID>& InTilesToUpdate,
223  bool InShouldBlockOnLoad,
224  bool InShouldBeLoaded,
225  bool InShouldBeVisible);
226 
227  void UpdateCurrentTilesLoaded(
228  const TSet<TileID>& InTilesToBeVisible,
229  const TSet<TileID>& InTilesToHidde);
230 
231  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
232  TMap<uint64, FCarlaMapTile> MapTiles;
233 
234  // All actors to be consider for tile loading (all hero vehicles)
235  // The first actor in the array is the one selected for rebase
236  // TODO: support rebase in more than one hero vehicle
237  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
238  TArray<AActor*> ActorsToConsider;
239  //UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
240  TArray<FCarlaActor::IdType> ActiveActors;
241  TArray<FCarlaActor::IdType> DormantActors;
242 
243  // Temporal sets to remove actors. Just to avoid removing them in the update loop
244  TSet<AActor*> ActorsToRemove;
245  TSet<FCarlaActor::IdType> ActivesToRemove;
246  TSet<FCarlaActor::IdType> DormantsToRemove;
247 
248  // Helpers to move Actors from one array to another.
249  TSet<FCarlaActor::IdType> ActiveToDormantActors;
250  TSet<FCarlaActor::IdType> DormantToActiveActors;
251 
252  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
253  TSet<uint64> CurrentTilesLoaded;
254 
255  // Current Origin after rebase
256  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
257  FIntVector CurrentOriginInt{ 0 };
258 
260 
261  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
262  FVector Tile0Offset = FVector(0,0,0);
263 
264  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
265  float TickInterval = 0.0f;
266 
267  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
268  float LayerStreamingDistance = 3.0f * 1000.0f * 100.0f;
269 
270  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
271  float ActorStreamingDistance = 2.0f * 1000.0f * 100.0f;
272 
273  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
274  float RebaseOriginDistance = 2.0f * 1000.0f * 100.0f;
275 
276  float LayerStreamingDistanceSquared = LayerStreamingDistance * LayerStreamingDistance;
277  float ActorStreamingDistanceSquared = ActorStreamingDistance * ActorStreamingDistance;
278  float RebaseOriginDistanceSquared = RebaseOriginDistance * RebaseOriginDistance;
279 
280  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
281  float TileSide = 2.0f * 1000.0f * 100.0f; // 2km
282 
283  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
284  bool ShouldTilesBlockOnLoad = false;
285 
286  UFUNCTION(BlueprintCallable, CallInEditor, Category = "Large Map Manager")
287  void GenerateMap_Editor()
288  {
289  if (!LargeMapTilePath.IsEmpty()) GenerateMap(LargeMapTilePath);
290  }
291 
292  void RegisterTilesInWorldComposition();
293 
294  FString GenerateTileName(TileID TileID);
295 
296  FString TileIDToString(TileID TileID);
297 
298  void DumpTilesTable() const;
299 
300  void PrintMapInfo();
301 
302  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
303  FString AssetsPath = "";
304 
305  FString BaseTileMapPath = "/Game/Carla/Maps/LargeMap/EmptyTileBase";
306 
307  FColor PositonMsgColor = FColor::Purple;
308 
309  const int32 TilesDistMsgIndex = 100;
310  const int32 MaxTilesDistMsgIndex = TilesDistMsgIndex + 10;
311 
312  const int32 ClientLocMsgIndex = 200;
313  const int32 MaxClientLocMsgIndex = ClientLocMsgIndex + 10;
314 
315  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
316  float MsgTime = 1.0f;
317 
318  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
319  bool bPrintMapInfo = true;
320 
321  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
322  bool bPrintErrors = false;
323 
324 };
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
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