05524aa8c379eda079b18c36ee75b63ea7770b44
[platform/core/uifw/dali-core.git] / dali / public-api / math / matrix3.h
1 #ifndef __DALI_MATRIX3_H__
2 #define __DALI_MATRIX3_H__
3
4 /*
5  * Copyright (c) 2015 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 // INTERNAL INCLUDES
22 #include <dali/public-api/math/vector3.h>
23 #include <dali/public-api/math/matrix.h>
24 #include <dali/public-api/common/type-traits.h>
25
26 namespace Dali
27 {
28
29 class Matrix;
30 struct Vector2;
31
32 /**
33  * @brief A 3x3 matrix.
34  *
35  */
36 class DALI_IMPORT_API Matrix3
37 {
38 public:
39
40   friend std::ostream& operator<< (std::ostream& o, const Matrix3& matrix);
41
42   /**
43    * @brief The identity matrix
44    */
45   static const Matrix3 IDENTITY;
46
47   /**
48    * @brief Constructor.
49    */
50   Matrix3();
51
52   /**
53    * @brief Copy Constructor.
54    *
55    * @param[in] m Another 3x3 matrix
56    */
57   Matrix3(const Matrix3& m);
58
59   /**
60    * @brief Constructor.
61    *
62    * @param[in] m A 4x4 matrix. The translation and shear components are ignored.
63    */
64   Matrix3(const Matrix& m);
65
66   /**
67    * @brief Constructor.
68    *
69    * @param[in] s00 First element
70    * @param[in] s01 Second element
71    * @param[in] s02 Third element
72    * @param[in] s10 Fourth element
73    * @param[in] s11 Fifth element
74    * @param[in] s12 Sixth element
75    * @param[in] s20 Seventh element
76    * @param[in] s21 Eighth element
77    * @param[in] s22 Ninth element
78    */
79   Matrix3(float s00, float s01, float s02, float s10, float s11, float s12, float s20, float s21, float s22);
80
81   /**
82    * @brief Assignment Operator
83    * @param matrix from which to copy values
84    * @return reference to this object
85    */
86   Matrix3& operator=( const Matrix3& matrix );
87
88   /**
89    * @brief Assignment Operator
90    * @param matrix from which to copy values
91    * @return reference to this object
92    */
93   Matrix3& operator=( const Matrix& matrix );
94
95   /**
96    * @brief The equality operator.
97    *
98    * Utilises appropriate machine epsilon values.
99    *
100    * @param [in] rhs    the Matrix to compare this to
101    * @return true if the matrices are equal
102    */
103   bool operator==(const Matrix3 & rhs) const;
104
105   /**
106    * @brief The inequality operator.
107    *
108    * Utilises appropriate machine epsilon values.
109    *
110    * @param [in] rhs    the Matrix to compare this to
111    * @return true if the matrices are equal
112    */
113   bool operator!=(const Matrix3 & rhs) const;
114
115   /**
116    * @brief Destructor.
117    */
118   ~Matrix3()
119   {
120   }
121
122   /**
123    * @brief Sets the matrix to the identity matrix.
124    */
125   void SetIdentity();
126
127   /**
128    * @brief Returns the contents of the matrix as an array of 9 floats.
129    *
130    * The order of the values for a matrix is:
131    *   xAxis.x yAxis.x zAxis.x
132    *   xAxis.y yAxis.y zAxis.y
133    *   xAxis.z yAxis.z zAxis.z
134    * @return the matrix contents as an array of 9 floats.
135    */
136   const float* AsFloat() const {return &mElements[0];}
137
138   /**
139    * @brief Returns the contents of the matrix as an array of 9 floats.
140    *
141    * The order of the values for a matrix is:
142    *   xAxis.x yAxis.x zAxis.x
143    *   xAxis.y yAxis.y zAxis.y
144    *   xAxis.z yAxis.z zAxis.z
145    * @return the matrix contents as an array of 9 floats.
146    */
147   float* AsFloat() {return &mElements[0];}
148
149   /**
150    * @brief Inverts the matrix.
151    *
152    * @return true if successful
153    */
154   bool Invert();
155
156   /**
157    * @brief Swaps the rows to columns
158    * @return true
159    */
160   bool Transpose();
161
162   /**
163    * @brief Multiplies all elements of the matrix by the scale value.
164    *
165    * @param scale - the value by which to scale the whole matrix.
166
167    */
168   void Scale(float scale);
169
170   /**
171    * @brief Magnitude returns the average of the absolute values of the
172    * elements * 3.
173    *
174    * (The Magnitude of the unit matrix is therefore 1)
175    * @return the magnitude - always positive.
176    */
177   float Magnitude() const;
178
179   /**
180    * @brief If the matrix is invertible, then this method inverts, transposes
181    * and scales the matrix such that the resultant element values
182    * average 1.
183    *
184    * If the matrix is not invertible, then the matrix is left unchanged.
185
186    * @return true if the matrix is invertible, otherwise false
187    */
188   bool ScaledInverseTranspose();
189
190   /**
191    * @brief Function to multiply two matrices and store the result onto third.
192    *
193    * Use this method in time critical path as it does not require temporaries
194    * @param result of the multiplication
195    * @param lhs matrix, this can be same matrix as result
196    * @param rhs matrix, this cannot be same matrix as result
197    */
198   static void Multiply( Matrix3& result, const Matrix3& lhs, const Matrix3& rhs );
199
200 private:
201
202   float mElements[9]; ///< The elements of the matrix
203 };
204
205 /**
206  * @brief Print a 3x3 matrix.
207  *
208  * @param [in] o The output stream operator.
209  * @param [in] matrix The matrix to print.
210  * @return The output stream operator.
211  */
212 DALI_IMPORT_API std::ostream& operator<< (std::ostream& o, const Matrix3& matrix);
213
214 // Allow Matrix3 to be treated as a POD type
215 template <> struct TypeTraits< Matrix3 > : public BasicTypes< Matrix3 > { enum { IS_TRIVIAL_TYPE = true }; };
216
217 } // namespace Dali
218
219 #endif //__DALI_MATRIX3_H__