Fix QuadTree boundary issues and add tests

This commit is contained in:
James Seibel
2023-03-20 20:12:27 -05:00
parent 03acb159e0
commit 515ebb8c99
2 changed files with 17 additions and 11 deletions
@@ -36,10 +36,9 @@ public class QuadTree<T>
/**
* Constructor of the quadTree
* @param widthInBlocks equivalent to the distance between two opposing sides
*/
public QuadTree(
int viewDistanceInBlocks,
DhBlockPos2D centerBlockPos)
public QuadTree(int widthInBlocks, DhBlockPos2D centerBlockPos)
{
DetailDistanceUtil.updateSettings(); //TODO: Move this to somewhere else
this.centerBlockPos = centerBlockPos;
@@ -47,7 +46,7 @@ public class QuadTree<T>
this.treeMaxDetailLevel = 10; // TODO we may need to make this dynamic // detail 10 = (2^10) 1024 blocks wide
// int halfSize = 12; // TODO use this.treeMaxDetailLevel to determine
int halfSize = Math.floorDiv(viewDistanceInBlocks, 2) / BitShiftUtil.powerOfTwo(this.treeMaxDetailLevel);
int halfSize = Math.floorDiv(widthInBlocks, 2) / BitShiftUtil.powerOfTwo(this.treeMaxDetailLevel);
halfSize = Math.max(halfSize, 1); // at minimum the ring list should have 3x3 (9) root nodes in it, to account for moving around
Pos2D ringListCenterPos = new Pos2D(
+14 -7
View File
@@ -38,7 +38,9 @@ public class QuadTreeTest
private static final Logger LOGGER = DhLoggerBuilder.getLogger();
private static final int ROOT_NODE_WIDTH_IN_BLOCKS = BitShiftUtil.powerOfTwo(10);
private static final int BASIC_TREE_WIDTH_IN_BLOCKS = ROOT_NODE_WIDTH_IN_BLOCKS * 8;
/** 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;
static
{
@@ -51,6 +53,7 @@ public class QuadTreeTest
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());
// root node //
@@ -136,7 +139,6 @@ public class QuadTreeTest
QuadTree<Integer> tree = new QuadTree<>(treeWidthInBlocks, new DhBlockPos2D(0, 0));
// root nodes //
testSet(tree, new DhSectionPos((byte)10, 0, 0), 1);
@@ -199,13 +201,18 @@ public class QuadTreeTest
tree.setCenterBlockPos(DhBlockPos2D.ZERO);
Assert.assertEquals("Tree center incorrect", DhBlockPos2D.ZERO, tree.getCenterBlockPos());
// 1 root node from the edge
DhSectionPos edgePos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, -((treeWidthInBlocks/2)-ROOT_NODE_WIDTH_IN_BLOCKS), 0);
testSet(tree, edgePos, 2);
// on the negative X edge
DhSectionPos edgePos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, -treeWidthInBlocks/2, 0);
testSet(tree, edgePos, 1);
Assert.assertEquals("incorrect leaf node count", 1, tree.leafNodeCount());
// edge move
DhBlockPos2D edgeMoveBlockPos = new DhBlockPos2D(ROOT_NODE_WIDTH_IN_BLOCKS, 0); // TODO I can only move this 1 root node away from the center for some reason
// +1 root node from the negative X edge
DhSectionPos adjacentEdgePos = new DhSectionPos(LodUtil.BLOCK_DETAIL_LEVEL, (-treeWidthInBlocks/2)+ROOT_NODE_WIDTH_IN_BLOCKS, 0);
testSet(tree, adjacentEdgePos, 2);
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);
tree.setCenterBlockPos(edgeMoveBlockPos);
Assert.assertEquals("Tree center incorrect", edgeMoveBlockPos, tree.getCenterBlockPos());