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 FBaseDoubleMatrix3.cpp
19 * @brief This is the implementation for DoubleMatrix class.
22 #include <FBaseDoubleMatrix3.h>
23 #include <FBaseSysLog.h>
25 namespace Tizen { namespace Base
28 static const int MATRIX_SIZE = sizeof(double) * 9;
29 #define SWAP_VALUES(a, b, tmp) \
34 DoubleMatrix3::DoubleMatrix3(void)
40 DoubleMatrix3::DoubleMatrix3(const DoubleMatrix3& rhs)
43 memcpy(matrix, rhs.matrix, MATRIX_SIZE);
46 DoubleMatrix3::DoubleMatrix3(const double matrix[3][3])
49 memcpy(this->matrix, matrix, MATRIX_SIZE);
52 DoubleMatrix3::~DoubleMatrix3(void)
57 DoubleMatrix3::operator ==(const DoubleMatrix3& rhs) const
64 return (matrix[0][0] == rhs.matrix[0][0] &&
65 matrix[0][1] == rhs.matrix[0][1] &&
66 matrix[0][2] == rhs.matrix[0][2] &&
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[2][0] == rhs.matrix[2][0] &&
71 matrix[2][1] == rhs.matrix[2][1] &&
72 matrix[2][2] == rhs.matrix[2][2]);
76 DoubleMatrix3::operator !=(const DoubleMatrix3& rhs) const
78 return !(*this == rhs);
82 DoubleMatrix3::operator =(const DoubleMatrix3& rhs)
86 memcpy(matrix, rhs.matrix, sizeof(double) * 9);
93 DoubleMatrix3::operator =(double value)
100 matrix[1][1] = value;
101 matrix[2][1] = value;
103 matrix[0][2] = value;
104 matrix[1][2] = value;
105 matrix[2][2] = value;
111 DoubleMatrix3::operator *(const DoubleMatrix3& rhs) const // need to check performance
113 DoubleMatrix3 resultMatrix;
115 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];
116 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];
117 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];
119 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];
120 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];
121 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];
123 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];
124 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];
125 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];
131 DoubleMatrix3::operator *(double value) const
133 DoubleMatrix3 resultMatrix(matrix);
135 resultMatrix.matrix[0][0] *= value;
136 resultMatrix.matrix[0][1] *= value;
137 resultMatrix.matrix[0][2] *= value;
139 resultMatrix.matrix[1][0] *= value;
140 resultMatrix.matrix[1][1] *= value;
141 resultMatrix.matrix[1][2] *= value;
143 resultMatrix.matrix[2][0] *= value;
144 resultMatrix.matrix[2][1] *= value;
145 resultMatrix.matrix[2][2] *= value;
151 DoubleMatrix3::operator +(const DoubleMatrix3& rhs) const
153 DoubleMatrix3 resultMatrix(matrix);
155 resultMatrix.matrix[0][0] += rhs.matrix[0][0];
156 resultMatrix.matrix[0][1] += rhs.matrix[0][1];
157 resultMatrix.matrix[0][2] += rhs.matrix[0][2];
159 resultMatrix.matrix[1][0] += rhs.matrix[1][0];
160 resultMatrix.matrix[1][1] += rhs.matrix[1][1];
161 resultMatrix.matrix[1][2] += rhs.matrix[1][2];
163 resultMatrix.matrix[2][0] += rhs.matrix[2][0];
164 resultMatrix.matrix[2][1] += rhs.matrix[2][1];
165 resultMatrix.matrix[2][2] += rhs.matrix[2][2];
171 DoubleMatrix3::operator +(double value) const
173 DoubleMatrix3 resultMatrix(matrix);
175 resultMatrix.matrix[0][0] += value;
176 resultMatrix.matrix[0][1] += value;
177 resultMatrix.matrix[0][2] += value;
179 resultMatrix.matrix[1][0] += value;
180 resultMatrix.matrix[1][1] += value;
181 resultMatrix.matrix[1][2] += value;
183 resultMatrix.matrix[2][0] += value;
184 resultMatrix.matrix[2][1] += value;
185 resultMatrix.matrix[2][2] += value;
191 DoubleMatrix3::operator -(const DoubleMatrix3& rhs) const
193 DoubleMatrix3 resultMatrix(matrix);
195 resultMatrix.matrix[0][0] -= rhs.matrix[0][0];
196 resultMatrix.matrix[0][1] -= rhs.matrix[0][1];
197 resultMatrix.matrix[0][2] -= rhs.matrix[0][2];
199 resultMatrix.matrix[1][0] -= rhs.matrix[1][0];
200 resultMatrix.matrix[1][1] -= rhs.matrix[1][1];
201 resultMatrix.matrix[1][2] -= rhs.matrix[1][2];
203 resultMatrix.matrix[2][0] -= rhs.matrix[2][0];
204 resultMatrix.matrix[2][1] -= rhs.matrix[2][1];
205 resultMatrix.matrix[2][2] -= rhs.matrix[2][2];
211 DoubleMatrix3::operator -(double value) const
213 DoubleMatrix3 resultMatrix(matrix);
215 resultMatrix.matrix[0][0] -= value;
216 resultMatrix.matrix[0][1] -= value;
217 resultMatrix.matrix[0][2] -= value;
219 resultMatrix.matrix[1][0] -= value;
220 resultMatrix.matrix[1][1] -= value;
221 resultMatrix.matrix[1][2] -= value;
223 resultMatrix.matrix[2][0] -= value;
224 resultMatrix.matrix[2][1] -= value;
225 resultMatrix.matrix[2][2] -= value;
231 DoubleMatrix3::operator *=(const DoubleMatrix3& rhs)
238 DoubleMatrix3::operator *=(double value)
240 *this = *this * value;
245 DoubleMatrix3::operator +=(const DoubleMatrix3& rhs)
252 DoubleMatrix3::operator +=(double value)
254 *this = *this + value;
259 DoubleMatrix3::operator -=(const DoubleMatrix3& rhs)
266 DoubleMatrix3::operator -=(double value)
268 *this = *this - value;
273 operator +(const double& value, const DoubleMatrix3& rhs)
279 operator *(const double& value, const DoubleMatrix3& rhs)
285 operator -(const double& value, const DoubleMatrix3& rhs)
287 DoubleMatrix3 returnMatrix(rhs);
288 returnMatrix.Negate();
289 returnMatrix += value;
294 DoubleMatrix3::Equals(const Tizen::Base::Object& obj) const
296 const DoubleMatrix3* pMatrix = dynamic_cast <const DoubleMatrix3*>(&obj);
303 return (*this == *pMatrix);
307 DoubleMatrix3::GetHashCode(void) const
309 return (Tizen::Base::Double::GetHashCode(this->matrix[0][0]) + Tizen::Base::Double::GetHashCode(this->matrix[1][0]) +
310 Tizen::Base::Double::GetHashCode(this->matrix[2][0]) + Tizen::Base::Double::GetHashCode(this->matrix[0][1]) +
311 Tizen::Base::Double::GetHashCode(this->matrix[1][1]) + Tizen::Base::Double::GetHashCode(this->matrix[2][1]) +
312 Tizen::Base::Double::GetHashCode(this->matrix[0][2]) + Tizen::Base::Double::GetHashCode(this->matrix[1][2]) +
313 Tizen::Base::Double::GetHashCode(this->matrix[2][2]));
317 DoubleMatrix3::GetDeterminant(void) const
319 double a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
320 double a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
321 double a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
323 return matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
327 DoubleMatrix3::GetInverse(void) const
329 double a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
330 double a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
331 double a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
333 double determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
335 if (Tizen::Base::Double::Compare(determinant, 0.0f) == 0)
340 double inverseMatrix[3][3];
342 inverseMatrix[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / determinant;
343 inverseMatrix[1][0] = (-matrix[1][0] * matrix[2][2] + matrix[1][2] * matrix[2][0]) / determinant;
344 inverseMatrix[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / determinant;
346 inverseMatrix[0][1] = (-matrix[0][1] * matrix[2][2] + matrix[0][2] * matrix[2][1]) / determinant;
347 inverseMatrix[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / determinant;
348 inverseMatrix[2][1] = (-matrix[0][0] * matrix[2][1] + matrix[0][1] * matrix[2][0]) / determinant;
350 inverseMatrix[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / determinant;
351 inverseMatrix[1][2] = (-matrix[0][0] * matrix[1][2] + matrix[0][2] * matrix[1][0]) / determinant;
352 inverseMatrix[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / determinant;
354 return DoubleMatrix3(inverseMatrix);
358 DoubleMatrix3::GetTrace(void) const
360 return matrix[0][0] + matrix[1][1] + matrix[2][2];
364 DoubleMatrix3::GetTranspose(void) const
366 DoubleMatrix3 transposeMatrix(matrix);
367 transposeMatrix.Transpose();
369 return transposeMatrix;
373 DoubleMatrix3::IsIdentity(void) const
375 if ((Tizen::Base::Double::Compare(matrix[0][0], 1.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][1], 1.0f) != 0) ||
376 (Tizen::Base::Double::Compare(matrix[2][2], 1.0f) != 0))
381 if ((Tizen::Base::Double::Compare(matrix[0][1], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[0][2], 0.0f) != 0) ||
382 (Tizen::Base::Double::Compare(matrix[1][0], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[1][2], 0.0f) != 0) ||
383 (Tizen::Base::Double::Compare(matrix[2][0], 0.0f) != 0) || (Tizen::Base::Double::Compare(matrix[2][1], 0.0f) != 0))
392 DoubleMatrix3::IsInvertible(void) const
394 if (Tizen::Base::Double::Compare(GetDeterminant(), 0.0f) == 0)
403 DoubleMatrix3::Negate(void)
405 matrix[0][0] = -matrix[0][0];
406 matrix[0][1] = -matrix[0][1];
407 matrix[0][2] = -matrix[0][2];
409 matrix[1][0] = -matrix[1][0];
410 matrix[1][1] = -matrix[1][1];
411 matrix[1][2] = -matrix[1][2];
413 matrix[2][0] = -matrix[2][0];
414 matrix[2][1] = -matrix[2][1];
415 matrix[2][2] = -matrix[2][2];
419 DoubleMatrix3::SetAsIdentity(void)
435 DoubleMatrix3::Invert(void)
437 double a0 = matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2];
438 double a1 = matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0];
439 double a2 = matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0];
441 double determinant = matrix[0][0] * a0 - matrix[0][1] * a1 + matrix[0][2] * a2;
443 SysTryReturnResult(NID_BASE, Tizen::Base::Double::Compare(determinant, 0.0f) != 0, E_INVALID_OPERATION,
444 "The current instance is not invertible.");
446 double inverseMatrix[3][3];
448 inverseMatrix[0][0] = (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) / determinant;
449 inverseMatrix[1][0] = (-matrix[1][0] * matrix[2][2] + matrix[1][2] * matrix[2][0]) / determinant;
450 inverseMatrix[2][0] = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]) / determinant;
452 inverseMatrix[0][1] = (-matrix[0][1] * matrix[2][2] + matrix[0][2] * matrix[2][1]) / determinant;
453 inverseMatrix[1][1] = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]) / determinant;
454 inverseMatrix[2][1] = (-matrix[0][0] * matrix[2][1] + matrix[0][1] * matrix[2][0]) / determinant;
456 inverseMatrix[0][2] = (matrix[0][1] * matrix[1][2] - matrix[0][2] * matrix[1][1]) / determinant;
457 inverseMatrix[1][2] = (-matrix[0][0] * matrix[1][2] + matrix[0][2] * matrix[1][0]) / determinant;
458 inverseMatrix[2][2] = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]) / determinant;
460 memcpy(matrix, inverseMatrix, MATRIX_SIZE);
466 DoubleMatrix3::Transpose(void)
469 SWAP_VALUES(matrix[0][1], matrix[1][0], tmp);
470 SWAP_VALUES(matrix[0][2], matrix[2][0], tmp);
472 SWAP_VALUES(matrix[1][2], matrix[2][1], tmp);
476 DoubleMatrix3::SetAsNull(void)