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>
34 * @brief The Matrix class represents transformations and projections.
35 * It is agnostic w.r.t. row/column major notation - it operates on a flat array.
36 * 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.
38 class DALI_IMPORT_API Matrix
42 friend std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
47 * Zero initialises the matrix
54 * @param initialize to zero or leave uninitialized
56 explicit Matrix( bool initialize );
61 * The matrix is initialised with the contents of 'array' which must contain 16 floats.
62 * The order of the values for a transform matrix is:
64 * xAxis.x xAxis.y xAxis.z 0.0f
65 * yAxis.x yAxis.y yAxis.z 0.0f
66 * zAxis.x zAxis.y zAxis.z 0.0f
67 * trans.x trans.y trans.z 1.0f
69 * @param [in] array 16 floats
71 explicit Matrix(const float* array);
74 * @brief Constructs a matrix from quaternion.
76 * @param rotation as quaternion
78 explicit Matrix( const Quaternion& rotation );
81 * @brief Copy constructor.
83 * @param [in] matrix to copy values from
85 Matrix( const Matrix& matrix );
88 * @brief Assignment operator.
90 * @param [in] matrix to copy values from
91 * @return a reference to this
93 Matrix& operator=( const Matrix& matrix );
96 * @brief The identity matrix.
98 static const Matrix IDENTITY;
101 * @brief Sets this matrix to be an identity matrix.
106 * @brief Sets this matrix to be an identity matrix with scale.
108 * @param scale to set on top of identity matrix
110 void SetIdentityAndScale( const Vector3& scale );
113 * @brief Invert a transform Matrix.
115 * Any Matrix representing only a rotation and/or translation
116 * can be inverted using this function. It is faster and more accurate then using Invert().
117 * @param [out] result returns the inverse of this matrix
119 void InvertTransform(Matrix& result) const;
122 * @brief Generic brute force Matrix Invert.
124 * Using the Matrix invert function for the specific type
125 * of matrix you are dealing with is faster, more accurate.
126 * @return true if successful
131 * @brief Swaps the rows to columns.
136 * @brief Returns the xAxis from a Transform matrix.
140 Vector3 GetXAxis() const;
143 * @brief Returns the yAxis from a Transform matrix.
147 Vector3 GetYAxis() const;
150 * @brief Returns the zAxis from a Transform matrix.
154 Vector3 GetZAxis() const;
157 * @brief Sets the x axis.
159 * This assumes the matrix is a transform matrix.
160 * @param [in] axis the values to set the axis to
162 void SetXAxis(const Vector3& axis);
165 * @brief Sets the y axis.
167 * This assumes the matrix is a transform matrix.
168 * @param [in] axis the values to set the axis to
170 void SetYAxis(const Vector3& axis);
173 * @brief Sets the z axis.
175 * This assumes the matrix is a transform matrix.
176 * @param [in] axis the values to set the axis to
178 void SetZAxis(const Vector3& axis);
181 * @brief Gets the translation.
183 * This assumes the matrix is a transform matrix.
184 * @note inlined for performance reasons (generates less code than a function call)
185 * @return the translation
187 const Vector4& GetTranslation() const { return reinterpret_cast<const Vector4&>(mMatrix[12]); }
190 * @brief Gets the x,y and z components of the translation as a Vector3.
192 * This assumes the matrix is a transform matrix.
193 * @note inlined for performance reasons (generates less code than a function call)
194 * @return the translation
196 const Vector3& GetTranslation3() const { return reinterpret_cast<const Vector3&>(mMatrix[12]); }
199 * @brief Sets the translation.
201 * This assumes the matrix is a transform matrix.
202 * @param [in] translation the translation
204 void SetTranslation(const Vector4& translation);
207 * @brief Sets the x,y and z components of the translation from a Vector3.
209 * This assumes the matrix is a transform matrix.
210 * @param [in] translation the translation
212 void SetTranslation(const Vector3& translation);
215 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
217 * This function is used to correct floating point errors which would otherwise accumulate
218 * as operations are applied to the matrix. This function assumes the matrix is a transform
221 void OrthoNormalize();
224 * @brief Returns the contents of the matrix as an array of 16 floats.
226 * The order of the values for a transform matrix is:
227 * xAxis.x xAxis.y xAxis.z 0.0f
228 * yAxis.x yAxis.y yAxis.z 0.0f
229 * zAxis.x zAxis.y zAxis.z 0.0f
230 * trans.x trans.y trans.z 1.0f
231 * @note inlined for performance reasons (generates less code than a function call)
232 * @return the matrix contents as an array of 16 floats.
234 const float* AsFloat() const {return mMatrix;}
237 * @brief Returns the contents of the matrix as an array of 16 floats.
239 * The order of the values for a transform matrix is:
241 * xAxis.x xAxis.y xAxis.z 0.0f
242 * yAxis.x yAxis.y yAxis.z 0.0f
243 * zAxis.x zAxis.y zAxis.z 0.0f
244 * trans.x trans.y trans.z 1.0f
245 * @note inlined for performance reasons (generates less code than a function call)
246 * @return the matrix contents as an array of 16 floats.
248 float* AsFloat() {return mMatrix;}
251 * @brief Function to multiply two matrices and store the result onto third.
253 * Use this method in time critical path as it does not require temporaries
254 * @param result of the multiplication
255 * @param lhs matrix, this can be same matrix as result
256 * @param rhs matrix, this cannot be same matrix as result
258 static void Multiply( Matrix& result, const Matrix& lhs, const Matrix& rhs );
261 * @brief Function to multiply a matrix and quaternion and store the result onto third.
263 * Use this method in time critical path as it does not require temporaries
264 * @param result of the multiplication
265 * @param lhs matrix, this can be same matrix as result
266 * @param rhs quaternion
268 static void Multiply( Matrix& result, const Matrix& lhs, const Quaternion& rhs );
271 * @brief The multiplication operator.
273 * @param [in] rhs the Matrix to multiply this by
274 * @return A matrix containing the result
276 Vector4 operator*(const Vector4& rhs) const;
279 * @brief The equality operator.
281 * Utilises appropriate machine epsilon values.
283 * @param [in] rhs the Matrix to compare this to
284 * @return true if the matrices are equal
286 bool operator==(const Matrix & rhs) const;
289 * @brief The inequality operator.
291 * Utilises appropriate machine epsilon values.
292 * @param [in] rhs the Matrix to compare this to
293 * @return true if the matrices are not equal.
295 bool operator!=(const Matrix & rhs) const;
298 * @brief Sets this matrix to contain the position, scale and rotation components.
300 * Performs scale, rotation, then translation
301 * @param[in] scale to apply
302 * @param[in] rotation to apply
303 * @param[in] translation to apply
305 void SetTransformComponents(const Vector3& scale,
306 const Quaternion& rotation,
307 const Vector3& translation );
310 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
312 * Performs translation, then rotation, then scale.
313 * @param[in] scale to apply
314 * @param[in] rotation to apply
315 * @param[in] translation to apply
317 void SetInverseTransformComponents(const Vector3& scale,
318 const Quaternion& rotation,
319 const Vector3& translation );
323 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
325 * Performs translation, then rotation.
326 * @param[in] xAxis The X axis of the basis
327 * @param[in] yAxis The Y axis of the basis
328 * @param[in] zAxis The Z axis of the basis
329 * @param[in] translation to apply
331 void SetInverseTransformComponents(const Vector3& xAxis,
332 const Vector3& yAxis,
333 const Vector3& zAxis,
334 const Vector3& translation );
337 * @brief Gets the position, scale and rotation components from the given transform matrix.
339 * @pre This matrix must not contain skews or shears.
340 * @param[out] position to set
341 * @param[out] rotation to set - only valid if the transform matrix has not been skewed or sheared
342 * @param[out] scale to set - only valid if the transform matrix has not been skewed or sheared
344 void GetTransformComponents(Vector3& position,
345 Quaternion& rotation,
346 Vector3& scale) const;
350 float mMatrix[16]; ///< The elements of the matrix
354 * @brief Print a matrix.
356 * It is printed in memory order, i.e. each printed row is contiguous in memory.
357 * @param [in] o The output stream operator.
358 * @param [in] matrix The matrix to print.
359 * @return The output stream operator.
361 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
363 // Allow Matrix to be treated as a POD type
364 template <> struct TypeTraits< Matrix > : public BasicTypes< Matrix > { enum { IS_TRIVIAL_TYPE = true }; };
368 #endif // __DALI_MATRIX_H__