mat3.js 18 KB

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