|
|
|
@ -18,28 +18,32 @@ public record Vec3(double x, double y, double z) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@return a uniformly random vector with components in the range [-1, 1)}
|
|
|
|
|
* {@return a uniformly random unit vector}
|
|
|
|
|
*/
|
|
|
|
|
public static @NotNull Vec3 random(@NotNull RandomGenerator random) {
|
|
|
|
|
return new Vec3(
|
|
|
|
|
Math.fma(2, random.nextDouble(), -1),
|
|
|
|
|
Math.fma(2, random.nextDouble(), -1),
|
|
|
|
|
Math.fma(2, random.nextDouble(), -1)
|
|
|
|
|
);
|
|
|
|
|
double x, y, z;
|
|
|
|
|
double squared;
|
|
|
|
|
do {
|
|
|
|
|
x = Math.fma(2, random.nextDouble(), -1);
|
|
|
|
|
y = Math.fma(2, random.nextDouble(), -1);
|
|
|
|
|
z = Math.fma(2, random.nextDouble(), -1);
|
|
|
|
|
squared = x * x + y * y + z * z;
|
|
|
|
|
} while (squared > 1);
|
|
|
|
|
var factor = 1 / Math.sqrt(squared);
|
|
|
|
|
return new Vec3(x * factor, y * factor, z * factor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@return a uniformly random unit vector}
|
|
|
|
|
*/
|
|
|
|
|
public static @NotNull Vec3 random(@NotNull RandomGenerator random, boolean unit) {
|
|
|
|
|
if (!unit) return random(random);
|
|
|
|
|
Vec3 vec;
|
|
|
|
|
public static @NotNull Vec3 randomOppositeHemisphere(@NotNull RandomGenerator random, @NotNull Vec3 direction) {
|
|
|
|
|
double x, y, z;
|
|
|
|
|
double squared;
|
|
|
|
|
do {
|
|
|
|
|
vec = random(random);
|
|
|
|
|
squared = vec.squared();
|
|
|
|
|
} while (squared > 1);
|
|
|
|
|
return vec.div(Math.sqrt(squared));
|
|
|
|
|
x = Math.fma(2, random.nextDouble(), -1);
|
|
|
|
|
y = Math.fma(2, random.nextDouble(), -1);
|
|
|
|
|
z = Math.fma(2, random.nextDouble(), -1);
|
|
|
|
|
squared = x * x + y * y + z * z;
|
|
|
|
|
} while (squared > 1 || direction.x() * x + direction.y() * y + direction.z() * z >= 0);
|
|
|
|
|
var factor = 1 / Math.sqrt(squared);
|
|
|
|
|
return new Vec3(x * factor, y * factor, z * factor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static @NotNull Vec3 reflect(@NotNull Vec3 vec, @NotNull Vec3 normal) {
|
|
|
|
|