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

Validate Circle on construction #913

Merged
merged 6 commits into from
Aug 4, 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
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/approx/curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn approx_circle(
tolerance: Tolerance,
out: &mut Vec<Local<Point<1>>>,
) {
let radius = circle.a.magnitude();
let radius = circle.a().magnitude();

// To approximate the circle, we use a regular polygon for which
// the circle is the circumscribed circle. The `tolerance`
Expand Down
13 changes: 8 additions & 5 deletions crates/fj-kernel/src/algorithms/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ fn reverse_local_coordinates_in_cycle<'r>(
let edges = cycle.edges().map(|edge| {
let curve = {
let local = match edge.curve().kind() {
CurveKind::Circle(Circle { center, a, b }) => {
let center = Point::from([center.u, -center.v]);
CurveKind::Circle(circle) => {
let center = Point::from([
circle.center().u,
-circle.center().v,
]);

let a = Vector::from([a.u, -a.v]);
let b = Vector::from([b.u, -b.v]);
let a = Vector::from([circle.a().u, -circle.a().v]);
let b = Vector::from([circle.b().u, -circle.b().v]);

CurveKind::Circle(Circle { center, a, b })
CurveKind::Circle(Circle::new(center, a, b))
}
CurveKind::Line(line) => {
let origin =
Expand Down
21 changes: 11 additions & 10 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ pub struct EdgeBuilder;
impl EdgeBuilder {
/// Create a circle from the given radius
pub fn circle_from_radius(&self, radius: Scalar) -> Edge {
let curve_local = CurveKind::Circle(Circle {
center: Point::origin(),
a: Vector::from([radius, Scalar::ZERO]),
b: Vector::from([Scalar::ZERO, radius]),
});
let curve_global = GlobalCurve::from_kind(CurveKind::Circle(Circle {
center: Point::origin(),
a: Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
b: Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
}));
let curve_local = CurveKind::Circle(Circle::new(
Point::origin(),
Vector::from([radius, Scalar::ZERO]),
Vector::from([Scalar::ZERO, radius]),
));
let curve_global =
GlobalCurve::from_kind(CurveKind::Circle(Circle::new(
Point::origin(),
Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
)));

Edge::new(
Curve::new(curve_local, curve_global),
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/objects/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<const D: usize> CurveKind<D> {
/// Access the origin of the curve's coordinate system
pub fn origin(&self) -> Point<D> {
match self {
Self::Circle(curve) => curve.center,
Self::Circle(curve) => curve.center(),
Self::Line(curve) => curve.origin(),
}
}
Expand Down
81 changes: 68 additions & 13 deletions crates/fj-math/src/circle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use approx::AbsDiffEq;

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

/// An n-dimensional circle
Expand All @@ -6,24 +8,77 @@ use crate::{Point, Scalar, Vector};
/// parameter.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Circle<const D: usize> {
/// The center point of the circle
pub center: Point<D>,
center: Point<D>,
a: Vector<D>,
b: Vector<D>,
}

/// A vector from the center to the starting point of the circle
impl<const D: usize> Circle<D> {
/// Construct a circle
///
/// The length of this vector defines the circle radius. Please also refer
/// to the documentation of `b`.
pub a: Vector<D>,
/// # Panics
///
/// Panics, if any of the following requirements are not met:
///
/// - The circle radius (defined by the length of `a` and `b`) must not be
/// zero.
/// - `a` and `b` must be of equal length.
/// - `a` and `b` must be perpendicular to each other.
pub fn new(
center: impl Into<Point<D>>,
a: impl Into<Vector<D>>,
b: impl Into<Vector<D>>,
) -> Self {
let center = center.into();
let a = a.into();
let b = b.into();

/// A second vector that defines the plane of the circle
assert_eq!(
a.magnitude(),
b.magnitude(),
"`a` and `b` must be of equal length"
);
assert_ne!(
a.magnitude(),
Scalar::ZERO,
"circle radius must not be zero"
);
// Requiring the vector to be *precisely* perpendicular is not
// practical, because of numerical inaccuracy. This epsilon value seems
// seems to work for now, but maybe it needs to become configurable.
assert!(
a.dot(&b) < Scalar::default_epsilon(),
"`a` and `b` must be perpendicular to each other"
);

Self { center, a, b }
}

/// Access the center point of the circle
pub fn center(&self) -> Point<D> {
self.center
}

/// Access the vector that defines the starting point of the circle
///
/// The vector must be of equal length to `a` (the circle radius) and must
/// be perpendicular to it. Code working with circles might assume that
/// these conditions are met.
pub b: Vector<D>,
}
/// The point where this vector points from the circle center, is the zero
/// coordinate of the circle's coordinate system. The length of the vector
/// defines the circle's radius.
///
/// Please also refer to [`Self::b`].
pub fn a(&self) -> Vector<D> {
self.a
}

/// Access the vector that defines the plane of the circle
///
/// Also defines the direction of the circle's coordinate system. The length
/// is equal to the circle's radius, and this vector is perpendicular to
/// [`Self::a`].
pub fn b(&self) -> Vector<D> {
self.b
}

impl<const D: usize> Circle<D> {
/// Create a new instance that is reversed
#[must_use]
pub fn reverse(mut self) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions crates/fj-math/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ impl Transform {

/// Transform the given circle
pub fn transform_circle(&self, circle: &Circle<3>) -> Circle<3> {
Circle {
center: self.transform_point(&circle.center),
a: self.transform_vector(&circle.a),
b: self.transform_vector(&circle.b),
}
Circle::new(
self.transform_point(&circle.center()),
self.transform_vector(&circle.a()),
self.transform_vector(&circle.b()),
)
}

/// Inverse transform
Expand Down