use glam::{Vec3A, vec3a};
pub fn safe_inverse(x: f32) -> f32 {
if x.abs() <= f32::EPSILON {
x.signum() / f32::EPSILON
} else {
1.0 / x
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct Ray {
pub origin: Vec3A,
pub direction: Vec3A,
pub inv_direction: Vec3A,
pub tmin: f32,
pub tmax: f32,
}
impl Ray {
pub fn new(origin: Vec3A, direction: Vec3A, min: f32, max: f32) -> Self {
let ray = Ray {
origin,
direction,
inv_direction: vec3a(
safe_inverse(direction.x),
safe_inverse(direction.y),
safe_inverse(direction.z),
),
tmin: min,
tmax: max,
};
debug_assert!(ray.inv_direction.is_finite());
debug_assert!(ray.direction.is_finite());
debug_assert!(origin.is_finite());
ray
}
pub fn new_inf(origin: Vec3A, direction: Vec3A) -> Self {
Self::new(origin, direction, 0.0, f32::INFINITY)
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct RayHit {
pub primitive_id: u32,
pub geometry_id: u32,
pub instance_id: u32,
pub t: f32,
}
pub const INVALID_ID: u32 = u32::MAX;
impl RayHit {
pub fn none() -> Self {
Self {
primitive_id: INVALID_ID,
geometry_id: INVALID_ID,
instance_id: INVALID_ID,
t: f32::INFINITY,
}
}
}