"L. Spiro Engine"
|
00001 00016 #ifndef __LSM_VECTOR2BASE_H__ 00017 #define __LSM_VECTOR2BASE_H__ 00018 00019 #include "../LSMMathLib.h" 00020 00021 namespace lsm { 00022 00029 template <typename _tType, typename _tInher> 00030 class CVector2Base { 00031 // All is public. This class has no secrets. 00032 public : 00033 // == Various constructors. 00034 LSE_CALLCTOR CVector2Base() { 00035 } 00036 LSE_CALLCTOR CVector2Base( const _tInher &_v2bOther ) : 00037 x( _v2bOther.x ), 00038 y( _v2bOther.y ) { 00039 } 00040 LSE_CALLCTOR CVector2Base( _tType _fX, _tType _fY ) : 00041 x( _fX ), 00042 y( _fY ) { 00043 } 00044 LSE_CALLCTOR CVector2Base( _tType * _pfValues ) : 00045 x( _pfValues[0] ), 00046 y( _pfValues[1] ) { 00047 } 00048 00049 // == Operators. 00056 _tType LSE_FCALL operator [] ( LSUINT32 _ui32Index ) const { 00057 return reinterpret_cast<const _tType *>(this)[_ui32Index]; 00058 } 00059 00066 _tType & LSE_FCALL operator [] ( LSUINT32 _ui32Index ) { 00067 return reinterpret_cast<_tType *>(this)[_ui32Index]; 00068 } 00069 00076 _tInher & LSE_FCALL operator = ( const _tInher &_v2bOther ) { 00077 x = _v2bOther.x; 00078 y = _v2bOther.y; 00079 return (*static_cast<_tInher *>(this)); 00080 } 00081 00087 _tInher LSE_FCALL operator - () const { 00088 return CVector2Base<_tType, _tInher>( -x, -y ); 00089 } 00090 00097 _tInher LSE_FCALL operator + ( const _tInher &_v2bOther ) const { 00098 return _tInher( x + _v2bOther.x, y + _v2bOther.y ); 00099 } 00100 00107 _tInher LSE_FCALL operator - ( const _tInher &_v2bOther ) const { 00108 return _tInher( x - _v2bOther.x, y - _v2bOther.y ); 00109 } 00110 00117 _tInher LSE_FCALL operator * ( _tType _tValue ) const { 00118 return _tInher( x * _tValue, y * _tValue ); 00119 } 00120 00127 _tInher LSE_FCALL operator / ( _tType _tValue ) const { 00128 return _tInher( x / _tValue, y / _tValue ); 00129 } 00130 00137 _tInher & LSE_FCALL operator += ( const _tInher &_v2bOther ) { 00138 x += _v2bOther.x; 00139 y += _v2bOther.y; 00140 return (*static_cast<_tInher *>(this)); 00141 } 00142 00149 _tInher & LSE_FCALL operator -= ( const _tInher &_v2bOther ) { 00150 x -= _v2bOther.x; 00151 y -= _v2bOther.y; 00152 return (*static_cast<_tInher *>(this)); 00153 } 00154 00161 _tInher & LSE_FCALL operator *= ( _tType _tValue ) { 00162 x *= _tValue; 00163 y *= _tValue; 00164 return (*static_cast<_tInher *>(this)); 00165 } 00166 00173 _tInher & LSE_FCALL operator /= ( _tType _tValue ) { 00174 x /= _tValue; 00175 y /= _tValue; 00176 return (*static_cast<_tInher *>(this)); 00177 } 00178 00186 _tType LSE_FCALL operator * ( const _tInher &_v2bOther ) const { 00187 return x * _v2bOther.x + y * _v2bOther.y; 00188 } 00189 00196 LSBOOL LSE_FCALL operator == ( const _tInher &_v2bOther ) const { 00197 return x == _v2bOther.x && y == _v2bOther.y; 00198 } 00199 00206 LSBOOL LSE_FCALL operator != ( const _tInher &_v2bOther ) const { 00207 return x != _v2bOther.x || y != _v2bOther.y; 00208 } 00209 00210 00211 // == Functions. 00218 LSVOID LSE_FCALL Set( _tType _fX, _tType _fY ) { 00219 x = _fX; 00220 y = _fY; 00221 } 00222 00226 LSVOID LSE_FCALL Clear() { 00227 x = y = _tType( 0 ); 00228 } 00229 00233 LSVOID LSE_FCALL Invert() { 00234 x = -x; 00235 y = -y; 00236 } 00237 00243 _tType LSE_FCALL Len() const { 00244 register LSREAL fSquared = x * x + y * y; 00245 return fSquared * CMathLib::InvSqrt( fSquared ); 00246 } 00247 00253 _tType LSE_FCALL LenSq() const { 00254 return x * x + y * y; 00255 } 00256 00262 LSVOID LSE_FCALL Normalize() { 00263 LSREAL fInvLen = CMathLib::InvSqrt( x * x + y * y ); 00264 x *= fInvLen; 00265 y *= fInvLen; 00266 } 00267 00275 _tType LSE_FCALL Dot( const CVector2Base<_tType, _tInher> &_v2bOther ) const { 00276 return x * _v2bOther.x + y * _v2bOther.y; 00277 } 00278 00287 static _tType LSE_FCALL DotProduct( const CVector2Base<_tType, _tInher> &_v2bLeft, const CVector2Base<_tType, _tInher> &_v2bRight ) { 00288 return _v2bLeft.x * _v2bRight.x + _v2bLeft.y * _v2bRight.y; 00289 } 00290 00299 static CVector2Base<_tType, _tInher> LSE_FCALL 00300 Lerp( const CVector2Base<_tType, _tInher> &_v2bLeft, const CVector2Base<_tType, _tInher> &_v2bRight, _tType _fFrac ) { 00301 return ((_v2bRight - _v2bLeft) * _fFrac) + _v2bLeft; 00302 } 00303 00314 static CVector2Base<_tType, _tInher> LSE_FCALL 00315 Hermite( const CVector2Base<_tType, _tInher> &_v2bLeft, const CVector2Base<_tType, _tInher> &_v2bT0, 00316 const CVector2Base<_tType, _tInher> &_v2bRight, const CVector2Base<_tType, _tInher> &_v2bT1, 00317 _tType _fFrac ) { 00318 _tType fS2 = _fFrac * _fFrac; 00319 _tType fS3 = fS2 * _fFrac; 00320 _tType f2s2 = static_cast<_tType>(2.0) * fS2; 00321 _tType f2s3 = static_cast<_tType>(2.0) * fS3; 00322 _tType f3s2 = static_cast<_tType>(3.0) * fS2; 00323 00324 return (_v2bLeft * (f2s3 - f3s2 + LSM_ONE)) + 00325 (_v2bRight * (-f2s3 + f3s2)) + 00326 (_v2bT0 * (fS3 - f2s2 + _fFrac)) + 00327 (_v2bT1 * (fS3 - fS2)); 00328 } 00329 00340 static CVector2Base<_tType, _tInher> LSE_FCALL 00341 CatmullRom( const CVector2Base<_tType, _tInher> &_v2bV0, const CVector2Base<_tType, _tInher> &_v2bV1, 00342 const CVector2Base<_tType, _tInher> &_v2bV2, const CVector2Base<_tType, _tInher> &_v2bV3, 00343 _tType _fFrac ) { 00344 _tType fS2 = _fFrac * _fFrac; 00345 return ((_v2bV1 * static_cast<_tType>(2.0)) + 00346 (-_v2bV0 + _v2bV2) * _fFrac + 00347 (_v2bV0 * static_cast<_tType>(2.0) - _v2bV1 * static_cast<_tType>(5.0) + _v2bV2 * static_cast<_tType>(4.0) - _v2bV3) * fS2 + 00348 (-_v2bV0 + _v2bV1 * static_cast<_tType>(3.0) - _v2bV2 * static_cast<_tType>(3.0) + _v2bV3) * fS2 * _fFrac) * LSM_HALF; 00349 } 00350 00351 00352 // == Members. 00353 #pragma pack( push, 1 ) 00354 00357 _tType x; 00358 00362 _tType y; 00363 #pragma pack( pop ) 00364 }; 00365 00366 } // namespace lsm 00367 00368 #endif // __LSM_VECTOR2BASE_H__ 00369