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.
40 * It is agnostic w.r.t. row/column major notation - it operates on a flat array.
41 * Each axis is contiguous in memory, so the x axis corresponds to elements 0, 1, 2 and 3, the y axis dorresponds to elements 4, 5, 6, 7, etc.
44 class DALI_IMPORT_API Matrix
48 friend std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
53 * Zero initialises the matrix
62 * @param initialize to zero or leave uninitialized
64 explicit Matrix( bool initialize );
69 * The matrix is initialised with the contents of 'array' which must contain 16 floats.
70 * The order of the values for a transform matrix is:
72 * xAxis.x xAxis.y xAxis.z 0.0f
73 * yAxis.x yAxis.y yAxis.z 0.0f
74 * zAxis.x zAxis.y zAxis.z 0.0f
75 * trans.x trans.y trans.z 1.0f
78 * @param [in] array 16 floats
80 explicit Matrix(const float* array);
83 * @brief Constructs a matrix from quaternion.
86 * @param rotation as quaternion
88 explicit Matrix( const Quaternion& rotation );
91 * @brief Copy constructor.
94 * @param [in] matrix to copy values from
96 Matrix( const Matrix& matrix );
99 * @brief Assignment operator.
102 * @param [in] matrix to copy values from
103 * @return a reference to this
105 Matrix& operator=( const Matrix& matrix );
108 * @brief The identity matrix.
110 static const Matrix IDENTITY;
113 * @brief Sets this matrix to be an identity matrix.
119 * @brief Sets this matrix to be an identity matrix with scale.
122 * @param scale to set on top of identity matrix
124 void SetIdentityAndScale( const Vector3& scale );
127 * @brief Invert a transform Matrix.
129 * Any Matrix representing only a rotation and/or translation
130 * can be inverted using this function. It is faster and more accurate then using Invert().
132 * @param [out] result returns the inverse of this matrix
134 void InvertTransform(Matrix& result) const;
137 * @brief Generic brute force Matrix Invert.
139 * Using the Matrix invert function for the specific type
140 * of matrix you are dealing with is faster, more accurate.
142 * @return true if successful
147 * @brief Swaps the rows to columns.
153 * @brief Returns the xAxis from a Transform matrix.
158 Vector3 GetXAxis() const;
161 * @brief Returns the yAxis from a Transform matrix.
166 Vector3 GetYAxis() const;
169 * @brief Returns the zAxis from a Transform matrix.
174 Vector3 GetZAxis() const;
177 * @brief Sets the x axis.
179 * This assumes the matrix is a transform matrix.
181 * @param [in] axis the values to set the axis to
183 void SetXAxis(const Vector3& axis);
186 * @brief Sets the y axis.
188 * This assumes the matrix is a transform matrix.
190 * @param [in] axis the values to set the axis to
192 void SetYAxis(const Vector3& axis);
195 * @brief Sets the z axis.
197 * This assumes the matrix is a transform matrix.
199 * @param [in] axis the values to set the axis to
201 void SetZAxis(const Vector3& axis);
204 * @brief Gets the translation.
206 * This assumes the matrix is a transform matrix.
208 * @return the translation
209 * @note inlined for performance reasons (generates less code than a function call)
211 const Vector4& GetTranslation() const { return reinterpret_cast<const Vector4&>(mMatrix[12]); }
214 * @brief Gets the x,y and z components of the translation as a Vector3.
216 * This assumes the matrix is a transform matrix.
218 * @return the translation
219 * @note inlined for performance reasons (generates less code than a function call)
221 const Vector3& GetTranslation3() const { return reinterpret_cast<const Vector3&>(mMatrix[12]); }
224 * @brief Sets the translation.
226 * This assumes the matrix is a transform matrix.
228 * @param [in] translation the translation
230 void SetTranslation(const Vector4& translation);
233 * @brief Sets the x,y and z components of the translation from a Vector3.
235 * This assumes the matrix is a transform matrix.
237 * @param [in] translation the translation
239 void SetTranslation(const Vector3& translation);
242 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
244 * This function is used to correct floating point errors which would otherwise accumulate
245 * as operations are applied to the matrix. This function assumes the matrix is a transform
249 void OrthoNormalize();
252 * @brief Returns the contents of the matrix as an array of 16 floats.
254 * The order of the values for a transform matrix is:
255 * xAxis.x xAxis.y xAxis.z 0.0f
256 * yAxis.x yAxis.y yAxis.z 0.0f
257 * zAxis.x zAxis.y zAxis.z 0.0f
258 * trans.x trans.y trans.z 1.0f
260 * @return the matrix contents as an array of 16 floats.
261 * @note inlined for performance reasons (generates less code than a function call)
263 const float* AsFloat() const {return mMatrix;}
266 * @brief Returns the contents of the matrix as an array of 16 floats.
268 * The order of the values for a transform matrix is:
270 * xAxis.x xAxis.y xAxis.z 0.0f
271 * yAxis.x yAxis.y yAxis.z 0.0f
272 * zAxis.x zAxis.y zAxis.z 0.0f
273 * trans.x trans.y trans.z 1.0f
275 * @return the matrix contents as an array of 16 floats.
276 * @note inlined for performance reasons (generates less code than a function call)
278 float* AsFloat() {return mMatrix;}
281 * @brief Function to multiply two matrices and store the result onto third.
283 * Use this method in time critical path as it does not require temporaries
285 * @param result of the multiplication
286 * @param lhs matrix, this can be same matrix as result
287 * @param rhs matrix, this cannot be same matrix as result
289 static void Multiply( Matrix& result, const Matrix& lhs, const Matrix& rhs );
292 * @brief Function to multiply a matrix and quaternion and store the result onto third.
294 * Use this method in time critical path as it does not require temporaries
296 * @param result of the multiplication
297 * @param lhs matrix, this can be same matrix as result
298 * @param rhs quaternion
300 static void Multiply( Matrix& result, const Matrix& lhs, const Quaternion& rhs );
303 * @brief The multiplication operator.
306 * @param [in] rhs the Matrix to multiply this by
307 * @return A matrix containing the result
309 Vector4 operator*(const Vector4& rhs) const;
312 * @brief The equality operator.
314 * Utilises appropriate machine epsilon values.
317 * @param [in] rhs the Matrix to compare this to
318 * @return true if the matrices are equal
320 bool operator==(const Matrix & rhs) const;
323 * @brief The inequality operator.
325 * Utilises appropriate machine epsilon values.
327 * @param [in] rhs the Matrix to compare this to
328 * @return true if the matrices are not equal.
330 bool operator!=(const Matrix & rhs) const;
333 * @brief Sets this matrix to contain the position, scale and rotation components.
335 * Performs scale, rotation, then translation
337 * @param[in] scale to apply
338 * @param[in] rotation to apply
339 * @param[in] translation to apply
341 void SetTransformComponents(const Vector3& scale,
342 const Quaternion& rotation,
343 const Vector3& translation );
346 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
348 * Performs translation, then rotation, then scale.
350 * @param[in] scale to apply
351 * @param[in] rotation to apply
352 * @param[in] translation to apply
354 void SetInverseTransformComponents(const Vector3& scale,
355 const Quaternion& rotation,
356 const Vector3& translation );
360 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
362 * Performs translation, then rotation.
364 * @param[in] xAxis The X axis of the basis
365 * @param[in] yAxis The Y axis of the basis
366 * @param[in] zAxis The Z axis of the basis
367 * @param[in] translation to apply
369 void SetInverseTransformComponents(const Vector3& xAxis,
370 const Vector3& yAxis,
371 const Vector3& zAxis,
372 const Vector3& translation );
375 * @brief Gets the position, scale and rotation components from the given transform matrix.
378 * @param[out] position to set
379 * @param[out] rotation to set - only valid if the transform matrix has not been skewed or sheared
380 * @param[out] scale to set - only valid if the transform matrix has not been skewed or sheared
381 * @pre This matrix must not contain skews or shears.
383 void GetTransformComponents(Vector3& position,
384 Quaternion& rotation,
385 Vector3& scale) const;
389 float mMatrix[16]; ///< The elements of the matrix
393 * @brief Print a matrix.
395 * It is printed in memory order, i.e. each printed row is contiguous in memory.
397 * @param [in] o The output stream operator.
398 * @param [in] matrix The matrix to print.
399 * @return The output stream operator.
401 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
403 // Allow Matrix to be treated as a POD type
404 template <> struct TypeTraits< Matrix > : public BasicTypes< Matrix > { enum { IS_TRIVIAL_TYPE = true }; };
411 #endif // __DALI_MATRIX_H__