Update lerp usage and docs

This commit is contained in:
Zoë Gidiere
2023-11-08 21:18:35 -07:00
parent 020033f839
commit dd7bebb27f
5 changed files with 32 additions and 36 deletions
@@ -7,6 +7,9 @@
package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation; package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation;
import com.dfsek.terra.api.util.MathUtil;
/** /**
* Class for bilinear interpolation of values arranged on a unit square. * Class for bilinear interpolation of values arranged on a unit square.
*/ */
@@ -28,19 +31,6 @@ public class Interpolator {
this.v3 = v3; this.v3 = v3;
} }
/**
* 1D Linear interpolation between 2 points 1 unit apart.
*
* @param t - Distance from v0. Total distance between v0 and v1 is 1 unit.
* @param v0 - Value at v0.
* @param v1 - Value at v1.
*
* @return double - The interpolated value.
*/
public static double lerp(double t, double v0, double v1) {
return v0 + t * (v1 - v0);
}
/** /**
* 2D Bilinear interpolation between 4 points on a unit square. * 2D Bilinear interpolation between 4 points on a unit square.
* *
@@ -50,8 +40,8 @@ public class Interpolator {
* @return double - The interpolated value. * @return double - The interpolated value.
*/ */
public double bilerp(double s, double t) { public double bilerp(double s, double t) {
double v01 = lerp(s, v0, v1); double v01 = MathUtil.lerp(s, v0, v1);
double v23 = lerp(s, v2, v3); double v23 = MathUtil.lerp(s, v2, v3);
return lerp(t, v01, v23); return MathUtil.lerp(t, v01, v23);
} }
} }
@@ -7,6 +7,9 @@
package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation; package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation;
import com.dfsek.terra.api.util.MathUtil;
/** /**
* Class for bilinear interpolation of values arranged on a unit square. * Class for bilinear interpolation of values arranged on a unit square.
*/ */
@@ -34,6 +37,6 @@ public class Interpolator3 {
} }
public double trilerp(double x, double y, double z) { public double trilerp(double x, double y, double z) {
return Interpolator.lerp(x, top.bilerp(y, z), bottom.bilerp(y, z)); return MathUtil.lerp(x, top.bilerp(y, z), bottom.bilerp(y, z));
} }
} }
@@ -2,11 +2,9 @@ package com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation;
import com.dfsek.terra.addons.chunkgenerator.config.noise.BiomeNoiseProperties; import com.dfsek.terra.addons.chunkgenerator.config.noise.BiomeNoiseProperties;
import com.dfsek.terra.api.properties.PropertyKey; import com.dfsek.terra.api.properties.PropertyKey;
import com.dfsek.terra.api.util.MathUtil;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider; import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import static com.dfsek.terra.addons.chunkgenerator.generation.math.interpolation.Interpolator.lerp;
public class LazilyEvaluatedInterpolator { public class LazilyEvaluatedInterpolator {
private final Double[] samples; // private final Double[] samples; //
@@ -82,10 +80,10 @@ public class LazilyEvaluatedInterpolator {
double xFrac = (double) (x % horizontalRes) / horizontalRes; double xFrac = (double) (x % horizontalRes) / horizontalRes;
double zFrac = (double) (z % horizontalRes) / horizontalRes; double zFrac = (double) (z % horizontalRes) / horizontalRes;
double lerp_bottom_0 = lerp(zFrac, sample_0_0_0, sample_0_0_1); double lerp_bottom_0 = MathUtil.lerp(zFrac, sample_0_0_0, sample_0_0_1);
double lerp_bottom_1 = lerp(zFrac, sample_1_0_0, sample_1_0_1); double lerp_bottom_1 = MathUtil.lerp(zFrac, sample_1_0_0, sample_1_0_1);
double lerp_bottom = lerp(xFrac, lerp_bottom_0, lerp_bottom_1); double lerp_bottom = MathUtil.lerp(xFrac, lerp_bottom_0, lerp_bottom_1);
if(yRange) { // we can do bilerp if(yRange) { // we can do bilerp
return lerp_bottom; return lerp_bottom;
@@ -101,11 +99,11 @@ public class LazilyEvaluatedInterpolator {
double sample_1_1_0 = sample(xIndex + 1, yIndex + 1, zIndex, x + horizontalRes, y + verticalRes, z); double sample_1_1_0 = sample(xIndex + 1, yIndex + 1, zIndex, x + horizontalRes, y + verticalRes, z);
double sample_1_1_1 = sample(xIndex + 1, yIndex + 1, zIndex + 1, x + horizontalRes, y + verticalRes, z + horizontalRes); double sample_1_1_1 = sample(xIndex + 1, yIndex + 1, zIndex + 1, x + horizontalRes, y + verticalRes, z + horizontalRes);
double lerp_top_0 = lerp(zFrac, sample_0_1_0, sample_0_1_1); double lerp_top_0 = MathUtil.lerp(zFrac, sample_0_1_0, sample_0_1_1);
double lerp_top_1 = lerp(zFrac, sample_1_1_0, sample_1_1_1); double lerp_top_1 = MathUtil.lerp(zFrac, sample_1_1_0, sample_1_1_1);
double lerp_top = lerp(xFrac, lerp_top_0, lerp_top_1); double lerp_top = MathUtil.lerp(xFrac, lerp_top_0, lerp_top_1);
return lerp(yFrac, lerp_bottom, lerp_top); return MathUtil.lerp(yFrac, lerp_bottom, lerp_top);
} }
} }
@@ -43,10 +43,6 @@ public class VanillaOre implements Structure {
this.materials = materials; this.materials = materials;
} }
public static double lerp(double t, double v0, double v1) {
return v0 + t * (v1 - v0);
}
@Override @Override
public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) { public boolean generate(Vector3Int location, WritableWorld world, Random random, Rotation rotation) {
float randomRadian = random.nextFloat() * (float) Math.PI; float randomRadian = random.nextFloat() * (float) Math.PI;
@@ -68,9 +64,9 @@ public class VanillaOre implements Structure {
// Compute initial point positions and radius // Compute initial point positions and radius
for(int i = 0; i < sizeInt; ++i) { for(int i = 0; i < sizeInt; ++i) {
float t = (float) i / (float) sizeInt; float t = (float) i / (float) sizeInt;
double xt = lerp(t, startX, endX); double xt = MathUtil.lerp(t, startX, endX);
double yt = lerp(t, startY, endY); double yt = MathUtil.lerp(t, startY, endY);
double zt = lerp(t, startZ, endZ); double zt = MathUtil.lerp(t, startZ, endZ);
double roll = random.nextDouble() * size / 16.0; double roll = random.nextDouble() * size / 16.0;
// Taper radius closer to line ends // Taper radius closer to line ends
double radius = ((MathUtil.sin((float) Math.PI * t) + 1.0F) * roll + 1.0) / 2.0; double radius = ((MathUtil.sin((float) Math.PI * t) + 1.0F) * roll + 1.0) / 2.0;
@@ -237,8 +237,17 @@ public final class MathUtil {
return h; return h;
} }
public static double lerp(double a, double b, double t) { /**
return a + t * (b - a); * 1D Linear interpolation between 2 points 1 unit apart.
*
* @param t - Distance from v0. Total distance between v0 and v1 is 1 unit.
* @param v0 - Value at v0.
* @param v1 - Value at v1.
*
* @return double - The interpolated value.
*/
public static double lerp(double t, double v0, double v1) {
return v0 + t * (v1 - v0);
} }
public static double cubicLerp(double a, double b, double c, double d, double t) { public static double cubicLerp(double a, double b, double c, double d, double t) {