|
|
|
@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.random.RandomGenerator;
|
|
|
|
|
|
|
|
|
|
import static eu.jonahbauer.raytracing.Main.DEBUG;
|
|
|
|
|
|
|
|
|
|
public record Vec3(double x, double y, double z) {
|
|
|
|
|
public static final Vec3 ZERO = new Vec3(0, 0, 0);
|
|
|
|
|
public static final Vec3 MAX = new Vec3(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
|
|
|
|
@ -14,7 +16,11 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
|
public static final Vec3 UNIT_Z = new Vec3(0, 0, 1);
|
|
|
|
|
|
|
|
|
|
public Vec3 {
|
|
|
|
|
assert Double.isFinite(x) && Double.isFinite(y) && Double.isFinite(z) : "x, y and z must be finite";
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
if (!Double.isFinite(x) || !Double.isFinite(y) || !Double.isFinite(z)) {
|
|
|
|
|
throw new IllegalArgumentException("x, y and z must be finite");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -48,11 +54,7 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) {
|
|
|
|
|
var factor = - 2 * normal.times(vec);
|
|
|
|
|
return new Vec3(
|
|
|
|
|
Math.fma(factor, normal.x(), vec.x()),
|
|
|
|
|
Math.fma(factor, normal.y(), vec.y()),
|
|
|
|
|
Math.fma(factor, normal.z(), vec.z())
|
|
|
|
|
);
|
|
|
|
|
return Vec3.fma(factor, normal, vec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NotNull Optional<Vec3> refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double ri) {
|
|
|
|
@ -101,6 +103,14 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 fma(double a, @NotNull Vec3 b, @NotNull Vec3 c) {
|
|
|
|
|
return new Vec3(
|
|
|
|
|
Math.fma(a, b.x(), c.x()),
|
|
|
|
|
Math.fma(a, b.y(), c.y()),
|
|
|
|
|
Math.fma(a, b.z(), c.z())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public @NotNull Vec3 plus(double x, double y, double z) {
|
|
|
|
|
return new Vec3(this.x + x, this.y + y, this.z + z);
|
|
|
|
|
}
|
|
|
|
|