raytracer 0.2.2

Toy raytracer in Rust
Documentation
use std::sync::Arc;
use std::slice::Iter;

use crate::{
    HitData,
    Ray,
    Shape
};



/// [`Shape`] that is made up of a list of various shapes.
///
/// [`Shape`]: shape/trait.Shape.html
pub struct ShapeList(Vec<Arc<dyn Shape>>);

impl ShapeList {
    pub fn new() -> ShapeList {
        ShapeList(Vec::new())
    }

    pub fn add(&mut self, shape: Arc<dyn Shape>) {
        self.0.push(shape);
    }

    pub fn iter(&self) -> Iter<Arc<dyn Shape>> {
        self.0.iter()
    }
}

impl Shape for ShapeList {
    fn hit(&self, r: &Ray, t_min: f32, t_max: f32) -> Option<HitData> {
        let mut t = t_max;
        let mut hit_data: Option<HitData> = None;

        for shape in self.iter() {
            if let Some(new_hit_data) = shape.hit(&r, t_min, t) {
                t = new_hit_data.t;
                hit_data.replace(new_hit_data);
            }
        }

        hit_data
    }
}