Inland hollow box cap

- Treat an inland hollow as open when its basin box would pass 2^20 cells, decided from the whole hollow
- Mark a classified hollow's cells inside the window as seen so its other pieces are not classified again
- Test a long diagonal trench classified open from two windows without sampling its basin box
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-02 04:19:56 -05:00
1 parent e108bd3c58
commit 06b7001132
3 files changed
+81 -25

No files matched your search

@@ -17,6 +17,7 @@ import java.util.Set;
final class InlandHollows {
private static final int MAX_HOLLOW_CELLS = 1 << 14;
private static final long MAX_BASIN_CELLS = 1L << 20;
private static final int BASIN_MARGIN = 64;
private static final int[] AROUND_X = {-1, 0, 1, -1, 1, -1, 0, 1};
private static final int[] AROUND_Z = {-1, -1, -1, 0, 0, 1, 1, 1};
@@ -59,11 +60,12 @@ final class InlandHollows {
if (underLake(local, lakes)) {
continue;
}
Set<Long> hollow = enclosed(local);
if (hollow == null) {
Hollow hollow = enclosed(local);
see(hollow.members(), seen);
if (hollow.open()) {
continue;
}
mark(hollow, inland);
mark(hollow.members(), inland);
mark(basin(hollow, minDepth, epsilon), basins);
}
return new Hollows(inland, basins);
@@ -92,10 +94,20 @@ final class InlandHollows {
return cells;
}
private Set<Long> enclosed(List<Long> local) {
private Hollow enclosed(List<Long> local) {
Set<Long> members = new HashSet<>(local);
Set<Long> checked = new HashSet<>(local);
List<Long> frontier = new ArrayList<>(local);
int minX = Integer.MAX_VALUE;
int minZ = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxZ = Integer.MIN_VALUE;
for (long cell : local) {
minX = Math.min(minX, x(cell));
minZ = Math.min(minZ, z(cell));
maxX = Math.max(maxX, x(cell));
maxZ = Math.max(maxZ, z(cell));
}
while (!frontier.isEmpty()) {
List<Long> around = new ArrayList<>();
for (long cell : frontier) {
@@ -110,35 +122,29 @@ final class InlandHollows {
frontier = new ArrayList<>();
for (long cell : around) {
if (oceans.contains(cell)) {
return null;
return new Hollow(members, minX, minZ, maxX, maxZ, true);
}
if (belowSea(heights.get(cell), false)) {
members.add(cell);
frontier.add(cell);
minX = Math.min(minX, x(cell));
minZ = Math.min(minZ, z(cell));
maxX = Math.max(maxX, x(cell));
maxZ = Math.max(maxZ, z(cell));
}
}
if (members.size() > MAX_HOLLOW_CELLS) {
return null;
if (members.size() > MAX_HOLLOW_CELLS || basinCells(minX, minZ, maxX, maxZ) > MAX_BASIN_CELLS) {
return new Hollow(members, minX, minZ, maxX, maxZ, true);
}
}
return members;
return new Hollow(members, minX, minZ, maxX, maxZ, false);
}
private Set<Long> basin(Set<Long> hollow, float minDepth, float epsilon) {
int minX = Integer.MAX_VALUE;
int minZ = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxZ = Integer.MIN_VALUE;
for (long cell : hollow) {
minX = Math.min(minX, x(cell));
minZ = Math.min(minZ, z(cell));
maxX = Math.max(maxX, x(cell));
maxZ = Math.max(maxZ, z(cell));
}
int boxX = minX - BASIN_MARGIN;
int boxZ = minZ - BASIN_MARGIN;
int width = maxX - minX + 1 + 2 * BASIN_MARGIN;
int depth = maxZ - minZ + 1 + 2 * BASIN_MARGIN;
private Set<Long> basin(Hollow hollow, float minDepth, float epsilon) {
int boxX = hollow.minX() - BASIN_MARGIN;
int boxZ = hollow.minZ() - BASIN_MARGIN;
int width = hollow.maxX() - hollow.minX() + 1 + 2 * BASIN_MARGIN;
int depth = hollow.maxZ() - hollow.minZ() + 1 + 2 * BASIN_MARGIN;
HydrologySampleBuffer samples = new HydrologySampleBuffer(width * depth);
fine.fill(boxX * stride, boxZ * stride, width, depth, stride, samples);
DemGrid box = new DemGrid(boxX * natural.cell(), boxZ * natural.cell(), natural.cell(), width, depth);
@@ -153,10 +159,10 @@ final class InlandHollows {
}
float[] filled = PriorityFlood.fill(box, seeds, epsilon);
float[] values = box.values();
Set<Long> basin = new HashSet<>(hollow);
Set<Long> basin = new HashSet<>(hollow.members());
ArrayDeque<Integer> queue = new ArrayDeque<>();
boolean[] seen = new boolean[width * depth];
for (long cell : hollow) {
for (long cell : hollow.members()) {
int index = (z(cell) - boxZ) * width + x(cell) - boxX;
seen[index] = true;
queue.add(index);
@@ -220,6 +226,15 @@ final class InlandHollows {
return false;
}
private void see(Set<Long> cells, boolean[] seen) {
for (long cell : cells) {
int index = local(cell);
if (index >= 0) {
seen[index] = true;
}
}
}
private void mark(Set<Long> cells, BitSet out) {
for (long cell : cells) {
int index = local(cell);
@@ -239,6 +254,10 @@ final class InlandHollows {
return !sea && Math.round(height) < seaLevel;
}
private static long basinCells(int minX, int minZ, int maxX, int maxZ) {
return (long) (maxX - minX + 1 + 2 * BASIN_MARGIN) * (maxZ - minZ + 1 + 2 * BASIN_MARGIN);
}
private static long key(int x, int z) {
return ((long) x << 32) | (z & 0xFFFFFFFFL);
}
@@ -253,4 +272,7 @@ final class InlandHollows {
record Hollows(BitSet inland, BitSet basins) {
}
private record Hollow(Set<Long> members, int minX, int minZ, int maxX, int maxZ, boolean open) {
}
}
@@ -22,6 +22,11 @@ public class InlandHollowsTest {
private static final double PIT_RADIUS = 100D;
private static final double PIT_DEPTH = 64D;
private static final int LAKE_STEP = 16;
private static final double TRENCH_FROM = 128D;
private static final double TRENCH_TO = 7800D;
private static final double TRENCH_HALF_WIDTH = 16D;
private static final int TRENCH_WINDOW_CELL = 500;
private static final long MAX_HOLLOW_SAMPLES = 200_000L;
@Test
public void hollowCutByTheGridEdgeIsClassifiedLikeAWholeOne() {
@@ -78,6 +83,26 @@ public class InlandHollowsTest {
assertEquals("and its basin keeps the trunk", 0, held.basins().cardinality());
}
@Test
public void hollowTooLongForItsBasinBoxIsOpenFromEveryWindow() {
SyntheticTerrain terrain = SyntheticTerrain.diagonalTrench(TRENCH_FROM, TRENCH_TO, TRENCH_HALF_WIDTH, SEA_LEVEL - 20D);
HeightLattice fine = new HeightLattice(terrain, WindowPlanner.FINE_CELL, false, 1 << 20);
for (int cell : new int[]{0, TRENCH_WINDOW_CELL}) {
Window window = new Window(fine, cell, cell);
int below = 0;
for (float height : window.natural.values()) {
below += Math.round(height) < SEA_LEVEL ? 1 : 0;
}
assertTrue("the trench crosses the window at cell " + cell + ": " + below, below > 20);
long before = terrain.naturalSamples();
InlandHollows.Hollows hollows = window.classify();
long sampled = terrain.naturalSamples() - before;
assertEquals("the trench is open from the window at cell " + cell, 0, hollows.inland().cardinality());
assertEquals("and has no basin there", 0, hollows.basins().cardinality());
assertTrue("ground sampled to classify the trench from the window at cell " + cell + ": " + sampled, sampled < MAX_HOLLOW_SAMPLES);
}
}
private static final class Window {
private final HeightLattice fine;
private final DemGrid natural;
@@ -303,6 +303,15 @@ final class SyntheticTerrain implements HydrologyTerrainSource {
});
}
static SyntheticTerrain diagonalTrench(double from, double to, double halfWidth, double floor) {
double land = SEA_LEVEL + 20D;
return new SyntheticTerrain((x, z) -> {
double along = 0.5D * (x + z);
double across = Math.abs(x - z) / Math.sqrt(2D);
return along >= from && along <= to && across <= halfWidth ? floor : land;
}).withLand((x, z) -> true);
}
private static double valleySlope(double x, double z) {
return 100D - 0.03D * x + 0.05D * Math.abs(z - VALLEY_AXIS);
}