vec3.js 21 KB

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