aboutsummaryrefslogtreecommitdiff
path: root/src/core/spectrum.rs
blob: ed3505be61178c4a0702cea055f2745bdbd8c023 (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
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
//! Used to represent color
//!
//! Currently only implements RGB colors
use crate::Float;

// TODO implement SampledSpectrum instead for nicer images

#[derive(Clone, Copy, Default)]
pub struct Spectrum {
    c: [Float; 3],
}

impl Spectrum {
    pub const ZERO: Self = Spectrum { c: [0.0; 3] };
    pub const WHITE: Self = Spectrum { c: [1.0; 3] };

    pub fn new_rgb(r: Float, g: Float, b: Float) -> Spectrum {
        Spectrum { c: [r, g, b] }
    }

    pub fn to_rgb(&self, scale: Float) -> (Float, Float, Float) {
        (self.c[0] * scale, self.c[1] * scale, self.c[2] * scale)
    }

    pub fn gamma_correct(&self) -> Self {
        Self::new_rgb(
            self.c[0].sqrt(),
            self.c[1].sqrt(),
            self.c[2].sqrt(),
            )
    }
}

impl std::ops::Mul<Float> for Spectrum {
    type Output = Spectrum;

    fn mul(self, op: Float) -> Self::Output {
        Self::Output::new_rgb(
            self.c[0] * op,
            self.c[1] * op,
            self.c[2] * op,
            )
    }
}

impl std::ops::Mul for Spectrum {
    type Output = Spectrum;

    fn mul(self, op: Self) -> Self::Output {
        Self::Output::new_rgb(
            self.c[0] * op.c[0],
            self.c[1] * op.c[1],
            self.c[2] * op.c[2],
            )
    }
}

impl std::ops::Div<Float> for Spectrum {
    type Output = Spectrum;

    fn div(self, op: Float) -> Self::Output {
        Self::Output::new_rgb(
            self.c[0] / op,
            self.c[1] / op,
            self.c[2] / op,
            )
    }
}

impl std::ops::Add for Spectrum {
    type Output = Spectrum;

    fn add(self, op: Self) -> Self::Output {
        Self::Output::new_rgb(
            self.c[0] + op.c[0],
            self.c[1] + op.c[1],
            self.c[2] + op.c[2],
            )
    }
}

impl std::ops::AddAssign<&Self> for Spectrum {
    fn add_assign(&mut self, op: &Self) {
        self.c[0] += op.c[0];
        self.c[1] += op.c[1];
        self.c[2] += op.c[2];
    }
}