License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / public-api / math / matrix3.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/public-api/math/matrix3.h>
20
21 // EXTERNAL INCLUDES
22 #include <string.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/math/math-utils.h>
26
27 #define S00 0
28 #define S01 1
29 #define S02 2
30 #define S10 3
31 #define S11 4
32 #define S12 5
33 #define S20 6
34 #define S21 7
35 #define S22 8
36
37 /*
38  * S00 S01 S02
39  * S10 S11 S12
40  * S20 S21 S22
41  */
42
43 namespace
44 {
45 const size_t NUM_BYTES_IN_ROW    = 3*sizeof(float);
46 const size_t NUM_BYTES_IN_MATRIX = 9*sizeof(float);
47 }
48
49 namespace Dali
50 {
51
52 const Matrix3 Matrix3::IDENTITY(1.0f, 0.0f, 0.0f,
53                                 0.0f, 1.0f, 0.0f,
54                                 0.0f, 0.0f, 1.0f);
55
56 Matrix3::Matrix3()
57 {
58   float* m = AsFloat();
59   memset(m, 0, NUM_BYTES_IN_MATRIX);
60   mElements[S00]=1.0f;
61   mElements[S11]=1.0f;
62   mElements[S22]=1.0f;
63 }
64
65 Matrix3::Matrix3(const Matrix3& m)
66 {
67   memcpy( mElements, m.mElements, NUM_BYTES_IN_MATRIX );
68 }
69
70 Matrix3::Matrix3(const Matrix& matrix)
71 {
72   const float* m4 = matrix.AsFloat();
73   memcpy(&mElements[S00], m4,   NUM_BYTES_IN_ROW);
74   memcpy(&mElements[S10], m4+4, NUM_BYTES_IN_ROW);
75   memcpy(&mElements[S20], m4+8, NUM_BYTES_IN_ROW);
76 }
77
78 Matrix3::Matrix3(float s00, float s01, float s02, float s10, float s11, float s12, float s20, float s21, float s22)
79 {
80   mElements[S00] = s00;
81   mElements[S01] = s01;
82   mElements[S02] = s02;
83   mElements[S10] = s10;
84   mElements[S11] = s11;
85   mElements[S12] = s12;
86   mElements[S20] = s20;
87   mElements[S21] = s21;
88   mElements[S22] = s22;
89 }
90
91
92 void Matrix3::SetIdentity()
93 {
94   memset(mElements, 0, NUM_BYTES_IN_MATRIX);
95   mElements[S00]=1.0f;
96   mElements[S11]=1.0f;
97   mElements[S22]=1.0f;
98 }
99
100 Matrix3& Matrix3::operator=( const Matrix3& matrix )
101 {
102   // no point copying if self assigning
103   if( this != &matrix )
104   {
105     memcpy( AsFloat(), matrix.AsFloat(), NUM_BYTES_IN_MATRIX );
106   }
107   return *this;
108 }
109
110 Matrix3& Matrix3::operator=( const Matrix& matrix )
111 {
112   const float* m4 = matrix.AsFloat();
113   memcpy(&mElements[S00], m4,   NUM_BYTES_IN_ROW);
114   memcpy(&mElements[S10], m4+4, NUM_BYTES_IN_ROW);
115   memcpy(&mElements[S20], m4+8, NUM_BYTES_IN_ROW);
116   return *this;
117 }
118
119 bool Matrix3::Invert()
120 {
121   bool succeeded = false;
122
123   float cof[9];
124   cof[S00] = (mElements[S11] * mElements[S22] - mElements[S12] * mElements[S21]);
125   cof[S01] = (mElements[S02] * mElements[S21] - mElements[S01] * mElements[S22]);
126   cof[S02] = (mElements[S01] * mElements[S12] - mElements[S02] * mElements[S11]);
127
128   cof[S10] = (mElements[S12] * mElements[S20] - mElements[S10] * mElements[S22]);
129   cof[S11] = (mElements[S00] * mElements[S22] - mElements[S02] * mElements[S20]);
130   cof[S12] = (mElements[S02] * mElements[S10] - mElements[S00] * mElements[S12]);
131
132   cof[S20] = (mElements[S10] * mElements[S21] - mElements[S11] * mElements[S20]);
133   cof[S21] = (mElements[S01] * mElements[S20] - mElements[S00] * mElements[S21]);
134   cof[S22] = (mElements[S00] * mElements[S11] - mElements[S01] * mElements[S10]);
135
136   float det = mElements[S00] * cof[S00] + mElements[S01] * cof[S10] + mElements[S02] * cof[S20];
137
138   // In the case where the determinant is exactly zero, the matrix is non-invertible
139   if( ! EqualsZero( det ) )
140   {
141     det = 1.0 / det;
142     for (int i = 0; i < 9; i++)
143     {
144       mElements[i] = cof[i] * det;
145     }
146     succeeded = true;
147   }
148   return succeeded;
149 }
150
151 bool Matrix3::Transpose()
152 {
153   float tmp;
154   tmp = mElements[S01]; mElements[S01] = mElements[S10]; mElements[S10]=tmp;
155   tmp = mElements[S02]; mElements[S02] = mElements[S20]; mElements[S20]=tmp;
156   tmp = mElements[S21]; mElements[S21] = mElements[S12]; mElements[S12]=tmp;
157   return true;
158 }
159
160 bool Matrix3::ScaledInverseTranspose()
161 {
162   bool succeeded = false;
163
164   float cof[9];
165   cof[S00] = (mElements[S11] * mElements[S22] - mElements[S12] * mElements[S21]);
166   cof[S01] = (mElements[S02] * mElements[S21] - mElements[S01] * mElements[S22]);
167   cof[S02] = (mElements[S01] * mElements[S12] - mElements[S02] * mElements[S11]);
168
169   cof[S10] = (mElements[S12] * mElements[S20] - mElements[S10] * mElements[S22]);
170   cof[S11] = (mElements[S00] * mElements[S22] - mElements[S02] * mElements[S20]);
171   cof[S12] = (mElements[S02] * mElements[S10] - mElements[S00] * mElements[S12]);
172
173   cof[S20] = (mElements[S10] * mElements[S21] - mElements[S11] * mElements[S20]);
174   cof[S21] = (mElements[S01] * mElements[S20] - mElements[S00] * mElements[S21]);
175   cof[S22] = (mElements[S00] * mElements[S11] - mElements[S01] * mElements[S10]);
176
177   float det = mElements[S00] * cof[S00] + mElements[S01] * cof[S10] + mElements[S02] * cof[S20];
178
179   // In the case where the determinant is exactly zero, the matrix is non-invertible
180   if( ! EqualsZero( det ) )
181   {
182     // Use average rather than determinant to remove rounding to zero errors in further multiplication
183     float sum=0;
184     for(size_t i=0;i<9;i++)
185     {
186       sum+=fabsf(cof[i]);
187     }
188     float scale = 9.0f/sum; // Inverse of the average values
189     if (det < 0)
190     {
191       // Ensure the signs of the inverse are correct
192       scale = -scale;
193     }
194
195     mElements[S00] = cof[S00] * scale;
196     mElements[S01] = cof[S10] * scale;
197     mElements[S02] = cof[S20] * scale;
198
199     mElements[S10] = cof[S01] * scale;
200     mElements[S11] = cof[S11] * scale;
201     mElements[S12] = cof[S21] * scale;
202
203     mElements[S20] = cof[S02] * scale;
204     mElements[S21] = cof[S12] * scale;
205     mElements[S22] = cof[S22] * scale;
206
207     succeeded = true;
208   }
209   return succeeded;
210 }
211
212 void Matrix3::Scale(float scale)
213 {
214   mElements[S00] *= scale;
215   mElements[S01] *= scale;
216   mElements[S02] *= scale;
217   mElements[S10] *= scale;
218   mElements[S11] *= scale;
219   mElements[S12] *= scale;
220   mElements[S20] *= scale;
221   mElements[S21] *= scale;
222   mElements[S22] *= scale;
223 }
224
225 float Matrix3::Magnitude() const
226 {
227   float avg=0;
228   for(size_t i=0;i<9;i++)
229   {
230     avg+=fabsf(mElements[i]);
231   }
232   return avg/3.0f;
233 }
234
235
236 void Matrix3::Multiply( Matrix3& result, const Matrix3& lhs, const Matrix3& rhs )
237 {
238   float* temp = result.AsFloat();
239   const float* rhsPtr  = rhs.AsFloat();
240   const float* lhsPtr = lhs.AsFloat();
241
242   for( int i=0; i < 3; i++ )
243   {
244     int loc = i * 3;
245     int loc1 = loc + 1;
246     int loc2 = loc + 2;
247
248     float value0 = lhsPtr[loc];
249     float value1 = lhsPtr[loc1];
250     float value2 = lhsPtr[loc2];
251     temp[loc]  = (value0 * rhsPtr[0]) +
252                  (value1 * rhsPtr[3]) +
253                  (value2 * rhsPtr[6]);
254
255     temp[loc1] = (value0 * rhsPtr[1]) +
256                  (value1 * rhsPtr[4]) +
257                  (value2 * rhsPtr[7]);
258
259     temp[loc2] = (value0 * rhsPtr[2]) +
260                  (value1 * rhsPtr[5]) +
261                  (value2 * rhsPtr[8]);
262   }
263 }
264
265 bool Matrix3::operator==(const Matrix3 & rhs) const
266 {
267   return (
268     Equals( mElements[0], rhs.mElements[0]) &&
269     Equals( mElements[1], rhs.mElements[1]) &&
270     Equals( mElements[2], rhs.mElements[2]) &&
271     Equals( mElements[3], rhs.mElements[3]) &&
272     Equals( mElements[4], rhs.mElements[4]) &&
273     Equals( mElements[5], rhs.mElements[5]) &&
274     Equals( mElements[6], rhs.mElements[6]) &&
275     Equals( mElements[7], rhs.mElements[7]) &&
276     Equals( mElements[8], rhs.mElements[8]));
277 }
278
279 bool Matrix3::operator!=(const Matrix3& rhs) const
280 {
281   return !(*this == rhs);
282 }
283
284 std::ostream& operator<< (std::ostream& o, const Matrix3& matrix)
285 {
286   return o << "[ [" << matrix.mElements[0] << ", " << matrix.mElements[1] << ", " << matrix.mElements[2]  << "], "
287              << "[" << matrix.mElements[3] << ", " << matrix.mElements[4] << ", " << matrix.mElements[5]  << "], "
288              << "[" << matrix.mElements[6] << ", " << matrix.mElements[7] << ", " << matrix.mElements[8]  << "] ]";
289 }
290
291 } // namespace Dali