Skip to main content

geodesic/traits/
traceable.rs

1//! Traceable trait.
2
3use nalgebra::RealField;
4
5use crate::{
6    error::Result,
7    rt::{Hit, Ray},
8};
9
10/// Trait for types which can be tested for intersection by `Ray`s.
11pub trait Traceable<T: RealField + Copy> {
12    /// Test for an intersection between a `Ray` and this geometry.
13    /// Returns the closest intersection if any, with the appropriate object index.
14    ///
15    /// # Errors
16    ///
17    /// Returns an error if the intersection calculation fails due to mathematical
18    /// operations or invalid geometric configurations.
19    fn intersect(&self, ray: &Ray<T>) -> Result<Option<Hit<T>>>;
20
21    /// Test if a `Ray` intersects this geometry (shadow ray optimization).
22    /// Returns true if there's any intersection within `max_distance`.
23    ///
24    /// # Errors
25    ///
26    /// Returns an error if the intersection test fails due to mathematical
27    /// operations or invalid geometric configurations.
28    fn intersect_any(&self, ray: &Ray<T>, max_distance: T) -> Result<bool> {
29        // Default implementation: just check if there's a hit within range
30        (self.intersect(ray)?).map_or(Ok(false), |hit| Ok(hit.distance <= max_distance))
31    }
32}