From 3bea3f5a9bd86c231863ec9dbecdc5f545de6344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Thu, 25 Sep 2025 19:11:18 +0200 Subject: [PATCH] Rename class BoundingBox2 to Rectangle --- Quadtree/IWorldObject.cs | 2 +- Quadtree/Quadtree.cs | 26 +++++++++++----------- Quadtree/QuadtreeQuery.cs | 4 ++-- Quadtree/{BoundingBox2.cs => Rectangle.cs} | 6 ++--- QuadtreeTests/Tests.cs | 4 ++-- 5 files changed, 21 insertions(+), 21 deletions(-) rename Quadtree/{BoundingBox2.cs => Rectangle.cs} (93%) diff --git a/Quadtree/IWorldObject.cs b/Quadtree/IWorldObject.cs index 2e7d911..cd31f26 100644 --- a/Quadtree/IWorldObject.cs +++ b/Quadtree/IWorldObject.cs @@ -8,6 +8,6 @@ namespace SpatialCollections float BoundingRadius { get; } - BoundingBox2 BoundingBox { get; } + Rectangle BoundingBox { get; } } } diff --git a/Quadtree/Quadtree.cs b/Quadtree/Quadtree.cs index 7347bb4..fdabf0c 100644 --- a/Quadtree/Quadtree.cs +++ b/Quadtree/Quadtree.cs @@ -37,7 +37,7 @@ namespace SpatialCollections /// private readonly List> _items; - private BoundingBox2 _rootBoundingBox; + private Rectangle _rootBoundingBox; /// /// The maximum bounding radius of all stored objects, used to inflate the bounding box for queries. @@ -60,7 +60,7 @@ namespace SpatialCollections _maxTreeDepth = maxTreeDepth; _vertices = new List() { new(-1, 0) }; _items = new List>(); - _rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero); + _rootBoundingBox = new Rectangle(Vector2.Zero, Vector2.Zero); _maxObjectRadius = 0f; _queryStack = new Stack(); _vertexStack = new Stack(); @@ -92,7 +92,7 @@ namespace SpatialCollections 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) { // Splits the vertex and decends into one of the new leaves. @@ -142,9 +142,9 @@ namespace SpatialCollections return false; } - public void Query(BoundingBox2 box, List resultList) + public void Query(Rectangle box, List resultList) { - BoundingBox2 inflatedBox = box; + Rectangle inflatedBox = box; inflatedBox.Expand(_maxObjectRadius); QueryProcessChildVertex(inflatedBox, box, 0, _rootBoundingBox, resultList, _queryStack); @@ -154,7 +154,7 @@ namespace SpatialCollections QuadtreeQuery query = _queryStack.Pop(); 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; QueryProcessChildVertex(inflatedBox, box, childIndex++, halfBounds, resultList, _queryStack); @@ -174,7 +174,7 @@ namespace SpatialCollections _vertices.Clear(); _vertices.Add(new QuadtreeVertex(-1, 0)); _items.Clear(); - _rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero); + _rootBoundingBox = new Rectangle(Vector2.Zero, Vector2.Zero); _maxObjectRadius = 0f; } @@ -255,7 +255,7 @@ namespace SpatialCollections _items[itemIndex] = new QuadtreeItem(_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. var oldLeaf = _vertices[leafIndex]; @@ -291,7 +291,7 @@ namespace SpatialCollections 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) { while (_vertices[vertexIndex].ChildCount == -1) @@ -321,8 +321,8 @@ namespace SpatialCollections return (vertexIndex, depth, bounds); } - private void QueryProcessChildVertex(BoundingBox2 inflatedBox, BoundingBox2 box, int vertexIndex, - BoundingBox2 vertexBounds, List resultList, Stack stack) + private void QueryProcessChildVertex(Rectangle inflatedBox, Rectangle box, int vertexIndex, + Rectangle vertexBounds, List resultList, Stack stack) { switch (inflatedBox.Intersects(vertexBounds)) { @@ -353,7 +353,7 @@ namespace SpatialCollections } } - private void QueryAppendContainedItems(int vertexIndex, BoundingBox2 box, List resultList) + private void QueryAppendContainedItems(int vertexIndex, Rectangle box, List resultList) { _vertexStack.Push(vertexIndex); @@ -377,7 +377,7 @@ namespace SpatialCollections } } - private void QueryAppendContainedLeafItems(int vertexIndex, BoundingBox2 box, List resultList) + private void QueryAppendContainedLeafItems(int vertexIndex, Rectangle box, List resultList) { int index = _vertices[vertexIndex].FirstChildIndex; while (index != -1) diff --git a/Quadtree/QuadtreeQuery.cs b/Quadtree/QuadtreeQuery.cs index 64b50c3..b682092 100644 --- a/Quadtree/QuadtreeQuery.cs +++ b/Quadtree/QuadtreeQuery.cs @@ -4,9 +4,9 @@ { public int VertexIndex; - public BoundingBox2 VertexBounds; + public Rectangle VertexBounds; - public QuadtreeQuery(int vertexIndex, BoundingBox2 vertexBounds) + public QuadtreeQuery(int vertexIndex, Rectangle vertexBounds) { VertexIndex = vertexIndex; VertexBounds = vertexBounds; diff --git a/Quadtree/BoundingBox2.cs b/Quadtree/Rectangle.cs similarity index 93% rename from Quadtree/BoundingBox2.cs rename to Quadtree/Rectangle.cs index c24edd7..e6c1020 100644 --- a/Quadtree/BoundingBox2.cs +++ b/Quadtree/Rectangle.cs @@ -4,7 +4,7 @@ namespace SpatialCollections { public enum IntersectionType { Contains, Intersects, Disjoint } - public class BoundingBox2 + public class Rectangle { /// /// 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 BoundingBox2(Vector2 min, Vector2 max) + public Rectangle(Vector2 min, Vector2 max) { Min = min; 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; } - 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) { diff --git a/QuadtreeTests/Tests.cs b/QuadtreeTests/Tests.cs index 49b3734..03f226c 100644 --- a/QuadtreeTests/Tests.cs +++ b/QuadtreeTests/Tests.cs @@ -12,7 +12,7 @@ namespace QuadtreeTests 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 _positions; @@ -58,7 +58,7 @@ namespace QuadtreeTests { AddObjectsAndAssertCount(); List 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)); }