[devel_3.0_main] Cherry-pick Beautification of source-code. 80383
[platform/framework/native/appfw.git] / src / base / FBaseDoubleMatrix3.cpp
1 //
2 // Copyright (c) 2012 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  * @file        FBaseDoubleMatrix3.cpp
19  * @brief       This is the implementation for DoubleMatrix class.
20  */
21 #include <FBaseDoubleMatrix3.h>
22 #include <FBaseSysLog.h>
23
24 namespace Tizen { namespace Base
25 {
26
27 static const int MATRIX_SIZE = sizeof(double) * 9;
28 #define SWAP_VALUES(a, b, tmp) \
29         tmp = a; \
30         a = b; \
31         b = tmp;
32
33 DoubleMatrix3::DoubleMatrix3(void)
34         : __pImpl(null)
35 {
36         SetAsNull();
37 }
38
39 DoubleMatrix3::DoubleMatrix3(const DoubleMatrix3& rhs)
40         : __pImpl(null)
41 {
42         memcpy(matrix, rhs.matrix, MATRIX_SIZE);
43 }
44
45 DoubleMatrix3::DoubleMatrix3(const double matrix[3][3])
46         : __pImpl(null)
47 {
48         memcpy(this->matrix, matrix, MATRIX_SIZE);
49 }
50
51 DoubleMatrix3::~DoubleMatrix3(void)
52 {
53 }
54
55 bool
56 DoubleMatrix3::operator ==(const DoubleMatrix3& rhs) const
57 {
58         if (this == &rhs)
59         {
60                 return true;
61         }
62
63         return (matrix[0][0] == rhs.matrix[0][0] &&
64                         matrix[0][1] == rhs.matrix[0][1] &&
65                         matrix[0][2] == rhs.matrix[0][2] &&
66                         matrix[1][0] == rhs.matrix[1][0] &&
67                         matrix[1][1] == rhs.matrix[1][1] &&
68                         matrix[1][2] == rhs.matrix[1][2] &&
69                         matrix[2][0] == rhs.matrix[2][0] &&
70                         matrix[2][1] == rhs.matrix[2][1] &&
71                         matrix[2][2] == rhs.matrix[2][2]);
72 }
73
74 bool
75 DoubleMatrix3::operator !=(const DoubleMatrix3& rhs) const
76 {
77         return !(*this == rhs);
78 }
79
80 DoubleMatrix3&
81 DoubleMatrix3::operator =(const DoubleMatrix3& rhs)
82 {
83         if (this != &rhs)
84         {
85                 memcpy(matrix, rhs.matrix, sizeof(double) * 9);
86         }
87
88         return *this;
89 }
90
91 DoubleMatrix3&
92 DoubleMatrix3::operator =(double value)
93 {
94         matrix[0][0] = value;
95         matrix[1][0] = value;
96         matrix[2][0] = value;
97
98         matrix[0][1] = value;
99         matrix[1][1] = value;
100         matrix[2][1] = value;
101
102         matrix[0][2] = value;
103         matrix[1][2] = value;
104         matrix[2][2] = value;
105
106         return *this;
107 }
108
109 DoubleMatrix3
110 DoubleMatrix3::operator *(const DoubleMatrix3& rhs) const  // need to check performance
111 {
112         DoubleMatrix3 resultMatrix;
113
114         resultMatrix.matrix[0][0] = matrix[0][0] * rhs.matrix[0][0] + matrix[0][1] * rhs.matrix[1][0] + matrix[0][2] * rhs.matrix[2][0];
115         resultMatrix.matrix[0][1] = matrix[0][0] * rhs.matrix[0][1] + matrix[0][1] * rhs.matrix[1][1] + matrix[0][2] * rhs.matrix[2][1];
116         resultMatrix.matrix[0][2] = matrix[0][0] * rhs.matrix[0][2] + matrix[0][1] * rhs.matrix[1][2] + matrix[0][2] * rhs.matrix[2][2];
117
118         resultMatrix.matrix[1][0] = matrix[1][0] * rhs.matrix[0][0] + matrix[1][1] * rhs.matrix[1][0] + matrix[1][2] * rhs.matrix[2][0];
119         resultMatrix.matrix[1][1] = matrix[1][0] * rhs.matrix[0][1] + matrix[1][1] * rhs.matrix[1][1] + matrix[1][2] * rhs.matrix[2][1];
120         resultMatrix.matrix[1][2] = matrix[1][0] * rhs.matrix[0][2] + matrix[1][1] * rhs.matrix[1][2] + matrix[1][2] * rhs.matrix[2][2];
121
122         resultMatrix.matrix[2][0] = matrix[2][0] * rhs.matrix[0][0] + matrix[2][1] * rhs.matrix[1][0] + matrix[2][2] * rhs.matrix[2][0];
123         resultMatrix.matrix[2][1] = matrix[2][0] * rhs.matrix[0][1] + matrix[2][1] * rhs.matrix[1][1] + matrix[2][2] * rhs.matrix[2][1];
124         resultMatrix.matrix[2][2] = matrix[2][0] * rhs.matrix[0][2] + matrix[2][1] * rhs.matrix[1][2] + matrix[2][2] * rhs.matrix[2][2];
125
126         return resultMatrix;
127 }
128
129 DoubleMatrix3
130 DoubleMatrix3::operator *(double value) const
131 {
132         DoubleMatrix3 resultMatrix(matrix);
133
134         resultMatrix.matrix[0][0] *= value;
135         resultMatrix.matrix[0][1] *= value;
136         resultMatrix.matrix[0][2] *= value;
137
138         resultMatrix.matrix[1][0] *= value;
139         resultMatrix.matrix[1][1] *= value;
140         resultMatrix.matrix[1][2] *= value;
141
142         resultMatrix.matrix[2][0] *= value;
143         resultMatrix.matrix[2][1] *= value;
144         resultMatrix.matrix[2][2] *= value;
145
146         return resultMatrix;
147 }
148
149 DoubleMatrix3
150 DoubleMatrix3::operator +(const DoubleMatrix3& rhs) const
151 {
152         DoubleMatrix3 resultMatrix(matrix);
153
154         resultMatrix.matrix[0][0] += rhs.matrix[0][0];
155         resultMatrix.matrix[0][1] += rhs.matrix[0][1];
156         resultMatrix.matrix[0][2] += rhs.matrix[0][2];
157
158         resultMatrix.matrix[1][0] += rhs.matrix[1][0];
159         resultMatrix.matrix[1][1] += rhs.matrix[1][1];
160         resultMatrix.matrix[1][2] += rhs.matrix[1][2];
161
162         resultMatrix.matrix[2][0] += rhs.matrix[2][0];
163         resultMatrix.matrix[2][1] += rhs.matrix[2][1];
164         resultMatrix.matrix[2][2] += rhs.matrix[2][2];
165
166         return resultMatrix;
167 }
168
169 DoubleMatrix3
170 DoubleMatrix3::operator +(double value) const
171 {
172         DoubleMatrix3 resultMatrix(matrix);
173
174         resultMatrix.matrix[0][0] += value;
175         resultMatrix.matrix[0][1] += value;
176         resultMatrix.matrix[0][2] += value;
177
178         resultMatrix.matrix[1][0] += value;
179         resultMatrix.matrix[1][1] += value;
180         resultMatrix.matrix[1][2] += value;
181
182         resultMatrix.matrix[2][0] += value;
183         resultMatrix.matrix[2][1] += value;
184         resultMatrix.matrix[2][2] += value;
185
186         return resultMatrix;
187 }
188
189 DoubleMatrix3
190 DoubleMatrix3::operator -(const DoubleMatrix3& rhs) const
191 {
192         DoubleMatrix3 resultMatrix(matrix);
193
194         resultMatrix.matrix[0][0] -= rhs.matrix[0][0];
195         resultMatrix.matrix[0][1] -= rhs.matrix[0][1];
196         resultMatrix.matrix[0][2] -= rhs.matrix[0][2];
197
198         resultMatrix.matrix[1][0] -= rhs.matrix[1][0];
199         resultMatrix.matrix[1][1] -= rhs.matrix[1][1];
200         resultMatrix.matrix[1][2] -= rhs.matrix[1][2];
201
202         resultMatrix.matrix[2][0] -= rhs.matrix[2][0];
203         resultMatrix.matrix[2][1] -= rhs.matrix[2][1];
204         resultMatrix.matrix[2][2] -= rhs.matrix[2][2];
205
206         return resultMatrix;
207 }
208
209 DoubleMatrix3
210 DoubleMatrix3::operator -(double value) const
211 {
212         DoubleMatrix3 resultMatrix(matrix);
213
214         resultMatrix.matrix[0][0] -= value;
215         resultMatrix.matrix[0][1] -= value;
216         resultMatrix.matrix[0][2] -= value;
217
218         resultMatrix.matrix[1][0] -= value;
219         resultMatrix.matrix[1][1] -= value;
220         resultMatrix.matrix[1][2] -= value;
221
222         resultMatrix.matrix[2][0] -= value;
223         resultMatrix.matrix[2][1] -= value;
224         resultMatrix.matrix[2][2] -= value;
225
226         return resultMatrix;
227 }
228
229 DoubleMatrix3&
230 DoubleMatrix3::operator *=(const DoubleMatrix3& rhs)
231 {
232         *this = *this * rhs;
233         return *this;
234 }
235
236 DoubleMatrix3&
237 DoubleMatrix3::operator *=(double value)
238 {
239         *this = *this * value;
240         return *this;
241 }
242
243 DoubleMatrix3&
244 DoubleMatrix3::operator +=(const DoubleMatrix3& rhs)
245 {
246         *this = *this + rhs;
247         return *this;
248 }
249
250 DoubleMatrix3&
251 DoubleMatrix3::operator +=(double value)
252 {
253         *this = *this + value;
254         return *this;
255 }
256
257 DoubleMatrix3&
258 DoubleMatrix3::operator -=(const DoubleMatrix3& rhs)
259 {
260         *this = *this - rhs;
261         return *this;
262 }
263
264 DoubleMatrix3&
265 DoubleMatrix3::operator -=(double value)
266 {
267         *this = *this - value;
268         return *this;
269 }
270
271 DoubleMatrix3
272 operator +(const double& value, const DoubleMatrix3& rhs)
273 {
274         return rhs + value;
275 }
276
277 DoubleMatrix3
278 operator *(const double& value, const DoubleMatrix3& rhs)
279 {
280         return rhs * value;
281 }
282
283 DoubleMatrix3
284 operator -(const double& value, const DoubleMatrix3& rhs)
285 {
286         DoubleMatrix3 returnMatrix(rhs);
287         returnMatrix.Negate();
288         returnMatrix += value;
289         return returnMatrix;
290 }
291
292 bool
293 DoubleMatrix3::Equals(const Tizen::Base::Object& obj) const
294 {
295         const DoubleMatrix3* pMatrix = dynamic_cast< const DoubleMatrix3* >(&obj);
296
297         if (pMatrix == null)
298         {
299                 return false;
300         }
301
302         return (*this == *pMatrix);
303 }
304
305 int
306 DoubleMatrix3::GetHashCode(void) const
307 {
308         return (Tizen::Base::Double::GetHashCode(this->matrix[0][0]) + Tizen::Base::Double::GetHashCode(this->matrix[1][0]) +
309                         Tizen::Base::Double::GetHashCode(this->matrix[2][0]) + Tizen::Base::Double::GetHashCode(this->matrix[0][1]) +
310                         Tizen::Base::Double::GetHashCode(this->matrix[1][1]) + Tizen::Base::Double::GetHashCode(this->matrix[2][1]) +
311                         Tizen::Base::Double::GetHashCode(this->matrix[0][2]) + Tizen::Base::Double::GetHashCode(this->matrix[1][2]) +
312                         Tizen::Base::Double::GetHashCode(this->matrix[2][2]));
313 }
314
315 double
316 DoubleMatrix3::GetDeterminant(void) const
317 {
318         double a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
319         double a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
320         double a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
321
322         return matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
323 }
324
325 DoubleMatrix3
326 DoubleMatrix3::GetInverse(void) const
327 {
328         double a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
329         double a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
330         double a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
331
332         double determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
333
334         if (Tizen::Base::Double::Compare(determinant, 0.0f) == 0)
335         {
336                 return *this;
337         }
338
339         double inverseMatrix[3][3];
340
341         inverseMatrix[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / determinant;
342         inverseMatrix[1][0] = (-matrix[1][0] * matrix[2][2] + matrix[1][2] * matrix[2][0]) / determinant;
343         inverseMatrix[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / determinant;
344
345         inverseMatrix[0][1] = (-matrix[0][1] * matrix[2][2] + matrix[0][2] * matrix[2][1]) / determinant;
346         inverseMatrix[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / determinant;
347         inverseMatrix[2][1] = (-matrix[0][0] * matrix[2][1] + matrix[0][1] * matrix[2][0]) / determinant;
348
349         inverseMatrix[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / determinant;
350         inverseMatrix[1][2] = (-matrix[0][0] * matrix[1][2] + matrix[0][2] * matrix[1][0]) / determinant;
351         inverseMatrix[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / determinant;
352
353         return DoubleMatrix3(inverseMatrix);
354 }
355
356 double
357 DoubleMatrix3::GetTrace(void) const
358 {
359         return matrix[0][0] + matrix[1][1] + matrix[2][2];
360 }
361
362 DoubleMatrix3
363 DoubleMatrix3::GetTranspose(void) const
364 {
365         DoubleMatrix3 transposeMatrix(matrix);
366         transposeMatrix.Transpose();
367
368         return transposeMatrix;
369 }
370
371 bool
372 DoubleMatrix3::IsIdentity(void) const
373 {
374         if ((Tizen::Base::Double::Compare(matrix[0][0], 1.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][1], 1.0f) != 0) ||
375                 (Tizen::Base::Double::Compare(matrix[2][2], 1.0f) != 0))
376         {
377                 return false;
378         }
379
380         if ((Tizen::Base::Double::Compare(matrix[0][1], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[0][2], 0.0f) != 0) ||
381                 (Tizen::Base::Double::Compare(matrix[1][0], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][2], 0.0f) != 0) ||
382                 (Tizen::Base::Double::Compare(matrix[2][0], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[2][1], 0.0f) != 0))
383         {
384                 return false;
385         }
386
387         return true;
388 }
389
390 bool
391 DoubleMatrix3::IsInvertible(void) const
392 {
393         if (Tizen::Base::Double::Compare(GetDeterminant(), 0.0f) == 0)
394         {
395                 return false;
396         }
397
398         return true;
399 }
400
401 void
402 DoubleMatrix3::Negate(void)
403 {
404         matrix[0][0] = -matrix[0][0];
405         matrix[0][1] = -matrix[0][1];
406         matrix[0][2] = -matrix[0][2];
407
408         matrix[1][0] = -matrix[1][0];
409         matrix[1][1] = -matrix[1][1];
410         matrix[1][2] = -matrix[1][2];
411
412         matrix[2][0] = -matrix[2][0];
413         matrix[2][1] = -matrix[2][1];
414         matrix[2][2] = -matrix[2][2];
415 }
416
417 void
418 DoubleMatrix3::SetAsIdentity(void)
419 {
420         matrix[0][0] = 1.0f;
421         matrix[0][1] = 0.0f;
422         matrix[0][2] = 0.0f;
423
424         matrix[1][0] = 0.0f;
425         matrix[1][1] = 1.0f;
426         matrix[1][2] = 0.0f;
427
428         matrix[2][0] = 0.0f;
429         matrix[2][1] = 0.0f;
430         matrix[2][2] = 1.0f;
431 }
432
433 result
434 DoubleMatrix3::Invert(void)
435 {
436         double a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
437         double a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
438         double a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
439
440         double determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
441
442         SysTryReturnResult(NID_BASE, Tizen::Base::Double::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
443                 "The current instance is not invertible.");
444
445         double inverseMatrix[3][3];
446
447         inverseMatrix[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / determinant;
448         inverseMatrix[1][0] = (-matrix[1][0] * matrix[2][2] + matrix[1][2] * matrix[2][0]) / determinant;
449         inverseMatrix[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / determinant;
450
451         inverseMatrix[0][1] = (-matrix[0][1] * matrix[2][2] + matrix[0][2] * matrix[2][1]) / determinant;
452         inverseMatrix[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / determinant;
453         inverseMatrix[2][1] = (-matrix[0][0] * matrix[2][1] + matrix[0][1] * matrix[2][0]) / determinant;
454
455         inverseMatrix[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / determinant;
456         inverseMatrix[1][2] = (-matrix[0][0] * matrix[1][2] + matrix[0][2] * matrix[1][0]) / determinant;
457         inverseMatrix[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / determinant;
458
459         memcpy(matrix, inverseMatrix, MATRIX_SIZE);
460
461         return E_SUCCESS;
462 }
463
464 void
465 DoubleMatrix3::Transpose(void)
466 {
467         double tmp = 0.0f;
468         SWAP_VALUES(matrix[0][1], matrix[1][0], tmp);
469         SWAP_VALUES(matrix[0][2], matrix[2][0], tmp);
470
471         SWAP_VALUES(matrix[1][2], matrix[2][1], tmp);
472 }
473
474 void
475 DoubleMatrix3::SetAsNull(void)
476 {
477         matrix[0][0] = 0.0f;
478         matrix[0][1] = 0.0f;
479         matrix[0][2] = 0.0f;
480
481         matrix[1][0] = 0.0f;
482         matrix[1][1] = 0.0f;
483         matrix[1][2] = 0.0f;
484
485         matrix[2][0] = 0.0f;
486         matrix[2][1] = 0.0f;
487         matrix[2][2] = 0.0f;
488 }
489
490 }} // Tizen::Base