Skip to content

Commit

Permalink
Split FaceBuilder method into two
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Sep 21, 2022
1 parent 212fbe2 commit 7f287bf
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 37 deletions.
3 changes: 2 additions & 1 deletion crates/fj-kernel/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 4 additions & 2 deletions crates/fj-kernel/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});

Expand All @@ -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()
});

Expand Down
34 changes: 26 additions & 8 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.]);

Expand All @@ -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.]);

Expand All @@ -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.]);

Expand All @@ -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.]);

Expand All @@ -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.]);

Expand All @@ -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.]);

Expand All @@ -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.]);

Expand All @@ -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.]);

Expand Down
21 changes: 14 additions & 7 deletions crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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!(
Expand All @@ -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);

Expand Down
12 changes: 8 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
9 changes: 6 additions & 3 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)?;
Expand Down
28 changes: 22 additions & 6 deletions crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cycle>,
}

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<Item = impl Into<Point<2>>>,
) -> 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 {
Expand Down
3 changes: 2 additions & 1 deletion crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});

Expand Down
3 changes: 2 additions & 1 deletion crates/fj-kernel/src/builder/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl<'a> SketchBuilder<'a> {
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> 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])
}
Expand Down
6 changes: 4 additions & 2 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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]);

Expand Down
6 changes: 5 additions & 1 deletion crates/fj-kernel/src/objects/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down

0 comments on commit 7f287bf

Please sign in to comment.