Guest User

Quaternion.h

a guest
Mar 1st, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1. #ifndef Quat_H
  2. #define Quat_H
  3.  
  4. //#include "vector.h"
  5. #include <math.h>
  6.  
  7.  
  8. //-----------------------------------------------------------------------------
  9. // Quat
  10. //-----------------------------------------------------------------------------
  11.  
  12. class RadEuler;
  13. class QAngle;
  14. class Vector;
  15. struct matrix3x4_t;
  16.  
  17. class Quat              // same data-layout as engine's vec4_t,
  18. {                               //      which is a vec_t[4]
  19. public:
  20.     inline Quat(void)   {
  21.    
  22.     // Initialize to NAN to catch errors
  23. #ifdef _DEBUG
  24. #ifdef VECTOR_PARANOIA
  25.         x = y = z = w = VEC_T_NAN;
  26. #endif
  27. #endif
  28.     }
  29.     inline Quat(vec_t ix, vec_t iy, vec_t iz, vec_t iw) : x(ix), y(iy), z(iz), w(iw) { }
  30.     inline Quat(RadEuler const &angle); // evil auto type promotion!!!
  31.  
  32.     inline void Init(vec_t ix=0.0f, vec_t iy=0.0f, vec_t iz=0.0f, vec_t iw=0.0f)    { x = ix; y = iy; z = iz; w = iw; }
  33.  
  34.     bool IsValid() const;
  35.  
  36.     bool operator==( const Quat &src ) const;
  37.     bool operator!=( const Quat &src ) const;
  38.  
  39.     Quat    operator*(const Quat& q) const;
  40.     Quat  operator/(const Quat& q) const;
  41.     Quat  operator*(const float c) const;
  42.  
  43.     Vector  operator*(const Vector& vec) const;
  44.  
  45.     FORCEINLINE Quat&   operator+=(const Quat &b);
  46.     FORCEINLINE Quat&   operator+(const Quat &b);
  47.  
  48.     FORCEINLINE Quat&   operator-=(const Quat &b);
  49.  
  50.     FORCEINLINE Quat&   operator*=(const float &f);
  51.     FORCEINLINE Quat&   operator*(const float &f);
  52.     //Quat operator*(const float& f) const;
  53.     //Quat operator-(const Quat& a, const Quat& b);
  54.     //friend inline Quat operator*(const Quat& a, const Quat& b);
  55.     //Quat operator+(const Quat& b) const;
  56.  
  57.     // array access...
  58.     vec_t operator[](int i) const;
  59.     vec_t& operator[](int i);
  60.  
  61.     vec_t x, y, z, w;
  62.  
  63.     //MKS - Quat extras
  64.     float  NormalizeInPlace();
  65.     void GetAngles( QAngle &angles );
  66.     //void GetAngles( RadEuler &angles );
  67.     //QuatAngles( const Quat &q, RadEuler &angles );
  68.     matrix3x4_t GetMatrix();
  69.  
  70.     inline Quat Conj() { return Quat( -x, -y, -z, w ); };
  71.     void Slerp( Quat &from, Quat &to, float t );
  72.     void SlerpNoAlign( const Quat &p, const Quat &q, float t );
  73.     void Align( const Quat &p, const Quat &q );
  74.     void AxisAngle( const Vector &axis, float angle );
  75.     void ToAxisAngle( Vector &axis, float &angle );
  76.  
  77.     void TransformVector( Vector &vec );
  78.     void FromRotationMatrix (const matrix3x4_t& kRot);
  79.     void ToRotationMatrix (matrix3x4_t& kRot) const;
  80.  
  81.     //Gets a rotation by using 2 UP directions.
  82.     void GetRotationTo(const Vector& start, const Vector& dest);
  83.  
  84.     //Gets a rotation by using cross product and given angle.
  85.     void GetRotationTo(const Vector& cross, float angle);
  86.  
  87.     void ToAxes(Vector* akAxis) const;
  88.    
  89.  
  90.     Quat i( void ) const;
  91.     Quat expon( void ) const;
  92.     Quat power(const float t) const;
  93.     Quat Logr( void )const;
  94.     float dot_prod(const Quat & q) const;
  95.  
  96.     void CreateFromYawPitchRoll(Vector up, float yaw, Vector right, float pitch, Vector forward, float roll);
  97. };
  98.  
  99. inline bool Quat::IsValid() const
  100. {
  101.     return IsFinite(x) && IsFinite(y) && IsFinite(z) && IsFinite(w);
  102. }
  103.  
  104. inline Quat& Quat::operator+= (const Quat& b)  
  105. //FORCEINLINE Quat operator+ (const Quat& b) const
  106. {
  107.     x += b.x;
  108.     y += b.y;
  109.     z += b.z;
  110.     w += b.w;
  111.     return *this;
  112. }
  113.  
  114. inline  Quat& Quat::operator+ (const Quat& b)  
  115. //FORCEINLINE Quat operator+ (const Quat& b) const
  116. {
  117.     x += b.x;
  118.     y += b.y;
  119.     z += b.z;
  120.     w += b.w;
  121.     return *this;
  122. }
  123. /*
  124. inline Quat operator* (const Quat& a, const Quat& b)
  125. {
  126.     return Quat( a.w*b.x + a.x*b.w + a.y*b.z - a.z*b.y,
  127.                     a.w*b.y - a.x*b.z + a.y*b.w + a.z*b.x,
  128.                     a.w*b.z + a.x*b.y - a.y*b.x + a.z*b.w,
  129.                     a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z );
  130. }
  131. */
  132. inline  Quat& Quat::operator*= (const float& f)
  133. //Quat Quat::operator* (const float& f) const
  134. {
  135.     x*=f;
  136.     y*=f;
  137.     z*=f;
  138.     w*=f;
  139.     return *this;
  140. }
  141.  
  142. inline  Quat& Quat::operator* (const float& f)
  143. //Quat Quat::operator* (const float& f) const
  144. {
  145.     x*=f;
  146.     y*=f;
  147.     z*=f;
  148.     w*=f;
  149.     return *this;
  150. }
  151. /*
  152. FORCEINLINE_VECTOR  Quat& Quat::operator- (const Quat& b)
  153. //Quat Quat::operator- (const Quat& b) const
  154. {
  155.     x -= b.x;
  156.     y -= b.y;
  157.     z -= b.z;
  158.     w -= b.w;
  159.     return *this;
  160. }
  161. */
  162. //-----------------------------------------------------------------------------
  163. // Radian Euler QAngle aligned to axis (NOT ROLL/PITCH/YAW)
  164. //-----------------------------------------------------------------------------
  165. class RadEuler
  166. {
  167. public:
  168.     inline RadEuler(void)                           { }
  169.     inline RadEuler(vec_t X, vec_t Y, vec_t Z)      { x = X; y = Y; z = Z; }
  170.     //inline RadEuler(Quat const &q);   // evil auto type promotion!!!
  171.  
  172.     // Initialization
  173.     inline void Init(vec_t ix=0.0f, vec_t iy=0.0f, vec_t iz=0.0f)   { x = ix; y = iy; z = iz; }
  174.  
  175.     bool IsValid() const;
  176.  
  177.     // array access...
  178.     vec_t operator[](int i) const;
  179.     vec_t& operator[](int i);
  180.  
  181.     vec_t x, y, z;
  182. };
  183.  
  184. inline bool RadEuler::IsValid() const
  185. {
  186.     return IsFinite(x) && IsFinite(y) && IsFinite(z);
  187. }
  188.  
  189. /*
  190. inline RadEuler::RadEuler(Quat const &q)
  191. {
  192.     RadEuler ang;
  193.     q.GetAngles( ang );
  194.     x = ang.x;
  195.     y = ang.y;
  196.     z = ang.z;
  197.     //QuatAngles( q, *this );
  198. }
  199. */
  200. inline void VectorCopy( RadEuler const& src, RadEuler &dst )
  201. {
  202.     //CHECK_VALID(src);
  203.     dst.x = src.x;
  204.     dst.y = src.y;
  205.     dst.z = src.z;
  206. }
  207.  
  208. inline void VectorScale( RadEuler const& src, float b, RadEuler &dst )
  209. {
  210.     //CHECK_VALID(src);
  211. //  Assert( IsFinite(b) );
  212.     dst.x = src.x * b;
  213.     dst.y = src.y * b;
  214.     dst.z = src.z * b;
  215. }
  216.  
  217. Quat Slerp2(const Quat & q0, const Quat & q1, const float t);
  218. Quat Slerp_Prime(const Quat & q0, const Quat & q1, const float t);
  219.  
  220. #endif
Advertisement
Add Comment
Please, Sign In to add comment