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);
173 Component.SetCustomDepthStencilValue(
static_cast<int>(Label) + 1);
175#ifdef InstanceSegmentationPass_EXISTS
176 float CurrentID =
ID;
179 Component.SetCustomPrimitiveDataVector4(4,
207 AActor* Actor = TaggedActorData.Actor;
223 if (TaggedActorData.InstanceID == InstanceID)
225 return TaggedActorData.Actor;
243 FString MapName = GetWorld()->GetMapName();
244 FString FileName = MapName + TEXT(
"_ObjectLocations");
246 if (MapName.Contains(
"dev"))
253 TArray<FString> Header = { TEXT(
"Name"), TEXT(
"InstanceID"), TEXT(
"LocationX"), TEXT(
"LocationY"),
254 TEXT(
"LocationZ"), TEXT(
"Yaw"), TEXT(
"Pitch"), TEXT(
"Roll"), TEXT(
"Bounds")};
257 Header.Append({ TEXT(
"Latitude"), TEXT(
"Longitude"), TEXT(
"Altitude") });
264 AActor* Actor = Data.
Actor;
273 FVector Location = Actor->GetActorLocation();
274 FRotator Rotation = Actor->GetActorRotation();
275 FBoxSphereBounds Bounds;
281 AssetPath = StaticMesh->GetPathName();
282 MeshName = StaticMesh->GetName();
283 Bounds = StaticMesh->GetBounds();
289 AssetPath = SkeletalMesh->GetPathName();
290 MeshName = SkeletalMesh->GetName();
291 Bounds = SkeletalMesh->GetBounds();
299 if (!AssetPath.StartsWith(TEXT(
"/Game/Agrarsense/Models")))
308 Row.Add(FString::Printf(TEXT(
"%.0f"), Data.
InstanceID));
311 Row.Add(FString::Printf(TEXT(
"%.2f"), Location.X));
312 Row.Add(FString::Printf(TEXT(
"%.2f"), Location.Y));
313 Row.Add(FString::Printf(TEXT(
"%.2f"), Location.Z));
314 Row.Add(FString::Printf(TEXT(
"%.2f"), Rotation.Yaw));
315 Row.Add(FString::Printf(TEXT(
"%.2f"), Rotation.Pitch));
316 Row.Add(FString::Printf(TEXT(
"%.2f"), Rotation.Roll));
319 Row.Add(FString::Printf(TEXT(
"Origin=(%.2f,%.2f,%.2f),BoxExtent=(%.2f,%.2f,%.2f),SphereRadius=%.2f"),
320 Bounds.Origin.X, Bounds.Origin.Y, Bounds.Origin.Z,
321 Bounds.BoxExtent.X, Bounds.BoxExtent.Y, Bounds.BoxExtent.Z,
322 Bounds.SphereRadius));
327 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Latitude));
328 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Longitude));
329 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Altitude));
345 constexpr float LANDSCAPE_ID = 0.0f;
348 TArray<UStaticMeshComponent*> StaticMeshComponents;
349 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
350 for (UStaticMeshComponent* Component : StaticMeshComponents)
354 Component->SetRenderCustomDepth(
true);
355 Component->SetCustomDepthStencilValue(
static_cast<int>(
ELabels::Terrain) + 1);
357#ifdef InstanceSegmentationPass_EXISTS
359 Component->SetCustomPrimitiveDataVector4(4,
360 FVector4(LANDSCAPE_ID,
379 UE_LOG(LogTemp, Warning, TEXT(
"Tagger.cpp: Missing Label: %s"), *String);
389 if (!StaticMeshComponentPtr)
400 UStaticMesh* MeshPtr = StaticMeshComponentPtr->GetStaticMesh();
408 StaticMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
419 if (!USkeletalMeshComponentPtr)
430 USkeletalMesh* PhysicsAsset = USkeletalMeshComponentPtr->GetSkeletalMeshAsset();
438 USkeletalMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
452 if (!Path.Contains(
"Models"))
457 TArray<FString> StringArray;
458 Path.ParseIntoArray(StringArray, TEXT(
"/"),
false);
460 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