"L. Spiro Engine"
|
00001 00016 #ifndef __LSM_VECTOR4BASE_H__ 00017 #define __LSM_VECTOR4BASE_H__ 00018 00019 #include "../LSMMathLib.h" 00020 #include "LSMVector3Base.h" 00021 00022 namespace lsm { 00023 00030 template <typename _tType, typename _tVec3> 00031 class CVector4Base { 00032 // All is public. This class has no secrets. 00033 public : 00034 // == Various constructors. 00035 LSE_CALLCTOR CVector4Base() { 00036 } 00037 LSE_CALLCTOR CVector4Base( _tType _fX, _tType _fY, _tType _fZ, _tType _fW ) : 00038 x( _fX ), 00039 y( _fY ), 00040 z( _fZ ), 00041 w( _fW ) { 00042 } 00043 LSE_CALLCTOR CVector4Base( _tType * _pfValues ) : 00044 x( _pfValues[0] ), 00045 y( _pfValues[1] ), 00046 z( _pfValues[2] ), 00047 w( _pfValues[3] ) { 00048 } 00049 00050 // == Operators. 00057 _tType LSE_FCALL operator [] ( LSUINT32 _ui32Index ) const { 00058 return reinterpret_cast<const _tType *>(this)[_ui32Index]; 00059 } 00060 00067 _tType & LSE_FCALL operator [] ( LSUINT32 _ui32Index ) { 00068 return reinterpret_cast<_tType *>(this)[_ui32Index]; 00069 } 00070 00076 CVector4Base<_tType, _tVec3> LSE_FCALL operator - () const { 00077 return CVector4Base<_tType, _tVec3>( -x, -y, -z, -w ); 00078 } 00079 00086 CVector4Base<_tType, _tVec3> LSE_FCALL operator + ( const CVector4Base<_tType, _tVec3> &_v4bOther ) const { 00087 return CVector4Base<_tType, _tVec3>( x + _v4bOther.x, y + _v4bOther.y, z + _v4bOther.z, w + _v4bOther.w ); 00088 } 00089 00096 CVector4Base<_tType, _tVec3> LSE_FCALL operator - ( const CVector4Base<_tType, _tVec3> &_v4bOther ) const { 00097 return CVector4Base<_tType, _tVec3>( x - _v4bOther.x, y - _v4bOther.y, z - _v4bOther.z, w - _v4bOther.w ); 00098 } 00099 00106 CVector4Base<_tType, _tVec3> LSE_FCALL operator * ( _tType _tValue ) const { 00107 return CVector4Base<_tType, _tVec3>( x * _tValue, y * _tValue, z * _tValue, w * _tValue ); 00108 } 00109 00116 CVector4Base<_tType, _tVec3> LSE_FCALL operator / ( _tType _tValue ) const { 00117 return (*this) * (_tType( 1.0 ) / _tValue); 00118 } 00119 00126 CVector4Base<_tType, _tVec3> & LSE_FCALL 00127 operator += ( const CVector4Base<_tType, _tVec3> &_v4bOther ) { 00128 x += _v4bOther.x; 00129 y += _v4bOther.y; 00130 z += _v4bOther.z; 00131 w += _v4bOther.w; 00132 return (*this); 00133 } 00134 00141 CVector4Base<_tType, _tVec3> & LSE_FCALL 00142 operator -= ( const CVector4Base<_tType, _tVec3> &_v4bOther ) { 00143 x -= _v4bOther.x; 00144 y -= _v4bOther.y; 00145 z -= _v4bOther.z; 00146 w -= _v4bOther.w; 00147 return (*this); 00148 } 00149 00156 CVector4Base<_tType, _tVec3> & LSE_FCALL 00157 operator *= ( _tType _tValue ) { 00158 x *= _tValue; 00159 y *= _tValue; 00160 z *= _tValue; 00161 w *= _tValue; 00162 return (*this); 00163 } 00164 00171 CVector4Base<_tType, _tVec3> & LSE_FCALL 00172 operator /= ( _tType _tValue ) { 00173 return (*this) *= (_tType( 1.0 ) / _tValue); 00174 } 00175 00183 _tType LSE_FCALL operator * ( const CVector4Base<_tType, _tVec3> &_v4bOther ) const { 00184 return x * _v4bOther.x + y * _v4bOther.y + z * _v4bOther.z + w * _v4bOther.w; 00185 } 00186 00193 LSBOOL LSE_FCALL operator == ( const CVector4Base<_tType, _tVec3> &_v4bOther ) const { 00194 return x == _v4bOther.x && y == _v4bOther.y && z == _v4bOther.z && w == _v4bOther.w; 00195 } 00196 00203 LSBOOL LSE_FCALL operator != ( const CVector4Base<_tType, _tVec3> &_v4bOther ) const { 00204 return x != _v4bOther.x || y != _v4bOther.y || z != _v4bOther.z || w != _v4bOther.w; 00205 } 00206 00207 00208 // == Functions. 00217 LSVOID LSE_FCALL Set( _tType _fX, _tType _fY, _tType _fZ, _tType _fW ) { 00218 x = _fX; 00219 y = _fY; 00220 z = _fZ; 00221 w = _fW; 00222 } 00223 00227 LSVOID LSE_FCALL Clear() { 00228 x = y = z = w = _tType( 0 ); 00229 } 00230 00234 LSVOID LSE_FCALL Invert() { 00235 x = -x; 00236 y = -y; 00237 z = -z; 00238 w = -w; 00239 } 00240 00246 _tType LSE_FCALL Len() const { 00247 register LSREAL fSquared = x * x + y * y + z * z + w * w; 00248 return fSquared * CMathLib::InvSqrt( fSquared ); 00249 } 00250 00256 _tType LSE_FCALL LenSq() const { 00257 return x * x + y * y + z * z + w * w; 00258 } 00259 00265 LSVOID LSE_FCALL Normalize() { 00266 LSREAL fInvLen = CMathLib::InvSqrt( x * x + y * y + z * z + w * w ); 00267 x *= fInvLen; 00268 y *= fInvLen; 00269 z *= fInvLen; 00270 w *= fInvLen; 00271 } 00272 00280 _tType LSE_FCALL Dot( const CVector4Base<_tType, _tVec3> &_v4bOther ) const { 00281 return x * _v4bOther.x + y * _v4bOther.y + z * _v4bOther.z + w * _v4bOther.w; 00282 } 00283 00289 _tVec3 LSE_FCALL Vec3() const { 00290 return _tVec3( x, y, z ); 00291 } 00292 00301 static _tType LSE_FCALL DotProduct( const CVector4Base<_tType, _tVec3> &_v4bLeft, const CVector4Base<_tType, _tVec3> &_v4bRight ) { 00302 return _v4bLeft.x * _v4bRight.x + _v4bLeft.y * _v4bRight.y + _v4bLeft.z * _v4bRight.z + _v4bLeft.w * _v4bRight.w; 00303 } 00304 00313 static CVector4Base<_tType, _tVec3> LSE_FCALL Lerp( const CVector4Base<_tType, _tVec3> &_v4bLeft, const CVector4Base<_tType, _tVec3> &_v4bRight, _tType _fFrac ) { 00314 return ((_v4bRight - _v4bLeft) * _fFrac) + _v4bLeft; 00315 } 00316 00327 static CVector4Base<_tType, _tVec3> LSE_FCALL Hermite( const CVector4Base<_tType, _tVec3> &_v4bLeft, const CVector4Base<_tType, _tVec3> &_v4bT0, 00328 const CVector4Base<_tType, _tVec3> &_v4bRight, const CVector4Base<_tType, _tVec3> &_v4bT1, 00329 _tType _fFrac ) { 00330 _tType fS2 = _fFrac * _fFrac; 00331 _tType fS3 = fS2 * _fFrac; 00332 _tType f2s2 = static_cast<_tType>(2.0) * fS2; 00333 _tType f2s3 = static_cast<_tType>(2.0) * fS3; 00334 _tType f3s2 = static_cast<_tType>(3.0) * fS2; 00335 00336 return (_v4bLeft * (f2s3 - f3s2 + LSM_ONE)) + 00337 (_v4bRight * (-f2s3 + f3s2)) + 00338 (_v4bT0 * (fS3 - f2s2 + _fFrac)) + 00339 (_v4bT1 * (fS3 - fS2)); 00340 } 00341 00352 static CVector4Base<_tType, _tVec3> LSE_FCALL CatmullRom( const CVector4Base<_tType, _tVec3> &_v4bV0, const CVector4Base<_tType, _tVec3> &_v4bV1, 00353 const CVector4Base<_tType, _tVec3> &_v4bV2, const CVector4Base<_tType, _tVec3> &_v4bV3, 00354 _tType _fFrac ) { 00355 _tType fS2 = _fFrac * _fFrac; 00356 return ((_v4bV1 * static_cast<_tType>(2.0)) + 00357 (-_v4bV0 + _v4bV2) * _fFrac + 00358 (_v4bV0 * static_cast<_tType>(2.0) - _v4bV1 * static_cast<_tType>(5.0) + _v4bV2 * static_cast<_tType>(4.0) - _v4bV3) * fS2 + 00359 (-_v4bV0 + _v4bV1 * static_cast<_tType>(3.0) - _v4bV2 * static_cast<_tType>(3.0) + _v4bV3) * fS2 * _fFrac) * LSM_HALF; 00360 } 00361 00362 00363 // == Members. 00364 #pragma pack( push, 1 ) 00365 00368 _tType x; 00369 00373 _tType y; 00374 00378 _tType z; 00379 00383 _tType w; 00384 #pragma pack( pop ) 00385 }; 00386 00387 } // namespace lsm 00388 00389 #endif // __LSM_VECTOR4BASE_H__ 00390