all files / diagram/primitives/ matrix.js

90.48% Statements 133/147
89.13% Branches 41/46
100% Functions 16/16
90.34% Lines 131/145
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183     569822× 569822× 569822× 569822× 569822× 569822× 569822×     569822×   1501668× 1501668×   111× 111× 308× 308×   111×   283883× 283883×   1006× 1006× 1006×   543× 543× 543×   1006× 1006× 1006× 1006×   283883× 283883× 283883× 283883× 283883× 283883× 283883× 283883×   1501668×   1110× 1110× 1110× 1110× 1110×   1500558× 1500558× 1500558× 1500558× 1500558× 1500558× 1500558×   1501668×   285159× 285159× 285159× 283963× 283963× 283963×   1196× 1196× 1196×               270× 270× 270× 270× 270× 361×   565×     565×     565×   926× 926× 926× 926× 926× 926× 926× 926× 926× 202×     724× 720×   724× 638×   724×   724×   926× 926× 926×                       285815× 285815× 285815× 285815× 285815× 285815×   284889× 284889× 284889× 284889× 284889× 284889× 284889×      
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MatrixTypes;
    (function (MatrixTypes) {
        MatrixTypes[MatrixTypes["Identity"] = 0] = "Identity";
        MatrixTypes[MatrixTypes["Translation"] = 1] = "Translation";
        MatrixTypes[MatrixTypes["Scaling"] = 2] = "Scaling";
        MatrixTypes[MatrixTypes["Unknown"] = 4] = "Unknown";
    })(MatrixTypes = exports.MatrixTypes || (exports.MatrixTypes = {}));
    var Matrix = (function () {
        function Matrix(m11, m12, m21, m22, offsetX, offsetY, type) {
            this.m11 = m11;
            this.m12 = m12;
            this.m21 = m21;
            this.m22 = m22;
            this.offsetX = offsetX;
            this.offsetY = offsetY;
            this.type = type;
        }
        return Matrix;
    }());
    exports.Matrix = Matrix;
    function identityMatrix() {
        return new Matrix(1, 0, 0, 1, 0, 0, MatrixTypes.Identity);
    }
    exports.identityMatrix = identityMatrix;
    function transformPointByMatrix(matrix, point) {
        var pt = multiplyPoint(matrix, point.x, point.y);
        return { x: Math.round(pt.x * 100) / 100, y: Math.round(pt.y * 100) / 100 };
    }
    exports.transformPointByMatrix = transformPointByMatrix;
    function transformPointsByMatrix(matrix, points) {
        var transformedPoints = [];
        for (var _i = 0, points_1 = points; _i < points_1.length; _i++) {
            var point = points_1[_i];
            transformedPoints.push(transformPointByMatrix(matrix, point));
        }
        return transformedPoints;
    }
    exports.transformPointsByMatrix = transformPointsByMatrix;
    function rotateMatrix(matrix, angle, centerX, centerY) {
        angle %= 360.0;
        multiplyMatrix(matrix, createRotationRadians(angle * 0.017453292519943295, centerX ? centerX : 0, centerY ? centerY : 0));
    }
    exports.rotateMatrix = rotateMatrix;
    function scaleMatrix(matrix, scaleX, scaleY, centerX, centerY) {
        if (centerX === void 0) { centerX = 0; }
        if (centerY === void 0) { centerY = 0; }
        multiplyMatrix(matrix, createScaling(scaleX, scaleY, centerX, centerY));
    }
    exports.scaleMatrix = scaleMatrix;
    function translateMatrix(matrix, offsetX, offsetY) {
        matrix.offsetX += offsetX;
        matrix.offsetY += offsetY;
        matrix.type |= MatrixTypes.Translation;
    }
    exports.translateMatrix = translateMatrix;
    function createScaling(scaleX, scaleY, centerX, centerY) {
        var result = identityMatrix();
        result.type = !(centerX || centerY) ? MatrixTypes.Scaling : MatrixTypes.Scaling | MatrixTypes.Translation;
        setMatrix(result, scaleX, 0.0, 0.0, scaleY, centerX - scaleX * centerX, centerY - scaleY * centerY);
        return result;
    }
    function createRotationRadians(angle, centerX, centerY) {
        var result = identityMatrix();
        var num = Math.sin(angle);
        var num2 = Math.cos(angle);
        var offsetX = centerX * (1.0 - num2) + centerY * num;
        var offsetY = centerY * (1.0 - num2) - centerX * num;
        result.type = MatrixTypes.Unknown;
        setMatrix(result, num2, num, -num, num2, offsetX, offsetY);
        return result;
    }
    function multiplyPoint(matrix, x, y) {
        switch (matrix.type) {
            case MatrixTypes.Translation | MatrixTypes.Scaling:
                x *= matrix.m11;
                x += matrix.offsetX;
                y *= matrix.m22;
                y += matrix.offsetY;
                break;
            default:
                var num = y * matrix.m21 + matrix.offsetX;
                var num2 = x * matrix.m12 + matrix.offsetY;
                x *= matrix.m11;
                x += num;
                y *= matrix.m22;
                y += num2;
                break;
        }
        return { x: x, y: y };
    }
    function multiplyMatrix(matrix1, matrix2) {
        var type = matrix1.type;
        var type2 = matrix2.type;
        if (type === MatrixTypes.Identity) {
            assignMatrix(matrix1, matrix2);
            matrix1.type = matrix2.type;
            return;
        }
        Eif (type !== MatrixTypes.Translation) {
            var num = type << 4 | type2;
            switch (num) {
                case 50:
                    matrix1.m11 *= matrix2.m11;
                    matrix1.m22 *= matrix2.m22;
                    matrix1.offsetX *= matrix2.m11;
                    matrix1.offsetY *= matrix2.m22;
                    return;
                case 51:
                    matrix1.m11 *= matrix2.m11;
                    matrix1.m22 *= matrix2.m22;
                    matrix1.offsetX = matrix2.m11 * matrix1.offsetX + matrix2.offsetX;
                    matrix1.offsetY = matrix2.m22 * matrix1.offsetY + matrix2.offsetY;
                    return;
                case 52: break;
                default:
                    switch (num) {
                        case 66:
                        case 67:
                        case 68: break;
                        default: return;
                    }
                    break;
            }
            var result = identityMatrix();
            var m11New = matrix1.m11 * matrix2.m11 + matrix1.m12 * matrix2.m21;
            var m12New = matrix1.m11 * matrix2.m12 + matrix1.m12 * matrix2.m22;
            var m21New = matrix1.m21 * matrix2.m11 + matrix1.m22 * matrix2.m21;
            var m22New = matrix1.m21 * matrix2.m12 + matrix1.m22 * matrix2.m22;
            var offsetX_1 = matrix1.offsetX * matrix2.m11 + matrix1.offsetY * matrix2.m21 + matrix2.offsetX;
            var offsetY_1 = matrix1.offsetX * matrix2.m12 + matrix1.offsetY * matrix2.m22 + matrix2.offsetY;
            setMatrix(result, m11New, m12New, m21New, m22New, offsetX_1, offsetY_1);
            if (result.m21 || result.m12) {
                result.type = MatrixTypes.Unknown;
            }
            else {
                if (result.m11 && result.m11 !== 1.0 || result.m22 && result.m22 !== 1.0) {
                    result.type = MatrixTypes.Scaling;
                }
                if (result.offsetX || result.offsetY) {
                    result.type |= MatrixTypes.Translation;
                }
                if ((result.type & (MatrixTypes.Translation | MatrixTypes.Scaling)) === MatrixTypes.Identity) {
                    result.type = MatrixTypes.Identity;
                }
                result.type = MatrixTypes.Scaling | MatrixTypes.Translation;
            }
            assignMatrix(matrix1, result);
            matrix1.type = result.type;
            return;
        }
        var offsetX = matrix1.offsetX;
        var offsetY = matrix1.offsetY;
        matrix1.offsetX = offsetX * matrix2.m11 + offsetY * matrix2.m21 + matrix2.offsetX;
        matrix1.offsetY = offsetX * matrix2.m12 + offsetY * matrix2.m22 + matrix2.offsetY;
        if (type2 === MatrixTypes.Unknown) {
            matrix1.type = MatrixTypes.Unknown;
            return;
        }
        matrix1.type = (MatrixTypes.Translation | MatrixTypes.Scaling);
    }
    exports.multiplyMatrix = multiplyMatrix;
    function setMatrix(mat, m11, m12, m21, m22, x, y) {
        mat.m11 = m11;
        mat.m12 = m12;
        mat.m21 = m21;
        mat.m22 = m22;
        mat.offsetX = x;
        mat.offsetY = y;
    }
    function assignMatrix(matrix1, matrix2) {
        matrix1.m11 = matrix2.m11;
        matrix1.m12 = matrix2.m12;
        matrix1.m21 = matrix2.m21;
        matrix1.m22 = matrix2.m22;
        matrix1.offsetX = matrix2.offsetX;
        matrix1.offsetY = matrix2.offsetY;
        matrix1.type = matrix2.type;
    }
});