summaryrefslogtreecommitdiff
path: root/src/vec3.rs
blob: 1bf864de89a4002f6ad8b8aff7c02a552f60e591 (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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use auto_ops::{impl_op_ex, impl_op_ex_commutative};
use std::fmt;

pub type Point3 = Vec3;
pub type Color = Vec3;

#[derive(Clone, Default, Debug)]
pub struct Vec3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vec3 {
    pub fn new() -> Vec3 {
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        }
    }

    pub fn get(&self, index: usize) -> Option<&f64> {
        match index {
            0 => Some(&self.x),
            1 => Some(&self.y),
            2 => Some(&self.z),
            _ => None,
        }
    }

    pub fn get_mut(&mut self, index: usize) -> Option<&mut f64> {
        match index {
            0 => Some(&mut self.x),
            1 => Some(&mut self.y),
            2 => Some(&mut self.z),
            _ => None,
        }
    }

    pub fn length(&self) -> f64 {
        self.length_squared().sqrt()
    }

    pub fn length_squared(&self) -> f64 {
        self.x * self.x + self.y * self.y + self.z * self.z
    }

    pub fn dot(&self, other: &Vec3) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    pub fn cross(&self, other: &Vec3) -> Vec3 {
        Vec3 {
            x: self.y * other.z - self.z * other.y,
            y: self.z * other.x - self.x * other.z,
            z: self.x * other.y - self.y * other.x,
        }
    }

    pub fn unit_vector(&self) -> Vec3 {
        self / self.length()
    }

    pub fn random() -> Vec3 {
        Vec3 {
            x: rand::random::<f64>(),
            y: rand::random::<f64>(),
            z: rand::random::<f64>(),
        }
    }

    pub fn random_in_range(min: f64, max: f64) -> Vec3 {
        Vec3 {
            x: min + (max - min) * rand::random::<f64>(),
            y: min + (max - min) * rand::random::<f64>(),
            z: min + (max - min) * rand::random::<f64>(),
        }
    }

    pub fn random_in_unit_sphere() -> Vec3 {
        loop {
            let p = Vec3 {
                x: 2.0 * rand::random::<f64>() - 1.0,
                y: 2.0 * rand::random::<f64>() - 1.0,
                z: 2.0 * rand::random::<f64>() - 1.0,
            };
            if p.length_squared() < 1.0 {
                return p;
            }
        };
    }

    pub fn random_unit_vector() -> Vec3 {
        Self::random_in_unit_sphere().unit_vector()
    }

    pub fn random_in_unit_disk() -> Vec3 {
        loop {
            let p = Vec3 {
                x: 2.0 * rand::random::<f64>() -1.0,
                y: 2.0 * rand::random::<f64>() -1.0,
                z: 0.0,
            };
            if p.length_squared() < 1.0 {
                return p;
            }
        }
    }

    pub fn near_zero(&self) -> bool {
        const S: f64 = 1e-8;
        self.x.abs() < S && self.y.abs() < S && self.z.abs() < S
    }

    pub fn reflect(&self, normal: &Vec3) -> Vec3 {
        self - 2.0 * self.dot(normal) * normal
    }

    pub fn refract(&self, normal: &Vec3, etai_over_etat: f64) -> Vec3 {
        let cos_theta = normal.dot(&-self).min(1.0);
        let r_out_perp = etai_over_etat * (self + cos_theta * normal);
        let r_out_parallel = -((1.0 - r_out_perp.length_squared()).abs().sqrt()) * normal;
        r_out_perp + r_out_parallel
    }

    pub fn has_infinite_member(&self) -> bool {
        self.x.is_infinite() || self.y.is_infinite() || self.z.is_infinite()
    }
    
    // fn hue_to_rgb(&self) -> f64 {
    //     let mut z = self.z;
    //     if z < 0.0 { z += 1.0 }
    //     if z > 1.0 { z -= 1.0 }
    //     if z < 1.0/6.0 {
    //         self.x + (self.y - self.x) * 6.0 * z
    //     } else if z < 1.0/2.0 {
    //         self.y
    //     } else if z < 2.0/3.0 {
    //         self.x + (self.y - self.x) * (2.0/3.0 - z) * 6.0
    //     } else {
    //         self.x
    //     }
    // }
    
    fn hue_to_rgb(p: f64, q: f64, t:f64) -> f64 {
        let mut t = t;
        if t < 0.0 { t += 1.0 }
        if t > 1.0 { t -= 1.0 }
        if t < 1.0/6.0 {
            p + (q - p) * 6.0 * t
        } else if t < 1.0/2.0 {
            q
        } else if t < 2.0/3.0 {
            p + (q - p) * (2.0/3.0 - t) * 6.0
        } else {
            p
        }
    }

    pub fn hsl_to_rgb(&self) -> Vec3 {
        if self.y == 0.0 {
            Vec3 {x: self.z, y: self.z, z: self.z}
        } else {
            let q = if self.z < 0.5 {
                self.z * (1.0 + self.y)
            } else {
                self.z + self.y - self.z * self.y
            };
            let p = 2.0 * self.z - q;
            Vec3{x: Self::hue_to_rgb(p, q, self.x + 1.0/3.0), y: Self::hue_to_rgb(p, q, self.x), z: Self::hue_to_rgb(p, q, self.x - 1.0/3.0)}
        }
    }
    
    pub fn xy_diff(&self, func: &dyn Fn(&Vec3) -> f64) -> Vec3 {
        let h = f64::EPSILON.powf(1.0 / 3.0);
        let here = func(self);
        let x_there = func(&(self + Vec3{x: h, y: 0.0, z: 0.0 }));
        let y_there = func(&(self + Vec3{x: 0.0, y: h, z: 0.0 }));
        let x_diff = (x_there - here) / h;
        let y_diff = (y_there - here) / h;
        Vec3 {
            x: x_diff,
            y: y_diff,
            z: 0.0,
        }
    }

}

impl fmt::Display for Vec3 {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {} {}", self.x, self.y, self.z)
    }
}

impl<'a> IntoIterator for &'a Vec3 {
    type Item = f64;
    type IntoIter = Vec3Iterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        Vec3Iterator {
            vec3: self,
            index: 0,
        }
    }
}

pub struct Vec3Iterator<'a> {
    vec3: &'a Vec3,
    index: usize,
}

impl<'a> Iterator for Vec3Iterator<'a> {
    type Item = f64;
    fn next(&mut self) -> Option<f64> {
        let result = match self.index {
            0 => self.vec3.x,
            1 => self.vec3.y,
            2 => self.vec3.z,
            _ => return None,
        };
        self.index += 1;
        Some(result)
    }
}

impl_op_ex!(- |a: &Vec3| -> Vec3 {
    Vec3 {
        x: -a.x,
        y: -a.y,
        z: -a.z,
    }
});
impl_op_ex!(+= |lhs: &mut Vec3, rhs: Vec3| { *lhs = Vec3 { x: lhs.x + rhs.x, y: lhs.y + rhs.y, z: lhs.z + rhs.z } });
impl_op_ex!(*= |lhs: &mut Vec3, rhs: &f64| { *lhs = Vec3 { x: lhs.x * rhs, y: lhs.y * rhs, z: lhs.z * rhs } });
impl_op_ex!(/= |lhs: &mut Vec3, rhs: &f64| { *lhs *= 1.0 / rhs });
impl_op_ex!(+ |lhs: &Vec3, rhs: &Vec3| -> Vec3 { Vec3 { x: lhs.x + rhs.x, y: lhs.y + rhs.y, z: lhs.z + rhs.z } });
impl_op_ex!(-|lhs: &Vec3, rhs: &Vec3| -> Vec3 {
    Vec3 {
        x: lhs.x - rhs.x,
        y: lhs.y - rhs.y,
        z: lhs.z - rhs.z,
    }
});
impl_op_ex!(*|lhs: &Vec3, rhs: &Vec3| -> Vec3 {
    Vec3 {
        x: lhs.x * rhs.x,
        y: lhs.y * rhs.y,
        z: lhs.z * rhs.z,
    }
});
impl_op_ex_commutative!(*|lhs: &Vec3, rhs: &f64| -> Vec3 {
    Vec3 {
        x: lhs.x * rhs,
        y: lhs.y * rhs,
        z: lhs.z * rhs,
    }
});
impl_op_ex!(/ |lhs: &Vec3, rhs: &f64| -> Vec3 { lhs * (1.0/rhs) });