Ocean
devices/arkit/Utilities.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_DEVICES_ARKIT_UTILITIES_H
9 #define META_OCEAN_DEVICES_ARKIT_UTILITIES_H
10 
12 
13 #include "ocean/base/Frame.h"
14 
15 #include "ocean/math/AnyCamera.h"
18 
19 #include <ARKit/ARKit.h>
20 
21 namespace Ocean
22 {
23 
24 namespace Devices
25 {
26 
27 namespace ARKit
28 {
29 
30 /**
31  * This class impelements utility functions.
32  */
33 class OCEAN_DEVICES_ARKIT_EXPORT Utilities
34 {
35  public:
36 
37  /**
38  * Converts a ARKit simd_float3x3 matrix to an Ocean SquareMatrix3 matrix.
39  * @param simdTransform The ARKit simd_float3x3 matrix to convert
40  * @return The resulting Ocean SquareMatrix3 matrix
41  * @tparam T The data type of the matrix elements, either 'float' or 'double'
42  */
43  template <typename T = Scalar>
44  static SquareMatrixT3<T> toSquareMatrix3(const simd_float3x3& simdTransform);
45 
46  /**
47  * Converts a ARKit simd_float4x4 matrix to an Ocean HomogenousMatrix4 matrix.
48  * @param simdTransform The ARKit simd_float4x4 matrix to convert
49  * @return The resulting Ocean HomogenousMatrix4 matrix
50  * @tparam T The data type of the matrix elements, either 'float' or 'double'
51  */
52  template <typename T = Scalar>
53  static HomogenousMatrixT4<T> toHomogenousMatrix4(const simd_float4x4& simdTransform);
54 
55  /**
56  * Converts an Ocean HomogenousMatrix4 matrix to an ARKit simd_float4x4 matrix.
57  * @param matrix The Ocean HomogenousMatrix4 matrix to convert
58  * @return The resulting ARKit simd_float4x4 matrix
59  * @tparam T The data type of the matrix elements, either 'float' or 'double'
60  */
61  template <typename T = Scalar>
62  static simd_float4x4 fromHomogenousMatrix4(const HomogenousMatrixT4<T>& matrix);
63 
64  /**
65  * Extracts the color image from an ARKit frame.
66  * @param arFrame The ARKit frame from which the color image will be extracted, must be valid
67  * @param copyData True, to copy the data; False, to only use the memory of the ARKit frame
68  * @param preferredPixelFormat Optional preferred pixel format of the resulting frame, however the resulting frame can have any pixel format, FORMAT_UNDEFINED if not of interest
69  */
70  static Frame extractFrame(const ARFrame* arFrame, const bool copyData = true, const FrameType::PixelFormat preferredPixelFormat = FrameType::FORMAT_UNDEFINED);
71 
72  /**
73  * Extracts the depth image from an ARKit frame, if existing.
74  * @param arFrame The ARKit frame from which the depth image will be extracted, must be valid
75  * @param confidenceFrame Optional resulting confidence frame, if existing, nullptr if not of interest
76  * @param copyData True, to copy the data; False, to only use the memory of the ARKit frame
77  * @return The resulting depth frame, invalid if no depth information was available
78  */
79  static Frame extractDepthFrame(const ARFrame* arFrame, Frame* confidenceFrame = nullptr, const bool copyData = true);
80 
81  /**
82  * Extracts the camera profile of the color image from an ARKit frame.
83  * @param arFrame The ARKit frame from which the camera profile will be extracted, must be valid
84  * @return The resulting camera profile, invalid if the camera profile could not be extracted
85  */
86  template <typename T = Scalar>
87  static SharedAnyCameraT<T> extractCameraModel(const ARFrame* arFrame);
88 };
89 
90 template <typename T>
91 SquareMatrixT3<T> Utilities::toSquareMatrix3(const simd_float3x3& simdTransform)
92 {
93  if constexpr (std::is_same<T, float>::value)
94  {
95  SquareMatrixF3 matrix;
96 
97  memcpy(matrix.data() + 0, &simdTransform.columns[0], sizeof(float) * 3);
98  memcpy(matrix.data() + 3, &simdTransform.columns[1], sizeof(float) * 3);
99  memcpy(matrix.data() + 6, &simdTransform.columns[2], sizeof(float) * 3);
100 
101  return matrix;
102  }
103  else
104  {
105  return SquareMatrixT3<T>(toSquareMatrix3<float>(simdTransform));
106  }
107 }
108 
109 template <typename T>
110 HomogenousMatrixT4<T> Utilities::toHomogenousMatrix4(const simd_float4x4& simdTransform)
111 {
112  if constexpr (std::is_same<T, float>::value)
113  {
114  HomogenousMatrixF4 matrix;
115 
116  memcpy(matrix.data() + 0, &simdTransform.columns[0], sizeof(float) * 4);
117  memcpy(matrix.data() + 4, &simdTransform.columns[1], sizeof(float) * 4);
118  memcpy(matrix.data() + 8, &simdTransform.columns[2], sizeof(float) * 4);
119  memcpy(matrix.data() + 12, &simdTransform.columns[3], sizeof(float) * 4);
120 
121  return matrix;
122  }
123  else
124  {
125  return HomogenousMatrixT4<T>(toHomogenousMatrix4<float>(simdTransform));
126  }
127 }
128 
129 template <typename T>
131 {
132  if constexpr (std::is_same<T, float>::value)
133  {
134  simd_float4x4 simdTransform;
135 
136  memcpy(&simdTransform.columns[0], matrix.data() + 0, sizeof(float) * 4);
137  memcpy(&simdTransform.columns[1], matrix.data() + 4, sizeof(float) * 4);
138  memcpy(&simdTransform.columns[2], matrix.data() + 8, sizeof(float) * 4);
139  memcpy(&simdTransform.columns[3], matrix.data() + 12, sizeof(float) * 4);
140 
141  return simdTransform;
142  }
143  else
144  {
146  }
147 }
148 
149 template <typename T>
151 {
152  ocean_assert(arFrame != nullptr);
153 
154  const SquareMatrixT3<T> cameraIntrinsics = toSquareMatrix3<T>(arFrame.camera.intrinsics);
155 
156  const int width = NumericD::round32(arFrame.camera.imageResolution.width);
157  const int height = NumericD::round32(arFrame.camera.imageResolution.height);
158 
159  if (width > 0 && height > 0 && !cameraIntrinsics.isSingular())
160  {
161  return std::make_shared<AnyCameraPinholeT<T>>(PinholeCameraT<T>(cameraIntrinsics, (unsigned int)(width), (unsigned int)(height)));
162  }
163 
164  ocean_assert(false && "This should never happen");
165  return nullptr;
166 }
167 
168 }
169 
170 }
171 
172 }
173 
174 #endif // META_OCEAN_DEVICES_ARKIT_UTILITIES_H
This class impelements utility functions.
Definition: devices/arkit/Utilities.h:34
static simd_float4x4 fromHomogenousMatrix4(const HomogenousMatrixT4< T > &matrix)
Converts an Ocean HomogenousMatrix4 matrix to an ARKit simd_float4x4 matrix.
Definition: devices/arkit/Utilities.h:130
static HomogenousMatrixT4< T > toHomogenousMatrix4(const simd_float4x4 &simdTransform)
Converts a ARKit simd_float4x4 matrix to an Ocean HomogenousMatrix4 matrix.
Definition: devices/arkit/Utilities.h:110
static Frame extractDepthFrame(const ARFrame *arFrame, Frame *confidenceFrame=nullptr, const bool copyData=true)
Extracts the depth image from an ARKit frame, if existing.
static SquareMatrixT3< T > toSquareMatrix3(const simd_float3x3 &simdTransform)
Converts a ARKit simd_float3x3 matrix to an Ocean SquareMatrix3 matrix.
Definition: devices/arkit/Utilities.h:91
static Frame extractFrame(const ARFrame *arFrame, const bool copyData=true, const FrameType::PixelFormat preferredPixelFormat=FrameType::FORMAT_UNDEFINED)
Extracts the color image from an ARKit frame.
static SharedAnyCameraT< T > extractCameraModel(const ARFrame *arFrame)
Extracts the camera profile of the color image from an ARKit frame.
Definition: devices/arkit/Utilities.h:150
This class implements Ocean's image class.
Definition: Frame.h:1792
PixelFormat
Definition of all pixel formats available in the Ocean framework.
Definition: Frame.h:183
@ FORMAT_UNDEFINED
Undefined pixel format.
Definition: Frame.h:187
This class implements a 4x4 homogeneous transformation matrix using floating point values with the pr...
Definition: HomogenousMatrix4.h:110
const T * data() const
Returns a pointer to the internal values.
Definition: HomogenousMatrix4.h:1843
static constexpr int32_t round32(const T value)
Returns the rounded 32 bit integer value of a given value.
Definition: Numeric.h:2064
Definition of a pinhole camera model.
Definition: PinholeCamera.h:114
This class implements a 3x3 square matrix.
Definition: SquareMatrix3.h:88
const T * data() const
Returns a pointer to the internal values.
Definition: SquareMatrix3.h:1046
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition: SquareMatrix3.h:1341
HomogenousMatrixT4< float > HomogenousMatrixF4
Instantiation of the HomogenousMatrixT4 template class using a float precision float data type.
Definition: HomogenousMatrix4.h:58
std::shared_ptr< AnyCameraT< T > > SharedAnyCameraT
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition: AnyCamera.h:53
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15