mat2.js 12 KB

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