Cliff edge review fixes

- Read centreline columns for every land point, so a cliff between lattice nodes lowers the read even where the node gate stays shut
- Close the trim where the channel edge at the next midpoint or the bank is below it, so an angled cliff gains no extra overhang
- Test station phases and fractional edges, angled cliffs and uniform slope 1 directly
- Assert the gate position directly instead of only against a mirror
- Pin the brink for each on-lattice edge instead of a range
This commit is contained in:
Brian Fopiano committed 2026-11-02 19:21:51 -05:00
1 parent 5450fa1ef1
commit 07e4d9cd8b
3 files changed
+168 -32

No files matched your search

@@ -272,8 +272,8 @@ final class ReachFitter {
HydrologySampleBuffer samples = new HydrologySampleBuffer(nodeX.length);
fine.fill(nodeX, nodeZ, samples);
Tangents tangents = tangents(fitted.xs(), fitted.zs());
float[] covered = new float[points];
Arrays.fill(covered, Float.POSITIVE_INFINITY);
float[] ahead = lowestColumnsAhead(fitted);
float[] edgesAhead = lowestEdgesAhead(fitted, tangents, tier);
float drop = tier.waterfallMinimumDrop();
for (int point = 0; point < points; point++) {
float lowest = Float.POSITIVE_INFINITY;
@@ -285,30 +285,33 @@ final class ReachFitter {
lowest = Math.min(lowest, height);
if (!samples.ocean()[node]) {
lowestLand = Math.min(lowestLand, height);
float ahead = (nodeX[node] * step - fitted.xs()[point]) * tangents.x()[point]
float forward = (nodeX[node] * step - fitted.xs()[point]) * tangents.x()[point]
+ (nodeZ[node] * step - fitted.zs()[point]) * tangents.z()[point];
if (ahead <= COVER_AHEAD) {
if (forward <= COVER_AHEAD) {
coveredLand = Math.min(coveredLand, height);
}
}
}
boolean sea = fitted.ocean()[point] || lowestLand == Float.POSITIVE_INFINITY;
fitted.ground()[point] = sea ? lowest : lowestLand;
if (!sea && coveredLand != Float.POSITIVE_INFINITY && coveredLand - lowestLand >= drop) {
covered[point] = coveredLand;
if (fitted.ocean()[point] || lowestLand == Float.POSITIVE_INFINITY) {
fitted.ground()[point] = lowest;
continue;
}
float edge = Math.min(Math.min(coveredLand, ahead[point]), Math.min(edgesAhead[point], fitted.bank()[point]));
if (coveredLand != Float.POSITIVE_INFINITY && edge - lowestLand >= drop) {
fitted.ground()[point] = edge;
} else {
fitted.ground()[point] = lowestLand - ahead[point] >= drop ? ahead[point] : lowestLand;
}
}
standAtCliffEdges(fitted, covered, drop);
}
private void standAtCliffEdges(Fitted fitted, float[] covered, float drop) {
private float[] lowestColumnsAhead(Fitted fitted) {
int points = fitted.xs().length;
int[] offsets = new int[points + 1];
for (int point = 0; point < points; point++) {
offsets[point + 1] = offsets[point] + (covered[point] == Float.POSITIVE_INFINITY ? 0 : columnsToNext(fitted, point));
}
if (offsets[points] == 0) {
return;
int next = Math.min(points - 1, point + 1);
double half = 0.5D * Math.hypot(fitted.xs()[next] - fitted.xs()[point], fitted.zs()[next] - fitted.zs()[point]);
offsets[point + 1] = offsets[point] + 2 + (int) Math.ceil(half);
}
int[] columnX = new int[offsets[points]];
int[] columnZ = new int[offsets[points]];
@@ -316,30 +319,54 @@ final class ReachFitter {
int columns = offsets[point + 1] - offsets[point];
int next = Math.min(points - 1, point + 1);
for (int column = 0; column < columns; column++) {
float share = columns == 1 ? 0f : 0.5f * column / (columns - 1);
float share = 0.5f * column / (columns - 1);
columnX[offsets[point] + column] = (int) Math.floor(lerp(fitted.xs()[point], fitted.xs()[next], share));
columnZ[offsets[point] + column] = (int) Math.floor(lerp(fitted.zs()[point], fitted.zs()[next], share));
}
}
HydrologySampleBuffer samples = new HydrologySampleBuffer(columnX.length);
fine.sampleColumns(columnX, columnZ, samples);
float[] heights = landColumns(columnX, columnZ);
float[] lowest = new float[points];
for (int point = 0; point < points; point++) {
float lowest = covered[point];
lowest[point] = Float.POSITIVE_INFINITY;
for (int column = offsets[point]; column < offsets[point + 1]; column++) {
if (!samples.ocean()[column]) {
int pond = ponds.at(columnX[column], columnZ[column]);
lowest = Math.min(lowest, pond < 0 ? samples.height()[column] : Math.max(samples.height()[column], ponds.pond(pond).level()));
}
}
if (offsets[point + 1] > offsets[point] && lowest - fitted.ground()[point] >= drop) {
fitted.ground()[point] = lowest;
lowest[point] = Math.min(lowest[point], heights[column]);
}
}
return lowest;
}
private static int columnsToNext(Fitted fitted, int point) {
int next = Math.min(fitted.xs().length - 1, point + 1);
return 2 + (int) Math.ceil(0.5D * Math.hypot(fitted.xs()[next] - fitted.xs()[point], fitted.zs()[next] - fitted.zs()[point]));
private float[] lowestEdgesAhead(Fitted fitted, Tangents tangents, HydrologyTier tier) {
int points = fitted.xs().length;
int[] columnX = new int[2 * points];
int[] columnZ = new int[2 * points];
for (int point = 0; point < points; point++) {
int next = Math.min(points - 1, point + 1);
float x = lerp(fitted.xs()[point], fitted.xs()[next], 0.5f);
float z = lerp(fitted.zs()[point], fitted.zs()[next], 0.5f);
float halfWidth = tier.halfWidth(fitted.catchment()[point]);
columnX[2 * point] = (int) Math.floor(x - halfWidth * tangents.z()[point]);
columnZ[2 * point] = (int) Math.floor(z + halfWidth * tangents.x()[point]);
columnX[2 * point + 1] = (int) Math.floor(x + halfWidth * tangents.z()[point]);
columnZ[2 * point + 1] = (int) Math.floor(z - halfWidth * tangents.x()[point]);
}
float[] heights = landColumns(columnX, columnZ);
float[] lowest = new float[points];
for (int point = 0; point < points; point++) {
lowest[point] = Math.min(heights[2 * point], heights[2 * point + 1]);
}
return lowest;
}
private float[] landColumns(int[] columnX, int[] columnZ) {
HydrologySampleBuffer samples = new HydrologySampleBuffer(columnX.length);
fine.sampleColumns(columnX, columnZ, samples);
float[] heights = new float[columnX.length];
for (int column = 0; column < heights.length; column++) {
int pond = ponds.at(columnX[column], columnZ[column]);
float height = pond < 0 ? samples.height()[column] : Math.max(samples.height()[column], ponds.pond(pond).level());
heights[column] = samples.ocean()[column] ? Float.POSITIVE_INFINITY : height;
}
return heights;
}
private static void seaEnd(boolean[] ocean, boolean drainsToTheSea) {
@@ -25,6 +25,7 @@ public class ReachFitterTest {
private static final int CHAIN = 24;
private static final int CHAIN_ROW = 16;
private static final float CATCHMENT = 2000f;
private static final float MAJOR_CATCHMENT = 500000f;
private static final byte ALLOW_ALL = HydrologySampleBuffer.ALLOW_STREAMS | HydrologySampleBuffer.ALLOW_PONDS;
@Test
@@ -62,6 +63,91 @@ public class ReachFitterTest {
assertTrue("the line takes the trench just past the blocked node", entered);
}
@Test
public void uniformSlopesKeepTheFullGroundRead() {
HydrologyTier stream = HydrologySettings.compile(new IrisHydrology(), SEED, SEA_LEVEL, MIN_Y, MAX_Y).tiers().get(0);
double diagonal = Math.sqrt(0.5D);
DoubleBinaryOperator along = (x, z) -> 300D - x;
DoubleBinaryOperator across = (x, z) -> 300D - diagonal * (x + z);
for (long shapeSeed = 11L; shapeSeed < 19L; shapeSeed++) {
for (int origin = 0; origin < FINE; origin++) {
assertFullRead(along, chainFit(along, stream, shapeSeed, origin, CATCHMENT, false));
assertFullRead(across, chainFit(across, stream, shapeSeed, origin, CATCHMENT, true));
}
}
}
@Test
public void cliffsAcrossTheChannelEdgeNeverTrimAboveTheEdgeGround() {
HydrologyTier major = HydrologySettings.compile(new IrisHydrology(), SEED, SEA_LEVEL, MIN_Y, MAX_Y).tiers().get(2);
int trimmed = 0;
for (int degrees : new int[]{0, 15, 30, 45}) {
double east = Math.cos(Math.toRadians(degrees));
double south = Math.sin(Math.toRadians(degrees));
DoubleBinaryOperator cliff = (x, z) -> (x - 96D) * east + (z - CHAIN_ROW * CELL) * south < 0D ? 120D : 108D;
for (int origin = 0; origin < FINE; origin++) {
ReachFitter.Fitted fitted = chainFit(cliff, major, 11L, origin, MAJOR_CATCHMENT, false);
for (int point = 0; point < fitted.xs().length; point++) {
float full = fullRead(cliff, fitted.xs()[point], fitted.zs()[point]);
float ground = fitted.ground()[point];
if (ground > full) {
trimmed++;
String label = degrees + " degrees origin " + origin + " point " + point + " ground " + ground;
assertTrue(label + " bank " + fitted.bank()[point], ground <= fitted.bank()[point]);
assertTrue(label, ground <= lowestEdgeAhead(cliff, fitted, point, major.halfWidth(MAJOR_CATCHMENT)));
}
}
}
}
assertTrue("the fixture must reach the trim", trimmed > 0);
}
private static double lowestEdgeAhead(DoubleBinaryOperator height, ReachFitter.Fitted fitted, int point, float halfWidth) {
int before = Math.max(0, point - 1);
int next = Math.min(fitted.xs().length - 1, point + 1);
double dx = fitted.xs()[next] - fitted.xs()[before];
double dz = fitted.zs()[next] - fitted.zs()[before];
double length = Math.hypot(dx, dz);
double x = (fitted.xs()[point] + fitted.xs()[next]) / 2D;
double z = (fitted.zs()[point] + fitted.zs()[next]) / 2D;
double left = height.applyAsDouble(Math.floor(x - halfWidth * dz / length), Math.floor(z + halfWidth * dx / length));
double right = height.applyAsDouble(Math.floor(x + halfWidth * dz / length), Math.floor(z - halfWidth * dx / length));
return Math.min(left, right);
}
private static void assertFullRead(DoubleBinaryOperator height, ReachFitter.Fitted fitted) {
for (int point = 0; point < fitted.xs().length; point++) {
assertEquals("point " + point, fullRead(height, fitted.xs()[point], fitted.zs()[point]), fitted.ground()[point], 1e-3f);
}
}
private static float fullRead(DoubleBinaryOperator height, float x, float z) {
float lowest = Float.POSITIVE_INFINITY;
for (int nodeX = (int) Math.ceil(x / (double) FINE - 1D); nodeX <= (int) Math.floor(x / (double) FINE + 1D); nodeX++) {
for (int nodeZ = (int) Math.ceil(z / (double) FINE - 1D); nodeZ <= (int) Math.floor(z / (double) FINE + 1D); nodeZ++) {
lowest = Math.min(lowest, (float) height.applyAsDouble(nodeX * FINE, nodeZ * FINE));
}
}
return lowest;
}
private static ReachFitter.Fitted chainFit(DoubleBinaryOperator height, HydrologyTier tier, long shapeSeed, int origin, float catchmentSize, boolean diagonal) {
DemGrid grid = new DemGrid(origin, 0, CELL, CHAIN, 2 * CHAIN);
for (int j = 0; j < grid.depth(); j++) {
for (int i = 0; i < grid.width(); i++) {
grid.values()[grid.index(i, j)] = (float) height.applyAsDouble(origin + i * CELL, j * CELL);
}
}
int[] cells = new int[CHAIN];
for (int i = 0; i < CHAIN; i++) {
cells[i] = grid.index(i, diagonal ? i : CHAIN_ROW);
}
float[] catchment = new float[CHAIN];
Arrays.fill(catchment, catchmentSize);
HeightLattice fine = new HeightLattice(new Terrain(height), FINE, false, 1 << 20);
return new ReachFitter(fine, new PondLookup(List.of())).fit(grid, cells, catchment, tier, shapeSeed, false);
}
private static float deviation(ReachFitter.Fitted fitted, float chainZ) {
int last = fitted.xs().length - 1;
assertEquals(0f, fitted.xs()[0], 0f);
@@ -381,8 +381,9 @@ public class ReachShaperTest {
public void cliffBetweenLatticeNodesKeepsWaterBelowTheGround() {
HydrologySettings settings = settings(config -> {
});
for (int edge = STEP_X - 3; edge <= STEP_X + 3; edge++) {
int cliffX = edge;
int[] brinks = {190, 190, 190, 194, 194, 194, 198};
for (int edge = 0; edge < brinks.length; edge++) {
int cliffX = STEP_X - 3 + edge;
DoubleBinaryOperator cliff = (x, z) -> (x < 2 * CELL ? 140D : x < cliffX ? 120D : 108D) + 0.1D * Math.abs(z - VALLEY_Z);
ShapedNetwork shaped = shape(settings, network(settings, cliff, 64, 64, VALLEY_THRESHOLD, false));
for (Reach reach : shaped.reaches()) {
@@ -392,8 +393,29 @@ public class ReachShaperTest {
+ " natural " + natural, reach.grade()[index] <= natural);
}
}
float brink = brinkMidpoint(shaped);
assertTrue("cliff " + cliffX + " brink midpoint " + brink, brink >= cliffX - 5 && brink <= cliffX);
assertEquals("cliff " + cliffX + " brink midpoint", brinks[edge], brinkMidpoint(shaped), 0.5f);
}
}
@Test
public void cliffAtAnyStationPhaseKeepsWaterBelowTheGround() {
HydrologySettings settings = settings(config -> {
});
for (int origin = 0; origin < FINE; origin++) {
for (double edge = STEP_X - 2D; edge <= STEP_X + 2D; edge += 0.5D) {
double cliffX = edge;
DoubleBinaryOperator cliff = (x, z) -> (x < 2 * CELL ? 140D : x < cliffX ? 120D : 108D) + 0.1D * Math.abs(z - VALLEY_Z);
ShapedNetwork shaped = shape(settings, network(settings, cliff, origin, 64, 64, CELL, VALLEY_THRESHOLD, false));
for (Reach reach : shaped.reaches()) {
for (int index = 0; index < reach.xs().length; index++) {
double natural = cliff.applyAsDouble(reach.xs()[index], reach.zs()[index]);
assertTrue("origin " + origin + " cliff " + cliffX + " " + reach + " at " + index + " x " + reach.xs()[index]
+ " grade " + reach.grade()[index] + " natural " + natural, reach.grade()[index] <= natural);
}
}
float brink = brinkMidpoint(shaped);
assertTrue("origin " + origin + " cliff " + cliffX + " brink midpoint " + brink, brink >= cliffX - 5D && brink <= cliffX);
}
}
}
@@ -405,6 +427,7 @@ public class ReachShaperTest {
DoubleBinaryOperator mirrored = (x, z) -> forward.applyAsDouble(504D - x, z);
float before = STEP_X - firstAtOrBelow(shape(settings, network(settings, forward, 64, 64, VALLEY_THRESHOLD, false)), 113, true);
float mirroredBefore = firstAtOrBelow(shape(settings, network(settings, mirrored, 64, 64, VALLEY_THRESHOLD, false)), 113, false) - (504 - STEP_X);
assertTrue("the full read starts the lower level 5.5 before the step, the trimmed read 1.5: " + before, before >= 4f);
assertEquals("a step below the waterfall minimum reads the same both ways", before, mirroredBefore, 1e-3f);
}