diff --git a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadNode.java b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadNode.java index 434340e65..44936a8cd 100644 --- a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadNode.java +++ b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadNode.java @@ -55,7 +55,7 @@ public class QuadNode /** @return the number of non-null child nodes */ - public int childCount() + public int getChildCount() { int count = 0; for (int i = 0; i < 4; i++) @@ -68,6 +68,21 @@ public class QuadNode return count; } + /** returns the number of children that have non-null values */ + public int getChildValueCount() + { + int count = 0; + for (int i = 0; i < 4; i++) + { + QuadNode child = this.getChildByIndex(i); + if (child != null && child.value != null) + { + count++; + } + } + return count; + } + /** @@ -241,7 +256,8 @@ public class QuadNode for (int i = 0; i < 4; i++) { DhSectionPos childPos = this.sectionPos.getChildByIndex(i); - callback.accept(this.getChildByIndex(i), childPos); + QuadNode childNode = this.getChildByIndex(i); + callback.accept(childNode, childPos); } } } @@ -252,7 +268,7 @@ public class QuadNode */ public void forAllLeafValues(Consumer callback) { - if (this.childCount() == 0 || this.sectionPos.sectionDetailLevel == this.minimumDetailLevel) + if (this.getChildCount() == 0 || this.sectionPos.sectionDetailLevel == this.minimumDetailLevel) { // base case, bottom leaf node found callback.accept(this.value); @@ -312,6 +328,6 @@ public class QuadNode @Override - public String toString() { return "pos: "+this.sectionPos+", value: "+this.value; } + public String toString() { return "pos: "+this.sectionPos+", children #: "+this.getChildCount()+", value: "+this.value; } } diff --git a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java index b6fe256ed..2b407d70b 100644 --- a/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java +++ b/core/src/main/java/com/seibel/lod/core/util/objects/quadTree/QuadTree.java @@ -153,6 +153,26 @@ public class QuadTree } + public int getNonNullChildCountAtPos(DhSectionPos pos) { return this.getChildCountAtPos(pos, false); } + public int getChildCountAtPos(DhSectionPos pos, boolean includeNullValues) + { + int childCount = 0; + for (int i = 0; i < 4; i++) + { + DhSectionPos childPos = pos.getChildByIndex(i); + if (this.isSectionPosInBounds(childPos)) + { + T value = this.get(childPos); + if (includeNullValues || value != null) + { + childCount++; + } + } + } + + return childCount; + } + /** no nulls TODO comment/rename */ public void forEachRootNode(Consumer> consumer) @@ -254,4 +274,7 @@ public class QuadTree // return sb.toString(); // } + @Override + public String toString() { return "center block: "+this.centerBlockPos+", block width: "+this.widthInBlocks+", detail level range: ["+this.treeMinDetailLevel+"-"+this.treeMaxDetailLevel+"], leaf #: "+this.leafNodeCount(); } + } diff --git a/core/src/test/java/tests/QuadTreeTest.java b/core/src/test/java/tests/QuadTreeTest.java index 4feff37f0..03899b6dc 100644 --- a/core/src/test/java/tests/QuadTreeTest.java +++ b/core/src/test/java/tests/QuadTreeTest.java @@ -529,7 +529,7 @@ public class QuadTreeTest QuadNode rootNode = new QuadNode<>(new DhSectionPos((byte)10, 0, 0), (byte)0); rootNode.forEachDirectChild((child, childPos) -> { - rootNode.setValue(childPos, 1, null); + rootNode.setValue(childPos, 1); }); Assert.assertEquals("node not filled", rootNode.getChildValueCount(), 4); @@ -543,6 +543,18 @@ public class QuadTreeTest } + // this is here for quickly testing the toString method, it should never fail + @Test + public void toStringTest() + { + QuadTree tree = new QuadTree<>(ROOT_NODE_WIDTH_IN_BLOCKS, new DhBlockPos2D(0, 0), (byte)6); + + String treeString = tree.toString(); + Assert.assertNotNull(treeString); + Assert.assertNotEquals("", treeString); + + } +