TAPs 0.7.7.3
TAPsMatrix2x2.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsMatrix2x2.cpp
00003 
00004 Matrix2x2 class is a class for 2-by-2 matrices.
00005 
00006 SUKITTI PUNAK   (09/12/2004)
00007 UPDATE          (07/30/2005)
00008 ******************************************************************************/
00009 #include "TAPsMatrix2x2.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs
00015 //=============================================================================
00016 // Constructors and Destructor
00017 //-----------------------------------------------------------------------------
00018 template <typename T>
00019 Matrix2x2<T>::Matrix2x2 ()
00020 {
00021     // Identity Matrix
00022     e[0] = e[3] = Math<T>::ONE;
00023     e[1] = e[2] = Math<T>::ZERO;
00024 }
00025 //-----------------------------------------------------------------------------
00026 template <typename T>
00027 Matrix2x2<T>::Matrix2x2 ( Matrix2x2<T> const &M )
00028 {
00029     e[0] = M.e[0];  e[1] = M.e[1];  e[2] = M.e[2];  e[3] = M.e[3];
00030 }
00031 //-----------------------------------------------------------------------------
00032 template <typename T>
00033 Matrix2x2<T>::Matrix2x2 ( T t )
00034 {
00035     e[0] = e[1] = e[2] = e[3] = t;
00036 }
00037 //-----------------------------------------------------------------------------
00038 template <typename T>
00039 Matrix2x2<T>::Matrix2x2 ( T e00, T e01, T e10, T e11 )
00040 {
00041     e[0] = e00;  e[1] = e01;  e[2] = e10;  e[3] = e11;
00042 }
00043 //-----------------------------------------------------------------------------
00044 template <typename T>
00045 Matrix2x2<T>::Matrix2x2 ( T const af[4] )
00046 {
00047     e[0] = af[0];  e[1] = af[1];  e[2] = af[2];  e[3] = af[3];
00048 }
00049 //-----------------------------------------------------------------------------
00050 template <typename T>
00051 Matrix2x2<T>::~Matrix2x2 ()
00052 {}
00053 //-----------------------------------------------------------------------------
00054 //=============================================================================
00055 // Member Access
00056 //-----------------------------------------------------------------------------
00057 template <typename T>
00058 inline T & Matrix2x2<T>::operator[] ( int i )
00059 {   return e[i];    }
00060 //-----------------------------------------------------------------------------
00061 template <typename T>
00062 inline T const & Matrix2x2<T>::operator[] ( int i ) const
00063 {   return e[i];    }
00064 //-----------------------------------------------------------------------------
00065 template <typename T>
00066 inline T & Matrix2x2<T>::operator() ( int r, int c )
00067 {   return e[r*2 + c];  }
00068 //-----------------------------------------------------------------------------
00069 template <typename T>
00070 inline T const & Matrix2x2<T>::operator() ( int r, int c ) const
00071 {   return e[r*2 + c];  }
00072 //-----------------------------------------------------------------------------
00073 //=============================================================================
00074 // Convert to one dimension array
00075 //-----------------------------------------------------------------------------
00076 template <typename T>
00077 Matrix2x2<T>::operator const T * () const
00078 {   return e;   }
00079 //-----------------------------------------------------------------------------
00080 template <typename T>
00081 Matrix2x2<T>::operator T * ()
00082 {   return e;   }
00083 //-----------------------------------------------------------------------------
00084 //=============================================================================
00085 // Useful Functions
00086 //-----------------------------------------------------------------------------
00087 template <typename T>
00088 inline void Matrix2x2<T>::SetAllElements (  T t )
00089 {
00090     e[0] = e[1] = e[2] = e[3] = t;
00091 }
00092 //-----------------------------------------------------------------------------
00093 template <typename T>
00094 inline void Matrix2x2<T>::SetAllElements (  T e00, T e01, T e10, T e11 )
00095 {
00096     e[0] = e00;  e[1] = e01;  e[2] = e10;  e[3] = e11;
00097 }
00098 //-----------------------------------------------------------------------------
00099 template <typename T>
00100 inline void Matrix2x2<T>::SetAllElements ( T const af[4] )
00101 {
00102     e[0] = af[0];  e[1] = af[1];  e[2] = af[2];  e[3] = af[3];
00103 }
00104 //-----------------------------------------------------------------------------
00105 template <typename T>
00106 inline void Matrix2x2<T>::MakeIdentity ()
00107 {
00108     e[0] = e[3] = Math<T>::ONE;
00109     e[1] = e[2] = Math<T>::ZERO;
00110 }
00111 //-----------------------------------------------------------------------------
00112 template <typename T>
00113 inline void Matrix2x2<T>::MakeDiagonal ( T d )
00114 {
00115     e[0] = e[3] = d;
00116     e[1] = e[2] = Math<T>::ZERO;
00117 }
00118 //-----------------------------------------------------------------------------
00119 template <typename T>
00120 inline void Matrix2x2<T>::MakeDiagonal ( T const af[2] )
00121 {
00122     e[0] = af[0];
00123     e[3] = af[1];
00124     e[1] = e[2] = Math<T>::ZERO;
00125 }
00126 //-----------------------------------------------------------------------------
00127 template <typename T>
00128 inline void Matrix2x2<T>::MakeZero ()
00129 {
00130     e[0] = e[1] = e[2] = e[3] = Math<T>::ZERO;
00131 }
00132 //-----------------------------------------------------------------------------
00133 template <typename T>
00134 inline bool Matrix2x2<T>::IsIdentity () const
00135 {
00136     return  e[ 0] == Math<T>::ONE   &&
00137             e[ 1] == Math<T>::ZERO  &&
00138             e[ 2] == Math<T>::ZERO  &&
00139             e[ 3] == Math<T>::ONE;
00140 }
00141 //-----------------------------------------------------------------------------
00142 //=============================================================================
00143 // Matrix Operations
00144 //-----------------------------------------------------------------------------
00145 template <typename T>
00146 inline Matrix2x2<T> & Matrix2x2<T>::Transposed ()
00147 {
00148     T temp;
00149     temp = e[1];  e[1] = e[2];  e[2] = temp;
00150     return *this;
00151 }
00152 //-----------------------------------------------------------------------------
00153 template <typename T>
00154 inline Matrix2x2<T> Matrix2x2<T>::GetTranspose() const
00155 {
00156     return  Matrix2x2<T> ( e[0], e[2], e[1], e[3] );
00157 }
00158 //-----------------------------------------------------------------------------
00159 // Inverse the matrix
00160 template <typename T>
00161 inline Matrix2x2<T> & Matrix2x2<T>::Inversed ()
00162 {
00163     *this = (*this).GetInverse();
00164     return *this;
00165 }
00166 //-----------------------------------------------------------------------------
00167 // Get the matrix inverse
00168 template <typename T>
00169 Matrix2x2<T> Matrix2x2<T>::GetInverse () const
00170 {
00171     T det = GetDeterminant();
00172 
00173     if ( -Math<T>::EPSILON < det && det < Math<T>::EPSILON )
00174         return Matrix2x2( Math<T>::INFINITY );
00175 
00176     return Matrix2x2<T>( e[3] / det, 
00177                         -e[1] / det, 
00178                         -e[2] / det, 
00179                          e[0] / det );
00180 }
00181 //-----------------------------------------------------------------------------
00182 template <typename T>
00183 inline T Matrix2x2<T>::GetDeterminant () const
00184 {
00185     return e[0]*e[3] - e[1]*e[2];
00186 }
00187 //-----------------------------------------------------------------------------
00188 //=============================================================================
00189 // Assignment Overloaded Operators
00190 //-----------------------------------------------------------------------------
00191 template <typename T>
00192 Matrix2x2<T> & Matrix2x2<T>::operator= ( Matrix2x2<T> const &M )
00193 {
00194     if ( this != &M )
00195     {
00196         e[0] = M.e[0];  e[1] = M.e[1];  e[2] = M.e[2];  e[3] = M.e[3];
00197     }
00198     return *this;
00199 }
00200 //-----------------------------------------------------------------------------
00201 //=============================================================================
00202 // Unary Overloaded Operators
00203 //-----------------------------------------------------------------------------
00204 template <typename T>
00205 Matrix2x2<T> Matrix2x2<T>::operator- ()
00206 {
00207     return Matrix2x2<T>( -e[0], -e[1], -e[2], -e[3] );
00208 }
00209 //-----------------------------------------------------------------------------
00210 //=============================================================================
00211 // Assign Overloaded Operators
00212 //-----------------------------------------------------------------------------
00213 template <typename T>
00214 Matrix2x2<T> & Matrix2x2<T>::operator+= ( Matrix2x2<T> const &M )
00215 {
00216     e[0] += M.e[0];  e[1] += M.e[1];  e[2] += M.e[2];  e[3] += M.e[3];
00217     return *this;
00218 }
00219 //-----------------------------------------------------------------------------
00220 template <typename T>
00221 Matrix2x2<T> & Matrix2x2<T>::operator-= ( Matrix2x2<T> const &M )
00222 {
00223     e[0] -= M.e[0];  e[1] -= M.e[1];  e[2] -= M.e[2];  e[3] -= M.e[3];
00224     return *this;
00225 }
00226 //-----------------------------------------------------------------------------
00227 template <typename T>
00228 Matrix2x2<T> & Matrix2x2<T>::operator*= ( Matrix2x2<T> const &M )
00229 {
00230     *this = (*this) * M;
00231     return *this;
00232 }
00233 //-----------------------------------------------------------------------------
00234 template <typename T>
00235 Matrix2x2<T> & Matrix2x2<T>::operator*= ( T s )
00236 {
00237     e[0] *= s;  e[1] *= s;  e[2] *= s;  e[3] *= s;
00238     return *this;
00239 }
00240 //-----------------------------------------------------------------------------
00241 template <typename T>
00242 Matrix2x2<T> & Matrix2x2<T>::operator/= ( T s )
00243 {
00244     e[0] /= s;  e[1] /= s;  e[2] /= s;  e[3] /= s;
00245     return *this;
00246 }
00247 //-----------------------------------------------------------------------------
00248 //=============================================================================
00249 // Binary Overloade Operators
00250 //-----------------------------------------------------------------------------
00251 template <typename T>
00252 Matrix2x2<T> Matrix2x2<T>::operator+ ( Matrix2x2<T> const &M ) const
00253 {
00254     return Matrix2x2<T>( e[0]+M.e[0], e[1]+M.e[1], e[2]+M.e[2], e[3]+M.e[3] );
00255 }
00256 //-----------------------------------------------------------------------------
00257 template <typename T>
00258 Matrix2x2<T> Matrix2x2<T>::operator- ( Matrix2x2<T> const &M ) const
00259 {
00260     return Matrix2x2<T>( e[0]-M.e[0], e[1]-M.e[1], e[2]-M.e[2], e[3]-M.e[3] );
00261 }
00262 //-----------------------------------------------------------------------------
00263 template <typename T>
00264 Matrix2x2<T> Matrix2x2<T>::operator* ( Matrix2x2<T> const &M ) const
00265 {
00266     return Matrix2x2<T>(    e[0]*M.e[0] + e[1]*M.e[2], 
00267                             e[0]*M.e[1] + e[1]*M.e[3], 
00268                             e[2]*M.e[0] + e[3]*M.e[2], 
00269                             e[2]*M.e[1] + e[3]*M.e[3] );
00270 }
00271 //-----------------------------------------------------------------------------
00272 template <typename T>
00273 Matrix2x2<T> Matrix2x2<T>::operator* ( T s ) const
00274 {
00275     return Matrix2x2<T>( e[0]*s, e[1]*s, e[2]*s, e[3]*s );
00276 }
00277 //-----------------------------------------------------------------------------
00278 template <typename T>
00279 Matrix2x2<T> Matrix2x2<T>::operator/ ( T s ) const
00280 {
00281     return Matrix2x2<T>( e[0]/s, e[1]/s, e[2]/s, e[3]/s );
00282 }
00283 //-----------------------------------------------------------------------------
00284 template <typename T>
00285 inline void Matrix2x2<T>::MultLeft ( Matrix2x2<T> const &M )
00286 {
00287     *this = M * (*this);
00288 }
00289 //-----------------------------------------------------------------------------
00290 template <typename T>
00291 inline void Matrix2x2<T>::MultRight ( Matrix2x2<T> const &M )
00292 {
00293     *this *= M;
00294 }
00295 //-----------------------------------------------------------------------------
00296 // Matrix * Vector
00297 template <typename T>
00298 Vector2<T> Matrix2x2<T>::operator* ( Vector2<T> const &V ) const
00299 {
00300     return Vector2<T>(
00301             V[0]*e[0] + V[1]*e[1],
00302             V[0]*e[2] + V[1]*e[3] );
00303 }
00304 //-----------------------------------------------------------------------------
00305 //=============================================================================
00306 END_NAMESPACE_TAPs
00307 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00308 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines