mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
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:
7 files changed
+461
-51
No files matched your search
@@ -47,6 +47,10 @@ final class PondLookup {
|
||||
return ponds.size();
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
return ponds.isEmpty();
|
||||
}
|
||||
|
||||
Pond pond(int index) {
|
||||
return ponds.get(index);
|
||||
}
|
||||
|
||||
@@ -9,8 +9,24 @@ import java.util.Arrays;
|
||||
|
||||
final class ReachFitter {
|
||||
private static final float SPACING = 4f;
|
||||
private static final float MEANDER_AMPLITUDE = 8f;
|
||||
private static final int RELAX_PASSES = 6;
|
||||
private static final float RELAX_MAX_SHIFT = 4f;
|
||||
private static final float RELAX_LAMBDA = 0.5f;
|
||||
private static final float RELAX_MU = -0.53f;
|
||||
private static final double MEANDER_WIDTH_SCALE = 2.5D;
|
||||
private static final double MEANDER_MIN = 2D;
|
||||
private static final double MEANDER_MAX = 16D;
|
||||
private static final double MEANDER_WAVELENGTH_WIDTHS = 12D;
|
||||
private static final double MEANDER_WAVELENGTH_MIN = 24D;
|
||||
private static final double MEANDER_WAVELENGTH_MAX = 192D;
|
||||
private static final double[] MEANDER_WAVE_SCALES = {1D, 0.43D, 1.77D};
|
||||
private static final double[] MEANDER_WAVE_WEIGHTS = {0.5D, 0.3D, 0.2D};
|
||||
private static final double MEANDER_SLOPE = 0.2D;
|
||||
private static final double STEEP_MEANDER = 0.4D;
|
||||
private static final float SHORE_MARGIN = 2f;
|
||||
private static final int SHORE_RAMP = 4;
|
||||
private static final int OFFSET_SMOOTHING = 3;
|
||||
private static final float OFFSET_CLAMP = 0.5f;
|
||||
private static final float VALLEY_SNAP = 8f;
|
||||
private static final float MOVE_PENALTY = 0.25f * SPACING;
|
||||
private static final float MIN_ADVANCE = 0.25f * SPACING;
|
||||
@@ -28,12 +44,12 @@ final class ReachFitter {
|
||||
|
||||
Fitted fit(DemGrid grid, int[] cells, float[] cellCatchment, HydrologyTier tier, long shapeSeed, boolean drainsToTheSea) {
|
||||
float halfWidth = tier.halfWidth(cellCatchment[cellCatchment.length - 1]);
|
||||
Line line = resample(grid, cells, cellCatchment);
|
||||
meander(line, shapeSeed);
|
||||
int reach = Math.max(2, (int) Math.ceil(Math.max(VALLEY_SNAP, halfWidth + 2f) / SPACING));
|
||||
Line line = relax(resample(grid, cells, cellCatchment));
|
||||
Tangents tangents = tangents(line.xs(), line.zs());
|
||||
meander(line, tangents, shapeSeed, halfWidth);
|
||||
int reach = Math.max(2, (int) Math.ceil(Math.max(VALLEY_SNAP, halfWidth + 2f) / SPACING));
|
||||
Reads reads = read(line, tangents, reach);
|
||||
Fitted fitted = place(line, tangents, reads, viterbi(reads, line.xs().length, reach), tier);
|
||||
Fitted fitted = place(line, tangents, reads, soften(viterbi(reads, line.xs().length, reach), reads, reach), tier);
|
||||
seaEnd(fitted.ocean(), drainsToTheSea);
|
||||
lowestCoveredGround(fitted, tier);
|
||||
return fitted;
|
||||
@@ -78,25 +94,95 @@ final class ReachFitter {
|
||||
return line;
|
||||
}
|
||||
|
||||
private static void meander(Line line, long shapeSeed) {
|
||||
private static Line relax(Line line) {
|
||||
int count = line.xs().length;
|
||||
if (count < 3) {
|
||||
return line;
|
||||
}
|
||||
float[] xs = line.xs().clone();
|
||||
float[] zs = line.zs().clone();
|
||||
for (int pass = 0; pass < RELAX_PASSES; pass++) {
|
||||
laplacian(xs, zs, line, RELAX_LAMBDA);
|
||||
laplacian(xs, zs, line, RELAX_MU);
|
||||
}
|
||||
return respace(line, xs, zs);
|
||||
}
|
||||
|
||||
private static void laplacian(float[] xs, float[] zs, Line origin, float weight) {
|
||||
float[] fromX = xs.clone();
|
||||
float[] fromZ = zs.clone();
|
||||
for (int point = 1; point < xs.length - 1; point++) {
|
||||
float x = fromX[point] + weight * (0.5f * (fromX[point - 1] + fromX[point + 1]) - fromX[point]);
|
||||
float z = fromZ[point] + weight * (0.5f * (fromZ[point - 1] + fromZ[point + 1]) - fromZ[point]);
|
||||
float shiftX = x - origin.xs()[point];
|
||||
float shiftZ = z - origin.zs()[point];
|
||||
float shift = (float) Math.sqrt(shiftX * shiftX + shiftZ * shiftZ);
|
||||
float keep = shift > RELAX_MAX_SHIFT ? RELAX_MAX_SHIFT / shift : 1f;
|
||||
xs[point] = origin.xs()[point] + shiftX * keep;
|
||||
zs[point] = origin.zs()[point] + shiftZ * keep;
|
||||
}
|
||||
}
|
||||
|
||||
private static Line respace(Line line, float[] xs, float[] zs) {
|
||||
int count = xs.length;
|
||||
double[] along = new double[count];
|
||||
for (int point = 1; point < count; point++) {
|
||||
along[point] = along[point - 1] + StrictMath.hypot(xs[point] - xs[point - 1], zs[point] - zs[point - 1]);
|
||||
}
|
||||
double length = along[count - 1];
|
||||
int segments = Math.max(1, (int) Math.round(length / SPACING));
|
||||
Line spaced = new Line(new float[segments + 1], new float[segments + 1], new float[segments + 1], new float[segments + 1],
|
||||
new float[segments + 1], (float) length);
|
||||
int segment = 0;
|
||||
for (int point = 0; point <= segments; point++) {
|
||||
double s = length * point / segments;
|
||||
while (segment < count - 2 && along[segment + 1] < s) {
|
||||
segment++;
|
||||
}
|
||||
double span = along[segment + 1] - along[segment];
|
||||
float t = span > 0D ? (float) ((s - along[segment]) / span) : 0f;
|
||||
spaced.xs()[point] = lerp(xs[segment], xs[segment + 1], t);
|
||||
spaced.zs()[point] = lerp(zs[segment], zs[segment + 1], t);
|
||||
spaced.heights()[point] = lerp(line.heights()[segment], line.heights()[segment + 1], t);
|
||||
spaced.catchment()[point] = lerp(line.catchment()[segment], line.catchment()[segment + 1], t);
|
||||
spaced.distance()[point] = (float) s;
|
||||
}
|
||||
spaced.xs()[0] = xs[0];
|
||||
spaced.zs()[0] = zs[0];
|
||||
spaced.heights()[0] = line.heights()[0];
|
||||
spaced.catchment()[0] = line.catchment()[0];
|
||||
spaced.xs()[segments] = xs[count - 1];
|
||||
spaced.zs()[segments] = zs[count - 1];
|
||||
spaced.heights()[segments] = line.heights()[count - 1];
|
||||
spaced.catchment()[segments] = line.catchment()[count - 1];
|
||||
return spaced;
|
||||
}
|
||||
|
||||
private void meander(Line line, Tangents tangents, long shapeSeed, float halfWidth) {
|
||||
int count = line.xs().length;
|
||||
if (count < 3) {
|
||||
return;
|
||||
}
|
||||
double[] phase = new double[3];
|
||||
double[] phase = new double[MEANDER_WAVE_SCALES.length];
|
||||
for (int wave = 0; wave < phase.length; wave++) {
|
||||
phase[wave] = 2D * Math.PI * CellHash.unit(CellHash.mix(shapeSeed, SALT_MEANDER, wave, 0));
|
||||
}
|
||||
Tangents tangents = tangents(line.xs(), line.zs());
|
||||
double amplitude = Math.clamp(MEANDER_WIDTH_SCALE * (halfWidth + 1f), MEANDER_MIN, MEANDER_MAX);
|
||||
double wavelength = Math.clamp(MEANDER_WAVELENGTH_WIDTHS * 2D * (halfWidth + 1f), MEANDER_WAVELENGTH_MIN, MEANDER_WAVELENGTH_MAX);
|
||||
double ramp = Math.min(line.length() / 2D, wavelength / 2D);
|
||||
float[] open = awayFromPonds(line, tangents, (float) amplitude + halfWidth + SHORE_MARGIN);
|
||||
float[] offsets = new float[count];
|
||||
for (int point = 1; point < count - 1; point++) {
|
||||
double s = line.distance()[point];
|
||||
double run = Math.max(1e-3D, line.distance()[point + 1] - line.distance()[point - 1]);
|
||||
double slope = Math.abs(line.heights()[point + 1] - line.heights()[point - 1]) / run;
|
||||
double damping = Math.clamp(1D - slope / MEANDER_SLOPE, 0D, 1D);
|
||||
double wave = 0.5D * StrictMath.sin(0.03D * s + phase[0]) + 0.3D * StrictMath.sin(0.07D * s + phase[1])
|
||||
+ 0.2D * StrictMath.sin(0.017D * s + phase[2]);
|
||||
offsets[point] = (float) (MEANDER_AMPLITUDE * StrictMath.sin(Math.PI * s / line.length()) * damping * wave);
|
||||
double damping = STEEP_MEANDER + (1D - STEEP_MEANDER) * (1D - smoothstep(slope / MEANDER_SLOPE));
|
||||
double wave = 0D;
|
||||
for (int index = 0; index < phase.length; index++) {
|
||||
wave += MEANDER_WAVE_WEIGHTS[index] * StrictMath.sin(2D * Math.PI * s / (wavelength * MEANDER_WAVE_SCALES[index]) + phase[index]);
|
||||
}
|
||||
double envelope = smoothstep(s / ramp) * smoothstep((line.length() - s) / ramp);
|
||||
offsets[point] = (float) (amplitude * envelope * damping * wave * open[point]);
|
||||
}
|
||||
for (int point = 1; point < count - 1; point++) {
|
||||
line.xs()[point] -= offsets[point] * tangents.z()[point];
|
||||
@@ -104,6 +190,36 @@ final class ReachFitter {
|
||||
}
|
||||
}
|
||||
|
||||
private float[] awayFromPonds(Line line, Tangents tangents, float reach) {
|
||||
int count = line.xs().length;
|
||||
float[] open = new float[count];
|
||||
Arrays.fill(open, 1f);
|
||||
if (ponds.isEmpty()) {
|
||||
return open;
|
||||
}
|
||||
int nodes = (int) Math.ceil(reach / SPACING);
|
||||
boolean[] beside = new boolean[count];
|
||||
for (int point = 0; point < count; point++) {
|
||||
for (int node = -nodes; node <= nodes && !beside[point]; node++) {
|
||||
float offset = node * SPACING;
|
||||
beside[point] = ponds.at(Math.round(line.xs()[point] - offset * tangents.z()[point]), Math.round(line.zs()[point] + offset * tangents.x()[point])) >= 0;
|
||||
}
|
||||
}
|
||||
for (int point = 0; point < count; point++) {
|
||||
for (int other = Math.max(0, point - SHORE_RAMP); other <= Math.min(count - 1, point + SHORE_RAMP); other++) {
|
||||
if (beside[other]) {
|
||||
open[point] = Math.min(open[point], (float) smoothstep(Math.abs(other - point) / (double) SHORE_RAMP));
|
||||
}
|
||||
}
|
||||
}
|
||||
return open;
|
||||
}
|
||||
|
||||
private static double smoothstep(double value) {
|
||||
double t = Math.clamp(value, 0D, 1D);
|
||||
return t * t * (3D - 2D * t);
|
||||
}
|
||||
|
||||
private Reads read(Line line, Tangents tangents, int reach) {
|
||||
int span = 2 * reach + 1;
|
||||
int count = line.xs().length * span;
|
||||
@@ -275,7 +391,30 @@ final class ReachFitter {
|
||||
return chosen;
|
||||
}
|
||||
|
||||
private static Fitted place(Line line, Tangents tangents, Reads reads, int[] offsets, HydrologyTier tier) {
|
||||
private static float[] soften(int[] chosen, Reads reads, int reach) {
|
||||
int count = chosen.length;
|
||||
float[] offsets = new float[count];
|
||||
for (int point = 0; point < count; point++) {
|
||||
offsets[point] = chosen[point];
|
||||
}
|
||||
for (int pass = 0; pass < OFFSET_SMOOTHING; pass++) {
|
||||
float[] from = offsets.clone();
|
||||
for (int point = 1; point < count - 1; point++) {
|
||||
float smoothed = 0.25f * from[point - 1] + 0.5f * from[point] + 0.25f * from[point + 1];
|
||||
offsets[point] = Math.clamp(smoothed, chosen[point] - OFFSET_CLAMP, chosen[point] + OFFSET_CLAMP);
|
||||
}
|
||||
}
|
||||
for (int point = 1; point < count - 1; point++) {
|
||||
int toward = (int) Math.signum(offsets[point] - chosen[point]);
|
||||
int neighbour = chosen[point] + toward + reach;
|
||||
if (toward != 0 && (neighbour < 0 || neighbour >= reads.span() || reads.blocked()[point * reads.span() + neighbour])) {
|
||||
offsets[point] = chosen[point];
|
||||
}
|
||||
}
|
||||
return offsets;
|
||||
}
|
||||
|
||||
private static Fitted place(Line line, Tangents tangents, Reads reads, float[] offsets, HydrologyTier tier) {
|
||||
int count = line.xs().length;
|
||||
float[] px = new float[count];
|
||||
float[] pz = new float[count];
|
||||
@@ -299,8 +438,8 @@ final class ReachFitter {
|
||||
Fitted fitted = new Fitted(new float[size], new float[size], new float[size], new float[size], new boolean[size], new float[size]);
|
||||
for (int index = 0; index < size; index++) {
|
||||
int point = kept[index];
|
||||
int chosen = offsets[point] + reads.span() / 2;
|
||||
int read = point * reads.span() + chosen;
|
||||
float chosen = offsets[point] + reads.span() / 2;
|
||||
int read = point * reads.span() + Math.round(chosen);
|
||||
float beside = (tier.halfWidth(line.catchment()[point]) + 1f) / SPACING;
|
||||
fitted.bank()[index] = Math.min(bank(reads, point, chosen, -beside), bank(reads, point, chosen, beside));
|
||||
fitted.xs()[index] = px[point];
|
||||
@@ -316,7 +455,7 @@ final class ReachFitter {
|
||||
return fitted;
|
||||
}
|
||||
|
||||
private static float bank(Reads reads, int point, int chosen, float offset) {
|
||||
private static float bank(Reads reads, int point, float chosen, float offset) {
|
||||
float position = Math.clamp(chosen + offset, 0f, reads.span() - 1f);
|
||||
int near = (int) Math.floor(position);
|
||||
int far = Math.min(reads.span() - 1, near + 1);
|
||||
|
||||
+27
-25
@@ -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) {
|
||||
|
||||
+17
-2
@@ -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()) {
|
||||
|
||||
+5
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+126
-7
@@ -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++) {
|
||||
|
||||
Reference in new issue
Block a user