vec4.js 18 KB

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