Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate builder API for Face in FaceBuilder #1254

Merged
merged 10 commits into from
Oct 22, 2022
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 @@ -182,7 +182,8 @@ mod tests {
[ 1., -1.],
];

let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points(exterior)
.with_interior_polygon_from_points(interior)
.build();
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 @@ -79,7 +79,8 @@ mod tests {
];
let [a, b] = [objects.surfaces.xy_plane(), objects.surfaces.xz_plane()]
.map(|surface| {
Face::builder(&objects, surface)
Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points(points)
.build()
});
Expand All @@ -103,7 +104,8 @@ mod tests {
let surfaces =
[objects.surfaces.xy_plane(), objects.surfaces.xz_plane()];
let [a, b] = surfaces.clone().map(|surface| {
Face::builder(&objects, surface)
Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points(points)
.build()
});
Expand Down
24 changes: 16 additions & 8 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [1., 1.], [0., 2.]])
.build();
let point = Point::from([2., 1.]);
Expand All @@ -159,7 +160,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [2., 1.], [0., 2.]])
.build();
let point = Point::from([1., 1.]);
Expand All @@ -176,7 +178,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([[4., 2.], [0., 4.], [0., 0.]])
.build();
let point = Point::from([1., 2.]);
Expand All @@ -193,7 +196,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[0., 0.],
[2., 1.],
Expand All @@ -215,7 +219,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[0., 0.],
[2., 1.],
Expand All @@ -237,7 +242,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[0., 0.],
[2., 1.],
Expand All @@ -260,7 +266,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [2., 0.], [0., 1.]])
.build();
let point = Point::from([1., 0.]);
Expand All @@ -286,7 +293,8 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]])
.build();
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 @@ -163,7 +163,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.yz_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand All @@ -183,7 +184,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.yz_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand All @@ -206,7 +208,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.yz_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand All @@ -226,7 +229,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.yz_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand Down Expand Up @@ -257,7 +261,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.yz_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand Down Expand Up @@ -286,7 +291,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand All @@ -308,7 +314,8 @@ mod tests {
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([
[-1., -1.],
[1., -1.],
Expand Down
4 changes: 3 additions & 1 deletion crates/fj-kernel/src/algorithms/reverse/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ impl Reverse for Face {
let interiors =
self.interiors().map(|cycle| cycle.clone().reverse(objects));

Face::from_exterior(exterior)
Face::builder(objects)
.with_exterior(exterior)
.with_interiors(interiors)
.with_color(self.color())
.build()
}
}
7 changes: 5 additions & 2 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ impl Sweep for (Handle<HalfEdge>, Color) {
Cycle::new(surface, edges, objects)
};

Face::from_exterior(cycle).with_color(color)
Face::builder(objects)
.with_exterior(cycle)
.with_color(color)
.build()
}
}

Expand Down Expand Up @@ -245,7 +248,7 @@ mod tests {
&objects,
);

Face::from_exterior(cycle)
Face::builder(&objects).with_exterior(cycle).build()
};

assert_eq!(face, expected_face);
Expand Down
19 changes: 11 additions & 8 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ mod tests {
.build_polygon_from_points(TRIANGLE)
.sweep(UP, &objects);

let bottom = Face::builder(&objects, surface.clone())
let bottom = Face::builder(&objects)
.with_surface(surface.clone())
.with_exterior_polygon_from_points(TRIANGLE)
.build()
.reverse(&objects);
let top = Face::builder(&objects, surface.translate(UP, &objects))
let top = Face::builder(&objects)
.with_surface(surface.translate(UP, &objects))
.with_exterior_polygon_from_points(TRIANGLE)
.build();

Expand Down Expand Up @@ -134,12 +136,13 @@ mod tests {
.build_polygon_from_points(TRIANGLE)
.sweep(DOWN, &objects);

let bottom =
Face::builder(&objects, surface.clone().translate(DOWN, &objects))
.with_exterior_polygon_from_points(TRIANGLE)
.build()
.reverse(&objects);
let top = Face::builder(&objects, surface)
let bottom = Face::builder(&objects)
.with_surface(surface.clone().translate(DOWN, &objects))
.with_exterior_polygon_from_points(TRIANGLE)
.build()
.reverse(&objects);
let top = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points(TRIANGLE)
.build();

Expand Down
4 changes: 3 additions & 1 deletion crates/fj-kernel/src/algorithms/transform/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ impl TransformObject for Face {

let color = self.color();

Face::from_exterior(exterior)
Face::builder(objects)
.with_exterior(exterior)
.with_interiors(interiors)
.with_color(color)
.build()
}
}

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 @@ -99,7 +99,8 @@ mod tests {
let d = [0., 1.];

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface)
let face = Face::builder(&objects)
.with_surface(surface)
.with_exterior_polygon_from_points([a, b, c, d])
.build();

Expand Down Expand Up @@ -133,7 +134,8 @@ mod tests {
let h = [3., 1.];

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface.clone())
let face = Face::builder(&objects)
.with_surface(surface.clone())
.with_exterior_polygon_from_points([a, b, c, d])
.with_interior_polygon_from_points([e, f, g, h])
.build();
Expand Down Expand Up @@ -193,7 +195,8 @@ mod tests {
let e = [0., 0.8];

let surface = objects.surfaces.xy_plane();
let face = Face::builder(&objects, surface.clone())
let face = Face::builder(&objects)
.with_surface(surface.clone())
.with_exterior_polygon_from_points([a, b, c, d, e])
.build();

Expand Down
41 changes: 37 additions & 4 deletions crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fj_interop::mesh::Color;
use fj_math::Point;

use crate::{
Expand All @@ -14,7 +15,7 @@ pub struct FaceBuilder<'a> {
pub objects: &'a Objects,

/// The surface that the [`Face`] is defined in
pub surface: Handle<Surface>,
pub surface: Option<Handle<Surface>>,

/// The exterior cycle that bounds the [`Face`] on the outside
///
Expand All @@ -24,44 +25,76 @@ pub struct FaceBuilder<'a> {

/// The interior cycles that form holes in the [`Face`]
pub interiors: Vec<Handle<Cycle>>,

/// The color of the [`Face`]
pub color: Option<Color>,
}

impl<'a> FaceBuilder<'a> {
/// Build the [`Face`] with the provided surface
pub fn with_surface(mut self, surface: Handle<Surface>) -> Self {
self.surface = Some(surface);
self
}

/// Build the [`Face`] with the provided exterior
pub fn with_exterior(mut self, exterior: Handle<Cycle>) -> Self {
self.exterior = Some(exterior);
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>>>,
) -> Self {
self.exterior = Some(
Cycle::partial()
.with_surface(Some(self.surface.clone()))
.with_surface(self.surface.clone())
.with_poly_chain_from_points(points)
.close_with_line_segment()
.build(self.objects),
);
self
}

/// Build the [`Face`] with the provided interior polygons
pub fn with_interiors(
mut self,
interiors: impl IntoIterator<Item = Handle<Cycle>>,
) -> Self {
self.interiors.extend(interiors);
self
}

/// Build the [`Face`] with an interior polygon from the provided points
pub fn with_interior_polygon_from_points(
mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
self.interiors.push(
Cycle::partial()
.with_surface(Some(self.surface.clone()))
.with_surface(self.surface.clone())
.with_poly_chain_from_points(points)
.close_with_line_segment()
.build(self.objects),
);
self
}

/// Build the [`Face`] with the provided color
pub fn with_color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}

/// Construct a polygon from a list of points
pub fn build(self) -> Face {
let exterior = self
.exterior
.expect("Can't build `Face` without exterior cycle");
Face::from_exterior(exterior).with_interiors(self.interiors)
let color = self.color.unwrap_or_default();

Face::new(exterior, self.interiors, color)
}
}
Loading