Agrarsense
SensorFactory.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
20
21#include "Engine/GameViewportClient.h"
22#include "Misc/Guid.h"
23
24TWeakObjectPtr<USensorMapDataAsset> USensorFactory::SensorMapDataAsset;
25
27{
28 if (SensorMapDataAsset.IsValid())
29 {
30 return;
31 }
32
33 SensorMapDataAsset.Reset();
34 FSoftObjectPath SensorMapDataAssetPath(TEXT("/Game/Agrarsense/Data/Sensors/SensorMap"));
35 SensorMapDataAsset = Cast<USensorMapDataAsset>(SensorMapDataAssetPath.TryLoad());
36
37#if WITH_EDITOR
38 if (SensorMapDataAsset.IsValid())
39 {
40 UE_LOG(LogTemp, Warning, TEXT("USensorFactory.cpp: Loaded SensorMapDataAsset successfully."));
41 }
42 else
43 {
44 UE_LOG(LogTemp, Warning, TEXT("USensorFactory.cpp: Loading SensorMapDataAsset failed."));
45 }
46#endif
47}
48
50{
51 Initialize();
52
53 if (SensorMapDataAsset.IsValid())
54 {
55 return SensorMapDataAsset.Get();
56 }
57 else
58 {
59 return nullptr;
60 }
61}
62
63void USensorFactory::SetSensorIdentifierAndNameWithFallbacks(ASensor* sensor, const FString& sensorIdentifier, const FString& sensorName)
64{
65 bool generateIdentifier = sensorIdentifier.IsEmpty();
66 bool generateName = sensorName.IsEmpty();
67
68 FString generatedSensorIdentifier;
69
70 // If ID not given, use ESensorTypes as the ID
71 // and let the ActorInformation-interface handle the numbering after that
72 if (generateIdentifier)
73 {
74 generatedSensorIdentifier = UEnumUtilities::ConvertSensorTypeToString(sensor->GetSensorType());
75 }
76
77 // If not using generated name -> set the given sensor name
78 if (!generateName)
79 {
80 sensor->SetSensorName(sensorName);
81 }
82
83 sensor->SetSensorIdentifier(generateIdentifier ? generatedSensorIdentifier : sensorIdentifier);
84
85 // If using generated name -> use the identifier as the name too
86 if (generateName)
87 {
88 sensor->SetSensorName(sensor->GetSensorIdentifier());
89 }
90}
91
92ALidar* USensorFactory::SpawnLidarSensor(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FLidarParameters lidarParameters, bool SimulateSensor)
93{
94 ALidar* LidarSensor = nullptr;
95 LidarSensor = SpawnSensor<ALidar>(transform, sensorIdentifier, sensorName, true);
96 if (LidarSensor)
97 {
98 LidarSensor->Init(lidarParameters, SimulateSensor);
99 }
100
101 return LidarSensor;
102}
103
104ACamera* USensorFactory::SpawnCamera(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool SimulateSensor)
105{
106 ACamera* RGBCamera = nullptr;
107 RGBCamera = SpawnSensor<ACamera>(transform, sensorIdentifier, sensorName, true);
108 if (RGBCamera)
109 {
110 RGBCamera->Init(cameraParameters, SimulateSensor);
111 }
112
113 return RGBCamera;
114}
115
116AThermalCamera* USensorFactory::SpawnThermalCamera(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FThermalCameraParameters thermalCameraParameters, bool SimulateSensor)
117{
118 AThermalCamera* ThermalCamera = nullptr;
119 ThermalCamera = SpawnSensor<AThermalCamera>(transform, sensorIdentifier, sensorName, true);
120 if (ThermalCamera)
121 {
122 ThermalCamera->ThermalInit(thermalCameraParameters, SimulateSensor);
123 }
124
125 return ThermalCamera;
126}
127
128ADepthCamera* USensorFactory::SpawnDepthCamera(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FDepthCameraParameters depthCameraParameters, bool SimulateSensor)
129{
130 ADepthCamera* DepthCamera = nullptr;
131 DepthCamera = SpawnSensor<ADepthCamera>(transform, sensorIdentifier, sensorName, true);
132 if (DepthCamera)
133 {
134 DepthCamera->DepthInit(depthCameraParameters, SimulateSensor);
135 }
136
137 return DepthCamera;
138}
139
140ASemanticSegmentationCamera* USensorFactory::SpawnSegmentationCamera(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool SimulateSensor)
141{
143 SemanticSegmentationCamera = SpawnSensor<ASemanticSegmentationCamera>(transform, sensorIdentifier, sensorName, true);
145 {
146 SemanticSegmentationCamera->Init(cameraParameters, SimulateSensor);
147 }
148
150}
151
152ADVSCamera* USensorFactory::SpawnDVSCamera(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FDVSCameraParameters DVSCameraParameters, bool SimulateSensor)
153{
154 ADVSCamera* DVSCamera = nullptr;
155 DVSCamera = SpawnSensor<ADVSCamera>(transform, sensorIdentifier, sensorName, true);
156 if (DVSCamera)
157 {
158 DVSCamera->DVSInit(DVSCameraParameters, SimulateSensor);
159 }
160
161 return DVSCamera;
162}
163
164ARadar* USensorFactory::SpawnRadar(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FRadarParameters radarParameters, bool SimulateSensor)
165{
166 ARadar* RadarSensor = nullptr;
167 RadarSensor = SpawnSensor<ARadar>(transform, sensorIdentifier, sensorName, true);
168 if (RadarSensor)
169 {
170 RadarSensor->Init(radarParameters, SimulateSensor);
171 }
172
173 return RadarSensor;
174}
175
176ACollisionSensor* USensorFactory::SpawnCollisionSensor(const FTransform& transform, AActor* Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor)
177{
178 ACollisionSensor* CollisionSensor = nullptr;
179 if (Owner)
180 {
181 CollisionSensor = SpawnSensor<ACollisionSensor>(transform, sensorIdentifier, sensorName, false);
182 if (CollisionSensor)
183 {
184 CollisionSensor->Init(Owner, SimulateSensor);
185 CollisionSensor->AttachToActor(Owner, FAttachmentTransformRules::KeepWorldTransform);
186 }
187 }
188
189 return CollisionSensor;
190}
191
192ATransformSensor* USensorFactory::SpawnTransformSensor(const FTransform& transform, AActor* Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor)
193{
194 ATransformSensor* TransformSensor = nullptr;
195 if (Owner)
196 {
197 TransformSensor = SpawnSensor<ATransformSensor>(transform, sensorIdentifier, sensorName, false);
198 if (TransformSensor)
199 {
200 TransformSensor->Init(Owner, SimulateSensor);
201 TransformSensor->AttachToActor(Owner, FAttachmentTransformRules::KeepWorldTransform);
202 }
203 }
204
205 return TransformSensor;
206}
207
208AOverlapSensor* USensorFactory::SpawnOverlapSensor(const FTransform& transform, FOverlapSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName)
209{
210 AOverlapSensor* OverlapSensor = nullptr;
211 OverlapSensor = SpawnSensor<AOverlapSensor>(transform, sensorIdentifier, sensorName, false);
212 if (OverlapSensor)
213 {
214 OverlapSensor->Init(Parameters);
215 }
216
217 return OverlapSensor;
218}
219
221{
222 ASensorModel* spawnedModel = nullptr;
223
224 if (!attachTo)
225 {
226 return spawnedModel;
227 }
228
230
231 if (SensorMap)
232 {
233 ESensorTypes sensorType = attachTo->GetSensorType();
234
235 TSubclassOf<ASensorModel> modelClass = nullptr;
236
237 bool matchFound;
238
239 USensorDataAsset* sensorData = SensorMap->GetAssetDataBySensorType(sensorType, matchFound);
240
241 if (matchFound && sensorData)
242 {
243 modelClass = sensorData->ModelClass;
244 }
245
246 if (modelClass)
247 {
248 spawnedModel = SpawnModelClass(modelClass, attachTo);
249 }
250 }
251
252 return spawnedModel;
253}
254
255ASensorModel* USensorFactory::SpawnModelClass(TSubclassOf<ASensorModel> modelClass, ASensor* attachTo)
256{
257 ASensorModel* createdModelActor = nullptr;
258
259 if (!attachTo)
260 {
261 return createdModelActor;
262 }
263
264 UWorld* World = nullptr;
265 if (GEngine && GEngine->GameViewport)
266 {
267 World = GEngine->GameViewport->GetWorld();
268 }
269
270 if (World)
271 {
272 FVector sensorLocation = attachTo->GetActorLocation();
273 createdModelActor = World->SpawnActor<ASensorModel>(modelClass);
274
275 if (createdModelActor)
276 {
277 createdModelActor->AttachToActor(attachTo, FAttachmentTransformRules::SnapToTargetIncludingScale);
278
279 attachTo->SetSensorModel(createdModelActor);
280 createdModelActor->SetAttachedToSensor(attachTo);
281 createdModelActor->InitAfterSpawn();
282 }
283 }
284
285 return createdModelActor;
286}
287
288template <typename T>
289T* USensorFactory::SpawnSensor(const FTransform& transform, const FString& sensorIdentifier, const FString& sensorName, bool SpawnSensorModel)
290{
291 UWorld* World = nullptr;
292
293 if (GEngine && GEngine->GameViewport)
294 {
295 // This might only fail if called in Editor without play mode.
296 World = GEngine->GameViewport->GetWorld();
297 }
298
299 T* Sensor = nullptr;
300
301 if (World)
302 {
303 Sensor = World->SpawnActorDeferred<T>(T::StaticClass(), transform);
304 if (Sensor)
305 {
306 SetSensorIdentifierAndNameWithFallbacks(Sensor, sensorIdentifier, sensorName);
307 Sensor->FinishSpawning(transform);
308
309 if (SpawnSensorModel)
310 {
311 SpawnModelClassForSensor(Cast<ASensor>(Sensor));
312 }
313 }
314 }
315
316 return Sensor;
317}
ESensorTypes
Definition: SensorTypes.h:15
@ SemanticSegmentationCamera
Definition: Camera.h:52
void Init(AActor *NewOwner, bool SimulateSensor=true)
Definition: Lidar.h:34
void Init(FLidarParameters parameters, bool SimulateSensor=true)
Definition: Lidar.cpp:37
void Init(FOverlapSensorParameters InParameters)
Definition: Radar.h:26
void Init(FRadarParameters parameters, bool SimulateSensor=true)
Definition: Radar.cpp:22
void InitAfterSpawn()
void SetAttachedToSensor(ASensor *NewAttachedToSensor)
Definition: SensorModel.h:38
Definition: Sensor.h:44
void SetSensorModel(ASensorModel *NewSensorModel)
Definition: Sensor.h:182
void SetSensorName(const FString newName)
Definition: Sensor.h:114
FString GetSensorIdentifier() const
Definition: Sensor.h:74
virtual ESensorTypes GetSensorType() const
Definition: Sensor.h:64
void SetSensorIdentifier(const FString newIdentifier)
Definition: Sensor.h:84
void Init(AActor *TransformActor, bool SimulateSensor=true)
static FString ConvertSensorTypeToString(ESensorTypes Sensortype)
TSubclassOf< ASensorModel > ModelClass
static void Initialize()
static T * SpawnSensor(const FTransform &transform, const FString &sensorIdentifier, const FString &sensorName, bool SpawnSensorModel=true)
static ATransformSensor * SpawnTransformSensor(const FTransform &transform, AActor *Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor=true)
static void SetSensorIdentifierAndNameWithFallbacks(ASensor *sensor, const FString &sensorIdentifier, const FString &sensorName)
static ASensorModel * SpawnModelClassForSensor(ASensor *attachTo)
static ARadar * SpawnRadar(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FRadarParameters radarParameters, bool SimulateSensor=true)
static TWeakObjectPtr< USensorMapDataAsset > SensorMapDataAsset
static ACollisionSensor * SpawnCollisionSensor(const FTransform &transform, AActor *Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor=true)
static AThermalCamera * SpawnThermalCamera(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FThermalCameraParameters thermalCameraParameters, bool SimulateSensor=true)
static ACamera * SpawnCamera(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool SimulateSensor=true)
static ADVSCamera * SpawnDVSCamera(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FDVSCameraParameters DVSCameraParameters, bool SimulateSensor)
static USensorMapDataAsset * GetSensorMapDataAsset()
static ASensorModel * SpawnModelClass(TSubclassOf< ASensorModel > modelClass, ASensor *attachTo)
static ADepthCamera * SpawnDepthCamera(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FDepthCameraParameters depthCameraParameters, bool SimulateSensor=true)
static ALidar * SpawnLidarSensor(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FLidarParameters lidarParameters, bool SimulateSensor=true)
static AOverlapSensor * SpawnOverlapSensor(const FTransform &transform, FOverlapSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName)
static ASemanticSegmentationCamera * SpawnSegmentationCamera(const FTransform &transform, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool SimulateSensor=true)
USensorDataAsset * GetAssetDataBySensorType(ESensorTypes SensorType, bool &MatchFound)