"L. Spiro Engine"

F:/My Projects/LSEngine/Modules/LSMathLib/Src/Matrix/LSMMatrix2x2Base.h

00001 
00016 #ifndef __LSM_MATRIX2X2BASE_H__
00017 #define __LSM_MATRIX2X2BASE_H__
00018 
00019 #include "../LSMMathLib.h"
00020 #include "../Vector/LSMVector2Base.h"
00021 
00022 namespace lsm {
00023 
00030         template <typename _tType, typename _tVector2Type>
00031         class CMatrix2x2Base {
00032                 // All is public.  This class has no secrets.
00033         public :
00034                 // == Various constructors.
00035                 LSE_CALLCTOR                                                                    CMatrix2x2Base() {
00036                 }
00037                 LSE_CALLCTOR                                                                    CMatrix2x2Base( const CMatrix2x2Base<_tType, _tVector2Type> &_m22bOther ) :
00038                         _11( _m22bOther._11 ), _12( _m22bOther._12 ),
00039                         _21( _m22bOther._21 ), _22( _m22bOther._22 ) {
00040                 }
00041                 LSE_CALLCTOR                                                                    CMatrix2x2Base( const CMatrix2x2Base<_tType, _tVector2Type> &_m22bOther, LSBOOL _bTranspose ) {
00042                         if ( !_bTranspose ) {
00043                                 _11 =_m22bOther._11; _12 =_m22bOther._12;
00044                                 _21 =_m22bOther._21; _22 =_m22bOther._22;
00045                         }
00046                         else {
00047                                 _11 =_m22bOther._11; _12 =_m22bOther._21;
00048                                 _21 =_m22bOther._12; _22 =_m22bOther._22;
00049                         }
00050                 }
00051                 LSE_CALLCTOR                                                                    CMatrix2x2Base( const _tType * _ptArray ) {
00052                         _11 = _ptArray[0];
00053                         _12 = _ptArray[1];
00054 
00055                         _21 = _ptArray[2];
00056                         _22 = _ptArray[3];
00057                 }
00058 
00059 
00060                 // == Operators.
00069                 _tType & LSE_FCALL                                                              operator () ( LSUINT32 _ui32Row, LSUINT32 _ui32Col ) {
00070                         return reinterpret_cast<_tType *>(this)[(_ui32Row<<1UL)+_ui32Col];
00071                 }
00072 
00080                 _tType LSE_FCALL                                                                operator () ( LSUINT32 _ui32Row, LSUINT32 _ui32Col ) const {
00081                         return reinterpret_cast<const _tType *>(this)[(_ui32Row<<1UL)+_ui32Col];
00082                 }
00083 
00090                 CMatrix2x2Base<_tType, _tVector2Type> LSE_FCALL operator * ( const CMatrix2x2Base<_tType, _tVector2Type> &_m22bOther ) const {
00091                         CMatrix2x2Base<_tType, _tVector2Type> m22bOut;
00092                         for ( LSINT32 I = 2; --I >= 0; ) {
00093                                 const _tType * pfBufferIn = &reinterpret_cast<const _tType *>(this)[I<<1UL];
00094                                 _tType * pfBufferOut = &reinterpret_cast<_tType *>(&m22bOut)[I<<1UL];
00095                                 for ( LSINT32 J = 2; --J >= 0; ) {
00096                                         _tType fSum = LSM_ZERO;
00097                                         for ( LSINT32 K = 2; --K >= 0; ) {
00098                                                 fSum += pfBufferIn[K] * _m22bOther( K, J );
00099                                         }
00100                                         pfBufferOut[J] = fSum;
00101                                 }
00102                         }
00103 
00104                         return m22bOut;
00105                 }
00106 
00113                 _tVector2Type LSE_FCALL                                                 operator * ( const _tVector2Type &_v2bOther ) const {
00114                         _tVector2Type v3bRet;
00115                         v3bRet.x = (_11 * _v2bOther.x + _21 * _v2bOther.y);
00116                         v3bRet.y = (_12 * _v2bOther.x + _22 * _v2bOther.y);
00117                         return v3bRet;
00118                 }
00119 
00126                 CMatrix2x2Base<_tType, _tVector2Type> LSE_FCALL operator * ( _tType _tValue ) const {
00127                         CMatrix2x2Base<_tType, _tVector2Type> m22bRet;
00128                         m22bRet._11 = _11 * _tValue;
00129                         m22bRet._12 = _12 * _tValue;
00130                         m22bRet._21 = _21 * _tValue;
00131                         m22bRet._22 = _22 * _tValue;
00132                         return m22bRet;
00133                 }
00134 
00141                 CMatrix2x2Base<_tType, _tVector2Type> & LSE_FCALL
00142                                                                                                                 operator *= ( _tType _tValue ) {
00143                         _11 = _11 * _tValue;
00144                         _12 = _12 * _tValue;
00145                         _21 = _21 * _tValue;
00146                         _22 = _22 * _tValue;
00147                         return (*this);
00148                 }
00149 
00156                 CMatrix2x2Base<_tType, _tVector2Type> & LSE_FCALL
00157                                                                                                                 operator += ( const CMatrix2x2Base<_tType, _tVector2Type> &_m22bOther ) {
00158                         _11 += _m22bOther._11;
00159                         _12 += _m22bOther._12;
00160                         _21 += _m22bOther._21;
00161                         _22 += _m22bOther._22;
00162                         return (*this);
00163                 }
00164 
00165 
00166                 // == Functions.
00172                 CMatrix2x2Base<_tType, _tVector2Type> & LSE_FCALL
00173                                                                                                                 Identity() {
00174                         _11 = _22 = _tType( 1.0 );
00175                         _12 = _21 = _tType( 0.0 );
00176                         return (*this);
00177                 }
00178 
00184                 CMatrix2x2Base<_tType, _tVector2Type> & LSE_FCALL
00185                                                                                                                 Transpose() {
00186                         CMathLib::Swap( _12, _21 );
00187                         return (*this);
00188                 }
00189 
00196                 LSVOID LSE_FCALL                                                                SetRow( LSUINT32 _ui32Row, const _tVector2Type &_v3bVec ) {
00197                         LSE_UNALIGNED _tType * pfData = &_11;
00198                         pfData += (_ui32Row << 1UL);
00199                         
00200                         pfData[0] = _v3bVec.x;
00201                         pfData[1] = _v3bVec.y;
00202                 }
00203 
00210                 CMatrix2x2Base<_tType, _tVector2Type> & LSE_FCALL
00211                                                                                                                 MatrixRotation( _tType _tAngle ) {
00212                         LSREAL fS;
00213                         LSREAL fC;
00214                         CMathLib::SinCos( static_cast<LSREAL>(_tAngle), fS, fC );
00215                         _11 = fC;
00216                         _12 = fS;
00217                         _21 = -fS;
00218                         _22 = fC;
00219                         return (*this);
00220                 }
00221 
00231                 static CMatrix2x2Base<_tType, _tVector2Type> * LSE_FCALL
00232                                                                                                                 MatrixMultiply( CMatrix2x2Base<_tType, _tVector2Type> &_m22bOut, const CMatrix2x2Base<_tType, _tVector2Type> &_m22bM1, const CMatrix2x2Base<_tType, _tVector2Type> &_m22bM2 ) {
00233                         const CMatrix2x2Base<_tType, _tVector2Type> * LSE_RESTRICT pmSrc1 = &_m22bM1;
00234                         const CMatrix2x2Base<_tType, _tVector2Type> * LSE_RESTRICT pmSrc2 = &_m22bM2;
00235 
00236                         for ( LSINT32 I = 2; --I >= 0; ) {
00237                                 const _tType * pfBufferIn = &reinterpret_cast<const _tType *>(pmSrc1)[I<<1UL];
00238                                 _tType * pfBufferOut = &reinterpret_cast<_tType *>(&_m22bOut)[I<<1UL];
00239                                 for ( LSINT32 J = 2; --J >= 0; ) {
00240                                         _tType tSum = LSM_ZERO;
00241                                         for ( LSINT32 K = 2; --K >= 0; ) {
00242                                                 tSum += pfBufferIn[K] * (*pmSrc2)( K, J );
00243                                         }
00244                                         pfBufferOut[J] = tSum;
00245                                 }
00246                         }
00247 
00248                         return &_m22bOut;
00249                 }
00250 
00258                 static LSVOID LSE_FCALL                                                 MultiplyVec2ByMat2x2( const CMatrix2x2Base<_tType, _tVector2Type> &_m22bMat, const _tVector2Type &_v2bIn, _tVector2Type &_v2bOut ) {
00259                         const _tType * /*LSE_RESTRICT*/ pfIn = reinterpret_cast<const _tType *>(&_v2bIn);
00260                         _v2bOut[0] = _m22bMat._11 * pfIn[0] + _m22bMat._21 * pfIn[1];
00261                         _v2bOut[1] = _m22bMat._12 * pfIn[0] + _m22bMat._22 * pfIn[1];
00262                 }
00263 
00271                 LSVOID LSE_FCALL                                                                MultiplyVec2ByMat2x2Transpose( const CMatrix2x2Base<_tType, _tVector2Type> &_m22bMat, const _tVector2Type &_v2bIn, _tVector2Type &_v2bOut ) {
00272                         const _tType * /*LSE_RESTRICT*/ pfIn = reinterpret_cast<const _tType *>(&_v2bIn);
00273                         _v2bOut[0] = _m22bMat._11 * pfIn[0] + _m22bMat._12 * pfIn[1];
00274                         _v2bOut[1] = _m22bMat._21 * pfIn[0] + _m22bMat._22 * pfIn[1];
00275                 }
00276 
00277                 // == Members.
00278 #pragma pack( push, 1 )
00279                 _tType                                                                                  _11, _12;               // Vector 0.
00280                 _tType                                                                                  _21, _22;               // Vector 1.
00281 #pragma pack( pop )
00282         };
00283 
00284 }       // namespace lsm
00285 
00286 #endif  // __LSM_MATRIX2X2BASE_H__
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator