Quat.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../precompiled.h"
  22. /*
  23. =====================
  24. idQuat::ToAngles
  25. =====================
  26. */
  27. idAngles idQuat::ToAngles() const {
  28. return ToMat3().ToAngles();
  29. }
  30. /*
  31. =====================
  32. idQuat::ToRotation
  33. =====================
  34. */
  35. idRotation idQuat::ToRotation() 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() 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() const {
  92. return ToMat3().ToMat4();
  93. }
  94. /*
  95. =====================
  96. idQuat::ToCQuat
  97. =====================
  98. */
  99. idCQuat idQuat::ToCQuat() 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() 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. idQuat::Lerp
  177. Approximation of spherical linear interpolation between two quaternions. The interpolation
  178. traces out the exact same curve as Slerp but does not maintain a constant speed across the arc.
  179. ========================
  180. */
  181. idQuat &idQuat::Lerp( const idQuat &from, const idQuat &to, const float t ) {
  182. if ( t <= 0.0f ) {
  183. *this = from;
  184. return *this;
  185. }
  186. if ( t >= 1.0f ) {
  187. *this = to;
  188. return *this;
  189. }
  190. if ( from == to ) {
  191. *this = to;
  192. return *this;
  193. }
  194. float cosom = from.x * to.x + from.y * to.y + from.z * to.z + from.w * to.w;
  195. float scale0 = 1.0f - t;
  196. float scale1 = ( cosom >= 0.0f ) ? t : -t;
  197. x = scale0 * from.x + scale1 * to.x;
  198. y = scale0 * from.y + scale1 * to.y;
  199. z = scale0 * from.z + scale1 * to.z;
  200. w = scale0 * from.w + scale1 * to.w;
  201. float s = idMath::InvSqrt( x * x + y * y + z * z + w * w );
  202. x *= s;
  203. y *= s;
  204. z *= s;
  205. w *= s;
  206. return *this;
  207. }
  208. /*
  209. =============
  210. idCQuat::ToAngles
  211. =============
  212. */
  213. idAngles idCQuat::ToAngles() const {
  214. return ToQuat().ToAngles();
  215. }
  216. /*
  217. =============
  218. idCQuat::ToRotation
  219. =============
  220. */
  221. idRotation idCQuat::ToRotation() const {
  222. return ToQuat().ToRotation();
  223. }
  224. /*
  225. =============
  226. idCQuat::ToMat3
  227. =============
  228. */
  229. idMat3 idCQuat::ToMat3() const {
  230. return ToQuat().ToMat3();
  231. }
  232. /*
  233. =============
  234. idCQuat::ToMat4
  235. =============
  236. */
  237. idMat4 idCQuat::ToMat4() const {
  238. return ToQuat().ToMat4();
  239. }
  240. /*
  241. =============
  242. idCQuat::ToString
  243. =============
  244. */
  245. const char *idCQuat::ToString( int precision ) const {
  246. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  247. }
  248. /*
  249. =====================
  250. Slerp
  251. Spherical linear interpolation between two quaternions.
  252. =====================
  253. */
  254. idQuat Slerp( const idQuat & from, const idQuat & to, const float t ) {
  255. return idQuat().Slerp( from, to, t );
  256. }