-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix blocks overflowing over the top of octree bounds (#1768)
* Fix blocks overflowing over the top of octree bounds This was due to an incorrect maxDimension calculation * Add helper method to get the previous multiple of 16 * Add input parameter assertions for Octree#setCube * Fix off-by-one in setCube assertions * Add clarifying comments to calculateOctreeOrigin
- Loading branch information
1 parent
2ba5e34
commit 41a9797
Showing
6 changed files
with
109 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package se.llbit.math; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static se.llbit.testutil.TestUtils.assertThrowsWithExpectedMessage; | ||
|
||
public class OctreeTest { | ||
@Test | ||
public void test() { | ||
int depth = 5; | ||
int cubeDepth = 3; | ||
|
||
Octree octree = new Octree(new PackedOctree(depth)); | ||
|
||
int cubeSize = 1 << cubeDepth; | ||
int[] types = new int[cubeSize * cubeSize * cubeSize]; | ||
int cubesWithinOctree = (1 << depth) - cubeSize; // the number of times the cube can fit along each dimension of the octree | ||
for (int x = 0; x < cubesWithinOctree; x++) { | ||
for (int y = 0; y < cubesWithinOctree; y++) { | ||
for (int z = 0; z < cubesWithinOctree; z++) { | ||
octree.setCube(cubeDepth, types, x, y, z); | ||
} | ||
} | ||
} | ||
|
||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, 25, 0, 0), | ||
"setCube x (25,33) out of bounds for octree (0,31)"); | ||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, 0, 25, 0), | ||
"setCube y (25,33) out of bounds for octree (0,31)"); | ||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, 0, 0, 25), | ||
"setCube z (25,33) out of bounds for octree (0,31)"); | ||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, 64, 13, 23), | ||
"setCube x (64,72) out of bounds for octree (0,31)"); | ||
|
||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, -1, 22, 1), | ||
"setCube position must not be negative (-1,22,1)"); | ||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, 5, -123, 23), | ||
"setCube position must not be negative (5,-123,23)"); | ||
assertThrowsWithExpectedMessage(AssertionError.class, () -> octree.setCube(cubeDepth, types, 17, 9, -32), | ||
"setCube position must not be negative (17,9,-32)"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package se.llbit.testutil; | ||
|
||
import org.junit.jupiter.api.function.Executable; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class TestUtils { | ||
public static <T extends Throwable> T assertThrowsWithExpectedMessage(Class<T> expectedType, Executable executable, String expectedMessage) { | ||
T t = assertThrows(expectedType, executable); | ||
assertEquals(t.getMessage(), expectedMessage, "Error message differs from expected"); | ||
return t; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters