Hashed D8 ties

- Break equal-slope D8 ties with a world-keyed hash of seed and cell
- Pass the settings seed from window, region and pond planners
- Replace the scan-order test with world-hash tie tests
This commit is contained in:
Brian Fopiano committed 2026-11-03 11:58:41 -05:00
1 parent 52450fbc72
commit 750bade003
9 files changed
+128 -17

No files matched your search

@@ -2,23 +2,28 @@ package art.arcane.iris.generation.hydrology.grid;
public final class D8Router {
private static final float DIAGONAL = (float) (1.0D / Math.sqrt(2.0D));
private static final long SALT_TIE = 0x4438544945L;
private D8Router() {
}
public static int[] receivers(DemGrid grid, float[] filled) {
public static int[] receivers(DemGrid grid, float[] filled, long seed) {
int count = grid.width() * grid.depth();
if (filled.length != count) {
throw new IllegalArgumentException("Filled surface has " + filled.length + " cells, grid has " + count);
}
int[] receivers = new int[count];
for (int cell = 0; cell < count; cell++) {
receivers[cell] = receiver(grid, filled, cell);
receivers[cell] = steepest(grid, filled, cell, true, seed);
}
return receivers;
}
static int receiver(DemGrid grid, float[] filled, int cell) {
return steepest(grid, filled, cell, false, 0L);
}
private static int steepest(DemGrid grid, float[] filled, int cell, boolean hashTies, long seed) {
int width = grid.width();
int depth = grid.depth();
int i = cell % width;
@@ -26,6 +31,7 @@ public final class D8Router {
float level = filled[cell];
int best = -1;
float bestSlope = 0f;
int bestDirection = -1;
for (int dz = -1; dz <= 1; dz++) {
int nj = j + dz;
if (nj < 0 || nj >= depth) {
@@ -42,12 +48,20 @@ public final class D8Router {
continue;
}
float slope = dx != 0 && dz != 0 ? drop * DIAGONAL : drop;
if (best < 0 || slope > bestSlope) {
int direction = (dz + 1) * 3 + dx + 1;
boolean wins = best < 0 || slope > bestSlope
|| hashTies && slope == bestSlope && tie(grid, cell, seed, direction) < tie(grid, cell, seed, bestDirection);
if (wins) {
best = neighbour;
bestSlope = slope;
bestDirection = direction;
}
}
}
return best;
}
private static long tie(DemGrid grid, int cell, long seed, int direction) {
return CellHash.mix(seed, SALT_TIE + direction, grid.blockX(cell), grid.blockZ(cell));
}
}
@@ -118,7 +118,7 @@ public final class RegionPlanner {
private List<Reach> majorReaches(DemGrid raw, float[] filled, HydrologySampleBuffer samples) {
DemGrid surface = new DemGrid(raw.originX(), raw.originZ(), CELL, SPAN, SPAN);
System.arraycopy(filled, 0, surface.values(), 0, filled.length);
int[] receivers = D8Router.receivers(surface, filled);
int[] receivers = D8Router.receivers(surface, filled, settings.seed());
float[] weights = new float[filled.length];
for (int cell = 0; cell < filled.length; cell++) {
if (samples.ocean()[cell]) {
@@ -212,7 +212,7 @@ public final class WindowPlanner {
}
DemGrid surface = new DemGrid(raw.originX(), raw.originZ(), CELL, SPAN, SPAN);
System.arraycopy(filled, 0, surface.values(), 0, filled.length);
int[] receivers = D8Router.receivers(surface, filled);
int[] receivers = D8Router.receivers(surface, filled, settings.seed());
trunks.route(receivers);
float[] weights = new float[filled.length];
for (int cell = 0; cell < filled.length; cell++) {
@@ -447,7 +447,7 @@ public final class PondPlanner {
if (order.size() < 2) {
return;
}
int[] receivers = D8Router.receivers(raw, filled);
int[] receivers = D8Router.receivers(raw, filled, settings.seed());
order.sort(Comparator.naturalOrder());
for (long key : order) {
int index = (int) key;
@@ -2,13 +2,17 @@ package art.arcane.iris.generation.hydrology.grid;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class D8RouterTest {
private static final int SEED = 0;
private static final long WORLD_SEED = 1337L;
@Test
public void everyCellDrainsToASeed() {
@@ -16,7 +20,7 @@ public class D8RouterTest {
BitSet seeds = new BitSet();
seeds.set(grid.index(0, 0));
float[] filled = PriorityFlood.fill(grid, seeds, 1e-3f);
int[] receivers = D8Router.receivers(grid, filled);
int[] receivers = D8Router.receivers(grid, filled, WORLD_SEED);
int n = receivers.length;
assertEquals(-1, receivers[SEED]);
for (int i = 0; i < n; i++) {
@@ -30,7 +34,7 @@ public class D8RouterTest {
DemGrid grid = GridFixtures.random(seed, 32, 32);
BitSet seeds = GridFixtures.edgeSeeds(grid);
float[] filled = PriorityFlood.fill(grid, seeds, 1e-3f);
int[] receivers = D8Router.receivers(grid, filled);
int[] receivers = D8Router.receivers(grid, filled, WORLD_SEED);
for (int i = 0; i < receivers.length; i++) {
int r = receivers[i];
if (r < 0) {
@@ -54,18 +58,111 @@ public class D8RouterTest {
cardinalPlane[grid.index(i, j)] = i + 3f * j;
}
}
assertEquals(grid.index(3, 3), D8Router.receivers(grid, diagonalPlane)[grid.index(4, 4)]);
assertEquals(grid.index(4, 3), D8Router.receivers(grid, cardinalPlane)[grid.index(4, 4)]);
assertEquals(grid.index(3, 3), D8Router.receivers(grid, diagonalPlane, WORLD_SEED)[grid.index(4, 4)]);
assertEquals(grid.index(4, 3), D8Router.receivers(grid, cardinalPlane, WORLD_SEED)[grid.index(4, 4)]);
}
@Test
public void equalSlopesBreakTowardLowerIndex() {
public void equalSlopeTiesFollowAWorldHashNotScanOrder() {
for (long seed = 1L; seed <= 24L; seed++) {
DemGrid grid = GridFixtures.uniform(33, 33, 5f);
float[] filled = PriorityFlood.fill(grid, GridFixtures.edgeSeeds(grid), 1e-3f);
int[] receivers = D8Router.receivers(grid, filled, seed);
assertTrue("seed " + seed + ": reruns agree", Arrays.equals(receivers, D8Router.receivers(grid, filled, seed)));
List<Integer> ties = ties(grid, filled);
assertTrue("seed " + seed + ": ties on the medial lines " + ties.size(), ties.size() >= 40);
int firstInScan = 0;
for (int cell : ties) {
assertEquals("seed " + seed + ": a tie takes one of its steepest neighbours", bestSlope(grid, filled, cell), slope(grid, filled, cell, receivers[cell]), 0f);
firstInScan += receivers[cell] == firstSteepest(grid, filled, cell) ? 1 : 0;
}
assertTrue("seed " + seed + ": the first neighbour in scan order wins " + firstInScan + " of " + ties.size(), 4 * firstInScan >= ties.size());
assertTrue("seed " + seed + ": a later neighbour wins " + (ties.size() - firstInScan) + " of " + ties.size(), 4 * (ties.size() - firstInScan) >= ties.size());
DemGrid shifted = new DemGrid(4096, 0, grid.cell(), 33, 33);
System.arraycopy(grid.values(), 0, shifted.values(), 0, grid.values().length);
float[] shiftedFilled = PriorityFlood.fill(shifted, GridFixtures.edgeSeeds(shifted), 1e-3f);
assertEquals("seed " + seed + ": the same cells tie after a shift", ties, ties(shifted, shiftedFilled));
int[] shiftedReceivers = D8Router.receivers(shifted, shiftedFilled, seed);
int moved = 0;
for (int cell : ties) {
moved += receivers[cell] == shiftedReceivers[cell] ? 0 : 1;
}
assertTrue("seed " + seed + ": ties follow world position, " + moved + " of " + ties.size() + " changed", 4 * moved >= ties.size());
}
}
@Test
public void tiesDependOnTheSeedAndOnlyOnTiedNeighbours() {
DemGrid grid = GridFixtures.uniform(9, 9, 20f);
float[] filled = grid.values().clone();
filled[grid.index(4, 4)] = 10f;
filled[grid.index(5, 4)] = 9f;
filled[grid.index(4, 5)] = 9f;
assertEquals(grid.index(5, 4), D8Router.receivers(grid, filled)[grid.index(4, 4)]);
filled[grid.index(3, 4)] = 9.5f;
int east = 0;
for (long seed = 0L; seed < 64L; seed++) {
int receiver = D8Router.receivers(grid, filled, seed)[grid.index(4, 4)];
assertTrue("seed " + seed + " goes to " + receiver, receiver == grid.index(5, 4) || receiver == grid.index(4, 5));
east += receiver == grid.index(5, 4) ? 1 : 0;
}
assertTrue("east wins " + east + " of 64 seeds", east >= 16 && east <= 48);
}
private static List<Integer> ties(DemGrid grid, float[] filled) {
List<Integer> ties = new ArrayList<>();
for (int j = 1; j < grid.depth() - 1; j++) {
for (int i = 1; i < grid.width() - 1; i++) {
int cell = grid.index(i, j);
float best = bestSlope(grid, filled, cell);
int sharing = 0;
for (int neighbour : neighbours(grid, cell)) {
sharing += best > 0f && slope(grid, filled, cell, neighbour) == best ? 1 : 0;
}
if (sharing >= 2) {
ties.add(cell);
}
}
}
return ties;
}
private static int firstSteepest(DemGrid grid, float[] filled, int cell) {
float best = bestSlope(grid, filled, cell);
for (int neighbour : neighbours(grid, cell)) {
if (slope(grid, filled, cell, neighbour) == best) {
return neighbour;
}
}
return -1;
}
private static float bestSlope(DemGrid grid, float[] filled, int cell) {
float best = 0f;
for (int neighbour : neighbours(grid, cell)) {
best = Math.max(best, slope(grid, filled, cell, neighbour));
}
return best;
}
private static float slope(DemGrid grid, float[] filled, int cell, int neighbour) {
boolean diagonal = cell % grid.width() != neighbour % grid.width() && cell / grid.width() != neighbour / grid.width();
float drop = filled[cell] - filled[neighbour];
return diagonal ? drop * (float) (1.0D / Math.sqrt(2.0D)) : drop;
}
private static List<Integer> neighbours(DemGrid grid, int cell) {
List<Integer> neighbours = new ArrayList<>(8);
int i = cell % grid.width();
int j = cell / grid.width();
for (int dz = -1; dz <= 1; dz++) {
for (int dx = -1; dx <= 1; dx++) {
if ((dx != 0 || dz != 0) && i + dx >= 0 && i + dx < grid.width() && j + dz >= 0 && j + dz < grid.depth()) {
neighbours.add(grid.index(i + dx, j + dz));
}
}
}
return neighbours;
}
private static boolean chainReachesSeed(int[] receivers, int start, int n) {
@@ -14,7 +14,7 @@ public class FlowAccumulatorTest {
public void outletTotalsEqualTotalWeight() {
DemGrid grid = GridFixtures.random(7L, 40, 36);
float[] filled = PriorityFlood.fill(grid, GridFixtures.edgeSeeds(grid), 1e-3f);
int[] receivers = D8Router.receivers(grid, filled);
int[] receivers = D8Router.receivers(grid, filled, 11L);
Random random = new Random(11L);
float[] weights = new float[receivers.length];
float sumWeights = 0f;
@@ -49,7 +49,7 @@ public class FlowAccumulatorTest {
BitSet seeds = new BitSet();
seeds.set(grid.index(0, 0));
float[] filled = PriorityFlood.fill(grid, seeds, 1e-3f);
int[] receivers = D8Router.receivers(grid, filled);
int[] receivers = D8Router.receivers(grid, filled, 11L);
float[] weights = new float[receivers.length];
Arrays.fill(weights, 1f);
assertEquals(256f, FlowAccumulator.accumulate(receivers, filled, weights, Float.POSITIVE_INFINITY)[grid.index(0, 0)], 0f);
@@ -65,7 +65,7 @@ final class InletTerrain {
float[] filled = PriorityFlood.fill(grid, seeds, 0.001f);
DemGrid surface = new DemGrid(originX, originZ, CELL, width, depth);
System.arraycopy(filled, 0, surface.values(), 0, count);
this.receivers = D8Router.receivers(surface, filled);
this.receivers = D8Router.receivers(surface, filled, SEED);
float[] weights = new float[count];
for (int cell = 0; cell < count; cell++) {
if (samples.ocean()[cell]) {
@@ -736,7 +736,7 @@ public class PondPlannerTest {
private static List<Pond> drainageOrder(Planned planned, int head) {
DemGrid grid = planned.grid();
int[] receivers = D8Router.receivers(grid, planned.filled());
int[] receivers = D8Router.receivers(grid, planned.filled(), SEED);
int[] basinOf = new int[receivers.length];
Arrays.fill(basinOf, -1);
for (int index = 0; index < planned.basins().size(); index++) {
@@ -2960,7 +2960,7 @@ public class ReachShaperTest {
}
DemGrid surface = new DemGrid(originX, originZ, cell, width, depth);
System.arraycopy(filled, 0, surface.values(), 0, count);
int[] receivers = D8Router.receivers(surface, filled);
int[] receivers = D8Router.receivers(surface, filled, settings.seed());
for (int index = 0; index < count; index++) {
if (samples.ocean()[index]) {
receivers[index] = -1;