Пример #1
0
    private void Relocate(QuadTreeObject <T> item)
    {
        // Are we still inside our parent?
        if (QuadRect.Contains(item.Data.Rect))
        {
            // Good, have we moved inside any of our children?
            if (childTL != null)
            {
                QuadTreeNode <T> dest = GetDestinationTree(item);
                if (item.Owner != dest)
                {
                    // Delete the item from this quad and add it to our child
                    // Note: Do NOT clean during this call, it can potentially delete our destination quad
                    QuadTreeNode <T> formerOwner = item.Owner;

                    Delete(item, false);
                    dest.Insert(item);

                    // Clean up ourselves
                    formerOwner.CleanUpwards();
                }
            }
        }
        else
        {
            // We don't fit here anymore, move up, if we can
            if (parent != null)
            {
                parent.Relocate(item);
            }
        }
    }
Пример #2
0
    ///<summary>
    ///Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />.
    ///</summary>
    ///
    ///<param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1" />.</param>
    ///<exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only.</exception>
    public void Add(T item)
    {
        QuadTreeObject <T> wrappedObject = new QuadTreeObject <T>(item);

        wrappedDictionary.Add(item, wrappedObject);
        quadTreeRoot.Insert(wrappedObject);
    }
Пример #3
0
 /// <summary>
 /// Moves the QuadTree object in the tree
 /// </summary>
 /// <param name="item">The item that has moved</param>
 internal void Move(QuadTreeObject <T> item)
 {
     if (item.Owner != null)
     {
         item.Owner.Relocate(item);
     }
     else
     {
         Relocate(item);
     }
 }
Пример #4
0
    /// <summary>
    /// Add an item to the object list.
    /// </summary>
    /// <param name="item">The item to add.</param>
    private void Add(QuadTreeObject <T> item)
    {
        if (objects == null)
        {
            //m_objects = new List<T>();
            objects = new List <QuadTreeObject <T> >();
        }

        item.Owner = this;
        lock (lockObject)
        {
            objects.Add(item);
        }
    }
Пример #5
0
 /// <summary>
 /// Deletes an item from this QuadTree. If the object is removed causes this Quad to have no objects in its children, it's children will be removed as well.
 /// </summary>
 /// <param name="item">The item to remove.</param>
 /// <param name="clean">Whether or not to clean the tree</param>
 internal void Delete(QuadTreeObject <T> item, bool clean)
 {
     if (item.Owner != null)
     {
         if (item.Owner == this)
         {
             Remove(item);
             if (clean)
             {
                 CleanUpwards();
             }
         }
         else
         {
             item.Owner.Delete(item, clean);
         }
     }
 }
Пример #6
0
    /// <summary>
    /// Insert an item into this QuadTree object.
    /// </summary>
    /// <param name="item">The item to insert.</param>
    internal void Insert(QuadTreeObject <T> item)
    {
        // If this quad doesn't contain the items rectangle, do nothing, unless we are the root
        if (!rect.Contains(item.Data.Rect))
        {
            System.Diagnostics.Debug.Assert(parent == null, "We are not the root, and this object doesn't fit here. How did we get here?");
            if (parent == null)
            {
                // This object is outside of the QuadTree bounds, we should add it at the root level
                Add(item);
            }
            else
            {
                return;
            }
        }

        if (objects == null ||
            childTL == null && objects.Count + 1 <= MaxObjectsPerNode)
        {
            // If there's room to add the object, just add it
            Add(item);
        }
        else
        {
            // No quads, create them and bump objects down where appropriate
            if (childTL == null)
            {
                Subdivide();
            }

            // Find out which tree this object should go in and add it there
            QuadTreeNode <T> destTree = GetDestinationTree(item);
            if (destTree == this)
            {
                Add(item);
            }
            else
            {
                destTree.Insert(item);
            }
        }
    }
Пример #7
0
    /// <summary>
    /// Remove an item from the object list.
    /// </summary>
    /// <param name="item">The object to remove.</param>
    private void Remove(QuadTreeObject <T> item)
    {
        if (objects != null)
        {
            int removeIndex = objects.IndexOf(item);
            if (removeIndex >= 0)
            {
                // We have to consider the case that a tile contains: User, Item1, Item2.  The user can Walk, which removes him from index 0.
                // We should use objects.Remove( index ) and let it "shift up" the remaining objects for us.

                //objects[removeIndex] = objects[objects.Count - 1];
                //objects.RemoveAt(objects.Count - 1);

                // We know the item, so just remove it by its index.
                lock (lockObject)
                {
                    objects.RemoveAt(removeIndex);
                }
            }
        }
    }
Пример #8
0
    /// <summary>
    /// Get the child Quad that would contain an object.
    /// </summary>
    /// <param name="item">The object to get a child for.</param>
    /// <returns></returns>
    private QuadTreeNode <T> GetDestinationTree(QuadTreeObject <T> item)
    {
        // If a child can't contain an object, it will live in this Quad
        QuadTreeNode <T> destTree = this;

        if (childTL.QuadRect.Contains(item.Data.Rect))
        {
            destTree = childTL;
        }
        else if (childTR.QuadRect.Contains(item.Data.Rect))
        {
            destTree = childTR;
        }
        else if (childBL.QuadRect.Contains(item.Data.Rect))
        {
            destTree = childBL;
        }
        else if (childBR.QuadRect.Contains(item.Data.Rect))
        {
            destTree = childBR;
        }

        return(destTree);
    }
Пример #9
0
 public override bool ContainsObject(QuadTreeObject <T, QuadTreePointNode <T> > qto)
 {
     return(CheckContains(QuadRect, qto.Data));
 }
Пример #10
0
 public override bool ContainsObject(QuadTreeObject <T, RectQuadTreeNode <T, Rectangle2D> > qto)
 {
     return(CheckContains(QuadRect, qto.Data));
 }