Use primary constructor for Quadtree
This commit is contained in:
parent
d92487ffc7
commit
2a9fba683a
@ -12,60 +12,48 @@ namespace SpatialCollections
|
|||||||
/// <typeparam name="T">Type of the managed entities.</typeparam>
|
/// <typeparam name="T">Type of the managed entities.</typeparam>
|
||||||
// TODO: Support for moving entites without having to remove and re-add them.
|
// TODO: Support for moving entites without having to remove and re-add them.
|
||||||
// TODO: Add method to prune empty leaves. An empty leaf is not removed automatically when its last entity is removed.
|
// TODO: Add method to prune empty leaves. An empty leaf is not removed automatically when its last entity is removed.
|
||||||
public class Quadtree<T>
|
public class Quadtree<T>(int maxLeafSize, int maxTreeDepth, Func<T, Vector2> getPositionCallback)
|
||||||
{
|
{
|
||||||
// TODO: It should be possible to calculate whether average query time will be better with or without split, without resorting to max leaf size and max tree depth.
|
// TODO: It should be possible to calculate whether average query time will be better with or without split, without resorting to max leaf size and max tree depth.
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maximum number of items in a leaf vertex before it is split, unless the depth of the leaf is greater or
|
/// Maximum number of items in a leaf vertex before it is split, unless the depth of the leaf is greater or
|
||||||
/// equal to the maximum tree depth.
|
/// equal to the maximum tree depth.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _maxLeafSize;
|
private readonly int _maxLeafSize = maxLeafSize;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maximum depth of the quadtree, that is the maximum allowed length of the path from the root to a new leaf
|
/// Maximum depth of the quadtree, that is the maximum allowed length of the path from the root to a new leaf
|
||||||
/// when determining whether a leaf vertex may be split.
|
/// when determining whether a leaf vertex may be split.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly int _maxTreeDepth;
|
private readonly int _maxTreeDepth = maxTreeDepth;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Callback to determine the position of an entity.
|
/// Callback to determine the position of an entity.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Func<T, Vector2> _getPositionCallback;
|
private readonly Func<T, Vector2> _getPositionCallback = getPositionCallback;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The list of quadtree vertices. The root vertex is always the first item. The four child vertices of a branch
|
/// The list of quadtree vertices. The root vertex is always the first item. The four child vertices of a branch
|
||||||
/// are always created together and stored contiguously.
|
/// are always created together and stored contiguously.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly List<QuadtreeVertex> _vertices;
|
private readonly List<QuadtreeVertex> _vertices = [new(-1, 0)];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The list of all items stored in the quadtree.
|
/// The list of all items stored in the quadtree.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly List<QuadtreeItem<T>> _items;
|
private readonly List<QuadtreeItem<T>> _items = [];
|
||||||
|
|
||||||
private Rectangle _rootBoundingBox;
|
private Rectangle _rootBoundingBox = new(Vector2.Zero, Vector2.Zero);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stack of vertex indices with their bounding boxes. It is reused for each query.
|
/// Stack of vertex indices with their bounding boxes. It is reused for each query.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Stack<QuadtreeQuery> _queryStack;
|
private readonly Stack<QuadtreeQuery> _queryStack = new();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stack of vertex indices. It is reused for each query.
|
/// Stack of vertex indices. It is reused for each query.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly Stack<int> _vertexStack;
|
private readonly Stack<int> _vertexStack = new();
|
||||||
|
|
||||||
public Quadtree(int maxLeafSize, int maxTreeDepth, Func<T, Vector2> getPositionCallback)
|
|
||||||
{
|
|
||||||
_maxLeafSize = maxLeafSize;
|
|
||||||
_maxTreeDepth = maxTreeDepth;
|
|
||||||
_getPositionCallback = getPositionCallback;
|
|
||||||
_vertices = [new(-1, 0)];
|
|
||||||
_items = [];
|
|
||||||
_rootBoundingBox = new Rectangle(Vector2.Zero, Vector2.Zero);
|
|
||||||
_queryStack = new Stack<QuadtreeQuery>();
|
|
||||||
_vertexStack = new Stack<int>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Count => _items.Count;
|
public int Count => _items.Count;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user