Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend and clean up AbsDiffEq impls in fj-math #912

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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