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

  1.  
  2. #ifndef __MATH_ANGLES_H__
  3. #define __MATH_ANGLES_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     Euler angles
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. // angle indexes
  14. #define    PITCH                0        // up / down
  15. #define    YAW                    1        // left / right
  16. #define    ROLL                2        // fall over
  17.  
  18. #ifndef M_PI
  19. #  define M_PI (3.1415926536f)
  20. #endif
  21.  
  22. class idVec3;
  23. class idQuat;
  24. class idRotation;
  25. class idMat3;
  26. class idMat4;
  27.  
  28. class idAngles {
  29. public:
  30.     float            pitch;
  31.     float            yaw;
  32.     float            roll;
  33.  
  34.                     idAngles( void );
  35.                     idAngles( float pitch, float yaw, float roll );
  36.                     explicit idAngles( const idVec3 &v );
  37.  
  38.     void             Set( float pitch, float yaw, float roll );
  39.     idAngles &        Zero( void );
  40.  
  41.     float            operator[]( int index ) const;
  42.     float &            operator[]( int index );
  43.     idAngles        operator-() const;            // negate angles, in general not the inverse rotation
  44.     idAngles &        operator=( const idAngles &a );
  45.     idAngles        operator+( const idAngles &a ) const;
  46.     idAngles &        operator+=( const idAngles &a );
  47.     idAngles        operator-( const idAngles &a ) const;
  48.     idAngles &        operator-=( const idAngles &a );
  49.     idAngles        operator*( const float a ) const;
  50.     idAngles &        operator*=( const float a );
  51.     idAngles        operator/( const float a ) const;
  52.     idAngles &        operator/=( const float a );
  53.  
  54.     friend idAngles    operator*( const float a, const idAngles &b );
  55.  
  56.     bool            Compare( const idAngles &a ) const;                            // exact compare, no epsilon
  57.     bool            Compare( const idAngles &a, const float epsilon ) const;    // compare with epsilon
  58.     bool            operator==(    const idAngles &a ) const;                        // exact compare, no epsilon
  59.     bool            operator!=(    const idAngles &a ) const;                        // exact compare, no epsilon
  60.  
  61.     idAngles &        Normalize360( void );    // normalizes 'this'
  62.     idAngles &        Normalize180( void );    // normalizes 'this'
  63.  
  64.     float            Length( void ) const;
  65.     float            LengthSqr( void ) const;
  66.     void            Clamp( const idAngles &min, const idAngles &max );
  67.  
  68.     int                GetDimension( void ) const;
  69.  
  70.     void            ToVectors( idVec3 *forward, idVec3 *right = NULL, idVec3 *up = NULL ) const;
  71.     idVec3            ToForward( void ) const;
  72.     idQuat            ToQuat( void ) const;
  73.     idRotation        ToRotation( void ) const;
  74.     idMat3            ToMat3( void ) const;
  75.  
  76. // RAVEN BEGIN
  77.     idMat3            &ToMat3( idMat3 &mat ) const;
  78. // abahr
  79.     idAngles        Random( const idVec3& range, idRandom& random ) const;
  80.     idAngles&        Scale( const idAngles& scalar );
  81.     idAngles&        Remap( const int map[], const int dirMap[] );
  82. // RAVEN END
  83.  
  84.     idMat4            ToMat4( void ) const;
  85.     idVec3            ToAngularVelocity( void ) const;
  86.     const float *    ToFloatPtr( void ) const;
  87.     float *            ToFloatPtr( void );
  88.     const char *    ToString( int precision = 2 ) const;
  89. };
  90.  
  91. extern idAngles ang_zero;
  92.  
  93. ID_INLINE idAngles::idAngles( void ) {
  94. }
  95.  
  96. ID_INLINE idAngles::idAngles( float pitch, float yaw, float roll ) {
  97.     this->pitch = pitch;
  98.     this->yaw    = yaw;
  99.     this->roll    = roll;
  100. }
  101.  
  102. ID_INLINE idAngles::idAngles( const idVec3 &v ) {
  103.     this->pitch = v[0];
  104.     this->yaw    = v[1];
  105.     this->roll    = v[2];
  106. }
  107.  
  108. ID_INLINE void idAngles::Set( float pitch, float yaw, float roll ) {
  109.     this->pitch = pitch;
  110.     this->yaw    = yaw;
  111.     this->roll    = roll;
  112. }
  113.  
  114. ID_INLINE idAngles &idAngles::Zero( void ) {
  115.     pitch = yaw = roll = 0.0f;
  116.     return *this;
  117. }
  118.  
  119. ID_INLINE float idAngles::operator[]( int index ) const {
  120.     assert( ( index >= 0 ) && ( index < 3 ) );
  121.     return ( &pitch )[ index ];
  122. }
  123.  
  124. ID_INLINE float &idAngles::operator[]( int index ) {
  125.     assert( ( index >= 0 ) && ( index < 3 ) );
  126.     return ( &pitch )[ index ];
  127. }
  128.  
  129. ID_INLINE idAngles idAngles::operator-() const {
  130.     return idAngles( -pitch, -yaw, -roll );
  131. }
  132.  
  133. ID_INLINE idAngles &idAngles::operator=( const idAngles &a ) {
  134.     pitch    = a.pitch;
  135.     yaw        = a.yaw;
  136.     roll    = a.roll;
  137.     return *this;
  138. }
  139.  
  140. ID_INLINE idAngles idAngles::operator+( const idAngles &a ) const {
  141.     return idAngles( pitch + a.pitch, yaw + a.yaw, roll + a.roll );
  142. }
  143.  
  144. ID_INLINE idAngles& idAngles::operator+=( const idAngles &a ) {
  145.     pitch    += a.pitch;
  146.     yaw        += a.yaw;
  147.     roll    += a.roll;
  148.  
  149.     return *this;
  150. }
  151.  
  152. ID_INLINE idAngles idAngles::operator-( const idAngles &a ) const {
  153.     return idAngles( pitch - a.pitch, yaw - a.yaw, roll - a.roll );
  154. }
  155.  
  156. ID_INLINE idAngles& idAngles::operator-=( const idAngles &a ) {
  157.     pitch    -= a.pitch;
  158.     yaw        -= a.yaw;
  159.     roll    -= a.roll;
  160.  
  161.     return *this;
  162. }
  163.  
  164. ID_INLINE idAngles idAngles::operator*( const float a ) const {
  165.     return idAngles( pitch * a, yaw * a, roll * a );
  166. }
  167.  
  168. ID_INLINE idAngles& idAngles::operator*=( float a ) {
  169.     pitch    *= a;
  170.     yaw        *= a;
  171.     roll    *= a;
  172.     return *this;
  173. }
  174.  
  175. ID_INLINE idAngles idAngles::operator/( const float a ) const {
  176.     float inva = 1.0f / a;
  177.     return idAngles( pitch * inva, yaw * inva, roll * inva );
  178. }
  179.  
  180. ID_INLINE idAngles& idAngles::operator/=( float a ) {
  181.     float inva = 1.0f / a;
  182.     pitch    *= inva;
  183.     yaw        *= inva;
  184.     roll    *= inva;
  185.     return *this;
  186. }
  187.  
  188. ID_INLINE idAngles operator*( const float a, const idAngles &b ) {
  189.     return idAngles( a * b.pitch, a * b.yaw, a * b.roll );
  190. }
  191.  
  192. ID_INLINE bool idAngles::Compare( const idAngles &a ) const {
  193.     return ( ( a.pitch == pitch ) && ( a.yaw == yaw ) && ( a.roll == roll ) );
  194. }
  195.  
  196. ID_INLINE bool idAngles::Compare( const idAngles &a, const float epsilon ) const {
  197.     if ( idMath::Fabs( pitch - a.pitch ) > epsilon ) {
  198.         return false;
  199.     }
  200.             
  201.     if ( idMath::Fabs( yaw - a.yaw ) > epsilon ) {
  202.         return false;
  203.     }
  204.  
  205.     if ( idMath::Fabs( roll - a.roll ) > epsilon ) {
  206.         return false;
  207.     }
  208.  
  209.     return true;
  210. }
  211.  
  212. ID_INLINE bool idAngles::operator==( const idAngles &a ) const {
  213.     return Compare( a );
  214. }
  215.  
  216. ID_INLINE bool idAngles::operator!=( const idAngles &a ) const {
  217.     return !Compare( a );
  218. }
  219.  
  220. ID_INLINE float idAngles::Length( void ) const {
  221.     return idMath::Sqrt( yaw * yaw + pitch * pitch + roll * roll );
  222. }
  223.  
  224. ID_INLINE float idAngles::LengthSqr( void ) const {
  225.     return ( yaw * yaw + pitch * pitch + roll * roll );
  226. }
  227.  
  228. ID_INLINE void idAngles::Clamp( const idAngles &min, const idAngles &max ) {
  229.     if ( pitch < min.pitch ) {
  230.         pitch = min.pitch;
  231.     } else if ( pitch > max.pitch ) {
  232.         pitch = max.pitch;
  233.     }
  234.     if ( yaw < min.yaw ) {
  235.         yaw = min.yaw;
  236.     } else if ( yaw > max.yaw ) {
  237.         yaw = max.yaw;
  238.     }
  239.     if ( roll < min.roll ) {
  240.         roll = min.roll;
  241.     } else if ( roll > max.roll ) {
  242.         roll = max.roll;
  243.     }
  244. }
  245.  
  246. ID_INLINE int idAngles::GetDimension( void ) const {
  247.     return 3;
  248. }
  249.  
  250. ID_INLINE const float *idAngles::ToFloatPtr( void ) const {
  251.     return &pitch;
  252. }
  253.  
  254. ID_INLINE float *idAngles::ToFloatPtr( void ) {
  255.     return &pitch;
  256. }
  257.  
  258. // RAVEN BEGIN
  259. // abahr
  260. ID_INLINE idAngles idAngles::Random( const idVec3& range, idRandom& random ) const {
  261.     idAngles a( *this );
  262.     for( int ix = 0; ix < GetDimension(); ++ix ) {
  263.         a[ ix ] += a[ ix ] * range[ix] * random.CRandomFloat();
  264.     }
  265.     return a;
  266. }
  267.  
  268. ID_INLINE idAngles& idAngles::Scale( const idAngles& scalar ) {
  269.     for( int ix = 0; ix < GetDimension(); ++ix ) {
  270.         (*this)[ix] *= scalar[ix];
  271.     }
  272.  
  273.     return *this;
  274. }
  275.  
  276. ID_INLINE idAngles& idAngles::Remap( const int map[], const int dirMap[] ) {
  277.     idAngles ang( *this );
  278.     for( int ix = 0; ix < GetDimension(); ++ix ) {
  279.         (*this)[ ix ] = SignZero(dirMap[ix]) * ang[ abs(map[ix]) ];
  280.     }
  281.  
  282.     return *this;
  283. }
  284. // RAVEN END
  285.  
  286. #endif /* !__MATH_ANGLES_H__ */
  287.