quat2.js.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: quat2.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: quat2.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import * as glMatrix from "./common.js";
  20. import * as quat from "./quat.js";
  21. import * as mat4 from "./mat4.js";
  22. /**
  23. * Dual Quaternion&lt;br>
  24. * Format: [real, dual]&lt;br>
  25. * Quaternion format: XYZW&lt;br>
  26. * Make sure to have normalized dual quaternions, otherwise the functions may not work as intended.&lt;br>
  27. * @module quat2
  28. */
  29. /**
  30. * Creates a new identity dual quat
  31. *
  32. * @returns {quat2} a new dual quaternion [real -> rotation, dual -> translation]
  33. */
  34. export function create() {
  35. let dq = new glMatrix.ARRAY_TYPE(8);
  36. if(glMatrix.ARRAY_TYPE != Float32Array) {
  37. dq[0] = 0;
  38. dq[1] = 0;
  39. dq[2] = 0;
  40. dq[4] = 0;
  41. dq[5] = 0;
  42. dq[6] = 0;
  43. dq[7] = 0;
  44. }
  45. dq[3] = 1;
  46. return dq;
  47. }
  48. /**
  49. * Creates a new quat initialized with values from an existing quaternion
  50. *
  51. * @param {quat2} a dual quaternion to clone
  52. * @returns {quat2} new dual quaternion
  53. * @function
  54. */
  55. export function clone(a) {
  56. let dq = new glMatrix.ARRAY_TYPE(8);
  57. dq[0] = a[0];
  58. dq[1] = a[1];
  59. dq[2] = a[2];
  60. dq[3] = a[3];
  61. dq[4] = a[4];
  62. dq[5] = a[5];
  63. dq[6] = a[6];
  64. dq[7] = a[7];
  65. return dq;
  66. }
  67. /**
  68. * Creates a new dual quat initialized with the given values
  69. *
  70. * @param {Number} x1 X component
  71. * @param {Number} y1 Y component
  72. * @param {Number} z1 Z component
  73. * @param {Number} w1 W component
  74. * @param {Number} x2 X component
  75. * @param {Number} y2 Y component
  76. * @param {Number} z2 Z component
  77. * @param {Number} w2 W component
  78. * @returns {quat2} new dual quaternion
  79. * @function
  80. */
  81. export function fromValues(x1, y1, z1, w1, x2, y2, z2, w2) {
  82. let dq = new glMatrix.ARRAY_TYPE(8);
  83. dq[0] = x1;
  84. dq[1] = y1;
  85. dq[2] = z1;
  86. dq[3] = w1;
  87. dq[4] = x2;
  88. dq[5] = y2;
  89. dq[6] = z2;
  90. dq[7] = w2;
  91. return dq;
  92. }
  93. /**
  94. * Creates a new dual quat from the given values (quat and translation)
  95. *
  96. * @param {Number} x1 X component
  97. * @param {Number} y1 Y component
  98. * @param {Number} z1 Z component
  99. * @param {Number} w1 W component
  100. * @param {Number} x2 X component (translation)
  101. * @param {Number} y2 Y component (translation)
  102. * @param {Number} z2 Z component (translation)
  103. * @returns {quat2} new dual quaternion
  104. * @function
  105. */
  106. export function fromRotationTranslationValues(x1, y1, z1, w1, x2, y2, z2) {
  107. let dq = new glMatrix.ARRAY_TYPE(8);
  108. dq[0] = x1;
  109. dq[1] = y1;
  110. dq[2] = z1;
  111. dq[3] = w1;
  112. let ax = x2 * 0.5,
  113. ay = y2 * 0.5,
  114. az = z2 * 0.5;
  115. dq[4] = ax * w1 + ay * z1 - az * y1;
  116. dq[5] = ay * w1 + az * x1 - ax * z1;
  117. dq[6] = az * w1 + ax * y1 - ay * x1;
  118. dq[7] = -ax * x1 - ay * y1 - az * z1;
  119. return dq;
  120. }
  121. /**
  122. * Creates a dual quat from a quaternion and a translation
  123. *
  124. * @param {quat2} dual quaternion receiving operation result
  125. * @param {quat} q quaternion
  126. * @param {vec3} t tranlation vector
  127. * @returns {quat2} dual quaternion receiving operation result
  128. * @function
  129. */
  130. export function fromRotationTranslation(out, q, t) {
  131. let ax = t[0] * 0.5,
  132. ay = t[1] * 0.5,
  133. az = t[2] * 0.5,
  134. bx = q[0],
  135. by = q[1],
  136. bz = q[2],
  137. bw = q[3];
  138. out[0] = bx;
  139. out[1] = by;
  140. out[2] = bz;
  141. out[3] = bw;
  142. out[4] = ax * bw + ay * bz - az * by;
  143. out[5] = ay * bw + az * bx - ax * bz;
  144. out[6] = az * bw + ax * by - ay * bx;
  145. out[7] = -ax * bx - ay * by - az * bz;
  146. return out;
  147. }
  148. /**
  149. * Creates a dual quat from a translation
  150. *
  151. * @param {quat2} dual quaternion receiving operation result
  152. * @param {vec3} t translation vector
  153. * @returns {quat2} dual quaternion receiving operation result
  154. * @function
  155. */
  156. export function fromTranslation(out, t) {
  157. out[0] = 0;
  158. out[1] = 0;
  159. out[2] = 0;
  160. out[3] = 1;
  161. out[4] = t[0] * 0.5;
  162. out[5] = t[1] * 0.5;
  163. out[6] = t[2] * 0.5;
  164. out[7] = 0;
  165. return out;
  166. }
  167. /**
  168. * Creates a dual quat from a quaternion
  169. *
  170. * @param {quat2} dual quaternion receiving operation result
  171. * @param {quat} q the quaternion
  172. * @returns {quat2} dual quaternion receiving operation result
  173. * @function
  174. */
  175. export function fromRotation(out, q) {
  176. out[0] = q[0];
  177. out[1] = q[1];
  178. out[2] = q[2];
  179. out[3] = q[3];
  180. out[4] = 0;
  181. out[5] = 0;
  182. out[6] = 0;
  183. out[7] = 0;
  184. return out;
  185. }
  186. /**
  187. * Creates a new dual quat from a matrix (4x4)
  188. *
  189. * @param {quat2} out the dual quaternion
  190. * @param {mat4} a the matrix
  191. * @returns {quat2} dual quat receiving operation result
  192. * @function
  193. */
  194. export function fromMat4(out, a) {
  195. //TODO Optimize this
  196. let outer = quat.create();
  197. mat4.getRotation(outer, a);
  198. let t = new glMatrix.ARRAY_TYPE(3);
  199. mat4.getTranslation(t, a);
  200. fromRotationTranslation(out, outer, t);
  201. return out;
  202. }
  203. /**
  204. * Copy the values from one dual quat to another
  205. *
  206. * @param {quat2} out the receiving dual quaternion
  207. * @param {quat2} a the source dual quaternion
  208. * @returns {quat2} out
  209. * @function
  210. */
  211. export function copy(out, a) {
  212. out[0] = a[0];
  213. out[1] = a[1];
  214. out[2] = a[2];
  215. out[3] = a[3];
  216. out[4] = a[4];
  217. out[5] = a[5];
  218. out[6] = a[6];
  219. out[7] = a[7];
  220. return out;
  221. }
  222. /**
  223. * Set a dual quat to the identity dual quaternion
  224. *
  225. * @param {quat2} out the receiving quaternion
  226. * @returns {quat2} out
  227. */
  228. export function identity(out) {
  229. out[0] = 0;
  230. out[1] = 0;
  231. out[2] = 0;
  232. out[3] = 1;
  233. out[4] = 0;
  234. out[5] = 0;
  235. out[6] = 0;
  236. out[7] = 0;
  237. return out;
  238. }
  239. /**
  240. * Set the components of a dual quat to the given values
  241. *
  242. * @param {quat2} out the receiving quaternion
  243. * @param {Number} x1 X component
  244. * @param {Number} y1 Y component
  245. * @param {Number} z1 Z component
  246. * @param {Number} w1 W component
  247. * @param {Number} x2 X component
  248. * @param {Number} y2 Y component
  249. * @param {Number} z2 Z component
  250. * @param {Number} w2 W component
  251. * @returns {quat2} out
  252. * @function
  253. */
  254. export function set(out, x1, y1, z1, w1, x2, y2, z2, w2) {
  255. out[0] = x1;
  256. out[1] = y1;
  257. out[2] = z1;
  258. out[3] = w1;
  259. out[4] = x2;
  260. out[5] = y2;
  261. out[6] = z2;
  262. out[7] = w2;
  263. return out;
  264. }
  265. /**
  266. * Gets the real part of a dual quat
  267. * @param {quat} out real part
  268. * @param {quat2} a Dual Quaternion
  269. * @return {quat} real part
  270. */
  271. export const getReal = quat.copy;
  272. /**
  273. * Gets the dual part of a dual quat
  274. * @param {quat} out dual part
  275. * @param {quat2} a Dual Quaternion
  276. * @return {quat} dual part
  277. */
  278. export function getDual(out, a) {
  279. out[0] = a[4];
  280. out[1] = a[5];
  281. out[2] = a[6];
  282. out[3] = a[7];
  283. return out;
  284. }
  285. /**
  286. * Set the real component of a dual quat to the given quaternion
  287. *
  288. * @param {quat2} out the receiving quaternion
  289. * @param {quat} q a quaternion representing the real part
  290. * @returns {quat2} out
  291. * @function
  292. */
  293. export const setReal = quat.copy;
  294. /**
  295. * Set the dual component of a dual quat to the given quaternion
  296. *
  297. * @param {quat2} out the receiving quaternion
  298. * @param {quat} q a quaternion representing the dual part
  299. * @returns {quat2} out
  300. * @function
  301. */
  302. export function setDual(out, q) {
  303. out[4] = q[0];
  304. out[5] = q[1];
  305. out[6] = q[2];
  306. out[7] = q[3];
  307. return out;
  308. }
  309. /**
  310. * Gets the translation of a normalized dual quat
  311. * @param {vec3} out translation
  312. * @param {quat2} a Dual Quaternion to be decomposed
  313. * @return {vec3} translation
  314. */
  315. export function getTranslation(out, a) {
  316. let ax = a[4],
  317. ay = a[5],
  318. az = a[6],
  319. aw = a[7],
  320. bx = -a[0],
  321. by = -a[1],
  322. bz = -a[2],
  323. bw = a[3];
  324. out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
  325. out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
  326. out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
  327. return out;
  328. }
  329. /**
  330. * Translates a dual quat by the given vector
  331. *
  332. * @param {quat2} out the receiving dual quaternion
  333. * @param {quat2} a the dual quaternion to translate
  334. * @param {vec3} v vector to translate by
  335. * @returns {quat2} out
  336. */
  337. export function translate(out, a, v) {
  338. let ax1 = a[0],
  339. ay1 = a[1],
  340. az1 = a[2],
  341. aw1 = a[3],
  342. bx1 = v[0] * 0.5,
  343. by1 = v[1] * 0.5,
  344. bz1 = v[2] * 0.5,
  345. ax2 = a[4],
  346. ay2 = a[5],
  347. az2 = a[6],
  348. aw2 = a[7];
  349. out[0] = ax1;
  350. out[1] = ay1;
  351. out[2] = az1;
  352. out[3] = aw1;
  353. out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2;
  354. out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2;
  355. out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2;
  356. out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2;
  357. return out;
  358. }
  359. /**
  360. * Rotates a dual quat around the X axis
  361. *
  362. * @param {quat2} out the receiving dual quaternion
  363. * @param {quat2} a the dual quaternion to rotate
  364. * @param {number} rad how far should the rotation be
  365. * @returns {quat2} out
  366. */
  367. export function rotateX(out, a, rad) {
  368. let bx = -a[0],
  369. by = -a[1],
  370. bz = -a[2],
  371. bw = a[3],
  372. ax = a[4],
  373. ay = a[5],
  374. az = a[6],
  375. aw = a[7],
  376. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  377. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  378. az1 = az * bw + aw * bz + ax * by - ay * bx,
  379. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  380. quat.rotateX(out, a, rad);
  381. bx = out[0];
  382. by = out[1];
  383. bz = out[2];
  384. bw = out[3];
  385. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  386. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  387. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  388. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  389. return out;
  390. }
  391. /**
  392. * Rotates a dual quat around the Y axis
  393. *
  394. * @param {quat2} out the receiving dual quaternion
  395. * @param {quat2} a the dual quaternion to rotate
  396. * @param {number} rad how far should the rotation be
  397. * @returns {quat2} out
  398. */
  399. export function rotateY(out, a, rad) {
  400. let bx = -a[0],
  401. by = -a[1],
  402. bz = -a[2],
  403. bw = a[3],
  404. ax = a[4],
  405. ay = a[5],
  406. az = a[6],
  407. aw = a[7],
  408. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  409. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  410. az1 = az * bw + aw * bz + ax * by - ay * bx,
  411. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  412. quat.rotateY(out, a, rad);
  413. bx = out[0];
  414. by = out[1];
  415. bz = out[2];
  416. bw = out[3];
  417. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  418. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  419. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  420. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  421. return out;
  422. }
  423. /**
  424. * Rotates a dual quat around the Z axis
  425. *
  426. * @param {quat2} out the receiving dual quaternion
  427. * @param {quat2} a the dual quaternion to rotate
  428. * @param {number} rad how far should the rotation be
  429. * @returns {quat2} out
  430. */
  431. export function rotateZ(out, a, rad) {
  432. let bx = -a[0],
  433. by = -a[1],
  434. bz = -a[2],
  435. bw = a[3],
  436. ax = a[4],
  437. ay = a[5],
  438. az = a[6],
  439. aw = a[7],
  440. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  441. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  442. az1 = az * bw + aw * bz + ax * by - ay * bx,
  443. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  444. quat.rotateZ(out, a, rad);
  445. bx = out[0];
  446. by = out[1];
  447. bz = out[2];
  448. bw = out[3];
  449. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  450. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  451. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  452. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  453. return out;
  454. }
  455. /**
  456. * Rotates a dual quat by a given quaternion (a * q)
  457. *
  458. * @param {quat2} out the receiving dual quaternion
  459. * @param {quat2} a the dual quaternion to rotate
  460. * @param {quat} q quaternion to rotate by
  461. * @returns {quat2} out
  462. */
  463. export function rotateByQuatAppend(out, a, q) {
  464. let qx = q[0],
  465. qy = q[1],
  466. qz = q[2],
  467. qw = q[3],
  468. ax = a[0],
  469. ay = a[1],
  470. az = a[2],
  471. aw = a[3];
  472. out[0] = ax * qw + aw * qx + ay * qz - az * qy;
  473. out[1] = ay * qw + aw * qy + az * qx - ax * qz;
  474. out[2] = az * qw + aw * qz + ax * qy - ay * qx;
  475. out[3] = aw * qw - ax * qx - ay * qy - az * qz;
  476. ax = a[4];
  477. ay = a[5];
  478. az = a[6];
  479. aw = a[7];
  480. out[4] = ax * qw + aw * qx + ay * qz - az * qy;
  481. out[5] = ay * qw + aw * qy + az * qx - ax * qz;
  482. out[6] = az * qw + aw * qz + ax * qy - ay * qx;
  483. out[7] = aw * qw - ax * qx - ay * qy - az * qz;
  484. return out;
  485. }
  486. /**
  487. * Rotates a dual quat by a given quaternion (q * a)
  488. *
  489. * @param {quat2} out the receiving dual quaternion
  490. * @param {quat} q quaternion to rotate by
  491. * @param {quat2} a the dual quaternion to rotate
  492. * @returns {quat2} out
  493. */
  494. export function rotateByQuatPrepend(out, q, a) {
  495. let qx = q[0],
  496. qy = q[1],
  497. qz = q[2],
  498. qw = q[3],
  499. bx = a[0],
  500. by = a[1],
  501. bz = a[2],
  502. bw = a[3];
  503. out[0] = qx * bw + qw * bx + qy * bz - qz * by;
  504. out[1] = qy * bw + qw * by + qz * bx - qx * bz;
  505. out[2] = qz * bw + qw * bz + qx * by - qy * bx;
  506. out[3] = qw * bw - qx * bx - qy * by - qz * bz;
  507. bx = a[4];
  508. by = a[5];
  509. bz = a[6];
  510. bw = a[7];
  511. out[4] = qx * bw + qw * bx + qy * bz - qz * by;
  512. out[5] = qy * bw + qw * by + qz * bx - qx * bz;
  513. out[6] = qz * bw + qw * bz + qx * by - qy * bx;
  514. out[7] = qw * bw - qx * bx - qy * by - qz * bz;
  515. return out;
  516. }
  517. /**
  518. * Rotates a dual quat around a given axis. Does the normalisation automatically
  519. *
  520. * @param {quat2} out the receiving dual quaternion
  521. * @param {quat2} a the dual quaternion to rotate
  522. * @param {vec3} axis the axis to rotate around
  523. * @param {Number} rad how far the rotation should be
  524. * @returns {quat2} out
  525. */
  526. export function rotateAroundAxis(out, a, axis, rad) {
  527. //Special case for rad = 0
  528. if (Math.abs(rad) &lt; glMatrix.EPSILON) {
  529. return copy(out, a);
  530. }
  531. let axisLength = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
  532. rad = rad * 0.5;
  533. let s = Math.sin(rad);
  534. let bx = s * axis[0] / axisLength;
  535. let by = s * axis[1] / axisLength;
  536. let bz = s * axis[2] / axisLength;
  537. let bw = Math.cos(rad);
  538. let ax1 = a[0],
  539. ay1 = a[1],
  540. az1 = a[2],
  541. aw1 = a[3];
  542. out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  543. out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  544. out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  545. out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  546. let ax = a[4],
  547. ay = a[5],
  548. az = a[6],
  549. aw = a[7];
  550. out[4] = ax * bw + aw * bx + ay * bz - az * by;
  551. out[5] = ay * bw + aw * by + az * bx - ax * bz;
  552. out[6] = az * bw + aw * bz + ax * by - ay * bx;
  553. out[7] = aw * bw - ax * bx - ay * by - az * bz;
  554. return out;
  555. }
  556. /**
  557. * Adds two dual quat's
  558. *
  559. * @param {quat2} out the receiving dual quaternion
  560. * @param {quat2} a the first operand
  561. * @param {quat2} b the second operand
  562. * @returns {quat2} out
  563. * @function
  564. */
  565. export function add(out, a, b) {
  566. out[0] = a[0] + b[0];
  567. out[1] = a[1] + b[1];
  568. out[2] = a[2] + b[2];
  569. out[3] = a[3] + b[3];
  570. out[4] = a[4] + b[4];
  571. out[5] = a[5] + b[5];
  572. out[6] = a[6] + b[6];
  573. out[7] = a[7] + b[7];
  574. return out;
  575. }
  576. /**
  577. * Multiplies two dual quat's
  578. *
  579. * @param {quat2} out the receiving dual quaternion
  580. * @param {quat2} a the first operand
  581. * @param {quat2} b the second operand
  582. * @returns {quat2} out
  583. */
  584. export function multiply(out, a, b) {
  585. let ax0 = a[0],
  586. ay0 = a[1],
  587. az0 = a[2],
  588. aw0 = a[3],
  589. bx1 = b[4],
  590. by1 = b[5],
  591. bz1 = b[6],
  592. bw1 = b[7],
  593. ax1 = a[4],
  594. ay1 = a[5],
  595. az1 = a[6],
  596. aw1 = a[7],
  597. bx0 = b[0],
  598. by0 = b[1],
  599. bz0 = b[2],
  600. bw0 = b[3];
  601. out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0;
  602. out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0;
  603. out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0;
  604. out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0;
  605. out[4] = ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0;
  606. out[5] = ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0;
  607. out[6] = az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0;
  608. out[7] = aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0;
  609. return out;
  610. }
  611. /**
  612. * Alias for {@link quat2.multiply}
  613. * @function
  614. */
  615. export const mul = multiply;
  616. /**
  617. * Scales a dual quat by a scalar number
  618. *
  619. * @param {quat2} out the receiving dual quat
  620. * @param {quat2} a the dual quat to scale
  621. * @param {Number} b amount to scale the dual quat by
  622. * @returns {quat2} out
  623. * @function
  624. */
  625. export function scale(out, a, b) {
  626. out[0] = a[0] * b;
  627. out[1] = a[1] * b;
  628. out[2] = a[2] * b;
  629. out[3] = a[3] * b;
  630. out[4] = a[4] * b;
  631. out[5] = a[5] * b;
  632. out[6] = a[6] * b;
  633. out[7] = a[7] * b;
  634. return out;
  635. }
  636. /**
  637. * Calculates the dot product of two dual quat's (The dot product of the real parts)
  638. *
  639. * @param {quat2} a the first operand
  640. * @param {quat2} b the second operand
  641. * @returns {Number} dot product of a and b
  642. * @function
  643. */
  644. export const dot = quat.dot;
  645. /**
  646. * Performs a linear interpolation between two dual quats's
  647. * NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5)
  648. *
  649. * @param {quat2} out the receiving dual quat
  650. * @param {quat2} a the first operand
  651. * @param {quat2} b the second operand
  652. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  653. * @returns {quat2} out
  654. */
  655. export function lerp(out, a, b, t) {
  656. let mt = 1 - t;
  657. if (dot(a, b) &lt; 0) t = -t;
  658. out[0] = a[0] * mt + b[0] * t;
  659. out[1] = a[1] * mt + b[1] * t;
  660. out[2] = a[2] * mt + b[2] * t;
  661. out[3] = a[3] * mt + b[3] * t;
  662. out[4] = a[4] * mt + b[4] * t;
  663. out[5] = a[5] * mt + b[5] * t;
  664. out[6] = a[6] * mt + b[6] * t;
  665. out[7] = a[7] * mt + b[7] * t;
  666. return out;
  667. }
  668. /**
  669. * Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper
  670. *
  671. * @param {quat2} out the receiving dual quaternion
  672. * @param {quat2} a dual quat to calculate inverse of
  673. * @returns {quat2} out
  674. */
  675. export function invert(out, a) {
  676. let sqlen = squaredLength(a);
  677. out[0] = -a[0] / sqlen;
  678. out[1] = -a[1] / sqlen;
  679. out[2] = -a[2] / sqlen;
  680. out[3] = a[3] / sqlen;
  681. out[4] = -a[4] / sqlen;
  682. out[5] = -a[5] / sqlen;
  683. out[6] = -a[6] / sqlen;
  684. out[7] = a[7] / sqlen;
  685. return out;
  686. }
  687. /**
  688. * Calculates the conjugate of a dual quat
  689. * If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result.
  690. *
  691. * @param {quat2} out the receiving quaternion
  692. * @param {quat2} a quat to calculate conjugate of
  693. * @returns {quat2} out
  694. */
  695. export function conjugate(out, a) {
  696. out[0] = -a[0];
  697. out[1] = -a[1];
  698. out[2] = -a[2];
  699. out[3] = a[3];
  700. out[4] = -a[4];
  701. out[5] = -a[5];
  702. out[6] = -a[6];
  703. out[7] = a[7];
  704. return out;
  705. }
  706. /**
  707. * Calculates the length of a dual quat
  708. *
  709. * @param {quat2} a dual quat to calculate length of
  710. * @returns {Number} length of a
  711. * @function
  712. */
  713. export const length = quat.length;
  714. /**
  715. * Alias for {@link quat2.length}
  716. * @function
  717. */
  718. export const len = length;
  719. /**
  720. * Calculates the squared length of a dual quat
  721. *
  722. * @param {quat2} a dual quat to calculate squared length of
  723. * @returns {Number} squared length of a
  724. * @function
  725. */
  726. export const squaredLength = quat.squaredLength;
  727. /**
  728. * Alias for {@link quat2.squaredLength}
  729. * @function
  730. */
  731. export const sqrLen = squaredLength;
  732. /**
  733. * Normalize a dual quat
  734. *
  735. * @param {quat2} out the receiving dual quaternion
  736. * @param {quat2} a dual quaternion to normalize
  737. * @returns {quat2} out
  738. * @function
  739. */
  740. export function normalize(out, a) {
  741. let magnitude = squaredLength(a);
  742. if (magnitude > 0) {
  743. magnitude = Math.sqrt(magnitude);
  744. let a0 = a[0] / magnitude;
  745. let a1 = a[1] / magnitude;
  746. let a2 = a[2] / magnitude;
  747. let a3 = a[3] / magnitude;
  748. let b0 = a[4];
  749. let b1 = a[5];
  750. let b2 = a[6];
  751. let b3 = a[7];
  752. let a_dot_b = (a0 * b0) + (a1 * b1) + (a2 * b2) + (a3 * b3);
  753. out[0] = a0;
  754. out[1] = a1;
  755. out[2] = a2;
  756. out[3] = a3;
  757. out[4] = (b0 - (a0 * a_dot_b)) / magnitude;
  758. out[5] = (b1 - (a1 * a_dot_b)) / magnitude;
  759. out[6] = (b2 - (a2 * a_dot_b)) / magnitude;
  760. out[7] = (b3 - (a3 * a_dot_b)) / magnitude;
  761. }
  762. return out;
  763. }
  764. /**
  765. * Returns a string representation of a dual quatenion
  766. *
  767. * @param {quat2} a dual quaternion to represent as a string
  768. * @returns {String} string representation of the dual quat
  769. */
  770. export function str(a) {
  771. return 'quat2(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' +
  772. a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ')';
  773. }
  774. /**
  775. * Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)
  776. *
  777. * @param {quat2} a the first dual quaternion.
  778. * @param {quat2} b the second dual quaternion.
  779. * @returns {Boolean} true if the dual quaternions are equal, false otherwise.
  780. */
  781. export function exactEquals(a, b) {
  782. return a[0] === b[0] &amp;&amp; a[1] === b[1] &amp;&amp; a[2] === b[2] &amp;&amp; a[3] === b[3] &amp;&amp;
  783. a[4] === b[4] &amp;&amp; a[5] === b[5] &amp;&amp; a[6] === b[6] &amp;&amp; a[7] === b[7];
  784. }
  785. /**
  786. * Returns whether or not the dual quaternions have approximately the same elements in the same position.
  787. *
  788. * @param {quat2} a the first dual quat.
  789. * @param {quat2} b the second dual quat.
  790. * @returns {Boolean} true if the dual quats are equal, false otherwise.
  791. */
  792. export function equals(a, b) {
  793. let a0 = a[0],
  794. a1 = a[1],
  795. a2 = a[2],
  796. a3 = a[3],
  797. a4 = a[4],
  798. a5 = a[5],
  799. a6 = a[6],
  800. a7 = a[7];
  801. let b0 = b[0],
  802. b1 = b[1],
  803. b2 = b[2],
  804. b3 = b[3],
  805. b4 = b[4],
  806. b5 = b[5],
  807. b6 = b[6],
  808. b7 = b[7];
  809. return (Math.abs(a0 - b0) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &amp;&amp;
  810. Math.abs(a1 - b1) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &amp;&amp;
  811. Math.abs(a2 - b2) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &amp;&amp;
  812. Math.abs(a3 - b3) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &amp;&amp;
  813. Math.abs(a4 - b4) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &amp;&amp;
  814. Math.abs(a5 - b5) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &amp;&amp;
  815. Math.abs(a6 - b6) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &amp;&amp;
  816. Math.abs(a7 - b7) &lt;= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)));
  817. }
  818. </code></pre>
  819. </article>
  820. </section>
  821. </div>
  822. <nav>
  823. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-glMatrix.html">glMatrix</a></li><li><a href="module-mat2.html">mat2</a></li><li><a href="module-mat2d.html">mat2d</a></li><li><a href="module-mat3.html">mat3</a></li><li><a href="module-mat4.html">mat4</a></li><li><a href="module-quat.html">quat</a></li><li><a href="module-quat2.html">quat2</a></li><li><a href="module-vec2.html">vec2</a></li><li><a href="module-vec3.html">vec3</a></li><li><a href="module-vec4.html">vec4</a></li></ul>
  824. </nav>
  825. <br class="clear">
  826. <footer>
  827. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Jul 13 2018 11:51:33 GMT+0200 (W. Europe Daylight Time)
  828. </footer>
  829. <script> prettyPrint(); </script>
  830. <script src="scripts/linenumber.js"> </script>
  831. </body>
  832. </html>