Quat.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. =====================
  24. idQuat::ToAngles
  25. =====================
  26. */
  27. idAngles idQuat::ToAngles( void ) const {
  28. return ToMat3().ToAngles();
  29. }
  30. /*
  31. =====================
  32. idQuat::ToRotation
  33. =====================
  34. */
  35. idRotation idQuat::ToRotation( void ) const {
  36. idVec3 vec;
  37. float angle;
  38. vec.x = x;
  39. vec.y = y;
  40. vec.z = z;
  41. angle = idMath::ACos( w );
  42. if ( angle == 0.0f ) {
  43. vec.Set( 0.0f, 0.0f, 1.0f );
  44. } else {
  45. //vec *= (1.0f / sin( angle ));
  46. vec.Normalize();
  47. vec.FixDegenerateNormal();
  48. angle *= 2.0f * idMath::M_RAD2DEG;
  49. }
  50. return idRotation( vec3_origin, vec, angle );
  51. }
  52. /*
  53. =====================
  54. idQuat::ToMat3
  55. =====================
  56. */
  57. idMat3 idQuat::ToMat3( void ) const {
  58. idMat3 mat;
  59. float wx, wy, wz;
  60. float xx, yy, yz;
  61. float xy, xz, zz;
  62. float x2, y2, z2;
  63. x2 = x + x;
  64. y2 = y + y;
  65. z2 = z + z;
  66. xx = x * x2;
  67. xy = x * y2;
  68. xz = x * z2;
  69. yy = y * y2;
  70. yz = y * z2;
  71. zz = z * z2;
  72. wx = w * x2;
  73. wy = w * y2;
  74. wz = w * z2;
  75. mat[ 0 ][ 0 ] = 1.0f - ( yy + zz );
  76. mat[ 0 ][ 1 ] = xy - wz;
  77. mat[ 0 ][ 2 ] = xz + wy;
  78. mat[ 1 ][ 0 ] = xy + wz;
  79. mat[ 1 ][ 1 ] = 1.0f - ( xx + zz );
  80. mat[ 1 ][ 2 ] = yz - wx;
  81. mat[ 2 ][ 0 ] = xz - wy;
  82. mat[ 2 ][ 1 ] = yz + wx;
  83. mat[ 2 ][ 2 ] = 1.0f - ( xx + yy );
  84. return mat;
  85. }
  86. /*
  87. =====================
  88. idQuat::ToMat4
  89. =====================
  90. */
  91. idMat4 idQuat::ToMat4( void ) const {
  92. return ToMat3().ToMat4();
  93. }
  94. /*
  95. =====================
  96. idQuat::ToCQuat
  97. =====================
  98. */
  99. idCQuat idQuat::ToCQuat( void ) const {
  100. if ( w < 0.0f ) {
  101. return idCQuat( -x, -y, -z );
  102. }
  103. return idCQuat( x, y, z );
  104. }
  105. /*
  106. ============
  107. idQuat::ToAngularVelocity
  108. ============
  109. */
  110. idVec3 idQuat::ToAngularVelocity( void ) const {
  111. idVec3 vec;
  112. vec.x = x;
  113. vec.y = y;
  114. vec.z = z;
  115. vec.Normalize();
  116. return vec * idMath::ACos( w );
  117. }
  118. /*
  119. =============
  120. idQuat::ToString
  121. =============
  122. */
  123. const char *idQuat::ToString( int precision ) const {
  124. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  125. }
  126. /*
  127. =====================
  128. idQuat::Slerp
  129. Spherical linear interpolation between two quaternions.
  130. =====================
  131. */
  132. idQuat &idQuat::Slerp( const idQuat &from, const idQuat &to, float t ) {
  133. idQuat temp;
  134. float omega, cosom, sinom, scale0, scale1;
  135. if ( t <= 0.0f ) {
  136. *this = from;
  137. return *this;
  138. }
  139. if ( t >= 1.0f ) {
  140. *this = to;
  141. return *this;
  142. }
  143. if ( from == to ) {
  144. *this = to;
  145. return *this;
  146. }
  147. cosom = from.x * to.x + from.y * to.y + from.z * to.z + from.w * to.w;
  148. if ( cosom < 0.0f ) {
  149. temp = -to;
  150. cosom = -cosom;
  151. } else {
  152. temp = to;
  153. }
  154. if ( ( 1.0f - cosom ) > 1e-6f ) {
  155. #if 0
  156. omega = acos( cosom );
  157. sinom = 1.0f / sin( omega );
  158. scale0 = sin( ( 1.0f - t ) * omega ) * sinom;
  159. scale1 = sin( t * omega ) * sinom;
  160. #else
  161. scale0 = 1.0f - cosom * cosom;
  162. sinom = idMath::InvSqrt( scale0 );
  163. omega = idMath::ATan16( scale0 * sinom, cosom );
  164. scale0 = idMath::Sin16( ( 1.0f - t ) * omega ) * sinom;
  165. scale1 = idMath::Sin16( t * omega ) * sinom;
  166. #endif
  167. } else {
  168. scale0 = 1.0f - t;
  169. scale1 = t;
  170. }
  171. *this = ( scale0 * from ) + ( scale1 * temp );
  172. return *this;
  173. }
  174. /*
  175. =============
  176. idCQuat::ToAngles
  177. =============
  178. */
  179. idAngles idCQuat::ToAngles( void ) const {
  180. return ToQuat().ToAngles();
  181. }
  182. /*
  183. =============
  184. idCQuat::ToRotation
  185. =============
  186. */
  187. idRotation idCQuat::ToRotation( void ) const {
  188. return ToQuat().ToRotation();
  189. }
  190. /*
  191. =============
  192. idCQuat::ToMat3
  193. =============
  194. */
  195. idMat3 idCQuat::ToMat3( void ) const {
  196. return ToQuat().ToMat3();
  197. }
  198. /*
  199. =============
  200. idCQuat::ToMat4
  201. =============
  202. */
  203. idMat4 idCQuat::ToMat4( void ) const {
  204. return ToQuat().ToMat4();
  205. }
  206. /*
  207. =============
  208. idCQuat::ToString
  209. =============
  210. */
  211. const char *idCQuat::ToString( int precision ) const {
  212. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  213. }