mat2d.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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.invert = invert;
  13. exports.determinant = determinant;
  14. exports.multiply = multiply;
  15. exports.rotate = rotate;
  16. exports.scale = scale;
  17. exports.translate = translate;
  18. exports.fromRotation = fromRotation;
  19. exports.fromScaling = fromScaling;
  20. exports.fromTranslation = fromTranslation;
  21. exports.str = str;
  22. exports.frob = frob;
  23. exports.add = add;
  24. exports.subtract = subtract;
  25. exports.multiplyScalar = multiplyScalar;
  26. exports.multiplyScalarAndAdd = multiplyScalarAndAdd;
  27. exports.exactEquals = exactEquals;
  28. exports.equals = equals;
  29. exports.sub = exports.mul = void 0;
  30. var glMatrix = _interopRequireWildcard(require("./common.js"));
  31. 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); }
  32. 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; }
  33. /**
  34. * 2x3 Matrix
  35. * @module mat2d
  36. * @description
  37. * A mat2d contains six elements defined as:
  38. * <pre>
  39. * [a, b,
  40. * c, d,
  41. * tx, ty]
  42. * </pre>
  43. * This is a short form for the 3x3 matrix:
  44. * <pre>
  45. * [a, b, 0,
  46. * c, d, 0,
  47. * tx, ty, 1]
  48. * </pre>
  49. * The last column is ignored so the array is shorter and operations are faster.
  50. */
  51. /**
  52. * Creates a new identity mat2d
  53. *
  54. * @returns {mat2d} a new 2x3 matrix
  55. */
  56. function create() {
  57. var out = new glMatrix.ARRAY_TYPE(6);
  58. if (glMatrix.ARRAY_TYPE != Float32Array) {
  59. out[1] = 0;
  60. out[2] = 0;
  61. out[4] = 0;
  62. out[5] = 0;
  63. }
  64. out[0] = 1;
  65. out[3] = 1;
  66. return out;
  67. }
  68. /**
  69. * Creates a new mat2d initialized with values from an existing matrix
  70. *
  71. * @param {ReadonlyMat2d} a matrix to clone
  72. * @returns {mat2d} a new 2x3 matrix
  73. */
  74. function clone(a) {
  75. var out = new glMatrix.ARRAY_TYPE(6);
  76. out[0] = a[0];
  77. out[1] = a[1];
  78. out[2] = a[2];
  79. out[3] = a[3];
  80. out[4] = a[4];
  81. out[5] = a[5];
  82. return out;
  83. }
  84. /**
  85. * Copy the values from one mat2d to another
  86. *
  87. * @param {mat2d} out the receiving matrix
  88. * @param {ReadonlyMat2d} a the source matrix
  89. * @returns {mat2d} out
  90. */
  91. function copy(out, a) {
  92. out[0] = a[0];
  93. out[1] = a[1];
  94. out[2] = a[2];
  95. out[3] = a[3];
  96. out[4] = a[4];
  97. out[5] = a[5];
  98. return out;
  99. }
  100. /**
  101. * Set a mat2d to the identity matrix
  102. *
  103. * @param {mat2d} out the receiving matrix
  104. * @returns {mat2d} out
  105. */
  106. function identity(out) {
  107. out[0] = 1;
  108. out[1] = 0;
  109. out[2] = 0;
  110. out[3] = 1;
  111. out[4] = 0;
  112. out[5] = 0;
  113. return out;
  114. }
  115. /**
  116. * Create a new mat2d with the given values
  117. *
  118. * @param {Number} a Component A (index 0)
  119. * @param {Number} b Component B (index 1)
  120. * @param {Number} c Component C (index 2)
  121. * @param {Number} d Component D (index 3)
  122. * @param {Number} tx Component TX (index 4)
  123. * @param {Number} ty Component TY (index 5)
  124. * @returns {mat2d} A new mat2d
  125. */
  126. function fromValues(a, b, c, d, tx, ty) {
  127. var out = new glMatrix.ARRAY_TYPE(6);
  128. out[0] = a;
  129. out[1] = b;
  130. out[2] = c;
  131. out[3] = d;
  132. out[4] = tx;
  133. out[5] = ty;
  134. return out;
  135. }
  136. /**
  137. * Set the components of a mat2d to the given values
  138. *
  139. * @param {mat2d} out the receiving matrix
  140. * @param {Number} a Component A (index 0)
  141. * @param {Number} b Component B (index 1)
  142. * @param {Number} c Component C (index 2)
  143. * @param {Number} d Component D (index 3)
  144. * @param {Number} tx Component TX (index 4)
  145. * @param {Number} ty Component TY (index 5)
  146. * @returns {mat2d} out
  147. */
  148. function set(out, a, b, c, d, tx, ty) {
  149. out[0] = a;
  150. out[1] = b;
  151. out[2] = c;
  152. out[3] = d;
  153. out[4] = tx;
  154. out[5] = ty;
  155. return out;
  156. }
  157. /**
  158. * Inverts a mat2d
  159. *
  160. * @param {mat2d} out the receiving matrix
  161. * @param {ReadonlyMat2d} a the source matrix
  162. * @returns {mat2d} out
  163. */
  164. function invert(out, a) {
  165. var aa = a[0],
  166. ab = a[1],
  167. ac = a[2],
  168. ad = a[3];
  169. var atx = a[4],
  170. aty = a[5];
  171. var det = aa * ad - ab * ac;
  172. if (!det) {
  173. return null;
  174. }
  175. det = 1.0 / det;
  176. out[0] = ad * det;
  177. out[1] = -ab * det;
  178. out[2] = -ac * det;
  179. out[3] = aa * det;
  180. out[4] = (ac * aty - ad * atx) * det;
  181. out[5] = (ab * atx - aa * aty) * det;
  182. return out;
  183. }
  184. /**
  185. * Calculates the determinant of a mat2d
  186. *
  187. * @param {ReadonlyMat2d} a the source matrix
  188. * @returns {Number} determinant of a
  189. */
  190. function determinant(a) {
  191. return a[0] * a[3] - a[1] * a[2];
  192. }
  193. /**
  194. * Multiplies two mat2d's
  195. *
  196. * @param {mat2d} out the receiving matrix
  197. * @param {ReadonlyMat2d} a the first operand
  198. * @param {ReadonlyMat2d} b the second operand
  199. * @returns {mat2d} out
  200. */
  201. function multiply(out, a, b) {
  202. var a0 = a[0],
  203. a1 = a[1],
  204. a2 = a[2],
  205. a3 = a[3],
  206. a4 = a[4],
  207. a5 = a[5];
  208. var b0 = b[0],
  209. b1 = b[1],
  210. b2 = b[2],
  211. b3 = b[3],
  212. b4 = b[4],
  213. b5 = b[5];
  214. out[0] = a0 * b0 + a2 * b1;
  215. out[1] = a1 * b0 + a3 * b1;
  216. out[2] = a0 * b2 + a2 * b3;
  217. out[3] = a1 * b2 + a3 * b3;
  218. out[4] = a0 * b4 + a2 * b5 + a4;
  219. out[5] = a1 * b4 + a3 * b5 + a5;
  220. return out;
  221. }
  222. /**
  223. * Rotates a mat2d by the given angle
  224. *
  225. * @param {mat2d} out the receiving matrix
  226. * @param {ReadonlyMat2d} a the matrix to rotate
  227. * @param {Number} rad the angle to rotate the matrix by
  228. * @returns {mat2d} out
  229. */
  230. function rotate(out, a, rad) {
  231. var a0 = a[0],
  232. a1 = a[1],
  233. a2 = a[2],
  234. a3 = a[3],
  235. a4 = a[4],
  236. a5 = a[5];
  237. var s = Math.sin(rad);
  238. var c = Math.cos(rad);
  239. out[0] = a0 * c + a2 * s;
  240. out[1] = a1 * c + a3 * s;
  241. out[2] = a0 * -s + a2 * c;
  242. out[3] = a1 * -s + a3 * c;
  243. out[4] = a4;
  244. out[5] = a5;
  245. return out;
  246. }
  247. /**
  248. * Scales the mat2d by the dimensions in the given vec2
  249. *
  250. * @param {mat2d} out the receiving matrix
  251. * @param {ReadonlyMat2d} a the matrix to translate
  252. * @param {ReadonlyVec2} v the vec2 to scale the matrix by
  253. * @returns {mat2d} out
  254. **/
  255. function scale(out, a, v) {
  256. var a0 = a[0],
  257. a1 = a[1],
  258. a2 = a[2],
  259. a3 = a[3],
  260. a4 = a[4],
  261. a5 = a[5];
  262. var v0 = v[0],
  263. v1 = v[1];
  264. out[0] = a0 * v0;
  265. out[1] = a1 * v0;
  266. out[2] = a2 * v1;
  267. out[3] = a3 * v1;
  268. out[4] = a4;
  269. out[5] = a5;
  270. return out;
  271. }
  272. /**
  273. * Translates the mat2d by the dimensions in the given vec2
  274. *
  275. * @param {mat2d} out the receiving matrix
  276. * @param {ReadonlyMat2d} a the matrix to translate
  277. * @param {ReadonlyVec2} v the vec2 to translate the matrix by
  278. * @returns {mat2d} out
  279. **/
  280. function translate(out, a, v) {
  281. var a0 = a[0],
  282. a1 = a[1],
  283. a2 = a[2],
  284. a3 = a[3],
  285. a4 = a[4],
  286. a5 = a[5];
  287. var v0 = v[0],
  288. v1 = v[1];
  289. out[0] = a0;
  290. out[1] = a1;
  291. out[2] = a2;
  292. out[3] = a3;
  293. out[4] = a0 * v0 + a2 * v1 + a4;
  294. out[5] = a1 * v0 + a3 * v1 + a5;
  295. return out;
  296. }
  297. /**
  298. * Creates a matrix from a given angle
  299. * This is equivalent to (but much faster than):
  300. *
  301. * mat2d.identity(dest);
  302. * mat2d.rotate(dest, dest, rad);
  303. *
  304. * @param {mat2d} out mat2d receiving operation result
  305. * @param {Number} rad the angle to rotate the matrix by
  306. * @returns {mat2d} out
  307. */
  308. function fromRotation(out, rad) {
  309. var s = Math.sin(rad),
  310. c = Math.cos(rad);
  311. out[0] = c;
  312. out[1] = s;
  313. out[2] = -s;
  314. out[3] = c;
  315. out[4] = 0;
  316. out[5] = 0;
  317. return out;
  318. }
  319. /**
  320. * Creates a matrix from a vector scaling
  321. * This is equivalent to (but much faster than):
  322. *
  323. * mat2d.identity(dest);
  324. * mat2d.scale(dest, dest, vec);
  325. *
  326. * @param {mat2d} out mat2d receiving operation result
  327. * @param {ReadonlyVec2} v Scaling vector
  328. * @returns {mat2d} out
  329. */
  330. function fromScaling(out, v) {
  331. out[0] = v[0];
  332. out[1] = 0;
  333. out[2] = 0;
  334. out[3] = v[1];
  335. out[4] = 0;
  336. out[5] = 0;
  337. return out;
  338. }
  339. /**
  340. * Creates a matrix from a vector translation
  341. * This is equivalent to (but much faster than):
  342. *
  343. * mat2d.identity(dest);
  344. * mat2d.translate(dest, dest, vec);
  345. *
  346. * @param {mat2d} out mat2d receiving operation result
  347. * @param {ReadonlyVec2} v Translation vector
  348. * @returns {mat2d} out
  349. */
  350. function fromTranslation(out, v) {
  351. out[0] = 1;
  352. out[1] = 0;
  353. out[2] = 0;
  354. out[3] = 1;
  355. out[4] = v[0];
  356. out[5] = v[1];
  357. return out;
  358. }
  359. /**
  360. * Returns a string representation of a mat2d
  361. *
  362. * @param {ReadonlyMat2d} a matrix to represent as a string
  363. * @returns {String} string representation of the matrix
  364. */
  365. function str(a) {
  366. return "mat2d(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ")";
  367. }
  368. /**
  369. * Returns Frobenius norm of a mat2d
  370. *
  371. * @param {ReadonlyMat2d} a the matrix to calculate Frobenius norm of
  372. * @returns {Number} Frobenius norm
  373. */
  374. function frob(a) {
  375. return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], 1);
  376. }
  377. /**
  378. * Adds two mat2d's
  379. *
  380. * @param {mat2d} out the receiving matrix
  381. * @param {ReadonlyMat2d} a the first operand
  382. * @param {ReadonlyMat2d} b the second operand
  383. * @returns {mat2d} out
  384. */
  385. function add(out, a, b) {
  386. out[0] = a[0] + b[0];
  387. out[1] = a[1] + b[1];
  388. out[2] = a[2] + b[2];
  389. out[3] = a[3] + b[3];
  390. out[4] = a[4] + b[4];
  391. out[5] = a[5] + b[5];
  392. return out;
  393. }
  394. /**
  395. * Subtracts matrix b from matrix a
  396. *
  397. * @param {mat2d} out the receiving matrix
  398. * @param {ReadonlyMat2d} a the first operand
  399. * @param {ReadonlyMat2d} b the second operand
  400. * @returns {mat2d} out
  401. */
  402. function subtract(out, a, b) {
  403. out[0] = a[0] - b[0];
  404. out[1] = a[1] - b[1];
  405. out[2] = a[2] - b[2];
  406. out[3] = a[3] - b[3];
  407. out[4] = a[4] - b[4];
  408. out[5] = a[5] - b[5];
  409. return out;
  410. }
  411. /**
  412. * Multiply each element of the matrix by a scalar.
  413. *
  414. * @param {mat2d} out the receiving matrix
  415. * @param {ReadonlyMat2d} a the matrix to scale
  416. * @param {Number} b amount to scale the matrix's elements by
  417. * @returns {mat2d} out
  418. */
  419. function multiplyScalar(out, a, b) {
  420. out[0] = a[0] * b;
  421. out[1] = a[1] * b;
  422. out[2] = a[2] * b;
  423. out[3] = a[3] * b;
  424. out[4] = a[4] * b;
  425. out[5] = a[5] * b;
  426. return out;
  427. }
  428. /**
  429. * Adds two mat2d's after multiplying each element of the second operand by a scalar value.
  430. *
  431. * @param {mat2d} out the receiving vector
  432. * @param {ReadonlyMat2d} a the first operand
  433. * @param {ReadonlyMat2d} b the second operand
  434. * @param {Number} scale the amount to scale b's elements by before adding
  435. * @returns {mat2d} out
  436. */
  437. function multiplyScalarAndAdd(out, a, b, scale) {
  438. out[0] = a[0] + b[0] * scale;
  439. out[1] = a[1] + b[1] * scale;
  440. out[2] = a[2] + b[2] * scale;
  441. out[3] = a[3] + b[3] * scale;
  442. out[4] = a[4] + b[4] * scale;
  443. out[5] = a[5] + b[5] * scale;
  444. return out;
  445. }
  446. /**
  447. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  448. *
  449. * @param {ReadonlyMat2d} a The first matrix.
  450. * @param {ReadonlyMat2d} b The second matrix.
  451. * @returns {Boolean} True if the matrices are equal, false otherwise.
  452. */
  453. function exactEquals(a, b) {
  454. 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];
  455. }
  456. /**
  457. * Returns whether or not the matrices have approximately the same elements in the same position.
  458. *
  459. * @param {ReadonlyMat2d} a The first matrix.
  460. * @param {ReadonlyMat2d} b The second matrix.
  461. * @returns {Boolean} True if the matrices are equal, false otherwise.
  462. */
  463. function equals(a, b) {
  464. var a0 = a[0],
  465. a1 = a[1],
  466. a2 = a[2],
  467. a3 = a[3],
  468. a4 = a[4],
  469. a5 = a[5];
  470. var b0 = b[0],
  471. b1 = b[1],
  472. b2 = b[2],
  473. b3 = b[3],
  474. b4 = b[4],
  475. b5 = b[5];
  476. 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));
  477. }
  478. /**
  479. * Alias for {@link mat2d.multiply}
  480. * @function
  481. */
  482. var mul = multiply;
  483. /**
  484. * Alias for {@link mat2d.subtract}
  485. * @function
  486. */
  487. exports.mul = mul;
  488. var sub = subtract;
  489. exports.sub = sub;