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* lidar = nullptr;
95 lidar = SpawnSensor<ALidar>(transform, sensorIdentifier, sensorName, true);
96 if (lidar)
97 {
98 lidar->Init(lidarParameters, SimulateSensor);
99 }
100
101 return lidar;
102}
103
104ACamera* USensorFactory::SpawnCamera(const FTransform& transform, const FString sensorIdentifier, const FString sensorName, FCameraBaseParameters cameraParameters, bool SimulateSensor)
105{
106 ACamera* camera = nullptr;
107 camera = SpawnSensor<ACamera>(transform, sensorIdentifier, sensorName, true);
108 if (camera)
109 {
110 camera->Init(cameraParameters, SimulateSensor);
111 }
112
113 return camera;
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{
142 ASemanticSegmentationCamera* segmentationCamera = nullptr;
143 segmentationCamera = SpawnSensor<ASemanticSegmentationCamera>(transform, sensorIdentifier, sensorName, true);
144 if (segmentationCamera)
145 {
146 segmentationCamera->Init(cameraParameters, SimulateSensor);
147 }
148
149 return segmentationCamera;
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* radar = nullptr;
167 radar = SpawnSensor<ARadar>(transform, sensorIdentifier, sensorName, true);
168 if (radar)
169 {
170 radar->Init(radarParameters, SimulateSensor);
171 }
172
173 return radar;
174}
175
176ACollisionSensor* USensorFactory::SpawnCollisionSensor(const FTransform& transform, AActor* Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor)
177{
178 ACollisionSensor* collisionSensor = nullptr;
179 collisionSensor = SpawnSensor<ACollisionSensor>(transform, sensorIdentifier, sensorName, false);
180 if (collisionSensor)
181 {
182 collisionSensor->Init(Owner, SimulateSensor);
183 }
184
185 return collisionSensor;
186}
187
188ATransformSensor* USensorFactory::SpawnTransformSensor(const FTransform& transform, AActor* Owner, const FString sensorIdentifier, const FString sensorName, bool SimulateSensor)
189{
190 ATransformSensor* transformSensor = nullptr;
191 transformSensor = SpawnSensor<ATransformSensor>(transform, sensorIdentifier, sensorName, false);
192 if (transformSensor)
193 {
194 transformSensor->Init(Owner, SimulateSensor);
195 }
196
197 return transformSensor;
198}
199
200AOverlapSensor* USensorFactory::SpawnOverlapSensor(const FTransform& transform, FOverlapSensorParameters Parameters, const FString sensorIdentifier, const FString sensorName)
201{
202 AOverlapSensor* OverlapSensor = nullptr;
203 OverlapSensor = SpawnSensor<AOverlapSensor>(transform, sensorIdentifier, sensorName, false);
204 if (OverlapSensor)
205 {
206 OverlapSensor->Init(Parameters);
207 }
208
209 return OverlapSensor;
210}
211
213{
214 ASensorModel* spawnedModel = nullptr;
215
216 if (!attachTo)
217 {
218 return spawnedModel;
219 }
220
222
223 if (SensorMap)
224 {
225 ESensorTypes sensorType = attachTo->GetSensorType();
226
227 TSubclassOf<ASensorModel> modelClass = nullptr;
228
229 bool matchFound;
230
231 USensorDataAsset* sensorData = SensorMap->GetAssetDataBySensorType(sensorType, matchFound);
232
233 if (matchFound && sensorData)
234 {
235 modelClass = sensorData->ModelClass;
236 }
237
238 if (modelClass)
239 {
240 spawnedModel = SpawnModelClass(modelClass, attachTo);
241 }
242 }
243
244 return spawnedModel;
245}
246
247ASensorModel* USensorFactory::SpawnModelClass(TSubclassOf<ASensorModel> modelClass, ASensor* attachTo)
248{
249 ASensorModel* createdModelActor = nullptr;
250
251 if (!attachTo)
252 {
253 return createdModelActor;
254 }
255
256 UWorld* World = nullptr;
257 if (GEngine && GEngine->GameViewport)
258 {
259 World = GEngine->GameViewport->GetWorld();
260 }
261
262 if (World)
263 {
264 FVector sensorLocation = attachTo->GetActorLocation();
265 createdModelActor = World->SpawnActor<ASensorModel>(modelClass);
266
267 if (createdModelActor)
268 {
269 createdModelActor->AttachToActor(attachTo, FAttachmentTransformRules::SnapToTargetIncludingScale);
270
271 attachTo->SetSensorModel(createdModelActor);
272 createdModelActor->SetAttachedToSensor(attachTo);
273 createdModelActor->InitAfterSpawn();
274 }
275 }
276
277 return createdModelActor;
278}
279
280template <typename T>
281T* USensorFactory::SpawnSensor(const FTransform& transform, const FString& sensorIdentifier, const FString& sensorName, bool SpawnSensorModel)
282{
283 UWorld* World = nullptr;
284
285 if (GEngine && GEngine->GameViewport)
286 {
287 // This might only fail if called in Editor without play mode.
288 World = GEngine->GameViewport->GetWorld();
289 }
290
291 T* Sensor = nullptr;
292
293 if (World)
294 {
295 Sensor = World->SpawnActorDeferred<T>(T::StaticClass(), transform);
296 if (Sensor)
297 {
298 SetSensorIdentifierAndNameWithFallbacks(Sensor, sensorIdentifier, sensorName);
299 Sensor->FinishSpawning(transform);
300
301 if (SpawnSensorModel)
302 {
303 SpawnModelClassForSensor(Cast<ASensor>(Sensor));
304 }
305 }
306 }
307
308 return Sensor;
309}
ESensorTypes
Definition: SensorTypes.h:15
Definition: Camera.h:52
virtual void Init(FCameraBaseParameters parameters, bool SimulateSensor=true)
Definition: Camera.cpp:80
void Init(AActor *NewOwner, bool SimulateSensor=true)
void DVSInit(FDVSCameraParameters Parameters, bool SimulateSensor=true)
Definition: DVSCamera.cpp:39
void DepthInit(FDepthCameraParameters Parameters, bool SimulateSensor=true)
Definition: DepthCamera.cpp:15
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 Init(FCameraBaseParameters parameters, bool SimulateSensor=true) override
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 ThermalInit(FThermalCameraParameters Parameters, bool SimulateSensor=true)
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)