Skip to content

Commit

Permalink
Merge pull request #1453 from hannobraun/builder
Browse files Browse the repository at this point in the history
Continue cleanup of builder API
  • Loading branch information
hannobraun authored Dec 15, 2022
2 parents 04ec62a + aa8b7e8 commit e7dc9b2
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 75 deletions.
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ mod tests {

let face = {
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(surface.clone(), exterior);
face.with_interior_polygon_from_points(surface, interior);
face.update_exterior_as_polygon(surface.clone(), exterior);
face.add_interior_polygon(surface, interior);

face.build(&mut services.objects)
};
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/intersect/face_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod tests {
]
.map(|surface| {
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(surface, points);
face.update_exterior_as_polygon(surface, points);

face.build(&mut services.objects)
});
Expand Down Expand Up @@ -122,7 +122,7 @@ mod tests {
];
let [a, b] = surfaces.clone().map(|surface| {
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(surface, points);
face.update_exterior_as_polygon(surface, points);

face.build(&mut services.objects)
});
Expand Down
16 changes: 8 additions & 8 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [1., 1.], [0., 2.]],
);
Expand All @@ -168,7 +168,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [2., 1.], [0., 2.]],
);
Expand All @@ -190,7 +190,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[4., 2.], [0., 4.], [0., 0.]],
);
Expand All @@ -212,7 +212,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [2., 1.], [3., 0.], [3., 4.]],
);
Expand All @@ -234,7 +234,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [2., 1.], [3., 1.], [0., 2.]],
);
Expand All @@ -256,7 +256,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [2., 1.], [3., 1.], [4., 0.], [4., 5.]],
);
Expand All @@ -278,7 +278,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [2., 0.], [0., 1.]],
);
Expand Down Expand Up @@ -309,7 +309,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[0., 0.], [1., 0.], [0., 1.]],
);
Expand Down
14 changes: 7 additions & 7 deletions crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ mod tests {

let surface = services.objects.surfaces.yz_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand All @@ -187,7 +187,7 @@ mod tests {

let surface = services.objects.surfaces.yz_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand All @@ -210,7 +210,7 @@ mod tests {

let surface = services.objects.surfaces.yz_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand All @@ -230,7 +230,7 @@ mod tests {

let surface = services.objects.surfaces.yz_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand Down Expand Up @@ -261,7 +261,7 @@ mod tests {

let surface = services.objects.surfaces.yz_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand Down Expand Up @@ -290,7 +290,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand All @@ -312,7 +312,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
);
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ mod tests {
.sweep(UP, &mut services.objects);

let mut bottom = PartialFace::default();
bottom.with_exterior_polygon_from_points(surface.clone(), TRIANGLE);
bottom.update_exterior_as_polygon(surface.clone(), TRIANGLE);
let bottom = bottom
.build(&mut services.objects)
.insert(&mut services.objects)
.reverse(&mut services.objects);
let mut top = PartialFace::default();
top.with_exterior_polygon_from_points(
top.update_exterior_as_polygon(
surface.translate(UP, &mut services.objects),
TRIANGLE,
);
Expand Down Expand Up @@ -164,7 +164,7 @@ mod tests {
.sweep(DOWN, &mut services.objects);

let mut bottom = PartialFace::default();
bottom.with_exterior_polygon_from_points(
bottom.update_exterior_as_polygon(
surface.clone().translate(DOWN, &mut services.objects),
TRIANGLE,
);
Expand All @@ -173,7 +173,7 @@ mod tests {
.insert(&mut services.objects)
.reverse(&mut services.objects);
let mut top = PartialFace::default();
top.with_exterior_polygon_from_points(surface, TRIANGLE);
top.update_exterior_as_polygon(surface, TRIANGLE);
let top = top
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down
11 changes: 4 additions & 7 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(surface, [a, b, c, d]);
face.update_exterior_as_polygon(surface, [a, b, c, d]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -134,8 +134,8 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(surface.clone(), [a, b, c, d]);
face.with_interior_polygon_from_points(surface.clone(), [e, f, g, h]);
face.update_exterior_as_polygon(surface.clone(), [a, b, c, d]);
face.add_interior_polygon(surface.clone(), [e, f, g, h]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -193,10 +193,7 @@ mod tests {

let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
surface.clone(),
[a, b, c, d, e],
);
face.update_exterior_as_polygon(surface.clone(), [a, b, c, d, e]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down
35 changes: 21 additions & 14 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ use super::HalfEdgeBuilder;
/// Builder API for [`PartialCycle`]
pub trait CycleBuilder {
/// Create a cycle as a polygonal chain from the provided points
fn from_poly_chain(
fn update_as_polygon(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self;
) -> Vec<Partial<HalfEdge>>;

/// Add a new half-edge to the cycle
///
Expand All @@ -28,32 +29,38 @@ pub trait CycleBuilder {
}

impl CycleBuilder for PartialCycle {
fn from_poly_chain(
fn update_as_polygon(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
) -> Vec<Partial<HalfEdge>> {
let surface = surface.into();

let mut cycle = PartialCycle::default();
let mut half_edges = Vec::new();

for point in points.into_iter().map(Into::into) {
let mut half_edge = cycle.add_half_edge();
let mut half_edge = half_edge.write();
let mut half_edge = self.add_half_edge();

half_edge.curve().write().surface = surface.clone();
{
let mut half_edge = half_edge.write();

let mut back = half_edge.back_mut().write();
let mut back_surface = back.surface_form.write();
half_edge.curve().write().surface = surface.clone();

back_surface.position = Some(point);
back_surface.surface = surface.clone();
let mut back = half_edge.back_mut().write();
let mut back_surface = back.surface_form.write();

back_surface.position = Some(point);
back_surface.surface = surface.clone();
}

half_edges.push(half_edge);
}

for half_edge in &mut cycle.half_edges {
for half_edge in &mut self.half_edges {
half_edge.write().update_as_line_segment();
}

cycle
half_edges
}

fn add_half_edge(&mut self) -> Partial<HalfEdge> {
Expand Down
26 changes: 16 additions & 10 deletions crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fj_math::Point;

use crate::{
objects::Surface,
objects::{HalfEdge, Surface},
partial::{Partial, PartialCycle, PartialFace},
};

Expand All @@ -10,36 +10,42 @@ use super::CycleBuilder;
/// Builder API for [`PartialFace`]
pub trait FaceBuilder {
/// Update the [`PartialFace`] with an exterior polygon
fn with_exterior_polygon_from_points(
fn update_exterior_as_polygon(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
);
) -> Vec<Partial<HalfEdge>>;

/// Update the [`PartialFace`] with an interior polygon
fn with_interior_polygon_from_points(
fn add_interior_polygon(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
);
}

impl FaceBuilder for PartialFace {
fn with_exterior_polygon_from_points(
fn update_exterior_as_polygon(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) {
let cycle = PartialCycle::from_poly_chain(surface, points);
) -> Vec<Partial<HalfEdge>> {
let mut cycle = PartialCycle::default();
let half_edges = cycle.update_as_polygon(surface, points);

self.exterior = Partial::from_partial(cycle);

half_edges
}

fn with_interior_polygon_from_points(
fn add_interior_polygon(
&mut self,
surface: impl Into<Partial<Surface>>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) {
let cycle = PartialCycle::from_poly_chain(surface, points);
self.interiors = vec![Partial::from_partial(cycle)];
let mut cycle = PartialCycle::default();
cycle.update_as_polygon(surface, points);

self.interiors.push(Partial::from_partial(cycle));
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ShellBuilder for PartialShell {
objects.surfaces.xy_plane().translate([Z, Z, -h], objects);

let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(
face.update_exterior_as_polygon(
surface,
[[-h, -h], [h, -h], [h, h], [-h, h]],
);
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl SketchBuilder for PartialSketch {
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) {
let mut face = PartialFace::default();
face.with_exterior_polygon_from_points(surface, points);
face.update_exterior_as_polygon(surface, points);

self.faces.extend([Partial::from_partial(face)]);
}
Expand Down
Loading

0 comments on commit e7dc9b2

Please sign in to comment.