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