vec3.js.html 20 KB

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