mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-06-17 22:31:52 +00:00
Optimize cardinal rotations for RotateColorSampler
This commit is contained in:
+42
-7
@@ -4,23 +4,58 @@ import net.jafama.FastMath;
|
|||||||
|
|
||||||
import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
|
import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
|
||||||
|
|
||||||
|
|
||||||
public class RotateColorSampler implements ColorSampler {
|
public class RotateColorSampler implements ColorSampler {
|
||||||
|
|
||||||
private final ColorSampler sampler;
|
private final ColorSampler sampler;
|
||||||
|
|
||||||
private final double radians;
|
private final double radians;
|
||||||
|
|
||||||
public RotateColorSampler(ColorSampler sampler, double radians) {
|
private final RotationMethod rotationMethod;
|
||||||
|
|
||||||
|
public RotateColorSampler(ColorSampler sampler, double degrees) {
|
||||||
this.sampler = sampler;
|
this.sampler = sampler;
|
||||||
this.radians = radians;
|
|
||||||
|
double normalizedDegrees = degrees % 360.0;
|
||||||
|
if (normalizedDegrees < 0) normalizedDegrees += 360.0;
|
||||||
|
|
||||||
|
if (normalizedDegrees == 0.0)
|
||||||
|
rotationMethod = RotationMethod.DEG_0;
|
||||||
|
else if (normalizedDegrees == 90.0)
|
||||||
|
rotationMethod = RotationMethod.DEG_90;
|
||||||
|
else if (normalizedDegrees == 180.0)
|
||||||
|
rotationMethod = RotationMethod.DEG_180;
|
||||||
|
else if (normalizedDegrees == 270.0)
|
||||||
|
rotationMethod = RotationMethod.DEG_270;
|
||||||
|
else
|
||||||
|
rotationMethod = RotationMethod.RAD_ANY;
|
||||||
|
|
||||||
|
this.radians = FastMath.toRadians(degrees);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int apply(int x, int z) {
|
public int apply(int x, int z) {
|
||||||
return sampler.apply(
|
int rx = switch(rotationMethod) {
|
||||||
(int) (x * FastMath.cos(radians) - z * FastMath.sin(radians)),
|
case DEG_0 -> x;
|
||||||
(int) (z * FastMath.cos(radians) + x * FastMath.sin(radians))
|
case DEG_90 -> -z;
|
||||||
);
|
case DEG_180 -> -x;
|
||||||
|
case DEG_270 -> z;
|
||||||
|
case RAD_ANY -> (int) (x * FastMath.cos(radians) - z * FastMath.sin(radians));
|
||||||
|
};
|
||||||
|
int rz = switch(rotationMethod) {
|
||||||
|
case DEG_0 -> z;
|
||||||
|
case DEG_90 -> x;
|
||||||
|
case DEG_180 -> -z;
|
||||||
|
case DEG_270 -> -x;
|
||||||
|
case RAD_ANY -> (int) (z * FastMath.cos(radians) + x * FastMath.sin(radians));
|
||||||
|
};
|
||||||
|
return sampler.apply(rx, rz);
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum RotationMethod {
|
||||||
|
DEG_0,
|
||||||
|
DEG_90,
|
||||||
|
DEG_180,
|
||||||
|
DEG_270,
|
||||||
|
RAD_ANY,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-3
@@ -1,7 +1,6 @@
|
|||||||
package com.dfsek.terra.addons.image.config.colorsampler.mutate;
|
package com.dfsek.terra.addons.image.config.colorsampler.mutate;
|
||||||
|
|
||||||
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||||
import net.jafama.FastMath;
|
|
||||||
|
|
||||||
import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
|
import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
|
||||||
import com.dfsek.terra.addons.image.colorsampler.mutate.RotateColorSampler;
|
import com.dfsek.terra.addons.image.colorsampler.mutate.RotateColorSampler;
|
||||||
@@ -10,10 +9,10 @@ import com.dfsek.terra.addons.image.colorsampler.mutate.RotateColorSampler;
|
|||||||
public class RotateColorSamplerTemplate extends MutateColorSamplerTemplate {
|
public class RotateColorSamplerTemplate extends MutateColorSamplerTemplate {
|
||||||
|
|
||||||
@Value("angle")
|
@Value("angle")
|
||||||
private double angle;
|
private double degrees;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ColorSampler get() {
|
public ColorSampler get() {
|
||||||
return new RotateColorSampler(sampler, FastMath.toRadians(angle));
|
return new RotateColorSampler(sampler, degrees);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user