Formatted API
[platform/core/uifw/dali-core.git] / dali / public-api / math / matrix.h
1 #ifndef DALI_MATRIX_H
2 #define DALI_MATRIX_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <iosfwd>
23
24 // INTERNAL INCLUDES
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>
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_math
33  * @{
34  */
35
36 class Quaternion;
37
38 /**
39  * @brief The Matrix class represents transformations and projections.
40  *
41  * The matrix is stored as a flat array and is Column Major, i.e. the storage order is as follows (numbers represent
42  * indices of array):
43  *
44  * @code
45  *
46  * 0   4   8   12
47  * 1   5   9   13
48  * 2   6   10  14
49  * 3   7   11  15
50  *
51  * @endcode
52  *
53  * Each axis is contiguous in memory, so the x-axis corresponds to elements 0, 1, 2 and 3, the y-axis corresponds to
54  * elements 4, 5, 6, 7, the z-axis corresponds to elements 12, 13, 14 and 15, and the translation vector corresponds to
55  * elements 12, 13 and 14.
56  *
57  * @SINCE_1_0.0
58  */
59 class DALI_CORE_API Matrix
60 {
61 public:
62   friend DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Matrix& matrix);
63
64   /**
65    * @brief Constructor.
66    *
67    * Zero initializes the matrix.
68    * @SINCE_1_0.0
69    */
70   Matrix();
71
72   /**
73    * @brief Constructor.
74    *
75    * @SINCE_1_0.0
76    * @param[in] initialize True for initialization by zero or otherwise
77    */
78   explicit Matrix(bool initialize);
79
80   /**
81    * @brief Constructor.
82    *
83    * The matrix is initialized with the contents of 'array' which must contain 16 floats.
84    * The order of the values for a transform matrix is:
85    *
86    * @code
87    * [ xAxis.x, xAxis.y, xAxis.z, 0.0f, yAxis.x, yAxis.y, yAxis.z, 0.0f, zAxis.x, zAxis.y, zAxis.z, 0.0f, trans.x, trans.y, trans.z, 1.0f ]
88    * @endcode
89    *
90    * @SINCE_1_0.0
91    * @param[in] array Pointer of 16 floats data
92    */
93   explicit Matrix(const float* array);
94
95   /**
96    * @brief Constructs a matrix from quaternion.
97    *
98    * @SINCE_1_0.0
99    * @param rotation Rotation as quaternion
100    */
101   explicit Matrix(const Quaternion& rotation);
102
103   /**
104    * @brief Copy constructor.
105    *
106    * @SINCE_1_0.0
107    * @param[in] matrix A reference to the copied matrix
108    */
109   Matrix(const Matrix& matrix);
110
111   /**
112    * @brief Assignment operator.
113    *
114    * @SINCE_1_0.0
115    * @param[in] matrix A reference to the copied matrix
116    * @return A reference to this
117    */
118   Matrix& operator=(const Matrix& matrix);
119
120   /**
121    * @brief Move constructor.
122    *
123    * @SINCE_1_9.21
124    * @param[in] matrix A reference to the moved matrix
125    */
126   Matrix(Matrix&& matrix);
127
128   /**
129    * @brief Move assignment operator.
130    *
131    * @SINCE_1_9.21
132    * @param[in] matrix A reference to the moved matrix
133    * @return A reference to this
134    */
135   Matrix& operator=(Matrix&& matrix);
136
137   /**
138    * @brief The identity matrix.
139    */
140   static const Matrix IDENTITY;
141
142   /**
143    * @brief Sets this matrix to be an identity matrix.
144    * @SINCE_1_0.0
145    */
146   void SetIdentity();
147
148   /**
149    * @brief Sets this matrix to be an identity matrix with scale.
150    *
151    * @SINCE_1_0.0
152    * @param[in] scale Scale to set on top of identity matrix
153    */
154   void SetIdentityAndScale(const Vector3& scale);
155
156   /**
157    * @brief Inverts a transform Matrix.
158    *
159    * Any Matrix representing only a rotation and/or translation
160    * can be inverted using this function. It is faster and more accurate then using Invert().
161    * @SINCE_1_0.0
162    * @param[out] result The inverse of this matrix
163    */
164   void InvertTransform(Matrix& result) const;
165
166   /**
167    * @brief Generic brute force Matrix Invert.
168    *
169    * Using the Matrix invert function for the specific type
170    * of matrix you are dealing with is faster, more accurate.
171    * @SINCE_1_0.0
172    * @return True if successful
173    */
174   bool Invert();
175
176   /**
177    * @brief Swaps the rows to columns.
178    * @SINCE_1_0.0
179    */
180   void Transpose();
181
182   /**
183    * @brief Returns the xAxis from a Transform matrix.
184    *
185    * @SINCE_1_0.0
186    * @return The x axis
187    */
188   Vector3 GetXAxis() const;
189
190   /**
191    * @brief Returns the yAxis from a Transform matrix.
192    *
193    * @SINCE_1_0.0
194    * @return The y axis
195    */
196   Vector3 GetYAxis() const;
197
198   /**
199    * @brief Returns the zAxis from a Transform matrix.
200    *
201    * @SINCE_1_0.0
202    * @return The z axis
203    */
204   Vector3 GetZAxis() const;
205
206   /**
207    * @brief Sets the x axis.
208    *
209    * This assumes the matrix is a transform matrix.
210    * @SINCE_1_0.0
211    * @param[in] axis The values to set the axis to
212    */
213   void SetXAxis(const Vector3& axis);
214
215   /**
216    * @brief Sets the y axis.
217    *
218    * This assumes the matrix is a transform matrix.
219    * @SINCE_1_0.0
220    * @param[in] axis The values to set the axis to
221    */
222   void SetYAxis(const Vector3& axis);
223
224   /**
225    * @brief Sets the z axis.
226    *
227    * This assumes the matrix is a transform matrix.
228    * @SINCE_1_0.0
229    * @param[in] axis The values to set the axis to
230    */
231   void SetZAxis(const Vector3& axis);
232
233   /**
234    * @brief Gets the translation.
235    *
236    * This assumes the matrix is a transform matrix.
237    * @SINCE_1_0.0
238    * @return The translation
239    * @note inlined for performance reasons (generates less code than a function call)
240    */
241   const Vector4& GetTranslation() const
242   {
243     return reinterpret_cast<const Vector4&>(mMatrix[12]);
244   }
245
246   /**
247    * @brief Gets the x,y and z components of the translation as a Vector3.
248    *
249    * This assumes the matrix is a transform matrix.
250    * @SINCE_1_0.0
251    * @return The translation
252    * @note inlined for performance reasons (generates less code than a function call)
253    */
254   const Vector3& GetTranslation3() const
255   {
256     return reinterpret_cast<const Vector3&>(mMatrix[12]);
257   }
258
259   /**
260    * @brief Sets the translation.
261    *
262    * This assumes the matrix is a transform matrix.
263    * @SINCE_1_0.0
264    * @param[in] translation The translation
265    */
266   void SetTranslation(const Vector4& translation);
267
268   /**
269    * @brief Sets the x,y and z components of the translation from a Vector3.
270    *
271    * This assumes the matrix is a transform matrix.
272    * @SINCE_1_0.0
273    * @param[in] translation The translation
274    */
275   void SetTranslation(const Vector3& translation);
276
277   /**
278    * @brief Makes the axes of the matrix orthogonal to each other and of unit length.
279    *
280    * This function is used to correct floating point errors which would otherwise accumulate
281    * as operations are applied to the matrix. This function assumes the matrix is a transform
282    * matrix.
283    * @SINCE_1_0.0
284    */
285   void OrthoNormalize();
286
287   /**
288    * @brief Returns the contents of the matrix as an array of 16 floats.
289    *
290    * The order of the values for a transform matrix is:
291    *
292    * @code
293    * [ xAxis.x, xAxis.y, xAxis.z, 0.0f, yAxis.x, yAxis.y, yAxis.z, 0.0f, zAxis.x, zAxis.y, zAxis.z, 0.0f, trans.x, trans.y, trans.z, 1.0f ]
294    * @endcode
295    *
296    * @SINCE_1_0.0
297    * @return The matrix contents as an array of 16 floats
298    * @note inlined for performance reasons (generates less code than a function call)
299    */
300   const float* AsFloat() const
301   {
302     return mMatrix;
303   }
304
305   /**
306    * @brief Returns the contents of the matrix as an array of 16 floats.
307    *
308    * The order of the values for a transform matrix is:
309    *
310    * @code
311    * [ xAxis.x, xAxis.y, xAxis.z, 0.0f, yAxis.x, yAxis.y, yAxis.z, 0.0f, zAxis.x, zAxis.y, zAxis.z, 0.0f, trans.x, trans.y, trans.z, 1.0f ]
312    * @endcode
313    *
314    * @SINCE_1_0.0
315    * @return The matrix contents as an array of 16 floats
316    * @note inlined for performance reasons (generates less code than a function call)
317    */
318   float* AsFloat()
319   {
320     return mMatrix;
321   }
322
323   /**
324    * @brief Function to multiply two matrices and store the result onto third.
325    *
326    * Use this method in time critical path as it does not require temporaries.
327    *
328    * result = rhs * lhs
329    *
330    * @SINCE_1_0.0
331    * @param[out] result Result of the multiplication
332    * @param[in] lhs Matrix, this can be same matrix as result
333    * @param[in] rhs Matrix, this cannot be same matrix as result
334    */
335   static void Multiply(Matrix& result, const Matrix& lhs, const Matrix& rhs);
336
337   /**
338    * @brief Function to multiply a matrix and quaternion and store the result onto third.
339    *
340    * Use this method in time critical path as it does not require temporaries.
341    * @SINCE_1_0.0
342    * @param[out] result Result of the multiplication
343    * @param[in] lhs Matrix, this can be same matrix as result
344    * @param[in] rhs Quaternion
345    */
346   static void Multiply(Matrix& result, const Matrix& lhs, const Quaternion& rhs);
347
348   /**
349    * @brief The multiplication operator.
350    *
351    * Returned Vector = This Matrix * rhs
352    *
353    * @SINCE_1_0.0
354    * @param[in] rhs The Vector4 to multiply this by
355    * @return A Vector4 containing the result
356    */
357   Vector4 operator*(const Vector4& rhs) const;
358
359   /**
360    * @brief The equality operator.
361    *
362    * Utilizes appropriate machine epsilon values.
363    *
364    * @SINCE_1_0.0
365    * @param[in] rhs The Matrix to compare this to
366    * @return true if the matrices are equal
367    */
368   bool operator==(const Matrix& rhs) const;
369
370   /**
371    * @brief The inequality operator.
372    *
373    * Utilizes appropriate machine epsilon values.
374    * @SINCE_1_0.0
375    * @param[in] rhs The Matrix to compare this to
376    * @return true if the matrices are not equal.
377    */
378   bool operator!=(const Matrix& rhs) const;
379
380   /**
381    * @brief Sets this matrix to contain the position, scale and rotation components.
382    *
383    * Performs scale, rotation, then translation
384    * @SINCE_1_0.0
385    * @param[in] scale Scale to apply
386    * @param[in] rotation Rotation to apply
387    * @param[in] translation Translation to apply
388    */
389   void SetTransformComponents(const Vector3&    scale,
390                               const Quaternion& rotation,
391                               const Vector3&    translation);
392
393   /**
394    * @brief Sets this matrix to contain the inverse of the position, scale and rotation components.
395    *
396    * Performs translation, then rotation, then scale.
397    * @SINCE_1_0.0
398    * @param[in] scale Scale to apply
399    * @param[in] rotation Rotation to apply
400    * @param[in] translation Translation to apply
401    */
402   void SetInverseTransformComponents(const Vector3&    scale,
403                                      const Quaternion& rotation,
404                                      const Vector3&    translation);
405
406   /**
407    * @brief Sets this matrix to contain the inverse of the orthonormal basis and position components.
408    *
409    * Performs translation, then rotation.
410    * @SINCE_1_0.0
411    * @param[in] xAxis The X axis of the basis
412    * @param[in] yAxis The Y axis of the basis
413    * @param[in] zAxis The Z axis of the basis
414    * @param[in] translation Translation to apply
415    */
416   void SetInverseTransformComponents(const Vector3& xAxis,
417                                      const Vector3& yAxis,
418                                      const Vector3& zAxis,
419                                      const Vector3& translation);
420
421   /**
422    * @brief Gets the position, scale and rotation components from the given transform matrix.
423    *
424    * @SINCE_1_0.0
425    * @param[out] position Position to set
426    * @param[out] rotation Rotation to set - only valid if the transform matrix has not been skewed or sheared
427    * @param[out] scale Scale to set - only valid if the transform matrix has not been skewed or sheared
428    * @pre This matrix must not contain skews or shears.
429    */
430   void GetTransformComponents(Vector3&    position,
431                               Quaternion& rotation,
432                               Vector3&    scale) const;
433
434 private:
435   float mMatrix[16]; ///< The elements of the matrix
436 };
437
438 /**
439  * @brief Prints a matrix.
440  *
441  * It is printed in memory order.
442  * @SINCE_1_0.0
443  * @param[in] o The output stream operator
444  * @param[in] matrix The matrix to print
445  * @return The output stream operator
446  */
447 DALI_CORE_API std::ostream& operator<<(std::ostream& o, const Matrix& matrix);
448
449 // Allow Matrix to be treated as a POD type
450 template<>
451 struct TypeTraits<Matrix> : public BasicTypes<Matrix>
452 {
453   enum
454   {
455     IS_TRIVIAL_TYPE = true
456   };
457 };
458
459 /**
460  * @}
461  */
462 } // namespace Dali
463
464 #endif // DALI_MATRIX_H