[devel_3.0_main] Cherry-pick Beautification of source-code. 80383
[platform/framework/native/appfw.git] / src / base / FBaseDoubleMatrix4.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        FBaseDoubleMatrix4.cpp
19  * @brief       This is the implementation for DoubleMatrix class.
20  */
21 #include <FBaseDoubleMatrix4.h>
22 #include <FBaseSysLog.h>
23
24 namespace Tizen { namespace Base
25 {
26
27 static const int MATRIX_SIZE = sizeof(double) * 16;
28 #define SWAP_VALUES(a, b, tmp) \
29         tmp = a; \
30         a = b; \
31         b = tmp;
32
33 DoubleMatrix4::DoubleMatrix4(void)
34         : __pImpl(null)
35 {
36         SetAsNull();
37 }
38
39 DoubleMatrix4::DoubleMatrix4(const DoubleMatrix4& rhs)
40         : __pImpl(null)
41 {
42         memcpy(matrix, rhs.matrix, MATRIX_SIZE);
43 }
44
45 DoubleMatrix4::DoubleMatrix4(const double matrix[4][4])
46         : __pImpl(null)
47 {
48         memcpy(this->matrix, matrix, MATRIX_SIZE);
49 }
50
51 DoubleMatrix4::~DoubleMatrix4(void)
52 {
53 }
54
55 bool
56 DoubleMatrix4::operator ==(const DoubleMatrix4& 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[0][3] == rhs.matrix[0][3] &&
67                         matrix[1][0] == rhs.matrix[1][0] &&
68                         matrix[1][1] == rhs.matrix[1][1] &&
69                         matrix[1][2] == rhs.matrix[1][2] &&
70                         matrix[1][3] == rhs.matrix[1][3] &&
71                         matrix[2][0] == rhs.matrix[2][0] &&
72                         matrix[2][1] == rhs.matrix[2][1] &&
73                         matrix[2][2] == rhs.matrix[2][2] &&
74                         matrix[2][3] == rhs.matrix[2][3] &&
75                         matrix[3][0] == rhs.matrix[3][0] &&
76                         matrix[3][1] == rhs.matrix[3][1] &&
77                         matrix[3][2] == rhs.matrix[3][2] &&
78                         matrix[3][3] == rhs.matrix[3][3]);
79 }
80
81 bool
82 DoubleMatrix4::operator !=(const DoubleMatrix4& rhs) const
83 {
84         return !(*this == rhs);
85 }
86
87 DoubleMatrix4&
88 DoubleMatrix4::operator =(const DoubleMatrix4& rhs)
89 {
90         if (this != &rhs)
91         {
92                 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
93         }
94
95         return *this;
96 }
97
98 DoubleMatrix4&
99 DoubleMatrix4::operator =(double value)
100 {
101         matrix[0][0] = value;
102         matrix[1][0] = value;
103         matrix[2][0] = value;
104         matrix[3][0] = value;
105
106         matrix[0][1] = value;
107         matrix[1][1] = value;
108         matrix[2][1] = value;
109         matrix[3][1] = value;
110
111         matrix[0][2] = value;
112         matrix[1][2] = value;
113         matrix[2][2] = value;
114         matrix[3][2] = value;
115
116         matrix[0][3] = value;
117         matrix[1][3] = value;
118         matrix[2][3] = value;
119         matrix[3][3] = value;
120
121         return *this;
122 }
123
124 DoubleMatrix4
125 DoubleMatrix4::operator *(const DoubleMatrix4& rhs) const  // need to check performance
126 {
127         DoubleMatrix4 resultMatrix;
128
129         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] + matrix[0][3] * rhs.matrix[3][0];
130         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] + matrix[0][3] * rhs.matrix[3][1];
131         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] + matrix[0][3] * rhs.matrix[3][2];
132         resultMatrix.matrix[0][3] = matrix[0][0] * rhs.matrix[0][3] + matrix[0][1] * rhs.matrix[1][3] + matrix[0][2] * rhs.matrix[2][3] + matrix[0][3] * rhs.matrix[3][3];
133
134         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] + matrix[1][3] * rhs.matrix[3][0];
135         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] + matrix[1][3] * rhs.matrix[3][1];
136         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] + matrix[1][3] * rhs.matrix[3][2];
137         resultMatrix.matrix[1][3] = matrix[1][0] * rhs.matrix[0][3] + matrix[1][1] * rhs.matrix[1][3] + matrix[1][2] * rhs.matrix[2][3] + matrix[1][3] * rhs.matrix[3][3];
138
139         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] + matrix[2][3] * rhs.matrix[3][0];
140         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] + matrix[2][3] * rhs.matrix[3][1];
141         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] + matrix[2][3] * rhs.matrix[3][2];
142         resultMatrix.matrix[2][3] = matrix[2][0] * rhs.matrix[0][3] + matrix[2][1] * rhs.matrix[1][3] + matrix[2][2] * rhs.matrix[2][3] + matrix[2][3] * rhs.matrix[3][3];
143
144         resultMatrix.matrix[3][0] = matrix[3][0] * rhs.matrix[0][0] + matrix[3][1] * rhs.matrix[1][0] + matrix[3][2] * rhs.matrix[2][0] + matrix[3][3] * rhs.matrix[3][0];
145         resultMatrix.matrix[3][1] = matrix[3][0] * rhs.matrix[0][1] + matrix[3][1] * rhs.matrix[1][1] + matrix[3][2] * rhs.matrix[2][1] + matrix[3][3] * rhs.matrix[3][1];
146         resultMatrix.matrix[3][2] = matrix[3][0] * rhs.matrix[0][2] + matrix[3][1] * rhs.matrix[1][2] + matrix[3][2] * rhs.matrix[2][2] + matrix[3][3] * rhs.matrix[3][2];
147         resultMatrix.matrix[3][3] = matrix[3][0] * rhs.matrix[0][3] + matrix[3][1] * rhs.matrix[1][3] + matrix[3][2] * rhs.matrix[2][3] + matrix[3][3] * rhs.matrix[3][3];
148
149         return resultMatrix;
150 }
151
152 DoubleMatrix4
153 DoubleMatrix4::operator *(double value) const
154 {
155         DoubleMatrix4 resultMatrix(matrix);
156
157         resultMatrix.matrix[0][0] *= value;
158         resultMatrix.matrix[0][1] *= value;
159         resultMatrix.matrix[0][2] *= value;
160         resultMatrix.matrix[0][3] *= value;
161
162         resultMatrix.matrix[1][0] *= value;
163         resultMatrix.matrix[1][1] *= value;
164         resultMatrix.matrix[1][2] *= value;
165         resultMatrix.matrix[1][3] *= value;
166
167         resultMatrix.matrix[2][0] *= value;
168         resultMatrix.matrix[2][1] *= value;
169         resultMatrix.matrix[2][2] *= value;
170         resultMatrix.matrix[2][3] *= value;
171
172         resultMatrix.matrix[3][0] *= value;
173         resultMatrix.matrix[3][1] *= value;
174         resultMatrix.matrix[3][2] *= value;
175         resultMatrix.matrix[3][3] *= value;
176
177         return resultMatrix;
178 }
179
180 DoubleMatrix4
181 DoubleMatrix4::operator +(const DoubleMatrix4& rhs) const
182 {
183         DoubleMatrix4 resultMatrix(matrix);
184
185         resultMatrix.matrix[0][0] += rhs.matrix[0][0];
186         resultMatrix.matrix[0][1] += rhs.matrix[0][1];
187         resultMatrix.matrix[0][2] += rhs.matrix[0][2];
188         resultMatrix.matrix[0][3] += rhs.matrix[0][3];
189
190         resultMatrix.matrix[1][0] += rhs.matrix[1][0];
191         resultMatrix.matrix[1][1] += rhs.matrix[1][1];
192         resultMatrix.matrix[1][2] += rhs.matrix[1][2];
193         resultMatrix.matrix[1][3] += rhs.matrix[1][3];
194
195         resultMatrix.matrix[2][0] += rhs.matrix[2][0];
196         resultMatrix.matrix[2][1] += rhs.matrix[2][1];
197         resultMatrix.matrix[2][2] += rhs.matrix[2][2];
198         resultMatrix.matrix[2][3] += rhs.matrix[2][3];
199
200         resultMatrix.matrix[3][0] += rhs.matrix[3][0];
201         resultMatrix.matrix[3][1] += rhs.matrix[3][1];
202         resultMatrix.matrix[3][2] += rhs.matrix[3][2];
203         resultMatrix.matrix[3][3] += rhs.matrix[3][3];
204
205         return resultMatrix;
206 }
207
208 DoubleMatrix4
209 DoubleMatrix4::operator +(double value) const
210 {
211         DoubleMatrix4 resultMatrix(matrix);
212
213         resultMatrix.matrix[0][0] += value;
214         resultMatrix.matrix[0][1] += value;
215         resultMatrix.matrix[0][2] += value;
216         resultMatrix.matrix[0][3] += value;
217
218         resultMatrix.matrix[1][0] += value;
219         resultMatrix.matrix[1][1] += value;
220         resultMatrix.matrix[1][2] += value;
221         resultMatrix.matrix[1][3] += value;
222
223         resultMatrix.matrix[2][0] += value;
224         resultMatrix.matrix[2][1] += value;
225         resultMatrix.matrix[2][2] += value;
226         resultMatrix.matrix[2][3] += value;
227
228         resultMatrix.matrix[3][0] += value;
229         resultMatrix.matrix[3][1] += value;
230         resultMatrix.matrix[3][2] += value;
231         resultMatrix.matrix[3][3] += value;
232
233         return resultMatrix;
234 }
235
236 DoubleMatrix4
237 DoubleMatrix4::operator -(const DoubleMatrix4& rhs) const
238 {
239         DoubleMatrix4 resultMatrix(matrix);
240
241         resultMatrix.matrix[0][0] -= rhs.matrix[0][0];
242         resultMatrix.matrix[0][1] -= rhs.matrix[0][1];
243         resultMatrix.matrix[0][2] -= rhs.matrix[0][2];
244         resultMatrix.matrix[0][3] -= rhs.matrix[0][3];
245
246         resultMatrix.matrix[1][0] -= rhs.matrix[1][0];
247         resultMatrix.matrix[1][1] -= rhs.matrix[1][1];
248         resultMatrix.matrix[1][2] -= rhs.matrix[1][2];
249         resultMatrix.matrix[1][3] -= rhs.matrix[1][3];
250
251         resultMatrix.matrix[2][0] -= rhs.matrix[2][0];
252         resultMatrix.matrix[2][1] -= rhs.matrix[2][1];
253         resultMatrix.matrix[2][2] -= rhs.matrix[2][2];
254         resultMatrix.matrix[2][3] -= rhs.matrix[2][3];
255
256         resultMatrix.matrix[3][0] -= rhs.matrix[3][0];
257         resultMatrix.matrix[3][1] -= rhs.matrix[3][1];
258         resultMatrix.matrix[3][2] -= rhs.matrix[3][2];
259         resultMatrix.matrix[3][3] -= rhs.matrix[3][3];
260
261         return resultMatrix;
262 }
263
264 DoubleMatrix4
265 DoubleMatrix4::operator -(double value) const
266 {
267         DoubleMatrix4 resultMatrix(matrix);
268
269         resultMatrix.matrix[0][0] -= value;
270         resultMatrix.matrix[0][1] -= value;
271         resultMatrix.matrix[0][2] -= value;
272         resultMatrix.matrix[0][3] -= value;
273
274         resultMatrix.matrix[1][0] -= value;
275         resultMatrix.matrix[1][1] -= value;
276         resultMatrix.matrix[1][2] -= value;
277         resultMatrix.matrix[1][3] -= value;
278
279         resultMatrix.matrix[2][0] -= value;
280         resultMatrix.matrix[2][1] -= value;
281         resultMatrix.matrix[2][2] -= value;
282         resultMatrix.matrix[2][3] -= value;
283
284         resultMatrix.matrix[3][0] -= value;
285         resultMatrix.matrix[3][1] -= value;
286         resultMatrix.matrix[3][2] -= value;
287         resultMatrix.matrix[3][3] -= value;
288
289         return resultMatrix;
290 }
291
292 DoubleMatrix4&
293 DoubleMatrix4::operator *=(const DoubleMatrix4& rhs)
294 {
295         *this = *this * rhs;
296         return *this;
297 }
298
299 DoubleMatrix4&
300 DoubleMatrix4::operator *=(double value)
301 {
302         *this = *this * value;
303         return *this;
304 }
305
306 DoubleMatrix4&
307 DoubleMatrix4::operator +=(const DoubleMatrix4& rhs)
308 {
309         *this = *this + rhs;
310         return *this;
311 }
312
313 DoubleMatrix4&
314 DoubleMatrix4::operator +=(double value)
315 {
316         *this = *this + value;
317         return *this;
318 }
319
320 DoubleMatrix4&
321 DoubleMatrix4::operator -=(const DoubleMatrix4& rhs)
322 {
323         *this = *this - rhs;
324         return *this;
325 }
326
327 DoubleMatrix4&
328 DoubleMatrix4::operator -=(double value)
329 {
330         *this = *this - value;
331         return *this;
332 }
333
334 DoubleMatrix4
335 operator +(const double& value, const DoubleMatrix4& rhs)
336 {
337         return rhs + value;
338 }
339
340 DoubleMatrix4
341 operator *(const double& value, const DoubleMatrix4& rhs)
342 {
343         return rhs * value;
344 }
345
346 DoubleMatrix4
347 operator -(const double& value, const DoubleMatrix4& rhs)
348 {
349         DoubleMatrix4 returnMatrix(rhs);
350         returnMatrix.Negate();
351         returnMatrix += value;
352         return returnMatrix;
353 }
354
355 bool
356 DoubleMatrix4::Equals(const Tizen::Base::Object& obj) const
357 {
358         const DoubleMatrix4* pMatrix = dynamic_cast< const DoubleMatrix4* >(&obj);
359
360         if (pMatrix == null)
361         {
362                 return false;
363         }
364
365         return (*this == *pMatrix);
366 }
367
368 int
369 DoubleMatrix4::GetHashCode(void) const
370 {
371         return (Tizen::Base::Double::GetHashCode(matrix[0][0]) + Tizen::Base::Double::GetHashCode(matrix[1][0]) +
372                         Tizen::Base::Double::GetHashCode(matrix[2][0]) + Tizen::Base::Double::GetHashCode(matrix[3][0]) +
373                         Tizen::Base::Double::GetHashCode(matrix[0][1]) + Tizen::Base::Double::GetHashCode(matrix[1][1]) +
374                         Tizen::Base::Double::GetHashCode(matrix[2][1]) + Tizen::Base::Double::GetHashCode(matrix[3][1]) +
375                         Tizen::Base::Double::GetHashCode(matrix[0][2]) + Tizen::Base::Double::GetHashCode(matrix[1][2]) +
376                         Tizen::Base::Double::GetHashCode(matrix[2][2]) + Tizen::Base::Double::GetHashCode(matrix[3][2]) +
377                         Tizen::Base::Double::GetHashCode(matrix[0][3]) + Tizen::Base::Double::GetHashCode(matrix[1][3]) +
378                         Tizen::Base::Double::GetHashCode(matrix[2][3]) + Tizen::Base::Double::GetHashCode(matrix[3][3]));
379 }
380
381 double
382 DoubleMatrix4::GetDeterminant(void) const
383 {
384         double a0 = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
385         double a1 = matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0];
386         double a2 = matrix[0][0] * matrix[1][3] - matrix[0][3] * matrix[1][0];
387         double a3 = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];
388         double a4 = matrix[0][1] * matrix[1][3] - matrix[0][3] * matrix[1][1];
389         double a5 = matrix[0][2] * matrix[1][3] - matrix[0][3] * matrix[1][2];
390
391         double b0 = matrix[2][0] * matrix[3][1] - matrix[2][1] * matrix[3][0];
392         double b1 = matrix[2][0] * matrix[3][2] - matrix[2][2] * matrix[3][0];
393         double b2 = matrix[2][0] * matrix[3][3] - matrix[2][3] * matrix[3][0];
394         double b3 = matrix[2][1] * matrix[3][2] - matrix[2][2] * matrix[3][1];
395         double b4 = matrix[2][1] * matrix[3][3] - matrix[2][3] * matrix[3][1];
396         double b5 = matrix[2][2] * matrix[3][3] - matrix[2][3] * matrix[3][2];
397
398         return a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
399 }
400
401 DoubleMatrix4
402 DoubleMatrix4::GetInverse(void) const
403 {
404         double a0 = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
405         double a1 = matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0];
406         double a2 = matrix[0][0] * matrix[1][3] - matrix[0][3] * matrix[1][0];
407         double a3 = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];
408         double a4 = matrix[0][1] * matrix[1][3] - matrix[0][3] * matrix[1][1];
409         double a5 = matrix[0][2] * matrix[1][3] - matrix[0][3] * matrix[1][2];
410
411         double b0 = matrix[2][0] * matrix[3][1] - matrix[2][1] * matrix[3][0];
412         double b1 = matrix[2][0] * matrix[3][2] - matrix[2][2] * matrix[3][0];
413         double b2 = matrix[2][0] * matrix[3][3] - matrix[2][3] * matrix[3][0];
414         double b3 = matrix[2][1] * matrix[3][2] - matrix[2][2] * matrix[3][1];
415         double b4 = matrix[2][1] * matrix[3][3] - matrix[2][3] * matrix[3][1];
416         double b5 = matrix[2][2] * matrix[3][3] - matrix[2][3] * matrix[3][2];
417
418         double determinant = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
419
420         if (Tizen::Base::Double::Compare(determinant, 0.0f) == 0)
421         {
422                 return *this;
423         }
424
425         double inverseMatrix[4][4];
426
427         inverseMatrix[0][0] = (matrix[1][1] * b5 - matrix[1][2] * b4 + matrix[1][3] * b3) / determinant;
428         inverseMatrix[1][0] = (-matrix[1][0] * b5 + matrix[1][2] * b2 - matrix[1][3] * b1) / determinant;
429         inverseMatrix[2][0] = (matrix[1][0] * b4 - matrix[1][1] * b2 + matrix[1][3] * b0) / determinant;
430         inverseMatrix[3][0] = (-matrix[1][0] * b3 + matrix[1][1] * b1 - matrix[1][2] * b0) / determinant;
431
432         inverseMatrix[0][1] = (-matrix[0][1] * b5 + matrix[0][2] * b4 - matrix[0][3] * b3) / determinant;
433         inverseMatrix[1][1] = (matrix[0][0] * b5 - matrix[0][2] * b2 + matrix[0][3] * b1) / determinant;
434         inverseMatrix[2][1] = (-matrix[0][0] * b4 + matrix[0][1] * b2 - matrix[0][3] * b0) / determinant;
435         inverseMatrix[3][1] = (matrix[0][0] * b3 - matrix[0][1] * b1 + matrix[0][2] * b0) / determinant;
436
437         inverseMatrix[0][2] = (matrix[3][1] * a5 - matrix[3][2] * a4 + matrix[3][3] * a3) / determinant;
438         inverseMatrix[1][2] = (-matrix[3][0] * a5 + matrix[3][2] * a2 - matrix[3][3] * a1) / determinant;
439         inverseMatrix[2][2] = (matrix[3][0] * a4 - matrix[3][1] * a2 + matrix[3][3] * a0) / determinant;
440         inverseMatrix[3][2] = (-matrix[3][0] * a3 + matrix[3][1] * a1 - matrix[3][2] * a0) / determinant;
441
442         inverseMatrix[0][3] = (-matrix[2][1] * a5 + matrix[2][2] * a4 - matrix[2][3] * a3) / determinant;
443         inverseMatrix[1][3] = (matrix[2][0] * a5 - matrix[2][2] * a2 + matrix[2][3] * a1) / determinant;
444         inverseMatrix[2][3] = (-matrix[2][0] * a4 + matrix[2][1] * a2 - matrix[2][3] * a0) / determinant;
445         inverseMatrix[3][3] = (matrix[2][0] * a3 - matrix[2][1] * a1 + matrix[2][2] * a0) / determinant;
446
447         return DoubleMatrix4(inverseMatrix);
448 }
449
450 double
451 DoubleMatrix4::GetTrace(void) const
452 {
453         return matrix[0][0] + matrix[1][1] + matrix[2][2] + matrix[3][3];
454 }
455
456 DoubleMatrix4
457 DoubleMatrix4::GetTranspose(void) const
458 {
459         DoubleMatrix4 transposeMatrix(matrix);
460         transposeMatrix.Transpose();
461
462         return transposeMatrix;
463 }
464
465 bool
466 DoubleMatrix4::IsIdentity(void) const
467 {
468         if ((Tizen::Base::Double::Compare(matrix[0][0], 1.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][1], 1.0f) != 0) ||
469                 (Tizen::Base::Double::Compare(matrix[2][2], 1.0f) != 0) || (Tizen::Base::Double::Compare(matrix[3][3], 1.0f) != 0))
470         {
471                 return false;
472         }
473
474         if ((Tizen::Base::Double::Compare(matrix[0][1], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[0][2], 0.0f) != 0) ||
475                 (Tizen::Base::Double::Compare(matrix[0][3], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][0], 0.0f) != 0) ||
476                 (Tizen::Base::Double::Compare(matrix[1][2], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][3], 0.0f) != 0) ||
477                 (Tizen::Base::Double::Compare(matrix[2][0], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[2][1], 0.0f) != 0) ||
478                 (Tizen::Base::Double::Compare(matrix[2][3], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[3][0], 0.0f) != 0) ||
479                 (Tizen::Base::Double::Compare(matrix[3][1], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[3][2], 0.0f) != 0))
480         {
481                 return false;
482         }
483
484         return true;
485 }
486
487 bool
488 DoubleMatrix4::IsInvertible(void) const
489 {
490         if (Tizen::Base::Double::Compare(GetDeterminant(), 0.0f) == 0)
491         {
492                 return false;
493         }
494
495         return true;
496 }
497
498 void
499 DoubleMatrix4::Negate(void)
500 {
501         matrix[0][0] = -matrix[0][0];
502         matrix[0][1] = -matrix[0][1];
503         matrix[0][2] = -matrix[0][2];
504         matrix[0][3] = -matrix[0][3];
505
506         matrix[1][0] = -matrix[1][0];
507         matrix[1][1] = -matrix[1][1];
508         matrix[1][2] = -matrix[1][2];
509         matrix[1][3] = -matrix[1][3];
510
511         matrix[2][0] = -matrix[2][0];
512         matrix[2][1] = -matrix[2][1];
513         matrix[2][2] = -matrix[2][2];
514         matrix[2][3] = -matrix[2][3];
515
516         matrix[3][0] = -matrix[3][0];
517         matrix[3][1] = -matrix[3][1];
518         matrix[3][2] = -matrix[3][2];
519         matrix[3][3] = -matrix[3][3];
520 }
521
522 void
523 DoubleMatrix4::SetAsIdentity(void)
524 {
525         matrix[0][0] = 1.0f;
526         matrix[0][1] = 0.0f;
527         matrix[0][2] = 0.0f;
528         matrix[0][3] = 0.0f;
529
530         matrix[1][0] = 0.0f;
531         matrix[1][1] = 1.0f;
532         matrix[1][2] = 0.0f;
533         matrix[1][3] = 0.0f;
534
535         matrix[2][0] = 0.0f;
536         matrix[2][1] = 0.0f;
537         matrix[2][2] = 1.0f;
538         matrix[2][3] = 0.0f;
539
540         matrix[3][0] = 0.0f;
541         matrix[3][1] = 0.0f;
542         matrix[3][2] = 0.0f;
543         matrix[3][3] = 1.0f;
544 }
545
546 result
547 DoubleMatrix4::Invert(void)
548 {
549         double a0 = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
550         double a1 = matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0];
551         double a2 = matrix[0][0] * matrix[1][3] - matrix[0][3] * matrix[1][0];
552         double a3 = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];
553         double a4 = matrix[0][1] * matrix[1][3] - matrix[0][3] * matrix[1][1];
554         double a5 = matrix[0][2] * matrix[1][3] - matrix[0][3] * matrix[1][2];
555
556         double b0 = matrix[2][0] * matrix[3][1] - matrix[2][1] * matrix[3][0];
557         double b1 = matrix[2][0] * matrix[3][2] - matrix[2][2] * matrix[3][0];
558         double b2 = matrix[2][0] * matrix[3][3] - matrix[2][3] * matrix[3][0];
559         double b3 = matrix[2][1] * matrix[3][2] - matrix[2][2] * matrix[3][1];
560         double b4 = matrix[2][1] * matrix[3][3] - matrix[2][3] * matrix[3][1];
561         double b5 = matrix[2][2] * matrix[3][3] - matrix[2][3] * matrix[3][2];
562
563         double determinant = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
564
565         SysTryReturnResult(NID_BASE, Tizen::Base::Double::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
566                 "The current instance is not invertible.");
567
568         double inverseMatrix[4][4];
569
570         inverseMatrix[0][0] = (matrix[1][1] * b5 - matrix[1][2] * b4 + matrix[1][3] * b3) / determinant;
571         inverseMatrix[1][0] = (-matrix[1][0] * b5 + matrix[1][2] * b2 - matrix[1][3] * b1) / determinant;
572         inverseMatrix[2][0] = (matrix[1][0] * b4 - matrix[1][1] * b2 + matrix[1][3] * b0) / determinant;
573         inverseMatrix[3][0] = (-matrix[1][0] * b3 + matrix[1][1] * b1 - matrix[1][2] * b0) / determinant;
574
575         inverseMatrix[0][1] = (-matrix[0][1] * b5 + matrix[0][2] * b4 - matrix[0][3] * b3) / determinant;
576         inverseMatrix[1][1] = (matrix[0][0] * b5 - matrix[0][2] * b2 + matrix[0][3] * b1) / determinant;
577         inverseMatrix[2][1] = (-matrix[0][0] * b4 + matrix[0][1] * b2 - matrix[0][3] * b0) / determinant;
578         inverseMatrix[3][1] = (matrix[0][0] * b3 - matrix[0][1] * b1 + matrix[0][2] * b0) / determinant;
579
580         inverseMatrix[0][2] = (matrix[3][1] * a5 - matrix[3][2] * a4 + matrix[3][3] * a3) / determinant;
581         inverseMatrix[1][2] = (-matrix[3][0] * a5 + matrix[3][2] * a2 - matrix[3][3] * a1) / determinant;
582         inverseMatrix[2][2] = (matrix[3][0] * a4 - matrix[3][1] * a2 + matrix[3][3] * a0) / determinant;
583         inverseMatrix[3][2] = (-matrix[3][0] * a3 + matrix[3][1] * a1 - matrix[3][2] * a0) / determinant;
584
585         inverseMatrix[0][3] = (-matrix[2][1] * a5 + matrix[2][2] * a4 - matrix[2][3] * a3) / determinant;
586         inverseMatrix[1][3] = (matrix[2][0] * a5 - matrix[2][2] * a2 + matrix[2][3] * a1) / determinant;
587         inverseMatrix[2][3] = (-matrix[2][0] * a4 + matrix[2][1] * a2 - matrix[2][3] * a0) / determinant;
588         inverseMatrix[3][3] = (matrix[2][0] * a3 - matrix[2][1] * a1 + matrix[2][2] * a0) / determinant;
589
590         memcpy(matrix, inverseMatrix, MATRIX_SIZE);
591
592         return E_SUCCESS;
593 }
594
595 void
596 DoubleMatrix4::Transpose(void)
597 {
598         double tmp = 0.0f;
599         SWAP_VALUES(matrix[0][1], matrix[1][0], tmp);
600         SWAP_VALUES(matrix[0][2], matrix[2][0], tmp);
601         SWAP_VALUES(matrix[0][3], matrix[3][0], tmp);
602
603         SWAP_VALUES(matrix[1][2], matrix[2][1], tmp);
604         SWAP_VALUES(matrix[1][3], matrix[3][1], tmp);
605
606         SWAP_VALUES(matrix[2][3], matrix[3][2], tmp);
607 }
608
609 void
610 DoubleMatrix4::SetAsNull(void)
611 {
612         matrix[0][0] = 0.0f;
613         matrix[0][1] = 0.0f;
614         matrix[0][2] = 0.0f;
615         matrix[0][3] = 0.0f;
616
617         matrix[1][0] = 0.0f;
618         matrix[1][1] = 0.0f;
619         matrix[1][2] = 0.0f;
620         matrix[1][3] = 0.0f;
621
622         matrix[2][0] = 0.0f;
623         matrix[2][1] = 0.0f;
624         matrix[2][2] = 0.0f;
625         matrix[2][3] = 0.0f;
626
627         matrix[3][0] = 0.0f;
628         matrix[3][1] = 0.0f;
629         matrix[3][2] = 0.0f;
630         matrix[3][3] = 0.0f;
631 }
632
633 }} // Tizen::Base