mat2d.js 12 KB

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