Junction width flare

- Blend a downstream reach's half-width over its first 16 blocks from the widest feeder end to the width law in ReachShaper.widths
- Move the blend math into the shared LinkBlend.blend(from, to, along) used by both the planner flare and cross-window links
- Measure the cross-window width blend from the junction vertex, so led-in pond outlets blend from their real junction
- Add ReachShaperTest.junctionWidthBlendsOverSixteenBlocks and ChunkWaterRasterTest.ledInOutletWidthBlendsFromItsJunctionAcrossAWindowLine
- Skip reaches whose end lies inside the junction flare in widthFollowsSqrtCatchment
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 18:34:34 -05:00
1 parent 52761e1784
commit b6166bc6fa
5 files changed
+100 -18

No files matched your search

@@ -347,7 +347,7 @@ final class ChunkFeatures {
for (Reach reach : owned) {
feeders.add(cornerBlock(reach) == block ? resolve(reach, link) : reach);
}
return link.equals(owner) ? LinkBlend.clamp(downstream, feeders) : LinkBlend.blend(downstream, feeders);
return link.equals(owner) ? LinkBlend.clamp(downstream, feeders) : LinkBlend.blend(downstream, feeders, junction);
}
private List<Reach> feeders(WindowKey key, long downstreamId) {
@@ -4,13 +4,21 @@ import art.arcane.iris.generation.hydrology.water.Reach;
import java.util.List;
final class LinkBlend {
public final class LinkBlend {
private static final float LENGTH = 16f;
private LinkBlend() {
}
static Reach blend(Reach downstream, List<Reach> feeders) {
public static float blend(float from, float to, float along) {
if (along >= LENGTH) {
return to;
}
float share = Math.max(0f, along) / LENGTH;
return from + (to - from) * share;
}
static Reach blend(Reach downstream, List<Reach> feeders, int junction) {
int end = lowestEnd(feeders);
float width = 0f;
float depth = 0f;
@@ -24,16 +32,14 @@ final class LinkBlend {
int[] grade = clamped(downstream.grade(), end);
float[] halfWidth = downstream.halfWidth().clone();
float[] depths = downstream.depth().clone();
float along = 0f;
float[] along = new float[xs.length];
for (int point = 1; point < xs.length; point++) {
along[point] = along[point - 1] + (float) Math.hypot(xs[point] - xs[point - 1], zs[point] - zs[point - 1]);
}
for (int point = 0; point < xs.length; point++) {
if (point > 0) {
along += (float) Math.hypot(xs[point] - xs[point - 1], zs[point] - zs[point - 1]);
}
if (along <= LENGTH) {
float share = along / LENGTH;
halfWidth[point] = width + (halfWidth[point] - width) * share;
depths[point] = depth + (depths[point] - depth) * share;
}
float fromJunction = along[point] - along[junction];
halfWidth[point] = blend(width, halfWidth[point], fromJunction);
depths[point] = blend(depth, depths[point], fromJunction);
}
return new Reach(downstream.id(), downstream.tier(), downstream.downstreamId(), xs, zs, grade, halfWidth, depths,
downstream.style(), downstream.catchment());
@@ -4,6 +4,7 @@ import art.arcane.iris.generation.hydrology.grid.CellHash;
import art.arcane.iris.generation.hydrology.grid.DemGrid;
import art.arcane.iris.generation.hydrology.grid.HeightLattice;
import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
import art.arcane.iris.generation.hydrology.raster.LinkBlend;
import java.util.ArrayList;
import java.util.Arrays;
@@ -364,10 +365,13 @@ public final class ReachShaper {
}
unfold(draft);
float upstream = 0f;
float feederEnd = 0f;
for (int index : draft.incoming) {
upstream = Math.max(upstream, drafts.get(index).maxHalfWidth);
ReachDraft feeder = drafts.get(index);
upstream = Math.max(upstream, feeder.maxHalfWidth);
feederEnd = Math.max(feederEnd, feeder.halfWidth[feeder.halfWidth.length - 1]);
}
widths(draft, upstream);
widths(draft, upstream, feederEnd);
restyle(draft);
}
@@ -479,16 +483,19 @@ public final class ReachShaper {
}
}
private static void widths(ReachDraft draft, float upstream) {
private static void widths(ReachDraft draft, float upstream, float feederEnd) {
int count = draft.xs.length;
boolean taper = draft.incoming.length == 0 && !draft.outlet;
boolean flare = draft.incoming.length > 0;
boolean taper = !flare && !draft.outlet;
float[] distance = distances(draft.xs, draft.zs);
float running = upstream;
draft.halfWidth = new float[count];
draft.depth = new float[count];
for (int point = 0; point < count; point++) {
float law = draft.tier.halfWidth(draft.catchments[point]);
if (taper) {
if (flare) {
law = LinkBlend.blend(feederEnd, law, distance[point]);
} else if (taper) {
law *= Math.min(1f, distance[point] / TAPER_LENGTH);
}
float lip = draft.held != null && draft.held[point] ? LIP_HALF_WIDTH : 0f;
@@ -676,6 +676,23 @@ public class ChunkWaterRasterTest {
}
}
@Test
public void ledInOutletWidthBlendsFromItsJunctionAcrossAWindowLine() {
HydrologyTier narrow = tier("river", 1f, 0.5f, 4, IrisBankStyle.SMOOTH);
HydrologyTier wide = tier("river", 8f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach outlet = ledIn(line(211L, narrow, 0L, 508f, 200f, 600f, 200f, (x, z) -> 66), 494f, 200f);
Reach feeder = line(212L, wide, outlet.id(), 508f, 120f, 508f, 200f, (x, z) -> 66);
Pond pond = new Pond(210L, PondKind.NATURAL, 66, 58, 496, 200, 67.5f, circle(478, 200, 17, 1), outlet.id(), 0, "water", 478, 200);
assertTrue("the lead-in starts in the pond", pond.mask().contains(494, 200));
assertFalse("the junction lies off the pond", pond.mask().contains(508, 200));
assertEquals(new WindowKey(0, 0), WindowKey.containing(508, 200));
assertEquals(new WindowKey(1, 0), WindowKey.containing(600, 200));
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(outlet).reach(feeder).pond(pond);
assertTrue("the outlet is half the feeder's width 8 blocks below the junction", scene.water(516, 204));
assertTrue("the outlet is still blending 10 blocks below the junction", scene.water(518, 202));
assertFalse("the narrow outlet width returns past the blend", scene.water(530, 202));
}
@Test
public void skippedLeadInKeepsTheOutletJunctionAtItsFirstVertex() {
HydrologyTier river = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
@@ -300,7 +300,7 @@ public class ReachShaperTest {
assertEquals(tier.depth(reach.halfWidth()[index]), reach.depth()[index], 0f);
}
boolean head = !incoming.containsKey(reach.id());
if (head && firstBeyond(reach, 24f) < 0) {
if (firstBeyond(reach, head ? 24f : 16f) < 0) {
continue;
}
float expected = tier.minHalfWidth() + tier.widthFactor() * (float) Math.sqrt(reach.catchment() / 2500f);
@@ -1102,6 +1102,58 @@ public class ReachShaperTest {
assertTrue("points widened past their tier by the waist rule " + widened, widened > 0);
}
@Test
public void junctionWidthBlendsOverSixteenBlocks() {
HydrologySettings settings = settings(config -> config.setTiers(new KList<>(
new IrisHydrologyTier().setHalfWidth(new KList<>(1D, 3D)),
new IrisHydrologyTier().setName("river").setMinCatchment(6000D).setHalfWidth(new KList<>(8D, 12D)))));
DemGrid flat = outletGrid();
int[] receivers = new int[flat.values().length];
Arrays.fill(receivers, -1);
float[] accumulation = new float[receivers.length];
BitSet channels = new BitSet(receivers.length);
for (int i = 0; i < 12; i++) {
flow(flat, receivers, accumulation, channels, i, 8, i + 1, 8, 2000f + 10f * i);
}
for (int j = 0; j < 8; j++) {
flow(flat, receivers, accumulation, channels, 12, j, 12, j + 1, 3500f + 10f * j);
}
for (int i = 12; i < flat.width(); i++) {
flow(flat, receivers, accumulation, channels, i, 8, i + 1 < flat.width() ? i + 1 : -1, 8, 6400f);
}
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(), fine, SEED);
Map<Long, List<Reach>> feeders = feeders(shaped.reaches());
assertEquals("one confluence " + feeders.keySet(), 1, feeders.size());
Map.Entry<Long, List<Reach>> confluence = feeders.entrySet().iterator().next();
assertEquals("two feeders", 2, confluence.getValue().size());
Reach downstream = byId(shaped.reaches()).get(confluence.getKey());
float widest = 0f;
for (Reach feeder : confluence.getValue()) {
assertEquals("feeders are streams", "stream", feeder.tier().name());
widest = Math.max(widest, feeder.halfWidth()[feeder.halfWidth().length - 1]);
}
assertEquals("the merged catchment is a river", "river", downstream.tier().name());
float law = downstream.tier().halfWidth(downstream.catchment());
assertTrue("streams " + widest + " join a river " + law, widest < 0.5f * law);
float along = 0f;
float previous = widest;
int blended = 0;
for (int point = 0; point < downstream.xs().length; point++) {
if (point > 0) {
along += (float) Math.hypot(downstream.xs()[point] - downstream.xs()[point - 1], downstream.zs()[point] - downstream.zs()[point - 1]);
}
float halfWidth = downstream.halfWidth()[point];
float expected = along >= 16f ? law : widest + (law - widest) * (along / 16f);
assertEquals("half-width " + along + " blocks below the junction", expected, halfWidth, 1e-3f);
assertTrue("half-width narrows from " + previous + " to " + halfWidth + " at " + along, halfWidth >= previous - 1e-6f);
assertEquals(downstream.tier().depth(halfWidth), downstream.depth()[point], 0f);
blended += along > 0f && along < 16f ? 1 : 0;
previous = halfWidth;
}
assertTrue("vertices inside the blend " + blended, blended >= 2);
}
@Test
public void reachEndingInOceanOrEdgeHasNoDownstream() {
HydrologySettings settings = settings(config -> {