1 #ifndef __DALI_MATRIX_H__
2 #define __DALI_MATRIX_H__
5 * Copyright (c) 2014 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 * @addtogroup CAPI_DALI_MATH_MODULE
30 #include <dali/public-api/math/vector4.h>
32 namespace Dali DALI_IMPORT_API
37 * @brief The Matrix class represents transformations and projections.
38 * It is agnostic w.r.t. row/column major notation - it operates on a flat array.
39 * 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.
41 class DALI_IMPORT_API Matrix
45 friend std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
50 * Zero initialises the matrix
57 * @param initialize to zero or leave uninitialized
59 explicit Matrix( bool initialize );
64 * The matrix is initialised with the contents of 'array' which must contain 16 floats.
65 * The order of the values for a transform matrix is:
67 * xAxis.x xAxis.y xAxis.z 0.0f
68 * yAxis.x yAxis.y yAxis.z 0.0f
69 * zAxis.x zAxis.y zAxis.z 0.0f
70 * trans.x trans.y trans.z 1.0f
72 * @param [in] array 16 floats
74 explicit Matrix(const float* array);
77 * @brief Constructs a matrix from quaternion.
79 * @param rotation as quaternion
81 explicit Matrix( const Quaternion& rotation );
84 * @brief Copy constructor.
86 * @param [in] matrix to copy values from
88 Matrix( const Matrix& matrix );
91 * @brief Assignment operator.
93 * @param [in] matrix to copy values from
94 * @return a reference to this
96 Matrix& operator=( const Matrix& matrix );
99 * @brief The identity matrix.
101 static const Matrix IDENTITY;
104 * @brief Sets this matrix to be an identity matrix.
109 * @brief Sets this matrix to be an identity matrix with scale.
111 * @param scale to set on top of identity matrix
113 void SetIdentityAndScale( const Vector3& scale );
116 * @brief Invert a transform Matrix.
118 * Any Matrix representing only a rotation and/or translation
119 * can be inverted using this function. It is faster and more accurate then using Invert().
120 * @param [out] result returns the inverse of this matrix
122 void InvertTransform(Matrix& result) const;
125 * @brief Generic brute force Matrix Invert.
127 * Using the Matrix invert function for the specific type
128 * of matrix you are dealing with is faster, more accurate.
129 * @return true if successful
134 * @brief Swaps the rows to columns.
139 * @brief Returns the xAxis from a Transform matrix.
143 Vector3 GetXAxis() const;
146 * @brief Returns the yAxis from a Transform matrix.
150 Vector3 GetYAxis() const;
153 * @brief Returns the zAxis from a Transform matrix.
157 Vector3 GetZAxis() const;
160 * @brief Sets the x axis.
162 * This assumes the matrix is a transform matrix.
163 * @param [in] axis the values to set the axis to
165 void SetXAxis(const Vector3& axis);
168 * @brief Sets the y axis.
170 * This assumes the matrix is a transform matrix.
171 * @param [in] axis the values to set the axis to
173 void SetYAxis(const Vector3& axis);
176 * @brief Sets the z axis.
178 * This assumes the matrix is a transform matrix.
179 * @param [in] axis the values to set the axis to
181 void SetZAxis(const Vector3& axis);
184 * @brief Gets the translation.
186 * This assumes the matrix is a transform matrix.
187 * @note inlined for performance reasons (generates less code than a function call)
188 * @return the translation
190 const Vector4& GetTranslation() const { return reinterpret_cast<const Vector4&>(mMatrix[12]); }
193 * @brief Gets the x,y and z components of the translation as a Vector3.
195 * This assumes the matrix is a transform matrix.
196 * @note inlined for performance reasons (generates less code than a function call)
197 * @return the translation
199 const Vector3& GetTranslation3() const { return reinterpret_cast<const Vector3&>(mMatrix[12]); }
202 * @brief Sets the translation.
204 * This assumes the matrix is a transform matrix.
205 * @param [in] translation the translation
207 void SetTranslation(const Vector4& translation);
210 * @brief Sets the x,y and z components of the translation from a Vector3.
212 * This assumes the matrix is a transform matrix.
213 * @param [in] translation the translation
215 void SetTranslation(const Vector3& translation);
218 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
220 * This function is used to correct floating point errors which would otherwise accumulate
221 * as operations are applied to the matrix. This function assumes the matrix is a transform
224 void OrthoNormalize();
227 * @brief Returns the contents of the matrix as an array of 16 floats.
229 * The order of the values for a transform matrix is:
230 * xAxis.x xAxis.y xAxis.z 0.0f
231 * yAxis.x yAxis.y yAxis.z 0.0f
232 * zAxis.x zAxis.y zAxis.z 0.0f
233 * trans.x trans.y trans.z 1.0f
234 * @note inlined for performance reasons (generates less code than a function call)
235 * @return the matrix contents as an array of 16 floats.
237 const float* AsFloat() const {return mMatrix;}
240 * @brief Returns the contents of the matrix as an array of 16 floats.
242 * The order of the values for a transform matrix is:
244 * xAxis.x xAxis.y xAxis.z 0.0f
245 * yAxis.x yAxis.y yAxis.z 0.0f
246 * zAxis.x zAxis.y zAxis.z 0.0f
247 * trans.x trans.y trans.z 1.0f
248 * @note inlined for performance reasons (generates less code than a function call)
249 * @return the matrix contents as an array of 16 floats.
251 float* AsFloat() {return mMatrix;}
254 * @brief Function to multiply two matrices and store the result onto third.
256 * Use this method in time critical path as it does not require temporaries
257 * @param result of the multiplication
258 * @param lhs matrix, this can be same matrix as result
259 * @param rhs matrix, this cannot be same matrix as result
261 static void Multiply( Matrix& result, const Matrix& lhs, const Matrix& rhs );
264 * @brief Function to multiply a matrix and quaternion and store the result onto third.
266 * Use this method in time critical path as it does not require temporaries
267 * @param result of the multiplication
268 * @param lhs matrix, this can be same matrix as result
269 * @param rhs quaternion
271 static void Multiply( Matrix& result, const Matrix& lhs, const Quaternion& rhs );
274 * @brief The multiplication operator.
276 * @param [in] rhs the Matrix to multiply this by
277 * @return A matrix containing the result
279 Vector4 operator*(const Vector4& rhs) const;
282 * @brief The equality operator.
284 * Utilises appropriate machine epsilon values.
286 * @param [in] rhs the Matrix to compare this to
287 * @return true if the matrices are equal
289 bool operator==(const Matrix & rhs) const;
292 * @brief The inequality operator.
294 * Utilises appropriate machine epsilon values.
295 * @param [in] rhs the Matrix to compare this to
296 * @return true if the matrices are not equal.
298 bool operator!=(const Matrix & rhs) const;
301 * @brief Sets this matrix to contain the position, scale and rotation components.
303 * Performs scale, rotation, then translation
304 * @param[in] scale to apply
305 * @param[in] rotation to apply
306 * @param[in] translation to apply
308 void SetTransformComponents(const Vector3& scale,
309 const Quaternion& rotation,
310 const Vector3& translation );
313 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
315 * Performs translation, then rotation, then scale.
316 * @param[in] scale to apply
317 * @param[in] rotation to apply
318 * @param[in] translation to apply
320 void SetInverseTransformComponents(const Vector3& scale,
321 const Quaternion& rotation,
322 const Vector3& translation );
326 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
328 * Performs translation, then rotation.
329 * @param[in] xAxis The X axis of the basis
330 * @param[in] yAxis The Y axis of the basis
331 * @param[in] zAxis The Z axis of the basis
332 * @param[in] translation to apply
334 void SetInverseTransformComponents(const Vector3& xAxis,
335 const Vector3& yAxis,
336 const Vector3& zAxis,
337 const Vector3& translation );
340 * @brief Gets the position, scale and rotation components from the given transform matrix.
342 * @pre This matrix must not contain skews or shears.
343 * @param[out] position to set
344 * @param[out] rotation to set - only valid if the transform matrix has not been skewed or sheared
345 * @param[out] scale to set - only valid if the transform matrix has not been skewed or sheared
347 void GetTransformComponents(Vector3& position,
348 Quaternion& rotation,
349 Vector3& scale) const;
353 float mMatrix[16]; ///< The elements of the matrix
357 * @brief Print a matrix.
359 * It is printed in memory order, i.e. each printed row is contiguous in memory.
360 * @param [in] o The output stream operator.
361 * @param [in] matrix The matrix to print.
362 * @return The output stream operator.
364 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
371 #endif // __DALI_MATRIX_H__