|
|
|
@ -23,15 +23,20 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) {
|
|
|
|
|
var vec = new Vec3(
|
|
|
|
|
2 * random.nextDouble() - 1,
|
|
|
|
|
2 * random.nextDouble() - 1,
|
|
|
|
|
2 * random.nextDouble() - 1
|
|
|
|
|
Math.fma(2, random.nextDouble(), -1),
|
|
|
|
|
Math.fma(2, random.nextDouble(), -1),
|
|
|
|
|
Math.fma(2, random.nextDouble(), -1)
|
|
|
|
|
);
|
|
|
|
|
return unit ? vec.unit() : vec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) {
|
|
|
|
|
return vec.minus(normal.times(2 * normal.times(vec)));
|
|
|
|
|
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())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NotNull Optional<Vec3> refract(@NotNull Vec3 vec, @NotNull Vec3 normal, double ri) {
|
|
|
|
@ -56,10 +61,11 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 average(@NotNull Vec3 current, @NotNull Vec3 next, int index) {
|
|
|
|
|
var factor = 1d / index;
|
|
|
|
|
return new Vec3(
|
|
|
|
|
current.x() + (next.x() - current.x()) / index,
|
|
|
|
|
current.y() + (next.y() - current.y()) / index,
|
|
|
|
|
current.z() + (next.z() - current.z()) / index
|
|
|
|
|
Math.fma(factor, next.x() - current.x(), current.x()),
|
|
|
|
|
Math.fma(factor, next.y() - current.y(), current.y()),
|
|
|
|
|
Math.fma(factor, next.z() - current.z(), current.z())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|