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