raytracing-rs

https://raytracing.github.io in Rust

git clone https://code.pdelong.com/raytracing-rs.git

 1use super::Hittable;
 2use crate::math::Interval;
 3
 4pub struct HittableList {
 5    list: Vec<Box<dyn Hittable + Send + Sync>>,
 6}
 7
 8impl HittableList {
 9    pub fn new() -> HittableList {
10        HittableList { list: Vec::new() }
11    }
12
13    pub fn push(&mut self, h: Box<dyn Hittable + Send + Sync>) {
14        self.list.push(h);
15    }
16}
17
18impl Hittable for HittableList {
19    fn hit(&self, r: &crate::math::Ray, range: &Interval) -> Option<super::HitRecord> {
20        self.list
21            .iter()
22            .fold((None, range.clone()), |(hit, range), h| {
23                match h.hit(r, &range) {
24                    Some(hit) => {
25                        let range = Interval::new(0., hit.t.get()).unwrap();
26                        (Some(hit), range)
27                    }
28                    None => (hit, range),
29                }
30            })
31            .0
32    }
33}