473,785 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Generic" QuadTree implementation

GaryTexmo
1,501 Recognized Expert Top Contributor
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 21089
GaryTexmo
1,501 Recognized Expert Top Contributor
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
3868
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 preview, it starts to draw (ev.Graphics.DrawImage) but after a few seconds an exception throws:
2
9199
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 for those objects. In fact, all the programmer has to worry about is the total sum of objects loaded into RAM at any known point. Memory leaks are not a problem. .... So one would like to think. The reality is that delegates and event...
3
1679
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; Console.Writeline("{0}, {1}, {2}", o, o, o); in which it appears that code referencing an Object magically knows
0
1692
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 Using CreateZone Class. But How to create other files like SOA, MX, Reverse look Up & other files. Iam working on Win 2000 M/c.Kindly Help. -- Regards, Prasana Srinivasan Software Engineer
1
1340
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 was wondering, is it possible to create my own kind of Command Column, with my own defined buttons and behaviors? Can I use a TemplateColumn as the basis for this? If you need more information to answer this question, let me know and I'll try...
4
2957
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: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the returned value.
5
11132
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 number of warnings from the compiler like: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets : warning : Type library exporter warning processing 'Utility.Array1D`1, Test2003'. Warning: Type library exporter encountered a...
0
1530
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 definition, along with creating abstraction layers for older-style C code. I've been working with .net (almost exclusively with C++.net, just a bit of C#) to create end-user interfaces with a relatively good deal of success (I'm used to ansi/STL and...
2
2246
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 someExtendedFunctionality(void) {} };
2
3520
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 manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.