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

  1.  
  2. #include "../precompiled.h"
  3. #pragma hdrstop
  4.  
  5. /*
  6. =====================
  7. idQuat::ToAngles
  8. =====================
  9. */
  10. idAngles idQuat::ToAngles( void ) const {
  11.     return ToMat3().ToAngles();
  12. }
  13.  
  14. /*
  15. =====================
  16. idQuat::ToRotation
  17. =====================
  18. */
  19. idRotation idQuat::ToRotation( void ) const {
  20.     idVec3 vec;
  21.     float angle;
  22.  
  23.     vec.x = x;
  24.     vec.y = y;
  25.     vec.z = z;
  26.     angle = idMath::ACos( w );
  27.     if ( angle == 0.0f ) {
  28.         vec.Set( 0.0f, 0.0f, 1.0f );
  29.     } else {
  30.         //vec *= (1.0f / sin( angle ));
  31.         vec.Normalize();
  32.         vec.FixDegenerateNormal();
  33.         angle *= 2.0f * idMath::M_RAD2DEG;
  34.     }
  35.     return idRotation( vec3_origin, vec, angle );
  36. }
  37.  
  38. /*
  39. =====================
  40. idQuat::ToMat3
  41. =====================
  42. */
  43. idMat3 idQuat::ToMat3( void ) const {
  44.     idMat3    mat;
  45.     float    wx, wy, wz;
  46.     float    xx, yy, yz;
  47.     float    xy, xz, zz;
  48.     float    x2, y2, z2;
  49.  
  50.     x2 = x + x;
  51.     y2 = y + y;
  52.     z2 = z + z;
  53.  
  54.     xx = x * x2;
  55.     xy = x * y2;
  56.     xz = x * z2;
  57.  
  58.     yy = y * y2;
  59.     yz = y * z2;
  60.     zz = z * z2;
  61.  
  62.     wx = w * x2;
  63.     wy = w * y2;
  64.     wz = w * z2;
  65.  
  66.     mat[ 0 ][ 0 ] = 1.0f - ( yy + zz );
  67.     mat[ 0 ][ 1 ] = xy - wz;
  68.     mat[ 0 ][ 2 ] = xz + wy;
  69.  
  70.     mat[ 1 ][ 0 ] = xy + wz;
  71.     mat[ 1 ][ 1 ] = 1.0f - ( xx + zz );
  72.     mat[ 1 ][ 2 ] = yz - wx;
  73.  
  74.     mat[ 2 ][ 0 ] = xz - wy;
  75.     mat[ 2 ][ 1 ] = yz + wx;
  76.     mat[ 2 ][ 2 ] = 1.0f - ( xx + yy );
  77.  
  78.     return mat;
  79. }
  80.  
  81. /*
  82. =====================
  83. idQuat::ToMat4
  84. =====================
  85. */
  86. idMat4 idQuat::ToMat4( void ) const {
  87.     return ToMat3().ToMat4();
  88. }
  89.  
  90. /*
  91. =====================
  92. idQuat::ToCQuat
  93. =====================
  94. */
  95. idCQuat idQuat::ToCQuat( void ) const {
  96.     if ( w < 0.0f ) {
  97.         return idCQuat( -x, -y, -z );
  98.     }
  99.     return idCQuat( x, y, z );
  100. }
  101.  
  102. /*
  103. ============
  104. idQuat::ToAngularVelocity
  105. ============
  106. */
  107. idVec3 idQuat::ToAngularVelocity( void ) const {
  108.     idVec3 vec;
  109.  
  110.     vec.x = x;
  111.     vec.y = y;
  112.     vec.z = z;
  113.     vec.Normalize();
  114.     return vec * idMath::ACos( w );
  115. }
  116.  
  117. /*
  118. =============
  119. idQuat::ToString
  120. =============
  121. */
  122. const char *idQuat::ToString( int precision ) const {
  123.     return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  124. }
  125.  
  126. /*
  127. =====================
  128. idQuat::Slerp
  129.  
  130. Spherical linear interpolation between two quaternions.
  131. =====================
  132. */
  133. idQuat &idQuat::Slerp( const idQuat &from, const idQuat &to, float t ) {
  134. #ifdef _XENON
  135. #else
  136.     idQuat    temp;
  137.     float    omega, cosom, sinom, scale0, scale1;
  138.  
  139.     if ( t <= 0.0f ) {
  140.         *this = from;
  141.         return *this;
  142.     }
  143.  
  144.     if ( t >= 1.0f ) {
  145.         *this = to;
  146.         return *this;
  147.     }
  148.  
  149.     if ( from == to ) {
  150.         *this = to;
  151.         return *this;
  152.     }
  153.  
  154.     cosom = from.x * to.x + from.y * to.y + from.z * to.z + from.w * to.w;
  155.     if ( cosom < 0.0f ) {
  156.         temp = -to;
  157.         cosom = -cosom;
  158.     } else {
  159.         temp = to;
  160.     }
  161.  
  162.     if ( ( 1.0f - cosom ) > 1e-6f ) {
  163. #if 0
  164.         omega = acos( cosom );
  165.         sinom = 1.0f / idMath::Sin( omega );
  166.         scale0 = idMath::Sin( ( 1.0f - t ) * omega ) * sinom;
  167.         scale1 = idMath::Sin( t * omega ) * sinom;
  168. #else
  169.         scale0 = 1.0f - cosom * cosom;
  170.         sinom = idMath::InvSqrt( scale0 );
  171.         omega = idMath::ATan16( scale0 * sinom, cosom );
  172.         scale0 = idMath::Sin16( ( 1.0f - t ) * omega ) * sinom;
  173.         scale1 = idMath::Sin16( t * omega ) * sinom;
  174. #endif
  175.     } else {
  176.         scale0 = 1.0f - t;
  177.         scale1 = t;
  178.     }
  179.  
  180.     *this = ( scale0 * from ) + ( scale1 * temp );
  181.     return *this;
  182. #endif
  183. }
  184.  
  185. /*
  186. =============
  187. idCQuat::ToAngles
  188. =============
  189. */
  190. idAngles idCQuat::ToAngles( void ) const {
  191.     return ToQuat().ToAngles();
  192. }
  193.  
  194. /*
  195. =============
  196. idCQuat::ToRotation
  197. =============
  198. */
  199. idRotation idCQuat::ToRotation( void ) const {
  200.     return ToQuat().ToRotation();
  201. }
  202.  
  203. /*
  204. =============
  205. idCQuat::ToMat3
  206. =============
  207. */
  208. idMat3 idCQuat::ToMat3( void ) const {
  209.     return ToQuat().ToMat3();
  210. }
  211.  
  212. /*
  213. =============
  214. idCQuat::ToMat4
  215. =============
  216. */
  217. idMat4 idCQuat::ToMat4( void ) const {
  218.     return ToQuat().ToMat4();
  219. }
  220.  
  221. /*
  222. =============
  223. idCQuat::ToString
  224. =============
  225. */
  226. const char *idCQuat::ToString( int precision ) const {
  227.     return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  228. }
  229.