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
39 * @brief The Matrix class represents transformations and projections.
41 * It is agnostic w.r.t. row/column major notation - it operates on a flat array.
42 * 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.
45 class DALI_IMPORT_API Matrix
49 friend std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
54 * Zero initializes the matrix.
63 * @param[in] initialize True for initialization by zero or otherwise
65 explicit Matrix( bool initialize );
70 * The matrix is initialized with the contents of 'array' which must contain 16 floats.
71 * The order of the values for a transform matrix is:
75 * xAxis.x xAxis.y xAxis.z 0.0f
76 * yAxis.x yAxis.y yAxis.z 0.0f
77 * zAxis.x zAxis.y zAxis.z 0.0f
78 * trans.x trans.y trans.z 1.0f
83 * @param[in] array Pointer of 16 floats data
85 explicit Matrix(const float* array);
88 * @brief Constructs a matrix from quaternion.
91 * @param rotation Rotation as quaternion
93 explicit Matrix( const Quaternion& rotation );
96 * @brief Copy constructor.
99 * @param[in] matrix A reference to the copied matrix
101 Matrix( const Matrix& matrix );
104 * @brief Assignment operator.
107 * @param[in] matrix A reference to the copied matrix
108 * @return A reference to this
110 Matrix& operator=( const Matrix& matrix );
113 * @brief The identity matrix.
115 static const Matrix IDENTITY;
118 * @brief Sets this matrix to be an identity matrix.
124 * @brief Sets this matrix to be an identity matrix with scale.
127 * @param[in] scale Scale to set on top of identity matrix
129 void SetIdentityAndScale( const Vector3& scale );
132 * @brief Inverts a transform Matrix.
134 * Any Matrix representing only a rotation and/or translation
135 * can be inverted using this function. It is faster and more accurate then using Invert().
137 * @param[out] result The inverse of this matrix
139 void InvertTransform(Matrix& result) const;
142 * @brief Generic brute force Matrix Invert.
144 * Using the Matrix invert function for the specific type
145 * of matrix you are dealing with is faster, more accurate.
147 * @return True if successful
152 * @brief Swaps the rows to columns.
158 * @brief Returns the xAxis from a Transform matrix.
163 Vector3 GetXAxis() const;
166 * @brief Returns the yAxis from a Transform matrix.
171 Vector3 GetYAxis() const;
174 * @brief Returns the zAxis from a Transform matrix.
179 Vector3 GetZAxis() const;
182 * @brief Sets the x axis.
184 * This assumes the matrix is a transform matrix.
186 * @param[in] axis The values to set the axis to
188 void SetXAxis(const Vector3& axis);
191 * @brief Sets the y axis.
193 * This assumes the matrix is a transform matrix.
195 * @param[in] axis The values to set the axis to
197 void SetYAxis(const Vector3& axis);
200 * @brief Sets the z axis.
202 * This assumes the matrix is a transform matrix.
204 * @param[in] axis The values to set the axis to
206 void SetZAxis(const Vector3& axis);
209 * @brief Gets the translation.
211 * This assumes the matrix is a transform matrix.
213 * @return The translation
214 * @note inlined for performance reasons (generates less code than a function call)
216 const Vector4& GetTranslation() const { return reinterpret_cast<const Vector4&>(mMatrix[12]); }
219 * @brief Gets the x,y and z components of the translation as a Vector3.
221 * This assumes the matrix is a transform matrix.
223 * @return The translation
224 * @note inlined for performance reasons (generates less code than a function call)
226 const Vector3& GetTranslation3() const { return reinterpret_cast<const Vector3&>(mMatrix[12]); }
229 * @brief Sets the translation.
231 * This assumes the matrix is a transform matrix.
233 * @param[in] translation The translation
235 void SetTranslation(const Vector4& translation);
238 * @brief Sets the x,y and z components of the translation from a Vector3.
240 * This assumes the matrix is a transform matrix.
242 * @param[in] translation The translation
244 void SetTranslation(const Vector3& translation);
247 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
249 * This function is used to correct floating point errors which would otherwise accumulate
250 * as operations are applied to the matrix. This function assumes the matrix is a transform
254 void OrthoNormalize();
257 * @brief Returns the contents of the matrix as an array of 16 floats.
259 * The order of the values for a transform matrix is:
263 * xAxis.x xAxis.y xAxis.z 0.0f
264 * yAxis.x yAxis.y yAxis.z 0.0f
265 * zAxis.x zAxis.y zAxis.z 0.0f
266 * trans.x trans.y trans.z 1.0f
271 * @return The matrix contents as an array of 16 floats
272 * @note inlined for performance reasons (generates less code than a function call)
274 const float* AsFloat() const {return mMatrix;}
277 * @brief Returns the contents of the matrix as an array of 16 floats.
279 * The order of the values for a transform matrix is:
283 * xAxis.x xAxis.y xAxis.z 0.0f
284 * yAxis.x yAxis.y yAxis.z 0.0f
285 * zAxis.x zAxis.y zAxis.z 0.0f
286 * trans.x trans.y trans.z 1.0f
291 * @return The matrix contents as an array of 16 floats
292 * @note inlined for performance reasons (generates less code than a function call)
294 float* AsFloat() {return mMatrix;}
297 * @brief Function to multiply two matrices and store the result onto third.
299 * Use this method in time critical path as it does not require temporaries.
301 * @param[out] result Result of the multiplication
302 * @param[in] lhs Matrix, this can be same matrix as result
303 * @param[in] rhs Matrix, this cannot be same matrix as result
305 static void Multiply( Matrix& result, const Matrix& lhs, const Matrix& rhs );
308 * @brief Function to multiply a matrix and quaternion and store the result onto third.
310 * Use this method in time critical path as it does not require temporaries.
312 * @param[out] result Result of the multiplication
313 * @param[in] lhs Matrix, this can be same matrix as result
314 * @param[in] rhs Quaternion
316 static void Multiply( Matrix& result, const Matrix& lhs, const Quaternion& rhs );
319 * @brief The multiplication operator.
322 * @param[in] rhs The Matrix to multiply this by
323 * @return A matrix containing the result
325 Vector4 operator*(const Vector4& rhs) const;
328 * @brief The equality operator.
330 * Utilizes appropriate machine epsilon values.
333 * @param[in] rhs The Matrix to compare this to
334 * @return true if the matrices are equal
336 bool operator==(const Matrix & rhs) const;
339 * @brief The inequality operator.
341 * Utilizes appropriate machine epsilon values.
343 * @param[in] rhs The Matrix to compare this to
344 * @return true if the matrices are not equal.
346 bool operator!=(const Matrix & rhs) const;
349 * @brief Sets this matrix to contain the position, scale and rotation components.
351 * Performs scale, rotation, then translation
353 * @param[in] scale Scale to apply
354 * @param[in] rotation Rotation to apply
355 * @param[in] translation Translation to apply
357 void SetTransformComponents(const Vector3& scale,
358 const Quaternion& rotation,
359 const Vector3& translation );
362 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
364 * Performs translation, then rotation, then scale.
366 * @param[in] scale Scale to apply
367 * @param[in] rotation Rotation to apply
368 * @param[in] translation Translation to apply
370 void SetInverseTransformComponents(const Vector3& scale,
371 const Quaternion& rotation,
372 const Vector3& translation );
376 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
378 * Performs translation, then rotation.
380 * @param[in] xAxis The X axis of the basis
381 * @param[in] yAxis The Y axis of the basis
382 * @param[in] zAxis The Z axis of the basis
383 * @param[in] translation Translation to apply
385 void SetInverseTransformComponents(const Vector3& xAxis,
386 const Vector3& yAxis,
387 const Vector3& zAxis,
388 const Vector3& translation );
391 * @brief Gets the position, scale and rotation components from the given transform matrix.
394 * @param[out] position Position to set
395 * @param[out] rotation Rotation to set - only valid if the transform matrix has not been skewed or sheared
396 * @param[out] scale Scale to set - only valid if the transform matrix has not been skewed or sheared
397 * @pre This matrix must not contain skews or shears.
399 void GetTransformComponents(Vector3& position,
400 Quaternion& rotation,
401 Vector3& scale) const;
405 float mMatrix[16]; ///< The elements of the matrix
409 * @brief Prints a matrix.
411 * It is printed in memory order, i.e. each printed row is contiguous in memory.
413 * @param[in] o The output stream operator
414 * @param[in] matrix The matrix to print
415 * @return The output stream operator
417 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
419 // Allow Matrix to be treated as a POD type
420 template <> struct TypeTraits< Matrix > : public BasicTypes< Matrix > { enum { IS_TRIVIAL_TYPE = true }; };
427 #endif // __DALI_MATRIX_H__