5 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/type-traits.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/math/vector3.h>
29 * @addtogroup dali_core_math
37 * @brief A 3x3 matrix.
39 * The matrix is stored as a flat array and is Column Major, i.e. the storage order is as follows (numbers represent
50 * Each axis is contiguous in memory, so the x-axis corresponds to elements 0, 1 and 2, the y-axis corresponds to
51 * elements 3, 4 and 5, and the z-axis corresponds to elements 6, 7 and 8.
55 class DALI_CORE_API Matrix3
58 friend DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Matrix3& matrix);
61 * @brief The identity matrix.
63 static const Matrix3 IDENTITY;
72 * @brief Copy Constructor.
75 * @param[in] m A reference to the copied 3x3 matrix
77 Matrix3(const Matrix3& m);
83 * @param[in] m A 4x4 matrix. The translation and shear components are ignored
85 Matrix3(const Matrix& m);
91 * @param[in] s00 First element
92 * @param[in] s01 Second element
93 * @param[in] s02 Third element
94 * @param[in] s10 Fourth element
95 * @param[in] s11 Fifth element
96 * @param[in] s12 Sixth element
97 * @param[in] s20 Seventh element
98 * @param[in] s21 Eighth element
99 * @param[in] s22 Ninth element
101 Matrix3(float s00, float s01, float s02, float s10, float s11, float s12, float s20, float s21, float s22);
104 * @brief Move constructor.
107 * @param[in] matrix A reference to the moved matrix
109 Matrix3(Matrix3&& matrix) noexcept;
112 * @brief Move assignment operator.
115 * @param[in] matrix A reference to the moved matrix
116 * @return A reference to this
118 Matrix3& operator=(Matrix3&& matrix) noexcept;
121 * @brief Assignment Operator.
123 * @param[in] matrix From which to copy values
124 * @return Reference to this object
126 Matrix3& operator=(const Matrix3& matrix);
129 * @brief Assignment Operator.
131 * @param[in] matrix A reference to the copied matrix
132 * @return A reference to this
134 Matrix3& operator=(const Matrix& matrix);
137 * @brief The equality operator.
139 * Utilizes appropriate machine epsilon values.
142 * @param[in] rhs The Matrix to compare this to
143 * @return True if the matrices are equal
145 bool operator==(const Matrix3& rhs) const;
148 * @brief The inequality operator.
150 * Utilizes appropriate machine epsilon values.
153 * @param[in] rhs The Matrix to compare this to
154 * @return true if the matrices are equal
156 bool operator!=(const Matrix3& rhs) const;
162 ~Matrix3() = default;
165 * @brief Sets the matrix to the identity matrix.
171 * @brief Returns the contents of the matrix as an array of 9 floats.
173 * The order of the values for a matrix is:
176 * [ xAxis.x, xAxis.y, xAxis.z, yAxis.x, yAxis.y, yAxis.z, zAxis.x, zAxis.y, zAxis.z ]
180 * @return The matrix contents as an array of 9 floats
182 const float* AsFloat() const
184 return &mElements[0];
188 * @brief Returns the contents of the matrix as an array of 9 floats.
190 * The order of the values for a matrix is:
193 * [ xAxis.x, xAxis.y, xAxis.z, yAxis.x, yAxis.y, yAxis.z, zAxis.x, zAxis.y, zAxis.z ]
197 * @return The matrix contents as an array of 9 floats
201 return &mElements[0];
205 * @brief Inverts the matrix.
208 * @return True if successful
213 * @brief Swaps the rows to columns.
215 * @return True if successful
220 * @brief Multiplies all elements of the matrix by the scale value.
223 * @param[in] scale The value by which to scale the whole matrix
225 void Scale(float scale);
228 * @brief Magnitude returns the average of the absolute values of the
231 * (The Magnitude of the unit matrix is therefore 1)
233 * @return The magnitude - always positive
235 float Magnitude() const;
238 * @brief If the matrix is invertible, then this method inverts, transposes
239 * and scales the matrix such that the resultant element values
242 * If the matrix is not invertible, then the matrix is left unchanged.
245 * @return @c true if the matrix is invertible, otherwise @c false
247 bool ScaledInverseTranspose();
250 * @brief Function to multiply two matrices and store the result onto third.
252 * Use this method in time critical path as it does not require temporaries
257 * @param[out] result Result of the multiplication
258 * @param[in] lhs Matrix, this can be same matrix as result
259 * @param[in] rhs Matrix, this cannot be same matrix as result
261 static void Multiply(Matrix3& result, const Matrix3& lhs, const Matrix3& rhs);
264 * @brief Multiplication operator.
266 * Returned Matrix = This Matrix * rhs
268 * @note It makes some memory allocate & copy internally.
269 * Use Matrix3::Multiply API for time critical path.
272 * @param[in] rhs The Matrix to multiply this by
273 * @return A Matrix containing the result
275 Matrix3 operator*(const Matrix3& rhs) const;
278 * @brief Multiplication assignment operator.
282 * @note It makes some memory allocate & copy internally.
285 * @param[in] rhs The Matrix to multiply this by
288 Matrix3& operator*=(const Matrix3& rhs);
291 * @brief The multiplication operator.
293 * Returned Vector = This Matrix * rhs
296 * @param[in] rhs The Vector3 to multiply this by
297 * @return A Vector3 containing the result
299 Vector3 operator*(const Vector3& rhs) const;
302 float mElements[9]; ///< The elements of the matrix
306 * @brief Prints a 3x3 matrix.
309 * @param[in] o The output stream operator
310 * @param[in] matrix The matrix to print
311 * @return The output stream operator
313 DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Matrix3& matrix);
315 // Allow Matrix3 to be treated as a POD type
317 struct TypeTraits<Matrix3> : public BasicTypes<Matrix3>
321 IS_TRIVIAL_TYPE = true
330 #endif //DALI_MATRIX3_H