Skip to content

Commit 67fed0b

Browse files
⚙ Subclassing simplified, MonoBehaviour dependency removed
- Base quadtree now independent of MonoBehaviour - Improved generic classes - Eased subclassing of the tree structures - Improved and added missing doc-comments
1 parent 0ca6407 commit 67fed0b

22 files changed

+1101
-824
lines changed

Scripts/GameObjectRootNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Quadtree
66
/// <inheritdoc cref="RootNode{TItem}"/>
77
[ExecuteInEditMode]
88
[AddComponentMenu("Spatial partitioning/Quadtree/Root node (for GameObjects)")]
9-
public class GameObjectRootNode : RootNode<GameObjectItem>
9+
public class GameObjectRootNode : QuadtreeMonoRoot<GameObjectItem>
1010
{
1111
}
1212
}

Scripts/GameObjectRootNode.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/INode.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using Quadtree.Items;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Quadtree
6+
{
7+
/// <summary>
8+
/// Mandatory interface of any single quad tree node.
9+
/// </summary>
10+
public interface INode<TItem, TNode>
11+
where TItem : IItem<TItem, TNode>
12+
where TNode : INode<TItem, TNode>
13+
{
14+
/// <summary>
15+
/// Bounds of this tree node.
16+
/// </summary>
17+
Bounds Bounds { get; set; }
18+
19+
/// <summary>
20+
/// Root of the whole tree.
21+
/// </summary>
22+
IQuadtreeRoot<TItem, TNode> TreeRoot { get; set; }
23+
24+
/// <summary>
25+
/// Reference to parent tree node.
26+
/// </summary>
27+
/// <remarks>
28+
/// Is <c>null</c> for root node of the tree.
29+
/// </remarks>
30+
TNode ParentNode { get; set; }
31+
32+
/// <summary>
33+
/// Child nodes of this node.
34+
/// </summary>
35+
IList<TNode> SubNodes { get; set; }
36+
37+
/// <summary>
38+
/// Verifies whether provided boundaries (<paramref name="bounds"/>) are fully contained within the boundaries of the node.
39+
/// </summary>
40+
///
41+
/// <param name="bounds">Boundaries of an object</param>
42+
/// <returns><c>True</c> if object is fully contained within the node, <c>False</c> otherwise</returns>
43+
bool Contains(Bounds bounds);
44+
45+
/// <summary>
46+
/// Calculates relative internal position of the provided bounds (<paramref name="bounds"/>) within the node.
47+
/// </summary>
48+
/// <remarks>
49+
/// The method expects the boundaries to be fully contained within the node.
50+
/// </remarks>
51+
///
52+
/// <param name="bounds">Boundaries contained within the node</param>
53+
/// <returns>Relative internal position</returns>
54+
IntraLocation Location(Bounds bounds);
55+
56+
/// <summary>
57+
/// Inserts item (<paramref name="item"/>) into the smallest node possible in the subtree.
58+
/// </summary>
59+
/// <remarks>
60+
/// The method expects item boundaries to be fully contained within the node.
61+
/// </remarks>
62+
///
63+
/// <param name="item">Item to be inserted</param>
64+
void Insert(TItem item);
65+
66+
/// <summary>
67+
/// Removes the provided item (<paramref name="item"/>) from the node and its subtree.
68+
/// </summary>
69+
///
70+
/// <param name="item">Item to be removed from the tree</param>
71+
void Remove(TItem item);
72+
73+
/// <summary>
74+
/// Checks whether the node and recursively all its subnodes are empty.
75+
/// </summary>
76+
///
77+
/// <returns><c>True</c> if node and all its subnodes are empty, <c>False</c> otherwise</returns>
78+
bool IsEmpty();
79+
80+
/// <summary>
81+
/// Updates provided item's (<paramref name="item"/>) location within the tree.
82+
/// </summary>
83+
///
84+
/// <param name="item">Item which's location is to be updated</param>
85+
/// <param name="forceInsertionEvaluation"><c>True</c> forces tree to re-insert the item</param>
86+
/// <param name="hasOriginallyContainedItem"><c>True</c> only for the first called node</param>
87+
void Update(TItem item, bool forceInsertionEvaluation = true, bool hasOriginallyContainedItem = true);
88+
89+
/// <summary>
90+
/// Finds items (<paramref name="items"/>) located within provided boundaries (<paramref name="bounds"/>).
91+
/// </summary>
92+
///
93+
/// <param name="bounds">Boundaries to look for items within</param>
94+
/// <param name="items">Output list for found items</param>
95+
void FindAndAddItems(Bounds bounds, ref IList<TItem> items);
96+
97+
/// <summary>
98+
/// Adds all items of this node and its sub-nodes to the provided list of items (<paramref name="items"/>).
99+
/// If boundaries (<paramref name="bounds"/>) are provided then only items intersecting with them will be added.
100+
/// </summary>
101+
///
102+
/// <param name="items">Output list for found items</param>
103+
/// <param name="bounds">Boundaries to look for items within</param>
104+
void AddItems(ref IList<TItem> items, Bounds? bounds = null);
105+
106+
/// <summary>
107+
/// Removes any existing items from the node and removes all of its sub-nodes.
108+
/// </summary>
109+
void Clear();
110+
111+
/// <summary>
112+
/// Displays boundaries of this node and all its sub-nodes and optinally a current number of contained items if <paramref name="displayNumberOfItems"/> is <c>True</c>.
113+
/// </summary>
114+
///
115+
/// <param name="displayNumberOfItems"><c>True</c> if number of node's items should be displayed</param>
116+
void DrawBounds(bool displayNumberOfItems = false);
117+
}
118+
}

Scripts/RootNode.cs.meta renamed to Scripts/INode.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/IQuadtreeRoot.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Quadtree.Items;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Quadtree
6+
{
7+
/// <summary>
8+
/// Main class of the Quadtree structure - it represents the root of the tree.
9+
/// </summary>
10+
public interface IQuadtreeRoot<TItem, TNode>
11+
where TItem : IItem<TItem, TNode>
12+
where TNode : INode<TItem, TNode>
13+
{
14+
/// <summary>
15+
/// The tree has been initialized and is ready to be used.
16+
/// </summary>
17+
bool Initialized { get; }
18+
19+
/// <summary>
20+
/// Node currently acting as a root of the tree.
21+
/// </summary>
22+
TNode CurrentRootNode { get; }
23+
24+
/// <summary>
25+
/// Minimum possible size of any of the nodes.
26+
/// </summary>
27+
/// <remarks>
28+
/// Must always be a positive number or zero for no size limit.
29+
/// </remarks>
30+
float MinimumPossibleNodeSize { get; }
31+
32+
/// <summary>
33+
/// Determines whether or not should number of items in nodes be displayed in gizmos.
34+
/// </summary>
35+
bool DisplayNumberOfItemsInGizmos { get; }
36+
37+
/// <summary>
38+
/// Inserts item to the tree structure.
39+
/// </summary>
40+
///
41+
/// <param name="item">Item to be inserted</param>
42+
void Insert(TItem item);
43+
44+
/// <summary>
45+
/// Expands size of root node.
46+
/// New root node is created and current root node is assigned as its sub-node.
47+
/// </summary>
48+
void Expand();
49+
50+
/// <summary>
51+
/// Finds items located within provided boundaries.
52+
/// </summary>
53+
///
54+
/// <param name="bounds">Boundaries to look for items within</param>
55+
/// <returns>List of items found within provided boundaries</returns>
56+
List<TItem> Find(Bounds bounds);
57+
58+
/// <summary>
59+
/// Removes provided item from the tree.
60+
/// </summary>
61+
///
62+
/// <param name="item">Item to be removed from the tree</param>
63+
void Remove(TItem item);
64+
65+
/// <summary>
66+
/// Clears and resets the whole tree.
67+
/// </summary>
68+
void Clear();
69+
}
70+
}

Scripts/IQuadtreeRoot.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/IntraLocation.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+

2+
namespace Quadtree
3+
{
4+
/// <summary>
5+
/// Describes relative local position in respect to the current node.
6+
/// </summary>
7+
/// <remarks>
8+
/// Integer values of <c>UPPER_LEFT</c>, <c>UPPER_RIGHT</c>, <c>LOWER_RIGHT</c>, <c>LOWER_LEFT</c> do correspond with the indices of the sub-nodes.
9+
/// </remarks>
10+
public enum IntraLocation
11+
{
12+
UPPER_LEFT,
13+
UPPER_RIGHT,
14+
LOWER_RIGHT,
15+
LOWER_LEFT,
16+
SPANNING_LEFT,
17+
SPANNING_RIGHT,
18+
SPANNING_UPPER,
19+
SPANNING_LOWER,
20+
SPANNING
21+
};
22+
}

Scripts/IntraLocation.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)