home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / math / Radians.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  6KB  |  253 lines

  1. #ifndef _MATH_RADIANS_H_INC_
  2. #define _MATH_RADIANS_H_INC_
  3.  
  4. /*
  5. ===============================================================================
  6.     Euler angles
  7.  
  8.     This is basically a duplicate of idAngles, but used radians rather than 
  9.     degrees (to avoid the conversion before trig calls)
  10.  
  11.     All trig calls use float precision
  12.  
  13.     ToMat3 passes in workspace to avoid a memcpy
  14.  
  15. ===============================================================================
  16. */
  17.  
  18. #ifndef M_PI
  19. #  define M_PI (3.1415926536f)
  20. #endif
  21.  
  22. class idVec3;
  23. class idMat3;
  24.  
  25. class rvAngles 
  26. {
  27. public:
  28.     float            pitch;
  29.     float            yaw;
  30.     float            roll;
  31.  
  32.                     rvAngles( void ) {}
  33.                     rvAngles( float pitch, float yaw, float roll );
  34.     explicit        rvAngles( const idVec3 &v );
  35.  
  36.     void             Set( float pitch, float yaw, float roll );
  37.     rvAngles &        Zero( void );
  38.  
  39.     float            operator[]( int index ) const;
  40.     float &            operator[]( int index );
  41.     rvAngles        operator-() const;
  42.     rvAngles &        operator=( const rvAngles &a );
  43.     rvAngles &        operator=( const idVec3 &a );
  44.     rvAngles        operator+( const rvAngles &a ) const;
  45.     rvAngles        operator+( const idVec3 &a ) const;
  46.     rvAngles &        operator+=( const rvAngles &a );
  47.     rvAngles &        operator+=( const idVec3 &a );
  48.     rvAngles        operator-( const rvAngles &a ) const;
  49.     rvAngles        operator-( const idVec3 &a ) const;
  50.     rvAngles &        operator-=( const rvAngles &a );
  51.     rvAngles &        operator-=( const idVec3 &a );
  52.     rvAngles        operator*( const float a ) const;
  53.     rvAngles &        operator*=( const float a );
  54.  
  55.     friend rvAngles    operator+( const idVec3 &a, const rvAngles &b );
  56.     friend rvAngles    operator-( const idVec3 &a, const rvAngles &b );
  57.     friend rvAngles    operator*( const float a, const rvAngles &b );
  58.  
  59.     bool            Compare( const rvAngles &a ) const;                            // exact compare, no epsilon
  60.     bool            Compare( const rvAngles &a, const float epsilon ) const;    // compare with epsilon
  61.     bool            operator==(    const rvAngles &a ) const;                        // exact compare, no epsilon
  62.     bool            operator!=(    const rvAngles &a ) const;                        // exact compare, no epsilon
  63.  
  64.     rvAngles &        NormalizeFull( void );    // normalizes 'this'
  65.     rvAngles &        NormalizeHalf( void );    // normalizes 'this'
  66.  
  67.     void            ToVectors( idVec3 *forward, idVec3 *right = NULL, idVec3 *up = NULL ) const;
  68.     idVec3            ToForward( void ) const;
  69.     idMat3            &ToMat3( idMat3 &mat ) const;
  70.  
  71.     const float *    ToFloatPtr( void ) const { return( &pitch ); }
  72.     float *            ToFloatPtr( void ) { return( &pitch ); }
  73. };
  74.  
  75. ID_INLINE rvAngles::rvAngles( float pitch, float yaw, float roll ) 
  76. {
  77.     this->pitch = pitch;
  78.     this->yaw    = yaw;
  79.     this->roll    = roll;
  80. }
  81.  
  82. ID_INLINE rvAngles::rvAngles( const idVec3 &v ) 
  83. {
  84.     this->pitch = v[0];
  85.     this->yaw    = v[1];
  86.     this->roll    = v[2];
  87. }
  88.  
  89. ID_INLINE void rvAngles::Set( float pitch, float yaw, float roll ) 
  90. {
  91.     this->pitch = pitch;
  92.     this->yaw    = yaw;
  93.     this->roll    = roll;
  94. }
  95.  
  96. ID_INLINE rvAngles &rvAngles::Zero( void ) 
  97. {
  98.     pitch = 0.0f;
  99.     yaw = 0.0f;
  100.     roll = 0.0f;
  101.     return( *this );
  102. }
  103.  
  104. ID_INLINE float rvAngles::operator[]( int index ) const 
  105. {
  106.     assert( ( index >= 0 ) && ( index < 3 ) );
  107.     return( ( &pitch )[ index ] );
  108. }
  109.  
  110. ID_INLINE float &rvAngles::operator[]( int index ) 
  111. {
  112.     assert( ( index >= 0 ) && ( index < 3 ) );
  113.     return( ( &pitch )[ index ] );
  114. }
  115.  
  116. ID_INLINE rvAngles rvAngles::operator-( void ) const 
  117. {
  118.     return( rvAngles( -pitch, -yaw, -roll ) );
  119. }
  120.  
  121. ID_INLINE rvAngles &rvAngles::operator=( const rvAngles &a ) 
  122. {
  123.     pitch = a.pitch;
  124.     yaw = a.yaw;
  125.     roll = a.roll;
  126.     return( *this );
  127. }
  128.  
  129. ID_INLINE rvAngles &rvAngles::operator=( const idVec3 &a ) 
  130. {
  131.     pitch = a.x;
  132.     yaw = a.y;
  133.     roll = a.z;
  134.     return( *this );
  135. }
  136.  
  137. ID_INLINE rvAngles rvAngles::operator+( const rvAngles &a ) const 
  138. {
  139.     return( rvAngles( pitch + a.pitch, yaw + a.yaw, roll + a.roll ) );
  140. }
  141.  
  142. ID_INLINE rvAngles rvAngles::operator+( const idVec3 &a ) const 
  143. {
  144.     return( rvAngles( pitch + a.x, yaw + a.y, roll + a.z ) );
  145. }
  146.  
  147. ID_INLINE rvAngles& rvAngles::operator+=( const rvAngles &a ) 
  148. {
  149.     pitch += a.pitch;
  150.     yaw += a.yaw;
  151.     roll += a.roll;
  152.     return( *this );
  153. }
  154.  
  155. ID_INLINE rvAngles& rvAngles::operator+=( const idVec3 &a ) 
  156. {
  157.     pitch += a.x;
  158.     yaw += a.y;
  159.     roll += a.z;
  160.     return( *this );
  161. }
  162.  
  163. ID_INLINE rvAngles rvAngles::operator-( const rvAngles &a ) const 
  164. {
  165.     return( rvAngles( pitch - a.pitch, yaw - a.yaw, roll - a.roll ) );
  166. }
  167.  
  168. ID_INLINE rvAngles rvAngles::operator-( const idVec3 &a ) const 
  169. {
  170.     return( rvAngles( pitch - a.x, yaw - a.y, roll - a.z ) );
  171. }
  172.  
  173. ID_INLINE rvAngles& rvAngles::operator-=( const rvAngles &a ) 
  174. {
  175.     pitch -= a.pitch;
  176.     yaw -= a.yaw;
  177.     roll -= a.roll;
  178.     return( *this );
  179. }
  180.  
  181. ID_INLINE rvAngles& rvAngles::operator-=( const idVec3 &a ) 
  182. {
  183.     pitch -= a.x;
  184.     yaw -= a.y;
  185.     roll -= a.z;
  186.     return( *this );
  187. }
  188.  
  189. ID_INLINE rvAngles rvAngles::operator*( const float a ) const 
  190. {
  191.     return( rvAngles( pitch * a, yaw * a, roll * a ) );
  192. }
  193.  
  194. ID_INLINE rvAngles& rvAngles::operator*=( float a ) 
  195. {
  196.     pitch *= a;
  197.     yaw *= a;
  198.     roll *= a;
  199.     return( *this );
  200. }
  201.  
  202. ID_INLINE rvAngles operator+( const idVec3 &a, const rvAngles &b ) 
  203. {
  204.     return( rvAngles( a.x + b.pitch, a.y + b.yaw, a.z + b.roll ) );
  205. }
  206.  
  207. ID_INLINE rvAngles operator-( const idVec3 &a, const rvAngles &b ) 
  208. {
  209.     return( rvAngles( a.x - b.pitch, a.y - b.yaw, a.z - b.roll ) ); 
  210. }
  211.  
  212. ID_INLINE rvAngles operator*( const float a, const rvAngles &b ) 
  213. {
  214.     return( rvAngles( a * b.pitch, a * b.yaw, a * b.roll ) );
  215. }
  216.  
  217. ID_INLINE bool rvAngles::Compare( const rvAngles &a ) const 
  218. {
  219.     return( ( a.pitch == pitch ) && ( a.yaw == yaw ) && ( a.roll == roll ) );
  220. }
  221.  
  222. ID_INLINE bool rvAngles::Compare( const rvAngles &a, const float epsilon ) const 
  223. {
  224.     if( idMath::Fabs( pitch - a.pitch ) > epsilon ) 
  225.     {
  226.         return( false );
  227.     }
  228.             
  229.     if( idMath::Fabs( yaw - a.yaw ) > epsilon ) 
  230.     {
  231.         return( false );
  232.     }
  233.  
  234.     if( idMath::Fabs( roll - a.roll ) > epsilon ) 
  235.     {
  236.         return( false );
  237.     }
  238.  
  239.     return( true );
  240. }
  241.  
  242. ID_INLINE bool rvAngles::operator==( const rvAngles &a ) const 
  243. {
  244.     return( Compare( a ) );
  245. }
  246.  
  247. ID_INLINE bool rvAngles::operator!=( const rvAngles &a ) const 
  248. {
  249.     return( !Compare( a ) );
  250. }
  251.  
  252. #endif // _MATH_RADIANS_H_INC_
  253.