5 * Copyright (c) 2020 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.
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/type-traits.h>
27 #include <dali/public-api/math/vector4.h>
32 * @addtogroup dali_core_math
39 * @brief The Matrix class represents transformations and projections.
41 * The matrix is stored as a flat array and is Column Major, i.e. the storage order is as follows (numbers represent
53 * Each axis is contiguous in memory, so the x-axis corresponds to elements 0, 1, 2 and 3, the y-axis corresponds to
54 * elements 4, 5, 6, 7, the z-axis corresponds to elements 12, 13, 14 and 15, and the translation vector corresponds to
55 * elements 12, 13 and 14.
59 class DALI_CORE_API Matrix
62 friend DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Matrix& matrix);
67 * Zero initializes the matrix.
76 * @param[in] initialize True for initialization by zero or otherwise
78 explicit Matrix(bool initialize);
83 * The matrix is initialized with the contents of 'array' which must contain 16 floats.
84 * The order of the values for a transform matrix is:
87 * [ xAxis.x, xAxis.y, xAxis.z, 0.0f, yAxis.x, yAxis.y, yAxis.z, 0.0f, zAxis.x, zAxis.y, zAxis.z, 0.0f, trans.x, trans.y, trans.z, 1.0f ]
91 * @param[in] array Pointer of 16 floats data
93 explicit Matrix(const float* array);
96 * @brief Constructs a matrix from quaternion.
99 * @param rotation Rotation as quaternion
101 explicit Matrix(const Quaternion& rotation);
104 * @brief Copy constructor.
107 * @param[in] matrix A reference to the copied matrix
109 Matrix(const Matrix& matrix);
112 * @brief Assignment operator.
115 * @param[in] matrix A reference to the copied matrix
116 * @return A reference to this
118 Matrix& operator=(const Matrix& matrix);
121 * @brief Move constructor.
124 * @param[in] matrix A reference to the moved matrix
126 Matrix(Matrix&& matrix);
129 * @brief Move assignment operator.
132 * @param[in] matrix A reference to the moved matrix
133 * @return A reference to this
135 Matrix& operator=(Matrix&& matrix);
138 * @brief The identity matrix.
140 static const Matrix IDENTITY;
143 * @brief Sets this matrix to be an identity matrix.
149 * @brief Sets this matrix to be an identity matrix with scale.
152 * @param[in] scale Scale to set on top of identity matrix
154 void SetIdentityAndScale(const Vector3& scale);
157 * @brief Inverts a transform Matrix.
159 * Any Matrix representing only a rotation and/or translation
160 * can be inverted using this function. It is faster and more accurate then using Invert().
162 * @param[out] result The inverse of this matrix
164 void InvertTransform(Matrix& result) const;
167 * @brief Generic brute force Matrix Invert.
169 * Using the Matrix invert function for the specific type
170 * of matrix you are dealing with is faster, more accurate.
172 * @return True if successful
177 * @brief Swaps the rows to columns.
183 * @brief Returns the xAxis from a Transform matrix.
188 Vector3 GetXAxis() const;
191 * @brief Returns the yAxis from a Transform matrix.
196 Vector3 GetYAxis() const;
199 * @brief Returns the zAxis from a Transform matrix.
204 Vector3 GetZAxis() const;
207 * @brief Sets the x axis.
209 * This assumes the matrix is a transform matrix.
211 * @param[in] axis The values to set the axis to
213 void SetXAxis(const Vector3& axis);
216 * @brief Sets the y axis.
218 * This assumes the matrix is a transform matrix.
220 * @param[in] axis The values to set the axis to
222 void SetYAxis(const Vector3& axis);
225 * @brief Sets the z axis.
227 * This assumes the matrix is a transform matrix.
229 * @param[in] axis The values to set the axis to
231 void SetZAxis(const Vector3& axis);
234 * @brief Gets the translation.
236 * This assumes the matrix is a transform matrix.
238 * @return The translation
239 * @note inlined for performance reasons (generates less code than a function call)
241 const Vector4& GetTranslation() const
243 return reinterpret_cast<const Vector4&>(mMatrix[12]);
247 * @brief Gets the x,y and z components of the translation as a Vector3.
249 * This assumes the matrix is a transform matrix.
251 * @return The translation
252 * @note inlined for performance reasons (generates less code than a function call)
254 const Vector3& GetTranslation3() const
256 return reinterpret_cast<const Vector3&>(mMatrix[12]);
260 * @brief Sets the translation.
262 * This assumes the matrix is a transform matrix.
264 * @param[in] translation The translation
266 void SetTranslation(const Vector4& translation);
269 * @brief Sets the x,y and z components of the translation from a Vector3.
271 * This assumes the matrix is a transform matrix.
273 * @param[in] translation The translation
275 void SetTranslation(const Vector3& translation);
278 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
280 * This function is used to correct floating point errors which would otherwise accumulate
281 * as operations are applied to the matrix. This function assumes the matrix is a transform
285 void OrthoNormalize();
288 * @brief Returns the contents of the matrix as an array of 16 floats.
290 * The order of the values for a transform matrix is:
293 * [ xAxis.x, xAxis.y, xAxis.z, 0.0f, yAxis.x, yAxis.y, yAxis.z, 0.0f, zAxis.x, zAxis.y, zAxis.z, 0.0f, trans.x, trans.y, trans.z, 1.0f ]
297 * @return The matrix contents as an array of 16 floats
298 * @note inlined for performance reasons (generates less code than a function call)
300 const float* AsFloat() const
306 * @brief Returns the contents of the matrix as an array of 16 floats.
308 * The order of the values for a transform matrix is:
311 * [ xAxis.x, xAxis.y, xAxis.z, 0.0f, yAxis.x, yAxis.y, yAxis.z, 0.0f, zAxis.x, zAxis.y, zAxis.z, 0.0f, trans.x, trans.y, trans.z, 1.0f ]
315 * @return The matrix contents as an array of 16 floats
316 * @note inlined for performance reasons (generates less code than a function call)
324 * @brief Function to multiply two matrices and store the result onto third.
326 * Use this method in time critical path as it does not require temporaries.
331 * @param[out] result Result of the multiplication
332 * @param[in] lhs Matrix, this can be same matrix as result
333 * @param[in] rhs Matrix, this cannot be same matrix as result
335 static void Multiply(Matrix& result, const Matrix& lhs, const Matrix& rhs);
338 * @brief Function to multiply a matrix and quaternion and store the result onto third.
340 * Use this method in time critical path as it does not require temporaries.
342 * @param[out] result Result of the multiplication
343 * @param[in] lhs Matrix, this can be same matrix as result
344 * @param[in] rhs Quaternion
346 static void Multiply(Matrix& result, const Matrix& lhs, const Quaternion& rhs);
349 * @brief The multiplication operator.
351 * Returned Vector = This Matrix * rhs
354 * @param[in] rhs The Vector4 to multiply this by
355 * @return A Vector4 containing the result
357 Vector4 operator*(const Vector4& rhs) const;
360 * @brief The equality operator.
362 * Utilizes appropriate machine epsilon values.
365 * @param[in] rhs The Matrix to compare this to
366 * @return true if the matrices are equal
368 bool operator==(const Matrix& rhs) const;
371 * @brief The inequality operator.
373 * Utilizes appropriate machine epsilon values.
375 * @param[in] rhs The Matrix to compare this to
376 * @return true if the matrices are not equal.
378 bool operator!=(const Matrix& rhs) const;
381 * @brief Sets this matrix to contain the position, scale and rotation components.
383 * Performs scale, rotation, then translation
385 * @param[in] scale Scale to apply
386 * @param[in] rotation Rotation to apply
387 * @param[in] translation Translation to apply
389 void SetTransformComponents(const Vector3& scale,
390 const Quaternion& rotation,
391 const Vector3& translation);
394 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
396 * Performs translation, then rotation, then scale.
398 * @param[in] scale Scale to apply
399 * @param[in] rotation Rotation to apply
400 * @param[in] translation Translation to apply
402 void SetInverseTransformComponents(const Vector3& scale,
403 const Quaternion& rotation,
404 const Vector3& translation);
407 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
409 * Performs translation, then rotation.
411 * @param[in] xAxis The X axis of the basis
412 * @param[in] yAxis The Y axis of the basis
413 * @param[in] zAxis The Z axis of the basis
414 * @param[in] translation Translation to apply
416 void SetInverseTransformComponents(const Vector3& xAxis,
417 const Vector3& yAxis,
418 const Vector3& zAxis,
419 const Vector3& translation);
422 * @brief Gets the position, scale and rotation components from the given transform matrix.
425 * @param[out] position Position to set
426 * @param[out] rotation Rotation to set - only valid if the transform matrix has not been skewed or sheared
427 * @param[out] scale Scale to set - only valid if the transform matrix has not been skewed or sheared
428 * @pre This matrix must not contain skews or shears.
430 void GetTransformComponents(Vector3& position,
431 Quaternion& rotation,
432 Vector3& scale) const;
435 float mMatrix[16]; ///< The elements of the matrix
439 * @brief Prints a matrix.
441 * It is printed in memory order.
443 * @param[in] o The output stream operator
444 * @param[in] matrix The matrix to print
445 * @return The output stream operator
447 DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Matrix& matrix);
449 // Allow Matrix to be treated as a POD type
451 struct TypeTraits<Matrix> : public BasicTypes<Matrix>
455 IS_TRIVIAL_TYPE = true
464 #endif // DALI_MATRIX_H