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"
21#include "LandscapeComponent.h"
22#include "Engine/World.h"
23#include "Kismet/GameplayStatics.h"
24#include "TimerManager.h"
28 PrimaryActorTick.bCanEverTick =
false;
40 UWorld* World = GetWorld();
43 LabelMap = UEnumUtilities::CreateStringEnumMap<ELabels>(
"/Script/Agrarsense.ELabels");
49 World->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([
this]
57 Super::EndPlay(EndPlayReason);
65 TArray<AActor*> Actors;
66 UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors);
72 const FVector Origin(0.0f, 0.0f, 0.0f);
73 Actors.Sort([&](
const AActor& A,
const AActor& B)
75 return FVector::DistSquared(A.GetActorLocation(), Origin) < FVector::DistSquared(B.GetActorLocation(), Origin);
78 for (AActor* Actor : Actors)
98 return static_cast<typename std::underlying_type<T>::type
>(label);
124 ALandscape* Landscape = Cast<ALandscape>(&Actor);
132 TArray<UStaticMeshComponent*> StaticMeshComponents;
133 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
134 for (UStaticMeshComponent* Component : StaticMeshComponents)
139 float InstanceSegmentationID =
SetStencilValue(*Component, Actor, Label);
142 ActorData.
Actor = &Actor;
143 ActorData.
InstanceID = InstanceSegmentationID;
145 ActorData.
Label = Label;
151 TArray<USkeletalMeshComponent*> SkeletalMeshComponents;
152 Actor.GetComponents<USkeletalMeshComponent>(SkeletalMeshComponents);
153 for (USkeletalMeshComponent* Component : SkeletalMeshComponents)
158 float InstanceSegmentationID =
SetStencilValue(*Component, Actor, Label);
161 ActorData.
Actor = &Actor;
162 ActorData.
InstanceID = InstanceSegmentationID;
164 ActorData.
Label = Label;
172 Component.SetRenderCustomDepth(
true);
174 const int32 ActorLabelStencilValue =
static_cast<int32
>(Label);
175 float ActorLabelStencilValueFloat =
static_cast<float>(ActorLabelStencilValue);
177 Component.SetCustomDepthStencilValue(ActorLabelStencilValue + 1);
181 const int32 CurrentIntID = Counter;
182 const float CurrentID =
static_cast<float>(CurrentIntID);
185 Component.SetCustomPrimitiveDataVector4f(4,
187 ActorLabelStencilValueFloat,
191 Component.MarkRenderStateDirty();
214 AActor* Actor = TaggedActorData.Actor;
230 if (TaggedActorData.InstanceID == InstanceID)
232 return TaggedActorData.Actor;
250 FString MapName = GetWorld()->GetMapName();
251 FString FileName = MapName + TEXT(
"_ObjectLocations");
253 if (MapName.Contains(
"dev"))
260 TArray<FString> Header = { TEXT(
"Name"), TEXT(
"InstanceID"), TEXT(
"LocationX"), TEXT(
"LocationY"),
261 TEXT(
"LocationZ"), TEXT(
"Yaw"), TEXT(
"Pitch"), TEXT(
"Roll"), TEXT(
"Bounds")};
264 Header.Append({ TEXT(
"Latitude"), TEXT(
"Longitude"), TEXT(
"Altitude") });
271 AActor* Actor = Data.
Actor;
280 FVector Location = Actor->GetActorLocation();
281 FRotator Rotation = Actor->GetActorRotation();
282 FBoxSphereBounds Bounds;
288 AssetPath = StaticMesh->GetPathName();
289 MeshName = StaticMesh->GetName();
290 Bounds = StaticMesh->GetBounds();
296 AssetPath = SkeletalMesh->GetPathName();
297 MeshName = SkeletalMesh->GetName();
298 Bounds = SkeletalMesh->GetBounds();
306 if (!AssetPath.StartsWith(TEXT(
"/Game/Agrarsense/Models")))
315 Row.Add(FString::Printf(TEXT(
"%.0f"), Data.
InstanceID));
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));
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));
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));
354 constexpr float LANDSCAPE_LABEL = 1.0f;
355 constexpr float LANDSCAPE_ID = 0.0f;
358 TArray<UStaticMeshComponent*> StaticMeshComponents;
359 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
360 for (UStaticMeshComponent* Component : StaticMeshComponents)
364 Component->SetRenderCustomDepth(
true);
365 Component->SetCustomDepthStencilValue(
static_cast<int32
>(
ELabels::Terrain) + 1);
367#ifdef InstanceSegmentationPass_EXISTS
369 Component->SetCustomPrimitiveDataVector4f(4,
370 FVector4f(LANDSCAPE_ID,
389 UE_LOG(LogTemp, Warning, TEXT(
"Tagger.cpp: Missing Label: %s"), *String);
399 if (!StaticMeshComponentPtr)
410 UStaticMesh* MeshPtr = StaticMeshComponentPtr->GetStaticMesh();
418 StaticMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
429 if (!USkeletalMeshComponentPtr)
440 USkeletalMesh* PhysicsAsset = USkeletalMeshComponentPtr->GetSkeletalMeshAsset();
448 USkeletalMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
462 if (!Path.Contains(
"Models"))
467 TArray<FString> StringArray;
468 Path.ParseIntoArray(StringArray, TEXT(
"/"),
false);
470 FolderName = StringArray[StringArray.Num() - 2];
static auto CastEnum(T label)
void ExportObjectLocationsToCSV()
virtual void BeginPlay() override
TMap< FString, ELabels > LabelMap
void WriteComponentToCSV(const FTaggedActorData Data)
ELabels GetLabelFromPath(const FString &Path, FString &FolderName)
ELabels GetLabelFromStaticComponent(UStaticMeshComponent *StaticMeshComponentPtr)
void OnActorSpawned(AActor *Actor)
ELabels GetLabelFromTag(const T *Object)
AGeoReferencingSystem * GeoReferencingSystem
TArray< FTaggedActorData > TaggedActors
void TagActor(AActor &Actor)
FDelegateHandle ActorSpawnedDelegateHandle
ELabels GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent *USkeletalMeshComponentPtr)
AActor * GetActorByInstanceID(float InstanceID)
TMap< ELabels, int32 > LabelCounters
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
float SetStencilValue(UPrimitiveComponent &Component, AActor &Actor, const ELabels &Label)
ELabels GetLabelFromString(const FString &String)
void TagLandscape(ALandscape *Landscape, AActor &Actor)
static bool IsPlayingInMainMenu()
static UCSVFile * CreateCSVFile(const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
void WriteRow(const TArray< FString > &Cells)
static FGeographicCoordinates UnrealToGeographicCoordinates(AGeoReferencingSystem *GeoReferencingSystem, const FVector &Position)
FCSVFileWriteOptions FileWriteOption
UStaticMeshComponent * StaticMeshComponent
USkeletalMeshComponent * SkeletalMeshComponent