Agrarsense
Camera.h
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
6#pragma once
7
8#include "CoreMinimal.h"
10
11#include "Engine/TextureRenderTarget2D.h"
12#include "Kismet/KismetSystemLibrary.h"
13#include "Delegates/IDelegateInstance.h"
14#include "Materials/MaterialInstanceDynamic.h"
15#include "Components/SceneCaptureComponent2D.h"
16#include "Engine/Texture2D.h"
17#include "FrameGrabber.h"
18#include "Materials/Material.h"
19
23
24#include "ROSIntegration/Public/sensor_msgs/Image.h"
25
26#include "Camera.generated.h"
27
28class AGeoReferencingSystem;
29class UROSHandler;
30class UWidget;
31
35DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FCameraDelegate_OnWindowClosed, ACamera*, Camera);
36
40DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FCameraDelegate_OnWindowResized, ACamera*, Camera, FCameraBaseParameters, Params);
41
50UCLASS()
51class AGRARSENSE_API ACamera : public ASensor
52{
53 GENERATED_BODY()
54
55public:
56
61 ACamera(const FObjectInitializer& ObjectInitializer);
62
67 virtual void Init(FCameraBaseParameters parameters, bool SimulateSensor = true);
68
73 virtual ESensorTypes GetSensorType() const override
74 {
76 }
77
82 UFUNCTION(BlueprintCallable)
83 void ChangeCameraParameters(FCameraBaseParameters newParameters);
84
89 UFUNCTION(BlueprintCallable, BlueprintPure)
90 FCameraBaseParameters GetCameraParameters()
91 {
92 return CameraParameters;
93 }
94
100 void AddPostProcessingMaterial(const FString& Path, float Weight = 1.0f);
101
106 void RemovePostProcessingMaterial(UMaterial* Material);
107
112 UFUNCTION(BlueprintCallable)
113 USceneCaptureComponent2D* GetCaptureComponent2D() const
114 {
115 return CaptureComponent2D;
116 }
117
122 UFUNCTION(BlueprintCallable)
123 UTextureRenderTarget2D* GetCaptureRenderTarget2D() const
124 {
125 return CaptureRenderTarget;
126 }
127
132 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "FrameGrabber")
133 TEnumAsByte<ETextureRenderTargetFormat> TextureFormat = ETextureRenderTargetFormat::RTF_RGBA8;
134
139 UPROPERTY(BlueprintAssignable)
140 FCameraDelegate_OnWindowClosed OnCameraWindowClosed;
141
146 UPROPERTY(BlueprintAssignable)
147 FCameraDelegate_OnWindowResized OnCameraWindowResized;
148
152 UFUNCTION(BlueprintCallable)
153 void AddWidgetToWindow(UWidget* WidgetToAdd);
154
158 UFUNCTION(BlueprintCallable)
159 void RemoveWidgetFromWindow(UWidget* WidgetToRemove);
160
164 UFUNCTION(BlueprintCallable)
165 void SetShadowRendering(bool RenderShadows);
166
170 UFUNCTION(BlueprintCallable)
171 void SetTemporalAA(bool SetTemporal);
172
176 UFUNCTION(BlueprintCallable)
177 void ResizeCamera(int Width = 1280, int Height = 720);
178
182 UFUNCTION(BlueprintCallable)
183 void SaveCurrentFrameToDisk()
184 {
185 if (!CameraParameters.SaveImageToDisk)
186 {
187 SaveCurrentFrameToDiskRequested = true;
188 }
189 }
190
191 UFUNCTION(BlueprintCallable)
192 int GetCameraWidth() const
193 {
194 return ImageWidth;
195 }
196
197 UFUNCTION(BlueprintCallable)
198 int GetCameraHeight() const
199 {
200 return ImageHeight;
201 }
202
206 virtual FString GetParametersAsString() const override
207 {
208 return StructToString(CameraParameters);
209 }
210
211protected:
212
213 virtual void BeginPlay() override;
214
215 virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
216
217 /*
218 * Should we use ATicMananger to tick in parallel or use post actor tick
219 */
220 bool UseParallelLateTick = true;
221
222 FDelegateHandle OnPreTickDelegate;
223 virtual void PreActorTick(UWorld* World, ELevelTick TickType, float DeltaSeconds);
224
225 FDelegateHandle OnPostTickDelegate;
226 virtual void EndOfFrame(UWorld* World, ELevelTick TickType, float DeltaSeconds);
227 virtual void EndOfFrameParellel(float DeltaTime);
228
229 /*
230 * Override function to all other camera variants if buffer editing is needed
231 */
232 virtual void AddProcessingToFrameBuffer(TArray<FColor>& buffer);
233
240 virtual void SendImageDataToROS(const TArray<FColor>& FrameBuffer, int32 Width, int32 Height);
241
242 UFUNCTION()
243 void HidePrimitiveComponent(UPrimitiveComponent* PrimitiveComponent);
244
251 void SaveImageToDisk(const TArray<FColor> FrameBuffer, int32 Width, int32 Height);
252
253 void SaveCameraMetaDataToDisk(const FString& ImageName);
254
255 void CreateLogFile() override;
256
262 FString CameraName = "Camera ";
263
264 FString FilePrefix = "Data/Camera_";
265
266 UPROPERTY()
267 UTextureRenderTarget2D* CaptureRenderTarget = nullptr;
268
269 UPROPERTY(VisibleAnywhere)
270 USceneCaptureComponent2D* CaptureComponent2D = nullptr;
271
276 UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "FrameGrabber")
277 UTexture2D* CaptureFrameTexture;
278
279 TSharedPtr<FUnrealWindow> UnrealWindow;
280
281 FTickEntry TickEntry;
282
283 TWeakObjectPtr<UMaterialInstanceDynamic> IceMaterialInstance;
284
285 TWeakObjectPtr<UMaterial> PhysicLensDistortion;
286
287private:
288
292 void StartFrameGrabber();
293
297 void ReleaseFrameGrabber();
298
302 void FrameGrabberCapture();
303
308 void OnWindowClosed(const TSharedRef<SWindow>& Window);
309
313 TSharedPtr<FFrameGrabber>FrameGrabber;
314
318 void SetupCamera(FCameraBaseParameters parameters);
319
323 bool ShouldSimulate(const float DeltaSeconds);
324
329 UPROPERTY(VisibleAnywhere, Category = "Sensor")
330 FCameraBaseParameters CameraParameters;
331
333
334 UPROPERTY()
335 AGeoReferencingSystem* GeoReferencingSystem = nullptr;
336
337 bool ParametersChanged = false;
338
339 FOnWindowClosed OnWindowClosedDelegate;
340
341 TSharedPtr<ROSMessages::sensor_msgs::Image> ImageMsg;
342
343 int ImageWidth = 1280;
344 int ImageHeight = 720;
345
346 bool SaveCurrentFrameToDiskRequested = false;
347
348 bool ShouldSimulateCamera = true;
349
350 float CameraFrameRate = 0.0f;
351 float FrameRateTimer = 0.0f;
352
353 int ImageNumber = 0;
354
355};
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FCameraDelegate_OnWindowResized, ACamera *, Camera, FCameraBaseParameters, Params)
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FCameraDelegate_OnWindowClosed, ACamera *, Camera)
ESensorTypes
Definition: SensorTypes.h:15
Definition: Camera.h:52
virtual ESensorTypes GetSensorType() const override
Definition: Camera.h:73
virtual FString GetParametersAsString() const override
Definition: Camera.h:206
FDelegateHandle OnPostTickDelegate
Definition: Camera.h:225
FDelegateHandle OnPreTickDelegate
Definition: Camera.h:222
Definition: Sensor.h:44