Home Reference Source

viewer/glmatrix/mat3.js

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