aboutsummaryrefslogtreecommitdiff
path: root/src/world/container/list.rs
blob: 22b6d88be0be50407a4662919eea23417bdaa7fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use crate::world::{Object, Hittable, Intersection};
use crate::core::{Bound3f, Ray};


pub struct HittableList {
    elems: Vec<Object>,
}

impl HittableList {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn add(&mut self, h: Object) {
        self.elems.push(h);
    }
}

impl Hittable for HittableList {
    fn intersect(&self, ray: &Ray) -> Option<Intersection> {
        let mut min: Option<Intersection> = None;

        for e in self.elems.iter() {
            if let Some(i) = e.intersect(&ray) {
                match min {
                    // Do nothing if distance is bigger than min
                    Some(ref min_i) if min_i.t < i.t => {},
                    // If no existing min or closer than
                    _ => min = Some(i),
                }
            }
        }

        min
    }

    fn bounding_box(&self) -> Bound3f {
        let mut bound: Bound3f = Bound3f::EMPTY;

        for e in self.elems.iter() {
            let eb = e.bounding_box();
            bound = bound.combine(&eb);
        }

        bound
    }
}

impl Default for HittableList {
    fn default() -> Self {
        Self {
            elems: Vec::new(),
        }
    }
}