Agrarsense
PhotoCapture.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 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#include "PhotoCapture.h"
7
9
10APhotoCapture::APhotoCapture(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
11{
12 RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
13 PrimaryActorTick.bCanEverTick = true;
14 PrimaryActorTick.bStartWithTickEnabled = false;
15 PrimaryActorTick.TickGroup = TG_PrePhysics;
16 SetActorTickEnabled(false);
17}
18
20{
21 CaptureData = NewCaptureData;
22
23 if (CaptureData.CapturePositions.Num() != 0 && CaptureData.Cameras.Num() != 0)
24 {
25 for (int32 i = 0; i < CaptureData.Cameras.Num(); i++)
26 {
27 ACamera* CameraPtr = CaptureData.Cameras[i];
28 if (CameraPtr)
29 {
30 FCameraBaseParameters params = CameraPtr->GetCameraParameters();
31 params.SaveImageToDisk = false;
32 params.SendDataToROS = false;
33 CameraPtr->ChangeCameraParameters(params);
34 }
35 }
36
38
39 // Enable This Actor tick with delay
40 FTimerHandle Handle;
41 GetWorld()->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([this]
42 {
44 SetActorTickEnabled(true);
45 }), 0.1f, false);
46 }
47 else
48 {
50 Destroy();
51 }
52}
53
55{
56 Super::BeginPlay();
57}
58
59void APhotoCapture::Tick(float DeltaTime)
60{
61 Super::Tick(DeltaTime);
62 Capture();
63}
64
65void APhotoCapture::EndPlay(const EEndPlayReason::Type EndPlayReason)
66{
67 Super::EndPlay(EndPlayReason);
68
69 CaptureData.Cameras.Empty();
71}
72
74{
75 // A bit convoluted logic due to its not guaranteed that we can move actor
76 // and save the camera image in the same frame.
77
79 {
83 {
84 FrameCounter = 0;
86 {
89 }
90 }
91 }
92
94 {
95 // Disable tick
96 SetActorTickEnabled(false);
97
98 // Destroy this Actor with delay so Camera sensors has time to save the last images
99 FTimerHandle Handle;
100 GetWorld()->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([this]
101 {
103 Destroy();
104 }), 1.0f, false);
105 }
106}
107
108void APhotoCapture::UpdateCameraPositions(const FTransform& Transform)
109{
110 for (int32 i = 0; i < CaptureData.Cameras.Num(); i++)
111 {
112 ACamera* CameraPtr = CaptureData.Cameras[i];
113 if (CameraPtr)
114 {
115 CameraPtr->SetActorTransform(Transform);
116 }
117 }
118}
119
121{
122 for (int32 i = 0; i < CaptureData.Cameras.Num(); i++)
123 {
124 ACamera* CameraPtr = CaptureData.Cameras[i];
125 if (CameraPtr)
126 {
127 CameraPtr->SaveCurrentFrameToDisk();
128 }
129 }
130}
131
133{
134 for (int32 i = 0; i < CaptureData.Cameras.Num(); i++)
135 {
136 ACamera* CameraPtr = CaptureData.Cameras[i];
137 if (CameraPtr)
138 {
139 CameraPtr->Destroy();
140 }
141 }
142}
Definition: Camera.h:52
void ChangeCameraParameters(FCameraBaseParameters newParameters)
Definition: Camera.cpp:50
void SaveCurrentFrameToDisk()
Definition: Camera.h:183
FCameraBaseParameters GetCameraParameters()
Definition: Camera.h:90
virtual void Tick(float DeltaTime) override
void DestroyCameras()
virtual void BeginPlay() override
void CaptureImages()
APhotoCapture(const FObjectInitializer &ObjectInitializer)
void SetupPhotoCapture(FCaptureData NewCaptureData)
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
void UpdateCameraPositions(const FTransform &Transform)
int32 FrameCounter
Definition: PhotoCapture.h:75
FCaptureData CaptureData
Definition: PhotoCapture.h:73
int32 FramesToKeepInSamePosition
Definition: PhotoCapture.h:30
TArray< FTransform > CapturePositions
Definition: PhotoCapture.h:24
TArray< ACamera * > Cameras
Definition: PhotoCapture.h:21
int32 CurrentCaptureIndex
Definition: PhotoCapture.h:27