2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.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
9 // http://floralicense.org/license/
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.
19 * @file FGrpFloatMatrix4.cpp
20 * @brief This is the implementation for FloatMatrix class.
23 #include <FGrpFloatMatrix4.h>
24 #include <FGrpFloatVector4.h>
25 #include <FBaseFloat.h>
26 #include <FBaseSysLog.h>
29 namespace Tizen { namespace Graphics
32 static const int MATRIX_SIZE = sizeof(float) * 16;
33 #define SWAP_VALUES(a, b, tmp) tmp = a; a = b; b = tmp;
35 FloatMatrix4::FloatMatrix4(void)
41 FloatMatrix4::FloatMatrix4(const FloatMatrix4& rhs)
44 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
47 FloatMatrix4::FloatMatrix4(const float matrix[4][4])
50 memcpy(this->matrix, matrix, MATRIX_SIZE);
53 FloatMatrix4::~FloatMatrix4(void)
58 FloatMatrix4::operator ==(const FloatMatrix4& rhs) const
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]);
84 FloatMatrix4::operator !=(const FloatMatrix4& rhs) const
86 return !(*this == rhs);
90 FloatMatrix4::operator =(const FloatMatrix4& rhs)
94 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
101 FloatMatrix4::operator =(float value)
103 matrix[0][0] = value;
104 matrix[1][0] = value;
105 matrix[2][0] = value;
106 matrix[3][0] = value;
108 matrix[0][1] = value;
109 matrix[1][1] = value;
110 matrix[2][1] = value;
111 matrix[3][1] = value;
113 matrix[0][2] = value;
114 matrix[1][2] = value;
115 matrix[2][2] = value;
116 matrix[3][2] = value;
118 matrix[0][3] = value;
119 matrix[1][3] = value;
120 matrix[2][3] = value;
121 matrix[3][3] = value;
127 FloatMatrix4::operator *(const FloatMatrix4& rhs) const
129 FloatMatrix4 resultMatrix;
131 resultMatrix.matrix[0][0] = (this->matrix[0][0] * rhs.matrix[0][0]) +
132 (this->matrix[1][0] * rhs.matrix[0][1]) +
133 (this->matrix[2][0] * rhs.matrix[0][2]) +
134 (this->matrix[3][0] * rhs.matrix[0][3]);
135 resultMatrix.matrix[1][0] = (this->matrix[0][0] * rhs.matrix[1][0]) +
136 (this->matrix[1][0] * rhs.matrix[1][1]) +
137 (this->matrix[2][0] * rhs.matrix[1][2]) +
138 (this->matrix[3][0] * rhs.matrix[1][3]);
139 resultMatrix.matrix[2][0] = (this->matrix[0][0] * rhs.matrix[2][0]) +
140 (this->matrix[1][0] * rhs.matrix[2][1]) +
141 (this->matrix[2][0] * rhs.matrix[2][2]) +
142 (this->matrix[3][0] * rhs.matrix[2][3]);
143 resultMatrix.matrix[3][0] = (this->matrix[0][0] * rhs.matrix[3][0]) +
144 (this->matrix[1][0] * rhs.matrix[3][1]) +
145 (this->matrix[2][0] * rhs.matrix[3][2]) +
146 (this->matrix[3][0] * rhs.matrix[3][3]);
148 resultMatrix.matrix[0][1] = (this->matrix[0][1] * rhs.matrix[0][0]) +
149 (this->matrix[1][1] * rhs.matrix[0][1]) +
150 (this->matrix[2][1] * rhs.matrix[0][2]) +
151 (this->matrix[3][1] * rhs.matrix[0][3]);
152 resultMatrix.matrix[1][1] = (this->matrix[0][1] * rhs.matrix[1][0]) +
153 (this->matrix[1][1] * rhs.matrix[1][1]) +
154 (this->matrix[2][1] * rhs.matrix[1][2]) +
155 (this->matrix[3][1] * rhs.matrix[1][3]);
156 resultMatrix.matrix[2][1] = (this->matrix[0][1] * rhs.matrix[2][0]) +
157 (this->matrix[1][1] * rhs.matrix[2][1]) +
158 (this->matrix[2][1] * rhs.matrix[2][2]) +
159 (this->matrix[3][1] * rhs.matrix[2][3]);
160 resultMatrix.matrix[3][1] = (this->matrix[0][1] * rhs.matrix[3][0]) +
161 (this->matrix[1][1] * rhs.matrix[3][1]) +
162 (this->matrix[2][1] * rhs.matrix[3][2]) +
163 (this->matrix[3][1] * rhs.matrix[3][3]);
165 resultMatrix.matrix[0][2] = (this->matrix[0][2] * rhs.matrix[0][0]) +
166 (this->matrix[1][2] * rhs.matrix[0][1]) +
167 (this->matrix[2][2] * rhs.matrix[0][2]) +
168 (this->matrix[3][2] * rhs.matrix[0][3]);
169 resultMatrix.matrix[1][2] = (this->matrix[0][2] * rhs.matrix[1][0]) +
170 (this->matrix[1][2] * rhs.matrix[1][1]) +
171 (this->matrix[2][2] * rhs.matrix[1][2]) +
172 (this->matrix[3][2] * rhs.matrix[1][3]);
173 resultMatrix.matrix[2][2] = (this->matrix[0][2] * rhs.matrix[2][0]) +
174 (this->matrix[1][2] * rhs.matrix[2][1]) +
175 (this->matrix[2][2] * rhs.matrix[2][2]) +
176 (this->matrix[3][2] * rhs.matrix[2][3]);
177 resultMatrix.matrix[3][2] = (this->matrix[0][2] * rhs.matrix[3][0]) +
178 (this->matrix[1][2] * rhs.matrix[3][1]) +
179 (this->matrix[2][2] * rhs.matrix[3][2]) +
180 (this->matrix[3][2] * rhs.matrix[3][3]);
182 resultMatrix.matrix[0][3] = (this->matrix[0][3] * rhs.matrix[0][0]) +
183 (this->matrix[1][3] * rhs.matrix[0][1]) +
184 (this->matrix[2][3] * rhs.matrix[0][2]) +
185 (this->matrix[3][3] * rhs.matrix[0][3]);
186 resultMatrix.matrix[1][3] = (this->matrix[0][3] * rhs.matrix[1][0]) +
187 (this->matrix[1][3] * rhs.matrix[1][1]) +
188 (this->matrix[2][3] * rhs.matrix[1][2]) +
189 (this->matrix[3][3] * rhs.matrix[1][3]);
190 resultMatrix.matrix[2][3] = (this->matrix[0][3] * rhs.matrix[2][0]) +
191 (this->matrix[1][3] * rhs.matrix[2][1]) +
192 (this->matrix[2][3] * rhs.matrix[2][2]) +
193 (this->matrix[3][3] * rhs.matrix[2][3]);
194 resultMatrix.matrix[3][3] = (this->matrix[0][3] * rhs.matrix[3][0]) +
195 (this->matrix[1][3] * rhs.matrix[3][1]) +
196 (this->matrix[2][3] * rhs.matrix[3][2]) +
197 (this->matrix[3][3] * rhs.matrix[3][3]);
203 FloatMatrix4::operator *(const FloatVector4& vector) const
205 FloatVector4 resultVector;
207 resultVector.x = (matrix[0][0] * vector.x) + (matrix[1][0] * vector.y) +
208 (matrix[2][0] * vector.z) + (matrix[3][0] * vector.w);
209 resultVector.y = (matrix[0][1] * vector.x) + (matrix[1][1] * vector.y) +
210 (matrix[2][1] * vector.z) + (matrix[3][1] * vector.w);
211 resultVector.z = (matrix[0][2] * vector.x) + (matrix[1][2] * vector.y) +
212 (matrix[2][2] * vector.z) + (matrix[3][2] * vector.w);
213 resultVector.w = (matrix[0][3] * vector.x) + (matrix[1][3] * vector.y) +
214 (matrix[2][3] * vector.z) + (matrix[3][3] * vector.w);
220 FloatMatrix4::operator *(float value) const
222 FloatMatrix4 resultMatrix(matrix);
224 resultMatrix.matrix[0][0] *= value;
225 resultMatrix.matrix[0][1] *= value;
226 resultMatrix.matrix[0][2] *= value;
227 resultMatrix.matrix[0][3] *= value;
229 resultMatrix.matrix[1][0] *= value;
230 resultMatrix.matrix[1][1] *= value;
231 resultMatrix.matrix[1][2] *= value;
232 resultMatrix.matrix[1][3] *= value;
234 resultMatrix.matrix[2][0] *= value;
235 resultMatrix.matrix[2][1] *= value;
236 resultMatrix.matrix[2][2] *= value;
237 resultMatrix.matrix[2][3] *= value;
239 resultMatrix.matrix[3][0] *= value;
240 resultMatrix.matrix[3][1] *= value;
241 resultMatrix.matrix[3][2] *= value;
242 resultMatrix.matrix[3][3] *= value;
248 FloatMatrix4::operator +(const FloatMatrix4& rhs) const
250 FloatMatrix4 resultMatrix(matrix);
252 resultMatrix.matrix[0][0] += rhs.matrix[0][0];
253 resultMatrix.matrix[0][1] += rhs.matrix[0][1];
254 resultMatrix.matrix[0][2] += rhs.matrix[0][2];
255 resultMatrix.matrix[0][3] += rhs.matrix[0][3];
257 resultMatrix.matrix[1][0] += rhs.matrix[1][0];
258 resultMatrix.matrix[1][1] += rhs.matrix[1][1];
259 resultMatrix.matrix[1][2] += rhs.matrix[1][2];
260 resultMatrix.matrix[1][3] += rhs.matrix[1][3];
262 resultMatrix.matrix[2][0] += rhs.matrix[2][0];
263 resultMatrix.matrix[2][1] += rhs.matrix[2][1];
264 resultMatrix.matrix[2][2] += rhs.matrix[2][2];
265 resultMatrix.matrix[2][3] += rhs.matrix[2][3];
267 resultMatrix.matrix[3][0] += rhs.matrix[3][0];
268 resultMatrix.matrix[3][1] += rhs.matrix[3][1];
269 resultMatrix.matrix[3][2] += rhs.matrix[3][2];
270 resultMatrix.matrix[3][3] += rhs.matrix[3][3];
276 FloatMatrix4::operator +(float value) const
278 FloatMatrix4 resultMatrix(matrix);
280 resultMatrix.matrix[0][0] += value;
281 resultMatrix.matrix[0][1] += value;
282 resultMatrix.matrix[0][2] += value;
283 resultMatrix.matrix[0][3] += value;
285 resultMatrix.matrix[1][0] += value;
286 resultMatrix.matrix[1][1] += value;
287 resultMatrix.matrix[1][2] += value;
288 resultMatrix.matrix[1][3] += value;
290 resultMatrix.matrix[2][0] += value;
291 resultMatrix.matrix[2][1] += value;
292 resultMatrix.matrix[2][2] += value;
293 resultMatrix.matrix[2][3] += value;
295 resultMatrix.matrix[3][0] += value;
296 resultMatrix.matrix[3][1] += value;
297 resultMatrix.matrix[3][2] += value;
298 resultMatrix.matrix[3][3] += value;
304 FloatMatrix4::operator -(const FloatMatrix4& rhs) const
306 FloatMatrix4 resultMatrix(matrix);
308 resultMatrix.matrix[0][0] -= rhs.matrix[0][0];
309 resultMatrix.matrix[0][1] -= rhs.matrix[0][1];
310 resultMatrix.matrix[0][2] -= rhs.matrix[0][2];
311 resultMatrix.matrix[0][3] -= rhs.matrix[0][3];
313 resultMatrix.matrix[1][0] -= rhs.matrix[1][0];
314 resultMatrix.matrix[1][1] -= rhs.matrix[1][1];
315 resultMatrix.matrix[1][2] -= rhs.matrix[1][2];
316 resultMatrix.matrix[1][3] -= rhs.matrix[1][3];
318 resultMatrix.matrix[2][0] -= rhs.matrix[2][0];
319 resultMatrix.matrix[2][1] -= rhs.matrix[2][1];
320 resultMatrix.matrix[2][2] -= rhs.matrix[2][2];
321 resultMatrix.matrix[2][3] -= rhs.matrix[2][3];
323 resultMatrix.matrix[3][0] -= rhs.matrix[3][0];
324 resultMatrix.matrix[3][1] -= rhs.matrix[3][1];
325 resultMatrix.matrix[3][2] -= rhs.matrix[3][2];
326 resultMatrix.matrix[3][3] -= rhs.matrix[3][3];
332 FloatMatrix4::operator -(float value) const
334 FloatMatrix4 resultMatrix(matrix);
336 resultMatrix.matrix[0][0] -= value;
337 resultMatrix.matrix[0][1] -= value;
338 resultMatrix.matrix[0][2] -= value;
339 resultMatrix.matrix[0][3] -= value;
341 resultMatrix.matrix[1][0] -= value;
342 resultMatrix.matrix[1][1] -= value;
343 resultMatrix.matrix[1][2] -= value;
344 resultMatrix.matrix[1][3] -= value;
346 resultMatrix.matrix[2][0] -= value;
347 resultMatrix.matrix[2][1] -= value;
348 resultMatrix.matrix[2][2] -= value;
349 resultMatrix.matrix[2][3] -= value;
351 resultMatrix.matrix[3][0] -= value;
352 resultMatrix.matrix[3][1] -= value;
353 resultMatrix.matrix[3][2] -= value;
354 resultMatrix.matrix[3][3] -= value;
360 FloatMatrix4::operator *=(const FloatMatrix4& rhs)
367 FloatMatrix4::operator *=(float value)
369 *this = *this * value;
374 FloatMatrix4::operator +=(const FloatMatrix4& rhs)
381 FloatMatrix4::operator +=(float value)
383 *this = *this + value;
388 FloatMatrix4::operator -=(const FloatMatrix4& rhs)
395 FloatMatrix4::operator -=(float value)
397 *this = *this - value;
402 operator +(const float& value, const FloatMatrix4& rhs)
408 operator *(const float& value, const FloatMatrix4& rhs)
414 operator -(const float& value, const FloatMatrix4& rhs)
416 FloatMatrix4 returnMatrix(rhs);
417 returnMatrix.Negate();
418 returnMatrix += value;
423 FloatMatrix4::Equals(const Tizen::Base::Object& obj) const
425 const FloatMatrix4* pMatrix = dynamic_cast <const FloatMatrix4*>(&obj);
432 return (*this == *pMatrix);
436 FloatMatrix4::GetHashCode(void) const
438 return (Tizen::Base::Float::GetHashCode(matrix[0][0]) + Tizen::Base::Float::GetHashCode(matrix[1][0]) +
439 Tizen::Base::Float::GetHashCode(matrix[2][0]) + Tizen::Base::Float::GetHashCode(matrix[3][0]) +
440 Tizen::Base::Float::GetHashCode(matrix[0][1]) + Tizen::Base::Float::GetHashCode(matrix[1][1]) +
441 Tizen::Base::Float::GetHashCode(matrix[2][1]) + Tizen::Base::Float::GetHashCode(matrix[3][1]) +
442 Tizen::Base::Float::GetHashCode(matrix[0][2]) + Tizen::Base::Float::GetHashCode(matrix[1][2]) +
443 Tizen::Base::Float::GetHashCode(matrix[2][2]) + Tizen::Base::Float::GetHashCode(matrix[3][2]) +
444 Tizen::Base::Float::GetHashCode(matrix[0][3]) + Tizen::Base::Float::GetHashCode(matrix[1][3]) +
445 Tizen::Base::Float::GetHashCode(matrix[2][3]) + Tizen::Base::Float::GetHashCode(matrix[3][3]));
449 FloatMatrix4::GetDeterminant(void) const
451 float a0 = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
452 float a1 = matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0];
453 float a2 = matrix[0][0] * matrix[1][3] - matrix[0][3] * matrix[1][0];
454 float a3 = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];
455 float a4 = matrix[0][1] * matrix[1][3] - matrix[0][3] * matrix[1][1];
456 float a5 = matrix[0][2] * matrix[1][3] - matrix[0][3] * matrix[1][2];
458 float b0 = matrix[2][0] * matrix[3][1] - matrix[2][1] * matrix[3][0];
459 float b1 = matrix[2][0] * matrix[3][2] - matrix[2][2] * matrix[3][0];
460 float b2 = matrix[2][0] * matrix[3][3] - matrix[2][3] * matrix[3][0];
461 float b3 = matrix[2][1] * matrix[3][2] - matrix[2][2] * matrix[3][1];
462 float b4 = matrix[2][1] * matrix[3][3] - matrix[2][3] * matrix[3][1];
463 float b5 = matrix[2][2] * matrix[3][3] - matrix[2][3] * matrix[3][2];
465 return a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
469 FloatMatrix4::GetInverse(void) const
471 float a0 = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
472 float a1 = matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0];
473 float a2 = matrix[0][0] * matrix[1][3] - matrix[0][3] * matrix[1][0];
474 float a3 = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];
475 float a4 = matrix[0][1] * matrix[1][3] - matrix[0][3] * matrix[1][1];
476 float a5 = matrix[0][2] * matrix[1][3] - matrix[0][3] * matrix[1][2];
478 float b0 = matrix[2][0] * matrix[3][1] - matrix[2][1] * matrix[3][0];
479 float b1 = matrix[2][0] * matrix[3][2] - matrix[2][2] * matrix[3][0];
480 float b2 = matrix[2][0] * matrix[3][3] - matrix[2][3] * matrix[3][0];
481 float b3 = matrix[2][1] * matrix[3][2] - matrix[2][2] * matrix[3][1];
482 float b4 = matrix[2][1] * matrix[3][3] - matrix[2][3] * matrix[3][1];
483 float b5 = matrix[2][2] * matrix[3][3] - matrix[2][3] * matrix[3][2];
485 float determinant = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
487 if (Tizen::Base::Float::Compare(determinant, 0.0f) == 0)
492 float inverseMatrix[4][4];
494 inverseMatrix[0][0] = (matrix[1][1] * b5 - matrix[1][2] * b4 + matrix[1][3] * b3) / determinant;
495 inverseMatrix[1][0] = (-matrix[1][0] * b5 + matrix[1][2] * b2 - matrix[1][3] * b1) / determinant;
496 inverseMatrix[2][0] = (matrix[1][0] * b4 - matrix[1][1] * b2 + matrix[1][3] * b0) / determinant;
497 inverseMatrix[3][0] = (-matrix[1][0] * b3 + matrix[1][1] * b1 - matrix[1][2] * b0) / determinant;
499 inverseMatrix[0][1] = (-matrix[0][1] * b5 + matrix[0][2] * b4 - matrix[0][3] * b3) / determinant;
500 inverseMatrix[1][1] = (matrix[0][0] * b5 - matrix[0][2] * b2 + matrix[0][3] * b1) / determinant;
501 inverseMatrix[2][1] = (-matrix[0][0] * b4 + matrix[0][1] * b2 - matrix[0][3] * b0) / determinant;
502 inverseMatrix[3][1] = (matrix[0][0] * b3 - matrix[0][1] * b1 + matrix[0][2] * b0) / determinant;
504 inverseMatrix[0][2] = (matrix[3][1] * a5 - matrix[3][2] * a4 + matrix[3][3] * a3) / determinant;
505 inverseMatrix[1][2] = (-matrix[3][0] * a5 + matrix[3][2] * a2 - matrix[3][3] * a1) / determinant;
506 inverseMatrix[2][2] = (matrix[3][0] * a4 - matrix[3][1] * a2 + matrix[3][3] * a0) / determinant;
507 inverseMatrix[3][2] = (-matrix[3][0] * a3 + matrix[3][1] * a1 - matrix[3][2] * a0) / determinant;
509 inverseMatrix[0][3] = (-matrix[2][1] * a5 + matrix[2][2] * a4 - matrix[2][3] * a3) / determinant;
510 inverseMatrix[1][3] = (matrix[2][0] * a5 - matrix[2][2] * a2 + matrix[2][3] * a1) / determinant;
511 inverseMatrix[2][3] = (-matrix[2][0] * a4 + matrix[2][1] * a2 - matrix[2][3] * a0) / determinant;
512 inverseMatrix[3][3] = (matrix[2][0] * a3 - matrix[2][1] * a1 + matrix[2][2] * a0) / determinant;
514 return FloatMatrix4(inverseMatrix);
518 FloatMatrix4::GetTrace(void) const
520 return matrix[0][0] + matrix[1][1] + matrix[2][2] + matrix[3][3];
524 FloatMatrix4::GetTranspose(void) const
526 FloatMatrix4 transposeMatrix(matrix);
527 transposeMatrix.Transpose();
529 return transposeMatrix;
533 FloatMatrix4::IsIdentity(void) const
535 if ((Tizen::Base::Float::Compare(matrix[0][0], 1.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][1], 1.0f) != 0) ||
536 (Tizen::Base::Float::Compare(matrix[2][2], 1.0f) != 0) || (Tizen::Base::Float::Compare(matrix[3][3], 1.0f) != 0))
541 if ((Tizen::Base::Float::Compare(matrix[0][1], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[0][2], 0.0f) != 0) ||
542 (Tizen::Base::Float::Compare(matrix[0][3], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][0], 0.0f) != 0) ||
543 (Tizen::Base::Float::Compare(matrix[1][2], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][3], 0.0f) != 0) ||
544 (Tizen::Base::Float::Compare(matrix[2][0], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[2][1], 0.0f) != 0) ||
545 (Tizen::Base::Float::Compare(matrix[2][3], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[3][0], 0.0f) != 0) ||
546 (Tizen::Base::Float::Compare(matrix[3][1], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[3][2], 0.0f) != 0))
555 FloatMatrix4::IsInvertible(void) const
557 if (Tizen::Base::Float::Compare(GetDeterminant(), 0.0f) == 0)
566 FloatMatrix4::Negate(void)
568 matrix[0][0] = -matrix[0][0];
569 matrix[0][1] = -matrix[0][1];
570 matrix[0][2] = -matrix[0][2];
571 matrix[0][3] = -matrix[0][3];
573 matrix[1][0] = -matrix[1][0];
574 matrix[1][1] = -matrix[1][1];
575 matrix[1][2] = -matrix[1][2];
576 matrix[1][3] = -matrix[1][3];
578 matrix[2][0] = -matrix[2][0];
579 matrix[2][1] = -matrix[2][1];
580 matrix[2][2] = -matrix[2][2];
581 matrix[2][3] = -matrix[2][3];
583 matrix[3][0] = -matrix[3][0];
584 matrix[3][1] = -matrix[3][1];
585 matrix[3][2] = -matrix[3][2];
586 matrix[3][3] = -matrix[3][3];
590 FloatMatrix4::SetAsIdentity(void)
614 FloatMatrix4::Invert(void)
616 float a0 = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
617 float a1 = matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0];
618 float a2 = matrix[0][0] * matrix[1][3] - matrix[0][3] * matrix[1][0];
619 float a3 = matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1];
620 float a4 = matrix[0][1] * matrix[1][3] - matrix[0][3] * matrix[1][1];
621 float a5 = matrix[0][2] * matrix[1][3] - matrix[0][3] * matrix[1][2];
623 float b0 = matrix[2][0] * matrix[3][1] - matrix[2][1] * matrix[3][0];
624 float b1 = matrix[2][0] * matrix[3][2] - matrix[2][2] * matrix[3][0];
625 float b2 = matrix[2][0] * matrix[3][3] - matrix[2][3] * matrix[3][0];
626 float b3 = matrix[2][1] * matrix[3][2] - matrix[2][2] * matrix[3][1];
627 float b4 = matrix[2][1] * matrix[3][3] - matrix[2][3] * matrix[3][1];
628 float b5 = matrix[2][2] * matrix[3][3] - matrix[2][3] * matrix[3][2];
630 float determinant = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
632 SysTryReturnResult(NID_GRP, Tizen::Base::Float::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION, "The current instance is not invertible.");
634 float inverseMatrix[4][4];
636 inverseMatrix[0][0] = (matrix[1][1] * b5 - matrix[1][2] * b4 + matrix[1][3] * b3) / determinant;
637 inverseMatrix[1][0] = (-matrix[1][0] * b5 + matrix[1][2] * b2 - matrix[1][3] * b1) / determinant;
638 inverseMatrix[2][0] = (matrix[1][0] * b4 - matrix[1][1] * b2 + matrix[1][3] * b0) / determinant;
639 inverseMatrix[3][0] = (-matrix[1][0] * b3 + matrix[1][1] * b1 - matrix[1][2] * b0) / determinant;
641 inverseMatrix[0][1] = (-matrix[0][1] * b5 + matrix[0][2] * b4 - matrix[0][3] * b3) / determinant;
642 inverseMatrix[1][1] = (matrix[0][0] * b5 - matrix[0][2] * b2 + matrix[0][3] * b1) / determinant;
643 inverseMatrix[2][1] = (-matrix[0][0] * b4 + matrix[0][1] * b2 - matrix[0][3] * b0) / determinant;
644 inverseMatrix[3][1] = (matrix[0][0] * b3 - matrix[0][1] * b1 + matrix[0][2] * b0) / determinant;
646 inverseMatrix[0][2] = (matrix[3][1] * a5 - matrix[3][2] * a4 + matrix[3][3] * a3) / determinant;
647 inverseMatrix[1][2] = (-matrix[3][0] * a5 + matrix[3][2] * a2 - matrix[3][3] * a1) / determinant;
648 inverseMatrix[2][2] = (matrix[3][0] * a4 - matrix[3][1] * a2 + matrix[3][3] * a0) / determinant;
649 inverseMatrix[3][2] = (-matrix[3][0] * a3 + matrix[3][1] * a1 - matrix[3][2] * a0) / determinant;
651 inverseMatrix[0][3] = (-matrix[2][1] * a5 + matrix[2][2] * a4 - matrix[2][3] * a3) / determinant;
652 inverseMatrix[1][3] = (matrix[2][0] * a5 - matrix[2][2] * a2 + matrix[2][3] * a1) / determinant;
653 inverseMatrix[2][3] = (-matrix[2][0] * a4 + matrix[2][1] * a2 - matrix[2][3] * a0) / determinant;
654 inverseMatrix[3][3] = (matrix[2][0] * a3 - matrix[2][1] * a1 + matrix[2][2] * a0) / determinant;
656 memcpy(matrix, inverseMatrix, MATRIX_SIZE);
662 FloatMatrix4::Transpose(void)
665 SWAP_VALUES(matrix[0][1], matrix[1][0], tmp);
666 SWAP_VALUES(matrix[0][2], matrix[2][0], tmp);
667 SWAP_VALUES(matrix[0][3], matrix[3][0], tmp);
669 SWAP_VALUES(matrix[1][2], matrix[2][1], tmp);
670 SWAP_VALUES(matrix[1][3], matrix[3][1], tmp);
672 SWAP_VALUES(matrix[2][3], matrix[3][2], tmp);
676 FloatMatrix4::SetAsNull(void)
699 }} // Tizen::Graphics