diff --git a/crates/fj-core/src/operations/build/mod.rs b/crates/fj-core/src/operations/build/mod.rs index 1039b131c..8bfbbe2f0 100644 --- a/crates/fj-core/src/operations/build/mod.rs +++ b/crates/fj-core/src/operations/build/mod.rs @@ -1,6 +1,7 @@ pub mod cycle; pub mod edge; pub mod face; +pub mod region; pub mod shell; pub mod sketch; pub mod solid; diff --git a/crates/fj-core/src/operations/build/region.rs b/crates/fj-core/src/operations/build/region.rs new file mode 100644 index 000000000..eacc85711 --- /dev/null +++ b/crates/fj-core/src/operations/build/region.rs @@ -0,0 +1,33 @@ +use fj_math::{Point, Scalar}; + +use crate::{ + objects::{Cycle, Region}, + operations::{BuildCycle, Insert}, + services::Services, +}; + +/// Build a [`Region`] +pub trait BuildRegion { + /// Build a circle + fn circle( + center: impl Into>, + radius: impl Into, + services: &mut Services, + ) -> Region { + let exterior = Cycle::circle(center, radius, services).insert(services); + Region::new(exterior, [], None) + } + + /// Build a polygon + fn polygon(points: Ps, services: &mut Services) -> Region + where + P: Into>, + Ps: IntoIterator, + Ps::IntoIter: Clone + ExactSizeIterator, + { + let exterior = Cycle::polygon(points, services).insert(services); + Region::new(exterior, [], None) + } +} + +impl BuildRegion for Region {} diff --git a/crates/fj-core/src/operations/mod.rs b/crates/fj-core/src/operations/mod.rs index 4f109a4f4..4bd694904 100644 --- a/crates/fj-core/src/operations/mod.rs +++ b/crates/fj-core/src/operations/mod.rs @@ -10,6 +10,7 @@ pub use self::{ cycle::BuildCycle, edge::BuildHalfEdge, face::{BuildFace, Polygon}, + region::BuildRegion, shell::{BuildShell, TetrahedronShell}, sketch::BuildSketch, solid::{BuildSolid, Tetrahedron}, diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index 8dd771cee..39783d245 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -1,9 +1,5 @@ -use fj_math::{Point, Scalar}; - use crate::{ - objects::{Cycle, Region, Sketch}, - operations::{BuildCycle, Insert}, - services::Services, + objects::{Region, Sketch}, storage::Handle, }; @@ -11,47 +7,10 @@ use crate::{ pub trait UpdateSketch { /// Add a region to the sketch fn add_region(&self, region: Handle) -> 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 - P: Into>, - Ps: IntoIterator, - Ps::IntoIter: Clone + ExactSizeIterator; } impl UpdateSketch for Sketch { fn add_region(&self, region: Handle) -> Self { 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); - let region = Region::new(exterior, [], None).insert(services); - self.add_region(region) - } - - 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, [], None).insert(services); - self.add_region(region) - } } diff --git a/models/cuboid/src/lib.rs b/models/cuboid/src/lib.rs index db2657c55..bcab9ee5e 100644 --- a/models/cuboid/src/lib.rs +++ b/models/cuboid/src/lib.rs @@ -1,8 +1,8 @@ use fj::{ core::{ algorithms::sweep::Sweep, - objects::{Sketch, Solid}, - operations::{BuildSketch, Insert, UpdateSketch}, + objects::{Region, Sketch, Solid}, + operations::{BuildRegion, BuildSketch, Insert, UpdateSketch}, services::Services, storage::Handle, }, @@ -13,14 +13,17 @@ pub fn cuboid(x: f64, y: f64, z: f64) -> Handle { let mut services = Services::new(); let sketch = Sketch::empty() - .add_polygon( - [ - [-x / 2., -y / 2.], - [x / 2., -y / 2.], - [x / 2., y / 2.], - [-x / 2., y / 2.], - ], - &mut services, + .add_region( + Region::polygon( + [ + [-x / 2., -y / 2.], + [x / 2., -y / 2.], + [x / 2., y / 2.], + [-x / 2., y / 2.], + ], + &mut services, + ) + .insert(&mut services), ) .insert(&mut services);