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