phys-raycast 2.0.0

Ray casting functionality for 3D physics shapes
Documentation
// Copyright (C) 2020-2025 phys-raycast authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use phys_geom::math::Real;
use phys_geom::shape::Triangle;

use crate::{Raycast, RaycastHitResult};

impl Raycast for Triangle {
    fn raycast(
        &self,
        local_ray: phys_geom::Ray,
        max_distance: Real,
        discard_inside_hit: bool,
    ) -> Option<RaycastHitResult> {
        // Möller–Trumbore ray-triangle intersection algorithm
        let edge1 = self.b - self.a;
        let edge2 = self.c - self.a;
        let h = local_ray.direction.into_inner().cross(&edge2);
        let a = edge1.dot(&h);

        // Ray is parallel to the triangle
        if a > -Real::EPSILON && a < Real::EPSILON {
            return None;
        }

        // Backface culling: only accept hits from the front side (a > 0)
        if a < 0.0 {
            return None;
        }

        let f = 1.0 / a;
        let s = local_ray.origin - self.a;
        let u = f * s.dot(&h);

        // Ray misses the triangle
        if !(0.0..=1.0).contains(&u) {
            return None;
        }

        let q = s.cross(&edge1);
        let v = f * local_ray.direction.into_inner().dot(&q);

        // Ray misses the triangle
        if v < 0.0 || u + v > 1.0 {
            return None;
        }

        let t = f * edge2.dot(&q);

        // Ray intersection
        if t > Real::EPSILON {
            if t > max_distance {
                return None;
            }

            // Handle inside hit (ray starts very close to the triangle)
            if t < Real::EPSILON * 100.0 {
                if discard_inside_hit {
                    return None;
                }
                return Some(RaycastHitResult {
                    distance: 0.0,
                    normal: -self.normal(), // Return normal facing the ray
                });
            }

            return Some(RaycastHitResult {
                distance: t,
                normal: -self.normal(), // Return normal facing the ray
            });
        }

        None
    }
}

#[cfg(test)]
mod tests {
    use approx::assert_relative_eq;
    use phys_geom::math::*;

    use super::*;

    #[test]
    fn test_raycast_triangle_basic() {
        // Simple triangle in XY plane
        let triangle = Triangle::new(
            Point3::new(-1.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.0, 1.0, 0.0),
        );

        // Ray hitting from above
        let ray = phys_geom::Ray::new(
            Point3::new(0.0, 0.5, 2.0),
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, -1.0)),
        );

        let hit = triangle.raycast(ray, 10.0, false).unwrap();
        assert_relative_eq!(hit.distance, 2.0);
        assert_relative_eq!(
            hit.normal,
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, -1.0))
        );
    }

    #[test]
    fn test_raycast_triangle_from_below() {
        let triangle = Triangle::new(
            Point3::new(-1.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.0, 1.0, 0.0),
        );

        // Ray hitting from below
        let ray = phys_geom::Ray::new(
            Point3::new(0.0, 0.5, -2.0),
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, 1.0)),
        );

        assert_eq!(triangle.raycast(ray, 10.0, false), None); // Ray hits backface
    }

    #[test]
    fn test_raycast_triangle_miss() {
        let triangle = Triangle::new(
            Point3::new(-1.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.0, 1.0, 0.0),
        );

        // Ray that misses the triangle
        let ray = phys_geom::Ray::new(
            Point3::new(2.0, 2.0, 2.0),
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, -1.0)),
        );

        assert_eq!(triangle.raycast(ray, 10.0, false), None);
    }

    #[test]
    fn test_raycast_triangle_parallel() {
        let triangle = Triangle::new(
            Point3::new(-1.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.0, 1.0, 0.0),
        );

        // Ray parallel to the triangle
        let ray = phys_geom::Ray::new(
            Point3::new(0.0, 0.5, 2.0),
            UnitVec3::new_normalize(Vec3::new(1.0, 0.0, 0.0)),
        );

        assert_eq!(triangle.raycast(ray, 10.0, false), None);
    }

    #[test]
    fn test_raycast_triangle_max_distance() {
        let triangle = Triangle::new(
            Point3::new(-1.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.0, 1.0, 0.0),
        );

        let ray = phys_geom::Ray::new(
            Point3::new(0.0, 0.5, 5.0),
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, -1.0)),
        );

        // Max distance too short
        assert_eq!(triangle.raycast(ray, 1.0, false), None);

        // Max distance sufficient
        let hit = triangle.raycast(ray, 10.0, false).unwrap();
        assert_relative_eq!(hit.distance, 5.0);
    }

    #[test]
    fn test_raycast_triangle_tilted() {
        // Tilted triangle
        let triangle = Triangle::new(
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 1.0),
            Point3::new(0.0, 1.0, 1.0),
        );

        // Ray hitting the tilted triangle
        let ray = phys_geom::Ray::new(
            Point3::new(0.2, 0.3, 2.0),
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, -1.0)),
        );

        let hit = triangle.raycast(ray, 10.0, false).unwrap();
        assert!(hit.distance > 0.0 && hit.distance < 2.0);

        // Normal should point roughly upward
        assert!(hit.normal.y > 0.0);
        assert!(hit.normal.z < 0.0);
    }

    #[test]
    fn test_raycast_triangle_edge_case() {
        let triangle = Triangle::new(
            Point3::new(0.0, 0.0, 0.0),
            Point3::new(1.0, 0.0, 0.0),
            Point3::new(0.0, 1.0, 0.0),
        );

        // Ray hitting right at the edge
        let ray = phys_geom::Ray::new(
            Point3::new(0.0, 0.0, 1.0),
            UnitVec3::new_normalize(Vec3::new(0.0, 0.0, -1.0)),
        );

        let hit = triangle.raycast(ray, 10.0, false).unwrap();
        assert_relative_eq!(hit.distance, 1.0);
    }
}