Quaternion.cpp 8.5 KB

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