Fix inconsistent private member naming style

This commit is contained in:
Stefan Müller 2025-08-27 17:28:17 +02:00
parent 6523d469cc
commit 0aad311a32

View File

@ -21,31 +21,31 @@ namespace Quadtree
/// 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 m_maxLeafSize; private readonly int _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 m_maxTreeDepth; private readonly int _maxTreeDepth;
/// <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> m_vertices; private readonly List<QuadtreeVertex> _vertices;
/// <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<IWorldObject>> m_items; private readonly List<QuadtreeItem<IWorldObject>> _items;
private BoundingBox2 m_rootBoundingBox; private BoundingBox2 _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.
/// </summary> /// </summary>
private float m_maxObjectRadius; private float _maxObjectRadius;
/// <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.
@ -59,26 +59,26 @@ namespace Quadtree
public Quadtree(int maxLeafSize, int maxTreeDepth) public Quadtree(int maxLeafSize, int maxTreeDepth)
{ {
m_maxLeafSize = maxLeafSize; _maxLeafSize = maxLeafSize;
m_maxTreeDepth = maxTreeDepth; _maxTreeDepth = maxTreeDepth;
m_vertices = new List<QuadtreeVertex>() { new QuadtreeVertex(-1, 0) }; _vertices = new List<QuadtreeVertex>() { new QuadtreeVertex(-1, 0) };
m_items = new List<QuadtreeItem<IWorldObject>>(); _items = new List<QuadtreeItem<IWorldObject>>();
m_rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero); _rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero);
m_maxObjectRadius = 0f; _maxObjectRadius = 0f;
_queryStack = new Stack<QuadtreeQuery>(); _queryStack = new Stack<QuadtreeQuery>();
_vertexStack = new Stack<int>(); _vertexStack = new Stack<int>();
} }
public void Add(IWorldObject obj) public void Add(IWorldObject obj)
{ {
var itemIndex = m_items.Count; var itemIndex = _items.Count;
m_items.Add(new QuadtreeItem<IWorldObject>(obj)); _items.Add(new QuadtreeItem<IWorldObject>(obj));
if (m_maxObjectRadius < obj.BoundingRadius) if (_maxObjectRadius < obj.BoundingRadius)
{ {
m_maxObjectRadius = obj.BoundingRadius; _maxObjectRadius = obj.BoundingRadius;
} }
if (m_vertices.Count == 1 && m_vertices[0].ChildCount < m_maxLeafSize) if (_vertices.Count == 1 && _vertices[0].ChildCount < _maxLeafSize)
{ {
// Before doing any splitting, the initial quadtree root vertex is filled up in order to get a // Before doing any splitting, the initial quadtree root vertex is filled up in order to get a
// reasonable guess for what the world space is. This space can be expanded later if that assumption // reasonable guess for what the world space is. This space can be expanded later if that assumption
@ -88,13 +88,13 @@ namespace Quadtree
else else
{ {
// Expands the root until the new world object fits. // Expands the root until the new world object fits.
while (!m_rootBoundingBox.Contains(obj.Position)) while (!_rootBoundingBox.Contains(obj.Position))
{ {
ExpandRoot(obj.Position); ExpandRoot(obj.Position);
} }
(int leafIndex, int depth, BoundingBox2 bounds) = FindLeaf(0, m_rootBoundingBox, 0, obj.Position); (int leafIndex, int depth, BoundingBox2 bounds) = FindLeaf(0, _rootBoundingBox, 0, obj.Position);
while (m_vertices[leafIndex].ChildCount >= m_maxLeafSize && depth < m_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.
SplitLeaf(leafIndex, bounds); SplitLeaf(leafIndex, bounds);
@ -107,28 +107,28 @@ namespace Quadtree
public bool Remove(IWorldObject obj) public bool Remove(IWorldObject obj)
{ {
// Finds the leaf that should contain the world object. // Finds the leaf that should contain the world object.
(int leafIndex, _, _) = FindLeaf(0, m_rootBoundingBox, 0, obj.Position); (int leafIndex, _, _) = FindLeaf(0, _rootBoundingBox, 0, obj.Position);
// Tries to find the item of the world object. // Tries to find the item of the world object.
int previous = -1; int previous = -1;
int current = m_vertices[leafIndex].FirstChildIndex; int current = _vertices[leafIndex].FirstChildIndex;
while (current != -1) while (current != -1)
{ {
QuadtreeItem<IWorldObject> item = m_items[current]; QuadtreeItem<IWorldObject> item = _items[current];
if (item.WorldObject == obj) if (item.WorldObject == obj)
{ {
// Removes the found item from the leaf vertex. // Removes the found item from the leaf vertex.
QuadtreeVertex leaf = m_vertices[leafIndex]; QuadtreeVertex leaf = _vertices[leafIndex];
leaf.ChildCount--; leaf.ChildCount--;
if (previous != -1) if (previous != -1)
{ {
m_items[previous] = new QuadtreeItem<IWorldObject>(m_items[previous].WorldObject, item.Next); _items[previous] = new QuadtreeItem<IWorldObject>(_items[previous].WorldObject, item.Next);
} }
else else
{ {
leaf.FirstChildIndex = item.Next; leaf.FirstChildIndex = item.Next;
} }
m_vertices[leafIndex] = leaf; _vertices[leafIndex] = leaf;
// Removes the found item from the item list. // Removes the found item from the item list.
RemoveFromItems(current); RemoveFromItems(current);
@ -146,15 +146,15 @@ namespace Quadtree
public void Query(BoundingBox2 box, List<IWorldObject> resultList) public void Query(BoundingBox2 box, List<IWorldObject> resultList)
{ {
BoundingBox2 inflatedBox = box; BoundingBox2 inflatedBox = box;
inflatedBox.Expand(m_maxObjectRadius); inflatedBox.Expand(_maxObjectRadius);
QueryProcessChildVertex(inflatedBox, box, 0, m_rootBoundingBox, resultList, _queryStack); QueryProcessChildVertex(inflatedBox, box, 0, _rootBoundingBox, resultList, _queryStack);
while (_queryStack.Count > 0) while (_queryStack.Count > 0)
{ {
QuadtreeQuery query = _queryStack.Pop(); QuadtreeQuery query = _queryStack.Pop();
int childIndex = m_vertices[query.VertexIndex].FirstChildIndex; int childIndex = _vertices[query.VertexIndex].FirstChildIndex;
BoundingBox2 halfBounds = new BoundingBox2(query.VertexBounds.Center, query.VertexBounds.Max); BoundingBox2 halfBounds = new BoundingBox2(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);
@ -172,11 +172,11 @@ namespace Quadtree
public void Clear() public void Clear()
{ {
m_vertices.Clear(); _vertices.Clear();
m_vertices.Add(new QuadtreeVertex(-1, 0)); _vertices.Add(new QuadtreeVertex(-1, 0));
m_items.Clear(); _items.Clear();
m_rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero); _rootBoundingBox = new BoundingBox2(Vector2.Zero, Vector2.Zero);
m_maxObjectRadius = 0f; _maxObjectRadius = 0f;
} }
/// <summary> /// <summary>
@ -186,92 +186,92 @@ namespace Quadtree
private void ExpandRoot(Vector2 position) private void ExpandRoot(Vector2 position)
{ {
// Finds the new index for the old root vertex and expands the bounds. // Finds the new index for the old root vertex and expands the bounds.
QuadtreeVertex newRoot = new QuadtreeVertex(m_vertices.Count, -1); QuadtreeVertex newRoot = new QuadtreeVertex(_vertices.Count, -1);
int oldRootIndex = m_vertices.Count; int oldRootIndex = _vertices.Count;
if (position.X > m_rootBoundingBox.Max.X) if (position.X > _rootBoundingBox.Max.X)
{ {
oldRootIndex++; oldRootIndex++;
m_rootBoundingBox.Max.X += m_rootBoundingBox.Max.X - m_rootBoundingBox.Min.X; _rootBoundingBox.Max.X += _rootBoundingBox.Max.X - _rootBoundingBox.Min.X;
} }
else else
{ {
m_rootBoundingBox.Min.X += m_rootBoundingBox.Min.X - m_rootBoundingBox.Max.X; _rootBoundingBox.Min.X += _rootBoundingBox.Min.X - _rootBoundingBox.Max.X;
} }
if (position.Y > m_rootBoundingBox.Max.Y) if (position.Y > _rootBoundingBox.Max.Y)
{ {
oldRootIndex += 2; oldRootIndex += 2;
m_rootBoundingBox.Max.Y += m_rootBoundingBox.Max.Y - m_rootBoundingBox.Min.Y; _rootBoundingBox.Max.Y += _rootBoundingBox.Max.Y - _rootBoundingBox.Min.Y;
} }
else else
{ {
m_rootBoundingBox.Min.Y += m_rootBoundingBox.Min.Y - m_rootBoundingBox.Max.Y; _rootBoundingBox.Min.Y += _rootBoundingBox.Min.Y - _rootBoundingBox.Max.Y;
} }
// Adds the new leaves and updates the old and new root. // Adds the new leaves and updates the old and new root.
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
if (oldRootIndex == m_vertices.Count) if (oldRootIndex == _vertices.Count)
{ {
m_vertices.Add(m_vertices[0]); _vertices.Add(_vertices[0]);
} }
else else
{ {
m_vertices.Add(new QuadtreeVertex(-1, 0)); _vertices.Add(new QuadtreeVertex(-1, 0));
} }
} }
m_vertices[0] = newRoot; _vertices[0] = newRoot;
} }
private void AddToRootLeaf(int itemIndex) private void AddToRootLeaf(int itemIndex)
{ {
Vector2 position = m_items[itemIndex].WorldObject.Position; Vector2 position = _items[itemIndex].WorldObject.Position;
if (m_vertices[0].ChildCount == 0) if (_vertices[0].ChildCount == 0)
{ {
m_vertices[0] = new QuadtreeVertex(itemIndex, 1); _vertices[0] = new QuadtreeVertex(itemIndex, 1);
m_rootBoundingBox.Min = (m_rootBoundingBox.Max = position); _rootBoundingBox.Min = (_rootBoundingBox.Max = position);
} }
else else
{ {
var item = m_items[itemIndex]; var item = _items[itemIndex];
item.Next = m_vertices[0].FirstChildIndex; item.Next = _vertices[0].FirstChildIndex;
m_items[itemIndex] = item; _items[itemIndex] = item;
m_vertices[0] = new QuadtreeVertex(itemIndex, m_vertices[0].ChildCount + 1); _vertices[0] = new QuadtreeVertex(itemIndex, _vertices[0].ChildCount + 1);
IncludeInBoundingBox(ref m_rootBoundingBox, position); IncludeInBoundingBox(ref _rootBoundingBox, position);
} }
} }
private void AddToLeaf(int leafIndex, int itemIndex) private void AddToLeaf(int leafIndex, int itemIndex)
{ {
int next; int next;
if (m_vertices[leafIndex].ChildCount == 0) if (_vertices[leafIndex].ChildCount == 0)
{ {
next = -1; next = -1;
m_vertices[leafIndex] = new QuadtreeVertex(itemIndex, 1); _vertices[leafIndex] = new QuadtreeVertex(itemIndex, 1);
} }
else else
{ {
next = m_vertices[leafIndex].FirstChildIndex; next = _vertices[leafIndex].FirstChildIndex;
m_vertices[leafIndex] = new QuadtreeVertex(itemIndex, m_vertices[leafIndex].ChildCount + 1); _vertices[leafIndex] = new QuadtreeVertex(itemIndex, _vertices[leafIndex].ChildCount + 1);
} }
m_items[itemIndex] = new QuadtreeItem<IWorldObject>(m_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, BoundingBox2 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 = m_vertices[leafIndex]; var oldLeaf = _vertices[leafIndex];
m_vertices[leafIndex] = new QuadtreeVertex(m_vertices.Count, -1); _vertices[leafIndex] = new QuadtreeVertex(_vertices.Count, -1);
m_vertices.Add(new QuadtreeVertex(-1, 0)); _vertices.Add(new QuadtreeVertex(-1, 0));
m_vertices.Add(new QuadtreeVertex(-1, 0)); _vertices.Add(new QuadtreeVertex(-1, 0));
m_vertices.Add(new QuadtreeVertex(-1, 0)); _vertices.Add(new QuadtreeVertex(-1, 0));
m_vertices.Add(new QuadtreeVertex(-1, 0)); _vertices.Add(new QuadtreeVertex(-1, 0));
// Distributes the existing items over the new leaves. // Distributes the existing items over the new leaves.
var center = leafBounds.Center; var center = leafBounds.Center;
int next = oldLeaf.FirstChildIndex; int next = oldLeaf.FirstChildIndex;
while (next != -1) while (next != -1)
{ {
var item = m_items[next]; var item = _items[next];
int newLeafIndex = FindNextVertex(leafIndex, center, item.WorldObject.Position); int newLeafIndex = FindNextVertex(leafIndex, center, item.WorldObject.Position);
AddToLeaf(newLeafIndex, next); AddToLeaf(newLeafIndex, next);
next = item.Next; next = item.Next;
@ -280,7 +280,7 @@ namespace Quadtree
private int FindNextVertex(int vertexIndex, Vector2 boundsCenter, Vector2 position) private int FindNextVertex(int vertexIndex, Vector2 boundsCenter, Vector2 position)
{ {
vertexIndex = m_vertices[vertexIndex].FirstChildIndex; vertexIndex = _vertices[vertexIndex].FirstChildIndex;
if (position.X < boundsCenter.X) if (position.X < boundsCenter.X)
{ {
vertexIndex++; vertexIndex++;
@ -295,11 +295,11 @@ namespace Quadtree
private (int leafIndex, int depth, BoundingBox2 bounds) FindLeaf(int vertexIndex, BoundingBox2 bounds, private (int leafIndex, int depth, BoundingBox2 bounds) FindLeaf(int vertexIndex, BoundingBox2 bounds,
int depth, Vector2 position) int depth, Vector2 position)
{ {
while (m_vertices[vertexIndex].ChildCount == -1) while (_vertices[vertexIndex].ChildCount == -1)
{ {
depth++; depth++;
var center = bounds.Center; var center = bounds.Center;
vertexIndex = m_vertices[vertexIndex].FirstChildIndex; vertexIndex = _vertices[vertexIndex].FirstChildIndex;
if (position.X < center.X) if (position.X < center.X)
{ {
vertexIndex++; vertexIndex++;
@ -328,7 +328,7 @@ namespace Quadtree
switch (inflatedBox.Intersects(vertexBounds)) switch (inflatedBox.Intersects(vertexBounds))
{ {
case IntersectionType.Contains: case IntersectionType.Contains:
if (m_vertices[vertexIndex].ChildCount == -1) if (_vertices[vertexIndex].ChildCount == -1)
{ {
// Found contained vertex, appends items of all child vertices. // Found contained vertex, appends items of all child vertices.
QueryAppendContainedItems(vertexIndex, box, resultList); QueryAppendContainedItems(vertexIndex, box, resultList);
@ -340,7 +340,7 @@ namespace Quadtree
} }
break; break;
case IntersectionType.Intersects: case IntersectionType.Intersects:
if (m_vertices[vertexIndex].ChildCount == -1) if (_vertices[vertexIndex].ChildCount == -1)
{ {
// Branches the query. // Branches the query.
stack.Push(new QuadtreeQuery(vertexIndex, vertexBounds)); stack.Push(new QuadtreeQuery(vertexIndex, vertexBounds));
@ -361,10 +361,10 @@ namespace Quadtree
while (_vertexStack.Count > 0) while (_vertexStack.Count > 0)
{ {
int current = _vertexStack.Pop(); int current = _vertexStack.Pop();
if (m_vertices[current].ChildCount == -1) if (_vertices[current].ChildCount == -1)
{ {
// Branches into all vertices until leaves are found. // Branches into all vertices until leaves are found.
int childIndex = m_vertices[current].FirstChildIndex; int childIndex = _vertices[current].FirstChildIndex;
_vertexStack.Push(childIndex++); _vertexStack.Push(childIndex++);
_vertexStack.Push(childIndex++); _vertexStack.Push(childIndex++);
_vertexStack.Push(childIndex++); _vertexStack.Push(childIndex++);
@ -380,41 +380,41 @@ namespace Quadtree
private void QueryAppendContainedLeafItems(int vertexIndex, BoundingBox2 box, List<IWorldObject> resultList) private void QueryAppendContainedLeafItems(int vertexIndex, BoundingBox2 box, List<IWorldObject> resultList)
{ {
int index = m_vertices[vertexIndex].FirstChildIndex; int index = _vertices[vertexIndex].FirstChildIndex;
while (index != -1) while (index != -1)
{ {
IWorldObject obj = m_items[index].WorldObject; IWorldObject obj = _items[index].WorldObject;
if (box.Intersects(obj.BoundingBox) != IntersectionType.Disjoint) if (box.Intersects(obj.BoundingBox) != IntersectionType.Disjoint)
{ {
resultList.Add(obj); resultList.Add(obj);
} }
index = m_items[index].Next; index = _items[index].Next;
} }
} }
/// <summary> /// <summary>
/// Removes the found item from the item list. Since the list is being reordered for fast removal, the reference /// Removes the found item from the item list. Since the list is being reordered for fast removal, the reference
/// to the relocated item has to be updated because its list index changes from "m_items.Count - 1" to "index". /// to the relocated item has to be updated because its list index changes from "_items.Count - 1" to "index".
/// </summary> /// </summary>
/// <param name="index">Index of the item to be removed.</param> /// <param name="index">Index of the item to be removed.</param>
private void RemoveFromItems(int index) private void RemoveFromItems(int index)
{ {
m_items.RemoveUnorderedAt(index); _items.RemoveUnorderedAt(index);
if (index != m_items.Count) if (index != _items.Count)
{ {
(int leafIndex, _, _) = FindLeaf(0, m_rootBoundingBox, 0, m_items[index].WorldObject.Position); (int leafIndex, _, _) = FindLeaf(0, _rootBoundingBox, 0, _items[index].WorldObject.Position);
int current = m_vertices[leafIndex].FirstChildIndex; int current = _vertices[leafIndex].FirstChildIndex;
if (current == m_items.Count) if (current == _items.Count)
{ {
m_vertices[leafIndex] = new QuadtreeVertex(index, m_vertices[leafIndex].ChildCount); _vertices[leafIndex] = new QuadtreeVertex(index, _vertices[leafIndex].ChildCount);
} }
else else
{ {
while (m_items[current].Next != m_items.Count) while (_items[current].Next != _items.Count)
{ {
current = m_items[current].Next; current = _items[current].Next;
} }
m_items[current] = new QuadtreeItem<IWorldObject>(m_items[current].WorldObject, index); _items[current] = new QuadtreeItem<IWorldObject>(_items[current].WorldObject, index);
} }
} }
} }