Outlet review fixes

- Treat an outlet as led in only when its first vertex lies inside its source pond mask, so links and corner blocks keep the first vertex as the junction when the lead-in was skipped
- Hold and lead in each outlet draft once, from the pond the grader uses as its source, through the new ReachGrader.outletPonds
- Let ocean points keep the sea cap on held outlet stretches
- Add tests for a skipped lead-in junction, two ponds sharing an exit, and a held outlet running into the sea
- Plan the openFractal outlet scenes inline and count only the outlets that detachedOutlets checks, through the new WaterInvariants.checkedPonds
- Reuse the WaterInvariants segment geometry helpers in WindowPlannerTest
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 17:29:53 -05:00
1 parent 0dabc54e88
commit 50839c4833
7 files changed
+154 -82

No files matched your search

@@ -295,6 +295,7 @@ final class ChunkFeatures {
private static final class Links { private static final class Links {
private final Map<WindowKey, IndexedWindow> windows; private final Map<WindowKey, IndexedWindow> windows;
private final Map<Long, Integer> outletLevels = new HashMap<>(); private final Map<Long, Integer> outletLevels = new HashMap<>();
private final Map<Long, List<PondMask>> outletMasks = new HashMap<>();
private final Map<Long, Reach> resolved = new HashMap<>(); private final Map<Long, Reach> resolved = new HashMap<>();
private final Set<Long> resolving = new HashSet<>(); private final Set<Long> resolving = new HashSet<>();
@@ -304,6 +305,7 @@ final class ChunkFeatures {
for (Pond pond : window.plan().ponds()) { for (Pond pond : window.plan().ponds()) {
if (pond.outletReachId() != 0L) { if (pond.outletReachId() != 0L) {
outletLevels.merge(pond.outletReachId(), pond.level(), Math::min); outletLevels.merge(pond.outletReachId(), pond.level(), Math::min);
outletMasks.computeIfAbsent(pond.outletReachId(), ignored -> new ArrayList<>()).add(pond.mask());
} }
} }
} }
@@ -354,7 +356,18 @@ final class ChunkFeatures {
} }
private int junction(Reach reach) { private int junction(Reach reach) {
return reach.xs().length > 1 && outletLevels.containsKey(reach.id()) ? 1 : 0; List<PondMask> masks = outletMasks.get(reach.id());
if (masks == null || reach.xs().length < 2) {
return 0;
}
int x = Math.round(reach.xs()[0]);
int z = Math.round(reach.zs()[0]);
for (PondMask mask : masks) {
if (mask.contains(x, z)) {
return 1;
}
}
return 0;
} }
private long cornerBlock(Reach reach) { private long cornerBlock(Reach reach) {
@@ -29,13 +29,7 @@ final class ReachGrader {
|| draft.downstream >= 0 && lakeFloor[draft.downstream] && downstreamFloor >= draft.endLevel; || draft.downstream >= 0 && lakeFloor[draft.downstream] && downstreamFloor >= draft.endLevel;
} }
int[] levels = ponds.levels().clone(); int[] levels = ponds.levels().clone();
int[] outletPond = new int[drafts.size()]; int[] outletPond = outletPonds(ponds.outletDrafts(), drafts.size());
Arrays.fill(outletPond, -1);
for (int pond = 0; pond < levels.length; pond++) {
if (ponds.outletDrafts()[pond] >= 0) {
outletPond[ponds.outletDrafts()[pond]] = pond;
}
}
int[][] inflows = inflows(ponds, levels.length); int[][] inflows = inflows(ponds, levels.length);
int[] fed = new int[drafts.size()]; int[] fed = new int[drafts.size()];
Arrays.fill(fed, Integer.MAX_VALUE); Arrays.fill(fed, Integer.MAX_VALUE);
@@ -58,7 +52,7 @@ final class ReachGrader {
} }
int[] grade = profile(drafts, draft); int[] grade = profile(drafts, draft);
for (int point = 0; point < grade.length; point++) { for (int point = 0; point < grade.length; point++) {
if (draft.held != null && draft.held[point]) { if (draft.held != null && draft.held[point] && !draft.ocean[point]) {
grade[point] = level; grade[point] = level;
continue; continue;
} }
@@ -84,6 +78,17 @@ final class ReachGrader {
return levels; return levels;
} }
static int[] outletPonds(int[] outletDrafts, int drafts) {
int[] outletPond = new int[drafts];
Arrays.fill(outletPond, -1);
for (int pond = 0; pond < outletDrafts.length; pond++) {
if (outletDrafts[pond] >= 0) {
outletPond[outletDrafts[pond]] = pond;
}
}
return outletPond;
}
private static int[][] inflows(PondChain ponds, int count) { private static int[][] inflows(PondChain ponds, int count) {
int[] sizes = new int[count]; int[] sizes = new int[count];
for (int pond : ponds.inflowPond()) { for (int pond : ponds.inflowPond()) {
@@ -61,9 +61,10 @@ public final class ReachShaper {
drafts.get(outlet).outlet = true; drafts.get(outlet).outlet = true;
} }
} }
for (int pond = 0; pond < outletDrafts.length; pond++) { int[] sources = ReachGrader.outletPonds(outletDrafts, drafts.size());
if (leavesThroughALip(ponds.get(pond), outletDrafts[pond])) { for (int index = 0; index < sources.length; index++) {
hold(drafts.get(outletDrafts[pond]), ponds.get(pond).mask()); if (leavesThroughALip(ponds, sources[index])) {
hold(drafts.get(index), ponds.get(sources[index]).mask());
} }
} }
ReachGrader.PondChain chain = pondChain(drafts, ponds, outletDrafts); ReachGrader.PondChain chain = pondChain(drafts, ponds, outletDrafts);
@@ -72,9 +73,9 @@ public final class ReachShaper {
for (int index : order) { for (int index : order) {
finish(drafts, drafts.get(index)); finish(drafts, drafts.get(index));
} }
for (int pond = 0; pond < outletDrafts.length; pond++) { for (int index = 0; index < sources.length; index++) {
if (leavesThroughALip(ponds.get(pond), outletDrafts[pond])) { if (leavesThroughALip(ponds, sources[index])) {
leadIn(drafts.get(outletDrafts[pond]), ponds.get(pond).mask()); leadIn(drafts.get(index), ponds.get(sources[index]).mask());
} }
} }
List<Pond> linked = new ArrayList<>(ponds.size()); List<Pond> linked = new ArrayList<>(ponds.size());
@@ -273,8 +274,8 @@ public final class ReachShaper {
throw new IllegalStateException("Reach and pond links loop through " + (count - size) + " of " + count + " reaches"); throw new IllegalStateException("Reach and pond links loop through " + (count - size) + " of " + count + " reaches");
} }
private static boolean leavesThroughALip(Pond pond, int outletDraft) { private static boolean leavesThroughALip(List<Pond> ponds, int source) {
return outletDraft >= 0 && pond.kind() != PondKind.LAKE; return source >= 0 && ponds.get(source).kind() != PondKind.LAKE;
} }
private static void hold(ReachDraft draft, PondMask mask) { private static void hold(ReachDraft draft, PondMask mask) {
@@ -29,11 +29,8 @@ final class WaterInvariants {
Map<Long, Reach> reaches = byId(scene.reaches()); Map<Long, Reach> reaches = byId(scene.reaches());
int seaLevel = scene.settings().seaLevel(); int seaLevel = scene.settings().seaLevel();
List<Long> detached = new ArrayList<>(); List<Long> detached = new ArrayList<>();
for (Pond pond : scene.ponds()) { for (Pond pond : checkedPonds(scene)) {
Reach outlet = pond.kind() == PondKind.LAKE ? null : reaches.get(pond.outletReachId()); Reach outlet = reaches.get(pond.outletReachId());
if (outlet == null || nearEdge(scene, outletBox(pond.mask(), outlet))) {
continue;
}
if (!floodsIntoOutlet(scene, pond, outlet, WaterHeads.snap(pond.level(), seaLevel))) { if (!floodsIntoOutlet(scene, pond, outlet, WaterHeads.snap(pond.level(), seaLevel))) {
detached.add(outlet.id()); detached.add(outlet.id());
} }
@@ -41,6 +38,18 @@ final class WaterInvariants {
return detached; return detached;
} }
static List<Pond> checkedPonds(PlannedScene scene) {
Map<Long, Reach> reaches = byId(scene.reaches());
List<Pond> checked = new ArrayList<>();
for (Pond pond : scene.ponds()) {
Reach outlet = pond.kind() == PondKind.LAKE ? null : reaches.get(pond.outletReachId());
if (outlet != null && !nearEdge(scene, outletBox(pond.mask(), outlet))) {
checked.add(pond);
}
}
return checked;
}
static List<Long> confluenceTongues(PlannedScene scene) { static List<Long> confluenceTongues(PlannedScene scene) {
Map<Long, Reach> reaches = byId(scene.reaches()); Map<Long, Reach> reaches = byId(scene.reaches());
Map<Long, List<Reach>> feeders = feeders(scene.reaches(), reaches); Map<Long, List<Reach>> feeders = feeders(scene.reaches(), reaches);
@@ -73,6 +82,25 @@ final class WaterInvariants {
return List.copyOf(features); return List.copyOf(features);
} }
static double along(Reach reach, int segment, double x, double z) {
double ax = reach.xs()[segment];
double az = reach.zs()[segment];
double dx = reach.xs()[segment + 1] - ax;
double dz = reach.zs()[segment + 1] - az;
double length = dx * dx + dz * dz;
return length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D);
}
static double offset(Reach reach, int segment, double t, double x, double z) {
double px = reach.xs()[segment] + t * (reach.xs()[segment + 1] - reach.xs()[segment]);
double pz = reach.zs()[segment] + t * (reach.zs()[segment + 1] - reach.zs()[segment]);
return Math.hypot(x - px, z - pz);
}
static double width(Reach reach, int segment, double t) {
return reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]);
}
private static boolean floodsIntoOutlet(PlannedScene scene, Pond pond, Reach outlet, int head) { private static boolean floodsIntoOutlet(PlannedScene scene, Pond pond, Reach outlet, int head) {
int[] box = maskBox(pond.mask()); int[] box = maskBox(pond.mask());
Set<Long> seen = new HashSet<>(); Set<Long> seen = new HashSet<>();
@@ -219,25 +247,6 @@ final class WaterInvariants {
return false; return false;
} }
private static double along(Reach reach, int segment, double x, double z) {
double ax = reach.xs()[segment];
double az = reach.zs()[segment];
double dx = reach.xs()[segment + 1] - ax;
double dz = reach.zs()[segment + 1] - az;
double length = dx * dx + dz * dz;
return length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D);
}
private static double offset(Reach reach, int segment, double t, double x, double z) {
double px = reach.xs()[segment] + t * (reach.xs()[segment + 1] - reach.xs()[segment]);
double pz = reach.zs()[segment] + t * (reach.zs()[segment + 1] - reach.zs()[segment]);
return Math.hypot(x - px, z - pz);
}
private static double width(Reach reach, int segment, double t) {
return reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]);
}
private static Map<Long, Reach> byId(List<Reach> reaches) { private static Map<Long, Reach> byId(List<Reach> reaches) {
Map<Long, Reach> byId = new HashMap<>(reaches.size() * 2); Map<Long, Reach> byId = new HashMap<>(reaches.size() * 2);
for (Reach reach : reaches) { for (Reach reach : reaches) {
@@ -14,7 +14,6 @@ import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ReachShaper; import art.arcane.iris.generation.hydrology.water.ReachShaper;
import art.arcane.iris.generation.hydrology.water.ReachStyle; import art.arcane.iris.generation.hydrology.water.ReachStyle;
import art.arcane.iris.generation.hydrology.water.ShapedNetwork; import art.arcane.iris.generation.hydrology.water.ShapedNetwork;
import org.junit.AfterClass;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
@@ -46,13 +45,6 @@ public class WindowPlannerTest {
private static final WindowKey ORIGIN = new WindowKey(0, 0); private static final WindowKey ORIGIN = new WindowKey(0, 0);
private static final WindowKey LAND = new WindowKey(4, 4); private static final WindowKey LAND = new WindowKey(4, 4);
private static final int SEAM_DISTANCE = 256; private static final int SEAM_DISTANCE = 256;
private static final Map<Long, PlannedScene> OPEN_SCENES = new HashMap<>();
@AfterClass
public static void dropScenes() {
OPEN_SCENES.clear();
}
@Test @Test
public void zeroDensityRemovesLocalChannelsAndPonds() { public void zeroDensityRemovesLocalChannelsAndPonds() {
HydrologySettings settings = settings(new IrisHydrology().setDensity(0D)); HydrologySettings settings = settings(new IrisHydrology().setDensity(0D));
@@ -631,20 +623,13 @@ public class WindowPlannerTest {
public void everyPondOutletLeavesThroughConnectedWater() { public void everyPondOutletLeavesThroughConnectedWater() {
int outlets = 0; int outlets = 0;
for (long seed = 1L; seed <= 7L; seed++) { for (long seed = 1L; seed <= 7L; seed++) {
PlannedScene scene = openScene(seed); PlannedScene scene = PlannedScene.plan(SyntheticTerrain.openFractal(seed), PlannedScene.packSettings(SEED), 1, 1);
assertEquals("detached outlets on openFractal(" + seed + ")", List.of(), WaterInvariants.detachedOutlets(scene)); assertEquals("detached outlets on openFractal(" + seed + ")", List.of(), WaterInvariants.detachedOutlets(scene));
Map<Long, Reach> byId = byId(scene.reaches()); outlets += WaterInvariants.checkedPonds(scene).size();
for (Pond pond : scene.ponds()) {
outlets += pond.kind() != PondKind.LAKE && byId.containsKey(pond.outletReachId()) ? 1 : 0;
}
} }
assertTrue("pond outlets checked " + outlets, outlets >= 50); assertTrue("pond outlets checked " + outlets, outlets >= 50);
} }
private static synchronized PlannedScene openScene(long seed) {
return OPEN_SCENES.computeIfAbsent(seed, key -> PlannedScene.plan(SyntheticTerrain.openFractal(key), PlannedScene.packSettings(SEED), 1, 1));
}
private static void assertOutletLeavesAtThePondHead(Pond pond, Reach outlet) { private static void assertOutletLeavesAtThePondHead(Pond pond, Reach outlet) {
PondMask mask = pond.mask(); PondMask mask = pond.mask();
int leading = 0; int leading = 0;
@@ -695,9 +680,9 @@ public class WindowPlannerTest {
double nearest = Double.POSITIVE_INFINITY; double nearest = Double.POSITIVE_INFINITY;
int[] found = {-1, -1}; int[] found = {-1, -1};
for (int segment = 0; segment + 1 < reach.xs().length; segment++) { for (int segment = 0; segment + 1 < reach.xs().length; segment++) {
double t = along(reach, segment, x, z); double t = WaterInvariants.along(reach, segment, x, z);
double distance = offset(reach, segment, t, x, z); double distance = WaterInvariants.offset(reach, segment, t, x, z);
if (distance <= width(reach, segment, t) && distance < nearest) { if (distance <= WaterInvariants.width(reach, segment, t) && distance < nearest) {
nearest = distance; nearest = distance;
found[0] = segment; found[0] = segment;
found[1] = t < 0.5D ? segment : segment + 1; found[1] = t < 0.5D ? segment : segment + 1;
@@ -707,27 +692,8 @@ public class WindowPlannerTest {
} }
private static boolean covers(Reach reach, int segment, int x, int z) { private static boolean covers(Reach reach, int segment, int x, int z) {
double t = along(reach, segment, x, z); double t = WaterInvariants.along(reach, segment, x, z);
return offset(reach, segment, t, x, z) <= width(reach, segment, t); return WaterInvariants.offset(reach, segment, t, x, z) <= WaterInvariants.width(reach, segment, t);
}
private static double along(Reach reach, int segment, double x, double z) {
double ax = reach.xs()[segment];
double az = reach.zs()[segment];
double dx = reach.xs()[segment + 1] - ax;
double dz = reach.zs()[segment + 1] - az;
double length = dx * dx + dz * dz;
return length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D);
}
private static double offset(Reach reach, int segment, double t, double x, double z) {
double px = reach.xs()[segment] + t * (reach.xs()[segment + 1] - reach.xs()[segment]);
double pz = reach.zs()[segment] + t * (reach.zs()[segment + 1] - reach.zs()[segment]);
return Math.hypot(x - px, z - pz);
}
private static double width(Reach reach, int segment, double t) {
return reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]);
} }
private static boolean nearMask(PondMask mask, float x, float z, int distance) { private static boolean nearMask(PondMask mask, float x, float z, int distance) {
@@ -676,6 +676,25 @@ public class ChunkWaterRasterTest {
} }
} }
@Test
public void skippedLeadInKeepsTheOutletJunctionAtItsFirstVertex() {
HydrologyTier river = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach outlet = line(201L, river, 0L, 255f, 200f, 335f, 200f, (x, z) -> 68);
Reach feeder = line(202L, river, outlet.id(), 255f, 140f, 255f, 200f, (x, z) -> 66);
Reach upstream = line(203L, river, feeder.id(), 255f, 60f, 255f, 140f, (x, z) -> z < 100f ? 70 : 62);
Pond pond = new Pond(200L, PondKind.NATURAL, 68, 60, 235, 200, 69.5f, circle(235, 200, 8, 1), outlet.id(), 0, "water", 235, 200);
assertFalse("the outlet starts off the pond", pond.mask().contains(255, 200));
assertNotEquals("the first two outlet vertices straddle a corner-block line",
Math.floorDiv((int) Math.floor(outlet.xs()[0]) + WindowKey.SIZE / 2, WindowKey.SIZE),
Math.floorDiv((int) Math.floor(outlet.xs()[1]) + WindowKey.SIZE / 2, WindowKey.SIZE));
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(outlet).reach(feeder).reach(upstream).pond(pond);
for (int x = 260; x <= 335; x++) {
int head = scene.head(x, 200);
assertNotEquals("centre line is water at x=" + x, Integer.MIN_VALUE, head);
assertTrue("outlet water at " + head + " above the resolved feeder end 62 at x=" + x, head <= 62);
}
}
@Test @Test
public void everyBankStyleRasterizes() { public void everyBankStyleRasterizes() {
IrisHydrology config = new IrisHydrology(); IrisHydrology config = new IrisHydrology();
@@ -414,6 +414,65 @@ public class ReachShaperTest {
assertEquals(99, PondChannelSettlement.settle(shaped, SEA_LEVEL).ponds().get(0).level()); assertEquals(99, PondChannelSettlement.settle(shaped, SEA_LEVEL).ponds().get(0).level());
} }
@Test
public void pondsSharingAnExitLeadInOnceFromTheirGradingPond() {
HydrologySettings settings = settings(config -> {
});
DemGrid flat = outletGrid();
int[] receivers = new int[flat.values().length];
float[] accumulation = new float[receivers.length];
BitSet channels = new BitSet(receivers.length);
outletRow(flat, receivers, accumulation, channels);
receivers[flat.index(13, 7)] = flat.index(13, 8);
accumulation[flat.index(13, 7)] = 100f;
Pond fed = new Pond(61L, PondKind.NATURAL, 99, 94, 104, 64, 100f, disk(96, 64, 5), 0L, 0, "water", 96, 64);
Pond side = new Pond(62L, PondKind.NATURAL, 99, 94, 104, 60, 100f, disk(104, 56, 3), 0L, 0, "water", 104, 56);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> fed.mask().contains((int) x, (int) z)
|| side.mask().contains((int) x, (int) z) ? 94D : 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(fed, side), fine, SEED);
assertEquals("both ponds drain through the same exit", shaped.ponds().get(0).outletReachId(), shaped.ponds().get(1).outletReachId());
Reach outlet = byId(shaped.reaches()).get(shaped.ponds().get(1).outletReachId());
assertNotNull(outlet);
assertEquals("the exit stays the junction vertex", 104f, outlet.xs()[1], 0f);
assertEquals(64f, outlet.zs()[1], 0f);
assertTrue("one lead-in from the grading pond " + outlet.xs()[0] + "," + outlet.zs()[0],
side.mask().contains(Math.round(outlet.xs()[0]), Math.round(outlet.zs()[0])));
assertEquals(99, outlet.grade()[0]);
}
@Test
public void heldOutletPointsOverTheSeaStayAtTheSeaSurface() {
HydrologySettings settings = settings(config -> {
});
int width = 24;
DemGrid surface = new DemGrid(0, 0, CELL, width, 3);
Arrays.fill(surface.values(), 60f);
int[] receivers = new int[width * 3];
Arrays.fill(receivers, -1);
float[] accumulation = new float[width * 3];
BitSet channels = new BitSet(width * 3);
for (int i = 0; i < 19; i++) {
flow(surface, receivers, accumulation, channels, i, 1, i + 1, 1, 1600f + 10f * i);
}
surface.set(19, 1, 30f);
Pond pond = new Pond(71L, PondKind.NATURAL, 59, 54, 136, 8, 60f, disk(128, 8, 7), 0L, 0, "water", 128, 8);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> x < 138D ? 60D : 35D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(surface, receivers, accumulation, channels, List.of(pond), fine, SEED);
Pond shapedPond = shaped.ponds().get(0);
Reach outlet = byId(shaped.reaches()).get(shapedPond.outletReachId());
assertNotNull(outlet);
assertTrue(shapedPond.mask().contains(Math.round(outlet.xs()[0]), Math.round(outlet.zs()[0])));
assertEquals("the lip holds the pond level", shapedPond.level(), outlet.grade()[0]);
int mouths = 0;
for (int point = 0; point < outlet.grade().length; point++) {
if (outlet.style()[point] == ReachStyle.MOUTH) {
mouths++;
assertEquals("ocean point " + point + " at " + outlet.xs()[point], SEA_LEVEL, outlet.grade()[point]);
}
}
assertTrue("ocean points " + mouths, mouths > 1);
}
@Test @Test
public void gorgeCutWhenRidgeMissedByGrid() { public void gorgeCutWhenRidgeMissedByGrid() {
HydrologySettings settings = settings(config -> { HydrologySettings settings = settings(config -> {