vec4.js 15 KB

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