quat.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.create = create;
  7. exports.identity = identity;
  8. exports.setAxisAngle = setAxisAngle;
  9. exports.getAxisAngle = getAxisAngle;
  10. exports.getAngle = getAngle;
  11. exports.multiply = multiply;
  12. exports.rotateX = rotateX;
  13. exports.rotateY = rotateY;
  14. exports.rotateZ = rotateZ;
  15. exports.calculateW = calculateW;
  16. exports.exp = exp;
  17. exports.ln = ln;
  18. exports.pow = pow;
  19. exports.slerp = slerp;
  20. exports.random = random;
  21. exports.invert = invert;
  22. exports.conjugate = conjugate;
  23. exports.fromMat3 = fromMat3;
  24. exports.fromEuler = fromEuler;
  25. exports.str = str;
  26. exports.setAxes = exports.sqlerp = exports.rotationTo = exports.equals = exports.exactEquals = exports.normalize = exports.sqrLen = exports.squaredLength = exports.len = exports.length = exports.lerp = exports.dot = exports.scale = exports.mul = exports.add = exports.set = exports.copy = exports.fromValues = exports.clone = void 0;
  27. var glMatrix = _interopRequireWildcard(require("./common.js"));
  28. var mat3 = _interopRequireWildcard(require("./mat3.js"));
  29. var vec3 = _interopRequireWildcard(require("./vec3.js"));
  30. var vec4 = _interopRequireWildcard(require("./vec4.js"));
  31. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  32. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  33. /**
  34. * Quaternion
  35. * @module quat
  36. */
  37. /**
  38. * Creates a new identity quat
  39. *
  40. * @returns {quat} a new quaternion
  41. */
  42. function create() {
  43. var out = new glMatrix.ARRAY_TYPE(4);
  44. if (glMatrix.ARRAY_TYPE != Float32Array) {
  45. out[0] = 0;
  46. out[1] = 0;
  47. out[2] = 0;
  48. }
  49. out[3] = 1;
  50. return out;
  51. }
  52. /**
  53. * Set a quat to the identity quaternion
  54. *
  55. * @param {quat} out the receiving quaternion
  56. * @returns {quat} out
  57. */
  58. function identity(out) {
  59. out[0] = 0;
  60. out[1] = 0;
  61. out[2] = 0;
  62. out[3] = 1;
  63. return out;
  64. }
  65. /**
  66. * Sets a quat from the given angle and rotation axis,
  67. * then returns it.
  68. *
  69. * @param {quat} out the receiving quaternion
  70. * @param {ReadonlyVec3} axis the axis around which to rotate
  71. * @param {Number} rad the angle in radians
  72. * @returns {quat} out
  73. **/
  74. function setAxisAngle(out, axis, rad) {
  75. rad = rad * 0.5;
  76. var s = Math.sin(rad);
  77. out[0] = s * axis[0];
  78. out[1] = s * axis[1];
  79. out[2] = s * axis[2];
  80. out[3] = Math.cos(rad);
  81. return out;
  82. }
  83. /**
  84. * Gets the rotation axis and angle for a given
  85. * quaternion. If a quaternion is created with
  86. * setAxisAngle, this method will return the same
  87. * values as providied in the original parameter list
  88. * OR functionally equivalent values.
  89. * Example: The quaternion formed by axis [0, 0, 1] and
  90. * angle -90 is the same as the quaternion formed by
  91. * [0, 0, 1] and 270. This method favors the latter.
  92. * @param {vec3} out_axis Vector receiving the axis of rotation
  93. * @param {ReadonlyQuat} q Quaternion to be decomposed
  94. * @return {Number} Angle, in radians, of the rotation
  95. */
  96. function getAxisAngle(out_axis, q) {
  97. var rad = Math.acos(q[3]) * 2.0;
  98. var s = Math.sin(rad / 2.0);
  99. if (s > glMatrix.EPSILON) {
  100. out_axis[0] = q[0] / s;
  101. out_axis[1] = q[1] / s;
  102. out_axis[2] = q[2] / s;
  103. } else {
  104. // If s is zero, return any axis (no rotation - axis does not matter)
  105. out_axis[0] = 1;
  106. out_axis[1] = 0;
  107. out_axis[2] = 0;
  108. }
  109. return rad;
  110. }
  111. /**
  112. * Gets the angular distance between two unit quaternions
  113. *
  114. * @param {ReadonlyQuat} a Origin unit quaternion
  115. * @param {ReadonlyQuat} b Destination unit quaternion
  116. * @return {Number} Angle, in radians, between the two quaternions
  117. */
  118. function getAngle(a, b) {
  119. var dotproduct = dot(a, b);
  120. return Math.acos(2 * dotproduct * dotproduct - 1);
  121. }
  122. /**
  123. * Multiplies two quat's
  124. *
  125. * @param {quat} out the receiving quaternion
  126. * @param {ReadonlyQuat} a the first operand
  127. * @param {ReadonlyQuat} b the second operand
  128. * @returns {quat} out
  129. */
  130. function multiply(out, a, b) {
  131. var ax = a[0],
  132. ay = a[1],
  133. az = a[2],
  134. aw = a[3];
  135. var bx = b[0],
  136. by = b[1],
  137. bz = b[2],
  138. bw = b[3];
  139. out[0] = ax * bw + aw * bx + ay * bz - az * by;
  140. out[1] = ay * bw + aw * by + az * bx - ax * bz;
  141. out[2] = az * bw + aw * bz + ax * by - ay * bx;
  142. out[3] = aw * bw - ax * bx - ay * by - az * bz;
  143. return out;
  144. }
  145. /**
  146. * Rotates a quaternion by the given angle about the X axis
  147. *
  148. * @param {quat} out quat receiving operation result
  149. * @param {ReadonlyQuat} a quat to rotate
  150. * @param {number} rad angle (in radians) to rotate
  151. * @returns {quat} out
  152. */
  153. function rotateX(out, a, rad) {
  154. rad *= 0.5;
  155. var ax = a[0],
  156. ay = a[1],
  157. az = a[2],
  158. aw = a[3];
  159. var bx = Math.sin(rad),
  160. bw = Math.cos(rad);
  161. out[0] = ax * bw + aw * bx;
  162. out[1] = ay * bw + az * bx;
  163. out[2] = az * bw - ay * bx;
  164. out[3] = aw * bw - ax * bx;
  165. return out;
  166. }
  167. /**
  168. * Rotates a quaternion by the given angle about the Y axis
  169. *
  170. * @param {quat} out quat receiving operation result
  171. * @param {ReadonlyQuat} a quat to rotate
  172. * @param {number} rad angle (in radians) to rotate
  173. * @returns {quat} out
  174. */
  175. function rotateY(out, a, rad) {
  176. rad *= 0.5;
  177. var ax = a[0],
  178. ay = a[1],
  179. az = a[2],
  180. aw = a[3];
  181. var by = Math.sin(rad),
  182. bw = Math.cos(rad);
  183. out[0] = ax * bw - az * by;
  184. out[1] = ay * bw + aw * by;
  185. out[2] = az * bw + ax * by;
  186. out[3] = aw * bw - ay * by;
  187. return out;
  188. }
  189. /**
  190. * Rotates a quaternion by the given angle about the Z axis
  191. *
  192. * @param {quat} out quat receiving operation result
  193. * @param {ReadonlyQuat} a quat to rotate
  194. * @param {number} rad angle (in radians) to rotate
  195. * @returns {quat} out
  196. */
  197. function rotateZ(out, a, rad) {
  198. rad *= 0.5;
  199. var ax = a[0],
  200. ay = a[1],
  201. az = a[2],
  202. aw = a[3];
  203. var bz = Math.sin(rad),
  204. bw = Math.cos(rad);
  205. out[0] = ax * bw + ay * bz;
  206. out[1] = ay * bw - ax * bz;
  207. out[2] = az * bw + aw * bz;
  208. out[3] = aw * bw - az * bz;
  209. return out;
  210. }
  211. /**
  212. * Calculates the W component of a quat from the X, Y, and Z components.
  213. * Assumes that quaternion is 1 unit in length.
  214. * Any existing W component will be ignored.
  215. *
  216. * @param {quat} out the receiving quaternion
  217. * @param {ReadonlyQuat} a quat to calculate W component of
  218. * @returns {quat} out
  219. */
  220. function calculateW(out, a) {
  221. var x = a[0],
  222. y = a[1],
  223. z = a[2];
  224. out[0] = x;
  225. out[1] = y;
  226. out[2] = z;
  227. out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
  228. return out;
  229. }
  230. /**
  231. * Calculate the exponential of a unit quaternion.
  232. *
  233. * @param {quat} out the receiving quaternion
  234. * @param {ReadonlyQuat} a quat to calculate the exponential of
  235. * @returns {quat} out
  236. */
  237. function exp(out, a) {
  238. var x = a[0],
  239. y = a[1],
  240. z = a[2],
  241. w = a[3];
  242. var r = Math.sqrt(x * x + y * y + z * z);
  243. var et = Math.exp(w);
  244. var s = r > 0 ? et * Math.sin(r) / r : 0;
  245. out[0] = x * s;
  246. out[1] = y * s;
  247. out[2] = z * s;
  248. out[3] = et * Math.cos(r);
  249. return out;
  250. }
  251. /**
  252. * Calculate the natural logarithm of a unit quaternion.
  253. *
  254. * @param {quat} out the receiving quaternion
  255. * @param {ReadonlyQuat} a quat to calculate the exponential of
  256. * @returns {quat} out
  257. */
  258. function ln(out, a) {
  259. var x = a[0],
  260. y = a[1],
  261. z = a[2],
  262. w = a[3];
  263. var r = Math.sqrt(x * x + y * y + z * z);
  264. var t = r > 0 ? Math.atan2(r, w) / r : 0;
  265. out[0] = x * t;
  266. out[1] = y * t;
  267. out[2] = z * t;
  268. out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
  269. return out;
  270. }
  271. /**
  272. * Calculate the scalar power of a unit quaternion.
  273. *
  274. * @param {quat} out the receiving quaternion
  275. * @param {ReadonlyQuat} a quat to calculate the exponential of
  276. * @param {Number} b amount to scale the quaternion by
  277. * @returns {quat} out
  278. */
  279. function pow(out, a, b) {
  280. ln(out, a);
  281. scale(out, out, b);
  282. exp(out, out);
  283. return out;
  284. }
  285. /**
  286. * Performs a spherical linear interpolation between two quat
  287. *
  288. * @param {quat} out the receiving quaternion
  289. * @param {ReadonlyQuat} a the first operand
  290. * @param {ReadonlyQuat} b the second operand
  291. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  292. * @returns {quat} out
  293. */
  294. function slerp(out, a, b, t) {
  295. // benchmarks:
  296. // http://jsperf.com/quaternion-slerp-implementations
  297. var ax = a[0],
  298. ay = a[1],
  299. az = a[2],
  300. aw = a[3];
  301. var bx = b[0],
  302. by = b[1],
  303. bz = b[2],
  304. bw = b[3];
  305. var omega, cosom, sinom, scale0, scale1; // calc cosine
  306. cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
  307. if (cosom < 0.0) {
  308. cosom = -cosom;
  309. bx = -bx;
  310. by = -by;
  311. bz = -bz;
  312. bw = -bw;
  313. } // calculate coefficients
  314. if (1.0 - cosom > glMatrix.EPSILON) {
  315. // standard case (slerp)
  316. omega = Math.acos(cosom);
  317. sinom = Math.sin(omega);
  318. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  319. scale1 = Math.sin(t * omega) / sinom;
  320. } else {
  321. // "from" and "to" quaternions are very close
  322. // ... so we can do a linear interpolation
  323. scale0 = 1.0 - t;
  324. scale1 = t;
  325. } // calculate final values
  326. out[0] = scale0 * ax + scale1 * bx;
  327. out[1] = scale0 * ay + scale1 * by;
  328. out[2] = scale0 * az + scale1 * bz;
  329. out[3] = scale0 * aw + scale1 * bw;
  330. return out;
  331. }
  332. /**
  333. * Generates a random unit quaternion
  334. *
  335. * @param {quat} out the receiving quaternion
  336. * @returns {quat} out
  337. */
  338. function random(out) {
  339. // Implementation of http://planning.cs.uiuc.edu/node198.html
  340. // TODO: Calling random 3 times is probably not the fastest solution
  341. var u1 = glMatrix.RANDOM();
  342. var u2 = glMatrix.RANDOM();
  343. var u3 = glMatrix.RANDOM();
  344. var sqrt1MinusU1 = Math.sqrt(1 - u1);
  345. var sqrtU1 = Math.sqrt(u1);
  346. out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
  347. out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
  348. out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
  349. out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
  350. return out;
  351. }
  352. /**
  353. * Calculates the inverse of a quat
  354. *
  355. * @param {quat} out the receiving quaternion
  356. * @param {ReadonlyQuat} a quat to calculate inverse of
  357. * @returns {quat} out
  358. */
  359. function invert(out, a) {
  360. var a0 = a[0],
  361. a1 = a[1],
  362. a2 = a[2],
  363. a3 = a[3];
  364. var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
  365. var invDot = dot ? 1.0 / dot : 0; // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
  366. out[0] = -a0 * invDot;
  367. out[1] = -a1 * invDot;
  368. out[2] = -a2 * invDot;
  369. out[3] = a3 * invDot;
  370. return out;
  371. }
  372. /**
  373. * Calculates the conjugate of a quat
  374. * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
  375. *
  376. * @param {quat} out the receiving quaternion
  377. * @param {ReadonlyQuat} a quat to calculate conjugate of
  378. * @returns {quat} out
  379. */
  380. function conjugate(out, a) {
  381. out[0] = -a[0];
  382. out[1] = -a[1];
  383. out[2] = -a[2];
  384. out[3] = a[3];
  385. return out;
  386. }
  387. /**
  388. * Creates a quaternion from the given 3x3 rotation matrix.
  389. *
  390. * NOTE: The resultant quaternion is not normalized, so you should be sure
  391. * to renormalize the quaternion yourself where necessary.
  392. *
  393. * @param {quat} out the receiving quaternion
  394. * @param {ReadonlyMat3} m rotation matrix
  395. * @returns {quat} out
  396. * @function
  397. */
  398. function fromMat3(out, m) {
  399. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  400. // article "Quaternion Calculus and Fast Animation".
  401. var fTrace = m[0] + m[4] + m[8];
  402. var fRoot;
  403. if (fTrace > 0.0) {
  404. // |w| > 1/2, may as well choose w > 1/2
  405. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  406. out[3] = 0.5 * fRoot;
  407. fRoot = 0.5 / fRoot; // 1/(4w)
  408. out[0] = (m[5] - m[7]) * fRoot;
  409. out[1] = (m[6] - m[2]) * fRoot;
  410. out[2] = (m[1] - m[3]) * fRoot;
  411. } else {
  412. // |w| <= 1/2
  413. var i = 0;
  414. if (m[4] > m[0]) i = 1;
  415. if (m[8] > m[i * 3 + i]) i = 2;
  416. var j = (i + 1) % 3;
  417. var k = (i + 2) % 3;
  418. fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
  419. out[i] = 0.5 * fRoot;
  420. fRoot = 0.5 / fRoot;
  421. out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
  422. out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
  423. out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
  424. }
  425. return out;
  426. }
  427. /**
  428. * Creates a quaternion from the given euler angle x, y, z.
  429. *
  430. * @param {quat} out the receiving quaternion
  431. * @param {x} Angle to rotate around X axis in degrees.
  432. * @param {y} Angle to rotate around Y axis in degrees.
  433. * @param {z} Angle to rotate around Z axis in degrees.
  434. * @returns {quat} out
  435. * @function
  436. */
  437. function fromEuler(out, x, y, z) {
  438. var halfToRad = 0.5 * Math.PI / 180.0;
  439. x *= halfToRad;
  440. y *= halfToRad;
  441. z *= halfToRad;
  442. var sx = Math.sin(x);
  443. var cx = Math.cos(x);
  444. var sy = Math.sin(y);
  445. var cy = Math.cos(y);
  446. var sz = Math.sin(z);
  447. var cz = Math.cos(z);
  448. out[0] = sx * cy * cz - cx * sy * sz;
  449. out[1] = cx * sy * cz + sx * cy * sz;
  450. out[2] = cx * cy * sz - sx * sy * cz;
  451. out[3] = cx * cy * cz + sx * sy * sz;
  452. return out;
  453. }
  454. /**
  455. * Returns a string representation of a quatenion
  456. *
  457. * @param {ReadonlyQuat} a vector to represent as a string
  458. * @returns {String} string representation of the vector
  459. */
  460. function str(a) {
  461. return "quat(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
  462. }
  463. /**
  464. * Creates a new quat initialized with values from an existing quaternion
  465. *
  466. * @param {ReadonlyQuat} a quaternion to clone
  467. * @returns {quat} a new quaternion
  468. * @function
  469. */
  470. var clone = vec4.clone;
  471. /**
  472. * Creates a new quat initialized with the given values
  473. *
  474. * @param {Number} x X component
  475. * @param {Number} y Y component
  476. * @param {Number} z Z component
  477. * @param {Number} w W component
  478. * @returns {quat} a new quaternion
  479. * @function
  480. */
  481. exports.clone = clone;
  482. var fromValues = vec4.fromValues;
  483. /**
  484. * Copy the values from one quat to another
  485. *
  486. * @param {quat} out the receiving quaternion
  487. * @param {ReadonlyQuat} a the source quaternion
  488. * @returns {quat} out
  489. * @function
  490. */
  491. exports.fromValues = fromValues;
  492. var copy = vec4.copy;
  493. /**
  494. * Set the components of a quat to the given values
  495. *
  496. * @param {quat} out the receiving quaternion
  497. * @param {Number} x X component
  498. * @param {Number} y Y component
  499. * @param {Number} z Z component
  500. * @param {Number} w W component
  501. * @returns {quat} out
  502. * @function
  503. */
  504. exports.copy = copy;
  505. var set = vec4.set;
  506. /**
  507. * Adds two quat's
  508. *
  509. * @param {quat} out the receiving quaternion
  510. * @param {ReadonlyQuat} a the first operand
  511. * @param {ReadonlyQuat} b the second operand
  512. * @returns {quat} out
  513. * @function
  514. */
  515. exports.set = set;
  516. var add = vec4.add;
  517. /**
  518. * Alias for {@link quat.multiply}
  519. * @function
  520. */
  521. exports.add = add;
  522. var mul = multiply;
  523. /**
  524. * Scales a quat by a scalar number
  525. *
  526. * @param {quat} out the receiving vector
  527. * @param {ReadonlyQuat} a the vector to scale
  528. * @param {Number} b amount to scale the vector by
  529. * @returns {quat} out
  530. * @function
  531. */
  532. exports.mul = mul;
  533. var scale = vec4.scale;
  534. /**
  535. * Calculates the dot product of two quat's
  536. *
  537. * @param {ReadonlyQuat} a the first operand
  538. * @param {ReadonlyQuat} b the second operand
  539. * @returns {Number} dot product of a and b
  540. * @function
  541. */
  542. exports.scale = scale;
  543. var dot = vec4.dot;
  544. /**
  545. * Performs a linear interpolation between two quat's
  546. *
  547. * @param {quat} out the receiving quaternion
  548. * @param {ReadonlyQuat} a the first operand
  549. * @param {ReadonlyQuat} b the second operand
  550. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  551. * @returns {quat} out
  552. * @function
  553. */
  554. exports.dot = dot;
  555. var lerp = vec4.lerp;
  556. /**
  557. * Calculates the length of a quat
  558. *
  559. * @param {ReadonlyQuat} a vector to calculate length of
  560. * @returns {Number} length of a
  561. */
  562. exports.lerp = lerp;
  563. var length = vec4.length;
  564. /**
  565. * Alias for {@link quat.length}
  566. * @function
  567. */
  568. exports.length = length;
  569. var len = length;
  570. /**
  571. * Calculates the squared length of a quat
  572. *
  573. * @param {ReadonlyQuat} a vector to calculate squared length of
  574. * @returns {Number} squared length of a
  575. * @function
  576. */
  577. exports.len = len;
  578. var squaredLength = vec4.squaredLength;
  579. /**
  580. * Alias for {@link quat.squaredLength}
  581. * @function
  582. */
  583. exports.squaredLength = squaredLength;
  584. var sqrLen = squaredLength;
  585. /**
  586. * Normalize a quat
  587. *
  588. * @param {quat} out the receiving quaternion
  589. * @param {ReadonlyQuat} a quaternion to normalize
  590. * @returns {quat} out
  591. * @function
  592. */
  593. exports.sqrLen = sqrLen;
  594. var normalize = vec4.normalize;
  595. /**
  596. * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)
  597. *
  598. * @param {ReadonlyQuat} a The first quaternion.
  599. * @param {ReadonlyQuat} b The second quaternion.
  600. * @returns {Boolean} True if the vectors are equal, false otherwise.
  601. */
  602. exports.normalize = normalize;
  603. var exactEquals = vec4.exactEquals;
  604. /**
  605. * Returns whether or not the quaternions have approximately the same elements in the same position.
  606. *
  607. * @param {ReadonlyQuat} a The first vector.
  608. * @param {ReadonlyQuat} b The second vector.
  609. * @returns {Boolean} True if the vectors are equal, false otherwise.
  610. */
  611. exports.exactEquals = exactEquals;
  612. var equals = vec4.equals;
  613. /**
  614. * Sets a quaternion to represent the shortest rotation from one
  615. * vector to another.
  616. *
  617. * Both vectors are assumed to be unit length.
  618. *
  619. * @param {quat} out the receiving quaternion.
  620. * @param {ReadonlyVec3} a the initial vector
  621. * @param {ReadonlyVec3} b the destination vector
  622. * @returns {quat} out
  623. */
  624. exports.equals = equals;
  625. var rotationTo = function () {
  626. var tmpvec3 = vec3.create();
  627. var xUnitVec3 = vec3.fromValues(1, 0, 0);
  628. var yUnitVec3 = vec3.fromValues(0, 1, 0);
  629. return function (out, a, b) {
  630. var dot = vec3.dot(a, b);
  631. if (dot < -0.999999) {
  632. vec3.cross(tmpvec3, xUnitVec3, a);
  633. if (vec3.len(tmpvec3) < 0.000001) vec3.cross(tmpvec3, yUnitVec3, a);
  634. vec3.normalize(tmpvec3, tmpvec3);
  635. setAxisAngle(out, tmpvec3, Math.PI);
  636. return out;
  637. } else if (dot > 0.999999) {
  638. out[0] = 0;
  639. out[1] = 0;
  640. out[2] = 0;
  641. out[3] = 1;
  642. return out;
  643. } else {
  644. vec3.cross(tmpvec3, a, b);
  645. out[0] = tmpvec3[0];
  646. out[1] = tmpvec3[1];
  647. out[2] = tmpvec3[2];
  648. out[3] = 1 + dot;
  649. return normalize(out, out);
  650. }
  651. };
  652. }();
  653. /**
  654. * Performs a spherical linear interpolation with two control points
  655. *
  656. * @param {quat} out the receiving quaternion
  657. * @param {ReadonlyQuat} a the first operand
  658. * @param {ReadonlyQuat} b the second operand
  659. * @param {ReadonlyQuat} c the third operand
  660. * @param {ReadonlyQuat} d the fourth operand
  661. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  662. * @returns {quat} out
  663. */
  664. exports.rotationTo = rotationTo;
  665. var sqlerp = function () {
  666. var temp1 = create();
  667. var temp2 = create();
  668. return function (out, a, b, c, d, t) {
  669. slerp(temp1, a, d, t);
  670. slerp(temp2, b, c, t);
  671. slerp(out, temp1, temp2, 2 * t * (1 - t));
  672. return out;
  673. };
  674. }();
  675. /**
  676. * Sets the specified quaternion with values corresponding to the given
  677. * axes. Each axis is a vec3 and is expected to be unit length and
  678. * perpendicular to all other specified axes.
  679. *
  680. * @param {ReadonlyVec3} view the vector representing the viewing direction
  681. * @param {ReadonlyVec3} right the vector representing the local "right" direction
  682. * @param {ReadonlyVec3} up the vector representing the local "up" direction
  683. * @returns {quat} out
  684. */
  685. exports.sqlerp = sqlerp;
  686. var setAxes = function () {
  687. var matr = mat3.create();
  688. return function (out, view, right, up) {
  689. matr[0] = right[0];
  690. matr[3] = right[1];
  691. matr[6] = right[2];
  692. matr[1] = up[0];
  693. matr[4] = up[1];
  694. matr[7] = up[2];
  695. matr[2] = -view[0];
  696. matr[5] = -view[1];
  697. matr[8] = -view[2];
  698. return normalize(out, fromMat3(out, matr));
  699. };
  700. }();
  701. exports.setAxes = setAxes;