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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::core::HitRecord;
use crate::core::ScatterRecord;
use crate::math::Ray;
use crate::types::{ColorRGB, FSize};
use std::error::Error;
use std::sync::Arc;

mod material_blend;
pub use self::material_blend::MaterialBlend;

mod dielectric;
pub use self::dielectric::Dielectric;

mod diffuse_light;
pub use self::diffuse_light::DiffuseLight;

mod isotropic;
pub use self::isotropic::Isotropic;

mod metal;
pub use self::metal::Metal;

mod no_material;
pub use self::no_material::NoMaterial;

mod lambertian;
pub use self::lambertian::Lambertian;

pub trait Material: Sync + Send {
    fn get_id(&self) -> usize;

    /// Scatter a ray at a point on the material.
    /// Returns a tuple with the color attenuation and outgoing ray and
    fn scatter(
        &self,
        self_material: Arc<dyn Material>,
        ray_in: &Ray,
        hit_record: &HitRecord,
    ) -> Option<ScatterRecord>;

    fn scattering_pdf(&self, ray_in: &Ray, hit_record: &HitRecord, scattered: &Ray) -> FSize;

    fn has_alpha(&self) -> bool;

    /// Get emitted material color
    fn emitted(&self, ray_in: &Ray, hit_record: &HitRecord) -> ColorRGB;

    fn accept(&self, visitor: &mut dyn Visitor) -> Result<(), Box<dyn Error>>;
}

pub trait Visitor {
    fn visit_no_material(&mut self, m: &NoMaterial) -> Result<(), Box<dyn Error>>;
    fn visit_lambertian(&mut self, m: &Lambertian) -> Result<(), Box<dyn Error>>;
    fn visit_metal(&mut self, m: &Metal) -> Result<(), Box<dyn Error>>;
    fn visit_dielectric(&mut self, m: &Dielectric) -> Result<(), Box<dyn Error>>;
    fn visit_isotropic(&mut self, m: &Isotropic) -> Result<(), Box<dyn Error>>;
    fn visit_diffuse_light(&mut self, m: &DiffuseLight) -> Result<(), Box<dyn Error>>;
    fn visit_material_blend(&mut self, m: &MaterialBlend) -> Result<(), Box<dyn Error>>;
}

#[cfg(test)]
mod test_visitor {
    use super::*;
    use crate::texture::ConstantTexture;
    use crate::types::ColorRGBA;
    use std::sync::Arc;

    struct TestVisitor {
        pub count: Vec<usize>,
    }

    impl TestVisitor {
        fn default() -> TestVisitor {
            TestVisitor {
                count: vec![0, 0, 0, 0, 0, 0, 0],
            }
        }

        pub fn evaluate(&self, index: usize, expected: usize) {
            for (i, count) in self.count.iter().enumerate() {
                assert_eq!(count, &if i == index { expected } else { 0 });
            }
        }
    }

    impl Visitor for TestVisitor {
        fn visit_no_material(&mut self, _: &NoMaterial) -> Result<(), Box<dyn Error>> {
            self.count[0] += 1;
            Ok(())
        }
        fn visit_lambertian(&mut self, _: &Lambertian) -> Result<(), Box<dyn Error>> {
            self.count[1] += 1;
            Ok(())
        }
        fn visit_metal(&mut self, _: &Metal) -> Result<(), Box<dyn Error>> {
            self.count[2] += 1;
            Ok(())
        }
        fn visit_dielectric(&mut self, _: &Dielectric) -> Result<(), Box<dyn Error>> {
            self.count[3] += 1;
            Ok(())
        }
        fn visit_isotropic(&mut self, _: &Isotropic) -> Result<(), Box<dyn Error>> {
            self.count[4] += 1;
            Ok(())
        }
        fn visit_diffuse_light(&mut self, _: &DiffuseLight) -> Result<(), Box<dyn Error>> {
            self.count[5] += 1;
            Ok(())
        }
        fn visit_material_blend(&mut self, _: &MaterialBlend) -> Result<(), Box<dyn Error>> {
            self.count[6] += 1;
            Ok(())
        }
    }

    #[test]
    pub fn test_visitor_no_material() {
        let m = NoMaterial::new();
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(0, 1);
    }

    #[test]
    pub fn test_visitor_lambertian() {
        let m = Lambertian::new(Arc::new(ConstantTexture::new(ColorRGBA::new(
            0.0, 0.0, 0.0, 1.0,
        ))));
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(1, 1);
    }

    #[test]
    pub fn test_visitor_metal() {
        let m = Metal::new(
            0.5,
            Arc::new(ConstantTexture::new(ColorRGBA::new(0.0, 0.0, 0.0, 1.0))),
        );
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(2, 1);
    }

    #[test]
    pub fn test_visitor_dielectric() {
        let m = Dielectric::new(
            0.5..0.5,
            Arc::new(ConstantTexture::new(ColorRGBA::new(1.0, 1.0, 1.0, 1.0))),
        );
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(3, 1);
    }

    #[test]
    pub fn test_visitor_isotropic() {
        let m = Isotropic::new(Arc::new(ConstantTexture::new(ColorRGBA::new(
            0.0, 0.0, 0.0, 1.0,
        ))));
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(4, 1);
    }

    #[test]
    pub fn test_visitor_diffuse_light() {
        let m = DiffuseLight::new(Arc::new(ConstantTexture::new(ColorRGBA::new(
            0.0, 0.0, 0.0, 1.0,
        ))));
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(5, 1);
    }

    #[test]
    pub fn test_visitor_material_blend() {
        let m = MaterialBlend::new(vec![
            (1.0, Arc::new(NoMaterial::new())),
            (1.0, Arc::new(NoMaterial::new())),
        ]);
        let mut v = TestVisitor::default();
        m.accept(&mut v).unwrap();
        v.evaluate(6, 1);
    }
}