use phys_geom::math::{Real, *};
use phys_geom::shape::Cuboid;
use phys_geom::ComputeAabb3;
use crate::{Raycast, RaycastHitResult};
impl Raycast for Cuboid {
fn raycast(
&self,
local_ray: phys_geom::Ray,
max_distance: Real,
discard_inside_hit: bool,
) -> Option<RaycastHitResult> {
let one_over_direction = local_ray.one_over_direction();
let aabb = self.compute_aabb();
if let Some((entry, max_entry)) = aabb.raycast_by_one_over_direction_impl(
local_ray.origin,
one_over_direction,
max_distance,
) {
if max_entry <= 0.0 {
if discard_inside_hit {
return None;
}
return Some(RaycastHitResult {
distance: 0.0,
normal: -local_ray.direction,
});
}
let mut normal: UnitVec3;
let inverse;
#[allow(clippy::float_cmp)]
if entry.x == max_entry {
normal = Vec3::x_axis();
inverse = local_ray.direction.x > 0.0;
} else if entry.y == max_entry {
normal = Vec3::y_axis();
inverse = local_ray.direction.y > 0.0;
} else {
normal = Vec3::z_axis();
inverse = local_ray.direction.z > 0.0;
}
if inverse {
normal = -normal;
}
Some(RaycastHitResult {
distance: max_entry,
normal,
})
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use phys_geom::math::*;
use phys_geom::Ray;
use super::*;
#[test]
fn test_raycast() {
let cuboid = Cuboid::new(Vec3::new(1.0, 1.0, 1.0) * 2.0);
let ray_z = Ray::new_with_vec3(Point3::new(0.0, 0.0, 2.0), Vec3::new(0.0, 0.0, -1.0));
assert_eq!(
cuboid.raycast(ray_z, 5.0, false),
Some(RaycastHitResult {
distance: 1.0,
normal: UnitVec3::new_normalize(Vec3::new(0.0, 0.0, 1.0)),
})
);
assert_eq!(cuboid.raycast(ray_z, 0.5, false), None);
assert_eq!(
cuboid.raycast(
Ray::new_with_vec3(Point3::new(2.0, 0.0, 0.0), Vec3::new(-1.0, 0.0, 0.0)),
5.0,
false
),
Some(RaycastHitResult {
distance: 1.0,
normal: UnitVec3::new_normalize(Vec3::new(1.0, 0.0, 0.0)),
})
);
assert_eq!(
cuboid.raycast(
Ray::new_with_vec3(Point3::new(0.0, 2.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
5.0,
false
),
Some(RaycastHitResult {
distance: 1.0,
normal: Vec3::y_axis(),
})
);
assert_eq!(
cuboid.raycast(
Ray::new_with_vec3(Point3::new(-2.0, 0.0, 0.0), Vec3::new(1.0, 0.0, 0.0)),
5.0,
false
),
Some(RaycastHitResult {
distance: 1.0,
normal: -Vec3::x_axis(),
})
);
assert_eq!(
cuboid.raycast(
Ray::new_with_vec3(Point3::new(0.0, -2.0, 0.0), Vec3::new(0.0, 1.0, 0.0)),
5.0,
false
),
Some(RaycastHitResult {
distance: 1.0,
normal: -Vec3::y_axis(),
})
);
assert_eq!(
cuboid.raycast(
Ray::new_with_vec3(Point3::new(0.0, 0.0, -2.0), Vec3::new(0.0, 0.0, 1.0)),
5.0,
false
),
Some(RaycastHitResult {
distance: 1.0,
normal: -Vec3::z_axis(),
})
);
{
let ray = Ray::new_with_vec3(Point3::new(0.0, 0.0, 2.0), Vec3::new(3.0, 0.0, -2.0));
assert_eq!(cuboid.raycast(ray, 10.0, false), None);
}
}
#[test]
fn test_raycast_surface() {
let cuboid = Cuboid::new(Vec3::new(1.0, 1.0, 1.0) * 2.0);
let ray = Ray::new_with_vec3(Point3::new(0.0, 0.0, 1.0), Vec3::new(0.0, 0.0, 1.0));
assert_eq!(cuboid.raycast(ray, 10.0, true), None);
assert_eq!(
cuboid.raycast(ray, 10.0, false),
Some(RaycastHitResult {
distance: 0.0,
normal: -ray.direction,
})
);
}
#[test]
fn test_raycast_inner() {
let cuboid = Cuboid::new(Vec3::new(1.0, 1.0, 1.0) * 2.0);
let ray = Ray::new_with_vec3(Point3::new(0.0, 0.0, 0.0), Vec3::new(0.0, 0.0, -1.0));
assert_eq!(
cuboid.raycast(ray, 5.0, false),
Some(RaycastHitResult {
distance: 0.0,
normal: -ray.direction,
})
);
assert_eq!(cuboid.raycast(ray, 5.0, true), None);
}
}