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
use std::ops::{Add, Mul, Neg, Sub};

/// A 3 dimensional vector of `f64`
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Vec3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vec3 {
    /// The Euclidean norm
    pub fn magnitude(self) -> f64 {
        self.dot(self).sqrt()
    }

    /// Dot product with another Vec3
    pub fn dot(self, other: Vec3) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Normalize this Vec3 so that it has a magnitude of 1
    ///
    /// Will panic with a div by zero if the self.magnitude() is zero
    pub fn normalized(self) -> Self {
        (1.0 / self.magnitude()) * self
    }

    /// Reflect this vector about a normal vec.
    pub fn reflect(self, norm: Vec3) -> Self {
        let norm = norm.normalized();
        self - 2.0 * self.dot(norm) * norm
    }

    /// Cross product with another Vec3
    pub fn cross(self, other: Vec3) -> Self {
        Self {
            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,
        }
    }

    /// Simulate Snell's law refrection through a surface with normal vec.
    ///
    /// eta is the relative index of refraction, ri1 / ri2
    pub fn refract(self, norm: Vec3, eta: f64) -> Self {
        let k = 1.0 - eta * eta * (1.0 - self.dot(norm) * self.dot(norm));
        if k < 0.0 {
            Default::default()
        } else {
            eta * self - (eta * self.dot(norm) + k.sqrt()) * norm
        }
    }

    /// Linearly interpolate between two Vec3 with an interpolation value `t`
    pub fn lerp(self, other: Vec3, t: f64) -> Self {
        self + t * (other - self)
    }
}

impl Add<Vec3> for Vec3 {
    type Output = Vec3;

    fn add(self, rhs: Vec3) -> Self::Output {
        Vec3 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

impl Sub<Vec3> for Vec3 {
    type Output = Vec3;

    fn sub(self, rhs: Vec3) -> Self::Output {
        Vec3 {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

impl Neg for Vec3 {
    type Output = Vec3;

    fn neg(self) -> Self::Output {
        Vec3 {
            x: -self.x,
            y: -self.y,
            z: -self.z,
        }
    }
}

impl Mul<Vec3> for f64 {
    type Output = Vec3;

    fn mul(self, rhs: Vec3) -> Self::Output {
        Vec3 {
            x: self * rhs.x,
            y: self * rhs.y,
            z: self * rhs.z,
        }
    }
}

impl Mul<f64> for Vec3 {
    type Output = Vec3;
    fn mul(self, rhs: f64) -> Self::Output {
        // scalar multiply is commutative, use impl above
        rhs * self
    }
}

impl Add<f64> for Vec3 {
    type Output = Vec3;

    fn add(self, rhs: f64) -> Self::Output {
        Vec3 {
            x: self.x + rhs,
            y: self.y + rhs,
            z: self.z + rhs,
        }
    }
}

impl Default for Vec3 {
    fn default() -> Self {
        Vec3 {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        }
    }
}

impl From<(f64, f64, f64)> for Vec3 {
    fn from((x, y, z): (f64, f64, f64)) -> Self {
        Vec3 { x, y, z }
    }
}

impl From<(i32, i32, i32)> for Vec3 {
    fn from((x, y, z): (i32, i32, i32)) -> Self {
        Vec3 {
            x: x as f64,
            y: y as f64,
            z: z as f64,
        }
    }
}

impl From<f64> for Vec3 {
    fn from(n: f64) -> Self {
        Vec3 { x: n, y: n, z: n }
    }
}

impl From<i32> for Vec3 {
    fn from(n: i32) -> Self {
        let n = n as f64;
        Vec3 { x: n, y: n, z: n }
    }
}

impl From<Vec3> for u32 {
    fn from(vec: Vec3) -> Self {
        u32::from_be_bytes([
            0,
            to_color_byte(vec.x),
            to_color_byte(vec.y),
            to_color_byte(vec.z),
        ])
    }
}

fn to_color_byte(val: f64) -> u8 {
    let val = clamp(val, 0.0, 1.0);
    (val * 255.0) as u8
}

fn clamp(val: f64, min: f64, max: f64) -> f64 {
    if val < min {
        min
    } else if val > max {
        max
    } else {
        val
    }
}