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);
105 ALandscape* Landscape = Cast<ALandscape>(&Actor);
113 TArray<UStaticMeshComponent*> StaticMeshComponents;
114 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
115 for (UStaticMeshComponent* Component : StaticMeshComponents)
120 int32 InstanceSegmentationID =
SetStencilValue(*Component, Actor, Label);
123 ActorData.
Actor = &Actor;
124 ActorData.
InstanceID = InstanceSegmentationID;
126 ActorData.
Label = Label;
132 TArray<USkeletalMeshComponent*> SkeletalMeshComponents;
133 Actor.GetComponents<USkeletalMeshComponent>(SkeletalMeshComponents);
134 for (USkeletalMeshComponent* Component : SkeletalMeshComponents)
139 int32 InstanceSegmentationID =
SetStencilValue(*Component, Actor, Label);
142 ActorData.
Actor = &Actor;
143 ActorData.
InstanceID = InstanceSegmentationID;
145 ActorData.
Label = Label;
153 Component.SetRenderCustomDepth(
true);
155 const int32 ActorLabelStencilValue =
static_cast<int32
>(Label);
156 float ActorLabelStencilValueFloat =
static_cast<float>(ActorLabelStencilValue);
158 Component.SetCustomDepthStencilValue(ActorLabelStencilValue + 1);
162 const int32 CurrentIntID = Counter;
163 const float CurrentID =
static_cast<float>(CurrentIntID);
166 Component.SetCustomPrimitiveDataVector4f(4,
168 ActorLabelStencilValueFloat,
172 Component.MarkRenderStateDirty();
195 AActor* Actor = TaggedActorData.Actor;
213 if (TaggedActorData.Label == Label && TaggedActorData.InstanceID == InstanceID)
215 return TaggedActorData.Actor;
233 FString MapName = GetWorld()->GetMapName();
234 FString FileName = MapName + TEXT(
"_ObjectLocations");
236 if (MapName.Contains(
"dev"))
243 TArray<FString> Header = { TEXT(
"Name"), TEXT(
"Label"), TEXT(
"InstanceID"), TEXT(
"LocationX"), TEXT(
"LocationY"),
244 TEXT(
"LocationZ"), TEXT(
"Yaw"), TEXT(
"Pitch"), TEXT(
"Roll"), TEXT(
"Bounds")};
247 Header.Append({ TEXT(
"Latitude"), TEXT(
"Longitude"), TEXT(
"Altitude") });
254 AActor* Actor = Data.
Actor;
262 FVector Location = Actor->GetActorLocation();
263 FRotator Rotation = Actor->GetActorRotation();
264 FBoxSphereBounds Bounds;
270 AssetPath = StaticMesh->GetPathName();
271 MeshName = StaticMesh->GetName();
272 Bounds = StaticMesh->GetBounds();
278 AssetPath = SkeletalMesh->GetPathName();
279 MeshName = SkeletalMesh->GetName();
280 Bounds = SkeletalMesh->GetBounds();
288 if (!AssetPath.StartsWith(TEXT(
"/Game/Agrarsense/Models")))
300 const UEnum* LabelsEnum = StaticEnum<ELabels>();
301 const int32 LabelIndex =
static_cast<int32
>(
static_cast<uint8
>(Data.
Label));
302 const FString LabelName = LabelsEnum
303 ? LabelsEnum->GetNameStringByValue((int64)LabelIndex)
305 Row.Add(FString::Printf(TEXT(
"%s (%d)"), *LabelName, LabelIndex));
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(
320 TEXT(
"Origin=(%.2f,%.2f,%.2f),BoxExtent=(%.2f,%.2f,%.2f),SphereRadius=%.2f"),
321 Bounds.Origin.X, Bounds.Origin.Y, Bounds.Origin.Z,
322 Bounds.BoxExtent.X, Bounds.BoxExtent.Y, Bounds.BoxExtent.Z,
323 Bounds.SphereRadius));
328 const FGeographicCoordinates GeoCoords =
330 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Latitude));
331 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Longitude));
332 Row.Add(FString::Printf(TEXT(
"%.8f"), GeoCoords.Altitude));
350 constexpr float LANDSCAPE_LABEL = 1.0f;
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->SetCustomPrimitiveDataVector4f(4,
366 FVector4f(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
int32 SetStencilValue(UPrimitiveComponent &Component, AActor &Actor, const ELabels &Label)
ELabels GetLabelFromPath(const FString &Path, FString &FolderName)
ELabels GetLabelFromStaticComponent(UStaticMeshComponent *StaticMeshComponentPtr)
void OnActorSpawned(AActor *Actor)
ELabels GetLabelFromTag(const T *Object)
void WriteComponentToCSV(const FTaggedActorData &Data)
AGeoReferencingSystem * GeoReferencingSystem
AActor * TryGetActorByInstanceID(int32 LabelIndex, int32 InstanceID)
TArray< FTaggedActorData > TaggedActors
void TagActor(AActor &Actor)
FDelegateHandle ActorSpawnedDelegateHandle
ELabels GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent *USkeletalMeshComponentPtr)
TMap< ELabels, int32 > LabelCounters
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
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