Source: node_modules/gl-matrix/esm/vec3.js

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