mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-22 08:10:40 +00:00
refactor interpolators
This commit is contained in:
@@ -3,7 +3,7 @@ package com.dfsek.terra.world.generation.math.interpolation;
|
|||||||
/**
|
/**
|
||||||
* Class for bilinear interpolation of values arranged on a unit square.
|
* 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;
|
private final double v0, v1, v2, v3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -14,7 +14,7 @@ public class Interpolator {
|
|||||||
* @param v2 - (0,1)
|
* @param v2 - (0,1)
|
||||||
* @param v3 - (1,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.v0 = v0;
|
||||||
this.v1 = v1;
|
this.v1 = v1;
|
||||||
this.v2 = v2;
|
this.v2 = v2;
|
||||||
@@ -40,7 +40,8 @@ public class Interpolator {
|
|||||||
* @param t - Z value
|
* @param t - Z value
|
||||||
* @return double - The interpolated 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 v01 = lerp(s, v0, v1);
|
||||||
double v23 = lerp(s, v2, v3);
|
double v23 = lerp(s, v2, v3);
|
||||||
return lerp(t, v01, v23);
|
return lerp(t, v01, v23);
|
||||||
@@ -67,7 +67,7 @@ public class ChunkInterpolator3D implements ChunkInterpolator {
|
|||||||
for(int x = 0; x < 4; x++) {
|
for(int x = 0; x < 4; x++) {
|
||||||
for(int z = 0; z < 4; z++) {
|
for(int z = 0; z < 4; z++) {
|
||||||
for(int y = 0; y < size; y++) {
|
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][z][y],
|
||||||
noiseStorage[x + 1][z][y],
|
noiseStorage[x + 1][z][y],
|
||||||
noiseStorage[x][z][y + 1],
|
noiseStorage[x][z][y + 1],
|
||||||
@@ -98,10 +98,10 @@ public class ChunkInterpolator3D implements ChunkInterpolator {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public double getNoise(double x, double y, double z) {
|
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) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
@@ -1,32 +1,5 @@
|
|||||||
package com.dfsek.terra.world.generation.math.interpolation;
|
package com.dfsek.terra.world.generation.math.interpolation;
|
||||||
|
|
||||||
/**
|
public interface Interpolator3 {
|
||||||
* Class for bilinear interpolation of values arranged on a unit square.
|
double interpolate(double x, double y, double z);
|
||||||
*/
|
|
||||||
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 <code>(t, u, v) = (0, 0, 0)</code>.
|
|
||||||
* * @param _100 The value at <code>(t, u, v) = (1, 0, 0)</code>.
|
|
||||||
* * @param _010 The value at <code>(t, u, v) = (0, 1, 0)</code>.
|
|
||||||
* * @param _110 The value at <code>(t, u, v) = (1, 1, 0)</code>.
|
|
||||||
* * @param _001 The value at <code>(t, u, v) = (0, 0, 1)</code>.
|
|
||||||
* * @param _101 The value at <code>(t, u, v) = (1, 0, 1)</code>.
|
|
||||||
* * @param _011 The value at <code>(t, u, v) = (0, 1, 1)</code>.
|
|
||||||
* * @param _111 The value at <code>(t, u, v) = (1, 1, 1)</code>.
|
|
||||||
*/
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -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 <code>(t, u, v) = (0, 0, 0)</code>.
|
||||||
|
* * @param _100 The value at <code>(t, u, v) = (1, 0, 0)</code>.
|
||||||
|
* * @param _010 The value at <code>(t, u, v) = (0, 1, 0)</code>.
|
||||||
|
* * @param _110 The value at <code>(t, u, v) = (1, 1, 0)</code>.
|
||||||
|
* * @param _001 The value at <code>(t, u, v) = (0, 0, 1)</code>.
|
||||||
|
* * @param _101 The value at <code>(t, u, v) = (1, 0, 1)</code>.
|
||||||
|
* * @param _011 The value at <code>(t, u, v) = (0, 1, 1)</code>.
|
||||||
|
* * @param _111 The value at <code>(t, u, v) = (1, 1, 1)</code>.
|
||||||
|
*/
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user