mat3.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.sub = exports.mul = undefined;
  6. exports.create = create;
  7. exports.fromMat4 = fromMat4;
  8. exports.clone = clone;
  9. exports.copy = copy;
  10. exports.fromValues = fromValues;
  11. exports.set = set;
  12. exports.identity = identity;
  13. exports.transpose = transpose;
  14. exports.invert = invert;
  15. exports.adjoint = adjoint;
  16. exports.determinant = determinant;
  17. exports.multiply = multiply;
  18. exports.translate = translate;
  19. exports.rotate = rotate;
  20. exports.scale = scale;
  21. exports.fromTranslation = fromTranslation;
  22. exports.fromRotation = fromRotation;
  23. exports.fromScaling = fromScaling;
  24. exports.fromMat2d = fromMat2d;
  25. exports.fromQuat = fromQuat;
  26. exports.normalFromMat4 = normalFromMat4;
  27. exports.projection = projection;
  28. exports.str = str;
  29. exports.frob = frob;
  30. exports.add = add;
  31. exports.subtract = subtract;
  32. exports.multiplyScalar = multiplyScalar;
  33. exports.multiplyScalarAndAdd = multiplyScalarAndAdd;
  34. exports.exactEquals = exactEquals;
  35. exports.equals = equals;
  36. var _common = require('./common.js');
  37. var glMatrix = _interopRequireWildcard(_common);
  38. 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; } }
  39. /**
  40. * 3x3 Matrix
  41. * @module mat3
  42. */
  43. /**
  44. * Creates a new identity mat3
  45. *
  46. * @returns {mat3} a new 3x3 matrix
  47. */
  48. function create() {
  49. var out = new glMatrix.ARRAY_TYPE(9);
  50. if (glMatrix.ARRAY_TYPE != Float32Array) {
  51. out[1] = 0;
  52. out[2] = 0;
  53. out[3] = 0;
  54. out[5] = 0;
  55. out[6] = 0;
  56. out[7] = 0;
  57. }
  58. out[0] = 1;
  59. out[4] = 1;
  60. out[8] = 1;
  61. return out;
  62. }
  63. /**
  64. * Copies the upper-left 3x3 values into the given mat3.
  65. *
  66. * @param {mat3} out the receiving 3x3 matrix
  67. * @param {mat4} a the source 4x4 matrix
  68. * @returns {mat3} out
  69. */
  70. function fromMat4(out, a) {
  71. out[0] = a[0];
  72. out[1] = a[1];
  73. out[2] = a[2];
  74. out[3] = a[4];
  75. out[4] = a[5];
  76. out[5] = a[6];
  77. out[6] = a[8];
  78. out[7] = a[9];
  79. out[8] = a[10];
  80. return out;
  81. }
  82. /**
  83. * Creates a new mat3 initialized with values from an existing matrix
  84. *
  85. * @param {mat3} a matrix to clone
  86. * @returns {mat3} a new 3x3 matrix
  87. */
  88. function clone(a) {
  89. var out = new glMatrix.ARRAY_TYPE(9);
  90. out[0] = a[0];
  91. out[1] = a[1];
  92. out[2] = a[2];
  93. out[3] = a[3];
  94. out[4] = a[4];
  95. out[5] = a[5];
  96. out[6] = a[6];
  97. out[7] = a[7];
  98. out[8] = a[8];
  99. return out;
  100. }
  101. /**
  102. * Copy the values from one mat3 to another
  103. *
  104. * @param {mat3} out the receiving matrix
  105. * @param {mat3} a the source matrix
  106. * @returns {mat3} out
  107. */
  108. function copy(out, a) {
  109. out[0] = a[0];
  110. out[1] = a[1];
  111. out[2] = a[2];
  112. out[3] = a[3];
  113. out[4] = a[4];
  114. out[5] = a[5];
  115. out[6] = a[6];
  116. out[7] = a[7];
  117. out[8] = a[8];
  118. return out;
  119. }
  120. /**
  121. * Create a new mat3 with the given values
  122. *
  123. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  124. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  125. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  126. * @param {Number} m10 Component in column 1, row 0 position (index 3)
  127. * @param {Number} m11 Component in column 1, row 1 position (index 4)
  128. * @param {Number} m12 Component in column 1, row 2 position (index 5)
  129. * @param {Number} m20 Component in column 2, row 0 position (index 6)
  130. * @param {Number} m21 Component in column 2, row 1 position (index 7)
  131. * @param {Number} m22 Component in column 2, row 2 position (index 8)
  132. * @returns {mat3} A new mat3
  133. */
  134. function fromValues(m00, m01, m02, m10, m11, m12, m20, m21, m22) {
  135. var out = new glMatrix.ARRAY_TYPE(9);
  136. out[0] = m00;
  137. out[1] = m01;
  138. out[2] = m02;
  139. out[3] = m10;
  140. out[4] = m11;
  141. out[5] = m12;
  142. out[6] = m20;
  143. out[7] = m21;
  144. out[8] = m22;
  145. return out;
  146. }
  147. /**
  148. * Set the components of a mat3 to the given values
  149. *
  150. * @param {mat3} out the receiving matrix
  151. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  152. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  153. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  154. * @param {Number} m10 Component in column 1, row 0 position (index 3)
  155. * @param {Number} m11 Component in column 1, row 1 position (index 4)
  156. * @param {Number} m12 Component in column 1, row 2 position (index 5)
  157. * @param {Number} m20 Component in column 2, row 0 position (index 6)
  158. * @param {Number} m21 Component in column 2, row 1 position (index 7)
  159. * @param {Number} m22 Component in column 2, row 2 position (index 8)
  160. * @returns {mat3} out
  161. */
  162. function set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) {
  163. out[0] = m00;
  164. out[1] = m01;
  165. out[2] = m02;
  166. out[3] = m10;
  167. out[4] = m11;
  168. out[5] = m12;
  169. out[6] = m20;
  170. out[7] = m21;
  171. out[8] = m22;
  172. return out;
  173. }
  174. /**
  175. * Set a mat3 to the identity matrix
  176. *
  177. * @param {mat3} out the receiving matrix
  178. * @returns {mat3} out
  179. */
  180. function identity(out) {
  181. out[0] = 1;
  182. out[1] = 0;
  183. out[2] = 0;
  184. out[3] = 0;
  185. out[4] = 1;
  186. out[5] = 0;
  187. out[6] = 0;
  188. out[7] = 0;
  189. out[8] = 1;
  190. return out;
  191. }
  192. /**
  193. * Transpose the values of a mat3
  194. *
  195. * @param {mat3} out the receiving matrix
  196. * @param {mat3} a the source matrix
  197. * @returns {mat3} out
  198. */
  199. function transpose(out, a) {
  200. // If we are transposing ourselves we can skip a few steps but have to cache some values
  201. if (out === a) {
  202. var a01 = a[1],
  203. a02 = a[2],
  204. a12 = a[5];
  205. out[1] = a[3];
  206. out[2] = a[6];
  207. out[3] = a01;
  208. out[5] = a[7];
  209. out[6] = a02;
  210. out[7] = a12;
  211. } else {
  212. out[0] = a[0];
  213. out[1] = a[3];
  214. out[2] = a[6];
  215. out[3] = a[1];
  216. out[4] = a[4];
  217. out[5] = a[7];
  218. out[6] = a[2];
  219. out[7] = a[5];
  220. out[8] = a[8];
  221. }
  222. return out;
  223. }
  224. /**
  225. * Inverts a mat3
  226. *
  227. * @param {mat3} out the receiving matrix
  228. * @param {mat3} a the source matrix
  229. * @returns {mat3} out
  230. */
  231. function invert(out, a) {
  232. var a00 = a[0],
  233. a01 = a[1],
  234. a02 = a[2];
  235. var a10 = a[3],
  236. a11 = a[4],
  237. a12 = a[5];
  238. var a20 = a[6],
  239. a21 = a[7],
  240. a22 = a[8];
  241. var b01 = a22 * a11 - a12 * a21;
  242. var b11 = -a22 * a10 + a12 * a20;
  243. var b21 = a21 * a10 - a11 * a20;
  244. // Calculate the determinant
  245. var det = a00 * b01 + a01 * b11 + a02 * b21;
  246. if (!det) {
  247. return null;
  248. }
  249. det = 1.0 / det;
  250. out[0] = b01 * det;
  251. out[1] = (-a22 * a01 + a02 * a21) * det;
  252. out[2] = (a12 * a01 - a02 * a11) * det;
  253. out[3] = b11 * det;
  254. out[4] = (a22 * a00 - a02 * a20) * det;
  255. out[5] = (-a12 * a00 + a02 * a10) * det;
  256. out[6] = b21 * det;
  257. out[7] = (-a21 * a00 + a01 * a20) * det;
  258. out[8] = (a11 * a00 - a01 * a10) * det;
  259. return out;
  260. }
  261. /**
  262. * Calculates the adjugate of a mat3
  263. *
  264. * @param {mat3} out the receiving matrix
  265. * @param {mat3} a the source matrix
  266. * @returns {mat3} out
  267. */
  268. function adjoint(out, a) {
  269. var a00 = a[0],
  270. a01 = a[1],
  271. a02 = a[2];
  272. var a10 = a[3],
  273. a11 = a[4],
  274. a12 = a[5];
  275. var a20 = a[6],
  276. a21 = a[7],
  277. a22 = a[8];
  278. out[0] = a11 * a22 - a12 * a21;
  279. out[1] = a02 * a21 - a01 * a22;
  280. out[2] = a01 * a12 - a02 * a11;
  281. out[3] = a12 * a20 - a10 * a22;
  282. out[4] = a00 * a22 - a02 * a20;
  283. out[5] = a02 * a10 - a00 * a12;
  284. out[6] = a10 * a21 - a11 * a20;
  285. out[7] = a01 * a20 - a00 * a21;
  286. out[8] = a00 * a11 - a01 * a10;
  287. return out;
  288. }
  289. /**
  290. * Calculates the determinant of a mat3
  291. *
  292. * @param {mat3} a the source matrix
  293. * @returns {Number} determinant of a
  294. */
  295. function determinant(a) {
  296. var a00 = a[0],
  297. a01 = a[1],
  298. a02 = a[2];
  299. var a10 = a[3],
  300. a11 = a[4],
  301. a12 = a[5];
  302. var a20 = a[6],
  303. a21 = a[7],
  304. a22 = a[8];
  305. return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
  306. }
  307. /**
  308. * Multiplies two mat3's
  309. *
  310. * @param {mat3} out the receiving matrix
  311. * @param {mat3} a the first operand
  312. * @param {mat3} b the second operand
  313. * @returns {mat3} out
  314. */
  315. function multiply(out, a, b) {
  316. var a00 = a[0],
  317. a01 = a[1],
  318. a02 = a[2];
  319. var a10 = a[3],
  320. a11 = a[4],
  321. a12 = a[5];
  322. var a20 = a[6],
  323. a21 = a[7],
  324. a22 = a[8];
  325. var b00 = b[0],
  326. b01 = b[1],
  327. b02 = b[2];
  328. var b10 = b[3],
  329. b11 = b[4],
  330. b12 = b[5];
  331. var b20 = b[6],
  332. b21 = b[7],
  333. b22 = b[8];
  334. out[0] = b00 * a00 + b01 * a10 + b02 * a20;
  335. out[1] = b00 * a01 + b01 * a11 + b02 * a21;
  336. out[2] = b00 * a02 + b01 * a12 + b02 * a22;
  337. out[3] = b10 * a00 + b11 * a10 + b12 * a20;
  338. out[4] = b10 * a01 + b11 * a11 + b12 * a21;
  339. out[5] = b10 * a02 + b11 * a12 + b12 * a22;
  340. out[6] = b20 * a00 + b21 * a10 + b22 * a20;
  341. out[7] = b20 * a01 + b21 * a11 + b22 * a21;
  342. out[8] = b20 * a02 + b21 * a12 + b22 * a22;
  343. return out;
  344. }
  345. /**
  346. * Translate a mat3 by the given vector
  347. *
  348. * @param {mat3} out the receiving matrix
  349. * @param {mat3} a the matrix to translate
  350. * @param {vec2} v vector to translate by
  351. * @returns {mat3} out
  352. */
  353. function translate(out, a, v) {
  354. var a00 = a[0],
  355. a01 = a[1],
  356. a02 = a[2],
  357. a10 = a[3],
  358. a11 = a[4],
  359. a12 = a[5],
  360. a20 = a[6],
  361. a21 = a[7],
  362. a22 = a[8],
  363. x = v[0],
  364. y = v[1];
  365. out[0] = a00;
  366. out[1] = a01;
  367. out[2] = a02;
  368. out[3] = a10;
  369. out[4] = a11;
  370. out[5] = a12;
  371. out[6] = x * a00 + y * a10 + a20;
  372. out[7] = x * a01 + y * a11 + a21;
  373. out[8] = x * a02 + y * a12 + a22;
  374. return out;
  375. }
  376. /**
  377. * Rotates a mat3 by the given angle
  378. *
  379. * @param {mat3} out the receiving matrix
  380. * @param {mat3} a the matrix to rotate
  381. * @param {Number} rad the angle to rotate the matrix by
  382. * @returns {mat3} out
  383. */
  384. function rotate(out, a, rad) {
  385. var a00 = a[0],
  386. a01 = a[1],
  387. a02 = a[2],
  388. a10 = a[3],
  389. a11 = a[4],
  390. a12 = a[5],
  391. a20 = a[6],
  392. a21 = a[7],
  393. a22 = a[8],
  394. s = Math.sin(rad),
  395. c = Math.cos(rad);
  396. out[0] = c * a00 + s * a10;
  397. out[1] = c * a01 + s * a11;
  398. out[2] = c * a02 + s * a12;
  399. out[3] = c * a10 - s * a00;
  400. out[4] = c * a11 - s * a01;
  401. out[5] = c * a12 - s * a02;
  402. out[6] = a20;
  403. out[7] = a21;
  404. out[8] = a22;
  405. return out;
  406. };
  407. /**
  408. * Scales the mat3 by the dimensions in the given vec2
  409. *
  410. * @param {mat3} out the receiving matrix
  411. * @param {mat3} a the matrix to rotate
  412. * @param {vec2} v the vec2 to scale the matrix by
  413. * @returns {mat3} out
  414. **/
  415. function scale(out, a, v) {
  416. var x = v[0],
  417. y = v[1];
  418. out[0] = x * a[0];
  419. out[1] = x * a[1];
  420. out[2] = x * a[2];
  421. out[3] = y * a[3];
  422. out[4] = y * a[4];
  423. out[5] = y * a[5];
  424. out[6] = a[6];
  425. out[7] = a[7];
  426. out[8] = a[8];
  427. return out;
  428. }
  429. /**
  430. * Creates a matrix from a vector translation
  431. * This is equivalent to (but much faster than):
  432. *
  433. * mat3.identity(dest);
  434. * mat3.translate(dest, dest, vec);
  435. *
  436. * @param {mat3} out mat3 receiving operation result
  437. * @param {vec2} v Translation vector
  438. * @returns {mat3} out
  439. */
  440. function fromTranslation(out, v) {
  441. out[0] = 1;
  442. out[1] = 0;
  443. out[2] = 0;
  444. out[3] = 0;
  445. out[4] = 1;
  446. out[5] = 0;
  447. out[6] = v[0];
  448. out[7] = v[1];
  449. out[8] = 1;
  450. return out;
  451. }
  452. /**
  453. * Creates a matrix from a given angle
  454. * This is equivalent to (but much faster than):
  455. *
  456. * mat3.identity(dest);
  457. * mat3.rotate(dest, dest, rad);
  458. *
  459. * @param {mat3} out mat3 receiving operation result
  460. * @param {Number} rad the angle to rotate the matrix by
  461. * @returns {mat3} out
  462. */
  463. function fromRotation(out, rad) {
  464. var s = Math.sin(rad),
  465. c = Math.cos(rad);
  466. out[0] = c;
  467. out[1] = s;
  468. out[2] = 0;
  469. out[3] = -s;
  470. out[4] = c;
  471. out[5] = 0;
  472. out[6] = 0;
  473. out[7] = 0;
  474. out[8] = 1;
  475. return out;
  476. }
  477. /**
  478. * Creates a matrix from a vector scaling
  479. * This is equivalent to (but much faster than):
  480. *
  481. * mat3.identity(dest);
  482. * mat3.scale(dest, dest, vec);
  483. *
  484. * @param {mat3} out mat3 receiving operation result
  485. * @param {vec2} v Scaling vector
  486. * @returns {mat3} out
  487. */
  488. function fromScaling(out, v) {
  489. out[0] = v[0];
  490. out[1] = 0;
  491. out[2] = 0;
  492. out[3] = 0;
  493. out[4] = v[1];
  494. out[5] = 0;
  495. out[6] = 0;
  496. out[7] = 0;
  497. out[8] = 1;
  498. return out;
  499. }
  500. /**
  501. * Copies the values from a mat2d into a mat3
  502. *
  503. * @param {mat3} out the receiving matrix
  504. * @param {mat2d} a the matrix to copy
  505. * @returns {mat3} out
  506. **/
  507. function fromMat2d(out, a) {
  508. out[0] = a[0];
  509. out[1] = a[1];
  510. out[2] = 0;
  511. out[3] = a[2];
  512. out[4] = a[3];
  513. out[5] = 0;
  514. out[6] = a[4];
  515. out[7] = a[5];
  516. out[8] = 1;
  517. return out;
  518. }
  519. /**
  520. * Calculates a 3x3 matrix from the given quaternion
  521. *
  522. * @param {mat3} out mat3 receiving operation result
  523. * @param {quat} q Quaternion to create matrix from
  524. *
  525. * @returns {mat3} out
  526. */
  527. function fromQuat(out, q) {
  528. var x = q[0],
  529. y = q[1],
  530. z = q[2],
  531. w = q[3];
  532. var x2 = x + x;
  533. var y2 = y + y;
  534. var z2 = z + z;
  535. var xx = x * x2;
  536. var yx = y * x2;
  537. var yy = y * y2;
  538. var zx = z * x2;
  539. var zy = z * y2;
  540. var zz = z * z2;
  541. var wx = w * x2;
  542. var wy = w * y2;
  543. var wz = w * z2;
  544. out[0] = 1 - yy - zz;
  545. out[3] = yx - wz;
  546. out[6] = zx + wy;
  547. out[1] = yx + wz;
  548. out[4] = 1 - xx - zz;
  549. out[7] = zy - wx;
  550. out[2] = zx - wy;
  551. out[5] = zy + wx;
  552. out[8] = 1 - xx - yy;
  553. return out;
  554. }
  555. /**
  556. * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
  557. *
  558. * @param {mat3} out mat3 receiving operation result
  559. * @param {mat4} a Mat4 to derive the normal matrix from
  560. *
  561. * @returns {mat3} out
  562. */
  563. function normalFromMat4(out, a) {
  564. var a00 = a[0],
  565. a01 = a[1],
  566. a02 = a[2],
  567. a03 = a[3];
  568. var a10 = a[4],
  569. a11 = a[5],
  570. a12 = a[6],
  571. a13 = a[7];
  572. var a20 = a[8],
  573. a21 = a[9],
  574. a22 = a[10],
  575. a23 = a[11];
  576. var a30 = a[12],
  577. a31 = a[13],
  578. a32 = a[14],
  579. a33 = a[15];
  580. var b00 = a00 * a11 - a01 * a10;
  581. var b01 = a00 * a12 - a02 * a10;
  582. var b02 = a00 * a13 - a03 * a10;
  583. var b03 = a01 * a12 - a02 * a11;
  584. var b04 = a01 * a13 - a03 * a11;
  585. var b05 = a02 * a13 - a03 * a12;
  586. var b06 = a20 * a31 - a21 * a30;
  587. var b07 = a20 * a32 - a22 * a30;
  588. var b08 = a20 * a33 - a23 * a30;
  589. var b09 = a21 * a32 - a22 * a31;
  590. var b10 = a21 * a33 - a23 * a31;
  591. var b11 = a22 * a33 - a23 * a32;
  592. // Calculate the determinant
  593. var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  594. if (!det) {
  595. return null;
  596. }
  597. det = 1.0 / det;
  598. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  599. out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  600. out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  601. out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  602. out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  603. out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  604. out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  605. out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  606. out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  607. return out;
  608. }
  609. /**
  610. * Generates a 2D projection matrix with the given bounds
  611. *
  612. * @param {mat3} out mat3 frustum matrix will be written into
  613. * @param {number} width Width of your gl context
  614. * @param {number} height Height of gl context
  615. * @returns {mat3} out
  616. */
  617. function projection(out, width, height) {
  618. out[0] = 2 / width;
  619. out[1] = 0;
  620. out[2] = 0;
  621. out[3] = 0;
  622. out[4] = -2 / height;
  623. out[5] = 0;
  624. out[6] = -1;
  625. out[7] = 1;
  626. out[8] = 1;
  627. return out;
  628. }
  629. /**
  630. * Returns a string representation of a mat3
  631. *
  632. * @param {mat3} a matrix to represent as a string
  633. * @returns {String} string representation of the matrix
  634. */
  635. function str(a) {
  636. return 'mat3(' + a[0] + ', ' + a[1] + ', ' + a[2] + ', ' + a[3] + ', ' + a[4] + ', ' + a[5] + ', ' + a[6] + ', ' + a[7] + ', ' + a[8] + ')';
  637. }
  638. /**
  639. * Returns Frobenius norm of a mat3
  640. *
  641. * @param {mat3} a the matrix to calculate Frobenius norm of
  642. * @returns {Number} Frobenius norm
  643. */
  644. function frob(a) {
  645. return Math.sqrt(Math.pow(a[0], 2) + Math.pow(a[1], 2) + Math.pow(a[2], 2) + Math.pow(a[3], 2) + Math.pow(a[4], 2) + Math.pow(a[5], 2) + Math.pow(a[6], 2) + Math.pow(a[7], 2) + Math.pow(a[8], 2));
  646. }
  647. /**
  648. * Adds two mat3's
  649. *
  650. * @param {mat3} out the receiving matrix
  651. * @param {mat3} a the first operand
  652. * @param {mat3} b the second operand
  653. * @returns {mat3} out
  654. */
  655. function add(out, a, b) {
  656. out[0] = a[0] + b[0];
  657. out[1] = a[1] + b[1];
  658. out[2] = a[2] + b[2];
  659. out[3] = a[3] + b[3];
  660. out[4] = a[4] + b[4];
  661. out[5] = a[5] + b[5];
  662. out[6] = a[6] + b[6];
  663. out[7] = a[7] + b[7];
  664. out[8] = a[8] + b[8];
  665. return out;
  666. }
  667. /**
  668. * Subtracts matrix b from matrix a
  669. *
  670. * @param {mat3} out the receiving matrix
  671. * @param {mat3} a the first operand
  672. * @param {mat3} b the second operand
  673. * @returns {mat3} out
  674. */
  675. function subtract(out, a, b) {
  676. out[0] = a[0] - b[0];
  677. out[1] = a[1] - b[1];
  678. out[2] = a[2] - b[2];
  679. out[3] = a[3] - b[3];
  680. out[4] = a[4] - b[4];
  681. out[5] = a[5] - b[5];
  682. out[6] = a[6] - b[6];
  683. out[7] = a[7] - b[7];
  684. out[8] = a[8] - b[8];
  685. return out;
  686. }
  687. /**
  688. * Multiply each element of the matrix by a scalar.
  689. *
  690. * @param {mat3} out the receiving matrix
  691. * @param {mat3} a the matrix to scale
  692. * @param {Number} b amount to scale the matrix's elements by
  693. * @returns {mat3} out
  694. */
  695. function multiplyScalar(out, a, b) {
  696. out[0] = a[0] * b;
  697. out[1] = a[1] * b;
  698. out[2] = a[2] * b;
  699. out[3] = a[3] * b;
  700. out[4] = a[4] * b;
  701. out[5] = a[5] * b;
  702. out[6] = a[6] * b;
  703. out[7] = a[7] * b;
  704. out[8] = a[8] * b;
  705. return out;
  706. }
  707. /**
  708. * Adds two mat3's after multiplying each element of the second operand by a scalar value.
  709. *
  710. * @param {mat3} out the receiving vector
  711. * @param {mat3} a the first operand
  712. * @param {mat3} b the second operand
  713. * @param {Number} scale the amount to scale b's elements by before adding
  714. * @returns {mat3} out
  715. */
  716. function multiplyScalarAndAdd(out, a, b, scale) {
  717. out[0] = a[0] + b[0] * scale;
  718. out[1] = a[1] + b[1] * scale;
  719. out[2] = a[2] + b[2] * scale;
  720. out[3] = a[3] + b[3] * scale;
  721. out[4] = a[4] + b[4] * scale;
  722. out[5] = a[5] + b[5] * scale;
  723. out[6] = a[6] + b[6] * scale;
  724. out[7] = a[7] + b[7] * scale;
  725. out[8] = a[8] + b[8] * scale;
  726. return out;
  727. }
  728. /**
  729. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  730. *
  731. * @param {mat3} a The first matrix.
  732. * @param {mat3} b The second matrix.
  733. * @returns {Boolean} True if the matrices are equal, false otherwise.
  734. */
  735. function exactEquals(a, b) {
  736. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8];
  737. }
  738. /**
  739. * Returns whether or not the matrices have approximately the same elements in the same position.
  740. *
  741. * @param {mat3} a The first matrix.
  742. * @param {mat3} b The second matrix.
  743. * @returns {Boolean} True if the matrices are equal, false otherwise.
  744. */
  745. function equals(a, b) {
  746. var a0 = a[0],
  747. a1 = a[1],
  748. a2 = a[2],
  749. a3 = a[3],
  750. a4 = a[4],
  751. a5 = a[5],
  752. a6 = a[6],
  753. a7 = a[7],
  754. a8 = a[8];
  755. var b0 = b[0],
  756. b1 = b[1],
  757. b2 = b[2],
  758. b3 = b[3],
  759. b4 = b[4],
  760. b5 = b[5],
  761. b6 = b[6],
  762. b7 = b[7],
  763. b8 = b[8];
  764. 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)) && Math.abs(a4 - b4) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= glMatrix.EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8));
  765. }
  766. /**
  767. * Alias for {@link mat3.multiply}
  768. * @function
  769. */
  770. var mul = exports.mul = multiply;
  771. /**
  772. * Alias for {@link mat3.subtract}
  773. * @function
  774. */
  775. var sub = exports.sub = subtract;