Agrarsense
Tagger.cpp
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#include "Tagger.h"
11
12#include "AssetRegistry/AssetRegistryModule.h"
13#include "Components/SkeletalMeshComponent.h"
14#include "Components/StaticMeshComponent.h"
15#include "Engine/SkeletalMesh.h"
16#include "Engine/StaticMesh.h"
17#include "Engine/World.h"
18#include "Kismet/GameplayStatics.h"
19#include "TimerManager.h"
20
22{
23 PrimaryActorTick.bCanEverTick = false;
24}
25
27{
28 Super::BeginPlay();
29
31 {
32 return;
33 }
34
35 UWorld* World = GetWorld();
36 if (!World)
37 {
38 return;
39 }
40
41 // Create TMap<FString, ELabels>
42 LabelMap = UEnumUtilities::CreateStringEnumMap<ELabels>("/Script/Agrarsense.ELabels");
43
44 ActorSpawnedDelegateHandle = World->AddOnActorSpawnedHandler(FOnActorSpawned::FDelegate::CreateUObject(this, &ATagger::OnActorSpawned));
45
46 // Tag all existing Actors in the World after small delay
47 FTimerHandle Handle;
48 World->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([this]
49 {
51 }), 0.500f, false);
52}
53
54void ATagger::EndPlay(const EEndPlayReason::Type EndPlayReason)
55{
56 Super::EndPlay(EndPlayReason);
57
59}
60
62{
63 TArray<AActor*> Actors;
64 UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), Actors);
65
66 for (AActor* Actor : Actors)
67 {
68 if (Actor)
69 {
70 TagActor(*Actor, true);
71 }
72 }
73}
74
75void ATagger::OnActorSpawned(AActor* Actor)
76{
77 if (Actor)
78 {
79 TagActor(*Actor, true);
80 }
81}
82
83void ATagger::TagActor(AActor& Actor, bool TagForSemanticSegmentation)
84{
85 AInstancedActor* InstancedActor = Cast<AInstancedActor>(&Actor);
86 if (InstancedActor)
87 {
88 if (InstancedActor->IsCustomDepthSetup())
89 {
90 // Custom depth has been setup beforehand. Return
91 return;
92 }
93 }
94
95 AWalker* Walker = Cast<AWalker>(&Actor);
96 if (Walker)
97 {
98 // If the actor is AWalker, return. These are tagged in editor.
99 return;
100 }
101
102 // Iterate all static meshes
103 TArray<UStaticMeshComponent*> StaticMeshComponents;
104 Actor.GetComponents<UStaticMeshComponent>(StaticMeshComponents);
105 for (UStaticMeshComponent* Component : StaticMeshComponents)
106 {
107 if (Component)
108 {
109 auto Label = GetLabelFromStaticComponent(Component);
110 SetStencilValue(*Component, Label, TagForSemanticSegmentation);
111 }
112 }
113
114 // Iterate all skeletal meshes
115 TArray<USkeletalMeshComponent*> SkeletalMeshComponents;
116 Actor.GetComponents<USkeletalMeshComponent>(SkeletalMeshComponents);
117 for (USkeletalMeshComponent* Component : SkeletalMeshComponents)
118 {
119 if (Component)
120 {
122 SetStencilValue(*Component, Label, TagForSemanticSegmentation);
123 }
124 }
125}
126
127void ATagger::SetStencilValue(UPrimitiveComponent& Component, const ELabels& Label, const bool SetRenderCustomDepth)
128{
129 // Only set Custom Depth enabled if Label is not None.
130 Component.SetRenderCustomDepth(SetRenderCustomDepth && (Label != ELabels::None));
131 Component.SetCustomDepthStencilValue(static_cast<int>(Label) + 1);
132}
133
135{
136 const ELabels* FoundLabel = LabelMap.Find(String);
137 if (FoundLabel)
138 {
139 return *FoundLabel;
140 }
141 else
142 {
143#if WITH_EDITOR
144 UE_LOG(LogTemp, Warning, TEXT("Tagger.cpp: Missing Label: %s"), *String);
145#endif
146 return ELabels::None;
147 }
148}
149
150ELabels ATagger::GetLabelFromStaticComponent(UStaticMeshComponent* StaticMeshComponentPtr)
151{
152 ELabels Label = ELabels::None;
153
154 if (!StaticMeshComponentPtr)
155 {
156 return Label;
157 }
158
159 Label = GetLabelFromTag(StaticMeshComponentPtr);
160 if (Label != ELabels::None)
161 {
162 return Label;
163 }
164
165 UStaticMesh* MeshPtr = StaticMeshComponentPtr->GetStaticMesh();
166 if (MeshPtr)
167 {
168 FString Name;
169 Label = GetLabelFromPath(MeshPtr->GetPathName(), Name);
170
171 if (Label != ELabels::None)
172 {
173 StaticMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
174 }
175 }
176
177 return Label;
178}
179
180ELabels ATagger::GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent* USkeletalMeshComponentPtr)
181{
182 ELabels Label = ELabels::None;
183
184 if (!USkeletalMeshComponentPtr)
185 {
186 return Label;
187 }
188
189 Label = GetLabelFromTag(USkeletalMeshComponentPtr);
190 if (Label != ELabels::None)
191 {
192 return Label;
193 }
194
195 USkeletalMesh* PhysicsAsset = USkeletalMeshComponentPtr->GetSkeletalMeshAsset();
196 if (PhysicsAsset)
197 {
198 FString Name;
199 Label = GetLabelFromPath(PhysicsAsset->GetPathName(), Name);
200
201 if (Label != ELabels::None)
202 {
203 USkeletalMeshComponentPtr->ComponentTags.Insert(FName(Name), 0);
204 }
205 }
206
207 return Label;
208}
209
210ELabels ATagger::GetLabelFromPath(const FString& Path, FString& FolderName)
211{
212 ELabels Label = ELabels::None;
213
214 // If the Path doesn't contain Models,
215 // it means the Asset is not under /Content/Agrarsense/Models
216 // and we ignore it.
217 if (!Path.Contains("Models"))
218 {
219 return Label;
220 }
221
222 TArray<FString> StringArray;
223 Path.ParseIntoArray(StringArray, TEXT("/"), false);
224
225 FolderName = StringArray[StringArray.Num() - 2];
226
227 Label = GetLabelFromString(FolderName);
228
229 //UE_LOG(LogTemp, Warning, TEXT("FolderName %s"), *FolderName);
230 //UE_LOG(LogTemp, Warning, TEXT("AssetPath %s"), *Path);
231
232 return Label;
233}
ELabels
Definition: ObjectLabel.h:14
bool IsCustomDepthSetup() const
virtual void BeginPlay() override
Definition: Tagger.cpp:26
TMap< FString, ELabels > LabelMap
Definition: Tagger.h:73
ELabels GetLabelFromPath(const FString &Path, FString &FolderName)
Definition: Tagger.cpp:210
ELabels GetLabelFromStaticComponent(UStaticMeshComponent *StaticMeshComponentPtr)
Definition: Tagger.cpp:150
void OnActorSpawned(AActor *Actor)
Definition: Tagger.cpp:75
ELabels GetLabelFromTag(const T *Object)
Definition: Tagger.h:61
void TagExistingActors()
Definition: Tagger.cpp:61
ATagger()
Definition: Tagger.cpp:21
void SetStencilValue(UPrimitiveComponent &Component, const ELabels &Label, const bool SetRenderCustomDepth)
Definition: Tagger.cpp:127
FDelegateHandle ActorSpawnedDelegateHandle
Definition: Tagger.h:71
ELabels GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent *USkeletalMeshComponentPtr)
Definition: Tagger.cpp:180
void TagActor(AActor &Actor, bool TagForSemanticSegmentation)
Definition: Tagger.cpp:83
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
Definition: Tagger.cpp:54
ELabels GetLabelFromString(const FString &String)
Definition: Tagger.cpp:134
Definition: Walker.h:28
static bool IsPlayingInMainMenu()