PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Smooth Drawing/libs/kazmath/src/quaternion.c

https://gitlab.com/Mr.Tomato/smooth-drawing
C | 582 lines | 326 code | 102 blank | 154 comment | 29 complexity | 5e426e219c6c70f1a47c5efec4567b9a MD5 | raw file
  1. /*
  2. Copyright (c) 2008, Luke Benstead.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  15. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  18. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  20. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <assert.h>
  23. #include <memory.h>
  24. #include "kazmath/utility.h"
  25. #include "kazmath/mat3.h"
  26. #include "kazmath/vec3.h"
  27. #include "kazmath/quaternion.h"
  28. ///< Returns pOut, sets pOut to the conjugate of pIn
  29. kmQuaternion* const kmQuaternionConjugate(kmQuaternion* pOut, const kmQuaternion* pIn)
  30. {
  31. pOut->x = -pIn->x;
  32. pOut->y = -pIn->y;
  33. pOut->z = -pIn->z;
  34. pOut->w = pIn->w;
  35. return pOut;
  36. }
  37. ///< Returns the dot product of the 2 quaternions
  38. const kmScalar kmQuaternionDot(const kmQuaternion* q1, const kmQuaternion* q2)
  39. {
  40. // A dot B = B dot A = AtBt + AxBx + AyBy + AzBz
  41. return (q1->w * q2->w +
  42. q1->x * q2->x +
  43. q1->y * q2->y +
  44. q1->z * q2->z);
  45. }
  46. ///< Returns the exponential of the quaternion
  47. kmQuaternion* kmQuaternionExp(kmQuaternion* pOut, const kmQuaternion* pIn)
  48. {
  49. assert(0);
  50. return pOut;
  51. }
  52. ///< Makes the passed quaternion an identity quaternion
  53. kmQuaternion* kmQuaternionIdentity(kmQuaternion* pOut)
  54. {
  55. pOut->x = 0.0;
  56. pOut->y = 0.0;
  57. pOut->z = 0.0;
  58. pOut->w = 1.0;
  59. return pOut;
  60. }
  61. ///< Returns the inverse of the passed Quaternion
  62. kmQuaternion* kmQuaternionInverse(kmQuaternion* pOut,
  63. const kmQuaternion* pIn)
  64. {
  65. kmScalar l = kmQuaternionLength(pIn);
  66. kmQuaternion tmp;
  67. if (fabs(l) > kmEpsilon)
  68. {
  69. pOut->x = 0.0;
  70. pOut->y = 0.0;
  71. pOut->z = 0.0;
  72. pOut->w = 0.0;
  73. return pOut;
  74. }
  75. ///Get the conjugute and divide by the length
  76. kmQuaternionScale(pOut,
  77. kmQuaternionConjugate(&tmp, pIn), 1.0f / l);
  78. return pOut;
  79. }
  80. ///< Returns true if the quaternion is an identity quaternion
  81. int kmQuaternionIsIdentity(const kmQuaternion* pIn)
  82. {
  83. return (pIn->x == 0.0 && pIn->y == 0.0 && pIn->z == 0.0 &&
  84. pIn->w == 1.0);
  85. }
  86. ///< Returns the length of the quaternion
  87. kmScalar kmQuaternionLength(const kmQuaternion* pIn)
  88. {
  89. return sqrtf(kmQuaternionLengthSq(pIn));
  90. }
  91. ///< Returns the length of the quaternion squared (prevents a sqrt)
  92. kmScalar kmQuaternionLengthSq(const kmQuaternion* pIn)
  93. {
  94. return pIn->x * pIn->x + pIn->y * pIn->y +
  95. pIn->z * pIn->z + pIn->w * pIn->w;
  96. }
  97. ///< Returns the natural logarithm
  98. kmQuaternion* kmQuaternionLn(kmQuaternion* pOut,
  99. const kmQuaternion* pIn)
  100. {
  101. /*
  102. A unit quaternion, is defined by:
  103. Q == (cos(theta), sin(theta) * v) where |v| = 1
  104. The natural logarithm of Q is, ln(Q) = (0, theta * v)
  105. */
  106. assert(0);
  107. return pOut;
  108. }
  109. ///< Multiplies 2 quaternions together
  110. extern
  111. kmQuaternion* kmQuaternionMultiply(kmQuaternion* pOut,
  112. const kmQuaternion* q1,
  113. const kmQuaternion* q2)
  114. {
  115. pOut->w = q1->w * q2->w - q1->x * q2->x - q1->y * q2->y - q1->z * q2->z;
  116. pOut->x = q1->w * q2->x + q1->x * q2->w + q1->y * q2->z - q1->z * q2->y;
  117. pOut->y = q1->w * q2->y + q1->y * q2->w + q1->z * q2->x - q1->x * q2->z;
  118. pOut->z = q1->w * q2->z + q1->z * q2->w + q1->x * q2->y - q1->y * q2->x;
  119. return pOut;
  120. }
  121. ///< Normalizes a quaternion
  122. kmQuaternion* kmQuaternionNormalize(kmQuaternion* pOut,
  123. const kmQuaternion* pIn)
  124. {
  125. kmScalar length = kmQuaternionLength(pIn);
  126. assert(fabs(length) > kmEpsilon);
  127. kmQuaternionScale(pOut, pIn, 1.0f / length);
  128. return pOut;
  129. }
  130. ///< Rotates a quaternion around an axis
  131. kmQuaternion* kmQuaternionRotationAxis(kmQuaternion* pOut,
  132. const kmVec3* pV,
  133. kmScalar angle)
  134. {
  135. kmScalar rad = angle * 0.5f;
  136. kmScalar scale = sinf(rad);
  137. pOut->w = cosf(rad);
  138. pOut->x = pV->x * scale;
  139. pOut->y = pV->y * scale;
  140. pOut->z = pV->z * scale;
  141. return pOut;
  142. }
  143. ///< Creates a quaternion from a rotation matrix
  144. kmQuaternion* kmQuaternionRotationMatrix(kmQuaternion* pOut,
  145. const kmMat3* pIn)
  146. {
  147. /*
  148. Note: The OpenGL matrices are transposed from the description below
  149. taken from the Matrix and Quaternion FAQ
  150. if ( mat[0] > mat[5] && mat[0] > mat[10] ) { // Column 0:
  151. S = sqrt( 1.0 + mat[0] - mat[5] - mat[10] ) * 2;
  152. X = 0.25 * S;
  153. Y = (mat[4] + mat[1] ) / S;
  154. Z = (mat[2] + mat[8] ) / S;
  155. W = (mat[9] - mat[6] ) / S;
  156. } else if ( mat[5] > mat[10] ) { // Column 1:
  157. S = sqrt( 1.0 + mat[5] - mat[0] - mat[10] ) * 2;
  158. X = (mat[4] + mat[1] ) / S;
  159. Y = 0.25 * S;
  160. Z = (mat[9] + mat[6] ) / S;
  161. W = (mat[2] - mat[8] ) / S;
  162. } else { // Column 2:
  163. S = sqrt( 1.0 + mat[10] - mat[0] - mat[5] ) * 2;
  164. X = (mat[2] + mat[8] ) / S;
  165. Y = (mat[9] + mat[6] ) / S;
  166. Z = 0.25 * S;
  167. W = (mat[4] - mat[1] ) / S;
  168. }
  169. */
  170. float x, y, z, w;
  171. float *pMatrix = NULL;
  172. float m4x4[16] = {0};
  173. float scale = 0.0f;
  174. float diagonal = 0.0f;
  175. if(!pIn) {
  176. return NULL;
  177. }
  178. /* 0 3 6
  179. 1 4 7
  180. 2 5 8
  181. 0 1 2 3
  182. 4 5 6 7
  183. 8 9 10 11
  184. 12 13 14 15*/
  185. m4x4[0] = pIn->mat[0];
  186. m4x4[1] = pIn->mat[3];
  187. m4x4[2] = pIn->mat[6];
  188. m4x4[4] = pIn->mat[1];
  189. m4x4[5] = pIn->mat[4];
  190. m4x4[6] = pIn->mat[7];
  191. m4x4[8] = pIn->mat[2];
  192. m4x4[9] = pIn->mat[5];
  193. m4x4[10] = pIn->mat[8];
  194. m4x4[15] = 1;
  195. pMatrix = &m4x4[0];
  196. diagonal = pMatrix[0] + pMatrix[5] + pMatrix[10] + 1;
  197. if(diagonal > kmEpsilon) {
  198. // Calculate the scale of the diagonal
  199. scale = (float)sqrt(diagonal ) * 2;
  200. // Calculate the x, y, x and w of the quaternion through the respective equation
  201. x = ( pMatrix[9] - pMatrix[6] ) / scale;
  202. y = ( pMatrix[2] - pMatrix[8] ) / scale;
  203. z = ( pMatrix[4] - pMatrix[1] ) / scale;
  204. w = 0.25f * scale;
  205. }
  206. else
  207. {
  208. // If the first element of the diagonal is the greatest value
  209. if ( pMatrix[0] > pMatrix[5] && pMatrix[0] > pMatrix[10] )
  210. {
  211. // Find the scale according to the first element, and double that value
  212. scale = (float)sqrt( 1.0f + pMatrix[0] - pMatrix[5] - pMatrix[10] ) * 2.0f;
  213. // Calculate the x, y, x and w of the quaternion through the respective equation
  214. x = 0.25f * scale;
  215. y = (pMatrix[4] + pMatrix[1] ) / scale;
  216. z = (pMatrix[2] + pMatrix[8] ) / scale;
  217. w = (pMatrix[9] - pMatrix[6] ) / scale;
  218. }
  219. // Else if the second element of the diagonal is the greatest value
  220. else if (pMatrix[5] > pMatrix[10])
  221. {
  222. // Find the scale according to the second element, and double that value
  223. scale = (float)sqrt( 1.0f + pMatrix[5] - pMatrix[0] - pMatrix[10] ) * 2.0f;
  224. // Calculate the x, y, x and w of the quaternion through the respective equation
  225. x = (pMatrix[4] + pMatrix[1] ) / scale;
  226. y = 0.25f * scale;
  227. z = (pMatrix[9] + pMatrix[6] ) / scale;
  228. w = (pMatrix[2] - pMatrix[8] ) / scale;
  229. }
  230. // Else the third element of the diagonal is the greatest value
  231. else
  232. {
  233. // Find the scale according to the third element, and double that value
  234. scale = (float)sqrt( 1.0f + pMatrix[10] - pMatrix[0] - pMatrix[5] ) * 2.0f;
  235. // Calculate the x, y, x and w of the quaternion through the respective equation
  236. x = (pMatrix[2] + pMatrix[8] ) / scale;
  237. y = (pMatrix[9] + pMatrix[6] ) / scale;
  238. z = 0.25f * scale;
  239. w = (pMatrix[4] - pMatrix[1] ) / scale;
  240. }
  241. }
  242. pOut->x = x;
  243. pOut->y = y;
  244. pOut->z = z;
  245. pOut->w = w;
  246. return pOut;
  247. #if 0
  248. kmScalar T = pIn->mat[0] + pIn->mat[5] + pIn->mat[10];
  249. if (T > kmEpsilon) {
  250. //If the trace is greater than zero we always use this calculation:
  251. /* S = sqrt(T) * 2;
  252. X = ( mat[9] - mat[6] ) / S;
  253. Y = ( mat[2] - mat[8] ) / S;
  254. Z = ( mat[4] - mat[1] ) / S;
  255. W = 0.25 * S;*/
  256. /* kmScalar s = sqrtf(T) * 2;
  257. pOut->x = (pIn->mat[9] - pIn->mat[6]) / s;
  258. pOut->y = (pIn->mat[8] - pIn->mat[2]) / s;
  259. pOut->z = (pIn->mat[1] - pIn->mat[4]) / s;
  260. pOut->w = 0.25f * s;
  261. kmQuaternionNormalize(pOut, pOut);
  262. return pOut;
  263. }
  264. //Otherwise the calculation depends on which major diagonal element has the greatest value.
  265. if (pIn->mat[0] > pIn->mat[5] && pIn->mat[0] > pIn->mat[10]) {
  266. kmScalar s = sqrtf(1 + pIn->mat[0] - pIn->mat[5] - pIn->mat[10]) * 2;
  267. pOut->x = 0.25f * s;
  268. pOut->y = (pIn->mat[1] + pIn->mat[4]) / s;
  269. pOut->z = (pIn->mat[8] + pIn->mat[2]) / s;
  270. pOut->w = (pIn->mat[9] - pIn->mat[6]) / s;
  271. }
  272. else if (pIn->mat[5] > pIn->mat[10]) {
  273. kmScalar s = sqrtf(1 + pIn->mat[5] - pIn->mat[0] - pIn->mat[10]) * 2;
  274. pOut->x = (pIn->mat[1] + pIn->mat[4]) / s;
  275. pOut->y = 0.25f * s;
  276. pOut->z = (pIn->mat[9] + pIn->mat[6]) / s;
  277. pOut->w = (pIn->mat[8] - pIn->mat[2]) / s;
  278. }
  279. else {
  280. kmScalar s = sqrt(1.0f + pIn->mat[10] - pIn->mat[0] - pIn->mat[5]) * 2.0f;
  281. pOut->x = (pIn->mat[8] + pIn->mat[2] ) / s;
  282. pOut->y = (pIn->mat[6] + pIn->mat[9] ) / s;
  283. pOut->z = 0.25f * s;
  284. pOut->w = (pIn->mat[1] - pIn->mat[4] ) / s;
  285. }
  286. kmQuaternionNormalize(pOut, pOut);
  287. return pOut;*/
  288. #endif // 0
  289. }
  290. ///< Create a quaternion from yaw, pitch and roll
  291. kmQuaternion* kmQuaternionRotationYawPitchRoll(kmQuaternion* pOut,
  292. kmScalar yaw,
  293. kmScalar pitch,
  294. kmScalar roll)
  295. {
  296. kmScalar ex, ey, ez; // temp half euler angles
  297. kmScalar cr, cp, cy, sr, sp, sy, cpcy, spsy; // temp vars in roll,pitch yaw
  298. ex = kmDegreesToRadians(pitch) / 2.0f; // convert to rads and half them
  299. ey = kmDegreesToRadians(yaw) / 2.0f;
  300. ez = kmDegreesToRadians(roll) / 2.0f;
  301. cr = cosf(ex);
  302. cp = cosf(ey);
  303. cy = cosf(ez);
  304. sr = sinf(ex);
  305. sp = sinf(ey);
  306. sy = sinf(ez);
  307. cpcy = cp * cy;
  308. spsy = sp * sy;
  309. pOut->w = cr * cpcy + sr * spsy;
  310. pOut->x = sr * cpcy - cr * spsy;
  311. pOut->y = cr * sp * cy + sr * cp * sy;
  312. pOut->z = cr * cp * sy - sr * sp * cy;
  313. kmQuaternionNormalize(pOut, pOut);
  314. return pOut;
  315. }
  316. ///< Interpolate between 2 quaternions
  317. kmQuaternion* kmQuaternionSlerp(kmQuaternion* pOut,
  318. const kmQuaternion* q1,
  319. const kmQuaternion* q2,
  320. kmScalar t)
  321. {
  322. /*float CosTheta = Q0.DotProd(Q1);
  323. float Theta = acosf(CosTheta);
  324. float SinTheta = sqrtf(1.0f-CosTheta*CosTheta);
  325. float Sin_T_Theta = sinf(T*Theta)/SinTheta;
  326. float Sin_OneMinusT_Theta = sinf((1.0f-T)*Theta)/SinTheta;
  327. Quaternion Result = Q0*Sin_OneMinusT_Theta;
  328. Result += (Q1*Sin_T_Theta);
  329. return Result;*/
  330. if (q1->x == q2->x &&
  331. q1->y == q2->y &&
  332. q1->z == q2->z &&
  333. q1->w == q2->w) {
  334. pOut->x = q1->x;
  335. pOut->y = q1->y;
  336. pOut->z = q1->z;
  337. pOut->w = q1->w;
  338. return pOut;
  339. }
  340. kmScalar ct = kmQuaternionDot(q1, q2);
  341. kmScalar theta = acosf(ct);
  342. kmScalar st = sqrtf(1.0 - kmSQR(ct));
  343. kmScalar stt = sinf(t * theta) / st;
  344. kmScalar somt = sinf((1.0 - t) * theta) / st;
  345. kmQuaternion temp, temp2;
  346. kmQuaternionScale(&temp, q1, somt);
  347. kmQuaternionScale(&temp2, q2, stt);
  348. kmQuaternionAdd(pOut, &temp, &temp2);
  349. return pOut;
  350. }
  351. ///< Get the axis and angle of rotation from a quaternion
  352. void kmQuaternionToAxisAngle(const kmQuaternion* pIn,
  353. kmVec3* pAxis,
  354. kmScalar* pAngle)
  355. {
  356. kmScalar tempAngle; // temp angle
  357. kmScalar scale; // temp vars
  358. tempAngle = acosf(pIn->w);
  359. scale = sqrtf(kmSQR(pIn->x) + kmSQR(pIn->y) + kmSQR(pIn->z));
  360. if (((scale > -kmEpsilon) && scale < kmEpsilon)
  361. || (scale < 2*kmPI + kmEpsilon && scale > 2*kmPI - kmEpsilon)) // angle is 0 or 360 so just simply set axis to 0,0,1 with angle 0
  362. {
  363. *pAngle = 0.0f;
  364. pAxis->x = 0.0f;
  365. pAxis->y = 0.0f;
  366. pAxis->z = 1.0f;
  367. }
  368. else
  369. {
  370. *pAngle = tempAngle * 2.0f; // angle in radians
  371. pAxis->x = pIn->x / scale;
  372. pAxis->y = pIn->y / scale;
  373. pAxis->z = pIn->z / scale;
  374. kmVec3Normalize(pAxis, pAxis);
  375. }
  376. }
  377. kmQuaternion* kmQuaternionScale(kmQuaternion* pOut,
  378. const kmQuaternion* pIn,
  379. kmScalar s)
  380. {
  381. pOut->x = pIn->x * s;
  382. pOut->y = pIn->y * s;
  383. pOut->z = pIn->z * s;
  384. pOut->w = pIn->w * s;
  385. return pOut;
  386. }
  387. kmQuaternion* kmQuaternionAssign(kmQuaternion* pOut, const kmQuaternion* pIn)
  388. {
  389. memcpy(pOut, pIn, sizeof(float) * 4);
  390. return pOut;
  391. }
  392. kmQuaternion* kmQuaternionAdd(kmQuaternion* pOut, const kmQuaternion* pQ1, const kmQuaternion* pQ2)
  393. {
  394. pOut->x = pQ1->x + pQ2->x;
  395. pOut->y = pQ1->y + pQ2->y;
  396. pOut->z = pQ1->z + pQ2->z;
  397. pOut->w = pQ1->w + pQ2->w;
  398. return pOut;
  399. }
  400. /** Adapted from the OGRE engine!
  401. Gets the shortest arc quaternion to rotate this vector to the destination
  402. vector.
  403. @remarks
  404. If you call this with a dest vector that is close to the inverse
  405. of this vector, we will rotate 180 degrees around the 'fallbackAxis'
  406. (if specified, or a generated axis if not) since in this case
  407. ANY axis of rotation is valid.
  408. */
  409. kmQuaternion* kmQuaternionRotationBetweenVec3(kmQuaternion* pOut, const kmVec3* vec1, const kmVec3* vec2, const kmVec3* fallback) {
  410. kmVec3 v1, v2;
  411. kmScalar a;
  412. kmVec3Assign(&v1, vec1);
  413. kmVec3Assign(&v2, vec2);
  414. kmVec3Normalize(&v1, &v1);
  415. kmVec3Normalize(&v2, &v2);
  416. a = kmVec3Dot(&v1, &v2);
  417. if (a >= 1.0) {
  418. kmQuaternionIdentity(pOut);
  419. return pOut;
  420. }
  421. if (a < (1e-6f - 1.0f)) {
  422. if (fabs(kmVec3LengthSq(fallback)) < kmEpsilon) {
  423. kmQuaternionRotationAxis(pOut, fallback, kmPI);
  424. } else {
  425. kmVec3 axis;
  426. kmVec3 X;
  427. X.x = 1.0;
  428. X.y = 0.0;
  429. X.z = 0.0;
  430. kmVec3Cross(&axis, &X, vec1);
  431. //If axis is zero
  432. if (fabs(kmVec3LengthSq(&axis)) < kmEpsilon) {
  433. kmVec3 Y;
  434. Y.x = 0.0;
  435. Y.y = 1.0;
  436. Y.z = 0.0;
  437. kmVec3Cross(&axis, &Y, vec1);
  438. }
  439. kmVec3Normalize(&axis, &axis);
  440. kmQuaternionRotationAxis(pOut, &axis, kmPI);
  441. }
  442. } else {
  443. kmScalar s = sqrtf((1+a) * 2);
  444. kmScalar invs = 1 / s;
  445. kmVec3 c;
  446. kmVec3Cross(&c, &v1, &v2);
  447. pOut->x = c.x * invs;
  448. pOut->y = c.y * invs;
  449. pOut->z = c.z * invs;
  450. pOut->w = s * 0.5f;
  451. kmQuaternionNormalize(pOut, pOut);
  452. }
  453. return pOut;
  454. }
  455. kmVec3* kmQuaternionMultiplyVec3(kmVec3* pOut, const kmQuaternion* q, const kmVec3* v) {
  456. kmVec3 uv, uuv, qvec;
  457. qvec.x = q->x;
  458. qvec.y = q->y;
  459. qvec.z = q->z;
  460. kmVec3Cross(&uv, &qvec, v);
  461. kmVec3Cross(&uuv, &qvec, &uv);
  462. kmVec3Scale(&uv, &uv, (2.0f * q->w));
  463. kmVec3Scale(&uuv, &uuv, 2.0f);
  464. kmVec3Add(pOut, v, &uv);
  465. kmVec3Add(pOut, pOut, &uuv);
  466. return pOut;
  467. }