Agrarsense
Tagger.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 FrostBit Software Lab at the Lapland University of Applied Sciences
2//
3// This work is licensed under the terms of the MIT license.
4// For a copy, see <https://opensource.org/licenses/MIT>.
5
6#include "Tagger.h"
12
14#include "GeoReferencingSystem.h"
15#include "AssetRegistry/AssetRegistryModule.h"
16#include "Components/SkeletalMeshComponent.h"
17#include "Components/StaticMeshComponent.h"
18#include "Engine/SkeletalMesh.h"
19#include "Engine/StaticMesh.h"
20#include "Landscape.h"
21#include "LandscapeComponent.h"
22#include "Engine/World.h"
23#include "Kismet/GameplayStatics.h"
24#include "TimerManager.h"
25
27{
28 PrimaryActorTick.bCanEverTick = false;
29}
30
32{
33 Super::BeginPlay();
34
36 {
37 return;
38 }
39
40 UWorld* World = GetWorld();
41
42 // Create TMap<FString, ELabels>
43 LabelMap = UEnumUtilities::CreateStringEnumMap<ELabels>("/Script/Agrarsense.ELabels");
44
45 ActorSpawnedDelegateHandle = World->AddOnActorSpawnedHandler(FOnActorSpawned::FDelegate::CreateUObject(this, &ATagger::OnActorSpawned));
46
47 // Tag all existing Actors in the World after small delay
48 FTimerHandle Handle;
49 World->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([this]
50 {
52 }), 0.200f, false);
53}
54
55void ATagger::EndPlay(const EEndPlayReason::Type EndPlayReason)
56{
57 Super::EndPlay(EndPlayReason);
58
59 TaggedActors.Empty();
61}
62
64{
65 TArray<AActor*> Actors;
66 UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors);
67
68 TaggedActors.Reserve(Actors.Num());
69
70 // Sort actors based on distance to the origin (0,0,0)
71 // This way we can ensure that the actors are tagged in a consistent order each run
72 const FVector Origin(0.0f, 0.0f, 0.0f);
73 Actors.Sort([&](const AActor& A, const AActor& B)
74 {
75 return FVector::DistSquared(A.GetActorLocation(), Origin) < FVector::DistSquared(B.GetActorLocation(), Origin);
76 });
77
78 for (AActor* Actor : Actors)
79 {
80 if (Actor)
81 {
82 TagActor(*Actor);
83 }
84 }
85}
86
87void ATagger::OnActorSpawned(AActor* Actor)
88{
89 if (Actor)
90 {
91 TagActor(*Actor);
92 }
93}
94
95template <typename T>
96static auto CastEnum(T label)
97{
98 return static_cast<typename std::underlying_type<T>::type>(label);
99}
100
101void ATagger::TagActor(AActor& Actor)
102{
103//#ifndef InstanceSegmentationPass_EXISTS
104// AInstancedActor* InstancedActor = Cast<AInstancedActor>(&Actor);
105// if (InstancedActor)
106// {
107// if (InstancedActor->IsCustomDepthSetup())
108// {
109// // Custom depth has been setup beforehand. Return
110// return;
111// }
112// }
113//
114// AWalker* Walker = Cast<AWalker>(&Actor);
115// if (Walker)
116// {
117// // If the actor is AWalker, return. These are tagged in editor.
118// return;
119// }
120//#endif
121
122 // Check if this is a Landscape actor
123 // if so, apply same ID and label to all landscape and its components.
124 ALandscape* Landscape = Cast<ALandscape>(&Actor);
125 if (Landscape)
126 {
127 TagLandscape(Landscape, Actor);
128 return;
129 }
130
131 // Iterate all static meshes
132 TArray<UStaticMeshComponent*> StaticMeshComponents;
133 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
134 for (UStaticMeshComponent* Component : StaticMeshComponents)
135 {
136 if (Component)
137 {
138 ELabels Label = GetLabelFromStaticComponent(Component);
139 float InstanceSegmentationID = SetStencilValue(*Component, Actor, Label);
140
141 FTaggedActorData ActorData;
142 ActorData.Actor = &Actor;
143 ActorData.InstanceID = InstanceSegmentationID;
144 ActorData.StaticMeshComponent = Component;
145 ActorData.Label = Label;
146 TaggedActors.Add(ActorData);
147 }
148 }
149
150 // Iterate all skeletal meshes
151 TArray<USkeletalMeshComponent*> SkeletalMeshComponents;
152 Actor.GetComponents<USkeletalMeshComponent>(SkeletalMeshComponents);
153 for (USkeletalMeshComponent* Component : SkeletalMeshComponents)
154 {
155 if (Component)
156 {
158 float InstanceSegmentationID = SetStencilValue(*Component, Actor, Label);
159
160 FTaggedActorData ActorData;
161 ActorData.Actor = &Actor;
162 ActorData.InstanceID = InstanceSegmentationID;
163 ActorData.SkeletalMeshComponent = Component;
164 ActorData.Label = Label;
165 TaggedActors.Add(ActorData);
166 }
167 }
168}
169
170float ATagger::SetStencilValue(UPrimitiveComponent& Component, AActor& Actor, const ELabels& Label)
171{
172 Component.SetRenderCustomDepth(true);
173
174 const int32 ActorLabelStencilValue = static_cast<int32>(Label);
175 float ActorLabelStencilValueFloat = static_cast<float>(ActorLabelStencilValue);
176
177 Component.SetCustomDepthStencilValue(ActorLabelStencilValue + 1);
178
179 int32& Counter = LabelCounters.FindOrAdd(Label); // default 0 on first use
180 Counter += 1; // assign next id
181 const int32 CurrentIntID = Counter; // 1..n for this label
182 const float CurrentID = static_cast<float>(CurrentIntID);
183
184 // Write custom primitive data (X=per-label instance id, Y=semantic/stencil)
185 Component.SetCustomPrimitiveDataVector4f(4,
186 FVector4f(CurrentID,
187 ActorLabelStencilValueFloat,
188 0.0f,
189 0.0f));
190
191 Component.MarkRenderStateDirty();
192
193 /*UE_LOG(LogTemp, Log, TEXT("Per-label ID %f -> Actor %s (Label %d)"),
194 CurrentID, *Actor.GetName(), static_cast<int32>(Label));*/
195
196 return CurrentID;
197}
198
200{
201 if (TaggedActors.IsEmpty())
202 {
203 return;
204 }
205
207 if (!CSVFile)
208 {
209 return;
210 }
211
212 for (const FTaggedActorData& TaggedActorData : TaggedActors)
213 {
214 AActor* Actor = TaggedActorData.Actor;
215 if (!Actor)
216 {
217 continue;
218 }
219
220 WriteComponentToCSV(TaggedActorData);
221 }
222
223 CSVFile->Close();
224}
225
226AActor* ATagger::GetActorByInstanceID(float InstanceID)
227{
228 for (const FTaggedActorData& TaggedActorData : TaggedActors)
229 {
230 if (TaggedActorData.InstanceID == InstanceID)
231 {
232 return TaggedActorData.Actor;
233 }
234 }
235
236 return nullptr;
237}
238
240{
241 GeoReferencingSystem = AGeoReferencingSystem::GetGeoReferencingSystem(GetWorld());
242
243 // CSV file settings
244 FCSVFileSettings Settings;
246 Settings.Append = false;
247 Settings.CreateUnique = true;
249
250 FString MapName = GetWorld()->GetMapName();
251 FString FileName = MapName + TEXT("_ObjectLocations");
252
253 if (MapName.Contains("dev"))
254 {
255 Settings.QueueSize = 65536;
256 }
257
258 CSVFile = UCSVFile::CreateCSVFile(FileName, Settings);
259
260 TArray<FString> Header = { TEXT("Name"), TEXT("InstanceID"), TEXT("LocationX"), TEXT("LocationY"),
261 TEXT("LocationZ"), TEXT("Yaw"), TEXT("Pitch"), TEXT("Roll"), TEXT("Bounds")};
263 {
264 Header.Append({ TEXT("Latitude"), TEXT("Longitude"), TEXT("Altitude") });
265 }
266 CSVFile->WriteRow(Header);
267}
268
270{
271 AActor* Actor = Data.Actor;
272
273 if (!Actor)
274 {
275 return;
276 }
277
278 FString AssetPath;
279 FString MeshName;
280 FVector Location = Actor->GetActorLocation();
281 FRotator Rotation = Actor->GetActorRotation();
282 FBoxSphereBounds Bounds;
283
284 // Try StaticMeshComponent first
285 if (Data.StaticMeshComponent && Data.StaticMeshComponent->GetStaticMesh())
286 {
287 UStaticMesh* StaticMesh = Data.StaticMeshComponent->GetStaticMesh();
288 AssetPath = StaticMesh->GetPathName();
289 MeshName = StaticMesh->GetName();
290 Bounds = StaticMesh->GetBounds();
291 }
292 // If not valid, try SkeletalMeshComponent
293 else if (Data.SkeletalMeshComponent && Data.SkeletalMeshComponent->GetSkeletalMeshAsset())
294 {
295 USkeletalMesh* SkeletalMesh = Data.SkeletalMeshComponent->GetSkeletalMeshAsset();
296 AssetPath = SkeletalMesh->GetPathName();
297 MeshName = SkeletalMesh->GetName();
298 Bounds = SkeletalMesh->GetBounds();
299 }
300 else
301 {
302 return;
303 }
304
305 // Check asset path
306 if (!AssetPath.StartsWith(TEXT("/Game/Agrarsense/Models")))
307 {
308 return;
309 }
310
311 TArray<FString> Row;
312 Row.Reserve(GeoReferencingSystem ? 12 : 9);
313
314 Row.Add(MeshName);
315 Row.Add(FString::Printf(TEXT("%.0f"), Data.InstanceID));
316
317 // TODO add semantic label to the CSV
318 Row.Add(FString::Printf(TEXT("%.2f"), Location.X));
319 Row.Add(FString::Printf(TEXT("%.2f"), Location.Y));
320 Row.Add(FString::Printf(TEXT("%.2f"), Location.Z));
321 Row.Add(FString::Printf(TEXT("%.2f"), Rotation.Yaw));
322 Row.Add(FString::Printf(TEXT("%.2f"), Rotation.Pitch));
323 Row.Add(FString::Printf(TEXT("%.2f"), Rotation.Roll));
324
325 // Bounds
326 Row.Add(FString::Printf(TEXT("Origin=(%.2f,%.2f,%.2f),BoxExtent=(%.2f,%.2f,%.2f),SphereRadius=%.2f"),
327 Bounds.Origin.X, Bounds.Origin.Y, Bounds.Origin.Z,
328 Bounds.BoxExtent.X, Bounds.BoxExtent.Y, Bounds.BoxExtent.Z,
329 Bounds.SphereRadius));
330
332 {
333 const FGeographicCoordinates GeoCoords = UCoordinateConversionUtilities::UnrealToGeographicCoordinates(GeoReferencingSystem, Location);
334 Row.Add(FString::Printf(TEXT("%.8f"), GeoCoords.Latitude));
335 Row.Add(FString::Printf(TEXT("%.8f"), GeoCoords.Longitude));
336 Row.Add(FString::Printf(TEXT("%.8f"), GeoCoords.Altitude));
337 }
338
339 if (CSVFile)
340 {
341 CSVFile->WriteRow(Row);
342 }
343}
344
345void ATagger::TagLandscape(ALandscape* Landscape, AActor& Actor)
346{
347 if (!Landscape)
348 {
349 return;
350 }
351
352 // Do not change. hardcoded values for landscape.
353 // So each landscape section has same ID and label.
354 constexpr float LANDSCAPE_LABEL = 1.0f;
355 constexpr float LANDSCAPE_ID = 0.0f;
356
357 // For some reason we needed to loop UStaticMeshComponents instead of ULandscapeComponents
358 TArray<UStaticMeshComponent*> StaticMeshComponents;
359 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
360 for (UStaticMeshComponent* Component : StaticMeshComponents)
361 {
362 if (Component)
363 {
364 Component->SetRenderCustomDepth(true);
365 Component->SetCustomDepthStencilValue(static_cast<int32>(ELabels::Terrain) + 1);
366
367#ifdef InstanceSegmentationPass_EXISTS
368 // Set custom primitive data, used for instance segmentation.
369 Component->SetCustomPrimitiveDataVector4f(4,
370 FVector4f(LANDSCAPE_ID,
371 LANDSCAPE_LABEL,
372 0.0f,
373 0.0f));
374#endif
375 }
376 }
377}
378
380{
381 const ELabels* FoundLabel = LabelMap.Find(String);
382 if (FoundLabel)
383 {
384 return *FoundLabel;
385 }
386 else
387 {
388#if WITH_EDITOR
389 UE_LOG(LogTemp, Warning, TEXT("Tagger.cpp: Missing Label: %s"), *String);
390#endif
391 return ELabels::None;
392 }
393}
394
395ELabels ATagger::GetLabelFromStaticComponent(UStaticMeshComponent* StaticMeshComponentPtr)
396{
397 ELabels Label = ELabels::None;
398
399 if (!StaticMeshComponentPtr)
400 {
401 return Label;
402 }
403
404 Label = GetLabelFromTag(StaticMeshComponentPtr);
405 if (Label != ELabels::None)
406 {
407 return Label;
408 }
409
410 UStaticMesh* MeshPtr = StaticMeshComponentPtr->GetStaticMesh();
411 if (MeshPtr)
412 {
413 FString Name;
414 Label = GetLabelFromPath(MeshPtr->GetPathName(), Name);
415
416 if (Label != ELabels::None)
417 {
418 StaticMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
419 }
420 }
421
422 return Label;
423}
424
425ELabels ATagger::GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent* USkeletalMeshComponentPtr)
426{
427 ELabels Label = ELabels::None;
428
429 if (!USkeletalMeshComponentPtr)
430 {
431 return Label;
432 }
433
434 Label = GetLabelFromTag(USkeletalMeshComponentPtr);
435 if (Label != ELabels::None)
436 {
437 return Label;
438 }
439
440 USkeletalMesh* PhysicsAsset = USkeletalMeshComponentPtr->GetSkeletalMeshAsset();
441 if (PhysicsAsset)
442 {
443 FString Name;
444 Label = GetLabelFromPath(PhysicsAsset->GetPathName(), Name);
445
446 if (Label != ELabels::None)
447 {
448 USkeletalMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
449 }
450 }
451
452 return Label;
453}
454
455ELabels ATagger::GetLabelFromPath(const FString& Path, FString& FolderName)
456{
457 ELabels Label = ELabels::None;
458
459 // If the Path doesn't contain Models,
460 // the asset is not under /Content/Agrarsense/Models directory
461 // meaing we ignore it.
462 if (!Path.Contains("Models"))
463 {
464 return Label;
465 }
466
467 TArray<FString> StringArray;
468 Path.ParseIntoArray(StringArray, TEXT("/"), false);
469
470 FolderName = StringArray[StringArray.Num() - 2];
471
472 Label = GetLabelFromString(FolderName);
473
474 return Label;
475}
ELabels
Definition: ObjectLabel.h:14
static auto CastEnum(T label)
Definition: Tagger.cpp:96
void ExportObjectLocationsToCSV()
Definition: Tagger.cpp:199
virtual void BeginPlay() override
Definition: Tagger.cpp:31
void CreateCSVFile()
Definition: Tagger.cpp:239
TMap< FString, ELabels > LabelMap
Definition: Tagger.h:115
void WriteComponentToCSV(const FTaggedActorData Data)
Definition: Tagger.cpp:269
ELabels GetLabelFromPath(const FString &Path, FString &FolderName)
Definition: Tagger.cpp:455
ELabels GetLabelFromStaticComponent(UStaticMeshComponent *StaticMeshComponentPtr)
Definition: Tagger.cpp:395
void OnActorSpawned(AActor *Actor)
Definition: Tagger.cpp:87
ELabels GetLabelFromTag(const T *Object)
Definition: Tagger.h:95
void TagExistingActors()
Definition: Tagger.cpp:63
AGeoReferencingSystem * GeoReferencingSystem
Definition: Tagger.h:107
TArray< FTaggedActorData > TaggedActors
Definition: Tagger.h:111
ATagger()
Definition: Tagger.cpp:26
void TagActor(AActor &Actor)
Definition: Tagger.cpp:101
UCSVFile * CSVFile
Definition: Tagger.h:109
FDelegateHandle ActorSpawnedDelegateHandle
Definition: Tagger.h:113
ELabels GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent *USkeletalMeshComponentPtr)
Definition: Tagger.cpp:425
AActor * GetActorByInstanceID(float InstanceID)
Definition: Tagger.cpp:226
TMap< ELabels, int32 > LabelCounters
Definition: Tagger.h:126
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: Tagger.cpp:55
float SetStencilValue(UPrimitiveComponent &Component, AActor &Actor, const ELabels &Label)
Definition: Tagger.cpp:170
ELabels GetLabelFromString(const FString &String)
Definition: Tagger.cpp:379
void TagLandscape(ALandscape *Landscape, AActor &Actor)
Definition: Tagger.cpp:345
static bool IsPlayingInMainMenu()
static UCSVFile * CreateCSVFile(const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
Definition: CSVFile.cpp:14
void Close()
Definition: CSVFile.cpp:115
void WriteRow(const TArray< FString > &Cells)
Definition: CSVFile.cpp:77
static FGeographicCoordinates UnrealToGeographicCoordinates(AGeoReferencingSystem *GeoReferencingSystem, const FVector &Position)
bool CreateUnique
Definition: CSVFile.h:42
ECSVDelimiter Delimiter
Definition: CSVFile.h:36
int32 QueueSize
Definition: CSVFile.h:48
FCSVFileWriteOptions FileWriteOption
Definition: CSVFile.h:45
float InstanceID
Definition: Tagger.h:37
ELabels Label
Definition: Tagger.h:40
AActor * Actor
Definition: Tagger.h:28
UStaticMeshComponent * StaticMeshComponent
Definition: Tagger.h:31
USkeletalMeshComponent * SkeletalMeshComponent
Definition: Tagger.h:34