From f1fd0f2098df740c948f7b87b4f3305a68c1dae9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 13 May 2022 13:00:03 +0200 Subject: [PATCH 1/2] Replace `curves::Circle` with `fj_math::Circle` --- .../fj-kernel/src/algorithms/approx/curves.rs | 6 +- .../fj-kernel/src/geometry/curves/circle.rs | 125 ------------------ crates/fj-kernel/src/geometry/curves/mod.rs | 14 +- crates/fj-kernel/src/geometry/mod.rs | 2 +- crates/fj-kernel/src/topology/builder.rs | 4 +- 5 files changed, 12 insertions(+), 139 deletions(-) delete mode 100644 crates/fj-kernel/src/geometry/curves/circle.rs diff --git a/crates/fj-kernel/src/algorithms/approx/curves.rs b/crates/fj-kernel/src/algorithms/approx/curves.rs index fe6eb7486..fc98d2346 100644 --- a/crates/fj-kernel/src/algorithms/approx/curves.rs +++ b/crates/fj-kernel/src/algorithms/approx/curves.rs @@ -1,8 +1,8 @@ use std::cmp::max; -use fj_math::Scalar; +use fj_math::{Circle, Scalar}; -use crate::geometry::{self, Circle, Curve}; +use crate::geometry::{self, Curve}; use super::Tolerance; @@ -36,7 +36,7 @@ pub fn approx_curve( /// `tolerance` specifies how much the approximation is allowed to deviate /// from the circle. pub fn approx_circle( - circle: &Circle, + circle: &Circle<3>, tolerance: Tolerance, out: &mut Vec>, ) { diff --git a/crates/fj-kernel/src/geometry/curves/circle.rs b/crates/fj-kernel/src/geometry/curves/circle.rs deleted file mode 100644 index a82609e94..000000000 --- a/crates/fj-kernel/src/geometry/curves/circle.rs +++ /dev/null @@ -1,125 +0,0 @@ -use fj_math::{Point, Scalar, Transform, Vector}; - -/// A circle -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct Circle { - /// The center point of the circle - pub center: Point<3>, - - /// A vector from the center to the starting point of the circle - /// - /// The length of this vector defines the circle radius. Please also refer - /// to the documentation of `b`. - pub a: Vector<3>, - - /// A second vector that defines the plane 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<3>, -} - -impl Circle { - /// Access the origin of the curve's coordinate system - pub fn origin(&self) -> Point<3> { - self.center - } - - /// Create a new instance that is reversed - #[must_use] - pub fn reverse(mut self) -> Self { - self.b = -self.b; - self - } - - /// Create a new instance that is transformed by `transform` - #[must_use] - pub fn transform(self, transform: &Transform) -> Self { - Self { - center: transform.transform_point(&self.center), - a: transform.transform_vector(&self.a), - b: transform.transform_vector(&self.b), - } - } - - /// Convert a point in model coordinates to curve coordinates - /// - /// Converts the provided point into curve coordinates between `0.` - /// (inclusive) and `PI * 2.` (exclusive). - /// - /// Projects the point onto the circle before computing curve coordinate, - /// ignoring the radius. This is done to make this method robust against - /// floating point accuracy issues. - /// - /// Callers are advised to be careful about the points they pass, as the - /// point not being on the curve, intentional or not, will not result in an - /// error. - pub fn point_to_circle_coords( - &self, - point: impl Into>, - ) -> Point<1> { - let v = point.into() - self.center; - let atan = Scalar::atan2(v.y, v.x); - let coord = if atan >= Scalar::ZERO { - atan - } else { - atan + Scalar::PI * 2. - }; - Point::from([coord]) - } - - /// Convert a point on the curve into model coordinates - pub fn point_from_circle_coords( - &self, - point: impl Into>, - ) -> Point<3> { - self.center + self.vector_from_circle_coords(point.into().coords) - } - - /// Convert a vector on the curve into model coordinates - pub fn vector_from_circle_coords( - &self, - vector: impl Into>, - ) -> Vector<3> { - let angle = vector.into().t; - let (sin, cos) = angle.sin_cos(); - - self.a * cos + self.b * sin - } -} - -#[cfg(test)] -mod tests { - use std::f64::consts::{FRAC_PI_2, PI}; - - use fj_math::{Point, Vector}; - - use super::Circle; - - #[test] - fn point_model_to_curve() { - let circle = Circle { - center: Point::from([1., 2., 3.]), - a: Vector::from([1., 0., 0.]), - b: Vector::from([0., 1., 0.]), - }; - - assert_eq!( - circle.point_to_circle_coords([2., 2., 3.]), - Point::from([0.]), - ); - assert_eq!( - circle.point_to_circle_coords([1., 3., 3.]), - Point::from([FRAC_PI_2]), - ); - assert_eq!( - circle.point_to_circle_coords([0., 2., 3.]), - Point::from([PI]), - ); - assert_eq!( - circle.point_to_circle_coords([1., 1., 3.]), - Point::from([FRAC_PI_2 * 3.]), - ); - } -} diff --git a/crates/fj-kernel/src/geometry/curves/mod.rs b/crates/fj-kernel/src/geometry/curves/mod.rs index ec2925092..a01c2c732 100644 --- a/crates/fj-kernel/src/geometry/curves/mod.rs +++ b/crates/fj-kernel/src/geometry/curves/mod.rs @@ -1,8 +1,4 @@ -mod circle; - -pub use self::circle::Circle; - -use fj_math::{Line, Point, Transform, Vector}; +use fj_math::{Circle, Line, Point, Transform, Vector}; use crate::geometry; @@ -18,7 +14,7 @@ use crate::geometry; #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub enum Curve { /// A circle - Circle(Circle), + Circle(Circle<3>), /// A line Line(Line<3>), @@ -52,7 +48,7 @@ impl Curve { /// Access the origin of the curve's coordinate system pub fn origin(&self) -> Point<3> { match self { - Self::Circle(curve) => curve.origin(), + Self::Circle(curve) => curve.center, Self::Line(curve) => curve.origin, } } @@ -70,7 +66,9 @@ impl Curve { #[must_use] pub fn transform(self, transform: &Transform) -> Self { match self { - Self::Circle(curve) => Self::Circle(curve.transform(transform)), + Self::Circle(curve) => { + Self::Circle(transform.transform_circle(&curve)) + } Self::Line(curve) => Self::Line(transform.transform_line(&curve)), } } diff --git a/crates/fj-kernel/src/geometry/mod.rs b/crates/fj-kernel/src/geometry/mod.rs index 1455f48bd..c965245d1 100644 --- a/crates/fj-kernel/src/geometry/mod.rs +++ b/crates/fj-kernel/src/geometry/mod.rs @@ -10,7 +10,7 @@ mod points; mod surfaces; pub use self::{ - curves::{Circle, Curve}, + curves::Curve, points::Point, surfaces::{Surface, SweptCurve}, }; diff --git a/crates/fj-kernel/src/topology/builder.rs b/crates/fj-kernel/src/topology/builder.rs index bb763a61f..982f76b6c 100644 --- a/crates/fj-kernel/src/topology/builder.rs +++ b/crates/fj-kernel/src/topology/builder.rs @@ -1,7 +1,7 @@ -use fj_math::{Line, Point, Scalar, Vector}; +use fj_math::{Circle, Line, Point, Scalar, Vector}; use crate::{ - geometry::{Circle, Curve, Surface}, + geometry::{Curve, Surface}, shape::{Handle, Shape, ValidationResult}, }; From 7fc2be5a7b9ae9209bf693133f59147590e4cfc7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 13 May 2022 13:02:12 +0200 Subject: [PATCH 2/2] Simplify directory structure --- crates/fj-kernel/src/geometry/{curves/mod.rs => curves.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename crates/fj-kernel/src/geometry/{curves/mod.rs => curves.rs} (100%) diff --git a/crates/fj-kernel/src/geometry/curves/mod.rs b/crates/fj-kernel/src/geometry/curves.rs similarity index 100% rename from crates/fj-kernel/src/geometry/curves/mod.rs rename to crates/fj-kernel/src/geometry/curves.rs