diff options
| author | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-01-12 22:07:57 +0100 |
|---|---|---|
| committer | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-01-12 22:07:57 +0100 |
| commit | 9d44f7ab04e6f6979e0eebc24f8fb439a23a3865 (patch) | |
| tree | 5194f6bd792c8ccf7a164582a1ebb5dc51e3a98c /racer-tracer/src/vec3.rs | |
| parent | a6302805d19273c95278c8d792ffbd9b2633fe20 (diff) | |
| download | racer-tracer-9d44f7ab04e6f6979e0eebc24f8fb439a23a3865.tar.gz racer-tracer-9d44f7ab04e6f6979e0eebc24f8fb439a23a3865.tar.xz racer-tracer-9d44f7ab04e6f6979e0eebc24f8fb439a23a3865.zip | |
🧹 Minor cleanup
- Made the traits into supertraits so we don't have to mention Send and
Sync everywhere.
- Add methods for Vec3 that modifies the existing Vector. Can be used
to make less copies.
Diffstat (limited to 'racer-tracer/src/vec3.rs')
| -rw-r--r-- | racer-tracer/src/vec3.rs | 94 |
1 files changed, 65 insertions, 29 deletions
diff --git a/racer-tracer/src/vec3.rs b/racer-tracer/src/vec3.rs index e092754..659d3ed 100644 --- a/racer-tracer/src/vec3.rs +++ b/racer-tracer/src/vec3.rs @@ -26,6 +26,44 @@ impl Vec3 { &self.data[2] } + pub fn add(&mut self, v: Vec3) { + self.data[0] += v.data[0]; + self.data[1] += v.data[1]; + self.data[2] += v.data[2]; + } + + pub fn sub(&mut self, v: Vec3) { + self.data[0] -= v.data[0]; + self.data[1] -= v.data[1]; + self.data[2] -= v.data[2]; + } + + pub fn div(&mut self, v: f64) { + self.data[0] /= v; + self.data[1] /= v; + self.data[2] /= v; + } + + pub fn mul(&mut self, v: f64) { + self.data[0] *= v; + self.data[1] *= v; + self.data[2] *= v; + } + + pub fn reflect(&mut self, mut v: Vec3) { + let double_dot = 2.0 * self.dot(&v); + v.mul(double_dot); + self.sub(v); + } + + pub fn unit_vector(mut self) -> Vec3 { + let len = self.length(); + self.data[0] /= len; + self.data[1] /= len; + self.data[2] /= len; + self + } + pub fn length(&self) -> f64 { f64::sqrt(self.length_squared()) } @@ -42,10 +80,6 @@ impl Vec3 { cross(self, v) } - pub fn unit_vector(&self) -> Vec3 { - unit_vector(self) - } - pub fn as_color(&self) -> u32 { let red: u32 = (self.data[0] * 255.0) as u32; let green: u32 = (self.data[1] * 255.0) as u32; @@ -83,31 +117,6 @@ impl Vec3 { } } -pub fn reflect(v1: &Vec3, v2: &Vec3) -> Vec3 { - v1 - 2.0 * v1.dot(v2) * v2 -} - -pub fn random_in_unit_sphere() -> Vec3 { - let mut v = Vec3::random_range(-1.0, 1.0); - while v.length_squared() >= 1.0 { - v = Vec3::random_range(-1.0, 1.0); - } - v -} - -pub fn random_in_hemisphere(normal: &Vec3) -> Vec3 { - let unit_sphere = random_in_unit_sphere(); - if unit_sphere.dot(normal) > 0.0 { - unit_sphere - } else { - -unit_sphere - } -} - -pub fn random_unit_vector() -> Vec3 { - random_in_unit_sphere().unit_vector() -} - pub fn dot(v1: &Vec3, v2: &Vec3) -> f64 { v1.data[0] * v2.data[0] + v1.data[1] * v2.data[1] + v1.data[2] * v2.data[2] } @@ -120,6 +129,7 @@ pub fn cross(v1: &Vec3, v2: &Vec3) -> Vec3 { ) } +#[allow(dead_code)] pub fn unit_vector(v: &Vec3) -> Vec3 { v / v.length() } @@ -291,6 +301,32 @@ impl std::fmt::Debug for Vec3 { } } +pub fn reflect(v1: &Vec3, v2: &Vec3) -> Vec3 { + v1 - 2.0 * v1.dot(v2) * v2 +} + +pub fn random_in_unit_sphere() -> Vec3 { + let mut v = Vec3::random_range(-1.0, 1.0); + while v.length_squared() >= 1.0 { + v = Vec3::random_range(-1.0, 1.0); + } + v +} + +#[allow(dead_code)] +pub fn random_in_hemisphere(normal: &Vec3) -> Vec3 { + let unit_sphere = random_in_unit_sphere(); + if unit_sphere.dot(normal) > 0.0 { + unit_sphere + } else { + -unit_sphere + } +} + +pub fn random_unit_vector() -> Vec3 { + random_in_unit_sphere().unit_vector() +} + #[cfg(test)] mod tests { use super::*; |
