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

#include <DeletionVolume.h>

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

Public Member Functions

 ADeletionVolume ()
 
void ChangeOverlapBounds (FVector Bounds)
 
void DestroyOverlappingActors (bool OnlyTrees)
 

Protected Member Functions

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

Protected Attributes

UBoxComponent * CollisionBox = nullptr
 
UBoundsVisualizerComponent * VisualizerComponent = nullptr
 

Detailed Description

Definition at line 16 of file DeletionVolume.h.

Constructor & Destructor Documentation

◆ ADeletionVolume()

ADeletionVolume::ADeletionVolume ( )

Definition at line 19 of file DeletionVolume.cpp.

20{
21 PrimaryActorTick.bCanEverTick = false;
22
23 CollisionBox = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionBox"));
24 CollisionBox->InitBoxExtent(FVector(200.0f, 200.0f, 200.0f));
25 CollisionBox->SetGenerateOverlapEvents(true);
26 RootComponent = CollisionBox;
27
28#if WITH_EDITOR
29 VisualizerComponent = CreateDefaultSubobject<UBoundsVisualizerComponent>(TEXT("BoundsVisualizerComponent"));
30 VisualizerComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
31 VisualizerComponent->SetGenerateOverlapEvents(false);
32 VisualizerComponent->SetupAttachment(RootComponent);
33 VisualizerComponent->SetVisibility(true);
34#endif
35}
UBoxComponent * CollisionBox
UBoundsVisualizerComponent * VisualizerComponent

References CollisionBox, and VisualizerComponent.

Member Function Documentation

◆ BeginPlay()

void ADeletionVolume::BeginPlay ( )
overrideprotectedvirtual

Definition at line 37 of file DeletionVolume.cpp.

38{
39 Super::BeginPlay();
40
42 {
43 VisualizerComponent->SetCubeMesh();
44 VisualizerComponent->SetVisibility(true);
45 }
46}

References VisualizerComponent.

◆ ChangeOverlapBounds()

void ADeletionVolume::ChangeOverlapBounds ( FVector  Bounds)

Definition at line 65 of file DeletionVolume.cpp.

66{
67 if (IsValid(this))
68 {
69 if (CollisionBox)
70 {
71 CollisionBox->SetBoxExtent(Bounds);
72 }
73
75 {
76 VisualizerComponent->SetRelativeScale3D(Bounds);
77 }
78 }
79}

References CollisionBox, and VisualizerComponent.

Referenced by UROSCommands::HandleDestroyActors(), and USimulatorJsonParser::ParseVolumeDeletion().

◆ DestroyOverlappingActors()

void ADeletionVolume::DestroyOverlappingActors ( bool  OnlyTrees)

Destroys all actors overlapping with this actor's UBoxComponent. If OnlyTrees is true, only tree actors are destroyed; otherwise, all overlapping actors (InstancedActors) are destroyed.

Parameters
OnlyTreesIf true, only tree actors will be destroyed. If false, all overlapping actors will be destroyed.

Definition at line 81 of file DeletionVolume.cpp.

82{
83 if (!IsValid(this) || !CollisionBox)
84 {
85 return;
86 }
87
88 FVector BoxCenter = CollisionBox->GetComponentLocation();
89 FVector BoxExtent = CollisionBox->GetScaledBoxExtent();
90
91 TArray<FOverlapResult> Overlaps;
92
93 bool HasOverlaps = GetWorld()->OverlapMultiByChannel(
94 Overlaps,
95 BoxCenter,
96 FQuat::Identity,
97 ECC_WorldDynamic,
98 FCollisionShape::MakeBox(BoxExtent),
99 FCollisionQueryParams(NAME_None, false, this)
100 );
101
102 if (!HasOverlaps)
103 {
104 SimulatorLog::Log("No overlaps detected.");
105 Destroy();
106 return;
107 }
108
110
111 for (const FOverlapResult& Overlap : Overlaps)
112 {
113 if (UPrimitiveComponent* Component = Overlap.GetComponent())
114 {
115 UInstancedStaticMeshComponent* InstancedMesh = Cast<UInstancedStaticMeshComponent>(Component);
116 if (InstancedMesh && Renderer)
117 {
118 // If this overlapped InstancedStaticMeshComponent,
119 // remove overlapping instances through InstancedRenderer
120 FBox Box = FBox(BoxCenter - BoxExtent, BoxCenter + BoxExtent);
121 Renderer->DestroyOverlappingInstancesBox(InstancedMesh, Box, OnlyTrees);
122 }
123 else
124 {
125 if (AActor* Actor = Component->GetOwner())
126 {
127 if (AInstancedActor* InstancedActor = Cast<AInstancedActor>(Actor))
128 {
129 bool CanDelete = !OnlyTrees || (OnlyTrees && InstancedActor->IsTreeActor());
130 if (CanDelete)
131 {
132 InstancedActor->Destroy();
133 }
134 }
135 }
136 }
137 }
138 }
139
140 // Destroy this actor after deleting overlapping actors inside the bounds.
141 Destroy();
142}
void DestroyOverlappingInstancesBox(UInstancedStaticMeshComponent *ISM, FBox AreaBox, bool OnlyTrees)
static void Log(const FString &Message, bool LogToTextFile=true, bool LogToROS=true)
static AInstancedRenderer * GetInstancedRenderer(const UObject *WorldContextObject)

References CollisionBox, Destroy, AInstancedRenderer::DestroyOverlappingInstancesBox(), UAgrarsenseStatics::GetInstancedRenderer(), SimulatorLog::Log(), and Overlap.

Referenced by UROSCommands::HandleDestroyActors(), and USimulatorJsonParser::ParseVolumeDeletion().

◆ EndPlay()

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

Definition at line 48 of file DeletionVolume.cpp.

49{
50 Super::EndPlay(EndPlayReason);
51
52 if (CollisionBox)
53 {
54 CollisionBox->DestroyComponent();
55 CollisionBox = nullptr;
56 }
57
59 {
60 VisualizerComponent->DestroyComponent();
61 VisualizerComponent = nullptr;
62 }
63}

References CollisionBox, and VisualizerComponent.

Member Data Documentation

◆ CollisionBox

UBoxComponent* ADeletionVolume::CollisionBox = nullptr
protected

◆ VisualizerComponent

UBoundsVisualizerComponent* ADeletionVolume::VisualizerComponent = nullptr
protected

Definition at line 48 of file DeletionVolume.h.

Referenced by ADeletionVolume(), BeginPlay(), ChangeOverlapBounds(), and EndPlay().


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