![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsColMatrix2x2.cpp 00003 00004 ColMatrix2x2 class is a class for 2-by-2 matrices. 00005 00006 SUKITTI PUNAK (09/12/2004) 00007 UPDATE (07/30/2005) 00008 ******************************************************************************/ 00009 #include "TAPsColMatrix2x2.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 ColMatrix2x2<T>::ColMatrix2x2 () 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 ColMatrix2x2<T>::ColMatrix2x2 ( ColMatrix2x2<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 ColMatrix2x2<T>::ColMatrix2x2 ( T t ) 00034 { 00035 e[0] = e[1] = e[2] = e[3] = t; 00036 } 00037 //----------------------------------------------------------------------------- 00038 template <typename T> 00039 ColMatrix2x2<T>::ColMatrix2x2 ( 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 ColMatrix2x2<T>::ColMatrix2x2 ( 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 ColMatrix2x2<T>::~ColMatrix2x2 () 00052 {} 00053 //----------------------------------------------------------------------------- 00054 //============================================================================= 00055 // Member Access 00056 //----------------------------------------------------------------------------- 00057 template <typename T> 00058 inline T & ColMatrix2x2<T>::operator[] ( int i ) 00059 { return e[i]; } 00060 //----------------------------------------------------------------------------- 00061 template <typename T> 00062 inline T const & ColMatrix2x2<T>::operator[] ( int i ) const 00063 { return e[i]; } 00064 //----------------------------------------------------------------------------- 00065 template <typename T> 00066 inline T & ColMatrix2x2<T>::operator() ( int r, int c ) 00067 { return e[r + c*2]; } 00068 //----------------------------------------------------------------------------- 00069 template <typename T> 00070 inline T const & ColMatrix2x2<T>::operator() ( int r, int c ) const 00071 { return e[r + c*2]; } 00072 //----------------------------------------------------------------------------- 00073 //============================================================================= 00074 // Convert to one dimension array 00075 //----------------------------------------------------------------------------- 00076 template <typename T> 00077 ColMatrix2x2<T>::operator const T * () const 00078 { return e; } 00079 //----------------------------------------------------------------------------- 00080 template <typename T> 00081 ColMatrix2x2<T>::operator T * () 00082 { return e; } 00083 //----------------------------------------------------------------------------- 00084 //============================================================================= 00085 // Useful Functions 00086 //----------------------------------------------------------------------------- 00087 template <typename T> 00088 void ColMatrix2x2<T>::SetAllElements ( T t ) 00089 { 00090 e[0] = e[1] = e[2] = e[3] = t; 00091 } 00092 //----------------------------------------------------------------------------- 00093 template <typename T> 00094 void ColMatrix2x2<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 void ColMatrix2x2<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 void ColMatrix2x2<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 void ColMatrix2x2<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 void ColMatrix2x2<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 void ColMatrix2x2<T>::MakeZero () 00129 { 00130 e[0] = e[1] = e[2] = e[3] = Math<T>::ZERO; 00131 } 00132 //----------------------------------------------------------------------------- 00133 //============================================================================= 00134 // Matrix Operations 00135 //----------------------------------------------------------------------------- 00136 template <typename T> 00137 ColMatrix2x2<T> & ColMatrix2x2<T>::Transposed () 00138 { 00139 T temp; 00140 temp = e[1]; e[1] = e[2]; e[2] = temp; 00141 return *this; 00142 } 00143 //----------------------------------------------------------------------------- 00144 template <typename T> 00145 ColMatrix2x2<T> ColMatrix2x2<T>::GetTranspose() const 00146 { 00147 return ColMatrix2x2<T> ( e[0], e[2], e[1], e[3] ); 00148 } 00149 //----------------------------------------------------------------------------- 00150 // Inverse the matrix 00151 template <typename T> 00152 ColMatrix2x2<T> & ColMatrix2x2<T>::Inversed () 00153 { 00154 *this = (*this).GetInverse(); 00155 return *this; 00156 } 00157 //----------------------------------------------------------------------------- 00158 // Get the matrix inverse 00159 template <typename T> 00160 ColMatrix2x2<T> ColMatrix2x2<T>::GetInverse () const 00161 { 00162 T det = GetDeterminant(); 00163 00164 if ( -Math<T>::EPSILON < det && det < Math<T>::EPSILON ) 00165 return ColMatrix2x2( Math<T>::INFINITY ); 00166 00167 return ColMatrix2x2<T>( e[3] / det, 00168 -e[1] / det, 00169 -e[2] / det, 00170 e[0] / det ); 00171 } 00172 //----------------------------------------------------------------------------- 00173 template <typename T> 00174 T ColMatrix2x2<T>::GetDeterminant () const 00175 { 00176 return e[0]*e[3] - e[1]*e[2]; 00177 } 00178 //----------------------------------------------------------------------------- 00179 //============================================================================= 00180 // Assignment Overloaded Operators 00181 //----------------------------------------------------------------------------- 00182 template <typename T> 00183 ColMatrix2x2<T> & ColMatrix2x2<T>::operator= ( ColMatrix2x2<T> const &M ) 00184 { 00185 if ( this != &M ) 00186 { 00187 e[0] = M.e[0]; e[1] = M.e[1]; e[2] = M.e[2]; e[3] = M.e[3]; 00188 } 00189 return *this; 00190 } 00191 //----------------------------------------------------------------------------- 00192 //============================================================================= 00193 // Unary Overloaded Operators 00194 //----------------------------------------------------------------------------- 00195 template <typename T> 00196 ColMatrix2x2<T> ColMatrix2x2<T>::operator- () 00197 { 00198 return ColMatrix2x2<T>( -e[0], -e[1], -e[2], -e[3] ); 00199 } 00200 //----------------------------------------------------------------------------- 00201 //============================================================================= 00202 // Assign Overloaded Operators 00203 //----------------------------------------------------------------------------- 00204 template <typename T> 00205 ColMatrix2x2<T> & ColMatrix2x2<T>::operator+= ( ColMatrix2x2<T> const &M ) 00206 { 00207 e[0] += M.e[0]; e[1] += M.e[1]; e[2] += M.e[2]; e[3] += M.e[3]; 00208 return *this; 00209 } 00210 //----------------------------------------------------------------------------- 00211 template <typename T> 00212 ColMatrix2x2<T> & ColMatrix2x2<T>::operator-= ( ColMatrix2x2<T> const &M ) 00213 { 00214 e[0] -= M.e[0]; e[1] -= M.e[1]; e[2] -= M.e[2]; e[3] -= M.e[3]; 00215 return *this; 00216 } 00217 //----------------------------------------------------------------------------- 00218 template <typename T> 00219 ColMatrix2x2<T> & ColMatrix2x2<T>::operator*= ( ColMatrix2x2<T> const &M ) 00220 { 00221 *this = (*this) * M; 00222 return *this; 00223 } 00224 //----------------------------------------------------------------------------- 00225 template <typename T> 00226 ColMatrix2x2<T> & ColMatrix2x2<T>::operator*= ( T s ) 00227 { 00228 e[0] *= s; e[1] *= s; e[2] *= s; e[3] *= s; 00229 return *this; 00230 } 00231 //----------------------------------------------------------------------------- 00232 template <typename T> 00233 ColMatrix2x2<T> & ColMatrix2x2<T>::operator/= ( T s ) 00234 { 00235 e[0] /= s; e[1] /= s; e[2] /= s; e[3] /= s; 00236 return *this; 00237 } 00238 //----------------------------------------------------------------------------- 00239 //============================================================================= 00240 // Binary Overloade Operators 00241 //----------------------------------------------------------------------------- 00242 template <typename T> 00243 ColMatrix2x2<T> ColMatrix2x2<T>::operator+ ( ColMatrix2x2<T> const &M ) const 00244 { 00245 return ColMatrix2x2<T>( e[0]+M.e[0], e[1]+M.e[1], e[2]+M.e[2], e[3]+M.e[3] ); 00246 } 00247 //----------------------------------------------------------------------------- 00248 template <typename T> 00249 ColMatrix2x2<T> ColMatrix2x2<T>::operator- ( ColMatrix2x2<T> const &M ) const 00250 { 00251 return ColMatrix2x2<T>( e[0]-M.e[0], e[1]-M.e[1], e[2]-M.e[2], e[3]-M.e[3] ); 00252 } 00253 //----------------------------------------------------------------------------- 00254 template <typename T> 00255 ColMatrix2x2<T> ColMatrix2x2<T>::operator* ( ColMatrix2x2<T> const &M ) const 00256 { 00257 return ColMatrix2x2<T>( M.e[0]*e[0] + M.e[1]*e[2], 00258 M.e[0]*e[1] + M.e[1]*e[3], 00259 M.e[2]*e[0] + M.e[3]*e[2], 00260 M.e[2]*e[1] + M.e[3]*e[3] ); 00261 } 00262 //----------------------------------------------------------------------------- 00263 template <typename T> 00264 ColMatrix2x2<T> ColMatrix2x2<T>::operator* ( T s ) const 00265 { 00266 return ColMatrix2x2<T>( e[0]*s, e[1]*s, e[2]*s, e[3]*s ); 00267 } 00268 //----------------------------------------------------------------------------- 00269 template <typename T> 00270 ColMatrix2x2<T> ColMatrix2x2<T>::operator/ ( T s ) const 00271 { 00272 return ColMatrix2x2<T>( e[0]/s, e[1]/s, e[2]/s, e[3]/s ); 00273 } 00274 //----------------------------------------------------------------------------- 00275 template <typename T> 00276 inline void ColMatrix2x2<T>::MultLeft ( ColMatrix2x2<T> const &M ) 00277 { 00278 *this = M * (*this); 00279 } 00280 //----------------------------------------------------------------------------- 00281 template <typename T> 00282 inline void ColMatrix2x2<T>::MultRight ( ColMatrix2x2<T> const &M ) 00283 { 00284 *this *= M; 00285 } 00286 //----------------------------------------------------------------------------- 00287 //============================================================================= 00288 END_NAMESPACE_TAPs 00289 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00290 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8