473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

"Generic" QuadTree implementation

GaryTexmo
1,501 Expert 1GB
For a project I'm working on I had need of a QuadTree to give me quick and easy access to objects within a given bounds. I learned quite a bit working on it and thought it might be a good idea to share the code with the Bytes community.

To use it, create a new QuadTree and tell it what area it's going to cover. Then simply insert objects into it using the Insert method. You can use the Remove method to take objects out. On a removal, if a quad's children are empty, the children will be removed.

Some notes, in no particular order...

* This QuadTree holds any type, so long as that type inherits from IHasRectangle and implements the property the interface defines. This is so the QuadTree knows what the bounds of an object are.

* In this implementation, a quad will subdivide when it holds more than two objects. That said, a quad will only contain an object that it completely contains. If a quad's children cannot contain an object, it will live in the quad itself.

* The code is built using an XNA rectangle. It should be easy to modify the code to work with System.Drawing, or even a custom Rectangle object if desired. So long as the object provides the appropriate methods (Contains and Intersects), minimal work should be required to port.

* This code is offered freely, enjoy!

* In light of the above, note that it's not guaranteed to be bug free. I've tested it fairly extensively but it's quite possible I missed something. I'm definitely open to feedback, but please keep it constructive.

* I'm still doing a bit of work on this, but it's going to make it a little more XNA specific. This is the generic version of the object. I may post an update later.

Enough rambling... here's the code.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6.  
  7. namespace QuadTree
  8. {
  9.     /// <summary>
  10.     /// Interface to define Rect, so that QuadTree knows how to store the object.
  11.     /// </summary>
  12.     public interface IHasRectangle
  13.     {
  14.         /// <summary>
  15.         /// The rectangle that defines the object's boundaries.
  16.         /// </summary>
  17.         Rectangle Rect { get; }
  18.     }
  19.  
  20.     /// <summary>
  21.     /// A QuadTree Object that provides fast and efficient storage of objects in a world space.
  22.     /// </summary>
  23.     /// <typeparam name="T">Any object iheriting from IHasRect.</typeparam>
  24.     public class QuadTree<T> where T : IHasRectangle
  25.     {
  26.         #region Constants
  27.         // How many objects can exist in a QuadTree before it sub divides itself
  28.         private const int MAX_OBJECTS_PER_NODE = 2;
  29.         #endregion
  30.  
  31.         #region Private Members
  32.         private List<T> m_objects = null;       // The objects in this QuadTree
  33.         private Rectangle m_rect;               // The area this QuadTree represents
  34.  
  35.         private QuadTree<T> m_childTL = null;   // Top Left Child
  36.         private QuadTree<T> m_childTR = null;   // Top Right Child
  37.         private QuadTree<T> m_childBL = null;   // Bottom Left Child
  38.         private QuadTree<T> m_childBR = null;   // Bottom Right Child
  39.         #endregion
  40.  
  41.         #region Public Properties
  42.         /// <summary>
  43.         /// The area this QuadTree represents.
  44.         /// </summary>
  45.         public Rectangle QuadRect { get { return m_rect; } }
  46.  
  47.         /// <summary>
  48.         /// The top left child for this QuadTree
  49.         /// </summary>
  50.         public QuadTree<T> TopLeftChild { get { return m_childTL; } }
  51.  
  52.         /// <summary>
  53.         /// The top right child for this QuadTree
  54.         /// </summary>
  55.         public QuadTree<T> TopRightChild { get { return m_childTR; } }
  56.  
  57.         /// <summary>
  58.         /// The bottom left child for this QuadTree
  59.         /// </summary>
  60.         public QuadTree<T> BottomLeftChild { get { return m_childBL; } }
  61.  
  62.         /// <summary>
  63.         /// The bottom right child for this QuadTree
  64.         /// </summary>
  65.         public QuadTree<T> BottomRightChild { get { return m_childBR; } }
  66.  
  67.         /// <summary>
  68.         /// The objects contained in this QuadTree at it's level (ie, excludes children)
  69.         /// </summary>
  70.         public List<T> Objects { get { return m_objects; } }
  71.  
  72.         /// <summary>
  73.         /// How many total objects are contained within this QuadTree (ie, includes children)
  74.         /// </summary>
  75.         public int Count { get { return this.ObjectCount(); } }
  76.         #endregion
  77.  
  78.         #region Constructor
  79.         /// <summary>
  80.         /// Creates a QuadTree for the specified area.
  81.         /// </summary>
  82.         /// <param name="rect">The area this QuadTree object will encompass.</param>
  83.         public QuadTree(Rectangle rect)
  84.         {
  85.             m_rect = rect;
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Creates a QuadTree for the specified area.
  90.         /// </summary>
  91.         /// <param name="x">The top-left position of the area rectangle.</param>
  92.         /// <param name="y">The top-right position of the area reactangle.</param>
  93.         /// <param name="width">The width of the area rectangle.</param>
  94.         /// <param name="height">The height of the area rectangle.</param>
  95.         public QuadTree(int x, int y, int width, int height)
  96.         {
  97.             m_rect = new Rectangle(x, y, width, height);
  98.         }
  99.         #endregion
  100.  
  101.         #region Private Members
  102.         /// <summary>
  103.         /// Add an item to the object list.
  104.         /// </summary>
  105.         /// <param name="item">The item to add.</param>
  106.         private void Add(T item)
  107.         {
  108.             if (m_objects == null)
  109.                 m_objects = new List<T>();
  110.  
  111.             m_objects.Add(item);
  112.         }
  113.  
  114.         /// <summary>
  115.         /// Remove an item from the object list.
  116.         /// </summary>
  117.         /// <param name="item">The object to remove.</param>
  118.         private void Remove(T item)
  119.         {
  120.             if (m_objects != null && m_objects.Contains(item))
  121.                 m_objects.Remove(item);
  122.         }
  123.  
  124.         /// <summary>
  125.         /// Get the total for all objects in this QuadTree, including children.
  126.         /// </summary>
  127.         /// <returns>The number of objects contained within this QuadTree and its children.</returns>
  128.         private int ObjectCount()
  129.         {
  130.             int count = 0;
  131.  
  132.             // Add the objects at this level
  133.             if (m_objects != null) count += m_objects.Count;
  134.  
  135.             // Add the objects that are contained in the children
  136.             if (m_childTL != null)
  137.             {
  138.                 count += m_childTL.ObjectCount();
  139.                 count += m_childTR.ObjectCount();
  140.                 count += m_childBL.ObjectCount();
  141.                 count += m_childBR.ObjectCount();
  142.             }
  143.  
  144.             return count;
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Subdivide this QuadTree and move it's children into the appropriate Quads where applicable.
  149.         /// </summary>
  150.         private void Subdivide()
  151.         {
  152.             // We've reached capacity, subdivide...
  153.             Point size = new Point(m_rect.Width / 2, m_rect.Height / 2);
  154.             Point mid = new Point(m_rect.X + size.X, m_rect.Y + size.Y);
  155.  
  156.             m_childTL = new QuadTree<T>(new Rectangle(m_rect.Left, m_rect.Top, size.X, size.Y));
  157.             m_childTR = new QuadTree<T>(new Rectangle(mid.X, m_rect.Top, size.X, size.Y));
  158.             m_childBL = new QuadTree<T>(new Rectangle(m_rect.Left, mid.Y, size.X, size.Y));
  159.             m_childBR = new QuadTree<T>(new Rectangle(mid.X, mid.Y, size.X, size.Y));
  160.  
  161.             // If they're completely contained by the quad, bump objects down
  162.             for (int i = 0; i < m_objects.Count; i++)
  163.             {
  164.                 QuadTree<T> destTree = GetDestinationTree(m_objects[i]);
  165.  
  166.                 if (destTree != this)
  167.                 {
  168.                     // Insert to the appropriate tree, remove the object, and back up one in the loop
  169.                     destTree.Insert(m_objects[i]);
  170.                     Remove(m_objects[i]);
  171.                     i--;
  172.                 }
  173.             }
  174.         }
  175.  
  176.         /// <summary>
  177.         /// Get the child Quad that would contain an object.
  178.         /// </summary>
  179.         /// <param name="item">The object to get a child for.</param>
  180.         /// <returns></returns>
  181.         private QuadTree<T> GetDestinationTree(T item)
  182.         {
  183.             // If a child can't contain an object, it will live in this Quad
  184.             QuadTree<T> destTree = this;
  185.  
  186.             if (m_childTL.QuadRect.Contains(item.Rect))
  187.             {
  188.                 destTree = m_childTL;
  189.             }
  190.             else if (m_childTR.QuadRect.Contains(item.Rect))
  191.             {
  192.                 destTree = m_childTR;
  193.             }
  194.             else if (m_childBL.QuadRect.Contains(item.Rect))
  195.             {
  196.                 destTree = m_childBL;
  197.             }
  198.             else if (m_childBR.QuadRect.Contains(item.Rect))
  199.             {
  200.                 destTree = m_childBR;
  201.             }
  202.  
  203.             return destTree;
  204.         }
  205.         #endregion
  206.  
  207.         #region Public Methods
  208.         /// <summary>
  209.         /// Clears the QuadTree of all objects, including any objects living in its children.
  210.         /// </summary>
  211.         public void Clear()
  212.         {
  213.             // Clear out the children, if we have any
  214.             if (m_childTL != null)
  215.             {
  216.                 m_childTL.Clear();
  217.                 m_childTR.Clear();
  218.                 m_childBL.Clear();
  219.                 m_childBR.Clear();
  220.             }
  221.  
  222.             // Clear any objects at this level
  223.             if (m_objects != null)
  224.             {
  225.                 m_objects.Clear();
  226.                 m_objects = null;
  227.             }
  228.  
  229.             // Set the children to null
  230.             m_childTL = null;
  231.             m_childTR = null;
  232.             m_childBL = null;
  233.             m_childBR = null;
  234.         }
  235.  
  236.         /// <summary>
  237.         /// 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.
  238.         /// </summary>
  239.         /// <param name="item">The item to remove.</param>
  240.         public void Delete(T item)
  241.         {
  242.             // If this level contains the object, remove it
  243.             bool objectRemoved = false;
  244.             if (m_objects != null && m_objects.Contains(item))
  245.             {
  246.                 Remove(item);
  247.                 objectRemoved = true;
  248.             }
  249.  
  250.             // If we didn't find the object in this tree, try to delete from its children
  251.             if (m_childTL != null && !objectRemoved)
  252.             {
  253.                 m_childTL.Delete(item);
  254.                 m_childTR.Delete(item);
  255.                 m_childBL.Delete(item);
  256.                 m_childBR.Delete(item);
  257.             }
  258.  
  259.             if (m_childTL != null)
  260.             {
  261.                 // If all the children are empty, delete all the children
  262.                 if (m_childTL.Count == 0 &&
  263.                     m_childTR.Count == 0 &&
  264.                     m_childBL.Count == 0 &&
  265.                     m_childBR.Count == 0)
  266.                 {
  267.                     m_childTL = null;
  268.                     m_childTR = null;
  269.                     m_childBL = null;
  270.                     m_childBR = null;
  271.                 }
  272.             }
  273.         }
  274.  
  275.         /// <summary>
  276.         /// Insert an item into this QuadTree object.
  277.         /// </summary>
  278.         /// <param name="item">The item to insert.</param>
  279.         public void Insert(T item)
  280.         {
  281.             // If this quad doesn't intersect the items rectangle, do nothing
  282.             if (!m_rect.Intersects(item.Rect))
  283.                 return;
  284.  
  285.             if (m_objects == null || 
  286.                 (m_childTL == null && m_objects.Count + 1 <= MAX_OBJECTS_PER_NODE))
  287.             {
  288.                 // If there's room to add the object, just add it
  289.                 Add(item);
  290.             }
  291.             else
  292.             {
  293.                 // No quads, create them and bump objects down where appropriate
  294.                 if (m_childTL == null)
  295.                 {
  296.                     Subdivide();
  297.                 }
  298.  
  299.                 // Find out which tree this object should go in and add it there
  300.                 QuadTree<T> destTree = GetDestinationTree(item);
  301.                 if (destTree == this)
  302.                 {
  303.                     Add(item);
  304.                 }
  305.                 else
  306.                 {
  307.                     destTree.Insert(item);
  308.                 }
  309.             }
  310.         }
  311.  
  312.         /// <summary>
  313.         /// Get the objects in this tree that intersect with the specified rectangle.
  314.         /// </summary>
  315.         /// <param name="rect">The rectangle to find objects in.</param>
  316.         /// <param name="results">A reference to a list that will be populated with the results.</param>
  317.         public void GetObjects(Rectangle rect, ref List<T> results)
  318.         {
  319.             // We can't do anything if the results list doesn't exist
  320.             if (results != null)
  321.             {
  322.                 if (rect.Contains(m_rect))
  323.                 {
  324.                     // If the search area completely contains this quad, just get every object this quad and all it's children have
  325.                     GetAllObjects(ref results);
  326.                 }
  327.                 else if (rect.Intersects(m_rect))
  328.                 {
  329.                     // Otherwise, if the quad isn't fully contained, only add objects that intersect with the search rectangle
  330.                     if (m_objects != null)
  331.                     {
  332.                         for (int i = 0; i < m_objects.Count; i++)
  333.                         {
  334.                             if (rect.Intersects(m_objects[i].Rect))
  335.                             {
  336.                                 results.Add(m_objects[i]);
  337.                             }
  338.                         }
  339.                     }
  340.  
  341.                     // Get the objects for the search rectangle from the children
  342.                     if (m_childTL != null)
  343.                     {
  344.                         m_childTL.GetObjects(rect, ref results);
  345.                         m_childTR.GetObjects(rect, ref results);
  346.                         m_childBL.GetObjects(rect, ref results);
  347.                         m_childBR.GetObjects(rect, ref results);
  348.                     }
  349.                 }
  350.             }
  351.         }
  352.  
  353.         /// <summary>
  354.         /// Get all objects in this Quad, and it's children.
  355.         /// </summary>
  356.         /// <param name="results">A reference to a list in which to store the objects.</param>
  357.         public void GetAllObjects(ref List<T> results)
  358.         {
  359.             // If this Quad has objects, add them
  360.             if (m_objects != null)
  361.                 results.AddRange(m_objects);
  362.  
  363.             // If we have children, get their objects too
  364.             if (m_childTL != null)
  365.             {
  366.                 m_childTL.GetAllObjects(ref results);
  367.                 m_childTR.GetAllObjects(ref results);
  368.                 m_childBL.GetAllObjects(ref results);
  369.                 m_childBR.GetAllObjects(ref results);
  370.             }
  371.         }
  372.         #endregion
  373.     }
  374. }
  375.  
Feb 3 '10 #1
2 21015
GaryTexmo
1,501 Expert 1GB
Update

This project is still being developed. When we have time to work on it, a friend and I are enhancing this to support moving objects in a more efficient manner. The project can be tracked at SourceForge at the following URL: https://sourceforge.net/projects/quadtree/.

The above still provides a good, basic implementation, but if your project is going to have many moving objects, you may want to keep track of the updates.

Thanks!
Oct 13 '10 #2
Thank you sir, I've implemented your code with my own interface and used a few foreach loops in an array of children, using null for no children. Thanks again for the start on this. I see using this for terrain and game objects that exist therein. :)
Oct 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Vicente Nicolau | last post by:
hello, I have a problem when I preview or print a document in an ISO A0. I have an application which load an 10x10 image (bmp or jpg) and spread it in the A0 paper. When I make the print...
2
by: Jon Davis | last post by:
The garbage handler in the .NET framework is handy. When objects fall out of scope, they are automatically destroyed, and the programmer doesn't have to worry about deallocating the memory space...
3
by: Tom H. | last post by:
Newbie question, hopefully not one that will embarrass me. I'm reading Richter's Applied MS .NET Framework Programming and keep seeing snippets such as this: Int32 v = 5; Object o = v;...
0
by: Prasana | last post by:
Hi EveryBody, I have one Problem When I am Creating the DNS Zone Using Resource Record using System Management. Iam Getting an Error Called "Generic Error". Iam Able to Create a Primary Zone File...
1
by: jason | last post by:
Hello everyone, I'm trying to expand my understanding of the DataGrid control, and I'm specifically curious about the Command columns. I have successfully implemented an EditCommandColumn, but I...
4
by: rsa_net_newbie | last post by:
Hi there, I have a Managed C++ object (in a DLL) which has a method that is defined like ... Generic::List<String^>^ buildList(String^ inParm) Now, when I compile it, I get "warning C4172:...
5
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
0
by: epoxyparser | last post by:
Hi all, I work as a glue coder and often have to interface existing mechanisms with other platforms. Creating bridge code between command-line functionality and desktop GUIs takes up a lot of that...
2
by: Christof Warlich | last post by:
Hi, I'd like to define a class that should behave as much as posible like std::string, but that has some small additional property: class ExtendedString: public std::string { public: void...
2
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.