2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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 FBaseFloatMatrix3.cpp
20 * @brief This is the implementation for FloatMatrix class.
23 #include <FBaseFloatMatrix3.h>
24 #include <FBaseSysLog.h>
26 namespace Tizen { namespace Base
29 static const int MATRIX_SIZE = sizeof(float) * 9;
30 #define SWAP_VALUES(a, b, tmp) \
35 FloatMatrix3::FloatMatrix3(void)
41 FloatMatrix3::FloatMatrix3(const FloatMatrix3& rhs)
44 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
47 FloatMatrix3::FloatMatrix3(const float matrix[3][3])
50 memcpy(this->matrix, matrix, MATRIX_SIZE);
53 FloatMatrix3::~FloatMatrix3(void)
58 FloatMatrix3::operator ==(const FloatMatrix3& 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[1][0] == rhs.matrix[1][0] &&
69 matrix[1][1] == rhs.matrix[1][1] &&
70 matrix[1][2] == rhs.matrix[1][2] &&
71 matrix[2][0] == rhs.matrix[2][0] &&
72 matrix[2][1] == rhs.matrix[2][1] &&
73 matrix[2][2] == rhs.matrix[2][2]);
77 FloatMatrix3::operator !=(const FloatMatrix3& rhs) const
79 return !(*this == rhs);
83 FloatMatrix3::operator =(const FloatMatrix3& rhs)
87 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
94 FloatMatrix3::operator =(float value)
100 matrix[0][1] = value;
101 matrix[1][1] = value;
102 matrix[2][1] = value;
104 matrix[0][2] = value;
105 matrix[1][2] = value;
106 matrix[2][2] = value;
112 FloatMatrix3::operator *(const FloatMatrix3& rhs) const // need to check performance
114 FloatMatrix3 resultMatrix;
116 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];
117 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];
118 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];
120 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];
121 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];
122 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];
124 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];
125 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];
126 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];
132 FloatMatrix3::operator *(float value) const
134 FloatMatrix3 resultMatrix(matrix);
136 resultMatrix.matrix[0][0] *= value;
137 resultMatrix.matrix[0][1] *= value;
138 resultMatrix.matrix[0][2] *= value;
140 resultMatrix.matrix[1][0] *= value;
141 resultMatrix.matrix[1][1] *= value;
142 resultMatrix.matrix[1][2] *= value;
144 resultMatrix.matrix[2][0] *= value;
145 resultMatrix.matrix[2][1] *= value;
146 resultMatrix.matrix[2][2] *= value;
152 FloatMatrix3::operator +(const FloatMatrix3& rhs) const
154 FloatMatrix3 resultMatrix(matrix);
156 resultMatrix.matrix[0][0] += rhs.matrix[0][0];
157 resultMatrix.matrix[0][1] += rhs.matrix[0][1];
158 resultMatrix.matrix[0][2] += rhs.matrix[0][2];
160 resultMatrix.matrix[1][0] += rhs.matrix[1][0];
161 resultMatrix.matrix[1][1] += rhs.matrix[1][1];
162 resultMatrix.matrix[1][2] += rhs.matrix[1][2];
164 resultMatrix.matrix[2][0] += rhs.matrix[2][0];
165 resultMatrix.matrix[2][1] += rhs.matrix[2][1];
166 resultMatrix.matrix[2][2] += rhs.matrix[2][2];
172 FloatMatrix3::operator +(float value) const
174 FloatMatrix3 resultMatrix(matrix);
176 resultMatrix.matrix[0][0] += value;
177 resultMatrix.matrix[0][1] += value;
178 resultMatrix.matrix[0][2] += value;
180 resultMatrix.matrix[1][0] += value;
181 resultMatrix.matrix[1][1] += value;
182 resultMatrix.matrix[1][2] += value;
184 resultMatrix.matrix[2][0] += value;
185 resultMatrix.matrix[2][1] += value;
186 resultMatrix.matrix[2][2] += value;
192 FloatMatrix3::operator -(const FloatMatrix3& rhs) const
194 FloatMatrix3 resultMatrix(matrix);
196 resultMatrix.matrix[0][0] -= rhs.matrix[0][0];
197 resultMatrix.matrix[0][1] -= rhs.matrix[0][1];
198 resultMatrix.matrix[0][2] -= rhs.matrix[0][2];
200 resultMatrix.matrix[1][0] -= rhs.matrix[1][0];
201 resultMatrix.matrix[1][1] -= rhs.matrix[1][1];
202 resultMatrix.matrix[1][2] -= rhs.matrix[1][2];
204 resultMatrix.matrix[2][0] -= rhs.matrix[2][0];
205 resultMatrix.matrix[2][1] -= rhs.matrix[2][1];
206 resultMatrix.matrix[2][2] -= rhs.matrix[2][2];
212 FloatMatrix3::operator -(float value) const
214 FloatMatrix3 resultMatrix(matrix);
216 resultMatrix.matrix[0][0] -= value;
217 resultMatrix.matrix[0][1] -= value;
218 resultMatrix.matrix[0][2] -= value;
220 resultMatrix.matrix[1][0] -= value;
221 resultMatrix.matrix[1][1] -= value;
222 resultMatrix.matrix[1][2] -= value;
224 resultMatrix.matrix[2][0] -= value;
225 resultMatrix.matrix[2][1] -= value;
226 resultMatrix.matrix[2][2] -= value;
232 FloatMatrix3::operator *=(const FloatMatrix3& rhs)
239 FloatMatrix3::operator *=(float value)
241 *this = *this * value;
246 FloatMatrix3::operator +=(const FloatMatrix3& rhs)
253 FloatMatrix3::operator +=(float value)
255 *this = *this + value;
260 FloatMatrix3::operator -=(const FloatMatrix3& rhs)
267 FloatMatrix3::operator -=(float value)
269 *this = *this - value;
274 operator +(const float& value, const FloatMatrix3& rhs)
280 operator *(const float& value, const FloatMatrix3& rhs)
286 operator -(const float& value, const FloatMatrix3& rhs)
288 FloatMatrix3 returnMatrix(rhs);
289 returnMatrix.Negate();
290 returnMatrix += value;
295 FloatMatrix3::Equals(const Tizen::Base::Object& obj) const
297 const FloatMatrix3* pMatrix = dynamic_cast <const FloatMatrix3*>(&obj);
304 return (*this == *pMatrix);
308 FloatMatrix3::GetHashCode(void) const
310 return (Tizen::Base::Float::GetHashCode(this->matrix[0][0]) + Tizen::Base::Float::GetHashCode(this->matrix[1][0]) +
311 Tizen::Base::Float::GetHashCode(this->matrix[2][0]) + Tizen::Base::Float::GetHashCode(this->matrix[0][1]) +
312 Tizen::Base::Float::GetHashCode(this->matrix[1][1]) + Tizen::Base::Float::GetHashCode(this->matrix[2][1]) +
313 Tizen::Base::Float::GetHashCode(this->matrix[0][2]) + Tizen::Base::Float::GetHashCode(this->matrix[1][2]) +
314 Tizen::Base::Float::GetHashCode(this->matrix[2][2]));
318 FloatMatrix3::GetDeterminant(void) const
320 float a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
321 float a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
322 float a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
324 return matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
328 FloatMatrix3::GetInverse(void) const
330 float a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
331 float a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
332 float a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
334 float determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
336 if (Tizen::Base::Float::Compare(determinant, 0.0f) == 0)
341 float inverseMatrix[3][3];
343 inverseMatrix[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / determinant;
344 inverseMatrix[1][0] = (-matrix[1][0] * matrix[2][2] + matrix[1][2] * matrix[2][0]) / determinant;
345 inverseMatrix[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / determinant;
347 inverseMatrix[0][1] = (-matrix[0][1] * matrix[2][2] + matrix[0][2] * matrix[2][1]) / determinant;
348 inverseMatrix[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / determinant;
349 inverseMatrix[2][1] = (-matrix[0][0] * matrix[2][1] + matrix[0][1] * matrix[2][0]) / determinant;
351 inverseMatrix[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / determinant;
352 inverseMatrix[1][2] = (-matrix[0][0] * matrix[1][2] + matrix[0][2] * matrix[1][0]) / determinant;
353 inverseMatrix[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / determinant;
355 return FloatMatrix3(inverseMatrix);
359 FloatMatrix3::GetTrace(void) const
361 return matrix[0][0] + matrix[1][1] + matrix[2][2];
365 FloatMatrix3::GetTranspose(void) const
367 FloatMatrix3 transposeMatrix(matrix);
368 transposeMatrix.Transpose();
370 return transposeMatrix;
374 FloatMatrix3::IsIdentity(void) const
376 if ((Tizen::Base::Float::Compare(matrix[0][0], 1.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][1], 1.0f) != 0) ||
377 (Tizen::Base::Float::Compare(matrix[2][2], 1.0f) != 0))
382 if ((Tizen::Base::Float::Compare(matrix[0][1], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[0][2], 0.0f) != 0) ||
383 (Tizen::Base::Float::Compare(matrix[1][0], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][2], 0.0f) != 0) ||
384 (Tizen::Base::Float::Compare(matrix[2][0], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[2][1], 0.0f) != 0))
393 FloatMatrix3::IsInvertible(void) const
395 if (Tizen::Base::Float::Compare(GetDeterminant(), 0.0f) == 0)
404 FloatMatrix3::Negate(void)
406 matrix[0][0] = -matrix[0][0];
407 matrix[0][1] = -matrix[0][1];
408 matrix[0][2] = -matrix[0][2];
410 matrix[1][0] = -matrix[1][0];
411 matrix[1][1] = -matrix[1][1];
412 matrix[1][2] = -matrix[1][2];
414 matrix[2][0] = -matrix[2][0];
415 matrix[2][1] = -matrix[2][1];
416 matrix[2][2] = -matrix[2][2];
420 FloatMatrix3::SetAsIdentity(void)
436 FloatMatrix3::Invert(void)
438 float a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
439 float a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
440 float a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
442 float determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
444 SysTryReturnResult(NID_BASE, Tizen::Base::Float::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
445 "The current instance is not invertible.");
447 float inverseMatrix[3][3];
449 inverseMatrix[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / determinant;
450 inverseMatrix[1][0] = (-matrix[1][0] * matrix[2][2] + matrix[1][2] * matrix[2][0]) / determinant;
451 inverseMatrix[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / determinant;
453 inverseMatrix[0][1] = (-matrix[0][1] * matrix[2][2] + matrix[0][2] * matrix[2][1]) / determinant;
454 inverseMatrix[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / determinant;
455 inverseMatrix[2][1] = (-matrix[0][0] * matrix[2][1] + matrix[0][1] * matrix[2][0]) / determinant;
457 inverseMatrix[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / determinant;
458 inverseMatrix[1][2] = (-matrix[0][0] * matrix[1][2] + matrix[0][2] * matrix[1][0]) / determinant;
459 inverseMatrix[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / determinant;
461 memcpy(matrix, inverseMatrix, MATRIX_SIZE);
467 FloatMatrix3::Transpose(void)
470 SWAP_VALUES(matrix[0][1], matrix[1][0], tmp);
471 SWAP_VALUES(matrix[0][2], matrix[2][0], tmp);
473 SWAP_VALUES(matrix[1][2], matrix[2][1], tmp);
477 FloatMatrix3::SetAsNull(void)