From f4a7d9cd40f871dc706555d75b9fc2f08f76697b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 21 Sep 2022 11:23:32 +0200 Subject: [PATCH 1/6] Inline variable --- crates/fj-kernel/src/algorithms/sweep/edge.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep/edge.rs b/crates/fj-kernel/src/algorithms/sweep/edge.rs index 123a45992..947c3820f 100644 --- a/crates/fj-kernel/src/algorithms/sweep/edge.rs +++ b/crates/fj-kernel/src/algorithms/sweep/edge.rs @@ -198,18 +198,17 @@ mod tests { let expected_face = { let surface = Surface::xz_plane(); - let builder = HalfEdge::builder(&stores, surface); - let bottom = - builder.build_line_segment_from_points([[0., 0.], [1., 0.]]); - let top = builder + let bottom = HalfEdge::builder(&stores, surface) + .build_line_segment_from_points([[0., 0.], [1., 0.]]); + let top = HalfEdge::builder(&stores, surface) .build_line_segment_from_points([[0., 1.], [1., 1.]]) .reverse(); - let left = builder + let left = HalfEdge::builder(&stores, surface) .build_line_segment_from_points([[0., 0.], [0., 1.]]) .reverse(); - let right = - builder.build_line_segment_from_points([[1., 0.], [1., 1.]]); + let right = HalfEdge::builder(&stores, surface) + .build_line_segment_from_points([[1., 0.], [1., 1.]]); let cycle = Cycle::new(surface, [bottom, right, top, left]); From 1d39bef7451a15e960837c6820319e979d454003 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 21 Sep 2022 11:20:22 +0200 Subject: [PATCH 2/6] Move `self` into all `build_` methods --- crates/fj-kernel/src/builder/curve.rs | 6 +++--- crates/fj-kernel/src/builder/cycle.rs | 2 +- crates/fj-kernel/src/builder/edge.rs | 4 ++-- crates/fj-kernel/src/builder/face.rs | 4 ++-- crates/fj-kernel/src/builder/shell.rs | 2 +- crates/fj-kernel/src/builder/sketch.rs | 2 +- crates/fj-kernel/src/builder/solid.rs | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/fj-kernel/src/builder/curve.rs b/crates/fj-kernel/src/builder/curve.rs index 543782856..bc421a319 100644 --- a/crates/fj-kernel/src/builder/curve.rs +++ b/crates/fj-kernel/src/builder/curve.rs @@ -19,7 +19,7 @@ pub struct CurveBuilder<'a> { impl<'a> CurveBuilder<'a> { /// Build a line that represents the u-axis on the surface - pub fn build_u_axis(&self) -> Curve { + pub fn build_u_axis(self) -> Curve { let a = Point::origin(); let b = a + Vector::unit_u(); @@ -27,7 +27,7 @@ impl<'a> CurveBuilder<'a> { } /// Build a line that represents the v-axis on the surface - pub fn build_v_axis(&self) -> Curve { + pub fn build_v_axis(self) -> Curve { let a = Point::origin(); let b = a + Vector::unit_v(); @@ -35,7 +35,7 @@ impl<'a> CurveBuilder<'a> { } /// Build a circle from the given radius - pub fn build_circle_from_radius(&self, radius: impl Into) -> Curve { + pub fn build_circle_from_radius(self, radius: impl Into) -> Curve { let radius = radius.into(); let path = SurfacePath::circle_from_radius(radius); diff --git a/crates/fj-kernel/src/builder/cycle.rs b/crates/fj-kernel/src/builder/cycle.rs index f582a4ade..798bbd5ca 100644 --- a/crates/fj-kernel/src/builder/cycle.rs +++ b/crates/fj-kernel/src/builder/cycle.rs @@ -19,7 +19,7 @@ pub struct CycleBuilder<'a> { impl<'a> CycleBuilder<'a> { /// Create a polygon from a list of points pub fn build_polygon_from_points( - &self, + self, points: impl IntoIterator>>, ) -> Cycle { let mut points: Vec<_> = points.into_iter().map(Into::into).collect(); diff --git a/crates/fj-kernel/src/builder/edge.rs b/crates/fj-kernel/src/builder/edge.rs index 949ae29a3..f8611d45d 100644 --- a/crates/fj-kernel/src/builder/edge.rs +++ b/crates/fj-kernel/src/builder/edge.rs @@ -23,7 +23,7 @@ pub struct HalfEdgeBuilder<'a> { impl<'a> HalfEdgeBuilder<'a> { /// Build a circle from the given radius pub fn build_circle_from_radius( - &self, + self, radius: impl Into, ) -> HalfEdge { let curve = Curve::builder(self.stores, self.surface) @@ -62,7 +62,7 @@ impl<'a> HalfEdgeBuilder<'a> { /// Build a line segment from two points pub fn build_line_segment_from_points( - &self, + self, points: [impl Into>; 2], ) -> HalfEdge { let points = points.map(Into::into); diff --git a/crates/fj-kernel/src/builder/face.rs b/crates/fj-kernel/src/builder/face.rs index 0289b7958..cca2e782b 100644 --- a/crates/fj-kernel/src/builder/face.rs +++ b/crates/fj-kernel/src/builder/face.rs @@ -21,9 +21,9 @@ pub struct FaceBuilder<'a> { impl<'a> FaceBuilder<'a> { /// Construct a polygon from a list of points pub fn build_polygon_from_points( - &self, + self, points: impl IntoIterator>>, - ) -> FacePolygon { + ) -> FacePolygon<'a> { let cycle = Cycle::builder(self.stores, self.surface) .build_polygon_from_points(points); let face = Face::new(self.surface, cycle); diff --git a/crates/fj-kernel/src/builder/shell.rs b/crates/fj-kernel/src/builder/shell.rs index 435046ad5..7a9698ad9 100644 --- a/crates/fj-kernel/src/builder/shell.rs +++ b/crates/fj-kernel/src/builder/shell.rs @@ -17,7 +17,7 @@ pub struct ShellBuilder<'a> { impl<'a> ShellBuilder<'a> { /// Create a cube from the length of its edges pub fn build_cube_from_edge_length( - &self, + self, edge_length: impl Into, ) -> Shell { // Let's define a short-hand for half the edge length. We're going to diff --git a/crates/fj-kernel/src/builder/sketch.rs b/crates/fj-kernel/src/builder/sketch.rs index 0d608b3ef..8b39b55b8 100644 --- a/crates/fj-kernel/src/builder/sketch.rs +++ b/crates/fj-kernel/src/builder/sketch.rs @@ -19,7 +19,7 @@ pub struct SketchBuilder<'a> { impl<'a> SketchBuilder<'a> { /// Construct a polygon from a list of points pub fn build_polygon_from_points( - &self, + self, points: impl IntoIterator>>, ) -> Sketch { let face = Face::builder(self.stores, self.surface) diff --git a/crates/fj-kernel/src/builder/solid.rs b/crates/fj-kernel/src/builder/solid.rs index 08f7c2ebb..335a051d7 100644 --- a/crates/fj-kernel/src/builder/solid.rs +++ b/crates/fj-kernel/src/builder/solid.rs @@ -16,7 +16,7 @@ pub struct SolidBuilder<'a> { impl<'a> SolidBuilder<'a> { /// Create a cube from the length of its edges pub fn build_cube_from_edge_length( - &self, + self, edge_length: impl Into, ) -> Solid { let shell = Shell::builder(self.stores) From 212fbe2853caf5cc8fe97757592aaaf01ddad23f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 21 Sep 2022 11:11:37 +0200 Subject: [PATCH 3/6] Make variable name more specific --- crates/fj-kernel/src/builder/face.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/builder/face.rs b/crates/fj-kernel/src/builder/face.rs index cca2e782b..b08c49ba4 100644 --- a/crates/fj-kernel/src/builder/face.rs +++ b/crates/fj-kernel/src/builder/face.rs @@ -24,9 +24,9 @@ impl<'a> FaceBuilder<'a> { self, points: impl IntoIterator>>, ) -> FacePolygon<'a> { - let cycle = Cycle::builder(self.stores, self.surface) + let exterior = Cycle::builder(self.stores, self.surface) .build_polygon_from_points(points); - let face = Face::new(self.surface, cycle); + let face = Face::new(self.surface, exterior); FacePolygon { stores: self.stores, From 7f287bff4827e17caa92356ee6f55b977d4ec6ff Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 21 Sep 2022 11:27:02 +0200 Subject: [PATCH 4/6] Split `FaceBuilder` method into two --- .../src/algorithms/intersect/curve_face.rs | 3 +- .../src/algorithms/intersect/face_face.rs | 6 ++-- .../src/algorithms/intersect/face_point.rs | 34 ++++++++++++++----- .../src/algorithms/intersect/ray_face.rs | 21 ++++++++---- crates/fj-kernel/src/algorithms/sweep/face.rs | 12 ++++--- .../src/algorithms/triangulate/mod.rs | 9 +++-- crates/fj-kernel/src/builder/face.rs | 28 +++++++++++---- crates/fj-kernel/src/builder/shell.rs | 3 +- crates/fj-kernel/src/builder/sketch.rs | 3 +- crates/fj-kernel/src/iter.rs | 6 ++-- crates/fj-kernel/src/objects/face.rs | 6 +++- crates/fj-operations/src/sketch.rs | 3 +- 12 files changed, 97 insertions(+), 37 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/intersect/curve_face.rs b/crates/fj-kernel/src/algorithms/intersect/curve_face.rs index 1f355df13..2223257e9 100644 --- a/crates/fj-kernel/src/algorithms/intersect/curve_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/curve_face.rs @@ -187,7 +187,8 @@ mod tests { ]; let face = Face::builder(&stores, surface) - .build_polygon_from_points(exterior) + .with_exterior_polygon_from_points(exterior) + .build() .with_hole(interior) .into_face(); diff --git a/crates/fj-kernel/src/algorithms/intersect/face_face.rs b/crates/fj-kernel/src/algorithms/intersect/face_face.rs index 806700be7..9e5aad5eb 100644 --- a/crates/fj-kernel/src/algorithms/intersect/face_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/face_face.rs @@ -84,7 +84,8 @@ mod tests { let surfaces = [Surface::xy_plane(), Surface::xz_plane()]; let [a, b] = surfaces.map(|surface| { Face::builder(&stores, surface) - .build_polygon_from_points(points) + .with_exterior_polygon_from_points(points) + .build() .into_face() }); @@ -107,7 +108,8 @@ mod tests { let surfaces = [Surface::xy_plane(), Surface::xz_plane()]; let [a, b] = surfaces.map(|surface| { Face::builder(&stores, surface) - .build_polygon_from_points(points) + .with_exterior_polygon_from_points(points) + .build() .into_face() }); diff --git a/crates/fj-kernel/src/algorithms/intersect/face_point.rs b/crates/fj-kernel/src/algorithms/intersect/face_point.rs index 92742d0f3..c27e18091 100644 --- a/crates/fj-kernel/src/algorithms/intersect/face_point.rs +++ b/crates/fj-kernel/src/algorithms/intersect/face_point.rs @@ -143,7 +143,8 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[0., 0.], [1., 1.], [0., 2.]]) + .with_exterior_polygon_from_points([[0., 0.], [1., 1.], [0., 2.]]) + .build() .into_face(); let point = Point::from([2., 1.]); @@ -156,7 +157,8 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[0., 0.], [2., 1.], [0., 2.]]) + .with_exterior_polygon_from_points([[0., 0.], [2., 1.], [0., 2.]]) + .build() .into_face(); let point = Point::from([1., 1.]); @@ -172,7 +174,8 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[4., 2.], [0., 4.], [0., 0.]]) + .with_exterior_polygon_from_points([[4., 2.], [0., 4.], [0., 0.]]) + .build() .into_face(); let point = Point::from([1., 2.]); @@ -188,7 +191,13 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[0., 0.], [2., 1.], [3., 0.], [3., 4.]]) + .with_exterior_polygon_from_points([ + [0., 0.], + [2., 1.], + [3., 0.], + [3., 4.], + ]) + .build() .into_face(); let point = Point::from([1., 1.]); @@ -204,7 +213,13 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[0., 0.], [2., 1.], [3., 1.], [0., 2.]]) + .with_exterior_polygon_from_points([ + [0., 0.], + [2., 1.], + [3., 1.], + [0., 2.], + ]) + .build() .into_face(); let point = Point::from([1., 1.]); @@ -220,13 +235,14 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [0., 0.], [2., 1.], [3., 1.], [4., 0.], [4., 5.], ]) + .build() .into_face(); let point = Point::from([1., 1.]); @@ -242,7 +258,8 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[0., 0.], [2., 0.], [0., 1.]]) + .with_exterior_polygon_from_points([[0., 0.], [2., 0.], [0., 1.]]) + .build() .into_face(); let point = Point::from([1., 0.]); @@ -267,7 +284,8 @@ mod tests { let stores = Stores::new(); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) + .with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) + .build() .into_face(); let point = Point::from([1., 0.]); diff --git a/crates/fj-kernel/src/algorithms/intersect/ray_face.rs b/crates/fj-kernel/src/algorithms/intersect/ray_face.rs index 0201a4efd..7ce478fdc 100644 --- a/crates/fj-kernel/src/algorithms/intersect/ray_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/ray_face.rs @@ -169,12 +169,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::yz_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face() .translate([-1., 0., 0.], &stores); @@ -188,12 +189,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::yz_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face() .translate([1., 0., 0.], &stores); @@ -210,12 +212,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::yz_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face() .translate([0., 0., 2.], &stores); @@ -229,12 +232,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::yz_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face() .translate([1., 1., 0.], &stores); @@ -259,12 +263,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::yz_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face() .translate([1., 1., 1.], &stores); @@ -287,12 +292,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face(); assert_eq!( @@ -308,12 +314,13 @@ mod tests { let ray = HorizontalRayToTheRight::from([0., 0., 0.]); let face = Face::builder(&stores, Surface::xy_plane()) - .build_polygon_from_points([ + .with_exterior_polygon_from_points([ [-1., -1.], [1., -1.], [1., 1.], [-1., 1.], ]) + .build() .into_face() .translate([0., 0., 1.], &stores); diff --git a/crates/fj-kernel/src/algorithms/sweep/face.rs b/crates/fj-kernel/src/algorithms/sweep/face.rs index 4715edb2d..535ede82e 100644 --- a/crates/fj-kernel/src/algorithms/sweep/face.rs +++ b/crates/fj-kernel/src/algorithms/sweep/face.rs @@ -95,11 +95,13 @@ mod tests { .sweep(UP, &stores); let bottom = Face::builder(&stores, surface) - .build_polygon_from_points(TRIANGLE) + .with_exterior_polygon_from_points(TRIANGLE) + .build() .into_face() .reverse(); let top = Face::builder(&stores, surface.translate(UP, &stores)) - .build_polygon_from_points(TRIANGLE) + .with_exterior_polygon_from_points(TRIANGLE) + .build() .into_face(); assert!(solid.find_face(&bottom).is_some()); @@ -130,11 +132,13 @@ mod tests { .sweep(DOWN, &stores); let bottom = Face::builder(&stores, surface.translate(DOWN, &stores)) - .build_polygon_from_points(TRIANGLE) + .with_exterior_polygon_from_points(TRIANGLE) + .build() .into_face() .reverse(); let top = Face::builder(&stores, surface) - .build_polygon_from_points(TRIANGLE) + .with_exterior_polygon_from_points(TRIANGLE) + .build() .into_face(); assert!(solid.find_face(&bottom).is_some()); diff --git a/crates/fj-kernel/src/algorithms/triangulate/mod.rs b/crates/fj-kernel/src/algorithms/triangulate/mod.rs index d29ae4752..26445c39b 100644 --- a/crates/fj-kernel/src/algorithms/triangulate/mod.rs +++ b/crates/fj-kernel/src/algorithms/triangulate/mod.rs @@ -101,7 +101,8 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) - .build_polygon_from_points([a, b, c, d]) + .with_exterior_polygon_from_points([a, b, c, d]) + .build() .into_face(); let a = Point::from(a).to_xyz(); @@ -135,7 +136,8 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) - .build_polygon_from_points([a, b, c, d]) + .with_exterior_polygon_from_points([a, b, c, d]) + .build() .with_hole([e, f, g, h]) .into_face(); @@ -188,7 +190,8 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) - .build_polygon_from_points([a, b, c, d, e]) + .with_exterior_polygon_from_points([a, b, c, d, e]) + .build() .into_face(); let triangles = triangulate(face)?; diff --git a/crates/fj-kernel/src/builder/face.rs b/crates/fj-kernel/src/builder/face.rs index b08c49ba4..f0fcb389f 100644 --- a/crates/fj-kernel/src/builder/face.rs +++ b/crates/fj-kernel/src/builder/face.rs @@ -16,16 +16,32 @@ pub struct FaceBuilder<'a> { /// The surface that the [`Face`] is defined in pub surface: Surface, + + /// The exterior cycle that bounds the [`Face`] on the outside + /// + /// Must be provided by the caller, directly or using one of the `with_` + /// methods, before [`FaceBuilder::build`] is called. + pub exterior: Option, } impl<'a> FaceBuilder<'a> { - /// Construct a polygon from a list of points - pub fn build_polygon_from_points( - self, + /// Build the [`Face`] with an exterior polygon from the provided points + pub fn with_exterior_polygon_from_points( + mut self, points: impl IntoIterator>>, - ) -> FacePolygon<'a> { - let exterior = Cycle::builder(self.stores, self.surface) - .build_polygon_from_points(points); + ) -> Self { + self.exterior = Some( + Cycle::builder(self.stores, self.surface) + .build_polygon_from_points(points), + ); + self + } + + /// Construct a polygon from a list of points + pub fn build(self) -> FacePolygon<'a> { + let exterior = self + .exterior + .expect("Can't build `Face` without exterior cycle"); let face = Face::new(self.surface, exterior); FacePolygon { diff --git a/crates/fj-kernel/src/builder/shell.rs b/crates/fj-kernel/src/builder/shell.rs index 7a9698ad9..705697e9e 100644 --- a/crates/fj-kernel/src/builder/shell.rs +++ b/crates/fj-kernel/src/builder/shell.rs @@ -38,7 +38,8 @@ impl<'a> ShellBuilder<'a> { let faces = planes.map(|plane| { Face::builder(self.stores, plane) - .build_polygon_from_points(points) + .with_exterior_polygon_from_points(points) + .build() .into_face() }); diff --git a/crates/fj-kernel/src/builder/sketch.rs b/crates/fj-kernel/src/builder/sketch.rs index 8b39b55b8..2f993524f 100644 --- a/crates/fj-kernel/src/builder/sketch.rs +++ b/crates/fj-kernel/src/builder/sketch.rs @@ -23,7 +23,8 @@ impl<'a> SketchBuilder<'a> { points: impl IntoIterator>>, ) -> Sketch { let face = Face::builder(self.stores, self.surface) - .build_polygon_from_points(points) + .with_exterior_polygon_from_points(points) + .build() .into_face(); Sketch::new().with_faces([face]) } diff --git a/crates/fj-kernel/src/iter.rs b/crates/fj-kernel/src/iter.rs index 39424bb70..a38a3a3e1 100644 --- a/crates/fj-kernel/src/iter.rs +++ b/crates/fj-kernel/src/iter.rs @@ -403,7 +403,8 @@ mod tests { let surface = Surface::xy_plane(); let object = Face::builder(&stores, surface) - .build_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) + .with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) + .build() .into_face(); assert_eq!(3, object.curve_iter().count()); @@ -500,7 +501,8 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) - .build_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) + .with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) + .build() .into_face(); let object = Sketch::new().with_faces([face]); diff --git a/crates/fj-kernel/src/objects/face.rs b/crates/fj-kernel/src/objects/face.rs index 21a94387d..540257d0b 100644 --- a/crates/fj-kernel/src/objects/face.rs +++ b/crates/fj-kernel/src/objects/face.rs @@ -67,7 +67,11 @@ pub struct Face { impl Face { /// Build a `Face` using [`FaceBuilder`] pub fn builder(stores: &Stores, surface: Surface) -> FaceBuilder { - FaceBuilder { stores, surface } + FaceBuilder { + stores, + surface, + exterior: None, + } } /// Construct a new instance of `Face` diff --git a/crates/fj-operations/src/sketch.rs b/crates/fj-operations/src/sketch.rs index 40e21bb3e..2dc3e0d0f 100644 --- a/crates/fj-operations/src/sketch.rs +++ b/crates/fj-operations/src/sketch.rs @@ -37,7 +37,8 @@ impl Shape for fj::Sketch { poly_chain.to_points().into_iter().map(Point::from); Face::builder(stores, surface) - .build_polygon_from_points(points) + .with_exterior_polygon_from_points(points) + .build() .into_face() .with_color(Color(self.color())) } From 5e526a74ae8aede2d02de5def257c58b67e6ec2e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 21 Sep 2022 11:39:03 +0200 Subject: [PATCH 5/6] Add additional method to `FaceBuilder` --- .../src/algorithms/intersect/curve_face.rs | 2 +- .../src/algorithms/triangulate/mod.rs | 2 +- crates/fj-kernel/src/builder/face.rs | 18 +++++++++++++++++- crates/fj-kernel/src/objects/face.rs | 1 + 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/intersect/curve_face.rs b/crates/fj-kernel/src/algorithms/intersect/curve_face.rs index 2223257e9..b33643310 100644 --- a/crates/fj-kernel/src/algorithms/intersect/curve_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/curve_face.rs @@ -188,8 +188,8 @@ mod tests { let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points(exterior) + .with_interior_polygon_from_points(interior) .build() - .with_hole(interior) .into_face(); let expected = diff --git a/crates/fj-kernel/src/algorithms/triangulate/mod.rs b/crates/fj-kernel/src/algorithms/triangulate/mod.rs index 26445c39b..8c8ae8e57 100644 --- a/crates/fj-kernel/src/algorithms/triangulate/mod.rs +++ b/crates/fj-kernel/src/algorithms/triangulate/mod.rs @@ -137,8 +137,8 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points([a, b, c, d]) + .with_interior_polygon_from_points([e, f, g, h]) .build() - .with_hole([e, f, g, h]) .into_face(); let triangles = triangulate(face)?; diff --git a/crates/fj-kernel/src/builder/face.rs b/crates/fj-kernel/src/builder/face.rs index f0fcb389f..4fc65c8d9 100644 --- a/crates/fj-kernel/src/builder/face.rs +++ b/crates/fj-kernel/src/builder/face.rs @@ -22,6 +22,9 @@ pub struct FaceBuilder<'a> { /// Must be provided by the caller, directly or using one of the `with_` /// methods, before [`FaceBuilder::build`] is called. pub exterior: Option, + + /// The interior cycles that form holes in the [`Face`] + pub interiors: Vec, } impl<'a> FaceBuilder<'a> { @@ -37,12 +40,25 @@ impl<'a> FaceBuilder<'a> { self } + /// Build the [`Face`] with an interior polygon from the provided points + pub fn with_interior_polygon_from_points( + mut self, + points: impl IntoIterator>>, + ) -> Self { + self.interiors.push( + Cycle::builder(self.stores, self.surface) + .build_polygon_from_points(points), + ); + self + } + /// Construct a polygon from a list of points pub fn build(self) -> FacePolygon<'a> { let exterior = self .exterior .expect("Can't build `Face` without exterior cycle"); - let face = Face::new(self.surface, exterior); + let face = + Face::new(self.surface, exterior).with_interiors(self.interiors); FacePolygon { stores: self.stores, diff --git a/crates/fj-kernel/src/objects/face.rs b/crates/fj-kernel/src/objects/face.rs index 540257d0b..948603d94 100644 --- a/crates/fj-kernel/src/objects/face.rs +++ b/crates/fj-kernel/src/objects/face.rs @@ -71,6 +71,7 @@ impl Face { stores, surface, exterior: None, + interiors: Vec::new(), } } From ba0bc7df214e7ae5f5c97b2708c141bf81d4924f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 21 Sep 2022 11:42:48 +0200 Subject: [PATCH 6/6] Remove redundant code --- .../src/algorithms/intersect/curve_face.rs | 3 +- .../src/algorithms/intersect/face_face.rs | 2 - .../src/algorithms/intersect/face_point.rs | 24 +++------ .../src/algorithms/intersect/ray_face.rs | 9 +--- crates/fj-kernel/src/algorithms/sweep/face.rs | 8 +-- .../src/algorithms/triangulate/mod.rs | 9 ++-- crates/fj-kernel/src/builder/face.rs | 54 +------------------ crates/fj-kernel/src/builder/mod.rs | 2 +- crates/fj-kernel/src/builder/shell.rs | 1 - crates/fj-kernel/src/builder/sketch.rs | 3 +- crates/fj-kernel/src/iter.rs | 6 +-- crates/fj-operations/src/sketch.rs | 1 - 12 files changed, 21 insertions(+), 101 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/intersect/curve_face.rs b/crates/fj-kernel/src/algorithms/intersect/curve_face.rs index b33643310..35ee1f673 100644 --- a/crates/fj-kernel/src/algorithms/intersect/curve_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/curve_face.rs @@ -189,8 +189,7 @@ mod tests { let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points(exterior) .with_interior_polygon_from_points(interior) - .build() - .into_face(); + .build(); let expected = CurveFaceIntersection::from_intervals([[[1.], [2.]], [[4.], [5.]]]); diff --git a/crates/fj-kernel/src/algorithms/intersect/face_face.rs b/crates/fj-kernel/src/algorithms/intersect/face_face.rs index 9e5aad5eb..1e93b9151 100644 --- a/crates/fj-kernel/src/algorithms/intersect/face_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/face_face.rs @@ -86,7 +86,6 @@ mod tests { Face::builder(&stores, surface) .with_exterior_polygon_from_points(points) .build() - .into_face() }); let intersection = FaceFaceIntersection::compute([&a, &b], &stores); @@ -110,7 +109,6 @@ mod tests { Face::builder(&stores, surface) .with_exterior_polygon_from_points(points) .build() - .into_face() }); let intersection = FaceFaceIntersection::compute([&a, &b], &stores); diff --git a/crates/fj-kernel/src/algorithms/intersect/face_point.rs b/crates/fj-kernel/src/algorithms/intersect/face_point.rs index c27e18091..8b1a8dcea 100644 --- a/crates/fj-kernel/src/algorithms/intersect/face_point.rs +++ b/crates/fj-kernel/src/algorithms/intersect/face_point.rs @@ -144,8 +144,7 @@ mod tests { let face = Face::builder(&stores, Surface::xy_plane()) .with_exterior_polygon_from_points([[0., 0.], [1., 1.], [0., 2.]]) - .build() - .into_face(); + .build(); let point = Point::from([2., 1.]); let intersection = (&face, &point).intersect(); @@ -158,8 +157,7 @@ mod tests { let face = Face::builder(&stores, Surface::xy_plane()) .with_exterior_polygon_from_points([[0., 0.], [2., 1.], [0., 2.]]) - .build() - .into_face(); + .build(); let point = Point::from([1., 1.]); let intersection = (&face, &point).intersect(); @@ -175,8 +173,7 @@ mod tests { let face = Face::builder(&stores, Surface::xy_plane()) .with_exterior_polygon_from_points([[4., 2.], [0., 4.], [0., 0.]]) - .build() - .into_face(); + .build(); let point = Point::from([1., 2.]); let intersection = (&face, &point).intersect(); @@ -197,8 +194,7 @@ mod tests { [3., 0.], [3., 4.], ]) - .build() - .into_face(); + .build(); let point = Point::from([1., 1.]); let intersection = (&face, &point).intersect(); @@ -219,8 +215,7 @@ mod tests { [3., 1.], [0., 2.], ]) - .build() - .into_face(); + .build(); let point = Point::from([1., 1.]); let intersection = (&face, &point).intersect(); @@ -242,8 +237,7 @@ mod tests { [4., 0.], [4., 5.], ]) - .build() - .into_face(); + .build(); let point = Point::from([1., 1.]); let intersection = (&face, &point).intersect(); @@ -259,8 +253,7 @@ mod tests { let face = Face::builder(&stores, Surface::xy_plane()) .with_exterior_polygon_from_points([[0., 0.], [2., 0.], [0., 1.]]) - .build() - .into_face(); + .build(); let point = Point::from([1., 0.]); let intersection = (&face, &point).intersect(); @@ -285,8 +278,7 @@ mod tests { let face = Face::builder(&stores, Surface::xy_plane()) .with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) - .build() - .into_face(); + .build(); let point = Point::from([1., 0.]); let intersection = (&face, &point).intersect(); diff --git a/crates/fj-kernel/src/algorithms/intersect/ray_face.rs b/crates/fj-kernel/src/algorithms/intersect/ray_face.rs index 7ce478fdc..30e9ee513 100644 --- a/crates/fj-kernel/src/algorithms/intersect/ray_face.rs +++ b/crates/fj-kernel/src/algorithms/intersect/ray_face.rs @@ -176,7 +176,6 @@ mod tests { [-1., 1.], ]) .build() - .into_face() .translate([-1., 0., 0.], &stores); assert_eq!((&ray, &face).intersect(), None); @@ -196,7 +195,6 @@ mod tests { [-1., 1.], ]) .build() - .into_face() .translate([1., 0., 0.], &stores); assert_eq!( @@ -219,7 +217,6 @@ mod tests { [-1., 1.], ]) .build() - .into_face() .translate([0., 0., 2.], &stores); assert_eq!((&ray, &face).intersect(), None); @@ -239,7 +236,6 @@ mod tests { [-1., 1.], ]) .build() - .into_face() .translate([1., 1., 0.], &stores); let edge = face @@ -270,7 +266,6 @@ mod tests { [-1., 1.], ]) .build() - .into_face() .translate([1., 1., 1.], &stores); let vertex = face @@ -298,8 +293,7 @@ mod tests { [1., 1.], [-1., 1.], ]) - .build() - .into_face(); + .build(); assert_eq!( (&ray, &face).intersect(), @@ -321,7 +315,6 @@ mod tests { [-1., 1.], ]) .build() - .into_face() .translate([0., 0., 1.], &stores); assert_eq!((&ray, &face).intersect(), None) diff --git a/crates/fj-kernel/src/algorithms/sweep/face.rs b/crates/fj-kernel/src/algorithms/sweep/face.rs index 535ede82e..36906cfbd 100644 --- a/crates/fj-kernel/src/algorithms/sweep/face.rs +++ b/crates/fj-kernel/src/algorithms/sweep/face.rs @@ -97,12 +97,10 @@ mod tests { let bottom = Face::builder(&stores, surface) .with_exterior_polygon_from_points(TRIANGLE) .build() - .into_face() .reverse(); let top = Face::builder(&stores, surface.translate(UP, &stores)) .with_exterior_polygon_from_points(TRIANGLE) - .build() - .into_face(); + .build(); assert!(solid.find_face(&bottom).is_some()); assert!(solid.find_face(&top).is_some()); @@ -134,12 +132,10 @@ mod tests { let bottom = Face::builder(&stores, surface.translate(DOWN, &stores)) .with_exterior_polygon_from_points(TRIANGLE) .build() - .into_face() .reverse(); let top = Face::builder(&stores, surface) .with_exterior_polygon_from_points(TRIANGLE) - .build() - .into_face(); + .build(); assert!(solid.find_face(&bottom).is_some()); assert!(solid.find_face(&top).is_some()); diff --git a/crates/fj-kernel/src/algorithms/triangulate/mod.rs b/crates/fj-kernel/src/algorithms/triangulate/mod.rs index 8c8ae8e57..5912fc12d 100644 --- a/crates/fj-kernel/src/algorithms/triangulate/mod.rs +++ b/crates/fj-kernel/src/algorithms/triangulate/mod.rs @@ -102,8 +102,7 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points([a, b, c, d]) - .build() - .into_face(); + .build(); let a = Point::from(a).to_xyz(); let b = Point::from(b).to_xyz(); @@ -138,8 +137,7 @@ mod tests { let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points([a, b, c, d]) .with_interior_polygon_from_points([e, f, g, h]) - .build() - .into_face(); + .build(); let triangles = triangulate(face)?; @@ -191,8 +189,7 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points([a, b, c, d, e]) - .build() - .into_face(); + .build(); let triangles = triangulate(face)?; diff --git a/crates/fj-kernel/src/builder/face.rs b/crates/fj-kernel/src/builder/face.rs index 4fc65c8d9..6dccad7b4 100644 --- a/crates/fj-kernel/src/builder/face.rs +++ b/crates/fj-kernel/src/builder/face.rs @@ -1,5 +1,3 @@ -use std::ops::Deref; - use fj_math::Point; use crate::{ @@ -53,58 +51,10 @@ impl<'a> FaceBuilder<'a> { } /// Construct a polygon from a list of points - pub fn build(self) -> FacePolygon<'a> { + pub fn build(self) -> Face { let exterior = self .exterior .expect("Can't build `Face` without exterior cycle"); - let face = - Face::new(self.surface, exterior).with_interiors(self.interiors); - - FacePolygon { - stores: self.stores, - face, - } - } -} - -/// A polygon -#[derive(Clone, Debug)] -pub struct FacePolygon<'a> { - stores: &'a Stores, - face: Face, -} - -impl FacePolygon<'_> { - /// Add a hole to the polygon - pub fn with_hole( - mut self, - points: impl IntoIterator>>, - ) -> Self { - let surface = *self.face.surface(); - self.face = - self.face - .with_interiors([Cycle::builder(self.stores, surface) - .build_polygon_from_points(points)]); - - self - } - - /// Consume the `Polygon` and return the [`Face`] it wraps - pub fn into_face(self) -> Face { - self.face - } -} - -impl From> for Face { - fn from(polygon: FacePolygon) -> Self { - polygon.into_face() - } -} - -impl Deref for FacePolygon<'_> { - type Target = Face; - - fn deref(&self) -> &Self::Target { - &self.face + Face::new(self.surface, exterior).with_interiors(self.interiors) } } diff --git a/crates/fj-kernel/src/builder/mod.rs b/crates/fj-kernel/src/builder/mod.rs index 9bc5b153a..0c73ef261 100644 --- a/crates/fj-kernel/src/builder/mod.rs +++ b/crates/fj-kernel/src/builder/mod.rs @@ -13,7 +13,7 @@ pub use self::{ curve::{CurveBuilder, GlobalCurveBuilder}, cycle::CycleBuilder, edge::HalfEdgeBuilder, - face::{FaceBuilder, FacePolygon}, + face::FaceBuilder, shell::ShellBuilder, sketch::SketchBuilder, solid::SolidBuilder, diff --git a/crates/fj-kernel/src/builder/shell.rs b/crates/fj-kernel/src/builder/shell.rs index 705697e9e..95107d69f 100644 --- a/crates/fj-kernel/src/builder/shell.rs +++ b/crates/fj-kernel/src/builder/shell.rs @@ -40,7 +40,6 @@ impl<'a> ShellBuilder<'a> { Face::builder(self.stores, plane) .with_exterior_polygon_from_points(points) .build() - .into_face() }); Shell::new().with_faces(faces) diff --git a/crates/fj-kernel/src/builder/sketch.rs b/crates/fj-kernel/src/builder/sketch.rs index 2f993524f..7a25982fa 100644 --- a/crates/fj-kernel/src/builder/sketch.rs +++ b/crates/fj-kernel/src/builder/sketch.rs @@ -24,8 +24,7 @@ impl<'a> SketchBuilder<'a> { ) -> Sketch { let face = Face::builder(self.stores, self.surface) .with_exterior_polygon_from_points(points) - .build() - .into_face(); + .build(); Sketch::new().with_faces([face]) } } diff --git a/crates/fj-kernel/src/iter.rs b/crates/fj-kernel/src/iter.rs index a38a3a3e1..477344458 100644 --- a/crates/fj-kernel/src/iter.rs +++ b/crates/fj-kernel/src/iter.rs @@ -404,8 +404,7 @@ mod tests { let surface = Surface::xy_plane(); let object = Face::builder(&stores, surface) .with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) - .build() - .into_face(); + .build(); assert_eq!(3, object.curve_iter().count()); assert_eq!(1, object.cycle_iter().count()); @@ -502,8 +501,7 @@ mod tests { let surface = Surface::xy_plane(); let face = Face::builder(&stores, surface) .with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]]) - .build() - .into_face(); + .build(); let object = Sketch::new().with_faces([face]); assert_eq!(3, object.curve_iter().count()); diff --git a/crates/fj-operations/src/sketch.rs b/crates/fj-operations/src/sketch.rs index 2dc3e0d0f..a22893d8a 100644 --- a/crates/fj-operations/src/sketch.rs +++ b/crates/fj-operations/src/sketch.rs @@ -39,7 +39,6 @@ impl Shape for fj::Sketch { Face::builder(stores, surface) .with_exterior_polygon_from_points(points) .build() - .into_face() .with_color(Color(self.color())) } };