Agrarsense
Tagger.h
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#pragma once
7
8#include "CoreMinimal.h"
9#include "GameFramework/Actor.h"
10#include "Kismet/GameplayStatics.h"
11#include "ObjectLabel.h"
12#include "PhysicsEngine/PhysicsAsset.h"
13
14#include "Tagger.generated.h"
15
16class USkeletalMeshComponent;
17class AGeoReferencingSystem;
18class UStaticMeshComponent;
19class ALandscape;
20class UCSVFile;
21
22USTRUCT(Blueprintable)
23struct AGRARSENSE_API FTaggedActorData
24{
25 GENERATED_BODY()
26
27 UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
28 AActor* Actor;
29
30 UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
31 UStaticMeshComponent* StaticMeshComponent;
32
33 UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
34 USkeletalMeshComponent* SkeletalMeshComponent;
35
36 UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
37 float InstanceID;
38
39 UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
40 ELabels Label;
41};
42
43/*
44* This Actor is responsible for tagging all Actors in the level.
45* During BeginPlay, this iterates through all Actors in the level, setting stencil value for object if it's not set.
46* This stencil value is used by SemanicSegmentationCamera in its post processing material.
47* Additionally, each time a new Actor is spawned, this Actor will automatically check it as well.
48*/
49UCLASS()
50class AGRARSENSE_API ATagger : public AActor
51{
52 GENERATED_BODY()
53
54public:
55
56 ATagger();
57
58 UFUNCTION(BlueprintCallable)
59 void ExportObjectLocationsToCSV();
60
61 UFUNCTION(BlueprintCallable)
62 AActor* GetActorByInstanceID(float InstanceID);
63
64protected:
65
66 virtual void BeginPlay() override;
67
68 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
69
70protected:
71
72 void CreateCSVFile();
73
74 void TagExistingActors();
75
76 void OnActorSpawned(AActor* Actor);
77
78 void TagActor(AActor& Actor);
79
80 void WriteComponentToCSV(const FTaggedActorData Data);
81
82 float SetStencilValue(UPrimitiveComponent& Component, AActor& Actor, const ELabels& Label);
83
84 void TagLandscape(ALandscape* Landscape, AActor& Actor);
85
86 ELabels GetLabelFromString(const FString& String);
87
88 ELabels GetLabelFromStaticComponent(UStaticMeshComponent* StaticMeshComponentPtr);
89
90 ELabels GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent* USkeletalMeshComponentPtr);
91
92 ELabels GetLabelFromPath(const FString& Path, FString& FolderName);
93
94 template <typename T>
95 ELabels GetLabelFromTag(const T* Object)
96 {
97 if (!Object->ComponentTags.IsEmpty())
98 {
99 return GetLabelFromString(Object->ComponentTags[0].ToString());
100 }
101
102 return ELabels::None;
103 }
104
105private:
106
107 AGeoReferencingSystem* GeoReferencingSystem = nullptr;
108
109 UCSVFile* CSVFile = nullptr;
110
111 TArray<FTaggedActorData> TaggedActors;
112
114
115 TMap<FString, ELabels> LabelMap;
116
117 bool TerrainIDCached = false;
118
119 uint32 CachedTerrainID = 0;
120
121 // ID Counter for instance segmentation.
122 // 0.0f reserved for Landscape
123 float ID = 1.0f;
124
125};
ELabels
Definition: ObjectLabel.h:14
Definition: Tagger.h:51
TMap< FString, ELabels > LabelMap
Definition: Tagger.h:115
ELabels GetLabelFromTag(const T *Object)
Definition: Tagger.h:95
TArray< FTaggedActorData > TaggedActors
Definition: Tagger.h:111
FDelegateHandle ActorSpawnedDelegateHandle
Definition: Tagger.h:113
static UCSVFile * CreateCSVFile(const FString &FileNameWithoutExtension, const FCSVFileSettings &Settings)
Definition: CSVFile.cpp:14