2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FBaseFloatMatrix3.cpp
19 * @brief This is the implementation for FloatMatrix class.
21 #include <FBaseFloatMatrix3.h>
22 #include <FBaseSysLog.h>
24 namespace Tizen { namespace Base
27 static const int MATRIX_SIZE = sizeof(float) * 9;
28 #define SWAP_VALUES(a, b, tmp) \
33 FloatMatrix3::FloatMatrix3(void)
39 FloatMatrix3::FloatMatrix3(const FloatMatrix3& rhs)
42 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
45 FloatMatrix3::FloatMatrix3(const float matrix[3][3])
48 memcpy(this->matrix, matrix, MATRIX_SIZE);
51 FloatMatrix3::~FloatMatrix3(void)
56 FloatMatrix3::operator ==(const FloatMatrix3& rhs) const
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]);
75 FloatMatrix3::operator !=(const FloatMatrix3& rhs) const
77 return !(*this == rhs);
81 FloatMatrix3::operator =(const FloatMatrix3& rhs)
85 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
92 FloatMatrix3::operator =(float value)
100 matrix[2][1] = value;
102 matrix[0][2] = value;
103 matrix[1][2] = value;
104 matrix[2][2] = value;
110 FloatMatrix3::operator *(const FloatMatrix3& rhs) const // need to check performance
112 FloatMatrix3 resultMatrix;
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];
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];
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];
130 FloatMatrix3::operator *(float value) const
132 FloatMatrix3 resultMatrix(matrix);
134 resultMatrix.matrix[0][0] *= value;
135 resultMatrix.matrix[0][1] *= value;
136 resultMatrix.matrix[0][2] *= value;
138 resultMatrix.matrix[1][0] *= value;
139 resultMatrix.matrix[1][1] *= value;
140 resultMatrix.matrix[1][2] *= value;
142 resultMatrix.matrix[2][0] *= value;
143 resultMatrix.matrix[2][1] *= value;
144 resultMatrix.matrix[2][2] *= value;
150 FloatMatrix3::operator +(const FloatMatrix3& rhs) const
152 FloatMatrix3 resultMatrix(matrix);
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];
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];
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];
170 FloatMatrix3::operator +(float value) const
172 FloatMatrix3 resultMatrix(matrix);
174 resultMatrix.matrix[0][0] += value;
175 resultMatrix.matrix[0][1] += value;
176 resultMatrix.matrix[0][2] += value;
178 resultMatrix.matrix[1][0] += value;
179 resultMatrix.matrix[1][1] += value;
180 resultMatrix.matrix[1][2] += value;
182 resultMatrix.matrix[2][0] += value;
183 resultMatrix.matrix[2][1] += value;
184 resultMatrix.matrix[2][2] += value;
190 FloatMatrix3::operator -(const FloatMatrix3& rhs) const
192 FloatMatrix3 resultMatrix(matrix);
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];
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];
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];
210 FloatMatrix3::operator -(float value) const
212 FloatMatrix3 resultMatrix(matrix);
214 resultMatrix.matrix[0][0] -= value;
215 resultMatrix.matrix[0][1] -= value;
216 resultMatrix.matrix[0][2] -= value;
218 resultMatrix.matrix[1][0] -= value;
219 resultMatrix.matrix[1][1] -= value;
220 resultMatrix.matrix[1][2] -= value;
222 resultMatrix.matrix[2][0] -= value;
223 resultMatrix.matrix[2][1] -= value;
224 resultMatrix.matrix[2][2] -= value;
230 FloatMatrix3::operator *=(const FloatMatrix3& rhs)
237 FloatMatrix3::operator *=(float value)
239 *this = *this * value;
244 FloatMatrix3::operator +=(const FloatMatrix3& rhs)
251 FloatMatrix3::operator +=(float value)
253 *this = *this + value;
258 FloatMatrix3::operator -=(const FloatMatrix3& rhs)
265 FloatMatrix3::operator -=(float value)
267 *this = *this - value;
272 operator +(const float& value, const FloatMatrix3& rhs)
278 operator *(const float& value, const FloatMatrix3& rhs)
284 operator -(const float& value, const FloatMatrix3& rhs)
286 FloatMatrix3 returnMatrix(rhs);
287 returnMatrix.Negate();
288 returnMatrix += value;
293 FloatMatrix3::Equals(const Tizen::Base::Object& obj) const
295 const FloatMatrix3* pMatrix = dynamic_cast< const FloatMatrix3* >(&obj);
302 return (*this == *pMatrix);
306 FloatMatrix3::GetHashCode(void) const
308 return (Tizen::Base::Float::GetHashCode(this->matrix[0][0]) + Tizen::Base::Float::GetHashCode(this->matrix[1][0]) +
309 Tizen::Base::Float::GetHashCode(this->matrix[2][0]) + Tizen::Base::Float::GetHashCode(this->matrix[0][1]) +
310 Tizen::Base::Float::GetHashCode(this->matrix[1][1]) + Tizen::Base::Float::GetHashCode(this->matrix[2][1]) +
311 Tizen::Base::Float::GetHashCode(this->matrix[0][2]) + Tizen::Base::Float::GetHashCode(this->matrix[1][2]) +
312 Tizen::Base::Float::GetHashCode(this->matrix[2][2]));
316 FloatMatrix3::GetDeterminant(void) const
318 float a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
319 float a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
320 float a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
322 return matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
326 FloatMatrix3::GetInverse(void) const
328 float a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
329 float a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
330 float a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
332 float determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
334 if (Tizen::Base::Float::Compare(determinant, 0.0f) == 0)
339 float inverseMatrix[3][3];
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;
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;
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;
353 return FloatMatrix3(inverseMatrix);
357 FloatMatrix3::GetTrace(void) const
359 return matrix[0][0] + matrix[1][1] + matrix[2][2];
363 FloatMatrix3::GetTranspose(void) const
365 FloatMatrix3 transposeMatrix(matrix);
366 transposeMatrix.Transpose();
368 return transposeMatrix;
372 FloatMatrix3::IsIdentity(void) const
374 if ((Tizen::Base::Float::Compare(matrix[0][0], 1.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][1], 1.0f) != 0) ||
375 (Tizen::Base::Float::Compare(matrix[2][2], 1.0f) != 0))
380 if ((Tizen::Base::Float::Compare(matrix[0][1], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[0][2], 0.0f) != 0) ||
381 (Tizen::Base::Float::Compare(matrix[1][0], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[1][2], 0.0f) != 0) ||
382 (Tizen::Base::Float::Compare(matrix[2][0], 0.0f) != 0) || (Tizen::Base::Float::Compare(matrix[2][1], 0.0f) != 0))
391 FloatMatrix3::IsInvertible(void) const
393 if (Tizen::Base::Float::Compare(GetDeterminant(), 0.0f) == 0)
402 FloatMatrix3::Negate(void)
404 matrix[0][0] = -matrix[0][0];
405 matrix[0][1] = -matrix[0][1];
406 matrix[0][2] = -matrix[0][2];
408 matrix[1][0] = -matrix[1][0];
409 matrix[1][1] = -matrix[1][1];
410 matrix[1][2] = -matrix[1][2];
412 matrix[2][0] = -matrix[2][0];
413 matrix[2][1] = -matrix[2][1];
414 matrix[2][2] = -matrix[2][2];
418 FloatMatrix3::SetAsIdentity(void)
434 FloatMatrix3::Invert(void)
436 float a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
437 float a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
438 float a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
440 float determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
442 SysTryReturnResult(NID_BASE, Tizen::Base::Float::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
443 "The current instance is not invertible.");
445 float inverseMatrix[3][3];
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;
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;
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;
459 memcpy(matrix, inverseMatrix, MATRIX_SIZE);
465 FloatMatrix3::Transpose(void)
468 SWAP_VALUES(matrix[0][1], matrix[1][0], tmp);
469 SWAP_VALUES(matrix[0][2], matrix[2][0], tmp);
471 SWAP_VALUES(matrix[1][2], matrix[2][1], tmp);
475 FloatMatrix3::SetAsNull(void)