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