From 77e4ac37b701004ff7135d1d386d1791e7c182cd Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:04:33 +0200 Subject: [PATCH 01/11] Remove outdated implementation note `Sketch` has been updated to make this mistake impossible. --- crates/fj-core/src/objects/kinds/sketch.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/crates/fj-core/src/objects/kinds/sketch.rs b/crates/fj-core/src/objects/kinds/sketch.rs index 69003e279..5e59f0cea 100644 --- a/crates/fj-core/src/objects/kinds/sketch.rs +++ b/crates/fj-core/src/objects/kinds/sketch.rs @@ -8,11 +8,6 @@ use crate::{ }; /// A 2-dimensional shape -/// -/// # Implementation Note -/// -/// The faces that make up the sketch must be in the same surface. This is not -/// currently validated. #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct Sketch { regions: BTreeSet, From dbe404859429230267287775f17350180f0033bf Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:19:17 +0200 Subject: [PATCH 02/11] Add `BuildSketch::empty` --- crates/fj-core/src/operations/build/sketch.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-core/src/operations/build/sketch.rs b/crates/fj-core/src/operations/build/sketch.rs index 111f06cc4..33ffd0fdc 100644 --- a/crates/fj-core/src/operations/build/sketch.rs +++ b/crates/fj-core/src/operations/build/sketch.rs @@ -9,6 +9,11 @@ use crate::{ /// Build a [`Sketch`] pub trait BuildSketch { + /// Create a sketch with no regions + fn empty() -> Sketch { + Sketch::new([]) + } + /// Build a polygon fn polygon(points: Ps, services: &mut Services) -> Sketch where From 0934127919bf7bf7c3126b45a202263aea81f53d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:28:33 +0200 Subject: [PATCH 03/11] Add `UpdateSketch` --- crates/fj-core/src/operations/mod.rs | 2 +- crates/fj-core/src/operations/update/mod.rs | 1 + crates/fj-core/src/operations/update/sketch.rs | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 crates/fj-core/src/operations/update/sketch.rs diff --git a/crates/fj-core/src/operations/mod.rs b/crates/fj-core/src/operations/mod.rs index 2331fa5ef..5f7bbd90f 100644 --- a/crates/fj-core/src/operations/mod.rs +++ b/crates/fj-core/src/operations/mod.rs @@ -19,6 +19,6 @@ pub use self::{ join::cycle::JoinCycle, update::{ cycle::UpdateCycle, edge::UpdateHalfEdge, face::UpdateFace, - shell::UpdateShell, solid::UpdateSolid, + shell::UpdateShell, sketch::UpdateSketch, solid::UpdateSolid, }, }; diff --git a/crates/fj-core/src/operations/update/mod.rs b/crates/fj-core/src/operations/update/mod.rs index 85f65f886..8928937cb 100644 --- a/crates/fj-core/src/operations/update/mod.rs +++ b/crates/fj-core/src/operations/update/mod.rs @@ -2,4 +2,5 @@ pub mod cycle; pub mod edge; pub mod face; pub mod shell; +pub mod sketch; pub mod solid; diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs new file mode 100644 index 000000000..5e51a8f17 --- /dev/null +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -0,0 +1,6 @@ +use crate::objects::Sketch; + +/// Update a [`Sketch`] +pub trait UpdateSketch {} + +impl UpdateSketch for Sketch {} From cd993e6709c63b8d83f881bdd11683bd885759ce Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:29:20 +0200 Subject: [PATCH 04/11] Add `Sketch::regions` --- crates/fj-core/src/objects/kinds/sketch.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-core/src/objects/kinds/sketch.rs b/crates/fj-core/src/objects/kinds/sketch.rs index 5e59f0cea..3baebb2a0 100644 --- a/crates/fj-core/src/objects/kinds/sketch.rs +++ b/crates/fj-core/src/objects/kinds/sketch.rs @@ -21,6 +21,11 @@ impl Sketch { } } + /// Access the regions of the sketch + pub fn regions(&self) -> impl Iterator { + self.regions.iter() + } + /// Apply the regions of the sketch to some [`Surface`] pub fn faces( &self, From c9b767b0a455fe05b2ab4b39503dcbe42c6933ef Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:29:43 +0200 Subject: [PATCH 05/11] Replace `BuildSketch::polygon` Having `UpdateSketch::add_polygon` instead is more flexible, providing an API to add new regions to the sketch. --- crates/fj-core/src/operations/build/sketch.rs | 21 +----------- .../fj-core/src/operations/update/sketch.rs | 32 +++++++++++++++++-- models/cuboid/src/lib.rs | 23 ++++++------- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/crates/fj-core/src/operations/build/sketch.rs b/crates/fj-core/src/operations/build/sketch.rs index 33ffd0fdc..274438059 100644 --- a/crates/fj-core/src/operations/build/sketch.rs +++ b/crates/fj-core/src/operations/build/sketch.rs @@ -1,11 +1,4 @@ -use fj_math::Point; - -use crate::{ - geometry::region::Region, - objects::{Cycle, Sketch}, - operations::{BuildCycle, Insert}, - services::Services, -}; +use crate::objects::Sketch; /// Build a [`Sketch`] pub trait BuildSketch { @@ -13,18 +6,6 @@ pub trait BuildSketch { fn empty() -> Sketch { Sketch::new([]) } - - /// Build a polygon - fn polygon(points: Ps, services: &mut Services) -> Sketch - where - P: Into>, - Ps: IntoIterator, - Ps::IntoIter: Clone + ExactSizeIterator, - { - let exterior = Cycle::polygon(points, services).insert(services); - let region = Region::new(exterior, Vec::new(), None); - Sketch::new([region]) - } } impl BuildSketch for Sketch {} diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index 5e51a8f17..349c5402e 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -1,6 +1,32 @@ -use crate::objects::Sketch; +use fj_math::Point; + +use crate::{ + geometry::region::Region, + objects::{Cycle, Sketch}, + operations::{BuildCycle, Insert}, + services::Services, +}; /// Update a [`Sketch`] -pub trait UpdateSketch {} +pub trait UpdateSketch { + /// Add a polygon to the sketch + fn add_polygon(&self, points: Ps, services: &mut Services) -> Self + where + P: Into>, + Ps: IntoIterator, + Ps::IntoIter: Clone + ExactSizeIterator; +} + +impl UpdateSketch for Sketch { + fn add_polygon(&self, points: Ps, services: &mut Services) -> Self + where + P: Into>, + Ps: IntoIterator, + Ps::IntoIter: Clone + ExactSizeIterator, + { + let exterior = Cycle::polygon(points, services).insert(services); + let region = Region::new(exterior, Vec::new(), None); -impl UpdateSketch for Sketch {} + Sketch::new(self.regions().cloned().chain([region])) + } +} diff --git a/models/cuboid/src/lib.rs b/models/cuboid/src/lib.rs index 049c22244..db2657c55 100644 --- a/models/cuboid/src/lib.rs +++ b/models/cuboid/src/lib.rs @@ -2,7 +2,7 @@ use fj::{ core::{ algorithms::sweep::Sweep, objects::{Sketch, Solid}, - operations::{BuildSketch, Insert}, + operations::{BuildSketch, Insert, UpdateSketch}, services::Services, storage::Handle, }, @@ -12,16 +12,17 @@ use fj::{ pub fn cuboid(x: f64, y: f64, z: f64) -> Handle { let mut services = Services::new(); - let sketch = Sketch::polygon( - [ - [-x / 2., -y / 2.], - [x / 2., -y / 2.], - [x / 2., y / 2.], - [-x / 2., y / 2.], - ], - &mut services, - ) - .insert(&mut services); + let sketch = Sketch::empty() + .add_polygon( + [ + [-x / 2., -y / 2.], + [x / 2., -y / 2.], + [x / 2., y / 2.], + [-x / 2., y / 2.], + ], + &mut services, + ) + .insert(&mut services); let surface = services.objects.surfaces.xy_plane(); From 0467a6643bdea4f187509d8ae889288ef3c4db33 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:32:44 +0200 Subject: [PATCH 06/11] Add `UpdateSketch::add_region` --- crates/fj-core/src/operations/update/sketch.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index 349c5402e..c7c4c4e5c 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -9,6 +9,9 @@ use crate::{ /// Update a [`Sketch`] pub trait UpdateSketch { + /// Add a region to the sketch + fn add_region(&self, region: Region) -> Self; + /// Add a polygon to the sketch fn add_polygon(&self, points: Ps, services: &mut Services) -> Self where @@ -18,6 +21,10 @@ pub trait UpdateSketch { } impl UpdateSketch for Sketch { + fn add_region(&self, region: Region) -> Self { + Sketch::new(self.regions().cloned().chain([region])) + } + fn add_polygon(&self, points: Ps, services: &mut Services) -> Self where P: Into>, @@ -25,8 +32,6 @@ impl UpdateSketch for Sketch { Ps::IntoIter: Clone + ExactSizeIterator, { let exterior = Cycle::polygon(points, services).insert(services); - let region = Region::new(exterior, Vec::new(), None); - - Sketch::new(self.regions().cloned().chain([region])) + self.add_region(Region::new(exterior, Vec::new(), None)) } } From 0f448c9003a74a5aed33556ad9ed76c6ac06f49c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:36:35 +0200 Subject: [PATCH 07/11] Refactor --- crates/fj-core/src/operations/update/sketch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index c7c4c4e5c..faeff727b 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -32,6 +32,6 @@ impl UpdateSketch for Sketch { Ps::IntoIter: Clone + ExactSizeIterator, { let exterior = Cycle::polygon(points, services).insert(services); - self.add_region(Region::new(exterior, Vec::new(), None)) + self.add_region(Region::new(exterior, [], None)) } } From cade1ca39adce8df9e4a744068846914f22408e2 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:38:53 +0200 Subject: [PATCH 08/11] Make `BuildHalfEdge::circle` more flexible --- crates/fj-core/src/algorithms/approx/edge.rs | 2 +- crates/fj-core/src/operations/build/edge.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/fj-core/src/algorithms/approx/edge.rs b/crates/fj-core/src/algorithms/approx/edge.rs index a52659fc0..b2db71cc1 100644 --- a/crates/fj-core/src/algorithms/approx/edge.rs +++ b/crates/fj-core/src/algorithms/approx/edge.rs @@ -340,7 +340,7 @@ mod tests { let mut services = Services::new(); let surface = services.objects.surfaces.xz_plane(); - let half_edge = HalfEdge::circle(1., &mut services); + let half_edge = HalfEdge::circle([0., 0.], 1., &mut services); let tolerance = 1.; let approx = (&half_edge, surface.deref()).approx(tolerance); diff --git a/crates/fj-core/src/operations/build/edge.rs b/crates/fj-core/src/operations/build/edge.rs index c2fd80032..dc6efe436 100644 --- a/crates/fj-core/src/operations/build/edge.rs +++ b/crates/fj-core/src/operations/build/edge.rs @@ -49,8 +49,12 @@ pub trait BuildHalfEdge { } /// Create a circle - fn circle(radius: impl Into, services: &mut Services) -> HalfEdge { - let curve = Curve::circle_from_radius(radius); + fn circle( + center: impl Into>, + radius: impl Into, + services: &mut Services, + ) -> HalfEdge { + let curve = Curve::circle_from_center_and_radius(center, radius); let boundary = [Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord])); From e7f3393379dc8617c4f9e3d411c9e37dbc779ebc Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:39:21 +0200 Subject: [PATCH 09/11] Remove unused method --- crates/fj-core/src/geometry/curve.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/crates/fj-core/src/geometry/curve.rs b/crates/fj-core/src/geometry/curve.rs index bb07226bd..fd93f4735 100644 --- a/crates/fj-core/src/geometry/curve.rs +++ b/crates/fj-core/src/geometry/curve.rs @@ -15,11 +15,6 @@ pub enum Curve { } impl Curve { - /// Build a circle from the given radius - pub fn circle_from_radius(radius: impl Into) -> Self { - Self::circle_from_center_and_radius(Point::origin(), radius) - } - /// Build a circle from the given radius pub fn circle_from_center_and_radius( center: impl Into>, From 6064c5129e19e9342b93445e8178d88bf4125464 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:40:55 +0200 Subject: [PATCH 10/11] Add `BuildCycle::circle` --- crates/fj-core/src/operations/build/cycle.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/fj-core/src/operations/build/cycle.rs b/crates/fj-core/src/operations/build/cycle.rs index 2e97cf50d..9533ef2ef 100644 --- a/crates/fj-core/src/operations/build/cycle.rs +++ b/crates/fj-core/src/operations/build/cycle.rs @@ -1,9 +1,9 @@ -use fj_math::Point; +use fj_math::{Point, Scalar}; use itertools::Itertools; use crate::{ objects::{Cycle, HalfEdge}, - operations::{BuildHalfEdge, Insert}, + operations::{BuildHalfEdge, Insert, UpdateCycle}, services::Services, }; @@ -14,6 +14,17 @@ pub trait BuildCycle { Cycle::new([]) } + /// Build a circle + fn circle( + center: impl Into>, + radius: impl Into, + services: &mut Services, + ) -> Cycle { + let circle = + HalfEdge::circle(center, radius, services).insert(services); + Cycle::empty().add_half_edges([circle]) + } + /// Build a polygon fn polygon(points: Ps, services: &mut Services) -> Cycle where From d0eb0b2df1bab4e1b276855d2a861c028cc5719e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 12 Jun 2023 12:41:09 +0200 Subject: [PATCH 11/11] Add `UpdateSketch::add_circle` --- .../fj-core/src/operations/update/sketch.rs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index faeff727b..42f518cb8 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -1,4 +1,4 @@ -use fj_math::Point; +use fj_math::{Point, Scalar}; use crate::{ geometry::region::Region, @@ -12,6 +12,14 @@ pub trait UpdateSketch { /// Add a region to the sketch fn add_region(&self, region: Region) -> Self; + /// Add a circle to the sketch + fn add_circle( + &self, + center: impl Into>, + radius: impl Into, + services: &mut Services, + ) -> Self; + /// Add a polygon to the sketch fn add_polygon(&self, points: Ps, services: &mut Services) -> Self where @@ -25,6 +33,16 @@ impl UpdateSketch for Sketch { Sketch::new(self.regions().cloned().chain([region])) } + fn add_circle( + &self, + center: impl Into>, + radius: impl Into, + services: &mut Services, + ) -> Self { + let exterior = Cycle::circle(center, radius, services).insert(services); + self.add_region(Region::new(exterior, [], None)) + } + fn add_polygon(&self, points: Ps, services: &mut Services) -> Self where P: Into>,