Quaternion.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. assert(dst);
  53. m.getRotation(dst);
  54. }
  55. void Quaternion::createFromAxisAngle(const Vector3& axis, float angle, Quaternion* dst)
  56. {
  57. 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. 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. 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. 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. assert(dst);
  197. 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. // Fast slerp implementation by kwhatmough:
  217. // It contains no division operations, no trig, no inverse trig
  218. // and no sqrt. Not only does this code tolerate small constraint
  219. // errors in the input quaternions, it actually corrects for them.
  220. assert(dst);
  221. assert(!(t < 0.0f || t > 1.0f));
  222. if (t == 0.0f)
  223. {
  224. memcpy(dst, &q1, sizeof(float) * 4);
  225. return;
  226. }
  227. else if (t == 1.0f)
  228. {
  229. memcpy(dst, &q2, sizeof(float) * 4);
  230. return;
  231. }
  232. if (q1.x == q2.x && q1.y == q2.y && q1.z == q2.z && q1.w == q2.w)
  233. {
  234. memcpy(dst, &q1, sizeof(float) * 4);
  235. return;
  236. }
  237. float halfY, alpha, beta;
  238. float u, f1, f2a, f2b;
  239. float ratio1, ratio2;
  240. float halfSecHalfTheta, versHalfTheta;
  241. float sqNotU, sqU;
  242. float cosTheta = q1.w * q2.w + q1.x * q2.x + q1.y * q2.y + q1.z * q2.z;
  243. // As usual in all slerp implementations, we fold theta.
  244. alpha = cosTheta >= 0 ? 1.0f : -1.0f;
  245. halfY = 1.0f + alpha * cosTheta;
  246. // Here we bisect the interval, so we need to fold t as well.
  247. f2b = t - 0.5f;
  248. u = f2b >= 0 ? f2b : -f2b;
  249. f2a = u - f2b;
  250. f2b += u;
  251. u += u;
  252. f1 = 1.0f - u;
  253. // One iteration of Newton to get 1-cos(theta / 2) to good accuracy.
  254. halfSecHalfTheta = 1.09f - (0.476537f - 0.0903321f * halfY) * halfY;
  255. halfSecHalfTheta *= 1.5f - halfY * halfSecHalfTheta * halfSecHalfTheta;
  256. versHalfTheta = 1.0f - halfY * halfSecHalfTheta;
  257. // Evaluate series expansions of the coefficients.
  258. sqNotU = f1 * f1;
  259. ratio2 = 0.0000440917108f * versHalfTheta;
  260. ratio1 = -0.00158730159f + (sqNotU - 16.0f) * ratio2;
  261. ratio1 = 0.0333333333f + ratio1 * (sqNotU - 9.0f) * versHalfTheta;
  262. ratio1 = -0.333333333f + ratio1 * (sqNotU - 4.0f) * versHalfTheta;
  263. ratio1 = 1.0f + ratio1 * (sqNotU - 1.0f) * versHalfTheta;
  264. sqU = u * u;
  265. ratio2 = -0.00158730159f + (sqU - 16.0f) * ratio2;
  266. ratio2 = 0.0333333333f + ratio2 * (sqU - 9.0f) * versHalfTheta;
  267. ratio2 = -0.333333333f + ratio2 * (sqU - 4.0f) * versHalfTheta;
  268. ratio2 = 1.0f + ratio2 * (sqU - 1.0f) * versHalfTheta;
  269. // Perform the bisection and resolve the folding done earlier.
  270. f1 *= ratio1 * halfSecHalfTheta;
  271. f2a *= ratio2;
  272. f2b *= ratio2;
  273. alpha *= f1 + f2a;
  274. beta = f1 + f2b;
  275. // Apply final coefficients to a and b as usual.
  276. float w = alpha * q1.w + beta * q2.w;
  277. float x = alpha * q1.x + beta * q2.x;
  278. float y = alpha * q1.y + beta * q2.y;
  279. float z = alpha * q1.z + beta * q2.z;
  280. // This final adjustment to the quaternion's length corrects for
  281. // any small constraint error in the inputs q1 and q2. But as you
  282. // can see, it comes at the cost of 9 additional multiplication
  283. // operations. If this error-correcting feature is not required,
  284. // the following code may be removed.
  285. f1 = 1.5f - 0.5f * (w * w + x * x + y * y + z * z);
  286. dst->w = w * f1;
  287. dst->x = x * f1;
  288. dst->y = y * f1;
  289. dst->z = z * f1;
  290. }
  291. void Quaternion::squad(const Quaternion& q1, const Quaternion& q2, const Quaternion& s1, const Quaternion& s2, float t, Quaternion* dst)
  292. {
  293. assert(dst);
  294. assert(!(t < 0.0f || t > 1.0f));
  295. Quaternion dstQ(0.0f, 0.0f, 0.0f, 1.0f);
  296. Quaternion dstS(0.0f, 0.0f, 0.0f, 1.0f);
  297. slerpForSquad(q1, q2, t, &dstQ);
  298. slerpForSquad(s1, s2, t, &dstS);
  299. slerpForSquad(dstQ, dstS, 2.0f * t * (1.0f - t), dst);
  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. }