public void Move(SVGBounds bounds) { if (this.bounds.Compare(bounds)) { return; } this.bounds.ApplyBounds(bounds); if (this.cell.bounds.Contains(bounds)) { return; } QuadTreeCell <T> root = this.cell.root; Remove(); root.Add(this); }
public QuadTreeNode <T> Add(T data, SVGBounds bounds) { return(_root.Add(data, bounds)); }
public QuadTreeNode <T> Add(QuadTreeNode <T> node) { if (nodes == null) { nodes = new List <QuadTreeNode <T> >(); } //Debug.Log("Add"); //Debug.Log(this.bounds); //Debug.Log(bounds); if (this.bounds.Contains(node.bounds)) { // It is inside our region bool leftSide = node.bounds.maxX <= this.bounds.center.x; //bool rightSide = node.bounds.minX >= this.bounds.center.x; bool topSide = node.bounds.minY >= this.bounds.center.y; //bool bottomSide = node.bounds.maxY <= this.bounds.center.y; bool intersectsLeft = node.bounds.minX < this.bounds.center.x; bool intersectsRight = node.bounds.maxX > this.bounds.center.x; bool intersectsTop = node.bounds.maxY > this.bounds.center.y; bool intersectsBottom = node.bounds.minY < this.bounds.center.y; /* * Debug.Log("Iside Region: "+data.GetHashCode()+", leftSide: "+leftSide+", rightSide: "+rightSide+", topSide: "+topSide+", bottomSide: "+bottomSide +"\n node.bounds.maxX: "+node.bounds.maxX+" <= this.bounds.center.x: "+this.bounds.center.x +"\n node.bounds.minX: "+node.bounds.minX+" >= this.bounds.center.x: "+this.bounds.center.x +"\n node.bounds.minY: "+node.bounds.minY+" >= this.bounds.center.y: "+this.bounds.center.y +"\n node.bounds.maxY: "+node.bounds.maxY+" <= this.bounds.center.y: "+this.bounds.center.y * * ); */ if ((intersectsLeft && intersectsRight) || (intersectsTop && intersectsBottom)) { //Debug.Log("Overlays more than one region: "+data.GetHashCode()); node.cell = this; node._depth = this._depth; nodes.Add(node); } else { if (nodes.Count < maxCapacity) { node.cell = this; node._depth = this._depth; nodes.Add(node); } else { //Debug.Log("Max Capacity Reached: "+data.GetHashCode()); if (topSide) { if (leftSide) { if (topLeft == null) { topLeft = new QuadTreeCell <T>(new SVGBounds(this.bounds.minX, this.bounds.center.y, this.bounds.center.x, this.bounds.maxY), this, this.quadTree, maxCapacity); } topLeft._depth = _depth + 1; topLeft.Add(node); } else { if (topRight == null) { topRight = new QuadTreeCell <T>(new SVGBounds(this.bounds.center.x, this.bounds.center.y, this.bounds.maxX, this.bounds.maxY), this, this.quadTree, maxCapacity); } topRight._depth = _depth + 1; topRight.Add(node); } } else { if (leftSide) { if (bottomLeft == null) { bottomLeft = new QuadTreeCell <T>(new SVGBounds(this.bounds.minX, this.bounds.minY, this.bounds.center.x, this.bounds.center.y), this, this.quadTree, maxCapacity); } bottomLeft._depth = _depth + 1; bottomLeft.Add(node); } else { if (bottomRight == null) { bottomRight = new QuadTreeCell <T>(new SVGBounds(this.bounds.center.x, this.bounds.minY, this.bounds.maxX, this.bounds.center.y), this, this.quadTree, maxCapacity); } bottomRight._depth = _depth + 1; bottomRight.Add(node); } } } } } else { node.cell = this; node._depth = this._depth; nodes.Add(node); //return node; /* * // TODO Expanding quadTree * * // It is outside our region * bool outsideLeft = node.bounds.minX < this.bounds.minX; * bool outsideRight = node.bounds.maxX > this.bounds.maxX; * bool outsideTop = node.bounds.maxY > this.bounds.maxY; * bool outsideBottom = node.bounds.minY < this.bounds.minY; * * //bool left = node.bounds.center.x < this.bounds.center.x; * //bool right = node.bounds.center.x >= this.bounds.center.x; * //bool top = node.bounds.center.y >= this.bounds.center.y; * //bool bottom = node.bounds.center.y < this.bounds.center.y; * //Debug.Log("Out of region: "+data.GetHashCode()+", outsideLeft: "+outsideLeft+", outsideRight: "+outsideRight+", outsideTop: "+outsideTop+", outsideBottom: "+outsideBottom); * * if (outsideTop) * { * if (outsideLeft) * { * parent = new QuadTreeCell<T>(new SVGBounds(this.bounds.minX - this.bounds.size.x, this.bounds.minY, this.bounds.maxX, this.bounds.maxY + this.bounds.size.y), null, this.quadTree, maxCapacity); * if (!isCellEmpty) * { * parent.bottomRight = this; * } * quadTree._root = parent; * parent._depth = _depth - 1; * parent.Add(node); * } * * if (outsideRight) * { * parent = new QuadTreeCell<T>(new SVGBounds(this.bounds.minX, this.bounds.minY, this.bounds.maxX + this.bounds.size.x, this.bounds.maxY + this.bounds.size.y), null, this.quadTree, maxCapacity); * if (!isCellEmpty) * parent.bottomLeft = this; * quadTree._root = parent; * parent._depth = _depth - 1; * parent.Add(node); * } * } * if (outsideBottom) * { * if (outsideLeft) * { * parent = new QuadTreeCell<T>(new SVGBounds(this.bounds.minX - this.bounds.size.x, this.bounds.minY - this.bounds.size.y, this.bounds.maxX, this.bounds.maxY), null, this.quadTree, maxCapacity); * if (!isCellEmpty) * parent.topRight = this; * quadTree._root = parent; * parent._depth = _depth - 1; * parent.Add(node); * } * * if (outsideRight) * { * parent = new QuadTreeCell<T>(new SVGBounds(this.bounds.minX, this.bounds.minY - this.bounds.size.y, this.bounds.maxX + this.bounds.size.x, this.bounds.maxY), null, this.quadTree, maxCapacity); * if (!isCellEmpty) * parent.topLeft = this; * quadTree._root = parent; * parent._depth = _depth - 1; * parent.Add(node); * } * } */ } return(node); }