TAPs 0.7.7.3
TAPsMatrix4x4.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsMatrix4x4.cpp
00003 
00004 Matrix4x4 class is a class for 4-by-4 matrices.
00005 
00006 SUKITTI PUNAK   (09/14/2004)
00007 UPDATE          (05/06/2010)
00008 ******************************************************************************/
00009 #include "TAPsMatrix4x4.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 //-----------------------------------------------------------------------------
00017 // Static Data Members for Data Conversions
00018 template <typename T> float         Matrix4x4<T>::g_f[16];
00019 template <typename T> double        Matrix4x4<T>::g_d[16];
00020 template <typename T> long double   Matrix4x4<T>::g_ld[16];
00021 //=============================================================================
00022 // Constructors and Destructor
00023 //-----------------------------------------------------------------------------
00024 template <typename T>
00025 Matrix4x4<T>::Matrix4x4 ()
00026 {
00027     // Identity Matrix
00028     /*e0*/  e[ 1] = e[ 2] = e[ 3] = 
00029     e[ 4] = /*e5*/  e[ 6] = e[ 7] = 
00030     e[ 8] = e[ 9] /*e10*/ = e[11] = 
00031     e[12] = e[13] = e[14]  /*e15*/= Math<T>::ZERO;
00032     e[ 0] = e[ 5] = e[10] = e[15] = Math<T>::ONE;
00033 }
00034 //-----------------------------------------------------------------------------
00035 template <typename T>
00036 Matrix4x4<T>::Matrix4x4 ( Matrix4x4<T> const &M )
00037 {
00038     e[ 0] = M.e[ 0];  e[ 1] = M.e[ 1];  e[ 2] = M.e[ 2];  e[ 3] = M.e[ 3];
00039     e[ 4] = M.e[ 4];  e[ 5] = M.e[ 5];  e[ 6] = M.e[ 6];  e[ 7] = M.e[ 7];
00040     e[ 8] = M.e[ 8];  e[ 9] = M.e[ 9];  e[10] = M.e[10];  e[11] = M.e[11];
00041     e[12] = M.e[12];  e[13] = M.e[13];  e[14] = M.e[14];  e[15] = M.e[15];
00042 }
00043 //-----------------------------------------------------------------------------
00044 template <typename T>
00045 Matrix4x4<T>::Matrix4x4 ( T t )
00046 {
00047     e[ 0] = e[ 1] = e[ 2] = e[ 3] = 
00048     e[ 4] = e[ 5] = e[ 6] = e[ 7] = 
00049     e[ 8] = e[ 9] = e[10] = e[11] = 
00050     e[12] = e[13] = e[14] = e[15] = t;
00051 }
00052 //-----------------------------------------------------------------------------
00053 template <typename T>
00054 Matrix4x4<T>::Matrix4x4 ( T e00, T e01, T e02, T e03, 
00055                           T e04, T e05, T e06, T e07, 
00056                           T e08, T e09, T e10, T e11, 
00057                           T e12, T e13, T e14, T e15 )
00058 {
00059     e[ 0] = e00;  e[ 1] = e01;  e[ 2] = e02;  e[ 3] = e03;
00060     e[ 4] = e04;  e[ 5] = e05;  e[ 6] = e06;  e[ 7] = e07;
00061     e[ 8] = e08;  e[ 9] = e09;  e[10] = e10;  e[11] = e11;
00062     e[12] = e12;  e[13] = e13;  e[14] = e14;  e[15] = e15;
00063 }
00064 //-----------------------------------------------------------------------------
00065 template <typename T>
00066 Matrix4x4<T>::Matrix4x4 ( T const af[16] )
00067 {
00068     e[ 0] = af[ 0];  e[ 1] = af[ 1];  e[ 2] = af[ 2];  e[ 3] = af[ 3];
00069     e[ 4] = af[ 4];  e[ 5] = af[ 5];  e[ 6] = af[ 6];  e[ 7] = af[ 7];
00070     e[ 8] = af[ 8];  e[ 9] = af[ 9];  e[10] = af[10];  e[11] = af[11];
00071     e[12] = af[12];  e[13] = af[13];  e[14] = af[14];  e[15] = af[15];
00072 }
00073 //-----------------------------------------------------------------------------
00074 template <typename T>
00075 Matrix4x4<T>::Matrix4x4 ( Matrix3x3<T> const & M )
00076 {
00077     e[ 0] = M[0];  e[ 1] = M[1];  e[ 2] = M[2];  e[ 3] = 0;
00078     e[ 4] = M[3];  e[ 5] = M[4];  e[ 6] = M[5];  e[ 7] = 0;
00079     e[ 8] = M[6];  e[ 9] = M[7];  e[10] = M[8];  e[11] = 0;
00080     e[12] = 0;     e[13] = 0;     e[14] = 0;     e[15] = 1;
00081 }
00082 //-----------------------------------------------------------------------------
00083 template <typename T>
00084 Matrix4x4<T>::Matrix4x4 ( Vector3<T> const & V )
00085 {
00086     e[ 0] = V[0];  e[ 1] = 0;     e[ 2] = 0;     e[ 3] = 0;
00087     e[ 4] = 0;     e[ 5] = V[1];  e[ 6] = 0;     e[ 7] = 0;
00088     e[ 8] = 0;     e[ 9] = 0;     e[10] = V[2];  e[11] = 0;
00089     e[12] = 0;     e[13] = 0;     e[14] = 0;     e[15] = 1;
00090 }
00091 //-----------------------------------------------------------------------------
00092 template <typename T>
00093 Matrix4x4<T>::Matrix4x4 ( Vector4<T> const & V )
00094 {
00095     e[ 0] = V[0];  e[ 1] = 0;     e[ 2] = 0;     e[ 3] = 0;
00096     e[ 4] = 0;     e[ 5] = V[1];  e[ 6] = 0;     e[ 7] = 0;
00097     e[ 8] = 0;     e[ 9] = 0;     e[10] = V[2];  e[11] = 0;
00098     e[12] = 0;     e[13] = 0;     e[14] = 0;     e[15] = V[3];
00099 }
00100 //-----------------------------------------------------------------------------
00101 template <typename T>
00102 Matrix4x4<T>::~Matrix4x4 ()
00103 {}
00104 //-----------------------------------------------------------------------------
00105 //=============================================================================
00106 // Member Access
00107 //-----------------------------------------------------------------------------
00108 template <typename T>
00109 inline T & Matrix4x4<T>::operator[] ( int i )
00110 {   return e[i];    }
00111 //-----------------------------------------------------------------------------
00112 template <typename T>
00113 inline T const & Matrix4x4<T>::operator[] ( int i ) const
00114 {   return e[i];    }
00115 //-----------------------------------------------------------------------------
00116 template <typename T>
00117 inline T & Matrix4x4<T>::operator() ( int r, int c )
00118 {   return e[r*4 + c];  }
00119 //-----------------------------------------------------------------------------
00120 template <typename T>
00121 inline T const & Matrix4x4<T>::operator() ( int r, int c ) const
00122 {   return e[r*4 + c];  }
00123 //-----------------------------------------------------------------------------
00124 //=============================================================================
00125 // Convert to one dimension array
00126 //-----------------------------------------------------------------------------
00127 template <typename T>
00128 Matrix4x4<T>::operator const T * () const
00129 {   return e;   }
00130 //-----------------------------------------------------------------------------
00131 template <typename T>
00132 Matrix4x4<T>::operator T * ()
00133 {   return e;   }
00134 //-----------------------------------------------------------------------------
00135 //=============================================================================
00136 // Useful Functions
00137 //-----------------------------------------------------------------------------
00138 template <typename T>
00139 inline void Matrix4x4<T>::SetAllElements (  T t )
00140 {
00141     e[ 0] = e[ 1] = e[ 2] = e[ 3] = 
00142     e[ 4] = e[ 5] = e[ 6] = e[ 7] = 
00143     e[ 8] = e[ 9] = e[10] = e[11] = 
00144     e[12] = e[13] = e[14] = e[15] = t;
00145 }
00146 //-----------------------------------------------------------------------------
00147 template <typename T>
00148 inline void Matrix4x4<T>::SetAllElements (  T e00, T e01, T e02, T e03, 
00149                                             T e10, T e11, T e12, T e13, 
00150                                             T e20, T e21, T e22, T e23, 
00151                                             T e30, T e31, T e32, T e33 )
00152 {
00153     e[ 0] = e00;  e[ 1] = e01;  e[ 2] = e02; e[ 3] = e03;
00154     e[ 4] = e10;  e[ 5] = e11;  e[ 6] = e12; e[ 7] = e13;
00155     e[ 8] = e20;  e[ 9] = e21;  e[10] = e22; e[11] = e23;
00156     e[12] = e30;  e[13] = e31;  e[14] = e32; e[15] = e33;
00157 }
00158 //-----------------------------------------------------------------------------
00159 template <typename T>
00160 inline void Matrix4x4<T>::SetAllElements ( T const af[16] )
00161 {
00162     e[ 0] = af[ 0];  e[ 1] = af[ 1];  e[ 2] = af[ 2]; e[ 3] = af[ 3];
00163     e[ 4] = af[ 4];  e[ 5] = af[ 5];  e[ 6] = af[ 6]; e[ 7] = af[ 7];
00164     e[ 8] = af[ 8];  e[ 9] = af[ 9];  e[10] = af[10]; e[11] = af[11];
00165     e[12] = af[12];  e[13] = af[13];  e[14] = af[14]; e[15] = af[15];
00166 }
00167 //-----------------------------------------------------------------------------
00168 template <typename T>
00169 inline void Matrix4x4<T>::MakeIdentity ()
00170 {
00171     e[ 0] = e[ 5] = e[10] = e[15] = Math<T>::ONE;
00172             e[ 1] = e[ 2] = e[ 3] = 
00173     e[ 4] =         e[ 6] = e[ 7] = 
00174     e[ 8] = e[ 9] =         e[11] = 
00175     e[12] = e[13] = e[14]         = Math<T>::ZERO;
00176 }
00177 //-----------------------------------------------------------------------------
00178 template <typename T>
00179 inline void Matrix4x4<T>::MakeDiagonal ( T d )
00180 {
00181     e[ 0] = e[ 5] = e[10] = e[15] = static_cast<T>(d);
00182             e[ 1] = e[ 2] = e[ 3] = 
00183     e[ 4] =         e[ 6] = e[ 7] = 
00184     e[ 8] = e[ 9] =         e[11] = 
00185     e[12] = e[13] = e[14]         = Math<T>::ZERO;
00186 }
00187 //-----------------------------------------------------------------------------
00188 template <typename T>
00189 inline void Matrix4x4<T>::MakeDiagonal ( T const af[4] )
00190 {
00191     e[ 0] = static_cast<T>(af[0]);
00192     e[ 5] = static_cast<T>(af[1]);
00193     e[10] = static_cast<T>(af[2]);
00194     e[15] = static_cast<T>(af[3]);
00195             e[ 1] = e[ 2] = e[ 3] = 
00196     e[ 4] =         e[ 6] = e[ 7] = 
00197     e[ 8] = e[ 9] =         e[11] = 
00198     e[12] = e[13] = e[14]         = Math<T>::ZERO;
00199 }
00200 //-----------------------------------------------------------------------------
00201 template <typename T>
00202 inline void Matrix4x4<T>::MakeZero ()
00203 {
00204     e[ 0] = e[ 1] = e[ 2] = e[ 3] = 
00205     e[ 4] = e[ 5] = e[ 6] = e[ 7] = 
00206     e[ 8] = e[ 9] = e[10] = e[11] = 
00207     e[12] = e[13] = e[14] = e[15] = Math<T>::ZERO;
00208 }
00209 //-----------------------------------------------------------------------------
00210 template <typename T>
00211 inline bool Matrix4x4<T>::IsIdentity () const
00212 {
00213     return  e[ 0] == Math<T>::ONE   &&
00214             e[ 1] == Math<T>::ZERO  &&
00215             e[ 2] == Math<T>::ZERO  &&
00216             e[ 3] == Math<T>::ZERO  &&
00217 
00218             e[ 4] == Math<T>::ZERO  &&
00219             e[ 5] == Math<T>::ONE   &&
00220             e[ 6] == Math<T>::ZERO  &&
00221             e[ 7] == Math<T>::ZERO  &&
00222             
00223             e[ 8] == Math<T>::ZERO  &&
00224             e[ 9] == Math<T>::ZERO  &&
00225             e[10] == Math<T>::ONE   &&
00226             e[11] == Math<T>::ZERO  &&
00227 
00228             e[12] == Math<T>::ZERO  &&
00229             e[13] == Math<T>::ZERO  &&
00230             e[14] == Math<T>::ZERO  &&
00231             e[15] == Math<T>::ONE;
00232 }
00233 //-----------------------------------------------------------------------------
00234 //=============================================================================
00235 // Matrix Operations
00236 //-----------------------------------------------------------------------------
00237 template <typename T>
00238 inline Matrix4x4<T> & Matrix4x4<T>::Transposed ()
00239 {
00240     T temp;
00241     temp = e[ 1];  e[ 1] = e[ 4];  e[ 4] = temp;
00242     temp = e[ 2];  e[ 2] = e[ 8];  e[ 8] = temp;
00243     temp = e[ 3];  e[ 3] = e[12];  e[12] = temp;
00244     temp = e[ 6];  e[ 6] = e[ 9];  e[ 9] = temp;
00245     temp = e[ 7];  e[ 7] = e[13];  e[13] = temp;
00246     temp = e[11];  e[11] = e[14];  e[14] = temp;
00247     return *this;
00248 }
00249 //-----------------------------------------------------------------------------
00250 template <typename T>
00251 inline Matrix4x4<T> Matrix4x4<T>::GetTranspose() const
00252 {
00253     return  Matrix4x4<T> (  e[ 0], e[ 4], e[ 8], e[12],  
00254                             e[ 1], e[ 5], e[ 9], e[13], 
00255                             e[ 2], e[ 6], e[10], e[14], 
00256                             e[ 3], e[ 7], e[11], e[15] );
00257 }
00258 //-----------------------------------------------------------------------------
00259 // Inverse the matrix
00260 template <typename T>
00261 inline Matrix4x4<T> & Matrix4x4<T>::Inversed ()
00262 {
00263     *this = (*this).GetInverse();
00264     return *this;
00265 }
00266 //-----------------------------------------------------------------------------
00267 // Get the matrix inverse
00268 template <typename T>
00269 Matrix4x4<T> Matrix4x4<T>::GetInverse () const
00270 {
00271     T detInv = T(1) / GetDeterminant();
00272 
00273     //if ( -Math<T>::EPSILON < det && det < Math<T>::EPSILON )
00274     //  return Matrix4x4( Math<T>::INFINITY );
00275 
00276     return Matrix4x4<T>(
00277         ( e[ 5]*e[10]*e[15] - e[ 5]*e[11]*e[14] - e[ 9]*e[ 6]*e[15] + e[ 9]*e[ 7]*e[14] + e[13]*e[ 6]*e[11] - e[13]*e[ 7]*e[10] ) * detInv, 
00278         ( e[ 1]*e[11]*e[14] - e[ 1]*e[10]*e[15] + e[ 9]*e[ 2]*e[15] - e[ 9]*e[ 3]*e[14] - e[13]*e[ 2]*e[11] + e[13]*e[ 3]*e[10] ) * detInv, 
00279         ( e[ 1]*e[ 6]*e[15] - e[ 1]*e[ 7]*e[14] - e[ 5]*e[ 2]*e[15] + e[ 5]*e[ 3]*e[14] + e[13]*e[ 2]*e[ 7] - e[13]*e[ 3]*e[ 6] ) * detInv, 
00280         ( e[ 1]*e[ 7]*e[10] - e[ 1]*e[ 6]*e[11] + e[ 5]*e[ 2]*e[11] - e[ 5]*e[ 3]*e[10] - e[ 9]*e[ 2]*e[ 7] + e[ 9]*e[ 3]*e[ 6] ) * detInv, 
00281 
00282         ( e[ 4]*e[11]*e[14] - e[ 8]*e[ 7]*e[14] - e[12]*e[ 6]*e[11] - e[ 4]*e[10]*e[15] + e[ 8]*e[ 6]*e[15] + e[12]*e[ 7]*e[10] ) * detInv, 
00283         ( e[ 0]*e[10]*e[15] - e[ 0]*e[11]*e[14] - e[ 8]*e[ 2]*e[15] + e[ 8]*e[ 3]*e[14] + e[12]*e[ 2]*e[11] - e[12]*e[ 3]*e[10] ) * detInv, 
00284         ( e[ 0]*e[ 7]*e[14] - e[ 0]*e[ 6]*e[15] + e[ 4]*e[ 2]*e[15] - e[ 4]*e[ 3]*e[14] - e[12]*e[ 2]*e[ 7] + e[12]*e[ 3]*e[ 6] ) * detInv, 
00285         ( e[ 0]*e[ 6]*e[11] - e[ 0]*e[ 7]*e[10] - e[ 4]*e[ 2]*e[11] + e[ 4]*e[ 3]*e[10] + e[ 8]*e[ 2]*e[ 7] - e[ 8]*e[ 3]*e[ 6] ) * detInv, 
00286 
00287         ( e[ 4]*e[ 9]*e[15] - e[ 4]*e[11]*e[13] - e[ 8]*e[ 5]*e[15] + e[ 8]*e[ 7]*e[13] + e[12]*e[ 5]*e[11] - e[12]*e[ 7]*e[ 9] ) * detInv, 
00288         ( e[ 0]*e[11]*e[13] - e[ 0]*e[ 9]*e[15] + e[ 8]*e[ 1]*e[15] - e[ 8]*e[ 3]*e[13] - e[12]*e[ 1]*e[11] + e[12]*e[ 3]*e[ 9] ) * detInv, 
00289         ( e[ 0]*e[ 5]*e[15] - e[ 0]*e[ 7]*e[13] - e[ 4]*e[ 1]*e[15] + e[ 4]*e[ 3]*e[13] + e[12]*e[ 1]*e[ 7] - e[12]*e[ 3]*e[ 5] ) * detInv, 
00290         ( e[ 0]*e[ 7]*e[ 9] - e[ 0]*e[ 5]*e[11] + e[ 4]*e[ 1]*e[11] - e[ 4]*e[ 3]*e[ 9] - e[ 8]*e[ 1]*e[ 7] + e[ 8]*e[ 3]*e[ 5] ) * detInv, 
00291 
00292         ( e[12]*e[ 6]*e[ 9] + e[ 4]*e[10]*e[13] - e[12]*e[ 5]*e[10] - e[ 8]*e[ 6]*e[13] - e[ 4]*e[ 9]*e[14] + e[ 8]*e[ 5]*e[14] ) * detInv, 
00293         ( e[ 0]*e[ 9]*e[14] - e[ 0]*e[10]*e[13] - e[ 8]*e[ 1]*e[14] + e[ 8]*e[ 2]*e[13] + e[12]*e[ 1]*e[10] - e[12]*e[ 2]*e[ 9] ) * detInv, 
00294         ( e[ 0]*e[ 6]*e[13] - e[ 0]*e[ 5]*e[14] + e[ 4]*e[ 1]*e[14] - e[ 4]*e[ 2]*e[13] - e[12]*e[ 1]*e[ 6] + e[12]*e[ 2]*e[ 5] ) * detInv, 
00295         ( e[ 0]*e[ 5]*e[10] - e[ 0]*e[ 6]*e[ 9] - e[ 4]*e[ 1]*e[10] + e[ 4]*e[ 2]*e[ 9] + e[ 8]*e[ 1]*e[ 6] - e[ 8]*e[ 2]*e[ 5] ) * detInv 
00296     );
00297 }
00298 //-----------------------------------------------------------------------------
00299 template <typename T>
00300 inline T Matrix4x4<T>::GetDeterminant () const
00301 {
00302     return  e[ 0] * ( e[ 5]*e[10]*e[15] - e[ 5]*e[11]*e[14] - e[ 9]*e[ 6]*e[15] + e[ 9]*e[ 7]*e[14] + e[13]*e[ 6]*e[11] - e[13]*e[ 7]*e[10] ) 
00303         +   e[ 4] * ( e[ 1]*e[11]*e[14] - e[ 1]*e[10]*e[15] + e[ 9]*e[ 2]*e[15] - e[ 9]*e[ 3]*e[14] - e[13]*e[ 2]*e[11] + e[13]*e[ 3]*e[10] ) 
00304         +   e[ 8] * ( e[ 1]*e[ 6]*e[15] - e[ 1]*e[ 7]*e[14] - e[ 5]*e[ 2]*e[15] + e[ 5]*e[ 3]*e[14] + e[13]*e[ 2]*e[ 7] - e[13]*e[ 3]*e[ 6] ) 
00305         +   e[12] * ( e[ 1]*e[ 7]*e[10] - e[ 1]*e[ 6]*e[11] + e[ 5]*e[ 2]*e[11] - e[ 5]*e[ 3]*e[10] - e[ 9]*e[ 2]*e[ 7] + e[ 9]*e[ 3]*e[ 6] );
00306 }
00307 //-----------------------------------------------------------------------------
00308 //=============================================================================
00309 // Assignment Overloaded Operator
00310 //--------------------------------------------------------------------------
00311 // Assignment Operator
00312 template <typename T>
00313 inline Matrix4x4<T> & Matrix4x4<T>::operator= ( Matrix4x4<T> const &M )
00314 {
00315     if ( this != &M )
00316     {
00317         e[ 0] = M.e[ 0];  e[ 1] = M.e[ 1];  e[ 2] = M.e[ 2];  e[ 3] = M.e[ 3];
00318         e[ 4] = M.e[ 4];  e[ 5] = M.e[ 5];  e[ 6] = M.e[ 6];  e[ 7] = M.e[ 7];
00319         e[ 8] = M.e[ 8];  e[ 9] = M.e[ 9];  e[10] = M.e[10];  e[11] = M.e[11];
00320         e[12] = M.e[12];  e[13] = M.e[13];  e[14] = M.e[14];  e[15] = M.e[15];
00321     }
00322     return *this;
00323 }
00324 //-----------------------------------------------------------------------------
00325 //=============================================================================
00326 // Unary Overloaded Operators
00327 //-----------------------------------------------------------------------------
00328 template <typename T>
00329 Matrix4x4<T> Matrix4x4<T>::operator- ()
00330 {
00331     return Matrix4x4<T>(    -e[ 0], -e[ 1], -e[ 2], -e[ 3], 
00332                             -e[ 4], -e[ 5], -e[ 6], -e[ 7], 
00333                             -e[ 8], -e[ 9], -e[10], -e[11], 
00334                             -e[12], -e[13], -e[14], -e[15] );
00335 }
00336 //-----------------------------------------------------------------------------
00337 //=============================================================================
00338 // Assign Overloaded Operators
00339 //-----------------------------------------------------------------------------
00340 template <typename T>
00341 Matrix4x4<T> & Matrix4x4<T>::operator+= ( Matrix4x4<T> const &M )
00342 {
00343     e[ 0] += M.e[ 0];  e[ 1] += M.e[ 1];  e[ 2] += M.e[ 2];  e[ 3] += M.e[ 3];
00344     e[ 4] += M.e[ 4];  e[ 5] += M.e[ 5];  e[ 6] += M.e[ 6];  e[ 7] += M.e[ 7];
00345     e[ 8] += M.e[ 8];  e[ 9] += M.e[ 9];  e[10] += M.e[10];  e[11] += M.e[11];
00346     e[12] += M.e[12];  e[13] += M.e[13];  e[14] += M.e[14];  e[15] += M.e[15];
00347     return *this;
00348 }
00349 //-----------------------------------------------------------------------------
00350 template <typename T>
00351 Matrix4x4<T> & Matrix4x4<T>::operator-= ( Matrix4x4<T> const &M )
00352 {
00353     e[ 0] -= M.e[ 0];  e[ 1] -= M.e[ 1];  e[ 2] -= M.e[ 2];  e[ 3] -= M.e[ 3];
00354     e[ 4] -= M.e[ 4];  e[ 5] -= M.e[ 5];  e[ 6] -= M.e[ 6];  e[ 7] -= M.e[ 7];
00355     e[ 8] -= M.e[ 8];  e[ 9] -= M.e[ 9];  e[10] -= M.e[10];  e[11] -= M.e[11];
00356     e[12] -= M.e[12];  e[13] -= M.e[13];  e[14] -= M.e[14];  e[15] -= M.e[15];
00357     return *this;
00358 }
00359 //-----------------------------------------------------------------------------
00360 template <typename T>
00361 Matrix4x4<T> & Matrix4x4<T>::operator*= ( Matrix4x4<T> const &M )
00362 {
00363     *this = (*this) * M;
00364     return *this;
00365 }
00366 //-----------------------------------------------------------------------------
00367 template <typename T>
00368 Matrix4x4<T> & Matrix4x4<T>::operator*= ( T s )
00369 {
00370     e[ 0] *= s;  e[ 1] *= s;  e[ 2] *= s;  e[ 3] *= s;
00371     e[ 4] *= s;  e[ 5] *= s;  e[ 6] *= s;  e[ 7] *= s;
00372     e[ 8] *= s;  e[ 9] *= s;  e[10] *= s;  e[11] *= s;
00373     e[12] *= s;  e[13] *= s;  e[14] *= s;  e[15] *= s;
00374     return *this;
00375 }
00376 //-----------------------------------------------------------------------------
00377 template <typename T>
00378 Matrix4x4<T> & Matrix4x4<T>::operator/= ( T s )
00379 {
00380     e[ 0] /= s;  e[ 1] /= s;  e[ 2] /= s;  e[ 3] /= s;
00381     e[ 4] /= s;  e[ 5] /= s;  e[ 6] /= s;  e[ 7] /= s;
00382     e[ 8] /= s;  e[ 9] /= s;  e[10] /= s;  e[11] /= s;
00383     e[12] /= s;  e[13] /= s;  e[14] /= s;  e[15] /= s;
00384     return *this;
00385 }
00386 //-----------------------------------------------------------------------------
00387 //=============================================================================
00388 // Binary Overloaded Operators
00389 //-----------------------------------------------------------------------------
00390 template <typename T>
00391 Matrix4x4<T> Matrix4x4<T>::operator+ ( Matrix4x4<T> const &M ) const
00392 {
00393     return Matrix4x4<T>(    e[ 0]+M.e[ 0], e[ 1]+M.e[ 1], e[ 2]+M.e[ 2], e[ 3]+M.e[ 3], 
00394                             e[ 4]+M.e[ 4], e[ 5]+M.e[ 5], e[ 6]+M.e[ 6], e[ 7]+M.e[ 7], 
00395                             e[ 8]+M.e[ 8], e[ 9]+M.e[ 9], e[10]+M.e[10], e[11]+M.e[11], 
00396                             e[12]+M.e[12], e[13]+M.e[13], e[14]+M.e[14], e[15]+M.e[15] );
00397 }
00398 //-----------------------------------------------------------------------------
00399 template <typename T>
00400 Matrix4x4<T> Matrix4x4<T>::operator- ( Matrix4x4<T> const &M ) const
00401 {
00402     return Matrix4x4<T>(    e[ 0]-M.e[ 0], e[ 1]-M.e[ 1], e[ 2]-M.e[ 2], e[ 3]-M.e[ 3], 
00403                             e[ 4]-M.e[ 4], e[ 5]-M.e[ 5], e[ 6]-M.e[ 6], e[ 7]-M.e[ 7], 
00404                             e[ 8]-M.e[ 8], e[ 9]-M.e[ 9], e[10]-M.e[10], e[11]-M.e[11], 
00405                             e[12]-M.e[12], e[13]-M.e[13], e[14]-M.e[14], e[15]-M.e[15] );
00406 }
00407 //-----------------------------------------------------------------------------
00408 template <typename T>
00409 Matrix4x4<T> Matrix4x4<T>::operator* ( Matrix4x4<T> const &M ) const
00410 {
00411     return Matrix4x4<T>(    
00412             e[ 0]*M.e[ 0] + e[ 1]*M.e[ 4] + e[ 2]*M.e[ 8] + e[ 3]*M.e[12],
00413             e[ 0]*M.e[ 1] + e[ 1]*M.e[ 5] + e[ 2]*M.e[ 9] + e[ 3]*M.e[13],
00414             e[ 0]*M.e[ 2] + e[ 1]*M.e[ 6] + e[ 2]*M.e[10] + e[ 3]*M.e[14],
00415             e[ 0]*M.e[ 3] + e[ 1]*M.e[ 7] + e[ 2]*M.e[11] + e[ 3]*M.e[15],
00416 
00417             e[ 4]*M.e[ 0] + e[ 5]*M.e[ 4] + e[ 6]*M.e[ 8] + e[ 7]*M.e[12],
00418             e[ 4]*M.e[ 1] + e[ 5]*M.e[ 5] + e[ 6]*M.e[ 9] + e[ 7]*M.e[13],
00419             e[ 4]*M.e[ 2] + e[ 5]*M.e[ 6] + e[ 6]*M.e[10] + e[ 7]*M.e[14],
00420             e[ 4]*M.e[ 3] + e[ 5]*M.e[ 7] + e[ 6]*M.e[11] + e[ 7]*M.e[15],
00421             
00422             e[ 8]*M.e[ 0] + e[ 9]*M.e[ 4] + e[10]*M.e[ 8] + e[11]*M.e[12],
00423             e[ 8]*M.e[ 1] + e[ 9]*M.e[ 5] + e[10]*M.e[ 9] + e[11]*M.e[13],
00424             e[ 8]*M.e[ 2] + e[ 9]*M.e[ 6] + e[10]*M.e[10] + e[11]*M.e[14],
00425             e[ 8]*M.e[ 3] + e[ 9]*M.e[ 7] + e[10]*M.e[11] + e[11]*M.e[15],
00426 
00427             e[12]*M.e[ 0] + e[13]*M.e[ 4] + e[14]*M.e[ 8] + e[15]*M.e[12],
00428             e[12]*M.e[ 1] + e[13]*M.e[ 5] + e[14]*M.e[ 9] + e[15]*M.e[13],
00429             e[12]*M.e[ 2] + e[13]*M.e[ 6] + e[14]*M.e[10] + e[15]*M.e[14],
00430             e[12]*M.e[ 3] + e[13]*M.e[ 7] + e[14]*M.e[11] + e[15]*M.e[15]
00431     );
00432 }
00433 //-----------------------------------------------------------------------------
00434 template <typename T>
00435 Matrix4x4<T> Matrix4x4<T>::operator* ( T s ) const
00436 {
00437     return Matrix4x4<T>(    e[ 0]*s, e[ 1]*s, e[ 2]*s, e[ 3]*s, 
00438                             e[ 4]*s, e[ 5]*s, e[ 6]*s, e[ 7]*s, 
00439                             e[ 8]*s, e[ 9]*s, e[10]*s, e[11]*s, 
00440                             e[12]*s, e[13]*s, e[14]*s, e[15]*s );
00441 }
00442 //-----------------------------------------------------------------------------
00443 template <typename T>
00444 Matrix4x4<T> Matrix4x4<T>::operator/ ( T s ) const
00445 {
00446     return Matrix4x4<T>(    e[ 0]/s, e[ 1]/s, e[ 2]/s, e[ 3]/s, 
00447                             e[ 4]/s, e[ 5]/s, e[ 6]/s, e[ 7]/s, 
00448                             e[ 8]/s, e[ 9]/s, e[10]/s, e[11]/s, 
00449                             e[12]/s, e[13]/s, e[14]/s, e[15]/s );
00450 }
00451 //-----------------------------------------------------------------------------
00452 template <typename T>
00453 inline void Matrix4x4<T>::MultLeft ( Matrix4x4<T> const &M )
00454 {
00455     *this = M * (*this);
00456 }
00457 //-----------------------------------------------------------------------------
00458 template <typename T>
00459 inline void Matrix4x4<T>::MultRight ( Matrix4x4<T> const &M )
00460 {
00461     *this *= M;
00462 }
00463 //-----------------------------------------------------------------------------
00464 // Matrix * Vector
00465 template <typename T>
00466 Vector4<T> Matrix4x4<T>::operator* ( Vector4<T> const &V ) const
00467 {
00468     return Vector4<T>(
00469             V[0]*e[ 0] + V[1]*e[ 1] + V[2]*e[ 2] + V[3]*e[ 3],
00470             V[0]*e[ 4] + V[1]*e[ 5] + V[2]*e[ 6] + V[3]*e[ 7],
00471             V[0]*e[ 8] + V[1]*e[ 9] + V[2]*e[10] + V[3]*e[11],
00472             V[0]*e[12] + V[1]*e[13] + V[2]*e[14] + V[3]*e[15] );
00473 }
00474 //-----------------------------------------------------------------------------
00475 // Matrix * Vector
00476 template <typename T>
00477 Vector3<T> Matrix4x4<T>::operator* ( Vector3<T> const &V ) const
00478 {
00479     return Vector3<T>(
00480             V[0]*e[ 0] + V[1]*e[ 1] + V[2]*e[ 2] + e[ 3],
00481             V[0]*e[ 4] + V[1]*e[ 5] + V[2]*e[ 6] + e[ 7],
00482             V[0]*e[ 8] + V[1]*e[ 9] + V[2]*e[10] + e[11] );
00483 }
00484 //-----------------------------------------------------------------------------
00485 // Graphics Operation(s)
00486 template <typename T>
00487 Matrix4x4<T> Matrix4x4<T>::CreateRotation ( Vector3<T> const &V, T const angle )
00488 {
00489     assert( V.Length() );   // Vector must not be zero
00490     Matrix4x4<T> mRotation;
00491     Vector3<T> axis = V.GetUnit();
00492     T x = axis[0], y = axis[1], z = axis[2];
00493     T xx = x*x,    yy = y*y,    zz = z*z;
00494     T xy = x*y,    yz = y*z,    zx = x*z;
00495     //----------------------------------------------------------------
00496     mRotation( 0, 0 ) = xx + cos(angle)*(1 - xx);
00497     mRotation( 0, 1 ) = xy*(1 - cos(angle)) - z*sin(angle);
00498     mRotation( 0, 2 ) = zx*(1 - cos(angle)) + y*sin(angle);
00499     //----------------------------------------------------------------
00500     mRotation( 1, 0 ) = xy*(1 - cos(angle)) + z*sin(angle);
00501     mRotation( 1, 1 ) = yy + cos(angle)*(1 - yy);
00502     mRotation( 1, 2 ) = yz*(1 - cos(angle)) - x*sin(angle);
00503     //----------------------------------------------------------------
00504     mRotation( 2, 0 ) = zx*(1 - cos(angle)) - y*sin(angle);
00505     mRotation( 2, 1 ) = yz*(1 - cos(angle)) + x*sin(angle);
00506     mRotation( 2, 2 ) = zz + cos(angle)*(1 - zz);
00507     //----------------------------------------------------------------
00508     return mRotation;
00509 }
00510 //-----------------------------------------------------------------------------
00511 
00512 //=============================================================================
00513 // Static Member Functions for Data Conversions
00514 //-----------------------------------------------------------------------------
00515 // Conversion(s)
00516 template <typename T>
00517 inline Matrix3x3<T> Matrix4x4<T>::GetMatrix3x3 () const
00518 {
00519     return Matrix3x3<T>( e[0], e[1], e[2],
00520                          e[4], e[5], e[6],
00521                          e[8], e[9], e[10] );
00522 }
00523 //-----------------------------------------------------------------------------
00524 
00525 //=============================================================================
00526 // Static Member Functions for Data Conversions
00527 //-----------------------------------------------------------------------------
00528 template <typename T>
00529 const float * Matrix4x4<T>::GetDataFloat () const
00530 {
00531     for ( int i = 0; i < 16; ++i ) {
00532         g_f[i] = static_cast<float>( e[i] );
00533     }
00534     return g_f;
00535 }
00536 //-----------------------------------------------------------------------------
00537 template <typename T>
00538 const double * Matrix4x4<T>::GetDataDouble () const
00539 {
00540     for ( int i = 0; i < 16; ++i ) {
00541         g_d[i] = static_cast<double>( e[i] );
00542     }
00543     return g_d;
00544 }
00545 //-----------------------------------------------------------------------------
00546 template <typename T>
00547 const long double * Matrix4x4<T>::GetDataLongDouble () const
00548 {
00549     for ( int i = 0; i < 16; ++i ) {
00550         g_ld[i] = static_cast<long double>( e[i] );
00551     }
00552     return g_ld;
00553 }
00554 //-----------------------------------------------------------------------------
00555 template <typename T>
00556 const float * Matrix4x4<T>::GetTransposeDataFloat () const
00557 {
00558     g_f[ 0] = static_cast<float>( e[ 0] );
00559     g_f[ 1] = static_cast<float>( e[ 4] );
00560     g_f[ 2] = static_cast<float>( e[ 8] );
00561     g_f[ 3] = static_cast<float>( e[12] );
00562     g_f[ 4] = static_cast<float>( e[ 1] );
00563     g_f[ 5] = static_cast<float>( e[ 5] );
00564     g_f[ 6] = static_cast<float>( e[ 9] );
00565     g_f[ 7] = static_cast<float>( e[13] );
00566     g_f[ 8] = static_cast<float>( e[ 2] );
00567     g_f[ 9] = static_cast<float>( e[ 6] );
00568     g_f[10] = static_cast<float>( e[10] );
00569     g_f[11] = static_cast<float>( e[14] );
00570     g_f[12] = static_cast<float>( e[ 3] );
00571     g_f[13] = static_cast<float>( e[ 7] );
00572     g_f[14] = static_cast<float>( e[11] );
00573     g_f[15] = static_cast<float>( e[15] );
00574     return g_f;
00575 }
00576 //-----------------------------------------------------------------------------
00577 template <typename T>
00578 const double * Matrix4x4<T>::GetTransposeDataDouble () const
00579 {
00580     g_d[ 0] = static_cast<double>( e[ 0] );
00581     g_d[ 1] = static_cast<double>( e[ 4] );
00582     g_d[ 2] = static_cast<double>( e[ 8] );
00583     g_d[ 3] = static_cast<double>( e[12] );
00584     g_d[ 4] = static_cast<double>( e[ 1] );
00585     g_d[ 5] = static_cast<double>( e[ 5] );
00586     g_d[ 6] = static_cast<double>( e[ 9] );
00587     g_d[ 7] = static_cast<double>( e[13] );
00588     g_d[ 8] = static_cast<double>( e[ 2] );
00589     g_d[ 9] = static_cast<double>( e[ 6] );
00590     g_d[10] = static_cast<double>( e[10] );
00591     g_d[11] = static_cast<double>( e[14] );
00592     g_d[12] = static_cast<double>( e[ 3] );
00593     g_d[13] = static_cast<double>( e[ 7] );
00594     g_d[14] = static_cast<double>( e[11] );
00595     g_d[15] = static_cast<double>( e[15] );
00596     return g_d;
00597 }
00598 //-----------------------------------------------------------------------------
00599 template <typename T>
00600 const long double * Matrix4x4<T>::GetTransposeDataLongDouble () const
00601 {
00602     g_ld[ 0] = static_cast<long double>( e[ 0] );
00603     g_ld[ 1] = static_cast<long double>( e[ 4] );
00604     g_ld[ 2] = static_cast<long double>( e[ 8] );
00605     g_ld[ 3] = static_cast<long double>( e[12] );
00606     g_ld[ 4] = static_cast<long double>( e[ 1] );
00607     g_ld[ 5] = static_cast<long double>( e[ 5] );
00608     g_ld[ 6] = static_cast<long double>( e[ 9] );
00609     g_ld[ 7] = static_cast<long double>( e[13] );
00610     g_ld[ 8] = static_cast<long double>( e[ 2] );
00611     g_ld[ 9] = static_cast<long double>( e[ 6] );
00612     g_ld[10] = static_cast<long double>( e[10] );
00613     g_ld[11] = static_cast<long double>( e[14] );
00614     g_ld[12] = static_cast<long double>( e[ 3] );
00615     g_ld[13] = static_cast<long double>( e[ 7] );
00616     g_ld[14] = static_cast<long double>( e[11] );
00617     g_ld[15] = static_cast<long double>( e[15] );
00618     return g_ld;
00619 }
00620 //-----------------------------------------------------------------------------
00621 //=============================================================================
00622 END_NAMESPACE_TAPs
00623 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00624 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines