예제 #1
0
 public QuadTree(SVGBounds bounds, int maxCapacity)
 {
     _originalBounds      = bounds;
     _originalMaxCapacity = maxCapacity;
     _root        = new QuadTreeCell <T>(bounds, null, this, _originalMaxCapacity);
     _root._depth = 0;
 }
예제 #2
0
        public void Clear()
        {
            if (nodes != null)
            {
                nodes.Clear();
                nodes = null;
            }

            if (topLeft != null)
            {
                topLeft.Clear();
                topLeft = null;
            }

            if (topRight != null)
            {
                topRight.Clear();
                topRight = null;
            }

            if (bottomLeft != null)
            {
                bottomLeft.Clear();
                bottomLeft = null;
            }

            if (bottomRight != null)
            {
                bottomRight.Clear();
                bottomRight = null;
            }

            _depth = 0;
        }
예제 #3
0
 public QuadTreeCell(SVGBounds bounds, QuadTreeCell <T> parent, QuadTree <T> quadTree, int maxCapacity)
 {
     this.bounds      = bounds;
     this.parent      = parent;
     this.quadTree    = quadTree;
     this.maxCapacity = maxCapacity;
 }
예제 #4
0
 public QuadTreeCell(SVGBounds bounds, int maxCapacity)
 {
     this.bounds      = bounds;
     this.parent      = null;
     this.quadTree    = null;
     this.maxCapacity = maxCapacity;
 }
예제 #5
0
 public QuadTreeCell(SVGBounds bounds)
 {
     this.bounds      = bounds;
     this.parent      = null;
     this.quadTree    = null;
     this.maxCapacity = DEFAULT_MAX_CAPACITY;
 }
예제 #6
0
        internal QuadTreeCell <T> FindRoot(QuadTreeCell <T> current)
        {
            if (current.parent != null)
            {
                return(FindRoot(current.parent));
            }

            return(current);
        }
예제 #7
0
        public static void CleanUnusedCells(QuadTreeCell <T> cell)
        {
            if (cell == null || !cell.isCellEmpty)
            {
                return;
            }

            cell.Remove();
            CleanUnusedCells(cell.parent);
        }
예제 #8
0
        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);
        }
예제 #9
0
 public void Reset()
 {
     _root.Clear();
     _root        = new QuadTreeCell <T>(_originalBounds, null, this, _originalMaxCapacity);
     _root._depth = 0;
 }
예제 #10
0
 public QuadTreeNode(T data, SVGBounds bounds, QuadTreeCell <T> cell)
 {
     this.data   = data;
     this.bounds = bounds;
     this.cell   = cell;
 }
예제 #11
0
        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);
        }