Skip to content

Commit

Permalink
Merge pull request #912 from hannobraun/math
Browse files Browse the repository at this point in the history
Extend and clean up `AbsDiffEq` impls in `fj-math`
  • Loading branch information
hannobraun authored Aug 3, 2022
2 parents c901659 + 2923d23 commit b68b6c1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
14 changes: 14 additions & 0 deletions crates/fj-math/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ impl<const D: usize> Circle<D> {
}
}

impl<const D: usize> approx::AbsDiffEq for Circle<D> {
type Epsilon = <Scalar as approx::AbsDiffEq>::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};
Expand Down
12 changes: 8 additions & 4 deletions crates/fj-math/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ impl<const D: usize> Line<D> {
}

impl<const D: usize> approx::AbsDiffEq for Line<D> {
type Epsilon = <f64 as approx::AbsDiffEq>::Epsilon;
type Epsilon = <Scalar 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 {
Expand All @@ -152,7 +152,7 @@ impl<const D: usize> approx::AbsDiffEq for Line<D> {
mod tests {
use approx::assert_abs_diff_eq;

use crate::{Point, Vector};
use crate::{Point, Scalar, Vector};

use super::Line;

Expand Down Expand Up @@ -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)
);
}
}
}
2 changes: 1 addition & 1 deletion crates/fj-math/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<const D: usize> approx::AbsDiffEq for Point<D> {
type Epsilon = <Vector<D> 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 {
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-math/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,13 @@ impl fmt::Display for Scalar {
}

impl approx::AbsDiffEq for Scalar {
type Epsilon = <f64 as approx::AbsDiffEq>::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)
}
}
2 changes: 1 addition & 1 deletion crates/fj-math/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
Point::from([1., 3., 3.]),
Vector::from([-1., 0., 0.]),
),
epsilon = 1e-8,
epsilon = Scalar::from(1e-8),
);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<const D: usize> approx::AbsDiffEq for Vector<D> {
type Epsilon = <Scalar 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 {
Expand Down

0 comments on commit b68b6c1

Please sign in to comment.