mQuat.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mQuat.h"
  24. #include "math/mAngAxis.h"
  25. #include "math/mMatrix.h"
  26. #include "platform/profiler.h"
  27. const QuatF QuatF::Identity(0.0f,0.0f,0.0f,1.0f);
  28. QuatF& QuatF::set( const EulerF & e )
  29. {
  30. /*
  31. F32 cx, sx;
  32. F32 cy, sy;
  33. F32 cz, sz;
  34. mSinCos( -e.x * 0.5f, sx, cx );
  35. mSinCos( -e.y * 0.5f, sy, cy );
  36. mSinCos( -e.z * 0.5f, sz, cz );
  37. // Qyaw(z) = [ (0, 0, sin z/2), cos z/2 ]
  38. // Qpitch(x) = [ (sin x/2, 0, 0), cos x/2 ]
  39. // Qroll(y) = [ (0, sin y/2, 0), cos y/2 ]
  40. // this = Qresult = Qyaw*Qpitch*Qroll ZXY
  41. //
  42. // The code that folows is a simplification of:
  43. // roll*=pitch;
  44. // roll*=yaw;
  45. // *this = roll;
  46. F32 cycz, sysz, sycz, cysz;
  47. cycz = cy*cz;
  48. sysz = sy*sz;
  49. sycz = sy*cz;
  50. cysz = cy*sz;
  51. w = cycz*cx + sysz*sx;
  52. x = cycz*sx + sysz*cx;
  53. y = sycz*cx - cysz*sx;
  54. z = cysz*cx - sycz*sx;
  55. */
  56. // Assuming the angles are in radians.
  57. F32 c1 = mCos(e.y * 0.5f);
  58. F32 s1 = mSin(e.y * 0.5f);
  59. F32 c2 = mCos(e.z * 0.5f);
  60. F32 s2 = mSin(e.z * 0.5f);
  61. F32 c3 = mCos(e.x * 0.5f);
  62. F32 s3 = mSin(e.x * 0.5f);
  63. F32 c1c2 = c1*c2;
  64. F32 s1s2 = s1*s2;
  65. w =c1c2*c3 - s1s2*s3;
  66. x =c1c2*s3 + s1s2*c3;
  67. y =s1*c2*c3 + c1*s2*s3;
  68. z =c1*s2*c3 - s1*c2*s3;
  69. return *this;
  70. }
  71. QuatF& QuatF::operator *=( const QuatF & b )
  72. {
  73. QuatF prod;
  74. prod.w = w * b.w - x * b.x - y * b.y - z * b.z;
  75. prod.x = w * b.x + x * b.w + y * b.z - z * b.y;
  76. prod.y = w * b.y + y * b.w + z * b.x - x * b.z;
  77. prod.z = w * b.z + z * b.w + x * b.y - y * b.x;
  78. *this = prod;
  79. return (*this);
  80. }
  81. QuatF& QuatF::operator /=( const QuatF & c )
  82. {
  83. QuatF temp = c;
  84. return ( (*this) *= temp.inverse() );
  85. }
  86. QuatF& QuatF::square()
  87. {
  88. F32 t = w*2.0f;
  89. w = (w*w) - (x*x + y*y + z*z);
  90. x *= t;
  91. y *= t;
  92. z *= t;
  93. return *this;
  94. }
  95. QuatF& QuatF::inverse()
  96. {
  97. F32 magnitude = w*w + x*x + y*y + z*z;
  98. F32 invMagnitude;
  99. if( magnitude == 1.0f ) // special case unit quaternion
  100. {
  101. x = -x;
  102. y = -y;
  103. z = -z;
  104. }
  105. else // else scale
  106. {
  107. if( magnitude == 0.0f )
  108. invMagnitude = 1.0f;
  109. else
  110. invMagnitude = 1.0f / magnitude;
  111. w *= invMagnitude;
  112. x *= -invMagnitude;
  113. y *= -invMagnitude;
  114. z *= -invMagnitude;
  115. }
  116. return *this;
  117. }
  118. QuatF & QuatF::set( const AngAxisF & a )
  119. {
  120. return set( a.axis, a.angle );
  121. }
  122. QuatF & QuatF::set( const Point3F &axis, F32 angle )
  123. {
  124. PROFILE_SCOPE( QuatF_set_AngAxisF );
  125. F32 sinHalfAngle, cosHalfAngle;
  126. mSinCos( angle * 0.5f, sinHalfAngle, cosHalfAngle );
  127. x = axis.x * sinHalfAngle;
  128. y = axis.y * sinHalfAngle;
  129. z = axis.z * sinHalfAngle;
  130. w = cosHalfAngle;
  131. return *this;
  132. }
  133. QuatF & QuatF::normalize()
  134. {
  135. PROFILE_SCOPE( QuatF_normalize );
  136. F32 l = mSqrt( x*x + y*y + z*z + w*w );
  137. if( l == 0.0f )
  138. identity();
  139. else
  140. {
  141. x /= l;
  142. y /= l;
  143. z /= l;
  144. w /= l;
  145. }
  146. return *this;
  147. }
  148. #define idx(r,c) (r*4 + c)
  149. QuatF & QuatF::set( const MatrixF & mat )
  150. {
  151. PROFILE_SCOPE( QuatF_set_MatrixF );
  152. F32 const *m = mat;
  153. F32 trace = m[idx(0, 0)] + m[idx(1, 1)] + m[idx(2, 2)];
  154. if (trace > 0.0f)
  155. {
  156. F32 s = mSqrt(trace + F32(1));
  157. w = s * 0.5f;
  158. s = 0.5f / s;
  159. x = (m[idx(1,2)] - m[idx(2,1)]) * s;
  160. y = (m[idx(2,0)] - m[idx(0,2)]) * s;
  161. z = (m[idx(0,1)] - m[idx(1,0)]) * s;
  162. }
  163. else
  164. {
  165. F32* q = &x;
  166. U32 i = 0;
  167. if (m[idx(1, 1)] > m[idx(0, 0)]) i = 1;
  168. if (m[idx(2, 2)] > m[idx(i, i)]) i = 2;
  169. U32 j = (i + 1) % 3;
  170. U32 k = (j + 1) % 3;
  171. F32 s = mSqrt((m[idx(i, i)] - (m[idx(j, j)] + m[idx(k, k)])) + 1.0f);
  172. q[i] = s * 0.5f;
  173. s = 0.5f / s;
  174. q[j] = (m[idx(i,j)] + m[idx(j,i)]) * s;
  175. q[k] = (m[idx(i,k)] + m[idx(k, i)]) * s;
  176. w = (m[idx(j,k)] - m[idx(k, j)]) * s;
  177. }
  178. // Added to resolve issue #2230
  179. normalize();
  180. return *this;
  181. }
  182. MatrixF * QuatF::setMatrix( MatrixF * mat ) const
  183. {
  184. if( x*x + y*y + z*z < 10E-20f) // isIdentity() -- substituted code a little more stringent but a lot faster
  185. mat->identity();
  186. else
  187. m_quatF_set_matF( x, y, z, w, *mat );
  188. return mat;
  189. }
  190. QuatF & QuatF::slerp( const QuatF & q, F32 t )
  191. {
  192. return interpolate( *this, q, t );
  193. }
  194. QuatF & QuatF::extrapolate( const QuatF & q1, const QuatF & q2, F32 t )
  195. {
  196. // assert t >= 0 && t <= 1
  197. // q1 is value at time = 0
  198. // q2 is value at time = t
  199. // Computes quaternion at time = 1
  200. F64 flip,cos = q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
  201. if (cos < 0.0)
  202. {
  203. cos = -cos;
  204. flip = -1.0;
  205. }
  206. else
  207. flip = 1.0;
  208. F64 s1,s2;
  209. if ((1.0 - cos) > 0.00001)
  210. {
  211. F64 om = mAcos(cos) / t;
  212. F64 sd = 1.0 / mSin(t * om);
  213. s1 = flip * mSin(om) * sd;
  214. s2 = mSin((1.0 - t) * om) * sd;
  215. }
  216. else
  217. {
  218. // If quats are very close, do linear interpolation
  219. s1 = flip / t;
  220. s2 = (1.0 - t) / t;
  221. }
  222. x = F32(s1 * q2.x - s2 * q1.x);
  223. y = F32(s1 * q2.y - s2 * q1.y);
  224. z = F32(s1 * q2.z - s2 * q1.z);
  225. w = F32(s1 * q2.w - s2 * q1.w);
  226. return *this;
  227. }
  228. QuatF & QuatF::interpolate( const QuatF & q1, const QuatF & q2, F32 t )
  229. {
  230. //-----------------------------------
  231. // Calculate the cosine of the angle:
  232. double cosOmega = q1.dot( q2 );
  233. //-----------------------------------
  234. // adjust signs if necessary:
  235. F32 sign2;
  236. if ( cosOmega < 0.0 )
  237. {
  238. cosOmega = -cosOmega;
  239. sign2 = -1.0f;
  240. }
  241. else
  242. sign2 = 1.0f;
  243. //-----------------------------------
  244. // calculate interpolating coeffs:
  245. F64 scale1, scale2;
  246. if ( (1.0 - cosOmega) > 0.00001 )
  247. {
  248. // standard case
  249. F64 omega = mAcos(cosOmega);
  250. F64 sinOmega = mSin(omega);
  251. scale1 = mSin((1.0 - t) * omega) / sinOmega;
  252. scale2 = sign2 * mSin(t * omega) / sinOmega;
  253. }
  254. else
  255. {
  256. // if quats are very close, just do linear interpolation
  257. scale1 = 1.0 - t;
  258. scale2 = sign2 * t;
  259. }
  260. //-----------------------------------
  261. // actually do the interpolation:
  262. x = F32(scale1 * q1.x + scale2 * q2.x);
  263. y = F32(scale1 * q1.y + scale2 * q2.y);
  264. z = F32(scale1 * q1.z + scale2 * q2.z);
  265. w = F32(scale1 * q1.w + scale2 * q2.w);
  266. return *this;
  267. }
  268. Point3F & QuatF::mulP(const Point3F& p, Point3F* r) const
  269. {
  270. QuatF qq;
  271. QuatF qi = *this;
  272. QuatF qv( p.x, p.y, p.z, 0.0f);
  273. qi.inverse();
  274. qq.mul(qi, qv);
  275. qv.mul(qq, *this);
  276. r->set(qv.x, qv.y, qv.z);
  277. return *r;
  278. }
  279. QuatF & QuatF::mul( const QuatF &a, const QuatF &b)
  280. {
  281. AssertFatal( &a != this && &b != this, "QuatF::mul: dest should not be same as source" );
  282. w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
  283. x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
  284. y = a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z;
  285. z = a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x;
  286. return *this;
  287. }
  288. QuatF & QuatF::shortestArc( const VectorF &a, const VectorF &b )
  289. {
  290. // From Game Programming Gems pg. 217
  291. VectorF c = mCross( a, b );
  292. F32 d = mDot( a, b );
  293. F32 s = mSqrt( ( 1 + d ) * 2 );
  294. x = c.x / s;
  295. y = c.y / s;
  296. z = c.z / s;
  297. w = s / 2.f;
  298. return *this;
  299. }