Fix QuadTree adjacent position issues

This commit is contained in:
James Seibel
2023-03-23 07:13:09 -05:00
parent 515ebb8c99
commit 57fec0f805
5 changed files with 430 additions and 124 deletions
+119 -3
View File
@@ -21,6 +21,7 @@ package tests;
import com.seibel.lod.core.logging.DhLoggerBuilder;
import com.seibel.lod.core.pos.DhBlockPos2D;
import com.seibel.lod.core.pos.DhLodPos;
import com.seibel.lod.core.pos.DhSectionPos;
import com.seibel.lod.core.util.BitShiftUtil;
import com.seibel.lod.core.util.LodUtil;
@@ -36,7 +37,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public class DhSectionPosTest
{
@Test
public void SectionPosTest()
public void ContainsPosTest()
{
DhSectionPos root = new DhSectionPos((byte)10, 0, 0);
DhSectionPos child = new DhSectionPos((byte)9, 1, 1);
@@ -46,10 +47,125 @@ public class DhSectionPosTest
root = new DhSectionPos((byte)10, 1, 0);
// out of bounds
child = new DhSectionPos((byte)9, 0, 0);
Assert.assertFalse("position should be out of bounds", root.contains(child));
child = new DhSectionPos((byte)9, 1, 1);
Assert.assertFalse("section pos contains fail", root.contains(child));
Assert.assertFalse("position should be out of bounds", root.contains(child));
// in bounds
child = new DhSectionPos((byte)9, 2, 0);
Assert.assertTrue("position should be in bounds", root.contains(child));
child = new DhSectionPos((byte)9, 3, 1);
Assert.assertTrue("position should be in bounds", root.contains(child));
// out of bounds
child = new DhSectionPos((byte)9, 2, 2);
Assert.assertTrue("section pos contains fail", root.contains(child));
Assert.assertFalse("position should be out of bounds", root.contains(child));
child = new DhSectionPos((byte)9, 3, 3);
Assert.assertFalse("position should be out of bounds", root.contains(child));
child = new DhSectionPos((byte)9, 4, 4);
Assert.assertFalse("position should be out of bounds", root.contains(child));
child = new DhSectionPos((byte)9, 5, 5);
Assert.assertFalse("position should be out of bounds", root.contains(child));
}
@Test
public void ContainsAdjacentPosTest()
{
// neither should contain the other, they are single blocks that are next to each other
DhSectionPos left = new DhSectionPos((byte)0, 4606, 0);
DhSectionPos right = new DhSectionPos((byte)0, 4607, 0);
Assert.assertFalse(left.contains(right));
Assert.assertFalse(right.contains(left));
// 512 block wide sections that are adjacent, but not overlapping
left = new DhSectionPos((byte)9, 0, 0);
right = new DhSectionPos((byte)9, 1, 0);
Assert.assertFalse(left.contains(right));
Assert.assertFalse(right.contains(left));
}
@Test
public void ParentPosTest()
{
DhSectionPos leaf = new DhSectionPos((byte)0, 0, 0);
DhSectionPos convert = leaf.convertToDetailLevel((byte)1);
DhSectionPos parent = leaf.getParentPos();
Assert.assertEquals("get parent at 0,0 fail", convert, parent);
leaf = new DhSectionPos((byte)0, 1, 1);
convert = leaf.convertToDetailLevel((byte)1);
parent = leaf.getParentPos();
Assert.assertEquals("get parent at 1,1 fail", convert, parent);
leaf = new DhSectionPos((byte)1, 2, 2);
convert = leaf.convertToDetailLevel((byte)2);
parent = leaf.getParentPos();
Assert.assertEquals("parent upscale fail", convert, parent);
convert = leaf.convertToDetailLevel((byte)0);
DhSectionPos childIndex = leaf.getChildByIndex(0);
Assert.assertEquals("child detail fail", convert, childIndex);
}
@Test
public void ChildPosTest()
{
DhSectionPos node = new DhSectionPos((byte)1, 2302, 0);
DhSectionPos nw = node.getChildByIndex(0);
DhSectionPos sw = node.getChildByIndex(1);
DhSectionPos ne = node.getChildByIndex(2);
DhSectionPos se = node.getChildByIndex(3);
// confirm no children have the same values
Assert.assertNotEquals(nw, sw);
Assert.assertNotEquals(sw, ne);
Assert.assertNotEquals(ne, se);
// confirm each child has the correct value
Assert.assertEquals(nw, new DhSectionPos((byte)0, 4604, 0));
Assert.assertEquals(sw, new DhSectionPos((byte)0, 4605, 0));
Assert.assertEquals(ne, new DhSectionPos((byte)0, 4604, 1));
Assert.assertEquals(se, new DhSectionPos((byte)0, 4605, 1));
}
@Test
public void GetCenterTest()
{
DhSectionPos node = new DhSectionPos((byte)1, 2303, 0);
DhLodPos centerNode = node.getCenter();
DhLodPos expectedCenterNode = new DhLodPos((byte)0, 4606,0);
Assert.assertEquals("", expectedCenterNode, centerNode);
node = new DhSectionPos((byte)10, 0, 0); // 1024 blocks wide
centerNode = node.getCenter();
expectedCenterNode = new DhLodPos((byte)0, 1024/2,1024/2);
Assert.assertEquals("", expectedCenterNode, centerNode);
}
@Test
public void GetCenter2Test()
{
DhSectionPos parentNode = new DhSectionPos((byte)2, 1151, 0); // width 4 blocks
DhSectionPos inputPos = new DhSectionPos((byte)0, 4606, 0); // width 1 block
Assert.assertTrue(parentNode.contains(inputPos));
DhLodPos parentCenter = parentNode.getCenter();
DhLodPos inputCenter = inputPos.getCenter();
Assert.assertEquals(new DhLodPos((byte)0, 4606, 2), parentCenter);
Assert.assertEquals(new DhLodPos((byte)0, 4606, 0), inputCenter);
}
+174 -28
View File
@@ -37,10 +37,17 @@ public class QuadTreeTest
{
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
private static final DhBlockPos2D TREE_CENTER_POS = new DhBlockPos2D(BitShiftUtil.powerOfTwo(10)/2, BitShiftUtil.powerOfTwo(10)/2);
private static final int ROOT_NODE_WIDTH_IN_BLOCKS = BitShiftUtil.powerOfTwo(10);
/** needs to be an odd number to function correctly */
private static final int BASIC_TREE_WIDTH_IN_ROOT_NODES = 9;
private static final int BASIC_TREE_WIDTH_IN_BLOCKS = ROOT_NODE_WIDTH_IN_BLOCKS * BASIC_TREE_WIDTH_IN_ROOT_NODES;
private static final int BASIC_TREE_INPUT_WIDTH_IN_ROOT_NODES = 9;
private static final int BASIC_TREE_WIDTH_IN_BLOCKS = ROOT_NODE_WIDTH_IN_BLOCKS * BASIC_TREE_INPUT_WIDTH_IN_ROOT_NODES;
/** the tree should be slightly larger to account for offset centers */
private static final int BASIC_TREE_ACTUAL_WIDTH_IN_ROOT_NODES = BASIC_TREE_INPUT_WIDTH_IN_ROOT_NODES + 2;
private static final int MINIMUM_TREE_WIDTH_IN_BLOCKS = 32;
static
{
@@ -52,8 +59,8 @@ public class QuadTreeTest
@Test
public void BasicPositiveQuadTreeTest()
{
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0));
Assert.assertEquals("Incorrect basic tree width", BASIC_TREE_WIDTH_IN_ROOT_NODES, tree.ringListWidth());
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
Assert.assertEquals("Incorrect basic tree width", BASIC_TREE_ACTUAL_WIDTH_IN_ROOT_NODES, tree.ringListWidth());
// root node //
@@ -87,7 +94,7 @@ public class QuadTreeTest
@Test
public void BasicNegativeQuadTreeTest()
{
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0));
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
// root node //
@@ -122,21 +129,52 @@ public class QuadTreeTest
@Test
public void OutOfBoundsQuadTreeTest()
{
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0));
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0,0));
Assert.assertEquals("tree diameter incorrect", BASIC_TREE_WIDTH_IN_BLOCKS, tree.diameterInBlocks());
// wrong detail level on purpose, if the detail level was 0 (block) this should work
DhSectionPos outOfBoundsPos = new DhSectionPos(DhSectionPos.SECTION_BLOCK_DETAIL_LEVEL, ROOT_NODE_WIDTH_IN_BLOCKS, 0);
testSet(tree, outOfBoundsPos, 2, IndexOutOfBoundsException.class);
testSet(tree, outOfBoundsPos, -1, IndexOutOfBoundsException.class);
Assert.assertEquals("incorrect leaf node count", 0, tree.leafNodeCount());
// out of bounds //
outOfBoundsPos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (BASIC_TREE_WIDTH_IN_BLOCKS/2) + 1, 0);
testSet(tree, outOfBoundsPos, -1, IndexOutOfBoundsException.class);
Assert.assertEquals("incorrect leaf node count", 0, tree.leafNodeCount());
outOfBoundsPos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (BASIC_TREE_WIDTH_IN_BLOCKS/2), 0);
testSet(tree, outOfBoundsPos, -1, IndexOutOfBoundsException.class);
Assert.assertEquals("incorrect leaf node count", 0, tree.leafNodeCount());
// in bounds //
outOfBoundsPos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (BASIC_TREE_WIDTH_IN_BLOCKS/2)-1, 0);
testSet(tree, outOfBoundsPos, 0);
Assert.assertEquals("incorrect leaf node count", 1, tree.leafNodeCount());
outOfBoundsPos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (BASIC_TREE_WIDTH_IN_BLOCKS/2)-3, 0);
testSet(tree, outOfBoundsPos, 0);
Assert.assertEquals("incorrect leaf node count", 2, tree.leafNodeCount());
// TODO this position probably has trouble with getting the center.
outOfBoundsPos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (BASIC_TREE_WIDTH_IN_BLOCKS/2)-2, 0);
testSet(tree, outOfBoundsPos, 0);
Assert.assertEquals("incorrect leaf node count", 3, tree.leafNodeCount());
outOfBoundsPos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (BASIC_TREE_WIDTH_IN_BLOCKS/2)-4, 0);
testSet(tree, outOfBoundsPos, 0);
Assert.assertEquals("incorrect leaf node count", 4, tree.leafNodeCount());
}
@Test
public void QuadTreeMovingTest()
public void QuadTreeRootAlignedMovingTest()
{
int treeWidthInRootNodes = 8;
int treeWidthInBlocks = ROOT_NODE_WIDTH_IN_BLOCKS * treeWidthInRootNodes;
QuadTree<Integer> tree = new QuadTree<>(treeWidthInBlocks, new DhBlockPos2D(0, 0));
QuadTree<Integer> tree = new QuadTree<>(treeWidthInBlocks, TREE_CENTER_POS);
// root nodes //
@@ -212,18 +250,18 @@ public class QuadTreeTest
Assert.assertEquals("incorrect leaf node count", 2, tree.leafNodeCount());
// move so only the root nodes exactly on the X edge remain
DhBlockPos2D edgeMoveBlockPos = new DhBlockPos2D(ROOT_NODE_WIDTH_IN_BLOCKS - (BASIC_TREE_WIDTH_IN_ROOT_NODES*ROOT_NODE_WIDTH_IN_BLOCKS), 0);
DhBlockPos2D edgeMoveBlockPos = new DhBlockPos2D(ROOT_NODE_WIDTH_IN_BLOCKS - (BASIC_TREE_INPUT_WIDTH_IN_ROOT_NODES*ROOT_NODE_WIDTH_IN_BLOCKS), 0);
tree.setCenterBlockPos(edgeMoveBlockPos);
Assert.assertEquals("Tree center incorrect", edgeMoveBlockPos, tree.getCenterBlockPos());
Assert.assertEquals("incorrect leaf node count", 1, tree.leafNodeCount());
Assert.assertEquals("incorrect leaf node count", 2, tree.leafNodeCount());
}
@Test
public void QuadTreeIterationTest()
{
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0));
QuadTree<Integer> tree = new QuadTree<>(BASIC_TREE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
// root nodes //
@@ -258,25 +296,133 @@ public class QuadTreeTest
}
@Test
public void FullTreeTest()
public void CenteredGridListIterationTest()
{
QuadTree<Integer> tree = new QuadTree<>(0, new DhBlockPos2D(0, 0));
// minimum size tree should be 3 root nodes wide
Assert.assertEquals("incorrect minimum size tree", 3, tree.ringListWidth());
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, -1), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, 0), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, 1), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, -1), 0);
final QuadTree<Integer> tree = new QuadTree<>(MINIMUM_TREE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, 0), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, 1), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, -1), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, 0), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, 1), 0);
// confirm the root node were added
final AtomicInteger rootNodeCount = new AtomicInteger(0);
tree.forEachRootNode((rootNode) -> { rootNodeCount.addAndGet(1); });
Assert.assertEquals("incorrect root count", 1, rootNodeCount.get());
// attempt to get and remove, each node in the tree
final AtomicInteger rootNodePosCount = new AtomicInteger(0);
tree.forEachRootNodePos((renderBufferNode, pos2d) ->
{
DhSectionPos sectionPos = new DhSectionPos(tree.treeMaxDetailLevel, pos2d.x, pos2d.y);
testGet(tree, sectionPos, 0);
testSet(tree, sectionPos, null);
rootNodePosCount.addAndGet(1);
});
Assert.assertEquals("incorrect root count", 1, rootNodeCount.get());
}
@Test
public void OffsetGridListIterationTest()
{
// offset fully inside (10*0,0)
final QuadTree<Integer> fullyInsideTree = new QuadTree<>(MINIMUM_TREE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
DhBlockPos2D fullyInsideOffsetBlockPos = new DhBlockPos2D(MINIMUM_TREE_WIDTH_IN_BLOCKS, MINIMUM_TREE_WIDTH_IN_BLOCKS);
fullyInsideTree.setCenterBlockPos(fullyInsideOffsetBlockPos);
fullyInsideTree.forEachRootNodePos((rootNode, pos2D) ->
{
testSet(fullyInsideTree, new DhSectionPos(fullyInsideTree.treeMaxDetailLevel, pos2D.x, pos2D.y), 0);
});
// only 1 root node should be added
final AtomicInteger fullyInsideRootNodeCount = new AtomicInteger(0);
fullyInsideTree.forEachRootNode((rootNode) -> { fullyInsideRootNodeCount.addAndGet(1); });
Assert.assertEquals("incorrect root count", 1, fullyInsideRootNodeCount.get());
// offset fully inside (10*0,0)
final QuadTree<Integer> borderInsideTree = new QuadTree<>(MINIMUM_TREE_WIDTH_IN_BLOCKS, new DhBlockPos2D(MINIMUM_TREE_WIDTH_IN_BLOCKS * 2, MINIMUM_TREE_WIDTH_IN_BLOCKS * 2));
borderInsideTree.forEachRootNodePos((rootNode, pos2D) ->
{
testSet(borderInsideTree, new DhSectionPos(borderInsideTree.treeMaxDetailLevel, pos2D.x, pos2D.y), 0);
});
// only 1 root node should be added
final AtomicInteger borderInsideRootNodeCount = new AtomicInteger(0);
borderInsideTree.forEachRootNode((rootNode) -> { borderInsideRootNodeCount.addAndGet(1); });
Assert.assertEquals("incorrect root count", 1, borderInsideRootNodeCount.get());
// offset across (10*-1,0) and (10*0,0)
final QuadTree<Integer> acrossTree = new QuadTree<>(MINIMUM_TREE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
DhBlockPos2D acrossOffsetBlockPos = new DhBlockPos2D(-MINIMUM_TREE_WIDTH_IN_BLOCKS/4, MINIMUM_TREE_WIDTH_IN_BLOCKS);
acrossTree.setCenterBlockPos(acrossOffsetBlockPos);
acrossTree.forEachRootNodePos((rootNode, pos2D) ->
{
testSet(acrossTree, new DhSectionPos(acrossTree.treeMaxDetailLevel, pos2D.x, pos2D.y), 0);
});
// 2 root nodes should be added
final AtomicInteger acrossRootNodeCount = new AtomicInteger(0);
acrossTree.forEachRootNode((rootNode) -> { acrossRootNodeCount.addAndGet(1); });
Assert.assertEquals("incorrect root count", 2, acrossRootNodeCount.get());
}
@Test
public void TinyGridAlignedTreeTest()
{
QuadTree<Integer> tree = new QuadTree<>(ROOT_NODE_WIDTH_IN_BLOCKS, TREE_CENTER_POS);
// minimum size tree should be 3 root nodes wide
Assert.assertEquals("incorrect tree node width", 3, tree.ringListWidth());
Assert.assertEquals("incorrect tree width", ROOT_NODE_WIDTH_IN_BLOCKS, tree.diameterInBlocks());
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, 0), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, -1), -1, IndexOutOfBoundsException.class);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, 1), -1, IndexOutOfBoundsException.class);
AtomicInteger rootCount = new AtomicInteger(0);
tree.forEachRootNode((rootNode) ->
{
rootCount.getAndAdd(1);
});
Assert.assertEquals("incorrect leaf value sum", 1, rootCount.get());
}
@Test
public void TinyGridOffsetTreeTest()
{
QuadTree<Integer> tree = new QuadTree<>(ROOT_NODE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0));
// minimum size tree should be 3 root nodes wide
Assert.assertEquals("incorrect tree node width", 3, tree.ringListWidth());
Assert.assertEquals("incorrect tree width", ROOT_NODE_WIDTH_IN_BLOCKS, tree.diameterInBlocks());
// 2x2 valid positions (overlap the tree's width)
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, 0), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, 0), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, -1), 0);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, -1), 0);
// invalid positions
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, -1, 1), -1, IndexOutOfBoundsException.class);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 0, 1), -1, IndexOutOfBoundsException.class);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, 0), -1, IndexOutOfBoundsException.class);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, 1), -1, IndexOutOfBoundsException.class);
testSet(tree, new DhSectionPos(tree.treeMaxDetailLevel, 1, -1), -1, IndexOutOfBoundsException.class);
AtomicInteger rootCount = new AtomicInteger(0);
@@ -284,7 +430,7 @@ public class QuadTreeTest
{
rootCount.getAndAdd(1);
});
Assert.assertEquals("incorrect leaf value sum", 9, rootCount.get());
Assert.assertEquals("incorrect leaf value sum", 4, rootCount.get());
}