keybode

Vector class

Feb 23rd, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.07 KB | None | 0 0
  1. #ifndef VECTOR_H
  2. #define VECTOR_H
  3.  
  4. #include "Include.h"
  5.  
  6. typedef float vec_t;
  7.  
  8. inline vec_t BitsToFloat( unsigned long i )
  9. {
  10.     return *reinterpret_cast<vec_t*>(&i);
  11. }
  12.  
  13. #define FLOAT32_NAN_BITS     (unsigned long)0x7FC00000
  14. #define FLOAT32_NAN          BitsToFloat( FLOAT32_NAN_BITS )
  15.  
  16. #define VEC_T_NAN FLOAT32_NAN
  17.  
  18. class Vector
  19. {
  20. public:
  21.     vec_t x, y, z;
  22.  
  23.     Vector( void );
  24.     Vector( vec_t X, vec_t Y, vec_t Z );
  25.     explicit Vector( vec_t XYZ );
  26.  
  27.     void Init( vec_t ix = 0.0f, vec_t iy = 0.0f, vec_t iz = 0.0f );
  28.  
  29.     bool IsValid( ) const;
  30.     void Invalidate( );
  31.  
  32.     vec_t   operator[]( int i ) const;
  33.     vec_t&  operator[]( int i );
  34.  
  35.     vec_t* Base( );
  36.     vec_t const* Base( ) const;
  37.  
  38.     inline void Zero( );
  39.  
  40.     bool operator==( const Vector& v ) const;
  41.     bool operator!=( const Vector& v ) const;
  42.  
  43.     FORCEINLINE Vector& operator+=( const Vector& v );
  44.     FORCEINLINE Vector& operator-=( const Vector& v );
  45.     FORCEINLINE Vector& operator*=( const Vector& v );
  46.     FORCEINLINE Vector& operator*=( float s );
  47.     FORCEINLINE Vector& operator/=( const Vector& v );
  48.     FORCEINLINE Vector& operator/=( float s );
  49.     FORCEINLINE Vector& operator+=( float fl );
  50.     FORCEINLINE Vector& operator-=( float fl );
  51.  
  52.     void Negate( );
  53.  
  54.     inline vec_t Length( ) const;
  55.  
  56.     FORCEINLINE vec_t LengthSqr( void ) const
  57.     {
  58.         return ( x * x + y * y + z * z );
  59.     }
  60.  
  61.     bool IsZero( float tolerance = 0.01f ) const
  62.     {
  63.         return ( x > -tolerance && x < tolerance &&
  64.                  y > -tolerance && y < tolerance &&
  65.                  z > -tolerance && z < tolerance );
  66.     }
  67.  
  68.     vec_t NormalizeInPlace( );
  69.     Vector Normalized( ) const;
  70.  
  71.     bool IsLengthGreaterThan( float val ) const;
  72.     bool IsLengthLessThan( float val ) const;
  73.  
  74.     FORCEINLINE bool WithinAABox( Vector const &boxmin, Vector const &boxmax );
  75.  
  76.     vec_t DistTo( const Vector& vOther ) const;
  77.  
  78.     FORCEINLINE vec_t DistToSqr( const Vector& vOther ) const
  79.     {
  80.         Vector delta;
  81.  
  82.         delta.x = x - vOther.x;
  83.         delta.y = y - vOther.y;
  84.         delta.z = z - vOther.z;
  85.  
  86.         return delta.LengthSqr( );
  87.     }
  88.  
  89.     void CopyToArray( float* rgfl ) const;
  90.     void MulAdd( const Vector& a, const Vector& b, float scalar );
  91.  
  92.     vec_t Dot( const Vector& vOther ) const;
  93.  
  94.     Vector& operator=( const Vector& vOther );
  95.  
  96.     vec_t Length2D( void ) const;
  97.     vec_t Length2DSqr( void ) const;
  98.  
  99.     Vector operator-( void ) const;
  100.     Vector operator+( const Vector& v ) const;
  101.     Vector operator-( const Vector& v ) const;
  102.     Vector operator*( const Vector& v ) const;
  103.     Vector operator/( const Vector& v ) const;
  104.     Vector operator*( float fl ) const;
  105.     Vector operator/( float fl ) const;
  106.  
  107.     Vector Cross( const Vector& vOther ) const;
  108.  
  109.     Vector Min( const Vector& vOther ) const;
  110.     Vector Max( const Vector& vOther ) const;
  111. };
  112.  
  113. FORCEINLINE Vector ReplicateToVector( float x )
  114. {
  115.     return Vector( x, x, x );
  116. }
  117.  
  118. inline Vector::Vector( void )
  119. {
  120.  
  121. }
  122.  
  123. inline Vector::Vector( vec_t X, vec_t Y, vec_t Z )
  124. {
  125.     x = X; y = Y; z = Z;
  126. }
  127.  
  128. inline Vector::Vector( vec_t XYZ )
  129. {
  130.     x = y = z = XYZ;
  131. }
  132.  
  133. inline void Vector::Init( vec_t ix, vec_t iy, vec_t iz )
  134. {
  135.     x = ix; y = iy; z = iz;
  136. }
  137.  
  138. inline void Vector::Zero( )
  139. {
  140.     x = y = z = 0.0f;
  141. }
  142.  
  143. inline void VectorClear( Vector& a )
  144. {
  145.     a.x = a.y = a.z = 0.0f;
  146. }
  147.  
  148. inline Vector& Vector::operator=( const Vector& vOther )
  149. {
  150.     x = vOther.x; y = vOther.y; z = vOther.z;
  151.     return *this;
  152. }
  153.  
  154. inline vec_t& Vector::operator[]( int i )
  155. {
  156.     return ( ( vec_t* )this )[ i ];
  157. }
  158.  
  159. inline vec_t Vector::operator[]( int i ) const
  160. {
  161.     return ( ( vec_t* )this )[ i ];
  162. }
  163.  
  164. inline vec_t* Vector::Base( )
  165. {
  166.     return ( vec_t* )this;
  167. }
  168.  
  169. inline vec_t const* Vector::Base( ) const
  170. {
  171.     return ( vec_t const* )this;
  172. }
  173.  
  174. inline bool Vector::IsValid( ) const
  175. {
  176.     return ( x == x && y == y && z == z );
  177. }
  178.  
  179. inline void Vector::Invalidate( )
  180. {
  181.     x = y = z = VEC_T_NAN;
  182. }
  183.  
  184. inline bool Vector::operator==( const Vector& src ) const
  185. {
  186.     return ( src.x == x ) && ( src.y == y ) && ( src.z == z );
  187. }
  188.  
  189. inline bool Vector::operator!=( const Vector& src ) const
  190. {
  191.     return ( src.x != x ) || ( src.y != y ) || ( src.z != z );
  192. }
  193.  
  194. FORCEINLINE void VectorCopy( const Vector& src, Vector& dst )
  195. {
  196.     dst.x = src.x;
  197.     dst.y = src.y;
  198.     dst.z = src.z;
  199. }
  200.  
  201. inline void Vector::CopyToArray( float* rgfl ) const
  202. {
  203.     rgfl[0] = x; rgfl[1] = y; rgfl[2] = z;
  204. }
  205.  
  206. inline void Vector::Negate( )
  207. {
  208.     x = -x; y = -y; z = -z;
  209. }
  210.  
  211. FORCEINLINE Vector& Vector::operator+=( const Vector& v )
  212. {
  213.     x += v.x; y += v.y; z += v.z;
  214.     return *this;
  215. }
  216.  
  217. FORCEINLINE Vector& Vector::operator-=( const Vector& v )
  218. {
  219.     x -= v.x; y -= v.y; z -= v.z;
  220.     return *this;
  221. }
  222.  
  223. FORCEINLINE Vector& Vector::operator*=( float fl )
  224. {
  225.     x *= fl;
  226.     y *= fl;
  227.     z *= fl;
  228.     return *this;
  229. }
  230.  
  231. FORCEINLINE Vector& Vector::operator*=( const Vector& v )
  232. {
  233.     x *= v.x;
  234.     y *= v.y;
  235.     z *= v.z;
  236.     return *this;
  237. }
  238.  
  239. FORCEINLINE Vector& Vector::operator+=( float fl )
  240. {
  241.     x += fl;
  242.     y += fl;
  243.     z += fl;
  244.     return *this;
  245. }
  246.  
  247. FORCEINLINE Vector& Vector::operator-=( float fl )
  248. {
  249.     x -= fl;
  250.     y -= fl;
  251.     z -= fl;
  252.     return *this;
  253. }
  254.  
  255. FORCEINLINE Vector& Vector::operator/=( float fl )
  256. {
  257.     float oofl = 1.0f / fl;
  258.     x *= oofl;
  259.     y *= oofl;
  260.     z *= oofl;
  261.     return *this;
  262. }
  263.  
  264. FORCEINLINE Vector& Vector::operator/=( const Vector& v )
  265. {
  266.     x /= v.x;
  267.     y /= v.y;
  268.     z /= v.z;
  269.     return *this;
  270. }
  271.  
  272. FORCEINLINE void VectorAdd( const Vector& a, const Vector& b, Vector& c )
  273. {
  274.     c.x = a.x + b.x;
  275.     c.y = a.y + b.y;
  276.     c.z = a.z + b.z;
  277. }
  278.  
  279. FORCEINLINE void VectorSubtract( const Vector& a, const Vector& b, Vector& c )
  280. {
  281.     c.x = a.x - b.x;
  282.     c.y = a.y - b.y;
  283.     c.z = a.z - b.z;
  284. }
  285.  
  286. FORCEINLINE void VectorMultiply( const Vector& a, vec_t b, Vector& c )
  287. {
  288.     c.x = a.x * b;
  289.     c.y = a.y * b;
  290.     c.z = a.z * b;
  291. }
  292.  
  293. FORCEINLINE void VectorMultiply( const Vector& a, const Vector& b, Vector& c )
  294. {
  295.     c.x = a.x * b.x;
  296.     c.y = a.y * b.y;
  297.     c.z = a.z * b.z;
  298. }
  299.  
  300. inline void VectorScale( const Vector& in, vec_t scale, Vector& result )
  301. {
  302.     VectorMultiply( in, scale, result );
  303. }
  304.  
  305. FORCEINLINE void VectorDivide( const Vector& a, vec_t b, Vector& c )
  306. {
  307.     vec_t oob = 1.0f / b;
  308.     c.x = a.x * oob;
  309.     c.y = a.y * oob;
  310.     c.z = a.z * oob;
  311. }
  312.  
  313. FORCEINLINE void VectorDivide( const Vector& a, const Vector& b, Vector& c )
  314. {
  315.     c.x = a.x / b.x;
  316.     c.y = a.y / b.y;
  317.     c.z = a.z / b.z;
  318. }
  319.  
  320. inline void Vector::MulAdd( const Vector& a, const Vector& b, float scalar )
  321. {
  322.     x = a.x + b.x * scalar;
  323.     y = a.y + b.y * scalar;
  324.     z = a.z + b.z * scalar;
  325. }
  326.  
  327. inline void VectorLerp( const Vector& src1, const Vector& src2, vec_t t, Vector& dest )
  328. {
  329.     dest.x = src1.x + ( src2.x - src1.x ) * t;
  330.     dest.y = src1.y + ( src2.y - src1.y ) * t;
  331.     dest.z = src1.z + ( src2.z - src1.z ) * t;
  332. }
  333.  
  334. FORCEINLINE vec_t DotProduct( const Vector& a, const Vector& b )
  335. {
  336.     return ( a.x * b.x + a.y * b.y + a.z * b.z );
  337. }
  338.  
  339. inline vec_t Vector::Dot( const Vector& vOther ) const
  340. {
  341.     return DotProduct( *this, vOther );
  342. }
  343.  
  344. inline void CrossProduct( const Vector& a, const Vector& b, Vector& result )
  345. {
  346.     result.x = a.y * b.z - a.z * b.y;
  347.     result.y = a.z * b.x - a.x * b.z;
  348.     result.z = a.x * b.y - a.y * b.x;
  349. }
  350.  
  351. inline vec_t DotProductAbs( const Vector& v0, const Vector& v1 )
  352. {
  353.     return abs( v0.x * v1.x ) + abs( v0.y * v1.y ) + abs( v0.z * v1.z );
  354. }
  355.  
  356. inline vec_t VectorLength( const Vector& v )
  357. {
  358.     return ( vec_t )sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
  359. }
  360.  
  361. inline vec_t Vector::Length( void ) const
  362. {
  363.     return VectorLength( *this );
  364. }
  365.  
  366. inline vec_t VectorNormalize( Vector& v )
  367. {
  368.     vec_t l = v.Length( );
  369.  
  370.     if( l != 0.0f )
  371.     {
  372.         v /= l;
  373.     }
  374.     else
  375.     {
  376.         v.x = v.y = 0.0f; v.z = 1.0f;
  377.     }
  378.  
  379.     return l;
  380. }
  381.  
  382. bool Vector::WithinAABox( Vector const &boxmin, Vector const &boxmax )
  383. {
  384.     return (
  385.         ( x >= boxmin.x ) && ( x <= boxmax.x ) &&
  386.         ( y >= boxmin.y ) && ( y <= boxmax.y ) &&
  387.         ( z >= boxmin.z ) && ( z <= boxmax.z )
  388.         );
  389. }
  390.  
  391. inline vec_t Vector::DistTo( const Vector& vOther ) const
  392. {
  393.     Vector delta;
  394.     VectorSubtract( *this, vOther, delta );
  395.     return delta.Length( );
  396. }
  397.  
  398. inline Vector Vector::Min( const Vector& vOther ) const
  399. {
  400.     return Vector( x < vOther.x ? x : vOther.x,
  401.         y < vOther.y ? y : vOther.y,
  402.         z < vOther.z ? z : vOther.z );
  403. }
  404.  
  405. inline Vector Vector::Max( const Vector& vOther ) const
  406. {
  407.     return Vector( x > vOther.x ? x : vOther.x,
  408.         y > vOther.y ? y : vOther.y,
  409.         z > vOther.z ? z : vOther.z );
  410. }
  411.  
  412. inline Vector Vector::operator-( void ) const
  413. {
  414.     return Vector( -x, -y, -z );
  415. }
  416.  
  417. inline Vector Vector::operator+( const Vector& v ) const
  418. {
  419.     Vector res;
  420.     VectorAdd( *this, v, res );
  421.     return res;
  422. }
  423.  
  424. inline Vector Vector::operator-( const Vector& v ) const
  425. {
  426.     Vector res;
  427.     VectorSubtract( *this, v, res );
  428.     return res;
  429. }
  430.  
  431. inline Vector Vector::operator*( float fl ) const
  432. {
  433.     Vector res;
  434.     VectorMultiply( *this, fl, res );
  435.     return res;
  436. }
  437.  
  438. inline Vector Vector::operator*( const Vector& v ) const
  439. {
  440.     Vector res;
  441.     VectorMultiply( *this, v, res );
  442.     return res;
  443. }
  444.  
  445. inline Vector Vector::operator/( float fl ) const
  446. {
  447.     Vector res;
  448.     VectorDivide( *this, fl, res );
  449.     return res;
  450. }
  451.  
  452. inline Vector Vector::operator/( const Vector& v ) const
  453. {
  454.     Vector res;
  455.     VectorDivide( *this, v, res );
  456.     return res;
  457. }
  458.  
  459. inline Vector operator*( float fl, const Vector& v )
  460. {
  461.     return v * fl;
  462. }
  463.  
  464. inline Vector Vector::Cross( const Vector& vOther ) const
  465. {
  466.     Vector res;
  467.     CrossProduct( *this, vOther, res );
  468.     return res;
  469. }
  470.  
  471. inline vec_t Vector::Length2D( void ) const
  472. {
  473.     return ( vec_t )::sqrtf( x * x + y * y );
  474. }
  475.  
  476. inline vec_t Vector::Length2DSqr( void ) const
  477. {
  478.     return ( x * x + y * y );
  479. }
  480.  
  481. inline Vector CrossProduct( const Vector& a, const Vector& b )
  482. {
  483.     return Vector( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x );
  484. }
  485.  
  486. inline void VectorMin( const Vector& a, const Vector& b, Vector& result )
  487. {
  488.     result.x = min( a.x, b.x );
  489.     result.y = min( a.y, b.y );
  490.     result.z = min( a.z, b.z );
  491. }
  492.  
  493. inline void VectorMax( const Vector& a, const Vector& b, Vector& result )
  494. {
  495.     result.x = max( a.x, b.x );
  496.     result.y = max( a.y, b.y );
  497.     result.z = max( a.z, b.z );
  498. }
  499.  
  500. class VectorAligned : public Vector
  501. {
  502. public:
  503.     VectorAligned ()
  504.     {
  505.         x = y = z = 0.0f;
  506.     }
  507.  
  508.     VectorAligned ( const Vector& v )
  509.     {
  510.         x = v.x; y = v.y; z = v.z;
  511.     }
  512.  
  513.     float w;
  514. };
  515.  
  516. #endif // VECTOR_H
Advertisement
Add Comment
Please, Sign In to add comment