All files / dist/Math MathLibs.js

0% Statements 0/229
0% Branches 0/75
0% Functions 0/27
0% Lines 0/212

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
"use strict";
// Everything but the relevant parts stripped out by Janne Aukia
// for Zoomooz on April 4 2012 by using jscoverage coverage analysis tool.
Object.defineProperty(exports, "__esModule", { value: true });
function Matrix() { }
exports.Matrix = Matrix;
Matrix.prototype = {
    // Returns a copy of the matrix
    dup: function () {
        return Matrix.create(this.elements);
    },
    // Maps the matrix to another matrix (of the same dimensions) according to the given function
    /*map: function(fn) {
      let els = [], ni = this.elements.length, ki = ni, i, nj, kj = this.elements[0].length, j;
      do { i = ki - ni;
        nj = kj;
        els[i] = [];
        do { j = kj - nj;
          els[i][j] = fn(this.elements[i][j], i + 1, j + 1);
        } while (--nj);
      } while (--ni);
      return Matrix.create(els);
    },*/
    // Returns true iff the matrix can multiply the argument from the left
    canMultiplyFromLeft: function (matrix) {
        var M = matrix.elements || matrix;
        if (typeof (M[0][0]) == 'undefined') {
            M = Matrix.create(M).elements;
        }
        // this.columns should equal matrix.rows
        return (this.elements[0].length == M.length);
    },
    // Returns the result of multiplying the matrix from the right by the argument.
    // If the argument is a scalar then just multiply all the elements. If the argument is
    // a vector, a vector is returned, which saves you having to remember calling
    // col(1) on the result.
    multiply: function (matrix) {
        /*if (!matrix.elements) {
          return this.map(function(x) { return x * matrix; });
        }*/
        var returnVector = matrix.modulus ? true : false;
        var M = matrix.elements || matrix;
        if (typeof (M[0][0]) == 'undefined') {
            M = Matrix.create(M).elements;
        }
        if (!this.canMultiplyFromLeft(M)) {
            return null;
        }
        var ni = this.elements.length, ki = ni, i, nj, kj = M[0].length, j;
        var cols = this.elements[0].length, elements = [], sum, nc, c;
        do {
            i = ki - ni;
            elements[i] = [];
            nj = kj;
            do {
                j = kj - nj;
                sum = 0;
                nc = cols;
                do {
                    c = cols - nc;
                    sum += this.elements[i][c] * M[c][j];
                } while (--nc);
                elements[i][j] = sum;
            } while (--nj);
        } while (--ni);
        M = Matrix.create(elements);
        return returnVector ? M.col(1) : M;
    },
    // Returns true iff the matrix is square
    isSquare: function () {
        return (this.elements.length == this.elements[0].length);
    },
    // Make the matrix upper (right) triangular by Gaussian elimination.
    // This method only adds multiples of rows to other rows. No rows are
    // scaled up or switched, and the determinant is preserved.
    toRightTriangular: function () {
        var M = this.dup(), els;
        var n = this.elements.length, k = n, i, np, kp = this.elements[0].length, p;
        do {
            i = k - n;
            if (M.elements[i][i] === 0) {
                for (var j = i + 1; j < k; j++) {
                    if (M.elements[j][i] !== 0) {
                        els = [];
                        np = kp;
                        do {
                            p = kp - np;
                            els.push(M.elements[i][p] + M.elements[j][p]);
                        } while (--np);
                        M.elements[i] = els;
                        break;
                    }
                }
            }
            if (M.elements[i][i] !== 0) {
                for (var j = i + 1; j < k; j++) {
                    var multiplier = M.elements[j][i] / M.elements[i][i];
                    els = [];
                    np = kp;
                    do {
                        p = kp - np;
                        // Elements with column numbers up to an including the number
                        // of the row that we're subtracting can safely be set straight to
                        // zero, since that's the point of this routine and it avoids having
                        // to loop over and correct rounding errors later
                        els.push(p <= i ? 0 : M.elements[j][p] - M.elements[i][p] * multiplier);
                    } while (--np);
                    M.elements[j] = els;
                }
            }
        } while (--n);
        return M;
    },
    // Returns the determinant for square matrices
    determinant: function () {
        if (!this.isSquare()) {
            return null;
        }
        var M = this.toRightTriangular();
        var det = M.elements[0][0], n = M.elements.length - 1, k = n, i;
        do {
            i = k - n + 1;
            det = det * M.elements[i][i];
        } while (--n);
        return det;
    },
    // Returns true iff the matrix is singular
    isSingular: function () {
        return (this.isSquare() && this.determinant() === 0);
    },
    // Returns the result of attaching the given argument to the right-hand side of the matrix
    augment: function (matrix) {
        var M = matrix.elements || matrix;
        if (typeof (M[0][0]) == 'undefined') {
            M = Matrix.create(M).elements;
        }
        var T = this.dup(), cols = T.elements[0].length;
        var ni = T.elements.length, ki = ni, i, nj, kj = M[0].length, j;
        if (ni != M.length) {
            return null;
        }
        do {
            i = ki - ni;
            nj = kj;
            do {
                j = kj - nj;
                T.elements[i][cols + j] = M[i][j];
            } while (--nj);
        } while (--ni);
        return T;
    },
    // Returns the inverse (if one exists) using Gauss-Jordan
    inverse: function () {
        if (!this.isSquare() || this.isSingular()) {
            return null;
        }
        var ni = this.elements.length, ki = ni, i, j;
        var M = this.augment(Matrix.I(ni)).toRightTriangular();
        var np, kp = M.elements[0].length, p, els, divisor;
        var inverse_elements = [], new_element;
        // Matrix is non-singular so there will be no zeros on the diagonal
        // Cycle through rows from last to first
        do {
            i = ni - 1;
            // First, normalise diagonal elements to 1
            els = [];
            np = kp;
            inverse_elements[i] = [];
            divisor = M.elements[i][i];
            do {
                p = kp - np;
                new_element = M.elements[i][p] / divisor;
                els.push(new_element);
                // Shuffle of the current row of the right hand side into the results
                // array as it will not be modified by later runs through this loop
                if (p >= ki) {
                    inverse_elements[i].push(new_element);
                }
            } while (--np);
            M.elements[i] = els;
            // Then, subtract this row from those above it to
            // give the identity matrix on the left hand side
            for (j = 0; j < i; j++) {
                els = [];
                np = kp;
                do {
                    p = kp - np;
                    els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]);
                } while (--np);
                M.elements[j] = els;
            }
        } while (--ni);
        return Matrix.create(inverse_elements);
    },
    // Set the matrix's elements from an array. If the argument passed
    // is a vector, the resulting matrix will be a single column.
    setElements: function (els) {
        var i, elements = els.elements || els;
        if (typeof (elements[0][0]) != 'undefined') {
            var ni = elements.length, ki = ni, nj = void 0, kj = void 0, j = void 0;
            this.elements = [];
            do {
                i = ki - ni;
                nj = elements[i].length;
                kj = nj;
                this.elements[i] = [];
                do {
                    j = kj - nj;
                    this.elements[i][j] = elements[i][j];
                } while (--nj);
            } while (--ni);
            return this;
        }
        var n = elements.length, k = n;
        this.elements = [];
        do {
            i = k - n;
            this.elements.push([elements[i]]);
        } while (--n);
        return this;
    }
};
// Constructor function
Matrix.create = function (elements) {
    var M = new Matrix();
    return M.setElements(elements);
};
// Identity matrix of size n
Matrix.I = function (n) {
    var els = [], k = n, i, nj, j;
    do {
        i = k - n;
        els[i] = [];
        nj = k;
        do {
            j = k - nj;
            els[i][j] = (i == j) ? 1 : 0;
        } while (--nj);
    } while (--n);
    return Matrix.create(els);
}; /*
 * purecssmatrix.js, version 0.10, part of:
 * http://janne.aukia.com/zoomooz
 *
 * 0.10 initial stand-alone version
 *
 * LICENCE INFORMATION:
 *
 * Copyright (c) 2010 Janne Aukia (janne.aukia.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL Version 2 (GPL-LICENSE.txt) licenses.
 *
 */
exports.PureCSSMatrix = (function () {
    "use strict";
    //**********************************//
    //***  Variables                 ***//
    //**********************************//
    var regexp_is_deg = /deg$/;
    var regexp_filter_number = /([0-9.\-e]+)/g;
    var regexp_trans_splitter = /([a-zA-Z]+)\(([^\)]+)\)/g;
    //**********************************//
    //***  WebKitCSSMatrix in        ***//
    //***  pure Javascript           ***//
    //**********************************//
    function CssMatrix(trans) {
        if (trans && trans !== null && trans != "none") {
            if (trans instanceof Matrix) {
                // @ts-ignore
                this.setMatrix(trans);
            }
            else {
                // @ts-ignore
                this.setMatrixValue(trans);
            }
        }
        else {
            // @ts-ignore
            this.m = Matrix.I(3);
        }
    }
    CssMatrix.prototype.setMatrix = function (matr) {
        this.m = matr;
    };
    function rawRotationToRadians(raw) {
        var rot = parseFloat(filterNumber(raw));
        if (raw.match(regexp_is_deg)) {
            rot = (2 * Math.PI) * rot / 360.0;
        }
        return rot;
    }
    CssMatrix.prototype.setMatrixValue = function (transString) {
        var mtr = Matrix.I(3);
        var items;
        while ((items = regexp_trans_splitter.exec(transString)) !== null) {
            var action = items[1].toLowerCase();
            var val = items[2].split(",");
            var trans = void 0;
            if (action == "matrix") {
                trans = Matrix.create([[parseFloat(val[0]), parseFloat(val[2]), parseFloat(filterNumber(val[4]))],
                    [parseFloat(val[1]), parseFloat(val[3]), parseFloat(filterNumber(val[5]))],
                    [0, 0, 1]]);
            }
            else if (action == "translate") {
                trans = Matrix.I(3);
                trans.elements[0][2] = parseFloat(filterNumber(val[0]));
                trans.elements[1][2] = parseFloat(filterNumber(val[1]));
            }
            else if (action == "scale") {
                var sx = parseFloat(val[0]);
                var sy = void 0;
                if (val.length > 1) {
                    sy = parseFloat(val[1]);
                }
                else {
                    sy = sx;
                }
                trans = Matrix.create([[sx, 0, 0], [0, sy, 0], [0, 0, 1]]);
            }
            else if (action == "rotate") {
                // @ts-ignore
                trans = Matrix.RotationZ(rawRotationToRadians(val[0]));
            }
            else if (action == "skew" || action == "skewx") {
                // TODO: supports only one parameter skew
                trans = Matrix.I(3);
                trans.elements[0][1] = Math.tan(rawRotationToRadians(val[0]));
            }
            else if (action == "skewy") {
                // TODO: test that this works (or unit test them all!)
                trans = Matrix.I(3);
                trans.elements[1][0] = Math.tan(rawRotationToRadians(val[0]));
            }
            else {
                console.log("Problem with setMatrixValue", action, val);
            }
            mtr = mtr.multiply(trans);
        }
        this.m = mtr;
    };
    CssMatrix.prototype.multiply = function (m2) {
        return new CssMatrix(this.m.multiply(m2.m));
    };
    CssMatrix.prototype.inverse = function () {
        if (Math.abs(this.m.elements[0][0]) < 0.000001) {
            /* fixes a weird displacement problem with 90 deg rotations */
            this.m.elements[0][0] = 0;
        }
        return new CssMatrix(this.m.inverse());
    };
    CssMatrix.prototype.translate = function (x, y) {
        var trans = Matrix.I(3);
        trans.elements[0][2] = x;
        trans.elements[1][2] = y;
        return new CssMatrix(this.m.multiply(trans));
    };
    CssMatrix.prototype.scale = function (sx, sy) {
        var trans = Matrix.create([[sx, 0, 0], [0, sy, 0], [0, 0, 1]]);
        var scaleMatrix = new CssMatrix(this.m.multiply(trans));
        return scaleMatrix;
    };
    CssMatrix.prototype.rotate = function (rot) {
        // @ts-ignore
        var trans = Matrix.RotationZ(rot);
        return new CssMatrix(this.m.multiply(trans));
    };
    CssMatrix.prototype.toString = function () {
        var e = this.m.elements;
        var pxstr = "";
        if ($.browser.mozilla || $.browser.opera) {
            pxstr = "px";
        }
        return "matrix(" + printFixedNumber(e[0][0]) + ", " + printFixedNumber(e[1][0]) + ", " +
            printFixedNumber(e[0][1]) + ", " + printFixedNumber(e[1][1]) + ", " +
            printFixedNumber(e[0][2]) + pxstr + ", " + printFixedNumber(e[1][2]) + pxstr + ")";
    };
    //****************************************//
    //***  Not part of the WebkitCSSMatrix ***//
    //***  interface (but used in Zoomooz) ***//
    //****************************************//
    CssMatrix.prototype.elements = function () {
        var mv = this.m.elements;
        return {
            "a": mv[0][0], "b": mv[1][0], "c": mv[0][1],
            "d": mv[1][1], "e": mv[0][2], "f": mv[1][2]
        };
    };
    //**********************************//
    //***  Helpers                   ***//
    //**********************************//
    function filterNumber(x) {
        return x.match(regexp_filter_number);
    }
    function printFixedNumber(x) {
        return Number(x).toFixed(6);
    }
    return CssMatrix;
})();
;