vec4.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.forEach = exports.sqrLen = exports.len = exports.sqrDist = exports.dist = exports.div = exports.mul = exports.sub = undefined;
  6. exports.create = create;
  7. exports.clone = clone;
  8. exports.fromValues = fromValues;
  9. exports.copy = copy;
  10. exports.set = set;
  11. exports.add = add;
  12. exports.subtract = subtract;
  13. exports.multiply = multiply;
  14. exports.divide = divide;
  15. exports.ceil = ceil;
  16. exports.floor = floor;
  17. exports.min = min;
  18. exports.max = max;
  19. exports.round = round;
  20. exports.scale = scale;
  21. exports.scaleAndAdd = scaleAndAdd;
  22. exports.distance = distance;
  23. exports.squaredDistance = squaredDistance;
  24. exports.length = length;
  25. exports.squaredLength = squaredLength;
  26. exports.negate = negate;
  27. exports.inverse = inverse;
  28. exports.normalize = normalize;
  29. exports.dot = dot;
  30. exports.lerp = lerp;
  31. exports.random = random;
  32. exports.transformMat4 = transformMat4;
  33. exports.transformQuat = transformQuat;
  34. exports.str = str;
  35. exports.exactEquals = exactEquals;
  36. exports.equals = equals;
  37. var _common = require('./common.js');
  38. var glMatrix = _interopRequireWildcard(_common);
  39. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  40. /**
  41. * 4 Dimensional Vector
  42. * @module vec4
  43. */
  44. /**
  45. * Creates a new, empty vec4
  46. *
  47. * @returns {vec4} a new 4D vector
  48. */
  49. function create() {
  50. var out = new glMatrix.ARRAY_TYPE(4);
  51. if (glMatrix.ARRAY_TYPE != Float32Array) {
  52. out[0] = 0;
  53. out[1] = 0;
  54. out[2] = 0;
  55. out[3] = 0;
  56. }
  57. return out;
  58. }
  59. /**
  60. * Creates a new vec4 initialized with values from an existing vector
  61. *
  62. * @param {vec4} a vector to clone
  63. * @returns {vec4} a new 4D vector
  64. */
  65. function clone(a) {
  66. var out = new glMatrix.ARRAY_TYPE(4);
  67. out[0] = a[0];
  68. out[1] = a[1];
  69. out[2] = a[2];
  70. out[3] = a[3];
  71. return out;
  72. }
  73. /**
  74. * Creates a new vec4 initialized with the given values
  75. *
  76. * @param {Number} x X component
  77. * @param {Number} y Y component
  78. * @param {Number} z Z component
  79. * @param {Number} w W component
  80. * @returns {vec4} a new 4D vector
  81. */
  82. function fromValues(x, y, z, w) {
  83. var out = new glMatrix.ARRAY_TYPE(4);
  84. out[0] = x;
  85. out[1] = y;
  86. out[2] = z;
  87. out[3] = w;
  88. return out;
  89. }
  90. /**
  91. * Copy the values from one vec4 to another
  92. *
  93. * @param {vec4} out the receiving vector
  94. * @param {vec4} a the source vector
  95. * @returns {vec4} out
  96. */
  97. function copy(out, a) {
  98. out[0] = a[0];
  99. out[1] = a[1];
  100. out[2] = a[2];
  101. out[3] = a[3];
  102. return out;
  103. }
  104. /**
  105. * Set the components of a vec4 to the given values
  106. *
  107. * @param {vec4} out the receiving vector
  108. * @param {Number} x X component
  109. * @param {Number} y Y component
  110. * @param {Number} z Z component
  111. * @param {Number} w W component
  112. * @returns {vec4} out
  113. */
  114. function set(out, x, y, z, w) {
  115. out[0] = x;
  116. out[1] = y;
  117. out[2] = z;
  118. out[3] = w;
  119. return out;
  120. }
  121. /**
  122. * Adds two vec4's
  123. *
  124. * @param {vec4} out the receiving vector
  125. * @param {vec4} a the first operand
  126. * @param {vec4} b the second operand
  127. * @returns {vec4} out
  128. */
  129. function add(out, a, b) {
  130. out[0] = a[0] + b[0];
  131. out[1] = a[1] + b[1];
  132. out[2] = a[2] + b[2];
  133. out[3] = a[3] + b[3];
  134. return out;
  135. }
  136. /**
  137. * Subtracts vector b from vector a
  138. *
  139. * @param {vec4} out the receiving vector
  140. * @param {vec4} a the first operand
  141. * @param {vec4} b the second operand
  142. * @returns {vec4} out
  143. */
  144. function subtract(out, a, b) {
  145. out[0] = a[0] - b[0];
  146. out[1] = a[1] - b[1];
  147. out[2] = a[2] - b[2];
  148. out[3] = a[3] - b[3];
  149. return out;
  150. }
  151. /**
  152. * Multiplies two vec4's
  153. *
  154. * @param {vec4} out the receiving vector
  155. * @param {vec4} a the first operand
  156. * @param {vec4} b the second operand
  157. * @returns {vec4} out
  158. */
  159. function multiply(out, a, b) {
  160. out[0] = a[0] * b[0];
  161. out[1] = a[1] * b[1];
  162. out[2] = a[2] * b[2];
  163. out[3] = a[3] * b[3];
  164. return out;
  165. }
  166. /**
  167. * Divides two vec4's
  168. *
  169. * @param {vec4} out the receiving vector
  170. * @param {vec4} a the first operand
  171. * @param {vec4} b the second operand
  172. * @returns {vec4} out
  173. */
  174. function divide(out, a, b) {
  175. out[0] = a[0] / b[0];
  176. out[1] = a[1] / b[1];
  177. out[2] = a[2] / b[2];
  178. out[3] = a[3] / b[3];
  179. return out;
  180. }
  181. /**
  182. * Math.ceil the components of a vec4
  183. *
  184. * @param {vec4} out the receiving vector
  185. * @param {vec4} a vector to ceil
  186. * @returns {vec4} out
  187. */
  188. function ceil(out, a) {
  189. out[0] = Math.ceil(a[0]);
  190. out[1] = Math.ceil(a[1]);
  191. out[2] = Math.ceil(a[2]);
  192. out[3] = Math.ceil(a[3]);
  193. return out;
  194. }
  195. /**
  196. * Math.floor the components of a vec4
  197. *
  198. * @param {vec4} out the receiving vector
  199. * @param {vec4} a vector to floor
  200. * @returns {vec4} out
  201. */
  202. function floor(out, a) {
  203. out[0] = Math.floor(a[0]);
  204. out[1] = Math.floor(a[1]);
  205. out[2] = Math.floor(a[2]);
  206. out[3] = Math.floor(a[3]);
  207. return out;
  208. }
  209. /**
  210. * Returns the minimum of two vec4's
  211. *
  212. * @param {vec4} out the receiving vector
  213. * @param {vec4} a the first operand
  214. * @param {vec4} b the second operand
  215. * @returns {vec4} out
  216. */
  217. function min(out, a, b) {
  218. out[0] = Math.min(a[0], b[0]);
  219. out[1] = Math.min(a[1], b[1]);
  220. out[2] = Math.min(a[2], b[2]);
  221. out[3] = Math.min(a[3], b[3]);
  222. return out;
  223. }
  224. /**
  225. * Returns the maximum of two vec4's
  226. *
  227. * @param {vec4} out the receiving vector
  228. * @param {vec4} a the first operand
  229. * @param {vec4} b the second operand
  230. * @returns {vec4} out
  231. */
  232. function max(out, a, b) {
  233. out[0] = Math.max(a[0], b[0]);
  234. out[1] = Math.max(a[1], b[1]);
  235. out[2] = Math.max(a[2], b[2]);
  236. out[3] = Math.max(a[3], b[3]);
  237. return out;
  238. }
  239. /**
  240. * Math.round the components of a vec4
  241. *
  242. * @param {vec4} out the receiving vector
  243. * @param {vec4} a vector to round
  244. * @returns {vec4} out
  245. */
  246. function round(out, a) {
  247. out[0] = Math.round(a[0]);
  248. out[1] = Math.round(a[1]);
  249. out[2] = Math.round(a[2]);
  250. out[3] = Math.round(a[3]);
  251. return out;
  252. }
  253. /**
  254. * Scales a vec4 by a scalar number
  255. *
  256. * @param {vec4} out the receiving vector
  257. * @param {vec4} a the vector to scale
  258. * @param {Number} b amount to scale the vector by
  259. * @returns {vec4} out
  260. */
  261. function scale(out, a, b) {
  262. out[0] = a[0] * b;
  263. out[1] = a[1] * b;
  264. out[2] = a[2] * b;
  265. out[3] = a[3] * b;
  266. return out;
  267. }
  268. /**
  269. * Adds two vec4's after scaling the second operand by a scalar value
  270. *
  271. * @param {vec4} out the receiving vector
  272. * @param {vec4} a the first operand
  273. * @param {vec4} b the second operand
  274. * @param {Number} scale the amount to scale b by before adding
  275. * @returns {vec4} out
  276. */
  277. function scaleAndAdd(out, a, b, scale) {
  278. out[0] = a[0] + b[0] * scale;
  279. out[1] = a[1] + b[1] * scale;
  280. out[2] = a[2] + b[2] * scale;
  281. out[3] = a[3] + b[3] * scale;
  282. return out;
  283. }
  284. /**
  285. * Calculates the euclidian distance between two vec4's
  286. *
  287. * @param {vec4} a the first operand
  288. * @param {vec4} b the second operand
  289. * @returns {Number} distance between a and b
  290. */
  291. function distance(a, b) {
  292. var x = b[0] - a[0];
  293. var y = b[1] - a[1];
  294. var z = b[2] - a[2];
  295. var w = b[3] - a[3];
  296. return Math.sqrt(x * x + y * y + z * z + w * w);
  297. }
  298. /**
  299. * Calculates the squared euclidian distance between two vec4's
  300. *
  301. * @param {vec4} a the first operand
  302. * @param {vec4} b the second operand
  303. * @returns {Number} squared distance between a and b
  304. */
  305. function squaredDistance(a, b) {
  306. var x = b[0] - a[0];
  307. var y = b[1] - a[1];
  308. var z = b[2] - a[2];
  309. var w = b[3] - a[3];
  310. return x * x + y * y + z * z + w * w;
  311. }
  312. /**
  313. * Calculates the length of a vec4
  314. *
  315. * @param {vec4} a vector to calculate length of
  316. * @returns {Number} length of a
  317. */
  318. function length(a) {
  319. var x = a[0];
  320. var y = a[1];
  321. var z = a[2];
  322. var w = a[3];
  323. return Math.sqrt(x * x + y * y + z * z + w * w);
  324. }
  325. /**
  326. * Calculates the squared length of a vec4
  327. *
  328. * @param {vec4} a vector to calculate squared length of
  329. * @returns {Number} squared length of a
  330. */
  331. function squaredLength(a) {
  332. var x = a[0];
  333. var y = a[1];
  334. var z = a[2];
  335. var w = a[3];
  336. return x * x + y * y + z * z + w * w;
  337. }
  338. /**
  339. * Negates the components of a vec4
  340. *
  341. * @param {vec4} out the receiving vector
  342. * @param {vec4} a vector to negate
  343. * @returns {vec4} out
  344. */
  345. function negate(out, a) {
  346. out[0] = -a[0];
  347. out[1] = -a[1];
  348. out[2] = -a[2];
  349. out[3] = -a[3];
  350. return out;
  351. }
  352. /**
  353. * Returns the inverse of the components of a vec4
  354. *
  355. * @param {vec4} out the receiving vector
  356. * @param {vec4} a vector to invert
  357. * @returns {vec4} out
  358. */
  359. function inverse(out, a) {
  360. out[0] = 1.0 / a[0];
  361. out[1] = 1.0 / a[1];
  362. out[2] = 1.0 / a[2];
  363. out[3] = 1.0 / a[3];
  364. return out;
  365. }
  366. /**
  367. * Normalize a vec4
  368. *
  369. * @param {vec4} out the receiving vector
  370. * @param {vec4} a vector to normalize
  371. * @returns {vec4} out
  372. */
  373. function normalize(out, a) {
  374. var x = a[0];
  375. var y = a[1];
  376. var z = a[2];
  377. var w = a[3];
  378. var len = x * x + y * y + z * z + w * w;
  379. if (len > 0) {
  380. len = 1 / Math.sqrt(len);
  381. out[0] = x * len;
  382. out[1] = y * len;
  383. out[2] = z * len;
  384. out[3] = w * len;
  385. }
  386. return out;
  387. }
  388. /**
  389. * Calculates the dot product of two vec4's
  390. *
  391. * @param {vec4} a the first operand
  392. * @param {vec4} b the second operand
  393. * @returns {Number} dot product of a and b
  394. */
  395. function dot(a, b) {
  396. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  397. }
  398. /**
  399. * Performs a linear interpolation between two vec4's
  400. *
  401. * @param {vec4} out the receiving vector
  402. * @param {vec4} a the first operand
  403. * @param {vec4} b the second operand
  404. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  405. * @returns {vec4} out
  406. */
  407. function lerp(out, a, b, t) {
  408. var ax = a[0];
  409. var ay = a[1];
  410. var az = a[2];
  411. var aw = a[3];
  412. out[0] = ax + t * (b[0] - ax);
  413. out[1] = ay + t * (b[1] - ay);
  414. out[2] = az + t * (b[2] - az);
  415. out[3] = aw + t * (b[3] - aw);
  416. return out;
  417. }
  418. /**
  419. * Generates a random vector with the given scale
  420. *
  421. * @param {vec4} out the receiving vector
  422. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  423. * @returns {vec4} out
  424. */
  425. function random(out, scale) {
  426. scale = scale || 1.0;
  427. // Marsaglia, George. Choosing a Point from the Surface of a
  428. // Sphere. Ann. Math. Statist. 43 (1972), no. 2, 645--646.
  429. // http://projecteuclid.org/euclid.aoms/1177692644;
  430. var v1, v2, v3, v4;
  431. var s1, s2;
  432. do {
  433. v1 = glMatrix.RANDOM() * 2 - 1;
  434. v2 = glMatrix.RANDOM() * 2 - 1;
  435. s1 = v1 * v1 + v2 * v2;
  436. } while (s1 >= 1);
  437. do {
  438. v3 = glMatrix.RANDOM() * 2 - 1;
  439. v4 = glMatrix.RANDOM() * 2 - 1;
  440. s2 = v3 * v3 + v4 * v4;
  441. } while (s2 >= 1);
  442. var d = Math.sqrt((1 - s1) / s2);
  443. out[0] = scale * v1;
  444. out[1] = scale * v2;
  445. out[2] = scale * v3 * d;
  446. out[3] = scale * v4 * d;
  447. return out;
  448. }
  449. /**
  450. * Transforms the vec4 with a mat4.
  451. *
  452. * @param {vec4} out the receiving vector
  453. * @param {vec4} a the vector to transform
  454. * @param {mat4} m matrix to transform with
  455. * @returns {vec4} out
  456. */
  457. function transformMat4(out, a, m) {
  458. var x = a[0],
  459. y = a[1],
  460. z = a[2],
  461. w = a[3];
  462. out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
  463. out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
  464. out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
  465. out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
  466. return out;
  467. }
  468. /**
  469. * Transforms the vec4 with a quat
  470. *
  471. * @param {vec4} out the receiving vector
  472. * @param {vec4} a the vector to transform
  473. * @param {quat} q quaternion to transform with
  474. * @returns {vec4} out
  475. */
  476. function transformQuat(out, a, q) {
  477. var x = a[0],
  478. y = a[1],
  479. z = a[2];
  480. var qx = q[0],
  481. qy = q[1],
  482. qz = q[2],
  483. qw = q[3];
  484. // calculate quat * vec
  485. var ix = qw * x + qy * z - qz * y;
  486. var iy = qw * y + qz * x - qx * z;
  487. var iz = qw * z + qx * y - qy * x;
  488. var iw = -qx * x - qy * y - qz * z;
  489. // calculate result * inverse quat
  490. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  491. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  492. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  493. out[3] = a[3];
  494. return out;
  495. }
  496. /**
  497. * Returns a string representation of a vector
  498. *
  499. * @param {vec4} a vector to represent as a string
  500. * @returns {String} string representation of the vector
  501. */
  502. function str(a) {
  503. return 'vec4(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ')';
  504. }
  505. /**
  506. * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
  507. *
  508. * @param {vec4} a The first vector.
  509. * @param {vec4} b The second vector.
  510. * @returns {Boolean} True if the vectors are equal, false otherwise.
  511. */
  512. function exactEquals(a, b) {
  513. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
  514. }
  515. /**
  516. * Returns whether or not the vectors have approximately the same elements in the same position.
  517. *
  518. * @param {vec4} a The first vector.
  519. * @param {vec4} b The second vector.
  520. * @returns {Boolean} True if the vectors are equal, false otherwise.
  521. */
  522. function equals(a, b) {
  523. var a0 = a[0],
  524. a1 = a[1],
  525. a2 = a[2],
  526. a3 = a[3];
  527. var b0 = b[0],
  528. b1 = b[1],
  529. b2 = b[2],
  530. b3 = b[3];
  531. return Math.abs(a0 - b0) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3));
  532. }
  533. /**
  534. * Alias for {@link vec4.subtract}
  535. * @function
  536. */
  537. var sub = exports.sub = subtract;
  538. /**
  539. * Alias for {@link vec4.multiply}
  540. * @function
  541. */
  542. var mul = exports.mul = multiply;
  543. /**
  544. * Alias for {@link vec4.divide}
  545. * @function
  546. */
  547. var div = exports.div = divide;
  548. /**
  549. * Alias for {@link vec4.distance}
  550. * @function
  551. */
  552. var dist = exports.dist = distance;
  553. /**
  554. * Alias for {@link vec4.squaredDistance}
  555. * @function
  556. */
  557. var sqrDist = exports.sqrDist = squaredDistance;
  558. /**
  559. * Alias for {@link vec4.length}
  560. * @function
  561. */
  562. var len = exports.len = length;
  563. /**
  564. * Alias for {@link vec4.squaredLength}
  565. * @function
  566. */
  567. var sqrLen = exports.sqrLen = squaredLength;
  568. /**
  569. * Perform some operation over an array of vec4s.
  570. *
  571. * @param {Array} a the array of vectors to iterate over
  572. * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
  573. * @param {Number} offset Number of elements to skip at the beginning of the array
  574. * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
  575. * @param {Function} fn Function to call for each vector in the array
  576. * @param {Object} [arg] additional argument to pass to fn
  577. * @returns {Array} a
  578. * @function
  579. */
  580. var forEach = exports.forEach = function () {
  581. var vec = create();
  582. return function (a, stride, offset, count, fn, arg) {
  583. var i = void 0,
  584. l = void 0;
  585. if (!stride) {
  586. stride = 4;
  587. }
  588. if (!offset) {
  589. offset = 0;
  590. }
  591. if (count) {
  592. l = Math.min(count * stride + offset, a.length);
  593. } else {
  594. l = a.length;
  595. }
  596. for (i = offset; i < l; i += stride) {
  597. vec[0] = a[i];vec[1] = a[i + 1];vec[2] = a[i + 2];vec[3] = a[i + 3];
  598. fn(vec, vec, arg);
  599. a[i] = vec[0];a[i + 1] = vec[1];a[i + 2] = vec[2];a[i + 3] = vec[3];
  600. }
  601. return a;
  602. };
  603. }();