vec3.js 19 KB

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