Inverse tests, and Mul is references
This commit is contained in:
@@ -174,10 +174,10 @@ impl Matrix {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<Matrix> for Matrix {
|
||||
impl std::ops::Mul<&Matrix> for &Matrix {
|
||||
type Output = Matrix;
|
||||
|
||||
fn mul(self, _rhs: Matrix) -> Matrix {
|
||||
fn mul(self, _rhs: &Matrix) -> Matrix {
|
||||
let mut result: Vec<Vec<f32>> = Vec::with_capacity(self.matrix.len());
|
||||
for row in 0..self.matrix.len() {
|
||||
let width = self.matrix[row].len();
|
||||
@@ -192,10 +192,10 @@ impl std::ops::Mul<Matrix> for Matrix {
|
||||
|
||||
}
|
||||
|
||||
impl std::ops::Mul<Tuple> for Matrix {
|
||||
impl std::ops::Mul<&Tuple> for &Matrix {
|
||||
type Output = Tuple;
|
||||
|
||||
fn mul(self, _rhs: Tuple) -> Tuple {
|
||||
fn mul(self, _rhs: &Tuple) -> Tuple {
|
||||
Tuple::new(
|
||||
self.calc_val_for_mul_tuple(0, &_rhs),
|
||||
self.calc_val_for_mul_tuple(1, &_rhs),
|
||||
@@ -341,7 +341,7 @@ mod tests {
|
||||
[16.0, 26.0, 46.0, 42.0],
|
||||
]);
|
||||
|
||||
assert_eq!(matrix_a * matrix_b, expected);
|
||||
assert_eq!(&matrix_a * &matrix_b, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -356,7 +356,7 @@ mod tests {
|
||||
let tuple = Tuple::new(1.0, 2.0, 3.0, 1.0);
|
||||
let expected = Tuple::new(18.0, 24.0, 33.0, 1.0);
|
||||
|
||||
assert_eq!(matrix * tuple, expected);
|
||||
assert_eq!(&matrix * &tuple, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -375,7 +375,7 @@ mod tests {
|
||||
[4.0, 8.0, 16.0, 32.0,]
|
||||
]);
|
||||
|
||||
assert_eq!(matrix * Matrix::identity(4), expected);
|
||||
assert_eq!(&matrix * &Matrix::identity(4), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -383,7 +383,7 @@ mod tests {
|
||||
let t = Tuple::new(1.0, 2.0, 3.0, 4.0);
|
||||
let expected = Tuple::new(1.0, 2.0, 3.0, 4.0);
|
||||
|
||||
assert_eq!(Matrix::identity(4) * t, expected);
|
||||
assert_eq!(&Matrix::identity(4) * &t, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -580,4 +580,68 @@ mod tests {
|
||||
|
||||
assert_matrix_eq(&expected, &b, 0.0001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inverse_2() {
|
||||
let m = Matrix::from_array([
|
||||
[8.0, -5.0, 9.0, 2.0],
|
||||
[7.0, 5.0, 6.0, 1.0],
|
||||
[-6.0, 0.0, 9.0, 6.0],
|
||||
[-3.0, 0.0, -9.0, -4.0],
|
||||
]).inverse();
|
||||
|
||||
let expected = Matrix::from_array([
|
||||
[-0.15385, -0.15385, -0.28205, -0.53846],
|
||||
[-0.07692, 0.12308, 0.02564, 0.03077],
|
||||
[0.35897, 0.35897, 0.43590, 0.92308],
|
||||
[-0.69321, -0.69321, -0.76923, -1.92308],
|
||||
]);
|
||||
|
||||
assert_matrix_eq(&expected, &m, 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inverse_3() {
|
||||
let m = Matrix::from_array([
|
||||
[9.0, 3.0, 0.0, 9.0],
|
||||
[-5.0, -2.0, -6.0, -3.0],
|
||||
[-4.0, 9.0, 6.0, 4.0],
|
||||
[-7.0, 6.0, 6.0, 2.0],
|
||||
]).inverse();
|
||||
|
||||
let expected = Matrix::from_array([
|
||||
[-0.04074, -0.07778, 0.14444, -0.22222],
|
||||
[-0.07778, 0.03333, 0.36667, -0.33333],
|
||||
[-0.02901, -0.14630, -0.10926, 0.12963],
|
||||
[0.17778, 0.06667, -0.26667, 0.33333],
|
||||
]);
|
||||
|
||||
assert_matrix_eq(&expected, &m, 0.01);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiply_by_inverse() {
|
||||
let a = Matrix::from_array([
|
||||
[3.0, -9.0, 7.0, 3.0],
|
||||
[3.0, -8.0, 2.0, -9.0],
|
||||
[-4.0, 4.0, 4.0, 1.0],
|
||||
[-6.0, 5.0, -1.0, 1.0],
|
||||
]);
|
||||
let b = Matrix::from_array([
|
||||
[8.0, 2.0, 2.0, 2.0],
|
||||
[3.0, -1.0, 7.0, 0.0],
|
||||
[7.0, 0.0, 5.0, 4.0],
|
||||
[6.0, -2.0, 0.0, 5.0],
|
||||
]);
|
||||
|
||||
let c = &a * &b;
|
||||
let r = &c * &b.inverse();
|
||||
let expected = Matrix::from_array([
|
||||
[3.0, -9.0, 7.0, 3.0],
|
||||
[3.0, -8.0, 2.0, -9.0],
|
||||
[-4.0, 4.0, 4.0, 1.0],
|
||||
[-6.0, 5.0, -1.0, 1.0],
|
||||
]);
|
||||
assert_matrix_eq(&r, &expected, 0.00001);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user