Iterator impl + findFirst + findClosest (#139)

This commit is contained in:
Caedis authored and GitHub committed 2025-06-03 17:50:57 +00:00
1 parent bc954a45b9
commit 6f99b32a0b
3 files changed
+328 -50

No files matched your search

@@ -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 <T> object type to store
*/
@SuppressWarnings("unused")
public class SpatialHashGrid<T> {
private final int cellSize;
@@ -43,11 +47,11 @@ public class SpatialHashGrid<T> {
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<T> {
*/
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<T> list = grid.computeIfAbsent(key, k -> new ObjectArrayList<>());
list.add(obj);
}
@@ -66,7 +70,7 @@ public class SpatialHashGrid<T> {
*/
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<T> list = grid.get(key);
if (list == null) return;
@@ -83,7 +87,15 @@ public class SpatialHashGrid<T> {
* @return list of nearby objects
*/
public List<T> findNearbySquaredEuclidean(int x, int y, int z, int radius) {
return findNearbyWithFormula(x, y, z, radius, DistanceFormula.SquaredEuclidean);
Iterator<T> iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.SquaredEuclidean);
List<T> resultList = new ArrayList<>();
while (iterator.hasNext()) {
T obj = iterator.next();
resultList.add(obj);
}
return resultList;
}
/**
@@ -93,7 +105,15 @@ public class SpatialHashGrid<T> {
* @return list of nearby objects
*/
public List<T> findNearbyChebyshev(int x, int y, int z, int radius) {
return findNearbyWithFormula(x, y, z, radius, DistanceFormula.Chebyshev);
Iterator<T> iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Chebyshev);
List<T> resultList = new ArrayList<>();
while (iterator.hasNext()) {
T obj = iterator.next();
resultList.add(obj);
}
return resultList;
}
/**
@@ -103,17 +123,175 @@ public class SpatialHashGrid<T> {
* @return list of nearby objects
*/
public List<T> findNearbyManhattan(int x, int y, int z, int radius) {
return findNearbyWithFormula(x, y, z, radius, DistanceFormula.Manhattan);
Iterator<T> iterator = findNearbyWithFormula(x, y, z, radius, DistanceFormula.Manhattan);
List<T> 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<T> 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<T> 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<T> 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<T> 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<T> 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<T> 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<T> findNearbyWithFormula(int x, int y, int z, int radius, DistanceFormula distanceFormula) {
public Iterator<T> 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<T> {
final int cellRad = (radius + cellSize - 1) / cellSize;
final int distanceCompared = (distanceFormula == DistanceFormula.SquaredEuclidean ? radius * radius : radius);
final ObjectArrayList<T> 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<T> 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<T> {
case Manhattan -> DistanceUtil.manhattanDistance(x1, y1, z1, x2, y2, z2);
};
}
public static class GridIterator<T> implements Iterator<T> {
private ObjectArrayList<T> currentList;
private final Long2ObjectOpenHashMap<ObjectArrayList<T>> 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<Vector3i, T> positionExtractor;
private final Vector3i scratch = new Vector3i();
public GridIterator(Long2ObjectOpenHashMap<ObjectArrayList<T>> grid, int x, int y, int z, int cellRad,
int cellX, int cellY, int cellZ, int distanceCompared, DistanceFormula distanceFormula,
BiConsumer<Vector3i, T> 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();
}
}
}
}