Ocean
Loading...
Searching...
No Matches
EigenUtilities.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_CV_EIGEN_UTILITIES_H
9#define META_OCEAN_CV_EIGEN_UTILITIES_H
10
11#include "ocean/math/Math.h"
12
13#include "ocean/base/Frame.h"
14
16
17#if defined(_MSC_VER)
18 #pragma warning(push)
19 #pragma warning(disable: 4244) // 'argument': conversion from 'const __int64' to 'int', possible loss of data
20#elif defined(__clang__)
21 #pragma clang diagnostic push
22 #pragma clang diagnostic ignored "-Wconversion"
23#endif
24
25#include <Eigen/Dense>
26
27#if defined(_MSC_VER)
28 #pragma warning(pop)
29#elif defined(__clang__)
30 #pragma clang diagnostic pop
31#endif
32
33namespace Ocean
34{
35
36namespace CV
37{
38
39/**
40 * This class provides several helper classes allowing to convert between Ocean data structures and Eigen data structures.
41 * @ingroup cv
42 */
44{
45 public:
46
47 /**
48 * Converts a frame to an Eigen matrix with column major memory layout (default in Eigen).
49 * @param frame The frame to be converted, must be a 1 channel 'unsigned char' or 'float' frame, must be valid
50 * @param matrix The resulting matrix receiving a copy of the frame data
51 * @return True, if succeeded
52 */
53 template <typename T>
54 static bool frame2matrix(const Frame& frame, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>& matrix);
55
56 /**
57 * Converts a frame to an Eigen matrix with row major memory layout.
58 * @param frame The frame to be converted, must be a 1 channel 'unsigned char' or 'float' frame, must be valid
59 * @param matrix The resulting matrix receiving a copy of the frame data
60 * @return True, if succeeded
61 */
62 template <typename T>
63 static bool frame2matrix(const Frame& frame, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>& matrix);
64
65 /**
66 * Converts an Ocean 2D vector to a corresponding Eigen vector
67 * @param ocnVector The Ocean vector that will be converted
68 * @return The Eigen vector with the same values as the Ocean vector
69 * @tparam TType The type of the vector elements, must be either `int`, `float`, or `double`
70 */
71 template <typename TType>
72 static Eigen::Matrix<TType, 2, 1> toEigenVector(const VectorT2<TType>& ocnVector);
73
74 /**
75 * Converts an Ocean 3D vector to a corresponding Eigen vector
76 * @param ocnVector The Ocean vector that will be converted
77 * @return The Eigen vector with the same values as the Ocean vector
78 * @tparam TType The type of the vector elements, must be either `int`, `float`, or `double`
79 */
80 template <typename TType>
81 static Eigen::Matrix<TType, 3, 1> toEigenVector(const VectorT3<TType>& ocnVector);
82
83 /**
84 * Converts an Ocean 4D vector to a corresponding Eigen vector
85 * @param ocnVector The Ocean vector that will be converted
86 * @return The Eigen vector with the same values as the Ocean vector
87 * @tparam TType The type of the vector elements, must be either `int`, `float`, or `double`
88 */
89 template <typename TType>
90 static Eigen::Matrix<TType, 4, 1> toEigenVector(const VectorT4<TType>& ocnVector);
91
92 /**
93 * Converts an Eigen 2D vector to a corresponding Ocean vector
94 * @param eigenVector The Eigen vector that will be converted
95 * @return The Ocean vector with the same value as the Eigen vector
96 * @tparam TType The type of the vector elements, must be either `int`, `float`, or `double`
97 */
98 template <typename TType>
99 static VectorT2<TType> toOceanVector(const Eigen::Matrix<TType, 2, 1>& eigenVector);
100
101 /**
102 * Converts an Eigen 3D vector to a corresponding Ocean vector
103 * @param eigenVector The Eigen vector that will be converted
104 * @return The Ocean vector with the same value as the Eigen vector
105 * @tparam TType The type of the vector elements, must be either `int`, `float`, or `double`
106 */
107 template <typename TType>
108 static VectorT3<TType> toOceanVector(const Eigen::Matrix<TType, 3, 1>& eigenVector);
109
110 /**
111 * Converts an Eigen 4D vector to a corresponding Ocean vector
112 * @param eigenVector The Eigen vector that will be converted
113 * @return The Ocean vector with the same value as the Eigen vector
114 * @tparam TType The type of the vector elements, must be either `int`, `float`, or `double`
115 */
116 template <typename TType>
117 static VectorT4<TType> toOceanVector(const Eigen::Matrix<TType, 4, 1>& eigenVector);
118
119 /**
120 * Converts an Ocean quaternion to an Eigen quaternion
121 * @param ocnQuaternion The Ocean quaternion that will be converted to an Eigen quaternion
122 * @return The resulting Eigen quaternion
123 * @tparam TType The type of the quaternion elements, must be either `float` or `double`
124 */
125 template <typename TType>
126 static Eigen::Quaternion<TType> toEigenQuaternion(const QuaternionT<TType>& ocnQuaternion);
127
128 /**
129 * Converts an Eigen quaternion to an Ocean quaternion
130 * @param eigenQuaternion The Eigen quaternion that will be converted to an Ocean quaternion
131 * @return The resulting Ocean quaternion
132 * @tparam TType The type of the quaternion elements, must be either `float` or `double`
133 */
134 template <typename TType>
135 static QuaternionT<TType> toOceanQuaternion(const Eigen::Quaternion<TType>& eigenQuaternion);
136};
137
138template <typename T>
139bool EigenUtilities::frame2matrix(const Frame& frame, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>& matrix)
140{
141 ocean_assert(frame.isValid() && frame.numberPlanes() == 1u && frame.channels() == 1u);
142
143 if (!frame.isValid() || frame.numberPlanes() != 1u || frame.channels() != 1u)
144 {
145 return false;
146 }
147
148 ocean_assert((matrix.Flags & Eigen::RowMajorBit) == 0);
149
150 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> rowMajorMatrix(frame.height(), frame.width());
151
152 constexpr unsigned int matrixPaddingElements = 0u;
153
155 {
156 CV::FrameConverter::cast<uint8_t, T>(frame.constdata<uint8_t>(), rowMajorMatrix.data(), frame.width(), frame.height(), 1u, frame.paddingElements(), matrixPaddingElements);
157 }
159 {
160 CV::FrameConverter::cast<float, T>(frame.constdata<float>(), rowMajorMatrix.data(), frame.width(), frame.height(), 1u, frame.paddingElements(), matrixPaddingElements);
161 }
162 else
163 {
164 ocean_assert(false && "Data type not supported!");
165 return false;
166 }
167
168 matrix = rowMajorMatrix;
169
170 ocean_assert((matrix.Flags & Eigen::RowMajorBit) == 0);
171
172 return true;
173}
174
175template <typename T>
176bool EigenUtilities::frame2matrix(const Frame& frame, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>& matrix)
177{
178 ocean_assert(frame.isValid() && frame.numberPlanes() == 1u && frame.channels() == 1u);
179
180 if (!frame.isValid() || frame.numberPlanes() != 1u || frame.channels() != 1u)
181 {
182 return false;
183 }
184
185 ocean_assert((matrix.Flags & Eigen::RowMajorBit) != 0);
186
187 matrix.resize(frame.height(), frame.width());
188
189 constexpr unsigned int matrixPaddingElements = 0u;
190
192 {
193 CV::FrameConverter::cast<uint8_t, T>(frame.constdata<uint8_t>(), matrix.data(), frame.width(), frame.height(), 1u, frame.paddingElements(), matrixPaddingElements);
194 return true;
195 }
197 {
198 CV::FrameConverter::cast<float, T>(frame.constdata<float>(), matrix.data(), frame.width(), frame.height(), 1u, frame.paddingElements(), matrixPaddingElements);
199 return true;
200 }
201
202 ocean_assert(false && "Data type not supported!");
203 return false;
204}
205
206template <typename TType>
207Eigen::Matrix<TType, 2, 1> EigenUtilities::toEigenVector(const VectorT2<TType>& ocnVector)
208{
209 return Eigen::Matrix<TType, 2, 1>(ocnVector.data());
210}
211
212template <typename TType>
213Eigen::Matrix<TType, 3, 1> EigenUtilities::toEigenVector(const VectorT3<TType>& ocnVector)
214{
215 return Eigen::Matrix<TType, 3, 1>(ocnVector.data());
216}
217
218template <typename TType>
219Eigen::Matrix<TType, 4, 1> EigenUtilities::toEigenVector(const VectorT4<TType>& ocnVector)
220{
221 return Eigen::Matrix<TType, 4, 1>(ocnVector.data());
222}
223
224template <typename TType>
225VectorT2<TType> EigenUtilities::toOceanVector(const Eigen::Matrix<TType, 2, 1>& eigenVector)
226{
227 return VectorT2<TType>(eigenVector.data());
228}
229
230template <typename TType>
231VectorT3<TType> EigenUtilities::toOceanVector(const Eigen::Matrix<TType, 3, 1>& eigenVector)
232{
233 return VectorT3<TType>(eigenVector.data());
234}
235
236template <typename TType>
237VectorT4<TType> EigenUtilities::toOceanVector(const Eigen::Matrix<TType, 4, 1>& eigenVector)
238{
239 return VectorT4<TType>(eigenVector.data());
240}
241
242template <typename TType>
243Eigen::Quaternion<TType> EigenUtilities::toEigenQuaternion(const QuaternionT<TType>& ocnQuaternion)
244{
245 static_assert(std::is_same<TType, float>::value || std::is_same<TType, double>::value, "Invalid value for type TType");
246
247 return Eigen::Quaternion<TType>(ocnQuaternion.w(), ocnQuaternion.x(), ocnQuaternion.y(), ocnQuaternion.z());
248}
249
250template <typename TType>
251QuaternionT<TType> EigenUtilities::toOceanQuaternion(const Eigen::Quaternion<TType>& eigenQuaternion)
252{
253 static_assert(std::is_same<TType, float>::value || std::is_same<TType, double>::value, "Invalid value for type TType");
254
255 return QuaternionT<TType>(eigenQuaternion.w(), eigenQuaternion.x(), eigenQuaternion.y(), eigenQuaternion.z());
256}
257
258} // namespace CV
259
260} // namespace Ocean
261
262#endif // META_OCEAN_CV_EIGEN_UTILITIES_H
This class provides several helper classes allowing to convert between Ocean data structures and Eige...
Definition EigenUtilities.h:44
static VectorT2< TType > toOceanVector(const Eigen::Matrix< TType, 2, 1 > &eigenVector)
Converts an Eigen 2D vector to a corresponding Ocean vector.
Definition EigenUtilities.h:225
static Eigen::Matrix< TType, 2, 1 > toEigenVector(const VectorT2< TType > &ocnVector)
Converts an Ocean 2D vector to a corresponding Eigen vector.
Definition EigenUtilities.h:207
static Eigen::Quaternion< TType > toEigenQuaternion(const QuaternionT< TType > &ocnQuaternion)
Converts an Ocean quaternion to an Eigen quaternion.
Definition EigenUtilities.h:243
static bool frame2matrix(const Frame &frame, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > &matrix)
Converts a frame to an Eigen matrix with column major memory layout (default in Eigen).
Definition EigenUtilities.h:139
static QuaternionT< TType > toOceanQuaternion(const Eigen::Quaternion< TType > &eigenQuaternion)
Converts an Eigen quaternion to an Ocean quaternion.
Definition EigenUtilities.h:251
static void cast(const TSource *__restrict source, TTarget *__restrict target, const unsigned int width, const unsigned int height, const unsigned int channels, const unsigned int sourcePaddingElements, const unsigned int targetPaddingElements)
Casts the pixel values from one frame type to another frame type.
This class implements Ocean's image class.
Definition Frame.h:1969
const T * constdata(const unsigned int planeIndex=0u) const
Returns a pointer to the read-only pixel data of a specific plane.
Definition Frame.h:4425
bool isValid() const
Returns whether this frame is valid.
Definition Frame.h:4705
unsigned int paddingElements(const unsigned int planeIndex=0u) const
Returns the optional number of padding elements at the end of each row for a specific plane.
Definition Frame.h:4299
unsigned int width() const
Returns the width of the frame format in pixel.
Definition Frame.h:3334
uint32_t numberPlanes() const
Returns the number of planes of the pixel format of this frame.
Definition Frame.h:3374
PixelFormat pixelFormat() const
Returns the pixel format of the frame.
Definition Frame.h:3344
@ DT_UNSIGNED_INTEGER_8
Unsigned 8 bit integer data type (uint8_t).
Definition Frame.h:41
@ DT_SIGNED_FLOAT_32
Signed 32 bit float data type (float).
Definition Frame.h:59
unsigned int height() const
Returns the height of the frame in pixel.
Definition Frame.h:3339
unsigned int channels() const
Returns the number of individual channels the frame has.
Definition Frame.h:3364
static bool formatIsGeneric(const PixelFormat pixelFormat, const DataType dataType, const uint32_t channels, const uint32_t planes=1u, const uint32_t widthMultiple=1u, const uint32_t heightMultiple=1u)
Checks whether a given pixel format is a specific layout regarding data channels and data type.
Definition Frame.h:3609
This class implements a unit quaternion rotation.
Definition Quaternion.h:100
const T & x() const
Returns the x value of the quaternion.
Definition Quaternion.h:900
const T & w() const
Returns the w value of the quaternion.
Definition Quaternion.h:888
const T & y() const
Returns the y value of the quaternion.
Definition Quaternion.h:912
const T & z() const
Returns the z value of the quaternion.
Definition Quaternion.h:924
This class implements a vector with two elements.
Definition Vector2.h:96
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition Vector2.h:727
This class implements a vector with three elements.
Definition Vector3.h:97
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition Vector3.h:846
This class implements a vector with four elements.
Definition Vector4.h:97
const T * data() const noexcept
Returns an pointer to the vector elements.
Definition Vector4.h:723
Namespace of the CV library.
Definition Advanced.h:17
The namespace covering the entire Ocean framework.
Definition Accessor.h:15