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