vec4.js.html 15 KB

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