Quaternion.cpp 10 KB

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