Matrix4x4T.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 //=============================================================================
00046 //
00047 //  CLASS Matrix4x4T
00048 //
00049 //=============================================================================
00050 
00051 
00052 #ifndef ACG_MATRIX4X4_HH
00053 #define ACG_MATRIX4X4_HH
00054 
00055 
00056 //== INCLUDES =================================================================
00057 
00058 #include "VectorT.hh"
00059 #include <math.h>
00060 #include "../Config/ACGDefines.hh"
00061 #include <iostream>
00062 
00063 
00064 //== NAMESPACES  ==============================================================
00065 
00066 
00067 namespace ACG {
00068 
00069 
00070 //== MACROS / HELPERS =========================================================
00071 
00072               
00073 #define matrixOperator(src, dst, op) { \
00074     Scalar *_a = dst;\
00075     const Scalar *_b = src;\
00076     *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
00077     *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
00078     *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
00079     *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a   op *_b  ; \
00080 }
00081   
00082 #define MAT(m,r,c) ((m)[(r)+((c)<<2)])
00083 
00084 template<typename Scalar> inline bool checkEpsilon(Scalar x) {
00085   return fabs(x) < (1e-6);
00086 }
00087 
00088 template<> inline bool checkEpsilon(float x) {
00089   return fabs(x) < (1e-4);
00090 }
00091 
00092 
00093 
00094 //== CLASS DEFINITION =========================================================
00095 
00096 
00100 template <class Scalar>
00101 class Matrix4x4T
00102 {
00103 public:
00104    
00106   Matrix4x4T() {}
00107  
00109   template <class OtherScalar>
00110   inline Matrix4x4T(const Matrix4x4T<OtherScalar> _rhs) {
00111     operator=(_rhs);
00112   }
00113   
00116   inline Matrix4x4T(const Scalar _array[16]) {
00117     matrixOperator(_array, mat_, =);
00118   }
00119 
00121   ~Matrix4x4T() {}
00122 
00123 
00125   template<typename otherScalar>
00126   inline Matrix4x4T<Scalar>& operator=(const Matrix4x4T<otherScalar>& _rhs) {
00127     for (int i=0; i<4; ++i)
00128       for (int j=0; j<4; ++j)
00129         operator()(i,j) = _rhs(i,j);
00130     return *this;
00131   }
00132 
00133 
00134 
00136   inline Scalar& operator()(unsigned int row, unsigned int col) {
00137     return MAT(mat_,row,col);
00138   }
00139 
00141   inline const Scalar& operator()(unsigned int row, unsigned int col) const {
00142     return MAT(mat_,row,col);
00143   }
00144 
00145 
00147   inline bool operator== (const Matrix4x4T<Scalar>& _rhs) const {
00148     int i;
00149     const Scalar *a = mat_;
00150     const Scalar *b = _rhs.mat_;
00151     for(i=0;i< 16;i++,a++,b++)
00152       if(! checkEpsilon( *a - *b ))
00153         return false;
00154     return true;
00155   }
00156 
00158   inline bool operator!= (const Matrix4x4T<Scalar>& _rhs) const {
00159     return !( operator==(_rhs) );
00160   }
00161 
00162 
00164   inline Matrix4x4T operator+ (const Matrix4x4T<Scalar>& _rhs) const {
00165     Matrix4x4T<Scalar> m(_rhs);
00166     matrixOperator(mat_, m.mat_, += );
00167     return m;
00168   }
00169 
00171   inline Matrix4x4T operator- (const Matrix4x4T<Scalar>& _rhs) const {
00172     Matrix4x4T<Scalar> m(*this);
00173     matrixOperator(_rhs.mat_, m.mat_, -= );
00174     return m;
00175   }
00176 
00178   Matrix4x4T operator*(const Matrix4x4T<Scalar>& inst) const;
00179 
00180 
00182   inline Matrix4x4T& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
00183     matrixOperator(_rhs.mat_, mat_, += );
00184     return *this;
00185   }
00186 
00188   inline Matrix4x4T& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
00189     matrixOperator(_rhs.mat_, mat_, -= );
00190     return *this;
00191   }
00192 
00194   Matrix4x4T& operator*= (const Matrix4x4T<Scalar>& _rhs);
00195 
00197   Matrix4x4T& leftMult(const Matrix4x4T<Scalar>& _rhs);
00198 
00199 
00201   template <typename T>
00202   inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const;
00203 
00205   template <typename T>
00206   inline VectorT<T,3> transform_point(const VectorT<T,3>& _v) const;
00207 
00209   template <typename T>
00210   inline VectorT<T,3> transform_vector(const VectorT<T,3>& _v) const;
00211 
00212   
00214   inline void clear();
00215 
00217   inline void identity();
00218 
00219 
00221   inline bool is_identity() const {
00222     int i;
00223     const Scalar *a = mat_;
00224     Scalar b = 0.0;
00225     for(i=0;i< 16;i++,a++,b++) {
00226       if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
00227          b = 1.0;
00228       else
00229          b = 0.0;
00230       if(! checkEpsilon( *a - b ))
00231         return false;
00232     }
00233     return true;
00234   }
00235 
00236 
00238   inline void transpose();
00239 
00240   
00242   bool invert();
00243 
00244 
00248   inline const Scalar* get_raw_data() const { return mat_; }
00249   inline const Scalar* raw() const { return mat_; }
00250   inline const Scalar* data() const { return mat_; }
00251 
00252 private:
00253 
00254     Scalar mat_[16];
00255 };
00256 
00257 
00259 typedef Matrix4x4T<float>  Matrix4x4f;
00261 typedef Matrix4x4T<double> Matrix4x4d;
00262 
00263 
00264 
00265 
00266 //== IO to/from streams =======================================================
00267 
00268 
00270 template<typename Scalar>
00271 inline std::ostream& 
00272 operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
00273 {
00274   for(int i=0; i<4; i++)
00275   {
00276     for(int j=0; j<4; j++)
00277       os << m(i,j) << " ";
00278     os << "\n";
00279   }
00280   return os;
00281 }
00282  
00283  
00285 template<typename Scalar>
00286 inline std::istream& 
00287 operator>>(std::istream& is, Matrix4x4T<Scalar>& m) 
00288 {
00289   for(int i=0; i<4; i++)
00290     for(int j=0; j<4; j++)
00291       is >> m(i,j);
00292   return is;
00293 }
00294 
00295 
00296 //=============================================================================
00297 
00298 #undef matrixOperator
00299 #undef MAT
00300 
00301 //=============================================================================
00302 } // namespace ACG
00303 //=============================================================================
00304 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
00305 #define ACG_MATRIX4X4_TEMPLATES
00306 #include "Matrix4x4T.cc"
00307 #endif
00308 //=============================================================================
00309 #endif // ACG_MATRIX4X4_HH defined
00310 //=============================================================================
00311 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .