00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __OPENKN_MATH__MATRIX3X3_HPP__
00027 #define __OPENKN_MATH__MATRIX3X3_HPP__
00028
00029
00030
00031
00032 #include "Matrix.hpp"
00033 #include "Vector3.hpp"
00034
00035 namespace kn
00036 {
00037
00038 template<class T> class Matrix3x3 : public Matrix <T>
00039 {
00040
00041 protected:
00042
00043 public:
00044
00049 Matrix3x3(void)
00050 : Matrix<T>(3)
00051 {
00052 setIdentity();
00053 }
00054
00060 Matrix3x3(const Matrix3x3<T>& m)
00061 : Matrix<T>(3)
00062 {
00063 std::copy(m.begin_, m.end_, this->begin_);
00064 }
00065
00072 Matrix3x3(const Matrix<T>& m);
00073
00080 Matrix3x3(const Matrix3x3<T>* m);
00081
00087 Matrix3x3(const T& d)
00088 : Matrix<T>(3)
00089 {
00090 std::fill(this->begin_, this->end_, d);
00091 }
00092
00093
00100 Matrix3x3(const T* d);
00101
00110 Matrix3x3(const Vector<T>& v,
00111 const bool& setasrows = true);
00112
00121 Matrix3x3(const Vector3<T>* v,
00122 const bool& setasrows = true);
00123
00127 ~Matrix3x3(void)
00128 {
00129 }
00130
00131
00132 public:
00133
00134 using Matrix<T>::operator*;
00135
00136
00143 Vector<T> operator*(const Vector<T>& v) const;
00144
00145
00150 inline size_t rows(void) const
00151 {
00152 return 3;
00153 }
00154
00159 inline size_t columns(void) const
00160 {
00161 return 3;
00162 }
00163
00168 Matrix3x3<T>& transpose(void);
00169
00175 inline Matrix3x3<T> getTranspose(void) const
00176 {
00177 return Matrix3x3<T>(*this).transpose();
00178 }
00179
00183 void setIdentity(void);
00184
00189 inline bool isSquare(void) const
00190 {
00191 return true;
00192 }
00193
00201 void cross3x3(const kn::Vector3<T> v);
00202
00209 Matrix3x3<T>& operator=(const Matrix3x3<T>& m);
00210
00217 Matrix3x3<T> operator+(const Matrix3x3<T>& m) const;
00218
00225 Matrix3x3<T> operator-(const Matrix3x3<T>& m) const;
00226
00233 Matrix3x3<T> operator/(const T& d) const;
00234
00241 Matrix3x3<T> operator*(const Matrix3x3<T>& m) const;
00242
00248 Matrix3x3<T> operator*(const T& d) const;
00249
00254 Matrix3x3<T> operator-(void) const;
00255
00262 Matrix3x3<T>& operator+=(const Matrix3x3<T>& m);
00263
00270 Matrix3x3<T>& operator-=(const Matrix3x3<T>& m);
00271
00279 Matrix3x3<T>& operator/=(const T& d);
00280
00287 Matrix3x3<T>& operator*=(const Matrix3x3<T>& m);
00288
00294 Matrix3x3<T>& operator*=(const T& d);
00295
00302 Vector3<T> getRow(const unsigned int& row) const;
00303
00310 Vector3<T> getColumn(const unsigned int& column) const;
00311
00316 Vector3<T> getDiagonal(void) const;
00317
00323 Matrix3x3<T>& power(const unsigned int& p);
00324
00332 T trace(void) const;
00333
00334 };
00335
00336
00337
00338 template<class T>
00339 Matrix3x3<T>::Matrix3x3(const Matrix3x3<T>* m)
00340 : Matrix<T>(3)
00341 {
00342 if(m==0)
00343 throw MathException("Pointer is null");
00344 std::copy(m->begin_, m->end_, this->begin_);
00345 }
00346
00347
00348 template<class T>
00349 Matrix3x3<T>::Matrix3x3(const T* a)
00350 : Matrix<T>(3)
00351 {
00352 if(a==0)
00353 throw MathException("Pointer is null");
00354 std::copy(a, a + 9, this->begin_);
00355 }
00356
00357
00358 template<class T>
00359 Matrix3x3<T>::Matrix3x3(const Vector<T>& v,
00360 const bool& setasrows)
00361 : Matrix<T>(3)
00362 {
00363 if(v.size()!=9)
00364 throw MathException("Vector size is different from Matrix size");
00365
00366 if(setasrows)
00367 for(unsigned int i = 0; i < 9; ++i)
00368 (this->data)[i] = v[i];
00369 else
00370 for(unsigned int j = 0; j < 3; ++j)
00371 for(unsigned int i = 0; i < 3; ++i)
00372 (this->accessor)[i][j] = v[j*3+i];
00373 }
00374
00375
00376 template<class T>
00377 Matrix3x3<T>::Matrix3x3(const Vector3<T>* v,
00378 const bool& setasrows)
00379 : Matrix<T>(3)
00380 {
00381
00382 if(v==0)
00383 throw MathException("Pointer is null");
00384
00385 if(setasrows)
00386 for(unsigned int i = 0; i < 3; ++i)
00387 for(unsigned int j = 0; j < 3; ++j)
00388 (this->accessor)[i][j] = v[i][j];
00389 else
00390 for(unsigned int j = 0; j < 3; ++j)
00391 for(unsigned int i = 0; i < 3; ++i)
00392 (this->accessor)[i][j] = v[j][i];
00393
00394 }
00395
00396 template<class T>
00397 Matrix3x3<T>::Matrix3x3(const Matrix<T>& m)
00398 : Matrix<T>(3) {
00399 if ((m.rows() != 3) || (m.columns() != 3))
00400 throw MathException("Matrix3x3 Constructor","Matrix sizes are different");
00401 std::copy(m.getMatrixArray(), m.getMatrixArray()+m.rows()*m.columns(), this->begin_);
00402 }
00403
00404 template<class T>
00405 Vector<T> Matrix3x3<T>::operator*(const Vector<T>& v) const
00406 {
00407 if(v.size() != 3)
00408 throw MathException("Matrice rows and vector size are different");
00409
00410 Vector<T> vtmp(3);
00411 vtmp[0] = v[0]*(this->accessor)[0][0] + v[1]*(this->accessor)[0][1] + v[2]*(this->accessor)[0][2];
00412 vtmp[1] = v[0]*(this->accessor)[1][0] + v[1]*(this->accessor)[1][1] + v[2]*(this->accessor)[1][2];
00413 vtmp[2] = v[0]*(this->accessor)[2][0] + v[1]*(this->accessor)[2][1] + v[2]*(this->accessor)[2][2];
00414 return vtmp;
00415 }
00416
00417
00418 template<class T>
00419 Matrix3x3<T>& Matrix3x3<T>::transpose(void)
00420 {
00421
00422 for(unsigned int i = 0; i < 3; ++i)
00423 for(unsigned int j = 0; j < i; ++j)
00424 std::swap((this->accessor)[i][j],(this->accessor)[j][i]);
00425
00426 return *this;
00427 }
00428
00429
00430 template<class T>
00431 void Matrix3x3<T>::setIdentity(void)
00432 {
00433 this->setZero();
00434 (this->accessor)[0][0] = (this->accessor)[1][1] = (this->accessor)[2][2] = T(1.0);
00435 }
00436
00437
00438
00439
00447 template<class U>
00448 Vector<U> operator* (const Vector3<U>& v,
00449 const Matrix3x3<U>& m)
00450 {
00451
00452 Vector<U> result(3);
00453 result[0] = m[0][0]*v[0] + m[1][0]*v[1] + m[2][0]*v[2];
00454 result[1] = m[0][1]*v[0] + m[1][1]*v[1] + m[2][1]*v[2];
00455 result[2] = m[0][2]*v[0] + m[1][2]*v[1] + m[2][2]*v[2];
00456 return result;
00457 }
00458
00459
00460 template<typename T>
00461 Matrix3x3<T>& Matrix3x3<T>::operator=(const Matrix3x3<T>& m){
00462 if(&m == this) return *this;
00463 std::copy(m.begin_,m.end_,this->begin_);
00464
00465 return *this;
00466 }
00467
00468
00469 template<typename T>
00470 Matrix3x3<T> Matrix3x3<T>::operator+(const Matrix3x3<T>& m) const{
00471 Matrix3x3<T> result = *this;
00472 result += m;
00473 return result;
00474 }
00475
00476
00477 template<typename T>
00478 Matrix3x3<T> Matrix3x3<T>::operator-(const Matrix3x3<T>& m) const{
00479 Matrix3x3<T> result = *this;
00480 result -= m;
00481 return result;
00482 }
00483
00484
00485 template<typename T>
00486 Matrix3x3<T> Matrix3x3<T>::operator/(const T& d) const{
00487 Matrix3x3<T> result = *this;
00488 result /= d;
00489 return result;
00490 }
00491
00492
00493 template<typename T>
00494 Matrix3x3<T> Matrix3x3<T>::operator*(const Matrix3x3<T>& m) const{
00495
00496 Matrix3x3<T> result;
00497 T sum;
00498 for(unsigned int i = 0; i < 3; ++i)
00499 for(unsigned int j = 0; j < 3; ++j){
00500 sum = T(0.0);
00501
00502 for(unsigned int k = 0; k < 3; ++k){
00503 sum += (*this)[i][k]*m[k][j];
00504 }
00505 result[i][j] = sum;
00506 }
00507
00508 return result;
00509 }
00510
00511 template<typename T>
00512 Matrix3x3<T> Matrix3x3<T>::operator*(const T& d) const{
00513 Matrix3x3<T> result = *this;
00514 result *= d;
00515 return result;
00516 }
00517
00518
00519 template<typename T>
00520 Matrix3x3<T> Matrix3x3<T>::operator-(void) const{
00521 Matrix3x3<T> result = *this;
00522 std::transform(this->begin_, this->end_, result.begin_, std::negate<T>());
00523 return result;
00524 }
00525
00526
00527 template<typename T>
00528 Matrix3x3<T>& Matrix3x3<T>::operator+=(const Matrix3x3<T>& m){
00529 std::transform(this->begin_, this->end_, m.begin_, this->begin_, std::plus<T>());
00530 return *this;
00531 }
00532
00533
00534 template<typename T>
00535 Matrix3x3<T>& Matrix3x3<T>::operator-=(const Matrix3x3<T>& m){
00536 std::transform(this->begin_, this->end_, m.begin_, this->begin_, std::minus<T>());
00537 return *this;
00538 }
00539
00540 template<typename T>
00541 Matrix3x3<T>& Matrix3x3<T>::operator/=(const T& d){
00542 if(d==0)
00543 throw MathException("Value is null");
00544 std::transform(this->begin_, this->end_, this->begin_, std::bind2nd(std::divides<T>(),d));
00545 return *this;
00546 }
00547
00548
00549 template<typename T>
00550 Matrix3x3<T>& Matrix3x3<T>::operator*=(const Matrix3x3<T>& m){
00551
00552 Matrix3x3<T> tmp = *this;
00553 T sum;
00554 for(unsigned int i = 0; i < 3; ++i)
00555 for(unsigned int j = 0; j < 3; ++j){
00556 sum = T(0.0);
00557
00558 for(unsigned int k = 0; k < 3; ++k){
00559 sum += tmp[i][k]*m[k][j];
00560 }
00561 this->accessor[i][j] = sum;
00562 }
00563 return *this;
00564 }
00565
00566 template<typename T>
00567 Matrix3x3<T>& Matrix3x3<T>::operator*=(const T& d){
00568 std::transform(this->begin_, this->end_, this->begin_, std::bind2nd(std::multiplies<T>(),d));
00569 return *this;
00570 }
00571
00572
00573 template<typename T>
00574 Matrix3x3<T>& Matrix3x3<T>::power(const unsigned int& p){
00575
00576 if(p == 0){
00577 setIdentity();
00578 return *this;
00579 }
00580 Matrix3x3<T> tmp(*this);
00581
00582 for(unsigned int i=1; i < p; ++i)
00583 *this *= tmp;
00584
00585 return *this;
00586 }
00587
00588 template<typename T>
00589 Vector3<T> Matrix3x3<T>::getDiagonal(void) const{
00590 Vector3<T> v;
00591 for(unsigned int i = 0; i < 3; ++i)
00592 v[i] = this->accessor[i][i];
00593 return v;
00594 }
00595
00596 template<typename T>
00597 Vector3<T> Matrix3x3<T>::getRow(const unsigned int& row) const{
00598 if(row >= this->rowsMatrix)
00599 throw MathException("Out of bounds");
00600 Vector3<T> v;
00601 for(unsigned int i = 0; i < 3; ++i)
00602 v[i] = this->accessor[row][i];
00603 return v;
00604 }
00605
00606 template<typename T>
00607 Vector3<T> Matrix3x3<T>::getColumn(const unsigned int& column) const{
00608 if(column >= this->columnsMatrix)
00609 throw MathException("Out of bounds");
00610 Vector3<T> v;
00611 for(unsigned int i = 0; i < 3; ++i)
00612 v[i] = this->accessor[i][column];
00613 return v;
00614 }
00615
00616 template<typename T>
00617 void Matrix3x3<T>::cross3x3(const kn::Vector3<T> v){
00618 this->accessor[0][0] = 0.0; this->accessor[0][1] = -v[2]; this->accessor[0][2] = v[1];
00619 this->accessor[1][0] = v[2]; this->accessor[1][1] = 0.0; this->accessor[1][2] = -v[0];
00620 this->accessor[2][0] = -v[1]; this->accessor[2][1] = v[0]; this->accessor[2][2] = 0.0;
00621 }
00622
00623
00631 template<typename T>
00632 T Matrix3x3<T>::trace(void) const{
00633 return (this->accessor[0][0] + this->accessor[1][1] + this->accessor[2][2]);
00634 }
00635
00636
00643 template<class U> Matrix3x3<U> operator* (const U& d,
00644 const Matrix3x3<U>& m){
00645 return m*d;
00646 }
00647
00648
00649
00650
00651
00652
00653 typedef Matrix3x3<float> Matrix3x3f;
00654 typedef Matrix3x3<double> Matrix3x3d;
00655 typedef Matrix3x3<int> Matrix3x3i;
00656
00657
00658 }
00659
00660
00661 #endif