Add addtional ops declarations to reduce cloning
This commit is contained in:
parent
af7d8680a0
commit
40bfe6d74f
|
@ -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.));
|
||||
}
|
||||
|
||||
|
|
|
@ -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<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;
|
||||
fn mul(self, rside: Tuple) -> Tuple {
|
||||
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;
|
||||
fn mul(self, rside: Point) -> Point {
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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<Tuple> 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<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)
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
fn neg(self) -> Self::Output {
|
||||
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;
|
||||
fn mul(self, scalar: f64) -> Self::Output {
|
||||
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;
|
||||
fn div(self, scalar: f64) -> Self::Output {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue