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.
25 #include <dali/public-api/math/vector4.h>
32 * @brief The Matrix class represents transformations and projections.
33 * It is agnostic w.r.t. row/column major notation - it operates on a flat array.
34 * 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.
36 class DALI_IMPORT_API Matrix
40 friend std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
45 * Zero initialises the matrix
52 * @param initialize to zero or leave uninitialized
54 explicit Matrix( bool initialize );
59 * The matrix is initialised with the contents of 'array' which must contain 16 floats.
60 * The order of the values for a transform matrix is:
62 * xAxis.x xAxis.y xAxis.z 0.0f
63 * yAxis.x yAxis.y yAxis.z 0.0f
64 * zAxis.x zAxis.y zAxis.z 0.0f
65 * trans.x trans.y trans.z 1.0f
67 * @param [in] array 16 floats
69 explicit Matrix(const float* array);
72 * @brief Constructs a matrix from quaternion.
74 * @param rotation as quaternion
76 explicit Matrix( const Quaternion& rotation );
79 * @brief Copy constructor.
81 * @param [in] matrix to copy values from
83 Matrix( const Matrix& matrix );
86 * @brief Assignment operator.
88 * @param [in] matrix to copy values from
89 * @return a reference to this
91 Matrix& operator=( const Matrix& matrix );
94 * @brief The identity matrix.
96 static const Matrix IDENTITY;
99 * @brief Sets this matrix to be an identity matrix.
104 * @brief Sets this matrix to be an identity matrix with scale.
106 * @param scale to set on top of identity matrix
108 void SetIdentityAndScale( const Vector3& scale );
111 * @brief Invert a transform Matrix.
113 * Any Matrix representing only a rotation and/or translation
114 * can be inverted using this function. It is faster and more accurate then using Invert().
115 * @param [out] result returns the inverse of this matrix
117 void InvertTransform(Matrix& result) const;
120 * @brief Generic brute force Matrix Invert.
122 * Using the Matrix invert function for the specific type
123 * of matrix you are dealing with is faster, more accurate.
124 * @return true if successful
129 * @brief Swaps the rows to columns.
134 * @brief Returns the xAxis from a Transform matrix.
138 Vector3 GetXAxis() const;
141 * @brief Returns the yAxis from a Transform matrix.
145 Vector3 GetYAxis() const;
148 * @brief Returns the zAxis from a Transform matrix.
152 Vector3 GetZAxis() const;
155 * @brief Sets the x axis.
157 * This assumes the matrix is a transform matrix.
158 * @param [in] axis the values to set the axis to
160 void SetXAxis(const Vector3& axis);
163 * @brief Sets the y axis.
165 * This assumes the matrix is a transform matrix.
166 * @param [in] axis the values to set the axis to
168 void SetYAxis(const Vector3& axis);
171 * @brief Sets the z axis.
173 * This assumes the matrix is a transform matrix.
174 * @param [in] axis the values to set the axis to
176 void SetZAxis(const Vector3& axis);
179 * @brief Gets the translation.
181 * This assumes the matrix is a transform matrix.
182 * @note inlined for performance reasons (generates less code than a function call)
183 * @return the translation
185 const Vector4& GetTranslation() const { return reinterpret_cast<const Vector4&>(mMatrix[12]); }
188 * @brief Gets the x,y and z components of the translation as a Vector3.
190 * This assumes the matrix is a transform matrix.
191 * @note inlined for performance reasons (generates less code than a function call)
192 * @return the translation
194 const Vector3& GetTranslation3() const { return reinterpret_cast<const Vector3&>(mMatrix[12]); }
197 * @brief Sets the translation.
199 * This assumes the matrix is a transform matrix.
200 * @param [in] translation the translation
202 void SetTranslation(const Vector4& translation);
205 * @brief Sets the x,y and z components of the translation from a Vector3.
207 * This assumes the matrix is a transform matrix.
208 * @param [in] translation the translation
210 void SetTranslation(const Vector3& translation);
213 * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
215 * This function is used to correct floating point errors which would otherwise accumulate
216 * as operations are applied to the matrix. This function assumes the matrix is a transform
219 void OrthoNormalize();
222 * @brief Returns the contents of the matrix as an array of 16 floats.
224 * The order of the values for a transform matrix is:
225 * xAxis.x xAxis.y xAxis.z 0.0f
226 * yAxis.x yAxis.y yAxis.z 0.0f
227 * zAxis.x zAxis.y zAxis.z 0.0f
228 * trans.x trans.y trans.z 1.0f
229 * @note inlined for performance reasons (generates less code than a function call)
230 * @return the matrix contents as an array of 16 floats.
232 const float* AsFloat() const {return mMatrix;}
235 * @brief Returns the contents of the matrix as an array of 16 floats.
237 * The order of the values for a transform matrix is:
239 * xAxis.x xAxis.y xAxis.z 0.0f
240 * yAxis.x yAxis.y yAxis.z 0.0f
241 * zAxis.x zAxis.y zAxis.z 0.0f
242 * trans.x trans.y trans.z 1.0f
243 * @note inlined for performance reasons (generates less code than a function call)
244 * @return the matrix contents as an array of 16 floats.
246 float* AsFloat() {return mMatrix;}
249 * @brief Function to multiply two matrices and store the result onto third.
251 * Use this method in time critical path as it does not require temporaries
252 * @param result of the multiplication
253 * @param lhs matrix, this can be same matrix as result
254 * @param rhs matrix, this cannot be same matrix as result
256 static void Multiply( Matrix& result, const Matrix& lhs, const Matrix& rhs );
259 * @brief Function to multiply a matrix and quaternion and store the result onto third.
261 * Use this method in time critical path as it does not require temporaries
262 * @param result of the multiplication
263 * @param lhs matrix, this can be same matrix as result
264 * @param rhs quaternion
266 static void Multiply( Matrix& result, const Matrix& lhs, const Quaternion& rhs );
269 * @brief The multiplication operator.
271 * @param [in] rhs the Matrix to multiply this by
272 * @return A matrix containing the result
274 Vector4 operator*(const Vector4& rhs) const;
277 * @brief The equality operator.
279 * Utilises appropriate machine epsilon values.
281 * @param [in] rhs the Matrix to compare this to
282 * @return true if the matrices are equal
284 bool operator==(const Matrix & rhs) const;
287 * @brief The inequality operator.
289 * Utilises appropriate machine epsilon values.
290 * @param [in] rhs the Matrix to compare this to
291 * @return true if the matrices are not equal.
293 bool operator!=(const Matrix & rhs) const;
296 * @brief Sets this matrix to contain the position, scale and rotation components.
298 * Performs scale, rotation, then translation
299 * @param[in] scale to apply
300 * @param[in] rotation to apply
301 * @param[in] translation to apply
303 void SetTransformComponents(const Vector3& scale,
304 const Quaternion& rotation,
305 const Vector3& translation );
308 * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
310 * Performs translation, then rotation, then scale.
311 * @param[in] scale to apply
312 * @param[in] rotation to apply
313 * @param[in] translation to apply
315 void SetInverseTransformComponents(const Vector3& scale,
316 const Quaternion& rotation,
317 const Vector3& translation );
321 * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
323 * Performs translation, then rotation.
324 * @param[in] xAxis The X axis of the basis
325 * @param[in] yAxis The Y axis of the basis
326 * @param[in] zAxis The Z axis of the basis
327 * @param[in] translation to apply
329 void SetInverseTransformComponents(const Vector3& xAxis,
330 const Vector3& yAxis,
331 const Vector3& zAxis,
332 const Vector3& translation );
335 * @brief Gets the position, scale and rotation components from the given transform matrix.
337 * @pre This matrix must not contain skews or shears.
338 * @param[out] position to set
339 * @param[out] rotation to set - only valid if the transform matrix has not been skewed or sheared
340 * @param[out] scale to set - only valid if the transform matrix has not been skewed or sheared
342 void GetTransformComponents(Vector3& position,
343 Quaternion& rotation,
344 Vector3& scale) const;
348 float mMatrix[16]; ///< The elements of the matrix
352 * @brief Print a matrix.
354 * It is printed in memory order, i.e. each printed row is contiguous in memory.
355 * @param [in] o The output stream operator.
356 * @param [in] matrix The matrix to print.
357 * @return The output stream operator.
359 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix& matrix);
363 #endif // __DALI_MATRIX_H__