From 1a414bc485fcef11e73bae721b313c9439560bce Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Sat, 11 Sep 2021 19:59:59 -0600 Subject: [PATCH] approx with numbers near 0 needs higher max relative --- Cargo.lock | 4 ++-- features/Cargo.lock | 4 ++-- features/Cargo.toml | 2 +- features/src/transformations.rs | 34 +++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b3e7e63..f4cefa4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "approx" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" +checksum = "072df7202e63b127ab55acfe16ce97013d5b97bf160489336d3f1840fd78e99e" dependencies = [ "num-traits", ] diff --git a/features/Cargo.lock b/features/Cargo.lock index 86081f3..ad1adb1 100644 --- a/features/Cargo.lock +++ b/features/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "approx" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" +checksum = "072df7202e63b127ab55acfe16ce97013d5b97bf160489336d3f1840fd78e99e" dependencies = [ "num-traits", ] diff --git a/features/Cargo.toml b/features/Cargo.toml index 6e4f893..b3ff419 100644 --- a/features/Cargo.toml +++ b/features/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -approx = "0.4" +approx = "0.5" diff --git a/features/src/transformations.rs b/features/src/transformations.rs index e07446e..398e45a 100644 --- a/features/src/transformations.rs +++ b/features/src/transformations.rs @@ -206,4 +206,38 @@ mod tests { assert_eq!(&transform * &p, Tuple::point(2.0, 3.0, 7.0)); } + + #[test] + fn individual_transformations_are_applied_in_sequence() { + let p = Tuple::point(1.0, 0.0, 1.0); + let a = Matrix::rotation_x(PI / 2.0); + let b = Matrix::scaling(5.0, 5.0, 5.0); + let c = Matrix::translation(10.0, 5.0, 7.0); + + let p2 = &a * &p; + assert_eq!(p2, Tuple::point(1.0, -1.0, 0.0)); + + let p3 = &b * &p2; + // assert_eq!(p3, Tuple::point(5.0, -5.0, -0.00)); + assert_relative_eq!(p3.x(), 5.0); + assert_relative_eq!(p3.y(), -5.0); + //assert_relative_eq!(p3.z(), 0.0, 1.0); + // I don't think the approx crate can handle numbers close to 0 appropriately + assert_eq!(true, relative_eq!(p3.z(), 0.0, max_relative = 1.0)); + assert_relative_eq!(p3.w(), 1.0); + + let p4 = &c * &p3; + assert_eq!(p4, Tuple::point(15.0, 0.0, 7.0)); + } + + #[test] + fn chained_transformations_must_be_applied_in_reverse_order() { + let p = Tuple::point(1.0, 0.0, 1.0); + let a = Matrix::rotation_x(PI / 2.0); + let b = Matrix::scaling(5.0, 5.0, 5.0); + let c = Matrix::translation(10.0, 5.0, 7.0); + + let t = &(&c * &b) * &a; + assert_eq!(&t * &p, Tuple::point(15.0, 0.0, 7.0)); + } }