Rename class BoundingBox2 to Rectangle
This commit is contained in:
parent
263e215f41
commit
3bea3f5a9b
@ -8,6 +8,6 @@ namespace SpatialCollections
|
|||||||
|
|
||||||
float BoundingRadius { get; }
|
float BoundingRadius { get; }
|
||||||
|
|
||||||
BoundingBox2 BoundingBox { get; }
|
Rectangle BoundingBox { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ namespace SpatialCollections
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private readonly List<QuadtreeItem<IWorldObject>> _items;
|
private readonly List<QuadtreeItem<IWorldObject>> _items;
|
||||||
|
|
||||||
private BoundingBox2 _rootBoundingBox;
|
private Rectangle _rootBoundingBox;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The maximum bounding radius of all stored objects, used to inflate the bounding box for queries.
|
/// The maximum bounding radius of all stored objects, used to inflate the bounding box for queries.
|
||||||
@ -60,7 +60,7 @@ namespace SpatialCollections
|
|||||||
_maxTreeDepth = maxTreeDepth;
|
_maxTreeDepth = maxTreeDepth;
|
||||||
_vertices = new List<QuadtreeVertex>() { new(-1, 0) };
|
_vertices = new List<QuadtreeVertex>() { new(-1, 0) };
|
||||||
_items = new List<QuadtreeItem<IWorldObject>>();
|
_items = new List<QuadtreeItem<IWorldObject>>();
|
||||||
_rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero);
|
_rootBoundingBox = new Rectangle(Vector2.Zero, Vector2.Zero);
|
||||||
_maxObjectRadius = 0f;
|
_maxObjectRadius = 0f;
|
||||||
_queryStack = new Stack<QuadtreeQuery>();
|
_queryStack = new Stack<QuadtreeQuery>();
|
||||||
_vertexStack = new Stack<int>();
|
_vertexStack = new Stack<int>();
|
||||||
@ -92,7 +92,7 @@ namespace SpatialCollections
|
|||||||
ExpandRoot(obj.Position);
|
ExpandRoot(obj.Position);
|
||||||
}
|
}
|
||||||
|
|
||||||
(int leafIndex, int depth, BoundingBox2 bounds) = FindLeaf(0, _rootBoundingBox, 0, obj.Position);
|
(int leafIndex, int depth, Rectangle bounds) = FindLeaf(0, _rootBoundingBox, 0, obj.Position);
|
||||||
while (_vertices[leafIndex].ChildCount >= _maxLeafSize && depth < _maxTreeDepth)
|
while (_vertices[leafIndex].ChildCount >= _maxLeafSize && depth < _maxTreeDepth)
|
||||||
{
|
{
|
||||||
// Splits the vertex and decends into one of the new leaves.
|
// Splits the vertex and decends into one of the new leaves.
|
||||||
@ -142,9 +142,9 @@ namespace SpatialCollections
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Query(BoundingBox2 box, List<IWorldObject> resultList)
|
public void Query(Rectangle box, List<IWorldObject> resultList)
|
||||||
{
|
{
|
||||||
BoundingBox2 inflatedBox = box;
|
Rectangle inflatedBox = box;
|
||||||
inflatedBox.Expand(_maxObjectRadius);
|
inflatedBox.Expand(_maxObjectRadius);
|
||||||
|
|
||||||
QueryProcessChildVertex(inflatedBox, box, 0, _rootBoundingBox, resultList, _queryStack);
|
QueryProcessChildVertex(inflatedBox, box, 0, _rootBoundingBox, resultList, _queryStack);
|
||||||
@ -154,7 +154,7 @@ namespace SpatialCollections
|
|||||||
QuadtreeQuery query = _queryStack.Pop();
|
QuadtreeQuery query = _queryStack.Pop();
|
||||||
|
|
||||||
int childIndex = _vertices[query.VertexIndex].FirstChildIndex;
|
int childIndex = _vertices[query.VertexIndex].FirstChildIndex;
|
||||||
BoundingBox2 halfBounds = new(query.VertexBounds.Center, query.VertexBounds.Max);
|
Rectangle halfBounds = new(query.VertexBounds.Center, query.VertexBounds.Max);
|
||||||
Vector2 halfSize = halfBounds.Size;
|
Vector2 halfSize = halfBounds.Size;
|
||||||
QueryProcessChildVertex(inflatedBox, box, childIndex++, halfBounds, resultList, _queryStack);
|
QueryProcessChildVertex(inflatedBox, box, childIndex++, halfBounds, resultList, _queryStack);
|
||||||
|
|
||||||
@ -174,7 +174,7 @@ namespace SpatialCollections
|
|||||||
_vertices.Clear();
|
_vertices.Clear();
|
||||||
_vertices.Add(new QuadtreeVertex(-1, 0));
|
_vertices.Add(new QuadtreeVertex(-1, 0));
|
||||||
_items.Clear();
|
_items.Clear();
|
||||||
_rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero);
|
_rootBoundingBox = new Rectangle(Vector2.Zero, Vector2.Zero);
|
||||||
_maxObjectRadius = 0f;
|
_maxObjectRadius = 0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +255,7 @@ namespace SpatialCollections
|
|||||||
_items[itemIndex] = new QuadtreeItem<IWorldObject>(_items[itemIndex].WorldObject, next);
|
_items[itemIndex] = new QuadtreeItem<IWorldObject>(_items[itemIndex].WorldObject, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SplitLeaf(int leafIndex, BoundingBox2 leafBounds)
|
private void SplitLeaf(int leafIndex, Rectangle leafBounds)
|
||||||
{
|
{
|
||||||
// Splits the leaf vertex by turning it into a branch and adding four new leaves.
|
// Splits the leaf vertex by turning it into a branch and adding four new leaves.
|
||||||
var oldLeaf = _vertices[leafIndex];
|
var oldLeaf = _vertices[leafIndex];
|
||||||
@ -291,7 +291,7 @@ namespace SpatialCollections
|
|||||||
return vertexIndex;
|
return vertexIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
private (int leafIndex, int depth, BoundingBox2 bounds) FindLeaf(int vertexIndex, BoundingBox2 bounds,
|
private (int leafIndex, int depth, Rectangle bounds) FindLeaf(int vertexIndex, Rectangle bounds,
|
||||||
int depth, Vector2 position)
|
int depth, Vector2 position)
|
||||||
{
|
{
|
||||||
while (_vertices[vertexIndex].ChildCount == -1)
|
while (_vertices[vertexIndex].ChildCount == -1)
|
||||||
@ -321,8 +321,8 @@ namespace SpatialCollections
|
|||||||
return (vertexIndex, depth, bounds);
|
return (vertexIndex, depth, bounds);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void QueryProcessChildVertex(BoundingBox2 inflatedBox, BoundingBox2 box, int vertexIndex,
|
private void QueryProcessChildVertex(Rectangle inflatedBox, Rectangle box, int vertexIndex,
|
||||||
BoundingBox2 vertexBounds, List<IWorldObject> resultList, Stack<QuadtreeQuery> stack)
|
Rectangle vertexBounds, List<IWorldObject> resultList, Stack<QuadtreeQuery> stack)
|
||||||
{
|
{
|
||||||
switch (inflatedBox.Intersects(vertexBounds))
|
switch (inflatedBox.Intersects(vertexBounds))
|
||||||
{
|
{
|
||||||
@ -353,7 +353,7 @@ namespace SpatialCollections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void QueryAppendContainedItems(int vertexIndex, BoundingBox2 box, List<IWorldObject> resultList)
|
private void QueryAppendContainedItems(int vertexIndex, Rectangle box, List<IWorldObject> resultList)
|
||||||
{
|
{
|
||||||
_vertexStack.Push(vertexIndex);
|
_vertexStack.Push(vertexIndex);
|
||||||
|
|
||||||
@ -377,7 +377,7 @@ namespace SpatialCollections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void QueryAppendContainedLeafItems(int vertexIndex, BoundingBox2 box, List<IWorldObject> resultList)
|
private void QueryAppendContainedLeafItems(int vertexIndex, Rectangle box, List<IWorldObject> resultList)
|
||||||
{
|
{
|
||||||
int index = _vertices[vertexIndex].FirstChildIndex;
|
int index = _vertices[vertexIndex].FirstChildIndex;
|
||||||
while (index != -1)
|
while (index != -1)
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
{
|
{
|
||||||
public int VertexIndex;
|
public int VertexIndex;
|
||||||
|
|
||||||
public BoundingBox2 VertexBounds;
|
public Rectangle VertexBounds;
|
||||||
|
|
||||||
public QuadtreeQuery(int vertexIndex, BoundingBox2 vertexBounds)
|
public QuadtreeQuery(int vertexIndex, Rectangle vertexBounds)
|
||||||
{
|
{
|
||||||
VertexIndex = vertexIndex;
|
VertexIndex = vertexIndex;
|
||||||
VertexBounds = vertexBounds;
|
VertexBounds = vertexBounds;
|
||||||
|
@ -4,7 +4,7 @@ namespace SpatialCollections
|
|||||||
{
|
{
|
||||||
public enum IntersectionType { Contains, Intersects, Disjoint }
|
public enum IntersectionType { Contains, Intersects, Disjoint }
|
||||||
|
|
||||||
public class BoundingBox2
|
public class Rectangle
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Vector containing the lowest X and Y coordinates contained in the bounding box.
|
/// Vector containing the lowest X and Y coordinates contained in the bounding box.
|
||||||
@ -20,7 +20,7 @@ namespace SpatialCollections
|
|||||||
|
|
||||||
public Vector2 Size => Max - Min;
|
public Vector2 Size => Max - Min;
|
||||||
|
|
||||||
public BoundingBox2(Vector2 min, Vector2 max)
|
public Rectangle(Vector2 min, Vector2 max)
|
||||||
{
|
{
|
||||||
Min = min;
|
Min = min;
|
||||||
Max = max;
|
Max = max;
|
||||||
@ -31,7 +31,7 @@ namespace SpatialCollections
|
|||||||
return Min.X <= position.X && position.X <= Max.X && Min.Y <= position.Y && position.Y <= Max.Y;
|
return Min.X <= position.X && position.X <= Max.X && Min.Y <= position.Y && position.Y <= Max.Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntersectionType Intersects(BoundingBox2 other)
|
public IntersectionType Intersects(Rectangle other)
|
||||||
{
|
{
|
||||||
if (Min.X <= other.Min.X && other.Max.X <= Max.X && Min.Y <= other.Min.Y && other.Max.Y <= Max.Y)
|
if (Min.X <= other.Min.X && other.Max.X <= Max.X && Min.Y <= other.Min.Y && other.Max.Y <= Max.Y)
|
||||||
{
|
{
|
@ -12,7 +12,7 @@ namespace QuadtreeTests
|
|||||||
|
|
||||||
public float BoundingRadius { get; } = 1.0f;
|
public float BoundingRadius { get; } = 1.0f;
|
||||||
|
|
||||||
public BoundingBox2 BoundingBox { get; } = new BoundingBox2(position - Vector2.One, position + Vector2.One);
|
public Rectangle BoundingBox { get; } = new Rectangle(position - Vector2.One, position + Vector2.One);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Vector2> _positions;
|
private List<Vector2> _positions;
|
||||||
@ -58,7 +58,7 @@ namespace QuadtreeTests
|
|||||||
{
|
{
|
||||||
AddObjectsAndAssertCount();
|
AddObjectsAndAssertCount();
|
||||||
List<IWorldObject> result = new();
|
List<IWorldObject> result = new();
|
||||||
_quadtree.Query(new BoundingBox2(new Vector2(3.5f, 3.5f), new Vector2(10.0f, 10.0f)), result);
|
_quadtree.Query(new Rectangle(new Vector2(3.5f, 3.5f), new Vector2(10.0f, 10.0f)), result);
|
||||||
Assert.That(result.Count, Is.EqualTo(2));
|
Assert.That(result.Count, Is.EqualTo(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user