mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 14:21:08 +00:00
move derivative method to PaletteUtil
This commit is contained in:
+19
-2
@@ -9,16 +9,20 @@ package com.dfsek.terra.addons.chunkgenerator.generation.math;
|
|||||||
|
|
||||||
import com.dfsek.terra.addons.chunkgenerator.palette.PaletteInfo;
|
import com.dfsek.terra.addons.chunkgenerator.palette.PaletteInfo;
|
||||||
import com.dfsek.terra.addons.chunkgenerator.palette.SlantHolder;
|
import com.dfsek.terra.addons.chunkgenerator.palette.SlantHolder;
|
||||||
import com.dfsek.terra.api.util.MathUtil;
|
|
||||||
import com.dfsek.terra.api.util.math.Sampler;
|
import com.dfsek.terra.api.util.math.Sampler;
|
||||||
import com.dfsek.terra.api.world.chunk.generation.util.Palette;
|
import com.dfsek.terra.api.world.chunk.generation.util.Palette;
|
||||||
|
|
||||||
|
|
||||||
public final class PaletteUtil {
|
public final class PaletteUtil {
|
||||||
|
/**
|
||||||
|
* Derivative constant.
|
||||||
|
*/
|
||||||
|
private static final double DERIVATIVE_DIST = 0.55;
|
||||||
|
|
||||||
public static Palette getPalette(int x, int y, int z, Sampler sampler, PaletteInfo paletteInfo) {
|
public static Palette getPalette(int x, int y, int z, Sampler sampler, PaletteInfo paletteInfo) {
|
||||||
SlantHolder slant = paletteInfo.getSlantHolder();
|
SlantHolder slant = paletteInfo.getSlantHolder();
|
||||||
if(slant != null) {
|
if(slant != null) {
|
||||||
double slope = MathUtil.derivative(sampler, x, y, z);
|
double slope = derivative(sampler, x, y, z);
|
||||||
if(slope > slant.getMinSlope()) {
|
if(slope > slant.getMinSlope()) {
|
||||||
return slant.getPalette(slope).getPalette(y);
|
return slant.getPalette(slope).getPalette(y);
|
||||||
}
|
}
|
||||||
@@ -26,4 +30,17 @@ public final class PaletteUtil {
|
|||||||
|
|
||||||
return paletteInfo.getPaletteHolder().getPalette(y);
|
return paletteInfo.getPaletteHolder().getPalette(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double derivative(Sampler sampler, double x, double y, double z) {
|
||||||
|
double baseSample = sampler.sample(x, y, z);
|
||||||
|
|
||||||
|
double xVal1 = (sampler.sample(x + DERIVATIVE_DIST, y, z) - baseSample) / DERIVATIVE_DIST;
|
||||||
|
double xVal2 = (sampler.sample(x - DERIVATIVE_DIST, y, z) - baseSample) / DERIVATIVE_DIST;
|
||||||
|
double zVal1 = (sampler.sample(x, y, z + DERIVATIVE_DIST) - baseSample) / DERIVATIVE_DIST;
|
||||||
|
double zVal2 = (sampler.sample(x, y, z - DERIVATIVE_DIST) - baseSample) / DERIVATIVE_DIST;
|
||||||
|
double yVal1 = (sampler.sample(x, y + DERIVATIVE_DIST, z) - baseSample) / DERIVATIVE_DIST;
|
||||||
|
double yVal2 = (sampler.sample(x, y - DERIVATIVE_DIST, z) - baseSample) / DERIVATIVE_DIST;
|
||||||
|
|
||||||
|
return Math.sqrt(((xVal2 - xVal1) * (xVal2 - xVal1)) + ((zVal2 - zVal1) * (zVal2 - zVal1)) + ((yVal2 - yVal1) * (yVal2 - yVal1)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -11,8 +11,6 @@ import net.jafama.FastMath;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.dfsek.terra.api.util.math.Sampler;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for mathematical functions.
|
* Utility class for mathematical functions.
|
||||||
@@ -22,10 +20,6 @@ public final class MathUtil {
|
|||||||
* Epsilon for fuzzy floating point comparisons.
|
* Epsilon for fuzzy floating point comparisons.
|
||||||
*/
|
*/
|
||||||
public static final double EPSILON = 1.0E-5;
|
public static final double EPSILON = 1.0E-5;
|
||||||
/**
|
|
||||||
* Derivative constant.
|
|
||||||
*/
|
|
||||||
private static final double DERIVATIVE_DIST = 0.55;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the standard deviation of an array of doubles.
|
* Gets the standard deviation of an array of doubles.
|
||||||
@@ -74,19 +68,6 @@ public final class MathUtil {
|
|||||||
return a == b || FastMath.abs(a - b) < EPSILON;
|
return a == b || FastMath.abs(a - b) < EPSILON;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double derivative(Sampler sampler, double x, double y, double z) {
|
|
||||||
double baseSample = sampler.sample(x, y, z);
|
|
||||||
|
|
||||||
double xVal1 = (sampler.sample(x + DERIVATIVE_DIST, y, z) - baseSample) / DERIVATIVE_DIST;
|
|
||||||
double xVal2 = (sampler.sample(x - DERIVATIVE_DIST, y, z) - baseSample) / DERIVATIVE_DIST;
|
|
||||||
double zVal1 = (sampler.sample(x, y, z + DERIVATIVE_DIST) - baseSample) / DERIVATIVE_DIST;
|
|
||||||
double zVal2 = (sampler.sample(x, y, z - DERIVATIVE_DIST) - baseSample) / DERIVATIVE_DIST;
|
|
||||||
double yVal1 = (sampler.sample(x, y + DERIVATIVE_DIST, z) - baseSample) / DERIVATIVE_DIST;
|
|
||||||
double yVal2 = (sampler.sample(x, y - DERIVATIVE_DIST, z) - baseSample) / DERIVATIVE_DIST;
|
|
||||||
|
|
||||||
return Math.sqrt(((xVal2 - xVal1) * (xVal2 - xVal1)) + ((zVal2 - zVal1) * (zVal2 - zVal1)) + ((yVal2 - yVal1) * (yVal2 - yVal1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int normalizeIndex(double val, int size) {
|
public static int normalizeIndex(double val, int size) {
|
||||||
return FastMath.max(FastMath.min(FastMath.floorToInt(((val + 1D) / 2D) * size), size - 1), 0);
|
return FastMath.max(FastMath.min(FastMath.floorToInt(((val + 1D) / 2D) * size), size - 1), 0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user