all files / primitives/ matrix.js

69.23% Statements 126/182
46.03% Branches 29/63
100% Functions 16/16
68.89% Lines 124/180
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253               554× 554× 554× 554× 554× 554×           554×       554×     379× 379×           275× 275×                                 275× 275× 275× 275× 275× 275× 275× 275×     379×                       378× 378× 378× 378× 378× 378× 378×   379×     277× 277× 277×     277× 276× 276× 276×                                                                                                                                         278× 278× 278× 278× 278× 278×     277× 277× 277× 277× 277× 277× 277×      
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /**
     * Matrix module is used to transform points based on offsets, angle
     */
    /** @private */
    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 = {}));
    /** @private */
    var Matrix = /** @class */ (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;
            // if (type === undefined) {
            //     this.type = MatrixTypes.Unknown;
            // } else {
            //     this.type = type;
            // }
            this.type = type;
        }
        return Matrix;
    }());
    exports.Matrix = Matrix;
    /** @private */
    function identityMatrix() {
        return new Matrix(1, 0, 0, 1, 0, 0, MatrixTypes.Identity);
    }
    exports.identityMatrix = identityMatrix;
    /** @private */
    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;
    /** @private */
    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;
    /** @private */
    function rotateMatrix(matrix, angle, centerX, centerY) {
        angle %= 360.0;
        multiplyMatrix(matrix, createRotationRadians(angle * 0.017453292519943295, centerX ? centerX : 0, centerY ? centerY : 0));
    }
    exports.rotateMatrix = rotateMatrix;
    /** @private */
    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;
    /** @private */
    function translateMatrix(matrix, offsetX, offsetY) {
        Iif (matrix.type & MatrixTypes.Identity) {
            matrix.type = MatrixTypes.Translation;
            setMatrix(matrix, 1.0, 0.0, 0.0, 1.0, offsetX, offsetY);
            return;
        }
        Iif (matrix.type & MatrixTypes.Unknown) {
            matrix.offsetX += offsetX;
            matrix.offsetY += offsetY;
            return;
        }
        matrix.offsetX += offsetX;
        matrix.offsetY += offsetY;
        matrix.type |= MatrixTypes.Translation;
    }
    exports.translateMatrix = translateMatrix;
    /** @private */
    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;
    }
    /** @private */
    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;
    }
    /** @private */
    function multiplyPoint(matrix, x, y) {
        switch (matrix.type) {
            case MatrixTypes.Identity: break;
            case MatrixTypes.Translation:
                x += matrix.offsetX;
                y += matrix.offsetY;
                break;
            case MatrixTypes.Scaling:
                x *= matrix.m11;
                y *= matrix.m22;
                break;
            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 };
    }
    /** @private */
    function multiplyMatrix(matrix1, matrix2) {
        var type = matrix1.type;
        var type2 = matrix2.type;
        Iif (type2 === MatrixTypes.Identity) {
            return;
        }
        if (type === MatrixTypes.Identity) {
            assignMatrix(matrix1, matrix2);
            matrix1.type = matrix2.type;
            return;
        }
        Iif (type2 === MatrixTypes.Translation) {
            matrix1.offsetX += matrix2.offsetX;
            matrix1.offsetY += matrix2.offsetY;
            if (type !== MatrixTypes.Unknown) {
                matrix1.type |= MatrixTypes.Translation;
            }
            return;
        }
        Eif (type !== MatrixTypes.Translation) {
            var num = type << 4 | type2;
            switch (num) {
                case 34:
                    matrix1.m11 *= matrix2.m11;
                    matrix1.m22 *= matrix2.m22;
                    return;
                case 35:
                    matrix1.m11 *= matrix2.m11;
                    matrix1.m22 *= matrix2.m22;
                    matrix1.offsetX = matrix2.offsetX;
                    matrix1.offsetY = matrix2.offsetY;
                    matrix1.type = (MatrixTypes.Translation | MatrixTypes.Scaling);
                    return;
                case 36: break;
                default:
                    {
                        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;
                        }
                        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);
            Eif (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;
    /** @private */
    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;
    }
    /** @private */
    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;
    }
});