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, 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  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 protected:
206 
207  void RemoveLandscapeCollisionIfHaveTerraMechanics(ULevel* InLevel);
208 
209  void UpdateTilesState();
210 
211  void RemovePendingActorsToRemove();
212 
213  // Check if any active actor has to be converted into dormant actor
214  // because it went out of range (ActorStreamingDistance)
215  // Just stores the array of selected actors
216  void CheckActiveActors();
217 
218  // Converts active actors that went out of range to dormant actors
219  void ConvertActiveToDormantActors();
220 
221  // Check if any dormant actor has to be converted into active actor
222  // because it enter in range (ActorStreamingDistance)
223  // Just stores the array of selected actors
224  void CheckDormantActors();
225 
226  // Converts active actors that went out of range to dormant actors
227  void ConvertDormantToActiveActors();
228 
229  void CheckIfRebaseIsNeeded();
230 
231  void GetTilesToConsider(
232  const AActor* ActorToConsider,
233  TSet<TileID>& OutTilesToConsider);
234 
235  void GetTilesThatNeedToChangeState(
236  const TSet<TileID>& InTilesToConsider,
237  TSet<TileID>& OutTilesToBeVisible,
238  TSet<TileID>& OutTilesToHidde);
239 
240  void UpdateTileState(
241  const TSet<TileID>& InTilesToUpdate,
242  bool InShouldBlockOnLoad,
243  bool InShouldBeLoaded,
244  bool InShouldBeVisible);
245 
246  void UpdateCurrentTilesLoaded(
247  const TSet<TileID>& InTilesToBeVisible,
248  const TSet<TileID>& InTilesToHidde);
249 
250  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
251  TMap<uint64, FCarlaMapTile> MapTiles;
252 
253  // All actors to be consider for tile loading (all hero vehicles)
254  // The first actor in the array is the one selected for rebase
255  // TODO: support rebase in more than one hero vehicle
256  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
257  TArray<AActor*> ActorsToConsider;
258 
259  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
260  AActor* Spectator = nullptr;
261  //UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
262  TArray<FCarlaActor::IdType> ActiveActors;
263  TArray<FCarlaActor::IdType> DormantActors;
264 
265  // Temporal sets to remove actors. Just to avoid removing them in the update loop
266  TSet<AActor*> ActorsToRemove;
267  TSet<FCarlaActor::IdType> ActivesToRemove;
268  TSet<FCarlaActor::IdType> DormantsToRemove;
269 
270  // Helpers to move Actors from one array to another.
271  TSet<FCarlaActor::IdType> ActiveToDormantActors;
272  TSet<FCarlaActor::IdType> DormantToActiveActors;
273 
274  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
275  TSet<uint64> CurrentTilesLoaded;
276 
277  // Current Origin after rebase
278  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
279  FIntVector CurrentOriginInt{ 0 };
280 
282 
283  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
284  FVector Tile0Offset = FVector(0,0,0);
285 
286  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
287  float TickInterval = 0.0f;
288 
289  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
290  float LayerStreamingDistance = 3.0f * 1000.0f * 100.0f;
291 
292  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
293  float ActorStreamingDistance = 2.0f * 1000.0f * 100.0f;
294 
295  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
296  float RebaseOriginDistance = 2.0f * 1000.0f * 100.0f;
297 
298  float LayerStreamingDistanceSquared = LayerStreamingDistance * LayerStreamingDistance;
299  float ActorStreamingDistanceSquared = ActorStreamingDistance * ActorStreamingDistance;
300  float RebaseOriginDistanceSquared = RebaseOriginDistance * RebaseOriginDistance;
301 
302  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
303  float TileSide = 2.0f * 1000.0f * 100.0f; // 2km
304 
305  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
306  FVector LocalTileOffset = FVector(0,0,0);
307 
308  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
309  bool ShouldTilesBlockOnLoad = false;
310 
311 
312  void RegisterTilesInWorldComposition();
313 
314  FString GenerateTileName(TileID TileID);
315 
316  FString TileIDToString(TileID TileID);
317 
318  void DumpTilesTable() const;
319 
320  void PrintMapInfo();
321 
322  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
323  FString AssetsPath = "";
324 
325  FString BaseTileMapPath = "/Game/Carla/Maps/LargeMap/EmptyTileBase";
326 
327  FColor PositonMsgColor = FColor::Purple;
328 
329  const int32 TilesDistMsgIndex = 100;
330  const int32 MaxTilesDistMsgIndex = TilesDistMsgIndex + 10;
331 
332  const int32 ClientLocMsgIndex = 200;
333  const int32 MaxClientLocMsgIndex = ClientLocMsgIndex + 10;
334 
335  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
336  float MsgTime = 1.0f;
337 
338  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
339  bool bPrintMapInfo = true;
340 
341  UPROPERTY(EditAnywhere, Category = "Large Map Manager")
342  bool bPrintErrors = false;
343 
344  UPROPERTY(VisibleAnywhere, Category = "Large Map Manager")
345  bool bHasTerramechanics = false;
346 
347 };
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