Quaternion.cpp 9.4 KB

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