mat2.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import * as glMatrix from "./common.js";
  2. /**
  3. * 2x2 Matrix
  4. * @module mat2
  5. */
  6. /**
  7. * Creates a new identity mat2
  8. *
  9. * @returns {mat2} a new 2x2 matrix
  10. */
  11. export function create() {
  12. var out = new glMatrix.ARRAY_TYPE(4);
  13. if (glMatrix.ARRAY_TYPE != Float32Array) {
  14. out[1] = 0;
  15. out[2] = 0;
  16. }
  17. out[0] = 1;
  18. out[3] = 1;
  19. return out;
  20. }
  21. /**
  22. * Creates a new mat2 initialized with values from an existing matrix
  23. *
  24. * @param {ReadonlyMat2} a matrix to clone
  25. * @returns {mat2} a new 2x2 matrix
  26. */
  27. export function clone(a) {
  28. var out = new glMatrix.ARRAY_TYPE(4);
  29. out[0] = a[0];
  30. out[1] = a[1];
  31. out[2] = a[2];
  32. out[3] = a[3];
  33. return out;
  34. }
  35. /**
  36. * Copy the values from one mat2 to another
  37. *
  38. * @param {mat2} out the receiving matrix
  39. * @param {ReadonlyMat2} a the source matrix
  40. * @returns {mat2} out
  41. */
  42. export function copy(out, a) {
  43. out[0] = a[0];
  44. out[1] = a[1];
  45. out[2] = a[2];
  46. out[3] = a[3];
  47. return out;
  48. }
  49. /**
  50. * Set a mat2 to the identity matrix
  51. *
  52. * @param {mat2} out the receiving matrix
  53. * @returns {mat2} out
  54. */
  55. export function identity(out) {
  56. out[0] = 1;
  57. out[1] = 0;
  58. out[2] = 0;
  59. out[3] = 1;
  60. return out;
  61. }
  62. /**
  63. * Create a new mat2 with the given values
  64. *
  65. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  66. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  67. * @param {Number} m10 Component in column 1, row 0 position (index 2)
  68. * @param {Number} m11 Component in column 1, row 1 position (index 3)
  69. * @returns {mat2} out A new 2x2 matrix
  70. */
  71. export function fromValues(m00, m01, m10, m11) {
  72. var out = new glMatrix.ARRAY_TYPE(4);
  73. out[0] = m00;
  74. out[1] = m01;
  75. out[2] = m10;
  76. out[3] = m11;
  77. return out;
  78. }
  79. /**
  80. * Set the components of a mat2 to the given values
  81. *
  82. * @param {mat2} out the receiving matrix
  83. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  84. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  85. * @param {Number} m10 Component in column 1, row 0 position (index 2)
  86. * @param {Number} m11 Component in column 1, row 1 position (index 3)
  87. * @returns {mat2} out
  88. */
  89. export function set(out, m00, m01, m10, m11) {
  90. out[0] = m00;
  91. out[1] = m01;
  92. out[2] = m10;
  93. out[3] = m11;
  94. return out;
  95. }
  96. /**
  97. * Transpose the values of a mat2
  98. *
  99. * @param {mat2} out the receiving matrix
  100. * @param {ReadonlyMat2} a the source matrix
  101. * @returns {mat2} out
  102. */
  103. export function transpose(out, a) {
  104. // If we are transposing ourselves we can skip a few steps but have to cache
  105. // some values
  106. if (out === a) {
  107. var a1 = a[1];
  108. out[1] = a[2];
  109. out[2] = a1;
  110. } else {
  111. out[0] = a[0];
  112. out[1] = a[2];
  113. out[2] = a[1];
  114. out[3] = a[3];
  115. }
  116. return out;
  117. }
  118. /**
  119. * Inverts a mat2
  120. *
  121. * @param {mat2} out the receiving matrix
  122. * @param {ReadonlyMat2} a the source matrix
  123. * @returns {mat2} out
  124. */
  125. export function invert(out, a) {
  126. var a0 = a[0],
  127. a1 = a[1],
  128. a2 = a[2],
  129. a3 = a[3]; // Calculate the determinant
  130. var det = a0 * a3 - a2 * a1;
  131. if (!det) {
  132. return null;
  133. }
  134. det = 1.0 / det;
  135. out[0] = a3 * det;
  136. out[1] = -a1 * det;
  137. out[2] = -a2 * det;
  138. out[3] = a0 * det;
  139. return out;
  140. }
  141. /**
  142. * Calculates the adjugate of a mat2
  143. *
  144. * @param {mat2} out the receiving matrix
  145. * @param {ReadonlyMat2} a the source matrix
  146. * @returns {mat2} out
  147. */
  148. export function adjoint(out, a) {
  149. // Caching this value is nessecary if out == a
  150. var a0 = a[0];
  151. out[0] = a[3];
  152. out[1] = -a[1];
  153. out[2] = -a[2];
  154. out[3] = a0;
  155. return out;
  156. }
  157. /**
  158. * Calculates the determinant of a mat2
  159. *
  160. * @param {ReadonlyMat2} a the source matrix
  161. * @returns {Number} determinant of a
  162. */
  163. export function determinant(a) {
  164. return a[0] * a[3] - a[2] * a[1];
  165. }
  166. /**
  167. * Multiplies two mat2's
  168. *
  169. * @param {mat2} out the receiving matrix
  170. * @param {ReadonlyMat2} a the first operand
  171. * @param {ReadonlyMat2} b the second operand
  172. * @returns {mat2} out
  173. */
  174. export function multiply(out, a, b) {
  175. var a0 = a[0],
  176. a1 = a[1],
  177. a2 = a[2],
  178. a3 = a[3];
  179. var b0 = b[0],
  180. b1 = b[1],
  181. b2 = b[2],
  182. b3 = b[3];
  183. out[0] = a0 * b0 + a2 * b1;
  184. out[1] = a1 * b0 + a3 * b1;
  185. out[2] = a0 * b2 + a2 * b3;
  186. out[3] = a1 * b2 + a3 * b3;
  187. return out;
  188. }
  189. /**
  190. * Rotates a mat2 by the given angle
  191. *
  192. * @param {mat2} out the receiving matrix
  193. * @param {ReadonlyMat2} a the matrix to rotate
  194. * @param {Number} rad the angle to rotate the matrix by
  195. * @returns {mat2} out
  196. */
  197. export function rotate(out, a, rad) {
  198. var a0 = a[0],
  199. a1 = a[1],
  200. a2 = a[2],
  201. a3 = a[3];
  202. var s = Math.sin(rad);
  203. var c = Math.cos(rad);
  204. out[0] = a0 * c + a2 * s;
  205. out[1] = a1 * c + a3 * s;
  206. out[2] = a0 * -s + a2 * c;
  207. out[3] = a1 * -s + a3 * c;
  208. return out;
  209. }
  210. /**
  211. * Scales the mat2 by the dimensions in the given vec2
  212. *
  213. * @param {mat2} out the receiving matrix
  214. * @param {ReadonlyMat2} a the matrix to rotate
  215. * @param {ReadonlyVec2} v the vec2 to scale the matrix by
  216. * @returns {mat2} out
  217. **/
  218. export function scale(out, a, v) {
  219. var a0 = a[0],
  220. a1 = a[1],
  221. a2 = a[2],
  222. a3 = a[3];
  223. var v0 = v[0],
  224. v1 = v[1];
  225. out[0] = a0 * v0;
  226. out[1] = a1 * v0;
  227. out[2] = a2 * v1;
  228. out[3] = a3 * v1;
  229. return out;
  230. }
  231. /**
  232. * Creates a matrix from a given angle
  233. * This is equivalent to (but much faster than):
  234. *
  235. * mat2.identity(dest);
  236. * mat2.rotate(dest, dest, rad);
  237. *
  238. * @param {mat2} out mat2 receiving operation result
  239. * @param {Number} rad the angle to rotate the matrix by
  240. * @returns {mat2} out
  241. */
  242. export function fromRotation(out, rad) {
  243. var s = Math.sin(rad);
  244. var c = Math.cos(rad);
  245. out[0] = c;
  246. out[1] = s;
  247. out[2] = -s;
  248. out[3] = c;
  249. return out;
  250. }
  251. /**
  252. * Creates a matrix from a vector scaling
  253. * This is equivalent to (but much faster than):
  254. *
  255. * mat2.identity(dest);
  256. * mat2.scale(dest, dest, vec);
  257. *
  258. * @param {mat2} out mat2 receiving operation result
  259. * @param {ReadonlyVec2} v Scaling vector
  260. * @returns {mat2} out
  261. */
  262. export function fromScaling(out, v) {
  263. out[0] = v[0];
  264. out[1] = 0;
  265. out[2] = 0;
  266. out[3] = v[1];
  267. return out;
  268. }
  269. /**
  270. * Returns a string representation of a mat2
  271. *
  272. * @param {ReadonlyMat2} a matrix to represent as a string
  273. * @returns {String} string representation of the matrix
  274. */
  275. export function str(a) {
  276. return "mat2(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
  277. }
  278. /**
  279. * Returns Frobenius norm of a mat2
  280. *
  281. * @param {ReadonlyMat2} a the matrix to calculate Frobenius norm of
  282. * @returns {Number} Frobenius norm
  283. */
  284. export function frob(a) {
  285. return Math.hypot(a[0], a[1], a[2], a[3]);
  286. }
  287. /**
  288. * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
  289. * @param {ReadonlyMat2} L the lower triangular matrix
  290. * @param {ReadonlyMat2} D the diagonal matrix
  291. * @param {ReadonlyMat2} U the upper triangular matrix
  292. * @param {ReadonlyMat2} a the input matrix to factorize
  293. */
  294. export function LDU(L, D, U, a) {
  295. L[2] = a[2] / a[0];
  296. U[0] = a[0];
  297. U[1] = a[1];
  298. U[3] = a[3] - L[2] * U[1];
  299. return [L, D, U];
  300. }
  301. /**
  302. * Adds two mat2's
  303. *
  304. * @param {mat2} out the receiving matrix
  305. * @param {ReadonlyMat2} a the first operand
  306. * @param {ReadonlyMat2} b the second operand
  307. * @returns {mat2} out
  308. */
  309. export function add(out, a, b) {
  310. out[0] = a[0] + b[0];
  311. out[1] = a[1] + b[1];
  312. out[2] = a[2] + b[2];
  313. out[3] = a[3] + b[3];
  314. return out;
  315. }
  316. /**
  317. * Subtracts matrix b from matrix a
  318. *
  319. * @param {mat2} out the receiving matrix
  320. * @param {ReadonlyMat2} a the first operand
  321. * @param {ReadonlyMat2} b the second operand
  322. * @returns {mat2} out
  323. */
  324. export function subtract(out, a, b) {
  325. out[0] = a[0] - b[0];
  326. out[1] = a[1] - b[1];
  327. out[2] = a[2] - b[2];
  328. out[3] = a[3] - b[3];
  329. return out;
  330. }
  331. /**
  332. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  333. *
  334. * @param {ReadonlyMat2} a The first matrix.
  335. * @param {ReadonlyMat2} b The second matrix.
  336. * @returns {Boolean} True if the matrices are equal, false otherwise.
  337. */
  338. export function exactEquals(a, b) {
  339. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
  340. }
  341. /**
  342. * Returns whether or not the matrices have approximately the same elements in the same position.
  343. *
  344. * @param {ReadonlyMat2} a The first matrix.
  345. * @param {ReadonlyMat2} b The second matrix.
  346. * @returns {Boolean} True if the matrices are equal, false otherwise.
  347. */
  348. export function equals(a, b) {
  349. var a0 = a[0],
  350. a1 = a[1],
  351. a2 = a[2],
  352. a3 = a[3];
  353. var b0 = b[0],
  354. b1 = b[1],
  355. b2 = b[2],
  356. b3 = b[3];
  357. 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));
  358. }
  359. /**
  360. * Multiply each element of the matrix by a scalar.
  361. *
  362. * @param {mat2} out the receiving matrix
  363. * @param {ReadonlyMat2} a the matrix to scale
  364. * @param {Number} b amount to scale the matrix's elements by
  365. * @returns {mat2} out
  366. */
  367. export function multiplyScalar(out, a, b) {
  368. out[0] = a[0] * b;
  369. out[1] = a[1] * b;
  370. out[2] = a[2] * b;
  371. out[3] = a[3] * b;
  372. return out;
  373. }
  374. /**
  375. * Adds two mat2's after multiplying each element of the second operand by a scalar value.
  376. *
  377. * @param {mat2} out the receiving vector
  378. * @param {ReadonlyMat2} a the first operand
  379. * @param {ReadonlyMat2} b the second operand
  380. * @param {Number} scale the amount to scale b's elements by before adding
  381. * @returns {mat2} out
  382. */
  383. export function multiplyScalarAndAdd(out, a, b, scale) {
  384. out[0] = a[0] + b[0] * scale;
  385. out[1] = a[1] + b[1] * scale;
  386. out[2] = a[2] + b[2] * scale;
  387. out[3] = a[3] + b[3] * scale;
  388. return out;
  389. }
  390. /**
  391. * Alias for {@link mat2.multiply}
  392. * @function
  393. */
  394. export var mul = multiply;
  395. /**
  396. * Alias for {@link mat2.subtract}
  397. * @function
  398. */
  399. export var sub = subtract;