From 6f99b32a0b46b966a82cb8a6cfe29047770f08eb Mon Sep 17 00:00:00 2001 From: Caedis Date: Tue, 3 Jun 2025 12:50:57 -0500 Subject: [PATCH] Iterator impl + findFirst + findClosest (#139) --- .../spatialhashgrid/SpatialHashGrid.java | 340 ++++++++++++++++-- .../spatialhashgrid/SpatialHashGridTest.java | 34 +- .../spatialhashgrid/TestObject.java | 4 +- 3 files changed, 328 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/spatialhashgrid/SpatialHashGrid.java b/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/spatialhashgrid/SpatialHashGrid.java index 01ba5ec0..8fbd75be 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/spatialhashgrid/SpatialHashGrid.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/datastructs/spatialhashgrid/SpatialHashGrid.java @@ -1,6 +1,9 @@ package com.gtnewhorizon.gtnhlib.datastructs.spatialhashgrid; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import java.util.function.BiConsumer; import org.joml.Vector3i; @@ -17,6 +20,7 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; * * @param object type to store */ +@SuppressWarnings("unused") public class SpatialHashGrid { private final int cellSize; @@ -43,11 +47,11 @@ public class SpatialHashGrid { this.positionExtractor = positionExtractor; } - private long pack(int x, int y, int z) { + private static long pack(int x, int y, int z) { return CoordinatePacker.pack(x, y, z); } - private long hash(int x, int y, int z) { + private static long hash(int cellSize, int x, int y, int z) { return pack(Math.floorDiv(x, cellSize), Math.floorDiv(y, cellSize), Math.floorDiv(z, cellSize)); } @@ -56,7 +60,7 @@ public class SpatialHashGrid { */ public void insert(T obj) { positionExtractor.accept(scratch, obj); - long key = hash(scratch.x, scratch.y, scratch.z); + long key = hash(cellSize, scratch.x, scratch.y, scratch.z); ObjectArrayList list = grid.computeIfAbsent(key, k -> new ObjectArrayList<>()); list.add(obj); } @@ -66,7 +70,7 @@ public class SpatialHashGrid { */ public void remove(T obj) { positionExtractor.accept(scratch, obj); - long key = hash(scratch.x, scratch.y, scratch.z); + long key = hash(cellSize, scratch.x, scratch.y, scratch.z); ObjectArrayList list = grid.get(key); if (list == null) return; @@ -83,7 +87,15 @@ public class SpatialHashGrid { * @return list of nearby objects */ public List findNearbySquaredEuclidean(int x, int y, int z, int radius) { - return findNearbyWithFormula(x, y, z, radius, DistanceFormula.SquaredEuclidean); + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.SquaredEuclidean); + List resultList = new ArrayList<>(); + + while (iterator.hasNext()) { + T obj = iterator.next(); + resultList.add(obj); + } + + return resultList; } /** @@ -93,7 +105,15 @@ public class SpatialHashGrid { * @return list of nearby objects */ public List findNearbyChebyshev(int x, int y, int z, int radius) { - return findNearbyWithFormula(x, y, z, radius, DistanceFormula.Chebyshev); + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Chebyshev); + List resultList = new ArrayList<>(); + + while (iterator.hasNext()) { + T obj = iterator.next(); + resultList.add(obj); + } + + return resultList; } /** @@ -103,17 +123,175 @@ public class SpatialHashGrid { * @return list of nearby objects */ public List findNearbyManhattan(int x, int y, int z, int radius) { - return findNearbyWithFormula(x, y, z, radius, DistanceFormula.Manhattan); + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Manhattan); + List resultList = new ArrayList<>(); + + while (iterator.hasNext()) { + T obj = iterator.next(); + resultList.add(obj); + } + + return resultList; + } + + /** + * Find the first nearby object using Squared Euclidean Distance Formula. + * + * @param x position to check + * @param y position to check + * @param z position to check + * @param radius distance in blocks to check (sphere) + * @return the first nearby object or null if none found + */ + public T findFirstNearbySquaredEuclidean(int x, int y, int z, int radius) { + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.SquaredEuclidean); + return (iterator.hasNext() ? iterator.next() : null); + } + + /** + * Find the first nearby object using Chebyshev (Chessboard) Distance Formula. + * + * @param x position to check + * @param y position to check + * @param z position to check + * @param radius distance in blocks to check (sphere) + * @return the first nearby object or null if none found + */ + public T findFirstNearbyChebyshev(int x, int y, int z, int radius) { + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Chebyshev); + return (iterator.hasNext() ? iterator.next() : null); + } + + /** + * Find the first nearby object using Manhattan (Taxicab) Distance Formula. + * + * @param x position to check + * @param y position to check + * @param z position to check + * @param radius distance in blocks to check (sphere) + * @return the first nearby object or null if none found + */ + public T findFirstNearbyManhattan(int x, int y, int z, int radius) { + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Manhattan); + return (iterator.hasNext() ? iterator.next() : null); + } + + /** + * Find the closest nearby object using Squared Euclidean Distance Formula. + * + * @param x position to check + * @param y position to check + * @param z position to check + * @param radius distance in blocks to check (sphere) + * @return the closest nearby object or null if none found + */ + public T findClosestNearbySquaredEuclidean(int x, int y, int z, int radius) { + T closestObject = null; + double closestDistance = Double.MAX_VALUE; + + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.SquaredEuclidean); + while (iterator.hasNext()) { + T obj = iterator.next(); + positionExtractor.accept(scratch, obj); + double distance = distanceBetweenPoints( + x, + y, + z, + scratch.x, + scratch.y, + scratch.z, + DistanceFormula.SquaredEuclidean); + + if (distance < closestDistance) { + closestDistance = distance; + closestObject = obj; + } + } + + return closestObject; + } + + /** + * Find the closest nearby object using Chebyshev (Chessboard) Distance Formula. + * + * @param x position to check + * @param y position to check + * @param z position to check + * @param radius distance in blocks to check (sphere) + * @return the closest nearby object or null if none found + */ + public T findClosestNearbyChebyshev(int x, int y, int z, int radius) { + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Chebyshev); + T closestObject = null; + double closestDistance = Double.MAX_VALUE; + + while (iterator.hasNext()) { + T obj = iterator.next(); + positionExtractor.accept(scratch, obj); + double distance = distanceBetweenPoints( + x, + y, + z, + scratch.x, + scratch.y, + scratch.z, + DistanceFormula.Chebyshev); + + if (distance < closestDistance) { + closestDistance = distance; + closestObject = obj; + } + } + + return closestObject; + } + + /** + * Find the closest nearby object using Manhattan (Taxicab) Distance Formula. + * + * @param x position to check + * @param y position to check + * @param z position to check + * @param radius distance in blocks to check (sphere) + * @return the closest nearby object or null if none found + */ + public T findClosestNearbyManhattan(int x, int y, int z, int radius) { + Iterator iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Manhattan); + T closestObject = null; + double closestDistance = Double.MAX_VALUE; + + while (iterator.hasNext()) { + T obj = iterator.next(); + positionExtractor.accept(scratch, obj); + double distance = distanceBetweenPoints( + x, + y, + z, + scratch.x, + scratch.y, + scratch.z, + DistanceFormula.Manhattan); + + if (distance < closestDistance) { + closestDistance = distance; + closestObject = obj; + } + } + + return closestObject; } /** * Search the grid for nearby objects using a specified distance formula. * + * @param x position to check + * @param y position to check + * @param z position to check * @param radius distance in blocks to check (sphere) * @param distanceFormula Distance Formula to use - * @return list of nearby objects + * @return iterator of nearby objects */ - private List findNearbyWithFormula(int x, int y, int z, int radius, DistanceFormula distanceFormula) { + public Iterator findNearbyWithFormula(int x, int y, int z, int radius, DistanceFormula distanceFormula) { radius = Math.abs(radius); // just no final int cellX = Math.floorDiv(x, cellSize); final int cellY = Math.floorDiv(y, cellSize); @@ -123,38 +301,21 @@ public class SpatialHashGrid { final int cellRad = (radius + cellSize - 1) / cellSize; final int distanceCompared = (distanceFormula == DistanceFormula.SquaredEuclidean ? radius * radius : radius); - final ObjectArrayList result = new ObjectArrayList<>(); - - for (int dx = -cellRad; dx <= cellRad; dx++) { - for (int dy = -cellRad; dy <= cellRad; dy++) { - for (int dz = -cellRad; dz <= cellRad; dz++) { - long key = pack(cellX + dx, cellY + dy, cellZ + dz); - final ObjectArrayList list = grid.get(key); - if (list == null || list.isEmpty()) continue; - - boolean isEdge = (Math.abs(dx) == cellRad) || (Math.abs(dy) == cellRad) - || (Math.abs(dz) == cellRad); - - if (isEdge) { - for (T obj : list) { - positionExtractor.accept(scratch, obj); - if (distanceBetweenPoints(x, y, z, scratch.x, scratch.y, scratch.z, distanceFormula) - > distanceCompared) - continue; - - result.add(obj); - } - } else { - result.addAll(list); - } - } - } - } - - return result; + return new GridIterator<>( + grid, + x, + y, + z, + cellRad, + cellX, + cellY, + cellZ, + distanceCompared, + distanceFormula, + positionExtractor); } - private double distanceBetweenPoints(double x1, double y1, double z1, double x2, double y2, double z2, + protected static double distanceBetweenPoints(double x1, double y1, double z1, double x2, double y2, double z2, DistanceFormula distanceFormula) { return switch (distanceFormula) { case SquaredEuclidean -> DistanceUtil.squaredEuclideanDistance(x1, y1, z1, x2, y2, z2); @@ -162,4 +323,105 @@ public class SpatialHashGrid { case Manhattan -> DistanceUtil.manhattanDistance(x1, y1, z1, x2, y2, z2); }; } + + public static class GridIterator implements Iterator { + + private ObjectArrayList currentList; + private final Long2ObjectOpenHashMap> grid; + private final int x; + private final int y; + private final int z; + private final int cellRad; + private int dx; + private int dy; + private int dz; + private final int cellX; + private final int cellY; + private final int cellZ; + private final int distanceCompared; + private final DistanceFormula distanceFormula; + private final BiConsumer positionExtractor; + private final Vector3i scratch = new Vector3i(); + + public GridIterator(Long2ObjectOpenHashMap> grid, int x, int y, int z, int cellRad, + int cellX, int cellY, int cellZ, int distanceCompared, DistanceFormula distanceFormula, + BiConsumer positionExtractor) { + this.grid = grid; + this.x = x; + this.y = y; + this.z = z; + this.cellRad = cellRad; + dx = -cellRad; + dy = -cellRad; + dz = -cellRad; + this.cellX = cellX; + this.cellY = cellY; + this.cellZ = cellZ; + this.distanceCompared = distanceCompared; + this.distanceFormula = distanceFormula; + this.positionExtractor = positionExtractor; + advance(); + } + + private void advance() { + while (dx <= cellRad) { + while (dy <= cellRad) { + while (dz <= cellRad) { + long key = pack(cellX + dx, cellY + dy, cellZ + dz); + boolean isEdge = (Math.abs(dx) == cellRad) || (Math.abs(dy) == cellRad) + || (Math.abs(dz) == cellRad); + currentList = grid.get(key); + if (currentList != null) { + if (isEdge) { + var iterator = currentList.iterator(); + while (iterator.hasNext()) { + T mte = iterator.next(); + positionExtractor.accept(scratch, mte); + if (distanceBetweenPoints(x, y, z, scratch.x, scratch.y, scratch.z, distanceFormula) + > distanceCompared) { + iterator.remove(); + } + } + } + if (currentList != null && !currentList.isEmpty()) { + return; + } + } + dz++; + } + dz = -cellRad; // Reset dz + dy++; + } + dy = -cellRad; // Reset dy + dx++; + } + } + + @Override + public boolean hasNext() { + return currentList != null && !currentList.isEmpty(); + } + + @Override + public T next() { + if (currentList != null && !currentList.isEmpty()) { + T obj = currentList.get(0); + currentList.remove(0); + if (currentList.isEmpty()) { + advance(); + } + return obj; + } else { + throw new NoSuchElementException(); + } + } + + public T peek() { + if (currentList != null && !currentList.isEmpty()) { + return currentList.get(0); + } else { + throw new NoSuchElementException(); + } + } + } } diff --git a/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/SpatialHashGridTest.java b/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/SpatialHashGridTest.java index e9d46705..1498bebd 100644 --- a/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/SpatialHashGridTest.java +++ b/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/SpatialHashGridTest.java @@ -119,22 +119,38 @@ public class SpatialHashGridTest { } @Test - void testLargeRadiusQuery() { + void testClosest() { SpatialHashGrid grid = new SpatialHashGrid<>(5, (pos, obj) -> { pos.x = obj.x; pos.y = obj.y; pos.z = obj.z; }); - TestObject obj1 = new TestObject(5, 5, 5); - TestObject obj2 = new TestObject(10, 10, 10); - grid.insert(obj1); + TestObject obj = new TestObject(5, 5, 5); + grid.insert(obj); + TestObject obj2 = new TestObject(10, 5, 5); grid.insert(obj2); - Collection results = grid.findNearbyChebyshev(5, 5, 5, 10); - assertNotNull(results); - assertTrue(results.contains(obj1)); - assertTrue(results.contains(obj2)); - assertEquals(2, results.size()); + TestObject result = grid.findClosestNearbyChebyshev(9, 5, 5, 10); + assertNotNull(result); + assertEquals(result, obj2); + } + + @Test + void testFirst() { + SpatialHashGrid grid = new SpatialHashGrid<>(5, (pos, obj) -> { + pos.x = obj.x; + pos.y = obj.y; + pos.z = obj.z; + }); + + TestObject obj = new TestObject(5, 5, 5); + grid.insert(obj); + TestObject obj2 = new TestObject(10, 5, 5); + grid.insert(obj2); + + TestObject result = grid.findFirstNearbyChebyshev(6, 5, 5, 10); + assertNotNull(result); + assertEquals(result, obj); } } diff --git a/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/TestObject.java b/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/TestObject.java index 81a1b4f9..302f3b30 100644 --- a/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/TestObject.java +++ b/src/test/java/com/gtnewhorizon/gtnhlib/test/datastructs/spatialhashgrid/TestObject.java @@ -1,10 +1,10 @@ package com.gtnewhorizon.gtnhlib.test.datastructs.spatialhashgrid; -class TestObject { +public class TestObject { public int x, y, z; - TestObject(int x, int y, int z) { + public TestObject(int x, int y, int z) { this.x = x; this.y = y; this.z = z;