all files / diagram/interaction/spatial-search/ quad.js

100% Statements 111/111
100% Branches 77/77
100% Functions 10/10
100% Lines 111/111
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   2542× 2542× 2542× 2542× 2542× 2542×   24482× 1569×   24482× 963×   24482× 1731×   24482× 945×   24482× 23609×     6525× 1317×   5208×   32644× 32644× 32644× 32644× 124039× 124039× 124039×   32644×   124039× 124039× 124039× 124039× 100058× 100058× 100058× 45551× 44422× 211× 211× 211×   44422×   1129× 731× 53× 53× 53×   731×     54507× 46431× 45597× 258× 258× 258×   45597×   834× 645× 56× 56× 56×   645×       32644× 32644× 32644×   57354×   55061×   2293×   32644× 32644× 32644× 32644× 33125× 33125×   32644×   33125× 33125× 32644× 32644×     481× 481×   481× 481× 481× 303× 253× 253× 42×       50× 50× 39×       178× 35× 35× 28×       143× 143× 135×     481× 481× 481×          
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Quad = (function () {
        function Quad(left, top, width, height, spatialSearching) {
            this.objects = [];
            this.left = left;
            this.top = top;
            this.width = width;
            this.height = height;
            this.spatialSearch = spatialSearching;
        }
        Quad.prototype.findQuads = function (currentViewPort, quads) {
            if (this.first != null && this.first.isIntersect(currentViewPort)) {
                this.first.findQuads(currentViewPort, quads);
            }
            if (this.second != null && this.second.isIntersect(currentViewPort)) {
                this.second.findQuads(currentViewPort, quads);
            }
            if (this.third != null && this.third.isIntersect(currentViewPort)) {
                this.third.findQuads(currentViewPort, quads);
            }
            if (this.fourth != null && this.fourth.isIntersect(currentViewPort)) {
                this.fourth.findQuads(currentViewPort, quads);
            }
            if (this.objects.length > 0) {
                quads.push(this);
            }
        };
        Quad.prototype.isIntersect = function (t) {
            if (this.left + this.width < t.left || this.top + this.height < t.top || this.left > t.right || this.top > t.bottom) {
                return false;
            }
            return true;
        };
        Quad.prototype.selectQuad = function () {
            var target = null;
            var current = this;
            var quadSet;
            while (current != null) {
                quadSet = current.getQuad(target);
                current = quadSet.source;
                target = quadSet.target || target;
            }
            return target;
        };
        Quad.prototype.getQuad = function (target) {
            target = null;
            var halfWidth = this.width / 2;
            var halfHeight = this.height / 2;
            if (halfWidth >= 1000 && halfHeight >= 1000) {
                var xCenter = this.left + halfWidth;
                var yCenter = this.top + halfHeight;
                if (this.spatialSearch.childRight <= xCenter) {
                    if (this.spatialSearch.childBottom <= yCenter) {
                        if (!this.first) {
                            var newQuad = new Quad(this.left, this.top, halfWidth, halfHeight, this.spatialSearch);
                            newQuad.parent = this;
                            this.first = newQuad;
                        }
                        return { source: this.first };
                    }
                    if (this.spatialSearch.childTop >= yCenter) {
                        if (!this.third) {
                            var newQuad = new Quad(this.left, yCenter, halfWidth, halfHeight, this.spatialSearch);
                            newQuad.parent = this;
                            this.third = newQuad;
                        }
                        return { source: this.third };
                    }
                }
                else if (this.spatialSearch.childLeft >= xCenter) {
                    if (this.spatialSearch.childBottom <= yCenter) {
                        if (!this.second) {
                            var newQuad = new Quad(xCenter, this.top, halfWidth, halfHeight, this.spatialSearch);
                            newQuad.parent = this;
                            this.second = newQuad;
                        }
                        return { source: this.second };
                    }
                    if (this.spatialSearch.childTop >= yCenter) {
                        if (!this.fourth) {
                            var newQuad = new Quad(xCenter, yCenter, halfWidth, halfHeight, this.spatialSearch);
                            newQuad.parent = this;
                            this.fourth = newQuad;
                        }
                        return { source: this.fourth };
                    }
                }
            }
            target = this;
            this.objects.push(this.spatialSearch.childNode);
            return { target: this };
        };
        Quad.prototype.isContained = function () {
            if (this.spatialSearch.childLeft >= this.left && this.spatialSearch.childRight <= this.left + this.width &&
                this.spatialSearch.childTop >= this.top && this.spatialSearch.childBottom <= this.top + this.height) {
                return true;
            }
            return false;
        };
        Quad.prototype.addIntoAQuad = function (node) {
            var quadAddition = {};
            this.spatialSearch.setCurrentNode(node);
            var quad = null;
            while (!quadAddition.isAdded) {
                quadAddition = this.spatialSearch.parentQuad.add(quad);
                quad = quadAddition.quad;
            }
            return quad;
        };
        Quad.prototype.add = function (quad) {
            quad = null;
            if (this.isContained()) {
                quad = this.selectQuad();
                return { isAdded: true, quad: quad };
            }
            else {
                var newParent = void 0;
                var isempty = this.objects.length === 0 && !this.first && !this.second && !this.third &&
                    !this.fourth;
                var newWidth = this.width * 2;
                var newHeight = this.height * 2;
                if (this.spatialSearch.childLeft < this.left) {
                    if (this.spatialSearch.childTop < this.top) {
                        newParent = new Quad(this.left - this.width, this.top - this.height, newWidth, newHeight, this.spatialSearch);
                        if (!isempty) {
                            newParent.fourth = this;
                        }
                    }
                    else {
                        newParent = new Quad(this.left - this.width, this.top, newWidth, newHeight, this.spatialSearch);
                        if (!isempty) {
                            newParent.second = this;
                        }
                    }
                }
                else if (this.spatialSearch.childTop < this.top) {
                    newParent = new Quad(this.left, this.top - this.height, newWidth, newHeight, this.spatialSearch);
                    if (!isempty) {
                        newParent.third = this;
                    }
                }
                else {
                    newParent = new Quad(this.left, this.top, newWidth, newHeight, this.spatialSearch);
                    if (!isempty) {
                        newParent.first = this;
                    }
                }
                this.parent = newParent;
                this.spatialSearch.parentQuad = newParent;
                return { isAdded: false, quad: quad };
            }
        };
        return Quad;
    }());
    exports.Quad = Quad;
});