Quaternion.cpp 9.2 KB

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