From 78d5a4804207cadf937f3144c17505fc19d13d75 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 3 Aug 2022 20:34:03 +0200 Subject: [PATCH 1/2] Use `Scalar` as epsilon type for `AbsDiffEq` impls --- crates/fj-math/src/line.rs | 12 ++++++++---- crates/fj-math/src/point.rs | 2 +- crates/fj-math/src/scalar.rs | 6 +++--- crates/fj-math/src/transform.rs | 2 +- crates/fj-math/src/vector.rs | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/fj-math/src/line.rs b/crates/fj-math/src/line.rs index 303989541..4bfed5413 100644 --- a/crates/fj-math/src/line.rs +++ b/crates/fj-math/src/line.rs @@ -136,10 +136,10 @@ impl Line { } impl approx::AbsDiffEq for Line { - type Epsilon = ::Epsilon; + type Epsilon = ::Epsilon; fn default_epsilon() -> Self::Epsilon { - f64::default_epsilon() + Scalar::default_epsilon() } fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { @@ -152,7 +152,7 @@ impl approx::AbsDiffEq for Line { mod tests { use approx::assert_abs_diff_eq; - use crate::{Point, Vector}; + use crate::{Point, Scalar, Vector}; use super::Line; @@ -185,7 +185,11 @@ mod tests { let point = line.point_from_line_coords([t]); let t_result = line.point_to_line_coords(point); - assert_abs_diff_eq!(Point::from([t]), t_result, epsilon = 1e-8); + assert_abs_diff_eq!( + Point::from([t]), + t_result, + epsilon = Scalar::from(1e-8) + ); } } } diff --git a/crates/fj-math/src/point.rs b/crates/fj-math/src/point.rs index 8502e8963..a8ad46785 100644 --- a/crates/fj-math/src/point.rs +++ b/crates/fj-math/src/point.rs @@ -209,7 +209,7 @@ impl approx::AbsDiffEq for Point { type Epsilon = as approx::AbsDiffEq>::Epsilon; fn default_epsilon() -> Self::Epsilon { - f64::default_epsilon() + Scalar::default_epsilon() } fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index 2036bfce0..96a258638 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -519,13 +519,13 @@ impl fmt::Display for Scalar { } impl approx::AbsDiffEq for Scalar { - type Epsilon = ::Epsilon; + type Epsilon = Self; fn default_epsilon() -> Self::Epsilon { - f64::default_epsilon() + f64::default_epsilon().into() } fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { - self.0.abs_diff_eq(&other.0, epsilon) + self.0.abs_diff_eq(&other.0, epsilon.0) } } diff --git a/crates/fj-math/src/transform.rs b/crates/fj-math/src/transform.rs index da95ea7f9..279e29ee6 100644 --- a/crates/fj-math/src/transform.rs +++ b/crates/fj-math/src/transform.rs @@ -178,7 +178,7 @@ mod tests { Point::from([1., 3., 3.]), Vector::from([-1., 0., 0.]), ), - epsilon = 1e-8, + epsilon = Scalar::from(1e-8), ); } diff --git a/crates/fj-math/src/vector.rs b/crates/fj-math/src/vector.rs index 2b99f4055..3a3c88f25 100644 --- a/crates/fj-math/src/vector.rs +++ b/crates/fj-math/src/vector.rs @@ -326,7 +326,7 @@ impl approx::AbsDiffEq for Vector { type Epsilon = ::Epsilon; fn default_epsilon() -> Self::Epsilon { - f64::default_epsilon() + Scalar::default_epsilon() } fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { From 2923d235fd584c2841bf71e9e2bcd6dea6f881bc Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 3 Aug 2022 20:36:03 +0200 Subject: [PATCH 2/2] Implement `AbsDiffEq` for `Circle` --- crates/fj-math/src/circle.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/fj-math/src/circle.rs b/crates/fj-math/src/circle.rs index 29212cf90..7ae63c8ba 100644 --- a/crates/fj-math/src/circle.rs +++ b/crates/fj-math/src/circle.rs @@ -77,6 +77,20 @@ impl Circle { } } +impl approx::AbsDiffEq for Circle { + type Epsilon = ::Epsilon; + + fn default_epsilon() -> Self::Epsilon { + Scalar::default_epsilon() + } + + fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { + self.center.abs_diff_eq(&other.center, epsilon) + && self.a.abs_diff_eq(&other.a, epsilon) + && self.b.abs_diff_eq(&other.b, epsilon) + } +} + #[cfg(test)] mod tests { use std::f64::consts::{FRAC_PI_2, PI};