Agrarsense
SensorUtilities.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 "SensorUtilities.h"
7#include "Kismet/GameplayStatics.h"
8#include "Components/SceneCaptureComponent2D.h"
9#include "Components/PrimitiveComponent.h"
10
15
16void USensorUtilities::EnableAllSensors(const UObject* WorldContextObject)
17{
18 SetSimulateAllSensors(WorldContextObject, true);
19}
20
21void USensorUtilities::DisableAllSensors(const UObject* WorldContextObject)
22{
23 SetSimulateAllSensors(WorldContextObject, false);
24}
25
26void USensorUtilities::SetSimulateAllSensors(const UObject* WorldContextObject, bool SimulateSensors)
27{
28 // Get all ASensor actors from the level
29 TArray<AActor*> Sensors;
30 UGameplayStatics::GetAllActorsOfClass(WorldContextObject, ASensor::StaticClass(), Sensors);
31
32 for (AActor* SensorActor : Sensors)
33 {
34 ASensor* sensor = Cast<ASensor>(SensorActor);
35 if (sensor != nullptr)
36 {
37 sensor->SetSimulateSensor(SimulateSensors);
38 }
39 }
40}
41
42void USensorUtilities::DestroyAllSensors(const UObject* WorldContextObject, bool DestroyDefaultVehicleSensors)
43{
44 // Most sensors should be attached to Vehicle(s)
45 // Get all Vehicles from the level and destroy the sensors through USensorsManagerComponent
46 TArray<AActor*> Vehicles;
47 UGameplayStatics::GetAllActorsOfClass(WorldContextObject, AVehicle::StaticClass(), Vehicles);
48 for (AActor* Actor : Vehicles)
49 {
50 AVehicle* Vehicle = Cast<AVehicle>(Actor);
51 if (Vehicle)
52 {
53 auto* SensorManager = Vehicle->GetSensorsManager();
54 if (SensorManager)
55 {
56 SensorManager->DestroyAllSensors();
57 }
58 }
59 }
60
61 Vehicles.Empty();
62
63 // Destroy remaining sensors if they exist
64 TArray<AActor*> Sensors;
65 UGameplayStatics::GetAllActorsOfClass(WorldContextObject, ASensor::StaticClass(), Sensors);
66 for (AActor* SensorActor : Sensors)
67 {
68 ASensor* Sensor = Cast<ASensor>(SensorActor);
69 if (Sensor)
70 {
71 bool AllowedToDestroy = true;
72 if (!DestroyDefaultVehicleSensors)
73 {
74 // We might not want to destroy these sensors,
75 // as these sensors cannot be spawned back any in way at the moment.
76 // These sensors are spawned automatically by Vehicle.cpp to each vehicle.
77 ESensorTypes SensorType = Sensor->GetSensorType();
78 if (SensorType == ESensorTypes::Overlap
79 || SensorType == ESensorTypes::Transform
80 || SensorType == ESensorTypes::Collision)
81 {
82 AllowedToDestroy = false;
83 }
84 }
85
86 if (AllowedToDestroy)
87 {
88 Sensor->Destroy();
89 }
90 }
91 }
92
93 Sensors.Empty();
94}
95
97{
99}
100
102{
104}
105
107{
108 if (Vehicle == nullptr)
109 {
110#if WITH_EDITOR
111 UE_LOG(LogTemp, Warning, TEXT("vehicle is nullptr. Returning."));
112#endif
113 return;
114 }
115
116 // Get all Actors attached to Vehicle actor
117 TArray<AActor*> Actors;
118 Vehicle->GetAttachedActors(Actors);
119
120 for (AActor* actor : Actors)
121 {
122 ASensor* sensor = Cast<ASensor>(actor);
123 if (sensor)
124 {
125 sensor->SetSimulateSensor(SimulateSensors);
126 }
127 }
128}
129
130void USensorUtilities::HideComponentForAllCameraSensors(UPrimitiveComponent* PrimitiveComponent)
131{
132 if (PrimitiveComponent)
133 {
134 ASensor::HideComponentForAllCameras(PrimitiveComponent);
135 }
136}
ESensorTypes
Definition: SensorTypes.h:15
Definition: Sensor.h:44
static void HideComponentForAllCameras(UPrimitiveComponent *PrimitiveComponent)
Definition: Sensor.cpp:255
virtual ESensorTypes GetSensorType() const
Definition: Sensor.h:64
void SetSimulateSensor(bool SimulateSensor)
Definition: Sensor.h:151
static void SetSimulateAllSensors(const UObject *WorldContextObject, bool SimulateSensors)
static void EnableSensorsAttachedToVehicle(AVehicle *Vehicle)
static void SetSimulateSensorsAttachedToVehicle(AVehicle *Vehicle, bool SimulateSensors)
static void DestroyAllSensors(const UObject *WorldContextObject, bool DestroyDefaultVehicleSensors=false)
static void HideComponentForAllCameraSensors(UPrimitiveComponent *PrimitiveComponent)
static void EnableAllSensors(const UObject *WorldContextObject)
static void DisableSensorsAttachedToVehicle(AVehicle *Vehicle)
static void DisableAllSensors(const UObject *WorldContextObject)