Quaternion.cpp 11 KB

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