Skip to content

Commit

Permalink
Merge pull request #3 from smartcmd/fix-issue#1
Browse files Browse the repository at this point in the history
fix: Fix missing resetting in AABBTreeNode.resetForReuse() method and solve issue#1
  • Loading branch information
pateman authored Aug 10, 2023
2 parents 515a0a7 + 4719b9a commit 27ecc10
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/java/pl/pateman/dynamicaabbtree/AABBTreeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ void computeAABBWithMargin(float margin) {

void resetForReuse() {
assignChildren(INVALID_NODE_INDEX, INVALID_NODE_INDEX);
setData(null);
aabb.setMin(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
aabb.setMax(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
setParent(INVALID_NODE_INDEX);
setHeight(0);
setData(null);

aabb.setMin(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
aabb.setMax(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
Expand Down
27 changes: 26 additions & 1 deletion src/test/java/pl/pateman/dynamicaabbtree/AABBTreeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;

import static org.junit.Assert.*;
import static pl.pateman.dynamicaabbtree.AABBTreeNode.INVALID_NODE_INDEX;

public class AABBTreeTest
{
Expand Down Expand Up @@ -237,7 +238,7 @@ public void shouldClearTheTree() {
assertFalse(tree.contains(entity2));
assertTrue(tree.getFreeNodes().isEmpty());
assertTrue(tree.getNodes().isEmpty());
assertEquals(AABBTreeNode.INVALID_NODE_INDEX, tree.getRoot());
assertEquals(INVALID_NODE_INDEX, tree.getRoot());
}

@Test
Expand Down Expand Up @@ -367,6 +368,30 @@ public void shouldDetectRayIntersection() {
assertEquals(1L, intersecting.get(0).getID());
}

@Test
public void shouldRestoreNodeToInitialStateOnReuse() {
// Given
TestEntity entity = new TestEntity(1, 0.0f, 0.0f, 1.0f, 1.0f);
AABBTreeNode<TestEntity> node = new AABBTreeNode<>();

// When
node.setHeight(10);
node.setIndex(2);
node.setParent(1);
node.setData(entity);
node.assignChildren(3, 4);

node.resetForReuse();

// Then
assertEquals(INVALID_NODE_INDEX, node.getLeftChild());
assertEquals(INVALID_NODE_INDEX, node.getRightChild());
assertEquals(new AABBf(), node.getAABB());
assertEquals(INVALID_NODE_INDEX, node.getParent());
assertEquals(0, node.getHeight());
assertNull(node.getData());
}

private AABBTree<TestEntity> givenTree() {
return new AABBTree<>();
}
Expand Down

0 comments on commit 27ecc10

Please sign in to comment.