Agrarsense
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
ATagger Class Reference

#include <Tagger.h>

Inheritance diagram for ATagger:
Inheritance graph
[legend]
Collaboration diagram for ATagger:
Collaboration graph
[legend]

Public Member Functions

 ATagger ()
 

Protected Member Functions

virtual void BeginPlay () override
 
virtual void EndPlay (const EEndPlayReason::Type EndPlayReason) override
 

Private Member Functions

void TagExistingActors ()
 
void OnActorSpawned (AActor *Actor)
 
void TagActor (AActor &Actor, bool TagForSemanticSegmentation)
 
void SetStencilValue (UPrimitiveComponent &Component, const ELabels &Label, const bool SetRenderCustomDepth)
 
ELabels GetLabelFromString (const FString &String)
 
ELabels GetLabelFromStaticComponent (UStaticMeshComponent *StaticMeshComponentPtr)
 
ELabels GetLabelFromSkeletalMeshComponent (USkeletalMeshComponent *USkeletalMeshComponentPtr)
 
ELabels GetLabelFromPath (const FString &Path, FString &FolderName)
 
template<typename T >
ELabels GetLabelFromTag (const T *Object)
 

Private Attributes

FDelegateHandle ActorSpawnedDelegateHandle
 
TMap< FString, ELabelsLabelMap
 

Detailed Description

Definition at line 28 of file Tagger.h.

Constructor & Destructor Documentation

◆ ATagger()

ATagger::ATagger ( )

Definition at line 21 of file Tagger.cpp.

22{
23 PrimaryActorTick.bCanEverTick = false;
24}

Member Function Documentation

◆ BeginPlay()

void ATagger::BeginPlay ( )
overrideprotectedvirtual

Definition at line 26 of file Tagger.cpp.

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}
TMap< FString, ELabels > LabelMap
Definition: Tagger.h:73
void OnActorSpawned(AActor *Actor)
Definition: Tagger.cpp:75
void TagExistingActors()
Definition: Tagger.cpp:61
FDelegateHandle ActorSpawnedDelegateHandle
Definition: Tagger.h:71
static bool IsPlayingInMainMenu()

References ActorSpawnedDelegateHandle, UAgrarsenseSettings::IsPlayingInMainMenu(), LabelMap, OnActorSpawned(), and TagExistingActors().

◆ EndPlay()

void ATagger::EndPlay ( const EEndPlayReason::Type  EndPlayReason)
overrideprotectedvirtual

Definition at line 54 of file Tagger.cpp.

55{
56 Super::EndPlay(EndPlayReason);
57
59}

References ActorSpawnedDelegateHandle.

◆ GetLabelFromPath()

ELabels ATagger::GetLabelFromPath ( const FString &  Path,
FString &  FolderName 
)
private

Definition at line 210 of file Tagger.cpp.

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
ELabels GetLabelFromString(const FString &String)
Definition: Tagger.cpp:134

References GetLabelFromString(), and None.

Referenced by GetLabelFromSkeletalMeshComponent(), and GetLabelFromStaticComponent().

◆ GetLabelFromSkeletalMeshComponent()

ELabels ATagger::GetLabelFromSkeletalMeshComponent ( USkeletalMeshComponent *  USkeletalMeshComponentPtr)
private

Definition at line 180 of file Tagger.cpp.

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}
ELabels GetLabelFromPath(const FString &Path, FString &FolderName)
Definition: Tagger.cpp:210
ELabels GetLabelFromTag(const T *Object)
Definition: Tagger.h:61

References GetLabelFromPath(), GetLabelFromTag(), and None.

Referenced by TagActor().

◆ GetLabelFromStaticComponent()

ELabels ATagger::GetLabelFromStaticComponent ( UStaticMeshComponent *  StaticMeshComponentPtr)
private

Definition at line 150 of file Tagger.cpp.

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}

References GetLabelFromPath(), GetLabelFromTag(), and None.

Referenced by TagActor().

◆ GetLabelFromString()

ELabels ATagger::GetLabelFromString ( const FString &  String)
private

Definition at line 134 of file Tagger.cpp.

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}

References LabelMap, and None.

Referenced by GetLabelFromPath().

◆ GetLabelFromTag()

template<typename T >
ELabels ATagger::GetLabelFromTag ( const T *  Object)
inlineprivate

Definition at line 61 of file Tagger.h.

62 {
63 if (!Object->ComponentTags.IsEmpty())
64 {
65 return GetLabelFromString(Object->ComponentTags[0].ToString());
66 }
67
68 return ELabels::None;
69 }

References None.

Referenced by GetLabelFromSkeletalMeshComponent(), and GetLabelFromStaticComponent().

◆ OnActorSpawned()

void ATagger::OnActorSpawned ( AActor *  Actor)
private

Definition at line 75 of file Tagger.cpp.

76{
77 if (Actor)
78 {
79 TagActor(*Actor, true);
80 }
81}
void TagActor(AActor &Actor, bool TagForSemanticSegmentation)
Definition: Tagger.cpp:83

References TagActor().

Referenced by BeginPlay().

◆ SetStencilValue()

void ATagger::SetStencilValue ( UPrimitiveComponent &  Component,
const ELabels Label,
const bool  SetRenderCustomDepth 
)
private

Definition at line 127 of file Tagger.cpp.

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}

References None.

Referenced by TagActor().

◆ TagActor()

void ATagger::TagActor ( AActor &  Actor,
bool  TagForSemanticSegmentation 
)
private

Definition at line 83 of file Tagger.cpp.

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}
bool IsCustomDepthSetup() const
ELabels GetLabelFromStaticComponent(UStaticMeshComponent *StaticMeshComponentPtr)
Definition: Tagger.cpp:150
void SetStencilValue(UPrimitiveComponent &Component, const ELabels &Label, const bool SetRenderCustomDepth)
Definition: Tagger.cpp:127
ELabels GetLabelFromSkeletalMeshComponent(USkeletalMeshComponent *USkeletalMeshComponentPtr)
Definition: Tagger.cpp:180
Definition: Walker.h:28

References GetLabelFromSkeletalMeshComponent(), GetLabelFromStaticComponent(), AInstancedActor::IsCustomDepthSetup(), SetStencilValue(), and Walker.

Referenced by OnActorSpawned(), and TagExistingActors().

◆ TagExistingActors()

void ATagger::TagExistingActors ( )
private

Definition at line 61 of file Tagger.cpp.

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}

References TagActor().

Referenced by BeginPlay().

Member Data Documentation

◆ ActorSpawnedDelegateHandle

FDelegateHandle ATagger::ActorSpawnedDelegateHandle
private

Definition at line 71 of file Tagger.h.

Referenced by BeginPlay(), and EndPlay().

◆ LabelMap

TMap<FString, ELabels> ATagger::LabelMap
private

Definition at line 73 of file Tagger.h.

Referenced by BeginPlay(), and GetLabelFromString().


The documentation for this class was generated from the following files: