Add addtional ops declarations to reduce cloning

This commit is contained in:
Savanni D'Gerinel 2024-06-23 15:07:41 -04:00
parent af7d8680a0
commit 40bfe6d74f
4 changed files with 117 additions and 18 deletions

View File

@ -71,7 +71,7 @@ mod tests {
fn multiply_by_translation_matrix() { fn multiply_by_translation_matrix() {
let tr = translation(5., -3., 2.); let tr = translation(5., -3., 2.);
let p = Point::new(-3., 4., 5.); 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(); let inv = tr.inverse();
assert_eq!(inv * p, Point::new(-8., 7., 3.)); assert_eq!(inv * p, Point::new(-8., 7., 3.));
@ -81,7 +81,7 @@ mod tests {
fn translation_does_not_affect_vectors() { fn translation_does_not_affect_vectors() {
let tr = translation(5., -3., 2.); let tr = translation(5., -3., 2.);
let v = Vector::new(-3., 4., 5.); let v = Vector::new(-3., 4., 5.);
assert_eq!(tr.clone() * v, v); assert_eq!(tr * v, v);
} }
#[test] #[test]
@ -90,7 +90,7 @@ mod tests {
let p = Point::new(-4., 6., 8.); let p = Point::new(-4., 6., 8.);
let v = Vector::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.)); assert_eq!(tr * v, Vector::new(-8., 18., 32.));
} }

View File

@ -169,9 +169,9 @@ impl PartialEq for Matrix {
} }
} }
impl std::ops::Mul for Matrix { impl std::ops::Mul<&Matrix> for &Matrix {
type Output = Matrix; type Output = Matrix;
fn mul(self, rside: Matrix) -> Matrix { fn mul(self, rside: &Matrix) -> Matrix {
assert_eq!(self.size, 4); assert_eq!(self.size, 4);
assert_eq!(rside.size, 4); assert_eq!(rside.size, 4);
@ -189,7 +189,28 @@ impl std::ops::Mul for Matrix {
} }
} }
impl std::ops::Mul<Tuple> for Matrix { impl std::ops::Mul<Matrix> 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<Tuple> for &Matrix {
type Output = Tuple; type Output = Tuple;
fn mul(self, rside: Tuple) -> Tuple { fn mul(self, rside: Tuple) -> Tuple {
assert_eq!(self.size, 4); assert_eq!(self.size, 4);
@ -207,13 +228,27 @@ impl std::ops::Mul<Tuple> for Matrix {
} }
} }
impl std::ops::Mul<Point> for Matrix { impl std::ops::Mul<Tuple> for Matrix {
type Output = Tuple;
fn mul(self, rside: Tuple) -> Tuple {
&self * rside
}
}
impl std::ops::Mul<Point> for &Matrix {
type Output = Point; type Output = Point;
fn mul(self, rside: Point) -> Point { fn mul(self, rside: Point) -> Point {
Point::from(self * *rside) Point::from(self * *rside)
} }
} }
impl std::ops::Mul<Point> for Matrix {
type Output = Point;
fn mul(self, rside: Point) -> Point {
&self * rside
}
}
impl std::ops::Mul<Vector> for Matrix { impl std::ops::Mul<Vector> for Matrix {
type Output = Vector; type Output = Vector;
fn mul(self, rside: Vector) -> Vector { fn mul(self, rside: Vector) -> Vector {
@ -325,7 +360,7 @@ mod tests {
[4., 8., 16., 32.], [4., 8., 16., 32.],
]); ]);
assert_eq!(a.clone() * Matrix::identity(), a); assert_eq!(&a * Matrix::identity(), a);
} }
#[test] #[test]
@ -488,7 +523,7 @@ mod tests {
[7., 0., 5., 4.], [7., 0., 5., 4.],
[6., -2., 0., 5.], [6., -2., 0., 5.],
]); ]);
let c = a.clone() * b.clone(); let c = &a * &b;
assert_eq!(c * b.inverse(), a); assert_eq!(c * b.inverse(), a);
} }
} }

View File

@ -70,7 +70,7 @@ impl Ray {
pub fn transform(&self, m: Matrix) -> Self { pub fn transform(&self, m: Matrix) -> Self {
Self { Self {
origin: m.clone() * self.origin, origin: &m * self.origin,
direction: m * self.direction, direction: m * self.direction,
} }
} }
@ -145,7 +145,7 @@ mod tests {
let s = Sphere::new(); let s = Sphere::new();
let i1 = Intersection { t: 1., object: &s }; let i1 = Intersection { t: 1., object: &s };
let i2 = Intersection { t: 2., 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)); assert_eq!(xs.hit(), Some(&i1));
} }

View File

@ -27,28 +27,77 @@ impl PartialEq for Tuple {
} }
} }
impl std::ops::Add for Tuple { impl std::ops::Add<&Tuple> for &Tuple {
type Output = 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) 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<Tuple> for &Tuple {
type Output = 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<Tuple> 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) 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<Tuple> 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<Tuple> for Tuple {
type Output = Tuple;
fn sub(self, r: Tuple) -> Self::Output {
&self - &r
}
}
impl std::ops::Neg for &Tuple {
type Output = Tuple; type Output = Tuple;
fn neg(self) -> Self::Output { fn neg(self) -> Self::Output {
Tuple(-self.0, -self.1, -self.2, -self.3) Tuple(-self.0, -self.1, -self.2, -self.3)
} }
} }
impl std::ops::Mul<f64> for Tuple { impl std::ops::Neg for Tuple {
type Output = Tuple;
fn neg(self) -> Self::Output {
-&self
}
}
impl std::ops::Mul<f64> for &Tuple {
type Output = Tuple; type Output = Tuple;
fn mul(self, scalar: f64) -> Self::Output { fn mul(self, scalar: f64) -> Self::Output {
Tuple( Tuple(
@ -60,7 +109,14 @@ impl std::ops::Mul<f64> for Tuple {
} }
} }
impl std::ops::Div<f64> for Tuple { impl std::ops::Mul<f64> for Tuple {
type Output = Tuple;
fn mul(self, scalar: f64) -> Self::Output {
&self * scalar
}
}
impl std::ops::Div<f64> for &Tuple {
type Output = Tuple; type Output = Tuple;
fn div(self, scalar: f64) -> Self::Output { fn div(self, scalar: f64) -> Self::Output {
Tuple( Tuple(
@ -71,3 +127,11 @@ impl std::ops::Div<f64> for Tuple {
) )
} }
} }
impl std::ops::Div<f64> for Tuple {
type Output = Tuple;
fn div(self, scalar: f64) -> Self::Output {
&self / scalar
}
}