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