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) + 1;
175 float ActorLabelStencilValueFloat =
static_cast<float>(ActorLabelStencilValue);
177#if ENGINE_MAJOR_VERSION < 5 || (ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION <= 5)
178 ActorLabelStencilValueFloat =
static_cast<float>(ActorLabelStencilValue - 1);
181 Component.SetCustomDepthStencilValue(ActorLabelStencilValue);
183 float CurrentID =
ID;
186 Component.SetCustomPrimitiveDataVector4(4,
188 ActorLabelStencilValueFloat,
213 AActor* Actor = TaggedActorData.Actor;
229 if (TaggedActorData.InstanceID == InstanceID)
231 return TaggedActorData.Actor;
249 FString MapName = GetWorld()->GetMapName();
250 FString FileName = MapName + TEXT(
"_ObjectLocations");
252 if (MapName.Contains(
"dev"))
259 TArray<FString> Header = { TEXT(
"Name"), TEXT(
"InstanceID"), TEXT(
"LocationX"), TEXT(
"LocationY"),
260 TEXT(
"LocationZ"), TEXT(
"Yaw"), TEXT(
"Pitch"), TEXT(
"Roll"), TEXT(
"Bounds")};
263 Header.Append({ TEXT(
"Latitude"), TEXT(
"Longitude"), TEXT(
"Altitude") });
270 AActor* Actor = Data.
Actor;
279 FVector Location = Actor->GetActorLocation();
280 FRotator Rotation = Actor->GetActorRotation();
281 FBoxSphereBounds Bounds;
287 AssetPath = StaticMesh->GetPathName();
288 MeshName = StaticMesh->GetName();
289 Bounds = StaticMesh->GetBounds();
295 AssetPath = SkeletalMesh->GetPathName();
296 MeshName = SkeletalMesh->GetName();
297 Bounds = SkeletalMesh->GetBounds();
305 if (!AssetPath.StartsWith(TEXT(
"/Game/Agrarsense/Models")))
314 Row.Add(FString::Printf(TEXT(
"%.0f"), Data.
InstanceID));
317 Row.Add(FString::Printf(TEXT(
"%.2f"), Location.X));
318 Row.Add(FString::Printf(TEXT(
"%.2f"), Location.Y));
319 Row.Add(FString::Printf(TEXT(
"%.2f"), Location.Z));
320 Row.Add(FString::Printf(TEXT(
"%.2f"), Rotation.Yaw));
321 Row.Add(FString::Printf(TEXT(
"%.2f"), Rotation.Pitch));
322 Row.Add(FString::Printf(TEXT(
"%.2f"), Rotation.Roll));
325 Row.Add(FString::Printf(TEXT(
"Origin=(%.2f,%.2f,%.2f),BoxExtent=(%.2f,%.2f,%.2f),SphereRadius=%.2f"),
326 Bounds.Origin.X, Bounds.Origin.Y, Bounds.Origin.Z,
327 Bounds.BoxExtent.X, Bounds.BoxExtent.Y, Bounds.BoxExtent.Z,
328 Bounds.SphereRadius));
333 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Latitude));
334 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Longitude));
335 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Altitude));
351 constexpr float LANDSCAPE_ID = 0.0f;
354 TArray<UStaticMeshComponent*> StaticMeshComponents;
355 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
356 for (UStaticMeshComponent* Component : StaticMeshComponents)
360 Component->SetRenderCustomDepth(
true);
361 Component->SetCustomDepthStencilValue(
static_cast<int32
>(
ELabels::Terrain) + 1);
363#ifdef InstanceSegmentationPass_EXISTS
365 Component->SetCustomPrimitiveDataVector4(4,
366 FVector4(LANDSCAPE_ID,
385 UE_LOG(LogTemp, Warning, TEXT(
"Tagger.cpp: Missing Label: %s"), *String);
395 if (!StaticMeshComponentPtr)
406 UStaticMesh* MeshPtr = StaticMeshComponentPtr->GetStaticMesh();
414 StaticMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
425 if (!USkeletalMeshComponentPtr)
436 USkeletalMesh* PhysicsAsset = USkeletalMeshComponentPtr->GetSkeletalMeshAsset();
444 USkeletalMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
458 if (!Path.Contains(
"Models"))
463 TArray<FString> StringArray;
464 Path.ParseIntoArray(StringArray, TEXT(
"/"),
false);
466 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)
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