SMatrix.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SMatrix = void 0;
  4. var SMatrix = (function () {
  5. function SMatrix() {
  6. this.m11 = 1;
  7. this.m21 = 0;
  8. this.m31 = 0;
  9. this.m41 = 0;
  10. this.m12 = 0;
  11. this.m22 = 1;
  12. this.m32 = 0;
  13. this.m42 = 0;
  14. this.m13 = 0;
  15. this.m23 = 0;
  16. this.m33 = 1;
  17. this.m43 = 0;
  18. this.m14 = 0;
  19. this.m24 = 0;
  20. this.m34 = 0;
  21. this.m44 = 1;
  22. }
  23. Object.defineProperty(SMatrix.prototype, "a", {
  24. get: function () {
  25. return this.m11;
  26. },
  27. set: function (v) {
  28. this.m11 = v;
  29. },
  30. enumerable: false,
  31. configurable: true
  32. });
  33. Object.defineProperty(SMatrix.prototype, "b", {
  34. get: function () {
  35. return this.m12;
  36. },
  37. set: function (v) {
  38. this.m12 = v;
  39. },
  40. enumerable: false,
  41. configurable: true
  42. });
  43. Object.defineProperty(SMatrix.prototype, "c", {
  44. get: function () {
  45. return this.m21;
  46. },
  47. set: function (v) {
  48. this.m21 = v;
  49. },
  50. enumerable: false,
  51. configurable: true
  52. });
  53. Object.defineProperty(SMatrix.prototype, "d", {
  54. get: function () {
  55. return this.m22;
  56. },
  57. set: function (v) {
  58. this.m22 = v;
  59. },
  60. enumerable: false,
  61. configurable: true
  62. });
  63. Object.defineProperty(SMatrix.prototype, "e", {
  64. get: function () {
  65. return this.m41;
  66. },
  67. set: function (v) {
  68. this.m41 = v;
  69. },
  70. enumerable: false,
  71. configurable: true
  72. });
  73. Object.defineProperty(SMatrix.prototype, "f", {
  74. get: function () {
  75. return this.m42;
  76. },
  77. set: function (v) {
  78. this.m42 = v;
  79. },
  80. enumerable: false,
  81. configurable: true
  82. });
  83. Object.defineProperty(SMatrix.prototype, "is2D", {
  84. get: function () {
  85. return true;
  86. },
  87. enumerable: false,
  88. configurable: true
  89. });
  90. Object.defineProperty(SMatrix.prototype, "isIdentity", {
  91. get: function () {
  92. return (this.m11 == 1 &&
  93. this.m21 == 0 &&
  94. this.m31 == 0 &&
  95. this.m41 == 0 &&
  96. this.m12 == 0 &&
  97. this.m22 == 1 &&
  98. this.m32 == 0 &&
  99. this.m42 == 0 &&
  100. this.m13 == 0 &&
  101. this.m23 == 0 &&
  102. this.m33 == 1 &&
  103. this.m43 == 0 &&
  104. this.m14 == 0 &&
  105. this.m24 == 0 &&
  106. this.m34 == 0 &&
  107. this.m44 == 1);
  108. },
  109. enumerable: false,
  110. configurable: true
  111. });
  112. SMatrix.prototype.reset = function () {
  113. this.m11 = 1;
  114. this.m21 = 0;
  115. this.m31 = 0;
  116. this.m41 = 0;
  117. this.m12 = 0;
  118. this.m22 = 1;
  119. this.m32 = 0;
  120. this.m42 = 0;
  121. this.m13 = 0;
  122. this.m23 = 0;
  123. this.m33 = 1;
  124. this.m43 = 0;
  125. this.m14 = 0;
  126. this.m24 = 0;
  127. this.m34 = 0;
  128. this.m44 = 1;
  129. return this;
  130. };
  131. SMatrix.prototype.multiply = function (mat) {
  132. var _a;
  133. _a = [
  134. this.m11 * mat.m11 +
  135. this.m21 * mat.m12 +
  136. this.m31 * mat.m13 +
  137. this.m41 * mat.m14,
  138. this.m11 * mat.m21 +
  139. this.m21 * mat.m22 +
  140. this.m31 * mat.m23 +
  141. this.m41 * mat.m24,
  142. this.m11 * mat.m31 +
  143. this.m21 * mat.m32 +
  144. this.m31 * mat.m33 +
  145. this.m41 * mat.m34,
  146. this.m11 * mat.m41 +
  147. this.m21 * mat.m42 +
  148. this.m31 * mat.m43 +
  149. this.m41 * mat.m44,
  150. this.m12 * mat.m11 +
  151. this.m22 * mat.m12 +
  152. this.m32 * mat.m13 +
  153. this.m42 * mat.m14,
  154. this.m12 * mat.m21 +
  155. this.m22 * mat.m22 +
  156. this.m32 * mat.m23 +
  157. this.m42 * mat.m24,
  158. this.m12 * mat.m31 +
  159. this.m22 * mat.m32 +
  160. this.m32 * mat.m33 +
  161. this.m42 * mat.m34,
  162. this.m12 * mat.m41 +
  163. this.m22 * mat.m42 +
  164. this.m32 * mat.m43 +
  165. this.m42 * mat.m44,
  166. this.m13 * mat.m11 +
  167. this.m23 * mat.m12 +
  168. this.m33 * mat.m13 +
  169. this.m43 * mat.m14,
  170. this.m13 * mat.m21 +
  171. this.m23 * mat.m22 +
  172. this.m33 * mat.m23 +
  173. this.m43 * mat.m24,
  174. this.m13 * mat.m31 +
  175. this.m23 * mat.m32 +
  176. this.m33 * mat.m33 +
  177. this.m43 * mat.m34,
  178. this.m13 * mat.m41 +
  179. this.m23 * mat.m42 +
  180. this.m33 * mat.m43 +
  181. this.m43 * mat.m44,
  182. this.m14 * mat.m11 +
  183. this.m24 * mat.m12 +
  184. this.m34 * mat.m13 +
  185. this.m44 * mat.m14,
  186. this.m14 * mat.m21 +
  187. this.m24 * mat.m22 +
  188. this.m34 * mat.m23 +
  189. this.m44 * mat.m24,
  190. this.m14 * mat.m31 +
  191. this.m24 * mat.m32 +
  192. this.m34 * mat.m33 +
  193. this.m44 * mat.m34,
  194. this.m14 * mat.m41 +
  195. this.m24 * mat.m42 +
  196. this.m34 * mat.m43 +
  197. this.m44 * mat.m44
  198. ], this.m11 = _a[0], this.m21 = _a[1], this.m31 = _a[2], this.m41 = _a[3], this.m12 = _a[4], this.m22 = _a[5], this.m32 = _a[6], this.m42 = _a[7], this.m13 = _a[8], this.m23 = _a[9], this.m33 = _a[10], this.m43 = _a[11], this.m14 = _a[12], this.m24 = _a[13], this.m34 = _a[14], this.m44 = _a[15];
  199. return this;
  200. };
  201. SMatrix.prototype.translate = function (dx, dy, dz) {
  202. if (dz === void 0) { dz = 0; }
  203. var mat = new SMatrix();
  204. mat.m41 = dx;
  205. mat.m42 = dy;
  206. mat.m43 = dz;
  207. this.multiply(mat);
  208. return this;
  209. };
  210. SMatrix.prototype.scale = function (sx, sy) {
  211. var mat = new SMatrix();
  212. mat.m11 = sx;
  213. mat.m22 = sy;
  214. this.multiply(mat);
  215. return this;
  216. };
  217. SMatrix.prototype.rotate = function (rotX, rotY, rotZ) {
  218. var matZ = new SMatrix();
  219. if (rotY != undefined && rotZ != undefined) {
  220. var matX = new SMatrix();
  221. matX.m22 = Math.cos((rotX * Math.PI) / 180);
  222. matX.m32 = -Math.sin((rotX * Math.PI) / 180);
  223. matX.m23 = Math.sin((rotX * Math.PI) / 180);
  224. matX.m33 = Math.cos((rotX * Math.PI) / 180);
  225. this.multiply(matX);
  226. var matY = new SMatrix();
  227. matY.m11 = Math.cos((rotY * Math.PI) / 180);
  228. matY.m31 = Math.sin((rotY * Math.PI) / 180);
  229. matY.m13 = -Math.sin((rotY * Math.PI) / 180);
  230. matY.m33 = Math.cos((rotY * Math.PI) / 180);
  231. this.multiply(matY);
  232. matZ.m11 = Math.cos((rotZ * Math.PI) / 180);
  233. matZ.m21 = -Math.sin((rotZ * Math.PI) / 180);
  234. matZ.m12 = Math.sin((rotZ * Math.PI) / 180);
  235. matZ.m22 = Math.cos((rotZ * Math.PI) / 180);
  236. this.multiply(matZ);
  237. return this;
  238. }
  239. else {
  240. matZ.m11 = Math.cos((rotX * Math.PI) / 180);
  241. matZ.m21 = -Math.sin((rotX * Math.PI) / 180);
  242. matZ.m12 = Math.sin((rotX * Math.PI) / 180);
  243. matZ.m22 = Math.cos((rotX * Math.PI) / 180);
  244. this.multiply(matZ);
  245. return this;
  246. }
  247. };
  248. SMatrix.prototype.transpose = function () {
  249. var _a, _b, _c, _d, _e, _f;
  250. _a = [this.m21, this.m12], this.m12 = _a[0], this.m21 = _a[1];
  251. _b = [this.m31, this.m13], this.m13 = _b[0], this.m31 = _b[1];
  252. _c = [this.m41, this.m14], this.m14 = _c[0], this.m41 = _c[1];
  253. _d = [this.m32, this.m23], this.m23 = _d[0], this.m32 = _d[1];
  254. _e = [this.m42, this.m24], this.m24 = _e[0], this.m42 = _e[1];
  255. _f = [this.m43, this.m34], this.m34 = _f[0], this.m43 = _f[1];
  256. return this;
  257. };
  258. SMatrix.prototype.inversed = function () {
  259. var detMat = this.det();
  260. var d = this.value();
  261. var ret = new SMatrix();
  262. ret.m11 = detMat.m11 / d;
  263. ret.m21 = detMat.m21 / d;
  264. ret.m31 = detMat.m31 / d;
  265. ret.m41 = detMat.m41 / d;
  266. ret.m12 = detMat.m12 / d;
  267. ret.m22 = detMat.m22 / d;
  268. ret.m32 = detMat.m32 / d;
  269. ret.m42 = detMat.m42 / d;
  270. ret.m13 = detMat.m13 / d;
  271. ret.m23 = detMat.m23 / d;
  272. ret.m33 = detMat.m33 / d;
  273. ret.m43 = detMat.m43 / d;
  274. ret.m14 = detMat.m14 / d;
  275. ret.m24 = detMat.m24 / d;
  276. ret.m34 = detMat.m34 / d;
  277. ret.m44 = detMat.m44 / d;
  278. return ret;
  279. };
  280. SMatrix.prototype.det = function () {
  281. var m = new SMatrix();
  282. m.m11 =
  283. this.m22 * this.m33 * this.m44 +
  284. this.m32 * this.m43 * this.m24 +
  285. this.m42 * this.m23 * this.m34 -
  286. this.m42 * this.m33 * this.m24 -
  287. this.m32 * this.m23 * this.m44 -
  288. this.m22 * this.m43 * this.m34;
  289. m.m12 = -(this.m12 * this.m33 * this.m44 +
  290. this.m32 * this.m43 * this.m14 +
  291. this.m42 * this.m13 * this.m34 -
  292. this.m42 * this.m33 * this.m14 -
  293. this.m32 * this.m13 * this.m44 -
  294. this.m12 * this.m43 * this.m34);
  295. m.m13 =
  296. this.m12 * this.m23 * this.m44 +
  297. this.m22 * this.m43 * this.m14 +
  298. this.m42 * this.m13 * this.m24 -
  299. this.m42 * this.m23 * this.m14 -
  300. this.m22 * this.m13 * this.m44 -
  301. this.m12 * this.m43 * this.m24;
  302. m.m14 = -(this.m12 * this.m23 * this.m34 +
  303. this.m22 * this.m33 * this.m14 +
  304. this.m32 * this.m13 * this.m24 -
  305. this.m32 * this.m23 * this.m14 -
  306. this.m22 * this.m13 * this.m34 -
  307. this.m12 * this.m33 * this.m24);
  308. m.m21 = -(this.m21 * this.m33 * this.m44 +
  309. this.m31 * this.m43 * this.m24 +
  310. this.m41 * this.m23 * this.m34 -
  311. this.m41 * this.m33 * this.m24 -
  312. this.m31 * this.m23 * this.m44 -
  313. this.m21 * this.m43 * this.m34);
  314. m.m22 =
  315. this.m11 * this.m33 * this.m44 +
  316. this.m31 * this.m43 * this.m14 +
  317. this.m41 * this.m13 * this.m34 -
  318. this.m41 * this.m33 * this.m14 -
  319. this.m31 * this.m13 * this.m44 -
  320. this.m11 * this.m43 * this.m34;
  321. m.m23 = -(this.m11 * this.m23 * this.m44 +
  322. this.m21 * this.m43 * this.m14 +
  323. this.m41 * this.m13 * this.m24 -
  324. this.m41 * this.m23 * this.m14 -
  325. this.m21 * this.m13 * this.m44 -
  326. this.m11 * this.m43 * this.m24);
  327. m.m24 =
  328. this.m11 * this.m23 * this.m34 +
  329. this.m21 * this.m33 * this.m14 +
  330. this.m31 * this.m13 * this.m24 -
  331. this.m31 * this.m23 * this.m14 -
  332. this.m21 * this.m13 * this.m34 -
  333. this.m11 * this.m33 * this.m24;
  334. m.m31 =
  335. this.m21 * this.m32 * this.m44 +
  336. this.m31 * this.m42 * this.m24 +
  337. this.m41 * this.m22 * this.m34 -
  338. this.m41 * this.m32 * this.m24 -
  339. this.m31 * this.m22 * this.m44 -
  340. this.m21 * this.m42 * this.m34;
  341. m.m32 = -(this.m11 * this.m32 * this.m44 +
  342. this.m31 * this.m42 * this.m14 +
  343. this.m41 * this.m12 * this.m34 -
  344. this.m41 * this.m32 * this.m14 -
  345. this.m31 * this.m12 * this.m44 -
  346. this.m11 * this.m42 * this.m34);
  347. m.m33 =
  348. this.m11 * this.m22 * this.m44 +
  349. this.m21 * this.m42 * this.m14 +
  350. this.m41 * this.m12 * this.m24 -
  351. this.m41 * this.m22 * this.m14 -
  352. this.m21 * this.m12 * this.m44 -
  353. this.m11 * this.m42 * this.m24;
  354. m.m34 = -(this.m11 * this.m22 * this.m34 +
  355. this.m21 * this.m32 * this.m14 +
  356. this.m31 * this.m12 * this.m24 -
  357. this.m31 * this.m22 * this.m14 -
  358. this.m21 * this.m12 * this.m34 -
  359. this.m11 * this.m32 * this.m24);
  360. m.m41 = -(this.m21 * this.m32 * this.m43 +
  361. this.m31 * this.m42 * this.m23 +
  362. this.m41 * this.m22 * this.m33 -
  363. this.m41 * this.m32 * this.m23 -
  364. this.m31 * this.m22 * this.m43 -
  365. this.m21 * this.m42 * this.m33);
  366. m.m42 =
  367. this.m11 * this.m32 * this.m43 +
  368. this.m31 * this.m42 * this.m13 +
  369. this.m41 * this.m12 * this.m33 -
  370. this.m41 * this.m32 * this.m13 -
  371. this.m31 * this.m12 * this.m43 -
  372. this.m11 * this.m42 * this.m33;
  373. m.m43 = -(this.m11 * this.m22 * this.m43 +
  374. this.m21 * this.m42 * this.m13 +
  375. this.m41 * this.m12 * this.m23 -
  376. this.m41 * this.m22 * this.m13 -
  377. this.m21 * this.m12 * this.m43 -
  378. this.m11 * this.m42 * this.m23);
  379. m.m44 =
  380. this.m11 * this.m22 * this.m33 +
  381. this.m21 * this.m32 * this.m13 +
  382. this.m31 * this.m12 * this.m23 -
  383. this.m31 * this.m22 * this.m13 -
  384. this.m21 * this.m12 * this.m33 -
  385. this.m11 * this.m32 * this.m23;
  386. return m;
  387. };
  388. SMatrix.prototype.value = function () {
  389. return (this.m11 *
  390. (this.m22 * this.m33 * this.m44 +
  391. this.m32 * this.m43 * this.m24 +
  392. this.m42 * this.m23 * this.m34 -
  393. this.m42 * this.m33 * this.m24 -
  394. this.m32 * this.m23 * this.m44 -
  395. this.m22 * this.m43 * this.m34) -
  396. this.m21 *
  397. (this.m12 * this.m33 * this.m44 +
  398. this.m32 * this.m43 * this.m14 +
  399. this.m42 * this.m13 * this.m34 -
  400. this.m42 * this.m33 * this.m14 -
  401. this.m32 * this.m13 * this.m44 -
  402. this.m12 * this.m43 * this.m34) +
  403. this.m31 *
  404. (this.m12 * this.m23 * this.m44 +
  405. this.m22 * this.m43 * this.m14 +
  406. this.m42 * this.m13 * this.m24 -
  407. this.m42 * this.m23 * this.m14 -
  408. this.m22 * this.m13 * this.m44 -
  409. this.m12 * this.m43 * this.m24) -
  410. this.m41 *
  411. (this.m12 * this.m23 * this.m34 +
  412. this.m22 * this.m33 * this.m14 +
  413. this.m32 * this.m13 * this.m24 -
  414. this.m32 * this.m23 * this.m14 -
  415. this.m22 * this.m13 * this.m34 -
  416. this.m12 * this.m33 * this.m24));
  417. };
  418. return SMatrix;
  419. }());
  420. exports.SMatrix = SMatrix;