1 #ifndef __DALI_MATRIX_H__
2 #define __DALI_MATRIX_H__
5 * Copyright (c) 2015 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
40 * @brief The Matrix class represents transformations and projections.
42 * It is agnostic w.r.t. row/column major notation - it operates on a flat array.
43 * Each axis is contiguous in memory, so the x axis corresponds to elements 0, 1, 2 and 3, the y axis corresponds to elements 4, 5, 6, 7, etc.
46 class DALI_IMPORT_API Matrix
50 friend std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
55 * Zero initializes the matrix.
64 * @param[in] initialize True for initialization by zero or otherwise
66 explicit Matrix( bool initialize );
71 * The matrix is initialized with the contents of 'array' which must contain 16 floats.
72 * The order of the values for a transform matrix is:
76 * xAxis.x xAxis.y xAxis.z 0.0f
77 * yAxis.x yAxis.y yAxis.z 0.0f
78 * zAxis.x zAxis.y zAxis.z 0.0f
79 * trans.x trans.y trans.z 1.0f
84 * @param[in] array Pointer of 16 floats data
86 explicit Matrix(const float* array);
89 * @brief Constructs a matrix from quaternion.
92 * @param rotation Rotation as quaternion
94 explicit Matrix( const Quaternion& rotation );
97 * @brief Copy constructor.
100 * @param[in] matrix A reference to the copied matrix
102 Matrix( const Matrix& matrix );
105 * @brief Assignment operator.
108 * @param[in] matrix A reference to the copied matrix
109 * @return A reference to this
111 Matrix& operator=( const Matrix& matrix );
114 * @brief The identity matrix.
116 static const Matrix IDENTITY;
119 * @brief Sets this matrix to be an identity matrix.
125 * @brief Sets this matrix to be an identity matrix with scale.
128 * @param[in] scale Scale to set on top of identity matrix
130 void SetIdentityAndScale( const Vector3& scale );
133 * @brief Inverts a transform Matrix.
135 * Any Matrix representing only a rotation and/or translation
136 * can be inverted using this function. It is faster and more accurate then using Invert().
138 * @param[out] result The inverse of this matrix
140 void InvertTransform(Matrix& result) const;
143 * @brief Generic brute force Matrix Invert.
145 * Using the Matrix invert function for the specific type
146 * of matrix you are dealing with is faster, more accurate.
148 * @return True if successful
153 * @brief Swaps the rows to columns.
159 * @brief Returns the xAxis from a Transform matrix.
164 Vector3 GetXAxis() const;
167 * @brief Returns the yAxis from a Transform matrix.
172 Vector3 GetYAxis() const;
175 * @brief Returns the zAxis from a Transform matrix.
180 Vector3 GetZAxis() const;
183 * @brief Sets the x axis.
185 * This assumes the matrix is a transform matrix.
187 * @param[in] axis The values to set the axis to
189 void SetXAxis(const Vector3& axis);
192 * @brief Sets the y axis.
194 * This assumes the matrix is a transform matrix.
196 * @param[in] axis The values to set the axis to
198 void SetYAxis(const Vector3& axis);
201 * @brief Sets the z axis.
203 * This assumes the matrix is a transform matrix.
205 * @param[in] axis The values to set the axis to
207 void SetZAxis(const Vector3& axis);
210 * @brief Gets the translation.
212 * This assumes the matrix is a transform matrix.
214 * @return The translation
215 * @note inlined for performance reasons (generates less code than a function call)
217 const Vector4& GetTranslation() const { return reinterpret_cast<const Vector4&>(mMatrix[12]); }
220 * @brief Gets the x,y and z components of the translation as a Vector3.
222 * This assumes the matrix is a transform matrix.
224 * @return The translation
225 * @note inlined for performance reasons (generates less code than a function call)
227 const Vector3& GetTranslation3() const { return reinterpret_cast<const Vector3&>(mMatrix[12]); }
230 * @brief Sets the translation.
232 * This assumes the matrix is a transform matrix.
234 * @param[in] translation The translation
236 void SetTranslation(const Vector4& translation);
239 * @brief Sets the x,y and z components of the translation from a Vector3.
241 * This assumes the matrix is a transform matrix.
243 * @param[in] translation The translation
245 void SetTranslation(const Vector3& translation);
248 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
250 * This function is used to correct floating point errors which would otherwise accumulate
251 * as operations are applied to the matrix. This function assumes the matrix is a transform
255 void OrthoNormalize();
258 * @brief Returns the contents of the matrix as an array of 16 floats.
260 * The order of the values for a transform matrix is:
264 * xAxis.x xAxis.y xAxis.z 0.0f
265 * yAxis.x yAxis.y yAxis.z 0.0f
266 * zAxis.x zAxis.y zAxis.z 0.0f
267 * trans.x trans.y trans.z 1.0f
272 * @return The matrix contents as an array of 16 floats
273 * @note inlined for performance reasons (generates less code than a function call)
275 const float* AsFloat() const {return mMatrix;}
278 * @brief Returns the contents of the matrix as an array of 16 floats.
280 * The order of the values for a transform matrix is:
284 * xAxis.x xAxis.y xAxis.z 0.0f
285 * yAxis.x yAxis.y yAxis.z 0.0f
286 * zAxis.x zAxis.y zAxis.z 0.0f
287 * trans.x trans.y trans.z 1.0f
292 * @return The matrix contents as an array of 16 floats
293 * @note inlined for performance reasons (generates less code than a function call)
295 float* AsFloat() {return mMatrix;}
298 * @brief Function to multiply two matrices and store the result onto third.
300 * Use this method in time critical path as it does not require temporaries.
302 * @param[out] result Result of the multiplication
303 * @param[in] lhs Matrix, this can be same matrix as result
304 * @param[in] rhs Matrix, this cannot be same matrix as result
306 static void Multiply( Matrix& result, const Matrix& lhs, const Matrix& rhs );
309 * @brief Function to multiply a matrix and quaternion and store the result onto third.
311 * Use this method in time critical path as it does not require temporaries.
313 * @param[out] result Result of the multiplication
314 * @param[in] lhs Matrix, this can be same matrix as result
315 * @param[in] rhs Quaternion
317 static void Multiply( Matrix& result, const Matrix& lhs, const Quaternion& rhs );
320 * @brief The multiplication by Vector4 operator.
323 * @param[in] rhs The Vector4 coordinates to multiply this matrix by
324 * @return A Vector4 containing the result coordinates
326 Vector4 operator*(const Vector4& rhs) const;
329 * @brief The multiplication by Vector2 operator.
330 * Note: This performs an optimized 2D transformation.
333 * @param[in] rhs The Vector2 coordinates to multiply this matrix by
334 * @return A Vector2 containing the result coordinates
336 Vector2 operator*( const Vector2& rhs ) const;
339 * @brief The equality operator.
341 * Utilizes appropriate machine epsilon values.
344 * @param[in] rhs The Matrix to compare this to
345 * @return true if the matrices are equal
347 bool operator==(const Matrix & rhs) const;
350 * @brief The inequality operator.
352 * Utilizes appropriate machine epsilon values.
354 * @param[in] rhs The Matrix to compare this to
355 * @return true if the matrices are not equal.
357 bool operator!=(const Matrix & rhs) const;
360 * @brief Sets this matrix to contain the position, scale and rotation components.
362 * Performs scale, rotation, then translation
364 * @param[in] scale Scale to apply
365 * @param[in] rotation Rotation to apply
366 * @param[in] translation Translation to apply
368 void SetTransformComponents(const Vector3& scale,
369 const Quaternion& rotation,
370 const Vector3& translation );
373 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
375 * Performs translation, then rotation, then scale.
377 * @param[in] scale Scale to apply
378 * @param[in] rotation Rotation to apply
379 * @param[in] translation Translation to apply
381 void SetInverseTransformComponents(const Vector3& scale,
382 const Quaternion& rotation,
383 const Vector3& translation );
387 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
389 * Performs translation, then rotation.
391 * @param[in] xAxis The X axis of the basis
392 * @param[in] yAxis The Y axis of the basis
393 * @param[in] zAxis The Z axis of the basis
394 * @param[in] translation Translation to apply
396 void SetInverseTransformComponents(const Vector3& xAxis,
397 const Vector3& yAxis,
398 const Vector3& zAxis,
399 const Vector3& translation );
402 * @brief Gets the position, scale and rotation components from the given transform matrix.
405 * @param[out] position Position to set
406 * @param[out] rotation Rotation to set - only valid if the transform matrix has not been skewed or sheared
407 * @param[out] scale Scale to set - only valid if the transform matrix has not been skewed or sheared
408 * @pre This matrix must not contain skews or shears.
410 void GetTransformComponents(Vector3& position,
411 Quaternion& rotation,
412 Vector3& scale) const;
416 float mMatrix[16]; ///< The elements of the matrix
420 * @brief Prints a matrix.
422 * It is printed in memory order, i.e. each printed row is contiguous in memory.
424 * @param[in] o The output stream operator
425 * @param[in] matrix The matrix to print
426 * @return The output stream operator
428 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
430 // Allow Matrix to be treated as a POD type
431 template <> struct TypeTraits< Matrix > : public BasicTypes< Matrix > { enum { IS_TRIVIAL_TYPE = true }; };
438 #endif // __DALI_MATRIX_H__