Quaternion.cpp 10 KB

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