From 111f6b05baacad80fa8cb671a90e3de90ef2d2c5 Mon Sep 17 00:00:00 2001 From: dfsek Date: Tue, 13 Apr 2021 00:16:40 -0700 Subject: [PATCH] refactor interpolators --- ...polator.java => BilinearInterpolator.java} | 7 ++-- .../interpolation/ChunkInterpolator3D.java | 6 ++-- .../math/interpolation/Interpolator2.java | 12 +++++++ .../math/interpolation/Interpolator3.java | 33 ++----------------- .../interpolation/TrilinearInterpolator.java | 33 +++++++++++++++++++ 5 files changed, 55 insertions(+), 36 deletions(-) rename common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/{Interpolator.java => BilinearInterpolator.java} (85%) create mode 100644 common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator2.java create mode 100644 common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/TrilinearInterpolator.java diff --git a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator.java b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/BilinearInterpolator.java similarity index 85% rename from common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator.java rename to common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/BilinearInterpolator.java index bf002e8a4..3f5fb72a2 100644 --- a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator.java +++ b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/BilinearInterpolator.java @@ -3,7 +3,7 @@ package com.dfsek.terra.world.generation.math.interpolation; /** * Class for bilinear interpolation of values arranged on a unit square. */ -public class Interpolator { +public class BilinearInterpolator implements Interpolator2 { private final double v0, v1, v2, v3; /** @@ -14,7 +14,7 @@ public class Interpolator { * @param v2 - (0,1) * @param v3 - (1,1) */ - public Interpolator(double v0, double v1, double v2, double v3) { + public BilinearInterpolator(double v0, double v1, double v2, double v3) { this.v0 = v0; this.v1 = v1; this.v2 = v2; @@ -40,7 +40,8 @@ public class Interpolator { * @param t - Z value * @return double - The interpolated value. */ - public double bilerp(double s, double t) { + @Override + public double interpolate(double s, double t) { double v01 = lerp(s, v0, v1); double v23 = lerp(s, v2, v3); return lerp(t, v01, v23); diff --git a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/ChunkInterpolator3D.java b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/ChunkInterpolator3D.java index 727a8e25e..e1e2ebeca 100644 --- a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/ChunkInterpolator3D.java +++ b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/ChunkInterpolator3D.java @@ -67,7 +67,7 @@ public class ChunkInterpolator3D implements ChunkInterpolator { for(int x = 0; x < 4; x++) { for(int z = 0; z < 4; z++) { for(int y = 0; y < size; y++) { - interpGrid[x][y][z] = new Interpolator3( + interpGrid[x][y][z] = new TrilinearInterpolator( noiseStorage[x][z][y], noiseStorage[x + 1][z][y], noiseStorage[x][z][y + 1], @@ -98,10 +98,10 @@ public class ChunkInterpolator3D implements ChunkInterpolator { */ @Override public double getNoise(double x, double y, double z) { - return interpGrid[reRange(((int) x) / 4, 3)][FastMath.max(FastMath.min(((int) y), max), min) / 4][reRange(((int) z) / 4, 3)].trilerp((x % 4) / 4, (y % 4) / 4, (z % 4) / 4); + return interpGrid[reRange(((int) x) / 4, 3)][FastMath.max(FastMath.min(((int) y), max), min) / 4][reRange(((int) z) / 4, 3)].interpolate((x % 4) / 4, (y % 4) / 4, (z % 4) / 4); } public double getNoise(int x, int y, int z) { - return interpGrid[x / 4][y / 4][z / 4].trilerp((double) (x % 4) / 4, (double) (y % 4) / 4, (double) (z % 4) / 4); + return interpGrid[x / 4][y / 4][z / 4].interpolate((double) (x % 4) / 4, (double) (y % 4) / 4, (double) (z % 4) / 4); } } diff --git a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator2.java b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator2.java new file mode 100644 index 000000000..be6f6e017 --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator2.java @@ -0,0 +1,12 @@ +package com.dfsek.terra.world.generation.math.interpolation; + +public interface Interpolator2 { + /** + * 2D Bilinear interpolation between 4 points on a unit square. + * + * @param s - X value + * @param t - Z value + * @return double - The interpolated value. + */ + double interpolate(double s, double t); +} diff --git a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator3.java b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator3.java index c7a7c0275..f411f5dcb 100644 --- a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator3.java +++ b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/Interpolator3.java @@ -1,32 +1,5 @@ package com.dfsek.terra.world.generation.math.interpolation; -/** - * Class for bilinear interpolation of values arranged on a unit square. - */ -public class Interpolator3 { - private final Interpolator bottom; - private final Interpolator top; - - /** - * Constructs an interpolator with given values as vertices of a unit cube. - * * @param _000 The value at (t, u, v) = (0, 0, 0). - * * @param _100 The value at (t, u, v) = (1, 0, 0). - * * @param _010 The value at (t, u, v) = (0, 1, 0). - * * @param _110 The value at (t, u, v) = (1, 1, 0). - * * @param _001 The value at (t, u, v) = (0, 0, 1). - * * @param _101 The value at (t, u, v) = (1, 0, 1). - * * @param _011 The value at (t, u, v) = (0, 1, 1). - * * @param _111 The value at (t, u, v) = (1, 1, 1). - */ - public Interpolator3(double _000, double _100, - double _010, double _110, - double _001, double _101, - double _011, double _111) { - this.top = new Interpolator(_000, _010, _001, _011); - this.bottom = new Interpolator(_100, _110, _101, _111); - } - - public double trilerp(double x, double y, double z) { - return Interpolator.lerp(x, top.bilerp(y, z), bottom.bilerp(y, z)); - } -} \ No newline at end of file +public interface Interpolator3 { + double interpolate(double x, double y, double z); +} diff --git a/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/TrilinearInterpolator.java b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/TrilinearInterpolator.java new file mode 100644 index 000000000..21a67960f --- /dev/null +++ b/common/src/main/java/com/dfsek/terra/world/generation/math/interpolation/TrilinearInterpolator.java @@ -0,0 +1,33 @@ +package com.dfsek.terra.world.generation.math.interpolation; + +/** + * Class for bilinear interpolation of values arranged on a unit square. + */ +public class TrilinearInterpolator implements Interpolator3 { + private final BilinearInterpolator bottom; + private final BilinearInterpolator top; + + /** + * Constructs an interpolator with given values as vertices of a unit cube. + * * @param _000 The value at (t, u, v) = (0, 0, 0). + * * @param _100 The value at (t, u, v) = (1, 0, 0). + * * @param _010 The value at (t, u, v) = (0, 1, 0). + * * @param _110 The value at (t, u, v) = (1, 1, 0). + * * @param _001 The value at (t, u, v) = (0, 0, 1). + * * @param _101 The value at (t, u, v) = (1, 0, 1). + * * @param _011 The value at (t, u, v) = (0, 1, 1). + * * @param _111 The value at (t, u, v) = (1, 1, 1). + */ + public TrilinearInterpolator(double _000, double _100, + double _010, double _110, + double _001, double _101, + double _011, double _111) { + this.top = new BilinearInterpolator(_000, _010, _001, _011); + this.bottom = new BilinearInterpolator(_100, _110, _101, _111); + } + + @Override + public double interpolate(double x, double y, double z) { + return BilinearInterpolator.lerp(x, top.interpolate(y, z), bottom.interpolate(y, z)); + } +} \ No newline at end of file