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
use rand::rngs::StdRng;

use super::{HitRecord, Ray, Shape};

/// A plane represented by the linear equation x • normal = value
#[derive(Copy, Clone)]
pub struct Plane {
    /// The normal vector
    pub normal: glm::DVec3,

    /// The distance from the origin
    pub value: f64,
}

impl Shape for Plane {
    /// Ray-plane intersection
    fn intersect(&self, ray: &Ray, t_min: f64, record: &mut HitRecord) -> bool {
        let cosine = self.normal.dot(&ray.dir);
        if cosine.abs() < 1e-8 {
            // Parallel ray and plane
            return false;
        }

        let time = (self.value - self.normal.dot(&ray.origin)) / cosine;
        if time >= t_min && time < record.time {
            record.time = time;
            record.normal = -self.normal.normalize() * cosine.signum();
            true
        } else {
            false
        }
    }

    fn sample(&self, _target: &glm::DVec3, _rng: &mut StdRng) -> (glm::DVec3, glm::DVec3, f64) {
        unimplemented!()
    }
}