UNPKG

@turbox3d/math

Version:

Large-scale graphics application math library

413 lines (412 loc) 11.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.Matrix3 = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _MathUtils = require("../MathUtils"); var _Vector = require("./Vector2"); /* eslint-disable function-call-argument-newline */ var Matrix3 = exports.Matrix3 = /*#__PURE__*/function () { function Matrix3() { (0, _classCallCheck2["default"])(this, Matrix3); this.isMatrix3 = true; this.elements = [1, 0, 0, 0, 1, 0, 0, 0, 1]; if (arguments.length > 0) { console.error('THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.'); } } return (0, _createClass2["default"])(Matrix3, [{ key: "set", value: function set(n11, n12, n13, n21, n22, n23, n31, n32, n33) { var te = this.elements; te[0] = n11; te[1] = n21; te[2] = n31; te[3] = n12; te[4] = n22; te[5] = n32; te[6] = n13; te[7] = n23; te[8] = n33; return this; } }, { key: "identity", value: function identity() { this.set(1, 0, 0, 0, 1, 0, 0, 0, 1); return this; } }, { key: "clone", value: function clone() { return new Matrix3().fromArray(this.elements); } }, { key: "copy", value: function copy(m) { var te = this.elements; var me = m.elements; te[0] = me[0]; te[1] = me[1]; te[2] = me[2]; te[3] = me[3]; te[4] = me[4]; te[5] = me[5]; te[6] = me[6]; te[7] = me[7]; te[8] = me[8]; return this; } }, { key: "extractBasis", value: function extractBasis(xAxis, yAxis, zAxis) { xAxis.setFromMatrix3Column(this, 0); yAxis.setFromMatrix3Column(this, 1); zAxis.setFromMatrix3Column(this, 2); return this; } }, { key: "setFromMatrix4", value: function setFromMatrix4(m) { var me = m.elements; this.set(me[0], me[4], me[8], me[1], me[5], me[9], me[2], me[6], me[10]); return this; } }, { key: "add", value: function add(b) { var me = this.elements; var be = b.elements; this.set(me[0] + be[0], me[3] + be[3], me[6] + be[6], me[1] + be[1], me[4] + be[4], me[7] + be[7], me[2] + be[2], me[5] + be[5], me[8] + be[8]); return this; } }, { key: "added", value: function added(b) { var me = this.elements; var be = b.elements; var res = new Matrix3(); res.set(me[0] + be[0], me[3] + be[3], me[6] + be[6], me[1] + be[1], me[4] + be[4], me[7] + be[7], me[2] + be[2], me[5] + be[5], me[8] + be[8]); return res; } }, { key: "multiply", value: function multiply(m) { return this.multiplyMatrices(this, m); } }, { key: "multiplied", value: function multiplied(m) { return new Matrix3().multiplyMatrices(this, m); } }, { key: "premultiply", value: function premultiply(m) { return this.multiplyMatrices(m, this); } }, { key: "premultiplied", value: function premultiplied(m) { return new Matrix3().multiplyMatrices(m, this); } }, { key: "multiplyMatrices", value: function multiplyMatrices(a, b) { var ae = a.elements; var be = b.elements; var te = this.elements; var a11 = ae[0]; var a12 = ae[3]; var a13 = ae[6]; var a21 = ae[1]; var a22 = ae[4]; var a23 = ae[7]; var a31 = ae[2]; var a32 = ae[5]; var a33 = ae[8]; var b11 = be[0]; var b12 = be[3]; var b13 = be[6]; var b21 = be[1]; var b22 = be[4]; var b23 = be[7]; var b31 = be[2]; var b32 = be[5]; var b33 = be[8]; te[0] = a11 * b11 + a12 * b21 + a13 * b31; te[3] = a11 * b12 + a12 * b22 + a13 * b32; te[6] = a11 * b13 + a12 * b23 + a13 * b33; te[1] = a21 * b11 + a22 * b21 + a23 * b31; te[4] = a21 * b12 + a22 * b22 + a23 * b32; te[7] = a21 * b13 + a22 * b23 + a23 * b33; te[2] = a31 * b11 + a32 * b21 + a33 * b31; te[5] = a31 * b12 + a32 * b22 + a33 * b32; te[8] = a31 * b13 + a32 * b23 + a33 * b33; return this; } }, { key: "multiplyScalar", value: function multiplyScalar(s) { var te = this.elements; te[0] *= s; te[3] *= s; te[6] *= s; te[1] *= s; te[4] *= s; te[7] *= s; te[2] *= s; te[5] *= s; te[8] *= s; return this; } }, { key: "determinant", value: function determinant() { var te = this.elements; var a = te[0]; var b = te[1]; var c = te[2]; var d = te[3]; var e = te[4]; var f = te[5]; var g = te[6]; var h = te[7]; var i = te[8]; return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g; } }, { key: "invert", value: function invert() { var te = this.elements; var n11 = te[0]; var n21 = te[1]; var n31 = te[2]; var n12 = te[3]; var n22 = te[4]; var n32 = te[5]; var n13 = te[6]; var n23 = te[7]; var n33 = te[8]; var t11 = n33 * n22 - n32 * n23; var t12 = n32 * n13 - n33 * n12; var t13 = n23 * n12 - n22 * n13; var det = n11 * t11 + n21 * t12 + n31 * t13; if (det === 0) return this.set(0, 0, 0, 0, 0, 0, 0, 0, 0); var detInv = 1 / det; te[0] = t11 * detInv; te[1] = (n31 * n23 - n33 * n21) * detInv; te[2] = (n32 * n21 - n31 * n22) * detInv; te[3] = t12 * detInv; te[4] = (n33 * n11 - n31 * n13) * detInv; te[5] = (n31 * n12 - n32 * n11) * detInv; te[6] = t13 * detInv; te[7] = (n21 * n13 - n23 * n11) * detInv; te[8] = (n22 * n11 - n21 * n12) * detInv; return this; } }, { key: "inverted", value: function inverted() { var result = new Matrix3(); result.copy(this).invert(); return result; } }, { key: "transpose", value: function transpose() { var tmp; var m = this.elements; tmp = m[1]; m[1] = m[3]; m[3] = tmp; tmp = m[2]; m[2] = m[6]; m[6] = tmp; tmp = m[5]; m[5] = m[7]; m[7] = tmp; return this; } }, { key: "getNormalMatrix", value: function getNormalMatrix(matrix4) { return this.setFromMatrix4(matrix4).copy(this).invert().transpose(); } }, { key: "transposeIntoArray", value: function transposeIntoArray(r) { var m = this.elements; r[0] = m[0]; r[1] = m[3]; r[2] = m[6]; r[3] = m[1]; r[4] = m[4]; r[5] = m[7]; r[6] = m[2]; r[7] = m[5]; r[8] = m[8]; return this; } }, { key: "setUvTransform", value: function setUvTransform(tx, ty, sx, sy, rotation, cx, cy) { var c = Math.cos(rotation); var s = Math.sin(rotation); this.set(sx * c, sx * s, -sx * (c * cx + s * cy) + cx + tx, -sy * s, sy * c, -sy * (-s * cx + c * cy) + cy + ty, 0, 0, 1); return this; } }, { key: "scale", value: function scale(sx, sy) { var te = this.elements; te[0] *= sx; te[3] *= sx; te[6] *= sx; te[1] *= sy; te[4] *= sy; te[7] *= sy; return this; } }, { key: "rotate", value: function rotate(theta) { var c = Math.cos(theta); var s = Math.sin(theta); var te = this.elements; var a11 = te[0]; var a12 = te[3]; var a13 = te[6]; var a21 = te[1]; var a22 = te[4]; var a23 = te[7]; te[0] = c * a11 + s * a21; te[3] = c * a12 + s * a22; te[6] = c * a13 + s * a23; te[1] = -s * a11 + c * a21; te[4] = -s * a12 + c * a22; te[7] = -s * a13 + c * a23; return this; } }, { key: "translate", value: function translate(tx, ty) { var te = this.elements; te[0] += tx * te[2]; te[3] += tx * te[5]; te[6] += tx * te[8]; te[1] += ty * te[2]; te[4] += ty * te[5]; te[7] += ty * te[8]; return this; } }, { key: "equals", value: function equals(matrix) { var te = this.elements; var me = matrix.elements; for (var i = 0; i < 9; i++) { if (!_MathUtils.MathUtils.isEqual(te[i], me[i])) { return false; } } return true; } }, { key: "fromArray", value: function fromArray(array) { var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; for (var i = 0; i < 9; i++) { this.elements[i] = array[i + offset]; } return this; } }, { key: "toArray", value: function toArray() { var array = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var te = this.elements; array[offset] = te[0]; array[offset + 1] = te[1]; array[offset + 2] = te[2]; array[offset + 3] = te[3]; array[offset + 4] = te[4]; array[offset + 5] = te[5]; array[offset + 6] = te[6]; array[offset + 7] = te[7]; array[offset + 8] = te[8]; return array; } /** 缩放,注意和 scale 的区别,这个不会影响 position */ }, { key: "doScale", value: function doScale(sx, sy) { var te = this.elements; te[0] *= sx; te[1] *= sx; te[2] *= sx; te[3] *= sy; te[4] *= sy; te[5] *= sy; return this; } /** * 设置成缩放矩阵 */ }, { key: "setScale", value: function setScale(scale) { var x = scale.x; var y = scale.y; return this.set(x, 0, 0, 0, y, 0, 0, 0, 1); } /** * 设置成旋转矩阵 */ }, { key: "setRotation", value: function setRotation(angle) { var c = Math.cos(angle); var s = Math.sin(angle); return this.set(c, -s, 0, s, c, 0, 0, 0, 1); } /** * 设置成平移矩阵 */ }, { key: "setTranslation", value: function setTranslation(translation) { var x = translation.x; var y = translation.y; return this.set(1, 0, x, 0, 1, y, 0, 0, 1); } /** * 把位置信息放到没用的两个矩阵索引 */ }, { key: "setPosition", value: function setPosition(position) { var me = this.elements; me[6] = position.x; me[7] = position.y; } }, { key: "getPosition", value: function getPosition() { return new _Vector.Vector2(this.elements[6], this.elements[7]); } }, { key: "compose", value: function compose(position, rotation, scale) { this.setRotation(rotation); if (scale) { this.doScale(scale.x, scale.y); } this.setPosition(position); return this; } }]); }();