Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions BepuPhysics/Trees/Tree_RayCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public unsafe static bool Intersects(Vector3 min, Vector3 max, TreeRay* ray, out
internal readonly unsafe void RayCast<TLeafTester>(int nodeIndex, TreeRay* treeRay, RayData* rayData, Buffer<int> stack, BufferPool pool, ref TLeafTester leafTester) where TLeafTester : IRayLeafTester
{
Debug.Assert((nodeIndex >= 0 && nodeIndex < NodeCount) || (Encode(nodeIndex) >= 0 && Encode(nodeIndex) < LeafCount));
Debug.Assert(LeafCount >= 2, "This implementation assumes all nodes are filled.");
Debug.Assert(LeafCount >= 2 || nodeIndex < 0, "The node traversal assumes all nodes are filled; a single-leaf tree can only be entered through its encoded leaf index.");

int stackEnd = 0;
while (true)
Expand Down Expand Up @@ -55,14 +55,14 @@ internal readonly unsafe void RayCast<TLeafTester>(int nodeIndex, TreeRay* treeR
//Visit the earlier AABB intersection first.
if (stackEnd == stack.Length)
{
if (stack.Length == TraversalStackCapacity)
{
// First allocation is on the stack.
pool.TakeAtLeast<int>(TraversalStackCapacity * 2, out var newStack);
stack.CopyTo(0, newStack, 0, TraversalStackCapacity);
stack = newStack;
if (stack.Length == TraversalStackCapacity)
{
// First allocation is on the stack.
pool.TakeAtLeast<int>(TraversalStackCapacity * 2, out var newStack);
stack.CopyTo(0, newStack, 0, TraversalStackCapacity);
stack = newStack;
}
else
else
pool.Resize(ref stack, stackEnd * 2, stackEnd);
}
if (tA < tB)
Expand Down
16 changes: 8 additions & 8 deletions BepuPhysics/Trees/Tree_Sweep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ partial struct Tree
readonly unsafe void Sweep<TLeafTester>(int nodeIndex, Vector3 expansion, Vector3 origin, Vector3 direction, TreeRay* treeRay, Buffer<int> stack, BufferPool pool, ref TLeafTester leafTester) where TLeafTester : ISweepLeafTester
{
Debug.Assert((nodeIndex >= 0 && nodeIndex < NodeCount) || (Encode(nodeIndex) >= 0 && Encode(nodeIndex) < LeafCount));
Debug.Assert(LeafCount >= 2, "This implementation assumes all nodes are filled.");
Debug.Assert(LeafCount >= 2 || nodeIndex < 0, "The node traversal assumes all nodes are filled; a single-leaf tree can only be entered through its encoded leaf index.");

int stackEnd = 0;
while (true)
Expand Down Expand Up @@ -47,14 +47,14 @@ readonly unsafe void Sweep<TLeafTester>(int nodeIndex, Vector3 expansion, Vector
//Visit the earlier AABB intersection first.
if (stackEnd == stack.Length)
{
if (stack.Length == TraversalStackCapacity)
{
// First allocation is on the stack.
pool.TakeAtLeast<int>(TraversalStackCapacity * 2, out var newStack);
stack.CopyTo(0, newStack, 0, TraversalStackCapacity);
stack = newStack;
if (stack.Length == TraversalStackCapacity)
{
// First allocation is on the stack.
pool.TakeAtLeast<int>(TraversalStackCapacity * 2, out var newStack);
stack.CopyTo(0, newStack, 0, TraversalStackCapacity);
stack = newStack;
}
else
else
pool.Resize(ref stack, stackEnd * 2, stackEnd);
}
if (tA < tB)
Expand Down
64 changes: 32 additions & 32 deletions BepuPhysics/Trees/Tree_VolumeQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ partial struct Tree
unsafe readonly void GetOverlaps<TEnumerator>(int nodeIndex, BoundingBox boundingBox, Buffer<int> stack, BufferPool pool, ref TEnumerator leafEnumerator) where TEnumerator : IBreakableForEach<int>
{
Debug.Assert((nodeIndex >= 0 && nodeIndex < NodeCount) || (Encode(nodeIndex) >= 0 && Encode(nodeIndex) < LeafCount));
Debug.Assert(LeafCount >= 2, "This implementation assumes all nodes are filled.");
Debug.Assert(LeafCount >= 2 || nodeIndex < 0, "The node traversal assumes all nodes are filled; a single-leaf tree can only be entered through its encoded leaf index.");

int stackEnd = 0;
while (true)
Expand All @@ -38,17 +38,17 @@ unsafe readonly void GetOverlaps<TEnumerator>(int nodeIndex, BoundingBox boundin
nodeIndex = node.A.Index;
if (bIntersected)
{
if (stackEnd == stack.Length)
{
if (stack.Length == TraversalStackCapacity)
{
// First allocation is on the stack.
pool.TakeAtLeast<int>(TraversalStackCapacity * 2, out var newStack);
stack.CopyTo(0, newStack, 0, TraversalStackCapacity);
stack = newStack;
if (stackEnd == stack.Length)
{
if (stack.Length == TraversalStackCapacity)
{
// First allocation is on the stack.
pool.TakeAtLeast<int>(TraversalStackCapacity * 2, out var newStack);
stack.CopyTo(0, newStack, 0, TraversalStackCapacity);
stack = newStack;
}
else
pool.Resize(ref stack, stackEnd * 2, stackEnd);
else
pool.Resize(ref stack, stackEnd * 2, stackEnd);
}
stack[stackEnd++] = node.B.Index;

Expand All @@ -67,22 +67,22 @@ unsafe readonly void GetOverlaps<TEnumerator>(int nodeIndex, BoundingBox boundin
}
}
}
if (stack.Length > TraversalStackCapacity)
{
// We rented a larger stack at some point. Return it.
pool.Return(ref stack);
if (stack.Length > TraversalStackCapacity)
{
// We rented a larger stack at some point. Return it.
pool.Return(ref stack);
}
}

/// <summary>
/// Finds and processes all leaves with bounding boxes that overlap the specified axis-aligned bounding box. The <paramref name="leafEnumerator"/> is invoked
/// for each overlapping element.
/// </summary>
/// <typeparam name="TEnumerator">The type of the <see cref="IBreakableForEach{T}"/> enumerator used to process the overlapping elements.</typeparam>
/// <param name="boundingBox">Query to test against the bounding volume hierarchy.</param>
/// <summary>
/// Finds and processes all leaves with bounding boxes that overlap the specified axis-aligned bounding box. The <paramref name="leafEnumerator"/> is invoked
/// for each overlapping element.
/// </summary>
/// <typeparam name="TEnumerator">The type of the <see cref="IBreakableForEach{T}"/> enumerator used to process the overlapping elements.</typeparam>
/// <param name="boundingBox">Query to test against the bounding volume hierarchy.</param>
/// <param name="pool">The buffer pool used for temporary allocations during the operation. Only used if the tree is pathologically deep; stack memory is used preferentially.</param>
/// <param name="leafEnumerator">A reference to the enumerator that processes the indices of overlapping elements. The enumerator can
/// terminate early by returning <see langword="false"/> from its iteration.</param>
/// <param name="leafEnumerator">A reference to the enumerator that processes the indices of overlapping elements. The enumerator can
/// terminate early by returning <see langword="false"/> from its iteration.</param>
public readonly unsafe void GetOverlaps<TEnumerator>(BoundingBox boundingBox, BufferPool pool, ref TEnumerator leafEnumerator) where TEnumerator : IBreakableForEach<int>
{
if (LeafCount > 1)
Expand All @@ -102,16 +102,16 @@ public readonly unsafe void GetOverlaps<TEnumerator>(BoundingBox boundingBox, Bu
//If the leaf count is zero, then there's nothing to test against.
}

/// <summary>
/// Finds and processes all leaves with bounding boxes that overlap the specified axis-aligned bounding box. The <paramref name="leafEnumerator"/> is invoked
/// for each overlapping element.
/// </summary>
/// <typeparam name="TEnumerator">The type of the <see cref="IBreakableForEach{T}"/> enumerator used to process the overlapping elements.</typeparam>
/// <param name="min">The minimum corner of the axis-aligned bounding box.</param>
/// <param name="max">The maximum corner of the axis-aligned bounding box.</param>
/// <summary>
/// Finds and processes all leaves with bounding boxes that overlap the specified axis-aligned bounding box. The <paramref name="leafEnumerator"/> is invoked
/// for each overlapping element.
/// </summary>
/// <typeparam name="TEnumerator">The type of the <see cref="IBreakableForEach{T}"/> enumerator used to process the overlapping elements.</typeparam>
/// <param name="min">The minimum corner of the axis-aligned bounding box.</param>
/// <param name="max">The maximum corner of the axis-aligned bounding box.</param>
/// <param name="pool">The buffer pool used for temporary allocations during the operation. Only used if the tree is pathologically deep; stack memory is used preferentially.</param>
/// <param name="leafEnumerator">A reference to the enumerator that processes the indices of overlapping elements. The enumerator can
/// terminate early by returning <see langword="false"/> from its iteration.</param>
/// <param name="leafEnumerator">A reference to the enumerator that processes the indices of overlapping elements. The enumerator can
/// terminate early by returning <see langword="false"/> from its iteration.</param>
public readonly void GetOverlaps<TEnumerator>(Vector3 min, Vector3 max, BufferPool pool, ref TEnumerator leafEnumerator) where TEnumerator : IBreakableForEach<int>
{
GetOverlaps(new BoundingBox(min, max), pool, ref leafEnumerator);
Expand Down
Loading