Agrarsense
SensorsManagerComponent.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
18
19const FString USensorsManagerComponent::ComponentHierarchySeparator = TEXT("_|_");
20
21USensorsManagerComponent::USensorsManagerComponent()
22{
23 PrimaryComponentTick.bCanEverTick = false;
24}
25
26USensorsManagerComponent::~USensorsManagerComponent()
27{
28}
29
30ALidar* USensorsManagerComponent::SpawnLidar(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FLidarParameters lidarParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
31{
32 // Check if attachToComponent is not set but attachToBone is set
33 if (!attachToComponent && !attachToBone.IsNone())
34 {
35 attachToComponent = GetOwner()->GetRootComponent();
36 }
37
38 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
39
40
41 FSensorSpawnParameters SpawnParams;
42 SpawnParams.Transform = sensorTransform;
43 SpawnParams.SensorIdentifier = sensorIdentifier;
44 SpawnParams.SensorName = sensorName;
45 SpawnParams.SimulateSensor = simulateSensor;
46 SpawnParams.Parent = GetOwner();
47
48 ALidar* lidar = USensorFactory::SpawnLidarSensor(SpawnParams, lidarParameters);
49
50 if (!lidar)
51 {
52 return nullptr;
53 }
54
55 createdModel = lidar->GetSensorModel();
56
57 ASensor* CastedSensor = Cast<ASensor>(lidar);
58
59 AddSensorDestroyListener(CastedSensor);
60
61 AttachSensor(CastedSensor, attachToComponent, attachToBone);
62
63 Lidars.Emplace(lidar);
64 Sensors.Emplace(CastedSensor);
65
66 OnLidarSpawned.Broadcast(lidar);
67 OnSensorSpawned.Broadcast(CastedSensor);
68
69 return lidar;
70}
71
72ACamera* USensorsManagerComponent::SpawnCamera(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
73{
74 // Check if attachToComponent is not set but attachToBone is set
75 if (!attachToComponent && !attachToBone.IsNone())
76 {
77 attachToComponent = GetOwner()->GetRootComponent();
78 }
79
80 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
81
82 FSensorSpawnParameters SpawnParams;
83 SpawnParams.Transform = sensorTransform;
84 SpawnParams.SensorIdentifier = sensorIdentifier;
85 SpawnParams.SensorName = sensorName;
86 SpawnParams.SimulateSensor = simulateSensor;
87 SpawnParams.Parent = GetOwner();
88
89 ACamera* camera = USensorFactory::SpawnCamera(SpawnParams, cameraParameters);
90
91 if (!camera)
92 {
93 return nullptr;
94 }
95
96 createdModel = camera->GetSensorModel();
97
98 ASensor* CastedSensor = Cast<ASensor>(camera);
99
100 AttachSensor(CastedSensor, attachToComponent, attachToBone);
101
102 AddSensorDestroyListener(CastedSensor);
103
104 Cameras.Emplace(camera);
105 Sensors.Emplace(CastedSensor);
106
107 camera->OnCameraWindowClosed.AddDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
108
109 OnCameraSpawned.Broadcast(camera);
110 OnSensorSpawned.Broadcast(CastedSensor);
111
112 return camera;
113}
114
115AThermalCamera* USensorsManagerComponent::SpawnThermalCamera(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FThermalCameraParameters thermalCameraParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
116{
117
118 // Check if attachToComponent is not set but attachToBone is set
119 if (!attachToComponent && !attachToBone.IsNone())
120 {
121 attachToComponent = GetOwner()->GetRootComponent();
122 }
123
124 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
125
126 FSensorSpawnParameters SpawnParams;
127 SpawnParams.Transform = sensorTransform;
128 SpawnParams.SensorIdentifier = sensorIdentifier;
129 SpawnParams.SensorName = sensorName;
130 SpawnParams.SimulateSensor = simulateSensor;
131 SpawnParams.Parent = GetOwner();
132
133 AThermalCamera* thermalCamera = USensorFactory::SpawnThermalCamera(SpawnParams, thermalCameraParameters);
134
135 if (!thermalCamera)
136 {
137 return nullptr;
138 }
139
140 createdModel = thermalCamera->GetSensorModel();
141
142 ASensor* CastedSensor = Cast<ASensor>(thermalCamera);
143
144 AttachSensor(CastedSensor, attachToComponent, attachToBone);
145
146 AddSensorDestroyListener(CastedSensor);
147
148 ThermalCameras.Emplace(thermalCamera);
149 Sensors.Emplace(CastedSensor);
150
151 thermalCamera->OnCameraWindowClosed.AddDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
152
153 OnThermalCameraSpawned.Broadcast(thermalCamera);
154 OnSensorSpawned.Broadcast(CastedSensor);
155
156 return thermalCamera;
157}
158
159ADVSCamera* USensorsManagerComponent::SpawnDVSCamera(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FDVSCameraParameters DVSCameraParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
160{
161 // Check if attachToComponent is not set but attachToBone is set
162 if (!attachToComponent && !attachToBone.IsNone())
163 {
164 attachToComponent = GetOwner()->GetRootComponent();
165 }
166
167 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
168
169 FSensorSpawnParameters SpawnParams;
170 SpawnParams.Transform = sensorTransform;
171 SpawnParams.SensorIdentifier = sensorIdentifier;
172 SpawnParams.SensorName = sensorName;
173 SpawnParams.SimulateSensor = simulateSensor;
174 SpawnParams.Parent = GetOwner();
175
176 ADVSCamera* DVSCamera = USensorFactory::SpawnDVSCamera(SpawnParams, DVSCameraParameters);
177
178 if (!DVSCamera)
179 {
180 return nullptr;
181 }
182
183 createdModel = DVSCamera->GetSensorModel();
184
185 ASensor* CastedSensor = Cast<ASensor>(DVSCamera);
186
187 AttachSensor(CastedSensor, attachToComponent, attachToBone);
188
189 AddSensorDestroyListener(CastedSensor);
190
191 DVSCameras.Emplace(DVSCamera);
192 Sensors.Emplace(CastedSensor);
193
194 DVSCamera->OnCameraWindowClosed.AddDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
195
196 OnDVSCameraSpawned.Broadcast(DVSCamera);
197 OnSensorSpawned.Broadcast(CastedSensor);
198
199 return DVSCamera;
200}
201
202ASemanticSegmentationCamera* USensorsManagerComponent::SpawnSegmentationCamera(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
203{
204 // Check if attachToComponent is not set but attachToBone is set
205 if (!attachToComponent && !attachToBone.IsNone())
206 {
207 attachToComponent = GetOwner()->GetRootComponent();
208 }
209
210 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
211
212 FSensorSpawnParameters SpawnParams;
213 SpawnParams.Transform = sensorTransform;
214 SpawnParams.SensorIdentifier = sensorIdentifier;
215 SpawnParams.SensorName = sensorName;
216 SpawnParams.SimulateSensor = simulateSensor;
217 SpawnParams.Parent = GetOwner();
218
219 ASemanticSegmentationCamera* segmentationCamera = USensorFactory::SpawnSegmentationCamera(SpawnParams, cameraParameters);
220
221 if (!segmentationCamera)
222 {
223 return nullptr;
224 }
225
226 createdModel = segmentationCamera->GetSensorModel();
227
228 ASensor* CastedSensor = Cast<ASensor>(segmentationCamera);
229
230 AttachSensor(CastedSensor, attachToComponent, attachToBone);
231
232 AddSensorDestroyListener(CastedSensor);
233
234 SegmentationCameras.Emplace(segmentationCamera);
235 Sensors.Emplace(CastedSensor);
236
237 segmentationCamera->OnCameraWindowClosed.AddDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
238
239 OnSegmentationCameraSpawned.Broadcast(segmentationCamera);
240 OnSensorSpawned.Broadcast(CastedSensor);
241
242 return segmentationCamera;
243}
244
245AInstanceSegmentationCamera* USensorsManagerComponent::SpawnInstanceSegmentationCamera(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters CameraParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
246{
247 // Check if attachToComponent is not set but attachToBone is set
248 if (!attachToComponent && !attachToBone.IsNone())
249 {
250 attachToComponent = GetOwner()->GetRootComponent();
251 }
252
253 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
254
255 FSensorSpawnParameters SpawnParams;
256 SpawnParams.Transform = sensorTransform;
257 SpawnParams.SensorIdentifier = sensorIdentifier;
258 SpawnParams.SensorName = sensorName;
259 SpawnParams.SimulateSensor = simulateSensor;
260 SpawnParams.Parent = GetOwner();
261
263
264 if (!InstanceSegmentationCamera)
265 {
266 return nullptr;
267 }
268
269 createdModel = InstanceSegmentationCamera->GetSensorModel();
270
271 ASensor* CastedSensor = Cast<ASensor>(InstanceSegmentationCamera);
272
273 AttachSensor(CastedSensor, attachToComponent, attachToBone);
274
275 AddSensorDestroyListener(CastedSensor);
276
277 InstanceSegmentationCameras.Emplace(InstanceSegmentationCamera);
278 Sensors.Emplace(CastedSensor);
279
280 InstanceSegmentationCamera->OnCameraWindowClosed.AddDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
281
282 OnInstanceSegmentationCameraSpawned.Broadcast(InstanceSegmentationCamera);
283 OnSensorSpawned.Broadcast(CastedSensor);
284
286}
287
288ADepthCamera* USensorsManagerComponent::SpawnDepthCamera(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FDepthCameraParameters DepthCameraParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
289{
290 // Check if attachToComponent is not set but attachToBone is set
291 if (!attachToComponent && !attachToBone.IsNone())
292 {
293 attachToComponent = GetOwner()->GetRootComponent();
294 }
295
296 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
297
298 FSensorSpawnParameters SpawnParams;
299 SpawnParams.Transform = sensorTransform;
300 SpawnParams.SensorIdentifier = sensorIdentifier;
301 SpawnParams.SensorName = sensorName;
302 SpawnParams.SimulateSensor = simulateSensor;
303 SpawnParams.Parent = GetOwner();
304
305 ADepthCamera* depthCamera = USensorFactory::SpawnDepthCamera(SpawnParams, DepthCameraParameters);
306
307 if (!depthCamera)
308 {
309 return nullptr;
310 }
311
312 createdModel = depthCamera->GetSensorModel();
313
314 ASensor* CastedSensor = Cast<ASensor>(depthCamera);
315
316 AttachSensor(CastedSensor, attachToComponent, attachToBone);
317
318 AddSensorDestroyListener(CastedSensor);
319
320 DepthCameras.Emplace(depthCamera);
321 Sensors.Emplace(CastedSensor);
322
323 depthCamera->OnCameraWindowClosed.AddDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
324
325 OnDepthCameraSpawned.Broadcast(depthCamera);
326 OnSensorSpawned.Broadcast(CastedSensor);
327
328 return depthCamera;
329}
330
331ARadar* USensorsManagerComponent::SpawnRadar(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, FRadarParameters radarParameters, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
332{
333 // Check if attachToComponent is not set but attachToBone is set
334 if (!attachToComponent && !attachToBone.IsNone())
335 {
336 attachToComponent = GetOwner()->GetRootComponent();
337 }
338
339 FTransform sensorTransform = isTransformRelativeToComponent ? GetSensorAttachmentTransform(transform, attachToComponent, attachToBone) : transform;
340
341 FSensorSpawnParameters SpawnParams;
342 SpawnParams.Transform = sensorTransform;
343 SpawnParams.SensorIdentifier = sensorIdentifier;
344 SpawnParams.SensorName = sensorName;
345 SpawnParams.SimulateSensor = simulateSensor;
346 SpawnParams.Parent = GetOwner();
347
348 ARadar* radar = USensorFactory::SpawnRadar(SpawnParams, radarParameters);
349
350 if (!radar)
351 {
352 return nullptr;
353 }
354
355 createdModel = radar->GetSensorModel();
356
357 ASensor* CastedSensor = Cast<ASensor>(radar);
358
359 AttachSensor(CastedSensor, attachToComponent, attachToBone);
360
361 AddSensorDestroyListener(CastedSensor);
362
363 Radars.Emplace(radar);
364 Sensors.Emplace(CastedSensor);
365
366 OnRadarSpawned.Broadcast(radar);
367 OnSensorSpawned.Broadcast(CastedSensor);
368
369 return radar;
370}
371
372ASensor* USensorsManagerComponent::SpawnSensor(const FTransform& transform, bool isTransformRelativeToComponent, const FString sensorIdentifier, const FString sensorName, ESensorTypes sensorType, bool simulateSensor, ASensorModel*& createdModel, USceneComponent* attachToComponent, FName attachToBone)
373{
374 ASensor* sensor = nullptr;
375
376 switch (sensorType)
377 {
379 sensor = Cast<ASensor>(SpawnLidar(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FLidarParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
380 break;
382 sensor = Cast<ASensor>(SpawnCamera(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FCameraBaseParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
383 break;
385 sensor = Cast<ASensor>(SpawnThermalCamera(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FThermalCameraParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
386 break;
388 sensor = Cast<ASensor>(SpawnDVSCamera(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FDVSCameraParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
389 break;
391 sensor = Cast<ASensor>(SpawnSegmentationCamera(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FCameraBaseParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
392 break;
394 sensor = Cast<ASensor>(SpawnDepthCamera(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FDepthCameraParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
395 break;
397 sensor = Cast<ASensor>(SpawnRadar(transform, isTransformRelativeToComponent, sensorIdentifier, sensorName, FRadarParameters(), simulateSensor, createdModel, attachToComponent, attachToBone));
398 break;
399 default:
400 break;
401 }
402
403 return sensor;
404}
405
406void USensorsManagerComponent::DestroyLidar(ALidar* lidarToDestroy)
407{
408 if (lidarToDestroy)
409 {
410 FString sensorIdentifier = lidarToDestroy->GetSensorIdentifier();
411 Lidars.Remove(lidarToDestroy);
412
413 ASensor* CastedSensor = Cast<ASensor>(lidarToDestroy);
414
415 RemoveSensorDestroyListener(CastedSensor);
416
417 Sensors.Remove(CastedSensor);
418 DestroySensorActor(CastedSensor);
419
420 OnLidarDestroyed.Broadcast(sensorIdentifier);
421 OnSensorDestroyed.Broadcast(ESensorTypes::Lidar, sensorIdentifier);
422 }
423}
424
425void USensorsManagerComponent::DestroyCamera(ACamera* cameraToDestroy)
426{
427 if (cameraToDestroy)
428 {
429 FString sensorIdentifier = cameraToDestroy->GetSensorIdentifier();
430
431 cameraToDestroy->OnCameraWindowClosed.RemoveDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
432 Cameras.Remove(cameraToDestroy);
433
434 ASensor* CastedSensor = Cast<ASensor>(cameraToDestroy);
435
436 RemoveSensorDestroyListener(CastedSensor);
437
438 Sensors.Remove(CastedSensor);
439 DestroySensorActor(CastedSensor);
440
441 OnCameraDestroyed.Broadcast(sensorIdentifier);
442 OnSensorDestroyed.Broadcast(ESensorTypes::RGBCamera, sensorIdentifier);
443 }
444}
445
446void USensorsManagerComponent::DestroyThermalCamera(AThermalCamera* thermalCameraToDestroy)
447{
448 if (thermalCameraToDestroy)
449 {
450 FString sensorIdentifier = thermalCameraToDestroy->GetSensorIdentifier();
451
452 thermalCameraToDestroy->OnCameraWindowClosed.RemoveDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
453 ThermalCameras.Remove(thermalCameraToDestroy);
454
455 ASensor* CastedSensor = Cast<ASensor>(thermalCameraToDestroy);
456
457 RemoveSensorDestroyListener(CastedSensor);
458
459 Sensors.Remove(CastedSensor);
460 DestroySensorActor(CastedSensor);
461
462 OnThermalCameraDestroyed.Broadcast(sensorIdentifier);
463 OnSensorDestroyed.Broadcast(ESensorTypes::ThermalCamera, sensorIdentifier);
464 }
465}
466
467void USensorsManagerComponent::DestroyDVSCamera(ADVSCamera* DVSCameraToDestroy)
468{
469 if (DVSCameraToDestroy)
470 {
471 FString sensorIdentifier = DVSCameraToDestroy->GetSensorIdentifier();
472
473 DVSCameraToDestroy->OnCameraWindowClosed.RemoveDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
474 DVSCameras.Remove(DVSCameraToDestroy);
475
476 ASensor* CastedSensor = Cast<ASensor>(DVSCameraToDestroy);
477
478 RemoveSensorDestroyListener(CastedSensor);
479
480 Sensors.Remove(CastedSensor);
481 DestroySensorActor(CastedSensor);
482
483 OnDVSCameraDestroyed.Broadcast(sensorIdentifier);
484 OnSensorDestroyed.Broadcast(ESensorTypes::DVSCamera, sensorIdentifier);
485 }
486}
487
488void USensorsManagerComponent::DestroySegmentationCamera(ASemanticSegmentationCamera* segmentationCameraToDestroy)
489{
490 if (segmentationCameraToDestroy)
491 {
492 FString sensorIdentifier = segmentationCameraToDestroy->GetSensorIdentifier();
493
494 segmentationCameraToDestroy->OnCameraWindowClosed.RemoveDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
495 SegmentationCameras.Remove(segmentationCameraToDestroy);
496
497 ASensor* CastedSensor = Cast<ASensor>(segmentationCameraToDestroy);
498
499 RemoveSensorDestroyListener(CastedSensor);
500
501 Sensors.Remove(CastedSensor);
502 DestroySensorActor(CastedSensor);
503
504 OnSegmentationCameraDestroyed.Broadcast(sensorIdentifier);
505 OnSensorDestroyed.Broadcast(ESensorTypes::SemanticSegmentationCamera, sensorIdentifier);
506 }
507}
508
509void USensorsManagerComponent::DestroyDepthCamera(ADepthCamera* depthCameraToDestroy)
510{
511 if (depthCameraToDestroy)
512 {
513 FString sensorIdentifier = depthCameraToDestroy->GetSensorIdentifier();
514
515 depthCameraToDestroy->OnCameraWindowClosed.RemoveDynamic(this, &USensorsManagerComponent::OnCameraWindowClosed);
516 DepthCameras.Remove(depthCameraToDestroy);
517
518 ASensor* CastedSensor = Cast<ASensor>(depthCameraToDestroy);
519
520 RemoveSensorDestroyListener(CastedSensor);
521
522 Sensors.Remove(CastedSensor);
523 DestroySensorActor(CastedSensor);
524
525 OnDepthCameraDestroyed.Broadcast(sensorIdentifier);
526 OnSensorDestroyed.Broadcast(ESensorTypes::DepthCamera, sensorIdentifier);
527 }
528}
529
530void USensorsManagerComponent::DestroyRadar(ARadar* radarToDestroy)
531{
532 if (radarToDestroy)
533 {
534 FString sensorIdentifier = radarToDestroy->GetSensorIdentifier();
535
536 Radars.Remove(radarToDestroy);
537
538 ASensor* CastedSensor = Cast<ASensor>(radarToDestroy);
539
540 RemoveSensorDestroyListener(CastedSensor);
541
542 Sensors.Remove(CastedSensor);
543 DestroySensorActor(CastedSensor);
544
545 OnRadarDestroyed.Broadcast(sensorIdentifier);
546 OnSensorDestroyed.Broadcast(ESensorTypes::Radar, sensorIdentifier);
547 }
548}
549
550void USensorsManagerComponent::DestroySensor(ASensor* sensorToDestroy)
551{
552 if (sensorToDestroy)
553 {
554 ESensorTypes sensorType = sensorToDestroy->GetSensorType();
555
556 switch (sensorType)
557 {
559 DestroyLidar(Cast<ALidar>(sensorToDestroy));
560 break;
562 DestroyCamera(Cast<ACamera>(sensorToDestroy));
563 break;
565 DestroyThermalCamera(Cast<AThermalCamera>(sensorToDestroy));
566 break;
568 DestroyDVSCamera(Cast<ADVSCamera>(sensorToDestroy));
569 break;
571 DestroySegmentationCamera(Cast<ASemanticSegmentationCamera>(sensorToDestroy));
572 break;
574 DestroyDepthCamera(Cast<ADepthCamera>(sensorToDestroy));
575 break;
577 DestroyRadar(Cast<ARadar>(sensorToDestroy));
578 break;
579 default:
580 break;
581 }
582 }
583}
584
585void USensorsManagerComponent::DestroyAllSensors()
586{
587 TArray<ASensor*> SensorsTempArray = Sensors;
588
589 for (const auto& sensor : SensorsTempArray)
590 {
591 DestroySensor(sensor);
592 }
593}
594
595FTransform USensorsManagerComponent::SensorRelativeTransformToWorld(const FTransform& relativeTransform)
596{
597 AActor* owner = GetOwner();
598 return relativeTransform * owner->GetActorTransform();
599}
600
601FTransform USensorsManagerComponent::SensorRelativeTransformToComponentWorld(const FTransform& relativeTransform, const FTransform& componentTransformWorld)
602{
603 return relativeTransform * componentTransformWorld;
604}
605
606FTransform USensorsManagerComponent::GetSensorAttachmentTransform(const FTransform& sensorRelativeTransform, USceneComponent* attachToComponent, const FName& attachToBone, bool revertScaleToVectorOne)
607{
608 FTransform worldTransform;
609
610 // Calculate worldTransform based on attachToComponent availability
611 if (attachToComponent)
612 {
613 worldTransform = SensorRelativeTransformToComponentWorld(sensorRelativeTransform, attachToComponent->GetSocketTransform(attachToBone));
614 }
615 else
616 {
617 worldTransform = SensorRelativeTransformToWorld(sensorRelativeTransform);
618 }
619
620 if (revertScaleToVectorOne)
621 {
622 worldTransform.SetScale3D(FVector(1, 1, 1));
623 }
624
625 return worldTransform;
626}
627
628USceneComponent* USensorsManagerComponent::GetComponentByHierarchyName(FString hierarchyName)
629{
630
631 // Array to hold the split parts
632 TArray<FString> componentNames;
633
634 AActor* owner = GetOwner();
635
636 // Always fallback to owner root component
637 USceneComponent* matchingComponent = owner->GetRootComponent();
638
639 // Split the hierarchy
640 hierarchyName.ParseIntoArray(componentNames, *ComponentHierarchySeparator, true);
641
642 if (componentNames.Num() > 0)
643 {
644 // Search for the initial component using the first element in the split array
645 FString initialComponentName = componentNames[0];
646 USceneComponent* initialComponent = nullptr;
647 bool skipFirstComponentName = true;
648
649 TArray<USceneComponent*> ownerComponents;
650 owner->GetComponents<USceneComponent>(ownerComponents);
651
652 // Iterate over owner's components to find the initial component
653 for (USceneComponent* childComponent : ownerComponents)
654 {
655 if (childComponent && childComponent->GetName() == initialComponentName)
656 {
657 initialComponent = childComponent;
658 matchingComponent = initialComponent;
659 break;
660 }
661 }
662
663 if (!initialComponent)
664 {
665 // UE_LOG(LogTemp, Warning, TEXT("Initial component '%s' not found. Fallback to using owner root component"), *initialComponentName);
666
667 initialComponent = owner->GetRootComponent();
668
669 skipFirstComponentName = false;
670 }
671
672 USceneComponent* currentComponent = initialComponent;
673
674 // Check if there is more components to search through.
675 if (currentComponent && componentNames.Num() > (skipFirstComponentName ? 1 : 0))
676 {
677 // Flag to check if the component with the given name is found
678 bool bComponentFound = false;
679
680 // Iterate over each part of the hierarchy
681 for (int32 i = (skipFirstComponentName ? 1 : 0); i < componentNames.Num(); ++i)
682 {
683 FString componentName = componentNames[i];
684
685 bComponentFound = false;
686
687 // Loop through the child components of the current component
688 for (USceneComponent* childComponent : currentComponent->GetAttachChildren())
689 {
690 FString childComponentName = childComponent->GetName();
691
692 // Check if the child component's name matches the current part of the split string
693 // UE_LOG(LogTemp, Warning, TEXT("Compare between %s and %s."), *childComponentName, *componentName);
694
695 if (childComponent && childComponent->GetName() == componentName)
696 {
697 // Match found, update the current component
698 currentComponent = childComponent;
699 bComponentFound = true;
700 // break out of this inner component search loop
701 break;
702 }
703 }
704
705 // If the component was not found, can't continue the search anymore
706 if (!bComponentFound)
707 {
708 // Component not found, break out of the loop or handle as needed
709 UE_LOG(LogTemp, Warning, TEXT("Component '%s' not found. Breaking out of the loop"), *componentName);
710 // break out of the whole search loop
711 break;
712 }
713 }
714
715 // At this point, CurrentComponent should point to the component matching the hierarchy
716 if (bComponentFound)
717 {
718 FString foundComponentName = currentComponent->GetName();
719 matchingComponent = currentComponent;
720 }
721 else
722 {
723 // Component not found, handle as needed
724 UE_LOG(LogTemp, Warning, TEXT("Component not found"));
725 }
726 }
727 }
728
729 return matchingComponent;
730}
731
732void USensorsManagerComponent::AttachSensorToOwner(ASensor* sensor)
733{
734 // Owner is probably a vehicle
735 AActor* owner = GetOwner();
736
737 // Parent sensor to the owner of this component (vehicle)
738 AttachSensorToActor(sensor, owner);
739}
740
741void USensorsManagerComponent::AttachSensorToActor(ASensor* sensor, AActor* attachToActor)
742{
743 if (sensor && attachToActor)
744 {
745 sensor->AttachToActor(attachToActor, FAttachmentTransformRules::KeepWorldTransform);
746 sensor->AttachedToComponent = FString();
747 sensor->AttachedToBone = NAME_None;
748 }
749}
750
751void USensorsManagerComponent::AttachSensorToBone(ASensor* sensor, USceneComponent* attachToComponent, const FName& boneName)
752{
753 if (!sensor || !attachToComponent)
754 {
755 return;
756 }
757
758 if (IsValid(attachToComponent))
759 {
760 sensor->AttachToComponent(attachToComponent, FAttachmentTransformRules::KeepWorldTransform, boneName);
761
762 USceneComponent* currentParentComponent = attachToComponent->GetAttachParent();
763
764 // FString fullComponentName = FString();
765 FString fullComponentName = attachToComponent->GetName();
766
767 // If this does not have a parent, this is the highest attach parent which we will not include
768 if (currentParentComponent)
769 {
770 fullComponentName = attachToComponent->GetName();
771
772 // Skip the highest attach parent which should be the owner itself. Having only the currentParentComponent-check would include the owner as well.
773 while (currentParentComponent && currentParentComponent->GetAttachParent())
774 {
775 FString ParentComponentName = currentParentComponent->GetName();
776 // Prepend the parent's name into the full component name like ParentComponent_|_ChildComponent.
777 // This could have been a TArray<FString> because that can be also saved easily
778 fullComponentName = FString::Format(TEXT("{0}{1}{2}"), { *ParentComponentName, *ComponentHierarchySeparator, *fullComponentName });
779
780 // Move to the next parent
781 currentParentComponent = currentParentComponent->GetAttachParent();
782 }
783 }
784
785 sensor->AttachedToComponent = fullComponentName;
786 sensor->AttachedToBone = boneName;
787
788 //UE_LOG(LogTemp, Warning, TEXT("Full component name: %s"), *fullComponentName);
789 }
790}
791
792void USensorsManagerComponent::AttachSensor(ASensor* sensor, USceneComponent* attachToComponent, const FName& boneName)
793{
794 // If attachToComponent is valid OR if the boneName is set.
795 if (IsValid(attachToComponent) || !boneName.IsNone())
796 {
797 AttachSensorToBone(sensor, attachToComponent, boneName);
798 }
799 else
800 {
801 AttachSensorToOwner(sensor);
802 }
803}
804
805void USensorsManagerComponent::DestroySensorActor(AActor* sensorActor)
806{
807 if (!sensorActor)
808 {
809 return;
810 }
811
812 TArray<AActor*> attachedActors;
813 sensorActor->GetAttachedActors(attachedActors);
814
815 for (AActor* CActor : attachedActors)
816 {
817 if (CActor)
818 {
819 CActor->Destroy();
820 }
821 }
822
823 sensorActor->Destroy();
824}
825
826void USensorsManagerComponent::LoadPreset(FVehicleSensorsPreset preset, bool simulateSensors)
827{
828 DestroyAllSensors();
829
830 for (const auto& LidarData : preset.LidarDatas)
831 {
832 FSensorPreset sensorData = LidarData.SensorData;
833 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
834
835 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
836 FName attachToBone = sensorData.AttachedToBone;
837
838 ASensorModel* createdModel = nullptr;
839
840 SpawnLidar(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, LidarData.LidarParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
841 }
842
843 for (const auto& RadarData : preset.RadarDatas)
844 {
845 FSensorPreset sensorData = RadarData.SensorData;
846 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
847
848 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
849 FName attachToBone = sensorData.AttachedToBone;
850
851 ASensorModel* createdModel = nullptr;
852
853 SpawnRadar(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, RadarData.RadarParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
854 }
855
856 for (const auto& CameraData : preset.CameraDatas)
857 {
858 FSensorPreset sensorData = CameraData.SensorData;
859
860 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
861
862 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
863 FName attachToBone = sensorData.AttachedToBone;
864
865 ASensorModel* createdModel = nullptr;
866
867 SpawnCamera(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, CameraData.CameraParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
868 }
869
870 for (const auto& CameraData : preset.ThermalCameraDatas)
871 {
872 FSensorPreset sensorData = CameraData.SensorData;
873
874 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
875
876 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
877 FName attachToBone = sensorData.AttachedToBone;
878
879 ASensorModel* createdModel = nullptr;
880
881 SpawnThermalCamera(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, CameraData.CameraParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
882 }
883
884 for (const auto& CameraData : preset.DVSCameraDatas)
885 {
886 FSensorPreset sensorData = CameraData.SensorData;
887
888 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
889
890 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
891 FName attachToBone = sensorData.AttachedToBone;
892
893 ASensorModel* createdModel = nullptr;
894
895 SpawnDVSCamera(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, CameraData.DVSCameraParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
896 }
897
898 for (const auto& CameraData : preset.SegmentationCameraDatas)
899 {
900 FSensorPreset sensorData = CameraData.SensorData;
901
902 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
903
904 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
905 FName attachToBone = sensorData.AttachedToBone;
906
907 ASensorModel* createdModel = nullptr;
908
909 SpawnSegmentationCamera(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, CameraData.CameraParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
910 }
911
912 for (const auto& CameraData : preset.DepthCameraDatas)
913 {
914 FSensorPreset sensorData = CameraData.SensorData;
915
916 FTransform sensorTransform(sensorData.Rotation, sensorData.Location);
917
918 USceneComponent* attachToComponent = GetComponentByHierarchyName(sensorData.AttachedToComponent);
919 FName attachToBone = sensorData.AttachedToBone;
920
921 ASensorModel* createdModel = nullptr;
922
923 SpawnDepthCamera(sensorTransform, true, sensorData.SensorIdentifier, sensorData.SensorName, CameraData.CameraParameters, simulateSensors, createdModel, attachToComponent, attachToBone);
924 }
925}
926
927FVehicleSensorsPreset USensorsManagerComponent::GetSensorsAsPreset()
928{
930
931 for (const auto& Lidar : Lidars)
932 {
933 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(Lidar));
934 FLidarPreset lidarPreset;
935 lidarPreset.SensorData = sensorPreset;
936 lidarPreset.LidarParameters = Lidar->GetLidarParameters();
937
938 preset.LidarDatas.Add(lidarPreset);
939 }
940
941 for (const auto& Radar : Radars)
942 {
943 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(Radar));
944 FRadarPreset radarPreset;
945 radarPreset.SensorData = sensorPreset;
946 radarPreset.RadarParameters = Radar->GetRadarParameters();
947
948 preset.RadarDatas.Add(radarPreset);
949 }
950
951 for (const auto& Camera : Cameras)
952 {
953 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(Camera));
954 FCameraPreset cameraPreset;
955 cameraPreset.SensorData = sensorPreset;
956 cameraPreset.CameraParameters = Camera->GetCameraParameters();
957 preset.CameraDatas.Add(cameraPreset);
958 }
959
960 for (const auto& ThermalCamera : ThermalCameras)
961 {
962 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(ThermalCamera));
963 FThermalCameraPreset cameraPreset;
964 cameraPreset.SensorData = sensorPreset;
965 cameraPreset.CameraParameters = ThermalCamera->GetThermalCameraParameters();
966 preset.ThermalCameraDatas.Add(cameraPreset);
967 }
968
969 for (const auto& DVSCamera : DVSCameras)
970 {
971 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(DVSCamera));
972 FDVSCameraPreset cameraPreset;
973 cameraPreset.SensorData = sensorPreset;
974 cameraPreset.DVSCameraParameters = DVSCamera->GetDVSCameraParameters();
975 preset.DVSCameraDatas.Add(cameraPreset);
976 }
977
978 for (const auto& SegmentationCamera : SegmentationCameras)
979 {
980 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(SegmentationCamera));
982 cameraPreset.SensorData = sensorPreset;
983 cameraPreset.CameraParameters = SegmentationCamera->GetCameraParameters();
984 preset.SegmentationCameraDatas.Add(cameraPreset);
985 }
986
987 for (const auto& DepthCamera : DepthCameras)
988 {
989 FSensorPreset sensorPreset = GetSensorPreset(Cast<ASensor>(DepthCamera));
990 FDepthCameraPreset cameraPreset;
991 cameraPreset.SensorData = sensorPreset;
992 cameraPreset.CameraParameters = DepthCamera->GetDepthCameraParameters();
993 preset.DepthCameraDatas.Add(cameraPreset);
994 }
995
996 return preset;
997}
998
999FSensorPreset USensorsManagerComponent::GetSensorPreset(ASensor* sensor)
1000{
1001 FSensorPreset sensorPreset;
1002
1003 FTransform relativeTransform = sensor->GetRootComponent()->GetRelativeTransform();
1004
1005 sensorPreset.Location = relativeTransform.GetLocation();
1006 sensorPreset.Rotation = relativeTransform.Rotator();
1007 sensorPreset.SensorIdentifier = sensor->GetSensorIdentifier();
1008 sensorPreset.SensorName = sensor->GetSensorName();
1009 sensorPreset.SensorType = sensor->GetSensorType();
1010 sensorPreset.AttachedToComponent = sensor->AttachedToComponent;
1011 sensorPreset.AttachedToBone = sensor->AttachedToBone;
1012
1013 return sensorPreset;
1014}
1015
1016void USensorsManagerComponent::AddSensorDestroyListener(ASensor* sensor)
1017{
1018 if (sensor)
1019 {
1020 sensor->OnSensorDestroy.AddDynamic(this, &USensorsManagerComponent::OnSensorDestroy);
1021 }
1022}
1023
1024void USensorsManagerComponent::RemoveSensorDestroyListener(ASensor* sensor)
1025{
1026 if (sensor)
1027 {
1028 sensor->OnSensorDestroy.RemoveDynamic(this, &USensorsManagerComponent::OnSensorDestroy);
1029 }
1030}
1031
1032void USensorsManagerComponent::OnCameraWindowClosed(ACamera* camera)
1033{
1034 if (camera)
1035 {
1036 ASensor* CastedSensor = Cast<ASensor>(camera);
1037 DestroySensor(CastedSensor);
1038 }
1039}
1040
1041void USensorsManagerComponent::OnSensorDestroy(ASensor* sensor)
1042{
1043 if (sensor)
1044 {
1045 // Calling this will actually try to remove the sensor again but I think that is OK
1046 DestroySensor(sensor);
1047 }
1048}
ESensorTypes
Definition: SensorTypes.h:15
@ SemanticSegmentationCamera
@ InstanceSegmentationCamera
Definition: Camera.h:53
FCameraDelegate_OnWindowClosed OnCameraWindowClosed
Definition: Camera.h:141
Definition: Lidar.h:35
Definition: Radar.h:26
Definition: Sensor.h:45
FSensorDestroy OnSensorDestroy
Definition: Sensor.h:283
FString GetSensorIdentifier() const
Definition: Sensor.h:75
FString GetSensorName() const
Definition: Sensor.h:96
virtual ESensorTypes GetSensorType() const
Definition: Sensor.h:65
FString AttachedToComponent
Definition: Sensor.h:286
ASensorModel * GetSensorModel() const
Definition: Sensor.h:181
FName AttachedToBone
Definition: Sensor.h:289
static AInstanceSegmentationCamera * SpawnInstanceSegmentationCamera(const FSensorSpawnParameters SpawnParameters, FCameraBaseParameters SensorParameters)
static AThermalCamera * SpawnThermalCamera(const FSensorSpawnParameters SpawnParameters, FThermalCameraParameters SensorParameters)
static ASemanticSegmentationCamera * SpawnSegmentationCamera(const FSensorSpawnParameters SpawnParameters, FCameraBaseParameters SensorParameters)
static ARadar * SpawnRadar(const FSensorSpawnParameters SpawnParameters, FRadarParameters SensorParameters)
static ACamera * SpawnCamera(const FSensorSpawnParameters SpawnParameters, FCameraBaseParameters SensorParameters)
static ADepthCamera * SpawnDepthCamera(const FSensorSpawnParameters SpawnParameters, FDepthCameraParameters SensorParameters)
static ADVSCamera * SpawnDVSCamera(const FSensorSpawnParameters SpawnParameters, FDVSCameraParameters SensorParameters)
static ALidar * SpawnLidarSensor(const FSensorSpawnParameters SpawnParameters, FLidarParameters SensorParameters)
FCameraBaseParameters CameraParameters
Definition: CameraPreset.h:30
FSensorPreset SensorData
Definition: CameraPreset.h:24
FSensorPreset SensorData
FDVSCameraParameters DVSCameraParameters
FDepthCameraParameters CameraParameters
FSensorPreset SensorData
FSensorPreset SensorData
Definition: LidarPreset.h:24
FLidarParameters LidarParameters
Definition: LidarPreset.h:30
FSensorPreset SensorData
Definition: RadarPreset.h:24
FRadarParameters RadarParameters
Definition: RadarPreset.h:30
FRotator Rotation
Definition: SensorPreset.h:50
FString SensorName
Definition: SensorPreset.h:32
ESensorTypes SensorType
Definition: SensorPreset.h:38
FString SensorIdentifier
Definition: SensorPreset.h:26
FName AttachedToBone
Definition: SensorPreset.h:56
FString AttachedToComponent
Definition: SensorPreset.h:53
FVector Location
Definition: SensorPreset.h:44
FThermalCameraParameters CameraParameters
TArray< FRadarPreset > RadarDatas
TArray< FLidarPreset > LidarDatas
TArray< FThermalCameraPreset > ThermalCameraDatas
TArray< FDVSCameraPreset > DVSCameraDatas
TArray< FSemanticSegmentationCameraPreset > SegmentationCameraDatas
TArray< FCameraPreset > CameraDatas
TArray< FDepthCameraPreset > DepthCameraDatas