mat3.js.html 20 KB

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