Organic steep channels

- Add Taubin relaxation of the resampled D8 line, with a 4-block shift limit and 4-block re-spacing
- Scale meander amplitude and wavelength by half-width and keep 40 percent of it on steep ground
- Use a smoothstep end envelope so short reaches keep their curve with exact endpoints
- Soften Viterbi offsets by half a node and pull back where the neighbouring node is blocked
- Move meander and Viterbi offsets along the relaxed line's normals so meander cannot tilt the valley read
- Hold meander to zero where a pond lies within its swing, so outlets stay on the shore
- Add steep-channel, reduced-meander, blocked-node and translation guard tests
- Revise fixture assumptions that required straight lines or one station phase
This commit is contained in:
Brian Fopiano committed 2026-11-02 19:01:52 -05:00
1 parent 1f581164a2
commit 5450fa1ef1
7 files changed
+461 -51

No files matched your search

@@ -25,7 +25,7 @@ public class ConfluenceIncisionTest {
private static final double WALL_RUN = 15D;
private static final double WALL_HEIGHT = 60D;
private static final double WALL_FACE_REACH = 24D;
private static final double MAX_WALL_CUT = 29D;
private static final double MAX_WALL_CUT = 35D;
private static final int MAX_COPY_DIFFERENCE = 4;
private static PlannedScene valley;
@@ -66,30 +66,32 @@ public class ConfluenceIncisionTest {
@Test
public void majorDownAWallBesideTheValleyRiverFollowsTheWall() {
SyntheticTerrain terrain = SyntheticTerrain.valleyBelowAWall(WALL_FOOT, WALL_RUN, WALL_HEIGHT);
HydrologySettings settings = settings();
PlannedScene scene = PlannedScene.plan(terrain, settings, 2, 2);
List<Reach> face = new ArrayList<>();
for (Reach reach : scene.reaches()) {
if (reach.tier().equals(settings.majorTier()) && onWallFace(reach)) {
face.add(reach);
for (double foot = WALL_FOOT - 4D; foot <= WALL_FOOT + 4D; foot += 1D) {
SyntheticTerrain terrain = SyntheticTerrain.valleyBelowAWall(foot, WALL_RUN, WALL_HEIGHT);
PlannedScene scene = PlannedScene.plan(terrain, settings, 2, 2);
List<Reach> face = new ArrayList<>();
for (Reach reach : scene.reaches()) {
if (reach.tier().equals(settings.majorTier()) && onWallFace(reach, foot)) {
face.add(reach);
}
}
assertEquals("foot " + foot + " majors running down the wall face " + face, 1, face.size());
Reach major = face.get(0);
double cut = 0D;
int largest = 0;
for (int point = 0; point < major.grade().length; point++) {
if (!inPond(scene.ponds(), major.xs()[point], major.zs()[point])) {
cut = Math.max(cut, ground(scene, major, point) - major.grade()[point]);
}
if (point > 0) {
largest = Math.max(largest, major.grade()[point - 1] - major.grade()[point]);
}
}
assertTrue("foot " + foot + " " + major + " is cut " + cut + " into the wall", cut <= MAX_WALL_CUT);
double tallestFall = WALL_HEIGHT / 2D + major.tier().waterfallMinimumDrop();
assertTrue("foot " + foot + " " + major + " falls " + largest + " blocks at once down a " + WALL_HEIGHT + "-block wall", largest <= tallestFall);
}
assertEquals("majors running down the wall face " + face, 1, face.size());
Reach major = face.get(0);
double cut = 0D;
int largest = 0;
for (int point = 0; point < major.grade().length; point++) {
if (!inPond(scene.ponds(), major.xs()[point], major.zs()[point])) {
cut = Math.max(cut, ground(scene, major, point) - major.grade()[point]);
}
if (point > 0) {
largest = Math.max(largest, major.grade()[point - 1] - major.grade()[point]);
}
}
assertTrue(major + " is cut " + cut + " into the wall", cut <= MAX_WALL_CUT);
double tallestFall = WALL_HEIGHT / 2D + major.tier().waterfallMinimumDrop();
assertTrue(major + " falls " + largest + " blocks at once down a " + WALL_HEIGHT + "-block wall", largest <= tallestFall);
}
@Test
@@ -138,9 +140,9 @@ public class ConfluenceIncisionTest {
return HydrologySettings.compile(new IrisHydrology(), SEED, SyntheticTerrain.SEA_LEVEL, SyntheticTerrain.MIN_Y, SyntheticTerrain.MAX_Y);
}
private static boolean onWallFace(Reach reach) {
double top = SyntheticTerrain.VALLEY_AXIS - WALL_FOOT - WALL_RUN;
double foot = SyntheticTerrain.VALLEY_AXIS - WALL_FOOT;
private static boolean onWallFace(Reach reach, double wallFoot) {
double top = SyntheticTerrain.VALLEY_AXIS - wallFoot - WALL_RUN;
double foot = SyntheticTerrain.VALLEY_AXIS - wallFoot;
for (int point = 0; point < reach.xs().length; point++) {
double z = reach.zs()[point];
if (Math.abs(reach.xs()[point] - SyntheticTerrain.VALLEY_AXIS) < WALL_FACE_REACH && z > top && z < foot) {
@@ -180,7 +180,7 @@ public class WindowPlannerTest {
float x = reach.xs()[point];
float z = reach.zs()[point];
if (z >= 1900f && z <= 2150f && x >= 3240f && x <= 3380f) {
assertEquals("tributary trunk at z=" + z, floor, x, WindowPlanner.CELL);
assertEquals("tributary trunk at z=" + z, floor, x, 2 * WindowPlanner.CELL);
checked++;
}
}
@@ -971,7 +971,7 @@ public class WindowPlannerTest {
}
int away = 0;
for (Pond pond : dry.ponds()) {
if (Math.abs(pond.floorX() - stripX) <= SLIVER_REACH) {
if (Math.abs(pond.floorX() - stripX) <= SLIVER_REACH || touchedByAChangedReach(pond, dry, scene)) {
continue;
}
away++;
@@ -986,6 +986,21 @@ public class WindowPlannerTest {
}
}
private static boolean touchedByAChangedReach(Pond pond, PlannedScene dry, PlannedScene scene) {
Set<Reach> unchanged = new HashSet<>(scene.reaches());
for (Reach reach : dry.reaches()) {
if (unchanged.contains(reach)) {
continue;
}
for (int point = 0; point < reach.xs().length; point++) {
if (pond.mask().nearest(reach.xs()[point], reach.zs()[point], reach.halfWidth()[point] + 2f) != null) {
return true;
}
}
}
return false;
}
private static int deepCuts(PlannedScene scene) {
int deep = 0;
for (Reach reach : scene.reaches()) {
@@ -1332,7 +1332,11 @@ public class ChunkWaterRasterTest {
assertTrue("delta water beyond the estuary " + added, added > 100);
assertTrue("river water above the sea next to the branch " + raised, !raisedAboveTheEstuary || raised > 0);
assertEquals("delta leaks", 0, delta.leaks(minX, minZ, maxX, maxZ));
Set<Long> reached = wetFrom(delta, Math.round(river.xs()[estuary]) - 16, Math.round(river.zs()[estuary]), minX, minZ, maxX, maxZ);
int above = estuary;
while (above > 0 && river.xs()[estuary] - river.xs()[above] < 16f) {
above--;
}
Set<Long> reached = wetFrom(delta, Math.round(river.xs()[above]), Math.round(river.zs()[above]), minX, minZ, maxX, maxZ);
for (Reach channel : fan) {
for (int point = 0; point < channel.xs().length; point++) {
int x = Math.round(channel.xs()[point]);
@@ -0,0 +1,127 @@
package art.arcane.iris.generation.hydrology.water;
import art.arcane.iris.generation.hydrology.config.IrisHydrology;
import art.arcane.iris.generation.hydrology.grid.DemGrid;
import art.arcane.iris.generation.hydrology.grid.HeightLattice;
import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
import art.arcane.iris.generation.hydrology.grid.HydrologyTerrainSource;
import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.function.DoubleBinaryOperator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ReachFitterTest {
private static final long SEED = 1337L;
private static final int SEA_LEVEL = 40;
private static final int MIN_Y = -64;
private static final int MAX_Y = 320;
private static final int CELL = 8;
private static final int FINE = 4;
private static final int CHAIN = 24;
private static final int CHAIN_ROW = 16;
private static final float CATCHMENT = 2000f;
private static final byte ALLOW_ALL = HydrologySampleBuffer.ALLOW_STREAMS | HydrologySampleBuffer.ALLOW_PONDS;
@Test
public void meanderSurvivesSteepSlopesAtReducedAmplitude() {
HydrologyTier stream = HydrologySettings.compile(new IrisHydrology(), SEED, SEA_LEVEL, MIN_Y, MAX_Y).tiers().get(0);
for (long shapeSeed = 11L; shapeSeed < 19L; shapeSeed++) {
float chainZ = CHAIN_ROW * CELL;
float steep = deviation(fit((x, z) -> 300D - 0.5D * x, stream, shapeSeed), chainZ);
float gentle = deviation(fit((x, z) -> 300D - 0.02D * x, stream, shapeSeed), chainZ);
String metric = "seed " + shapeSeed + " steep " + steep + " gentle " + gentle;
assertTrue(metric, steep >= 1f);
assertTrue(metric, gentle > steep);
}
}
@Test
public void softenedOffsetsStayOffABlockedNode() {
HydrologyTier stream = HydrologySettings.compile(new IrisHydrology(), SEED, SEA_LEVEL, MIN_Y, MAX_Y).tiers().get(0);
int chainZ = CHAIN_ROW * CELL;
int blockedX = 96;
DoubleBinaryOperator trench = (x, z) -> 200D - 0.02D * x - (x > blockedX && x < blockedX + 40 && Math.abs(z - chainZ - 4D) <= 2D ? 3D : 0D);
PondMask mask = new PondMask(blockedX, chainZ + FINE, FINE, 1, 1, new long[]{1L});
Pond pond = new Pond(5L, PondKind.NATURAL, 150, 149, blockedX, chainZ + FINE, 151f, mask, 0L, 0, "water", blockedX, chainZ + FINE);
ReachFitter.Fitted fitted = fit(trench, stream, 11L, List.of(pond));
int checked = 0;
boolean entered = false;
for (int point = 0; point < fitted.xs().length; point++) {
if (Math.abs(fitted.xs()[point] - blockedX) < 0.5f) {
checked++;
assertEquals("the point beside the blocked node stays on its own node", chainZ, fitted.zs()[point], 1e-3f);
}
entered |= fitted.xs()[point] > blockedX + 8 && fitted.xs()[point] < blockedX + 32 && fitted.zs()[point] > chainZ + 2f;
}
assertEquals(1, checked);
assertTrue("the line takes the trench just past the blocked node", entered);
}
private static float deviation(ReachFitter.Fitted fitted, float chainZ) {
int last = fitted.xs().length - 1;
assertEquals(0f, fitted.xs()[0], 0f);
assertEquals(chainZ, fitted.zs()[0], 0f);
assertEquals((CHAIN - 1) * CELL, fitted.xs()[last], 0f);
assertEquals(chainZ, fitted.zs()[last], 0f);
float deviation = 0f;
for (int point = 1; point < last; point++) {
deviation = Math.max(deviation, Math.abs(fitted.zs()[point] - chainZ));
}
return deviation;
}
private static ReachFitter.Fitted fit(DoubleBinaryOperator height, HydrologyTier tier, long shapeSeed) {
return fit(height, tier, shapeSeed, List.of());
}
private static ReachFitter.Fitted fit(DoubleBinaryOperator height, HydrologyTier tier, long shapeSeed, List<Pond> ponds) {
DemGrid grid = new DemGrid(0, 0, CELL, CHAIN, 2 * CHAIN_ROW + 1);
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(i * CELL, j * CELL);
}
}
int[] cells = new int[CHAIN];
for (int i = 0; i < CHAIN; i++) {
cells[i] = grid.index(i, CHAIN_ROW);
}
float[] catchment = new float[CHAIN];
Arrays.fill(catchment, CATCHMENT);
HeightLattice fine = new HeightLattice(new Terrain(height), FINE, false, 1 << 20);
return new ReachFitter(fine, new PondLookup(ponds)).fit(grid, cells, catchment, tier, shapeSeed, false);
}
private record Terrain(DoubleBinaryOperator height) implements HydrologyTerrainSource {
@Override
public void sample(int[] xs, int[] zs, HydrologySampleBuffer out, boolean baseOnly) {
for (int index = 0; index < xs.length; index++) {
out.height()[index] = (float) height.applyAsDouble(xs[index], zs[index]);
out.ocean()[index] = false;
out.rainfall()[index] = 1f;
out.pondDensity()[index] = 1f;
out.permission()[index] = ALLOW_ALL;
out.fluidProfile()[index] = null;
}
}
@Override
public int seaLevel() {
return SEA_LEVEL;
}
@Override
public int minY() {
return MIN_Y;
}
@Override
public int maxY() {
return MAX_Y;
}
}
}
@@ -193,8 +193,8 @@ public class ReachShaperTest {
for (int point = 0; point < tributary.xs().length; point++) {
if (tributary.style()[point] == ReachStyle.WATERFALL) {
waterfalls++;
double edge = ConfluenceGrader.edgeDistance(receiving, tributary.xs()[point], tributary.zs()[point]);
assertTrue(tributary + " falls at point " + point + " inside the receiving channel, " + edge + " from its edge", edge > 0D);
double edge = ConfluenceGrader.edgeDistance(receiving, tributary.xs()[point - 1], tributary.zs()[point - 1]);
assertTrue(tributary + " brink before point " + point + " is inside the receiving channel, " + edge + " from its edge", edge > 0D);
}
}
assertEquals(tributary + " waterfalls", 1, waterfalls);
@@ -408,6 +408,120 @@ public class ReachShaperTest {
assertEquals("a step below the waterfall minimum reads the same both ways", before, mirroredBefore, 1e-3f);
}
@Test
public void steepPlanarChannelsCurveWithoutLongStraightRunsOrKinks() {
HydrologySettings settings = settings(config -> {
});
double east = Math.cos(Math.toRadians(30D));
double south = Math.sin(Math.toRadians(30D));
DoubleBinaryOperator terrain = (x, z) -> 300D - 0.6D * (x * east + z * south) + 1.5D * valueNoise(SEED, x / 24D, z / 24D);
ShapedNetwork shaped = shape(settings, network(settings, terrain, 64, 64, 2000f, false));
int measured = 0;
double longestRun = 0D;
double sharpest = 0D;
for (Reach reach : shaped.reaches()) {
assertNoFold("steep", reach);
for (int index = 0; index < reach.xs().length; index++) {
double natural = Math.max(SEA_LEVEL, terrain.applyAsDouble(reach.xs()[index], reach.zs()[index]));
assertTrue(reach + " at " + index + " grade " + reach.grade()[index] + " natural " + natural, reach.grade()[index] <= natural);
}
float[] distance = ReachShaper.distances(reach.xs(), reach.zs());
if (distance[distance.length - 1] < 64f) {
continue;
}
measured++;
double[] turns = turnsPerStation(reach, distance, 2 * STATION);
double run = 0D;
for (double turn : turns) {
sharpest = Math.max(sharpest, turn);
run = turn < 1.5D ? run + STATION : 0D;
longestRun = Math.max(longestRun, run);
}
}
String metric = "reaches " + measured + " longest straight run " + longestRun + " sharpest turn " + sharpest;
assertTrue(metric, measured >= 5);
assertTrue(metric, longestRun <= 40D);
assertTrue(metric, sharpest <= 30D);
}
private static double[] turnsPerStation(Reach reach, float[] distance, float excluded) {
double length = distance[distance.length - 1] - 2D * excluded;
int stations = (int) Math.floor(length / STATION) + 1;
double[] x = new double[stations];
double[] z = new double[stations];
int segment = 0;
for (int station = 0; station < stations; station++) {
double along = excluded + station * (double) STATION;
while (segment < distance.length - 2 && distance[segment + 1] < along) {
segment++;
}
double t = (along - distance[segment]) / Math.max(1e-6D, distance[segment + 1] - distance[segment]);
x[station] = lerp(reach.xs()[segment], reach.xs()[segment + 1], t);
z[station] = lerp(reach.zs()[segment], reach.zs()[segment + 1], t);
}
double[] turns = new double[Math.max(0, stations - 2)];
for (int station = 0; station < turns.length; station++) {
double first = Math.atan2(z[station + 1] - z[station], x[station + 1] - x[station]);
double second = Math.atan2(z[station + 2] - z[station + 1], x[station + 2] - x[station + 1]);
turns[station] = Math.abs(Math.toDegrees(Math.IEEEremainder(second - first, 2D * Math.PI)));
}
return turns;
}
@Test
public void shapeIsTranslationInvariant() {
HydrologySettings settings = settings(config -> {
});
int shift = 512;
DoubleBinaryOperator eastward = (x, z) -> 240D - 0.3D * z + 16D * Math.abs(Math.sin(Math.PI * (x + 12D * Math.sin(z / 40D)) / 64D));
DoubleBinaryOperator southward = (x, z) -> eastward.applyAsDouble(z, x);
assertInteriorReachesIgnoreTheGridOrigin(shape(settings, network(settings, eastward, 0, 0, 128, 64, CELL, 1200f, false)),
shape(settings, network(settings, eastward, shift, 0, 128, 64, CELL, 1200f, false)), true);
assertInteriorReachesIgnoreTheGridOrigin(shape(settings, network(settings, southward, 0, 0, 64, 128, CELL, 1200f, false)),
shape(settings, network(settings, southward, 0, shift, 64, 128, CELL, 1200f, false)), false);
DoubleBinaryOperator moved = (x, z) -> eastward.applyAsDouble(x - shift, z - shift);
List<Reach> here = shape(settings, network(settings, eastward, 0, 0, 64, 64, CELL, 1200f, false)).reaches();
List<Reach> there = shape(settings, network(settings, moved, shift, shift, 64, 64, CELL, 1200f, false)).reaches();
assertEquals(here.size(), there.size());
assertEquals(reachEnds(here, 0), reachEnds(there, shift));
int bends = 0;
for (int index = 0; index < here.size(); index++) {
bends += Arrays.equals(here.get(index).zs(), offset(there.get(index).zs(), -shift)) ? 0 : 1;
}
assertTrue("meander phases are keyed on world position, reaches that moved " + bends + " of " + here.size(), 2 * bends > here.size());
}
private static void assertInteriorReachesIgnoreTheGridOrigin(ShapedNetwork first, ShapedNetwork second, boolean acrossX) {
Map<Long, Reach> others = byId(second.reaches());
int compared = 0;
for (Reach reach : first.reaches()) {
float[] across = acrossX ? reach.xs() : reach.zs();
if (minimum(across) >= 576f && maximum(across) <= 960f) {
assertEquals(reach, others.get(reach.id()));
compared++;
}
}
assertTrue("reaches compared " + compared, compared >= 5);
}
private static List<String> reachEnds(List<Reach> reaches, int shift) {
List<String> ends = new ArrayList<>();
for (Reach reach : reaches) {
int last = reach.xs().length - 1;
ends.add((reach.xs()[0] - shift) + "," + (reach.zs()[0] - shift) + " to " + (reach.xs()[last] - shift) + "," + (reach.zs()[last] - shift));
}
return ends;
}
private static float[] offset(float[] values, float by) {
float[] moved = values.clone();
for (int index = 0; index < moved.length; index++) {
moved[index] += by;
}
return moved;
}
private static float brinkMidpoint(ShapedNetwork shaped) {
int falls = 0;
float midpoint = Float.NaN;
@@ -947,7 +1061,7 @@ public class ReachShaperTest {
assertTrue(reach + " at " + x + " grade " + reach.grade()[index] + " pool end " + poolEnd, reach.grade()[index] >= poolEnd - 1);
}
if (overPit(x, z, pitX)) {
pooled += reach.grade()[index] > (int) Math.floor(shallow.applyAsDouble(pitX, VALLEY_Z)) ? 1 : 0;
pooled += reach.grade()[index] > (int) Math.floor(shallow.applyAsDouble(pitX, VALLEY_Z)) - reach.tier().inset() ? 1 : 0;
}
}
}
@@ -1432,7 +1546,7 @@ public class ReachShaperTest {
});
int inletX = 204;
DoubleBinaryOperator terrain = (x, z) -> 60D - 0.05D * x + 0.1D * Math.abs(z - VALLEY_Z)
- (Math.abs(x - inletX) <= 2D && Math.abs(z - VALLEY_Z) <= 20D ? 10.3D : 0D);
- (Math.abs(x - inletX) <= 2D && Math.abs(z - VALLEY_Z) <= 20D ? 12.3D : 0D);
ShapedNetwork shaped = shape(settings, network(settings, terrain, 64, 64, VALLEY_THRESHOLD, false));
int crossing = 0;
int below = 0;
@@ -2627,11 +2741,16 @@ public class ReachShaperTest {
private static Network network(HydrologySettings settings, DoubleBinaryOperator height, int originX, int width, int depth,
int cell, float threshold, boolean withPonds) {
return network(settings, height, originX, 0, width, depth, cell, threshold, withPonds);
}
private static Network network(HydrologySettings settings, DoubleBinaryOperator height, int originX, int originZ, int width, int depth,
int cell, float threshold, boolean withPonds) {
Terrain terrain = new Terrain(height);
int count = width * depth;
HydrologySampleBuffer samples = new HydrologySampleBuffer(count);
new HeightLattice(terrain, cell, false, 1 << 20).fill(Math.floorDiv(originX, cell), 0, width, depth, samples);
DemGrid raw = new DemGrid(originX, 0, cell, width, depth);
new HeightLattice(terrain, cell, false, 1 << 20).fill(Math.floorDiv(originX, cell), Math.floorDiv(originZ, cell), width, depth, samples);
DemGrid raw = new DemGrid(originX, originZ, cell, width, depth);
System.arraycopy(samples.height(), 0, raw.values(), 0, count);
BitSet seeds = new BitSet(count);
for (int j = 0; j < depth; j++) {
@@ -2649,7 +2768,7 @@ public class ReachShaperTest {
List<Basin> basins = BasinExtractor.basins(raw, filled, 0.5f, SEA_LEVEL);
ponds = new PondPlanner(settings).ponds(raw, filled, basins, List.of(), fine, CupSet.EMPTY);
}
DemGrid surface = new DemGrid(originX, 0, cell, width, depth);
DemGrid surface = new DemGrid(originX, originZ, cell, width, depth);
System.arraycopy(filled, 0, surface.values(), 0, count);
int[] receivers = D8Router.receivers(surface, filled);
for (int index = 0; index < count; index++) {