From 40bfe6d74f00244df21963a4ef4df4b7bc978600 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 23 Jun 2024 15:07:41 -0400 Subject: [PATCH] Add addtional ops declarations to reduce cloning --- ray-tracer/src/transforms.rs | 6 +-- ray-tracer/src/types/matrix.rs | 47 +++++++++++++++++--- ray-tracer/src/types/ray.rs | 4 +- ray-tracer/src/types/tuple.rs | 78 +++++++++++++++++++++++++++++++--- 4 files changed, 117 insertions(+), 18 deletions(-) diff --git a/ray-tracer/src/transforms.rs b/ray-tracer/src/transforms.rs index ce48829..22539a3 100644 --- a/ray-tracer/src/transforms.rs +++ b/ray-tracer/src/transforms.rs @@ -71,7 +71,7 @@ mod tests { fn multiply_by_translation_matrix() { let tr = translation(5., -3., 2.); let p = Point::new(-3., 4., 5.); - assert_eq!(tr.clone() * p, Point::new(2., 1., 7.)); + assert_eq!(&tr * p, Point::new(2., 1., 7.)); let inv = tr.inverse(); assert_eq!(inv * p, Point::new(-8., 7., 3.)); @@ -81,7 +81,7 @@ mod tests { fn translation_does_not_affect_vectors() { let tr = translation(5., -3., 2.); let v = Vector::new(-3., 4., 5.); - assert_eq!(tr.clone() * v, v); + assert_eq!(tr * v, v); } #[test] @@ -90,7 +90,7 @@ mod tests { let p = Point::new(-4., 6., 8.); let v = Vector::new(-4., 6., 8.); - assert_eq!(tr.clone() * p, Point::new(-8., 18., 32.)); + assert_eq!(&tr * p, Point::new(-8., 18., 32.)); assert_eq!(tr * v, Vector::new(-8., 18., 32.)); } diff --git a/ray-tracer/src/types/matrix.rs b/ray-tracer/src/types/matrix.rs index 4a6f3a9..226a42c 100644 --- a/ray-tracer/src/types/matrix.rs +++ b/ray-tracer/src/types/matrix.rs @@ -169,9 +169,9 @@ impl PartialEq for Matrix { } } -impl std::ops::Mul for Matrix { +impl std::ops::Mul<&Matrix> for &Matrix { type Output = Matrix; - fn mul(self, rside: Matrix) -> Matrix { + fn mul(self, rside: &Matrix) -> Matrix { assert_eq!(self.size, 4); assert_eq!(rside.size, 4); @@ -189,7 +189,28 @@ impl std::ops::Mul for Matrix { } } -impl std::ops::Mul for Matrix { +impl std::ops::Mul for &Matrix { + type Output = Matrix; + fn mul(self, rside: Matrix) -> Matrix { + self * &rside + } +} + +impl std::ops::Mul<&Matrix> for Matrix { + type Output = Matrix; + fn mul(self, rside: &Matrix) -> Matrix { + &self * rside + } +} + +impl std::ops::Mul for Matrix { + type Output = Matrix; + fn mul(self, rside: Matrix) -> Matrix { + &self * &rside + } +} + +impl std::ops::Mul for &Matrix { type Output = Tuple; fn mul(self, rside: Tuple) -> Tuple { assert_eq!(self.size, 4); @@ -207,13 +228,27 @@ impl std::ops::Mul for Matrix { } } -impl std::ops::Mul for Matrix { +impl std::ops::Mul for Matrix { + type Output = Tuple; + fn mul(self, rside: Tuple) -> Tuple { + &self * rside + } +} + +impl std::ops::Mul for &Matrix { type Output = Point; fn mul(self, rside: Point) -> Point { Point::from(self * *rside) } } +impl std::ops::Mul for Matrix { + type Output = Point; + fn mul(self, rside: Point) -> Point { + &self * rside + } +} + impl std::ops::Mul for Matrix { type Output = Vector; fn mul(self, rside: Vector) -> Vector { @@ -325,7 +360,7 @@ mod tests { [4., 8., 16., 32.], ]); - assert_eq!(a.clone() * Matrix::identity(), a); + assert_eq!(&a * Matrix::identity(), a); } #[test] @@ -488,7 +523,7 @@ mod tests { [7., 0., 5., 4.], [6., -2., 0., 5.], ]); - let c = a.clone() * b.clone(); + let c = &a * &b; assert_eq!(c * b.inverse(), a); } } diff --git a/ray-tracer/src/types/ray.rs b/ray-tracer/src/types/ray.rs index 6b904b8..43f6515 100644 --- a/ray-tracer/src/types/ray.rs +++ b/ray-tracer/src/types/ray.rs @@ -70,7 +70,7 @@ impl Ray { pub fn transform(&self, m: Matrix) -> Self { Self { - origin: m.clone() * self.origin, + origin: &m * self.origin, direction: m * self.direction, } } @@ -145,7 +145,7 @@ mod tests { let s = Sphere::new(); let i1 = Intersection { t: 1., object: &s }; let i2 = Intersection { t: 2., object: &s }; - let xs = Intersections::from(vec![i1.clone(), i2]); + let xs = Intersections::from(vec![i1.clone(), i2.clone()]); assert_eq!(xs.hit(), Some(&i1)); } diff --git a/ray-tracer/src/types/tuple.rs b/ray-tracer/src/types/tuple.rs index 137201d..0a45320 100644 --- a/ray-tracer/src/types/tuple.rs +++ b/ray-tracer/src/types/tuple.rs @@ -27,28 +27,77 @@ impl PartialEq for Tuple { } } -impl std::ops::Add for Tuple { +impl std::ops::Add<&Tuple> for &Tuple { type Output = Tuple; - fn add(self, r: Tuple) -> Self::Output { + fn add(self, r: &Tuple) -> Self::Output { Tuple(self.0 + r.0, self.1 + r.1, self.2 + r.2, self.3 + r.3) } } -impl std::ops::Sub for Tuple { +impl std::ops::Add for &Tuple { type Output = Tuple; - fn sub(self, r: Tuple) -> Self::Output { + fn add(self, r: Tuple) -> Self::Output { + self + &r + } +} + +impl std::ops::Add<&Tuple> for Tuple { + type Output = Tuple; + fn add(self, r: &Tuple) -> Self::Output { + &self + r + } +} + +impl std::ops::Add for Tuple { + type Output = Tuple; + fn add(self, r: Tuple) -> Self::Output { + &self + &r + } +} + +impl std::ops::Sub<&Tuple> for &Tuple { + type Output = Tuple; + fn sub(self, r: &Tuple) -> Self::Output { Tuple(self.0 - r.0, self.1 - r.1, self.2 - r.2, self.3 - r.3) } } -impl std::ops::Neg for Tuple { +impl std::ops::Sub for &Tuple { + type Output = Tuple; + fn sub(self, r: Tuple) -> Self::Output { + self - &r + } +} + +impl std::ops::Sub<&Tuple> for Tuple { + type Output = Tuple; + fn sub(self, r: &Tuple) -> Self::Output { + &self - r + } +} + +impl std::ops::Sub for Tuple { + type Output = Tuple; + fn sub(self, r: Tuple) -> Self::Output { + &self - &r + } +} + +impl std::ops::Neg for &Tuple { type Output = Tuple; fn neg(self) -> Self::Output { Tuple(-self.0, -self.1, -self.2, -self.3) } } -impl std::ops::Mul for Tuple { +impl std::ops::Neg for Tuple { + type Output = Tuple; + fn neg(self) -> Self::Output { + -&self + } +} + +impl std::ops::Mul for &Tuple { type Output = Tuple; fn mul(self, scalar: f64) -> Self::Output { Tuple( @@ -60,7 +109,14 @@ impl std::ops::Mul for Tuple { } } -impl std::ops::Div for Tuple { +impl std::ops::Mul for Tuple { + type Output = Tuple; + fn mul(self, scalar: f64) -> Self::Output { + &self * scalar + } +} + +impl std::ops::Div for &Tuple { type Output = Tuple; fn div(self, scalar: f64) -> Self::Output { Tuple( @@ -71,3 +127,11 @@ impl std::ops::Div for Tuple { ) } } + +impl std::ops::Div for Tuple { + type Output = Tuple; + fn div(self, scalar: f64) -> Self::Output { + &self / scalar + } +} +