Quaternion.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #include "Base.h"
  2. #include "Quaternion.h"
  3. namespace gameplay
  4. {
  5. Quaternion::Quaternion()
  6. : x(0.0f), y(0.0f), z(0.0f), w(1.0f)
  7. {
  8. }
  9. Quaternion::Quaternion(float x, float y, float z, float w)
  10. {
  11. set(x, y, z, w);
  12. }
  13. Quaternion::Quaternion(float* array)
  14. {
  15. set(array);
  16. }
  17. Quaternion::Quaternion(const Matrix& m)
  18. {
  19. set(m);
  20. }
  21. Quaternion::Quaternion(const Vector3& axis, float angle)
  22. {
  23. set(axis, angle);
  24. }
  25. Quaternion::Quaternion(const Quaternion& copy)
  26. {
  27. set(copy);
  28. }
  29. Quaternion::~Quaternion()
  30. {
  31. }
  32. const Quaternion& Quaternion::identity()
  33. {
  34. static Quaternion value(0.0f, 0.0f, 0.0f, 1.0f);
  35. return value;
  36. }
  37. const Quaternion& Quaternion::zero()
  38. {
  39. static Quaternion value(0.0f, 0.0f, 0.0f, 0.0f);
  40. return value;
  41. }
  42. bool Quaternion::isIdentity() const
  43. {
  44. return x == 0.0f && y == 0.0f && z == 0.0f && w == 1.0f;
  45. }
  46. bool Quaternion::isZero() const
  47. {
  48. return x == 0.0f && y == 0.0f && z == 0.0f && z == 0.0f;
  49. }
  50. void Quaternion::createFromRotationMatrix(const Matrix& m, Quaternion* dst)
  51. {
  52. GP_ASSERT(dst);
  53. m.getRotation(dst);
  54. }
  55. void Quaternion::createFromAxisAngle(const Vector3& axis, float angle, Quaternion* dst)
  56. {
  57. GP_ASSERT(dst);
  58. float halfAngle = angle * 0.5f;
  59. float sinHalfAngle = sinf(halfAngle);
  60. Vector3 normal(axis);
  61. normal.normalize();
  62. dst->x = normal.x * sinHalfAngle;
  63. dst->y = normal.y * sinHalfAngle;
  64. dst->z = normal.z * sinHalfAngle;
  65. dst->w = cosf(halfAngle);
  66. }
  67. void Quaternion::conjugate()
  68. {
  69. conjugate(this);
  70. }
  71. void Quaternion::conjugate(Quaternion* dst) const
  72. {
  73. dst->x = -x;
  74. dst->y = -y;
  75. dst->z = -z;
  76. dst->w = w;
  77. }
  78. bool Quaternion::inverse()
  79. {
  80. return inverse(this);
  81. }
  82. bool Quaternion::inverse(Quaternion* dst) const
  83. {
  84. float n = x * x + y * y + z * z + w * w;
  85. if (n == 1.0f)
  86. {
  87. dst->x = -x;
  88. dst->y = -y;
  89. dst->z = -z;
  90. dst->w = w;
  91. return true;
  92. }
  93. // too close to zero
  94. if (n < 0.000001f)
  95. return false;
  96. n = 1.0f / n;
  97. dst->x = -x * n;
  98. dst->y = -y * n;
  99. dst->z = -z * n;
  100. dst->w = w * n;
  101. return true;
  102. }
  103. void Quaternion::multiply(const Quaternion& q)
  104. {
  105. multiply(*this, q, this);
  106. }
  107. void Quaternion::multiply(const Quaternion& q1, const Quaternion& q2, Quaternion* dst)
  108. {
  109. float x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
  110. float y = q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x;
  111. float z = q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w;
  112. float w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
  113. dst->x = x;
  114. dst->y = y;
  115. dst->z = z;
  116. dst->w = w;
  117. }
  118. void Quaternion::normalize()
  119. {
  120. normalize(this);
  121. }
  122. void Quaternion::normalize(Quaternion* dst) const
  123. {
  124. GP_ASSERT(dst);
  125. if (this != dst)
  126. {
  127. dst->x = x;
  128. dst->y = y;
  129. dst->z = z;
  130. dst->w = w;
  131. }
  132. float n = x * x + y * y + z * z + w * w;
  133. // Already normalized.
  134. if (n == 1.0f)
  135. return;
  136. n = sqrt(n);
  137. // Too close to zero.
  138. if (n < 0.000001f)
  139. return;
  140. n = 1.0f / n;
  141. dst->x *= n;
  142. dst->y *= n;
  143. dst->z *= n;
  144. dst->w *= n;
  145. }
  146. void Quaternion::set(float x, float y, float z, float w)
  147. {
  148. this->x = x;
  149. this->y = y;
  150. this->z = z;
  151. this->w = w;
  152. }
  153. void Quaternion::set(float* array)
  154. {
  155. GP_ASSERT(array);
  156. x = array[0];
  157. y = array[1];
  158. z = array[2];
  159. w = array[3];
  160. }
  161. void Quaternion::set(const Matrix& m)
  162. {
  163. Quaternion::createFromRotationMatrix(m, this);
  164. }
  165. void Quaternion::set(const Vector3& axis, float angle)
  166. {
  167. Quaternion::createFromAxisAngle(axis, angle, this);
  168. }
  169. void Quaternion::set(const Quaternion& q)
  170. {
  171. this->x = q.x;
  172. this->y = q.y;
  173. this->z = q.z;
  174. this->w = q.w;
  175. }
  176. void Quaternion::setIdentity()
  177. {
  178. x = 0.0f;
  179. y = 0.0f;
  180. z = 0.0f;
  181. w = 1.0f;
  182. }
  183. float Quaternion::toAxisAngle(Vector3* axis) const
  184. {
  185. GP_ASSERT(axis);
  186. Quaternion q(x, y, z, w);
  187. q.normalize();
  188. axis->x = q.x;
  189. axis->y = q.y;
  190. axis->z = q.z;
  191. axis->normalize();
  192. return (2.0f * acos(q.w));
  193. }
  194. void Quaternion::lerp(const Quaternion& q1, const Quaternion& q2, float t, Quaternion* dst)
  195. {
  196. GP_ASSERT(dst);
  197. GP_ASSERT(!(t < 0.0f || t > 1.0f));
  198. if (t == 0.0f)
  199. {
  200. memcpy(dst, &q1, sizeof(float) * 4);
  201. return;
  202. }
  203. else if (t == 1.0f)
  204. {
  205. memcpy(dst, &q2, sizeof(float) * 4);
  206. return;
  207. }
  208. float t1 = 1.0f - t;
  209. dst->x = t1 * q1.x + t * q2.x;
  210. dst->y = t1 * q1.y + t * q2.y;
  211. dst->z = t1 * q1.z + t * q2.z;
  212. dst->w = t1 * q1.w + t * q2.w;
  213. }
  214. void Quaternion::slerp(const Quaternion& q1, const Quaternion& q2, float t, Quaternion* dst)
  215. {
  216. slerp(q1.x, q1.y, q1.z, q1.w, q2.x, q2.y, q2.z, q2.w, t, &dst->x, &dst->y, &dst->z, &dst->w);
  217. }
  218. void Quaternion::squad(const Quaternion& q1, const Quaternion& q2, const Quaternion& s1, const Quaternion& s2, float t, Quaternion* dst)
  219. {
  220. GP_ASSERT(dst);
  221. GP_ASSERT(!(t < 0.0f || t > 1.0f));
  222. Quaternion dstQ(0.0f, 0.0f, 0.0f, 1.0f);
  223. Quaternion dstS(0.0f, 0.0f, 0.0f, 1.0f);
  224. slerpForSquad(q1, q2, t, &dstQ);
  225. slerpForSquad(s1, s2, t, &dstS);
  226. slerpForSquad(dstQ, dstS, 2.0f * t * (1.0f - t), dst);
  227. }
  228. void Quaternion::slerp(float q1x, float q1y, float q1z, float q1w, float q2x, float q2y, float q2z, float q2w, float t, float* dstx, float* dsty, float* dstz, float* dstw)
  229. {
  230. // Fast slerp implementation by kwhatmough:
  231. // It contains no division operations, no trig, no inverse trig
  232. // and no sqrt. Not only does this code tolerate small constraint
  233. // errors in the input quaternions, it actually corrects for them.
  234. GP_ASSERT(dstx && dsty && dstz && dstw);
  235. GP_ASSERT(!(t < 0.0f || t > 1.0f));
  236. if (t == 0.0f)
  237. {
  238. *dstx = q1x;
  239. *dsty = q1y;
  240. *dstz = q1z;
  241. *dstw = q1w;
  242. return;
  243. }
  244. else if (t == 1.0f)
  245. {
  246. *dstx = q2x;
  247. *dsty = q2y;
  248. *dstz = q2z;
  249. *dstw = q2w;
  250. return;
  251. }
  252. if (q1x == q2x && q1y == q2y && q1z == q2z && q1w == q2w)
  253. {
  254. *dstx = q1x;
  255. *dsty = q1y;
  256. *dstz = q1z;
  257. *dstw = q1w;
  258. return;
  259. }
  260. float halfY, alpha, beta;
  261. float u, f1, f2a, f2b;
  262. float ratio1, ratio2;
  263. float halfSecHalfTheta, versHalfTheta;
  264. float sqNotU, sqU;
  265. float cosTheta = q1w * q2w + q1x * q2x + q1y * q2y + q1z * q2z;
  266. // As usual in all slerp implementations, we fold theta.
  267. alpha = cosTheta >= 0 ? 1.0f : -1.0f;
  268. halfY = 1.0f + alpha * cosTheta;
  269. // Here we bisect the interval, so we need to fold t as well.
  270. f2b = t - 0.5f;
  271. u = f2b >= 0 ? f2b : -f2b;
  272. f2a = u - f2b;
  273. f2b += u;
  274. u += u;
  275. f1 = 1.0f - u;
  276. // One iteration of Newton to get 1-cos(theta / 2) to good accuracy.
  277. halfSecHalfTheta = 1.09f - (0.476537f - 0.0903321f * halfY) * halfY;
  278. halfSecHalfTheta *= 1.5f - halfY * halfSecHalfTheta * halfSecHalfTheta;
  279. versHalfTheta = 1.0f - halfY * halfSecHalfTheta;
  280. // Evaluate series expansions of the coefficients.
  281. sqNotU = f1 * f1;
  282. ratio2 = 0.0000440917108f * versHalfTheta;
  283. ratio1 = -0.00158730159f + (sqNotU - 16.0f) * ratio2;
  284. ratio1 = 0.0333333333f + ratio1 * (sqNotU - 9.0f) * versHalfTheta;
  285. ratio1 = -0.333333333f + ratio1 * (sqNotU - 4.0f) * versHalfTheta;
  286. ratio1 = 1.0f + ratio1 * (sqNotU - 1.0f) * versHalfTheta;
  287. sqU = u * u;
  288. ratio2 = -0.00158730159f + (sqU - 16.0f) * ratio2;
  289. ratio2 = 0.0333333333f + ratio2 * (sqU - 9.0f) * versHalfTheta;
  290. ratio2 = -0.333333333f + ratio2 * (sqU - 4.0f) * versHalfTheta;
  291. ratio2 = 1.0f + ratio2 * (sqU - 1.0f) * versHalfTheta;
  292. // Perform the bisection and resolve the folding done earlier.
  293. f1 *= ratio1 * halfSecHalfTheta;
  294. f2a *= ratio2;
  295. f2b *= ratio2;
  296. alpha *= f1 + f2a;
  297. beta = f1 + f2b;
  298. // Apply final coefficients to a and b as usual.
  299. float w = alpha * q1w + beta * q2w;
  300. float x = alpha * q1x + beta * q2x;
  301. float y = alpha * q1y + beta * q2y;
  302. float z = alpha * q1z + beta * q2z;
  303. // This final adjustment to the quaternion's length corrects for
  304. // any small constraint error in the inputs q1 and q2 But as you
  305. // can see, it comes at the cost of 9 additional multiplication
  306. // operations. If this error-correcting feature is not required,
  307. // the following code may be removed.
  308. f1 = 1.5f - 0.5f * (w * w + x * x + y * y + z * z);
  309. *dstw = w * f1;
  310. *dstx = x * f1;
  311. *dsty = y * f1;
  312. *dstz = z * f1;
  313. }
  314. void Quaternion::slerpForSquad(const Quaternion& q1, const Quaternion& q2, float t, Quaternion* dst)
  315. {
  316. // cos(omega) = q1 * q2;
  317. // slerp(q1, q2, t) = (q1*sin((1-t)*omega) + q2*sin(t*omega))/sin(omega);
  318. // q1 = +- q2, slerp(q1,q2,t) = q1.
  319. // This is a straight-foward implementation of the formula of slerp. It does not do any sign switching.
  320. float c = q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
  321. if (fabs(c) >= 1.0f)
  322. {
  323. dst->x = q1.x;
  324. dst->y = q1.y;
  325. dst->z = q1.z;
  326. dst->w = q1.w;
  327. return;
  328. }
  329. float omega = acos(c);
  330. float s = sqrt(1.0f - c * c);
  331. if (fabs(s) <= 0.00001f)
  332. {
  333. dst->x = q1.x;
  334. dst->y = q1.y;
  335. dst->z = q1.z;
  336. dst->w = q1.w;
  337. return;
  338. }
  339. float r1 = sin((1 - t) * omega) / s;
  340. float r2 = sin(t * omega) / s;
  341. dst->x = (q1.x * r1 + q2.x * r2);
  342. dst->y = (q1.y * r1 + q2.y * r2);
  343. dst->z = (q1.z * r1 + q2.z * r2);
  344. dst->w = (q1.w * r1 + q2.w * r2);
  345. }
  346. }