pub use glam::Vec3;
use rand::Rng;
use rand_xoshiro::Xoshiro256PlusPlus;
#[inline]
pub fn random_unit_vec(rng: &mut Xoshiro256PlusPlus) -> Vec3 {
loop {
let p = Vec3::new(
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
);
let len_sq = p.length_squared();
if (1e-6..=1.0).contains(&len_sq) {
return p / len_sq.sqrt();
}
}
}
#[inline]
pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
v - 2.0 * v.dot(n) * n
}
#[inline]
pub fn near_zero(v: Vec3) -> bool {
const EPS: f32 = 1e-8;
v.x.abs() < EPS && v.y.abs() < EPS && v.z.abs() < EPS
}