Skip to content

Commit

Permalink
Merge pull request #1483 from hannobraun/builder
Browse files Browse the repository at this point in the history
Expand and clean up builder API
  • Loading branch information
hannobraun authored Jan 5, 2023
2 parents 4e1ebf1 + 2e35790 commit c471900
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 69 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 @@ -188,8 +188,8 @@ mod tests {
let face = {
let mut face = PartialFace::default();
face.exterior.write().surface = surface;
face.update_exterior_as_polygon(exterior);
face.add_interior_polygon(interior);
face.update_exterior_as_polygon_from_points(exterior);
face.add_interior_polygon_from_points(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 @@ -95,7 +95,7 @@ mod tests {
.map(|surface| {
let mut face = PartialFace::default();
face.exterior.write().surface = Partial::from(surface);
face.update_exterior_as_polygon(points);
face.update_exterior_as_polygon_from_points(points);

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

face.build(&mut services.objects)
});
Expand Down
36 changes: 28 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,11 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([[0., 0.], [1., 1.], [0., 2.]]);
face.update_exterior_as_polygon_from_points([
[0., 0.],
[1., 1.],
[0., 2.],
]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand All @@ -166,7 +170,11 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([[0., 0.], [2., 1.], [0., 2.]]);
face.update_exterior_as_polygon_from_points([
[0., 0.],
[2., 1.],
[0., 2.],
]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand All @@ -186,7 +194,11 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([[4., 2.], [0., 4.], [0., 0.]]);
face.update_exterior_as_polygon_from_points([
[4., 2.],
[0., 4.],
[0., 0.],
]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand All @@ -206,7 +218,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[0., 0.],
[2., 1.],
[3., 0.],
Expand All @@ -231,7 +243,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[0., 0.],
[2., 1.],
[3., 1.],
Expand All @@ -256,7 +268,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[0., 0.],
[2., 1.],
[3., 1.],
Expand All @@ -282,7 +294,11 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([[0., 0.], [2., 0.], [0., 1.]]);
face.update_exterior_as_polygon_from_points([
[0., 0.],
[2., 0.],
[0., 1.],
]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -312,7 +328,11 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([[0., 0.], [1., 0.], [0., 1.]]);
face.update_exterior_as_polygon_from_points([
[0., 0.],
[1., 0.],
[0., 1.],
]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
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 mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -190,7 +190,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -216,7 +216,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -239,7 +239,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand Down Expand Up @@ -274,7 +274,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand Down Expand Up @@ -308,7 +308,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -333,7 +333,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([
face.update_exterior_as_polygon_from_points([
[-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 @@ -116,15 +116,15 @@ mod tests {

let mut bottom = PartialFace::default();
bottom.exterior.write().surface = Partial::from(surface.clone());
bottom.update_exterior_as_polygon(TRIANGLE);
bottom.update_exterior_as_polygon_from_points(TRIANGLE);
let bottom = bottom
.build(&mut services.objects)
.insert(&mut services.objects)
.reverse(&mut services.objects);
let mut top = PartialFace::default();
top.exterior.write().surface =
Partial::from(surface.translate(UP, &mut services.objects));
top.update_exterior_as_polygon(TRIANGLE);
top.update_exterior_as_polygon_from_points(TRIANGLE);
let top = top
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -169,14 +169,14 @@ mod tests {
bottom.exterior.write().surface = Partial::from(
surface.clone().translate(DOWN, &mut services.objects),
);
bottom.update_exterior_as_polygon(TRIANGLE);
bottom.update_exterior_as_polygon_from_points(TRIANGLE);
let bottom = bottom
.build(&mut services.objects)
.insert(&mut services.objects)
.reverse(&mut services.objects);
let mut top = PartialFace::default();
top.exterior.write().surface = Partial::from(surface);
top.update_exterior_as_polygon(TRIANGLE);
top.update_exterior_as_polygon_from_points(TRIANGLE);
let top = top
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon([a, b, c, d]);
face.update_exterior_as_polygon_from_points([a, b, c, d]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -136,8 +136,8 @@ mod tests {
let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.exterior.write().surface = Partial::from(surface.clone());
face.update_exterior_as_polygon([a, b, c, d]);
face.add_interior_polygon([e, f, g, h]);
face.update_exterior_as_polygon_from_points([a, b, c, d]);
face.add_interior_polygon_from_points([e, f, g, h]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -196,7 +196,7 @@ mod tests {
let surface = services.objects.surfaces.xy_plane();
let mut face = PartialFace::default();
face.exterior.write().surface = Partial::from(surface.clone());
face.update_exterior_as_polygon([a, b, c, d, e]);
face.update_exterior_as_polygon_from_points([a, b, c, d, e]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down
94 changes: 65 additions & 29 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fj_math::Point;

use crate::{
builder::SurfaceBuilder,
objects::HalfEdge,
partial::{Partial, PartialCycle},
};
Expand All @@ -9,17 +10,6 @@ use super::HalfEdgeBuilder;

/// Builder API for [`PartialCycle`]
pub trait CycleBuilder {
/// Create a cycle as a polygonal chain from the provided points
fn update_as_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>>;

/// Update cycle to be a polygon
///
/// Will update each half-edge in the cycle to be a line segment.
fn update_as_polygon(&mut self);

/// Add a new half-edge to the cycle
///
/// Creates a half-edge and adds it to the cycle. The new half-edge is
Expand All @@ -43,31 +33,35 @@ pub trait CycleBuilder {
&mut self,
point: impl Into<Point<2>>,
) -> Partial<HalfEdge>;
}

impl CycleBuilder for PartialCycle {
/// Update cycle as a polygon from the provided points
fn update_as_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>> {
let mut half_edges = Vec::new();

for point in points {
let half_edge = self.add_half_edge_from_point_to_start(point);
half_edges.push(half_edge);
}

self.update_as_polygon();
) -> Vec<Partial<HalfEdge>>;

half_edges
}
/// Update cycle as a polygon
///
/// Will update each half-edge in the cycle to be a line segment.
fn update_as_polygon(&mut self);

fn update_as_polygon(&mut self) {
for half_edge in &mut self.half_edges {
half_edge.write().update_as_line_segment();
}
}
/// Update cycle as a triangle, from global (3D) points
///
/// Uses the three points to infer a plane that is used as the surface.
///
/// # Implementation Note
///
/// This method is probably just temporary, and will be generalized into a
/// "update as polygon from global points" method sooner or later. For now,
/// I didn't want to deal with the question of how to infer the surface, and
/// how to handle points that don't fit that surface.
fn update_as_triangle_from_global_points(
&mut self,
points: [impl Into<Point<3>>; 3],
) -> [Partial<HalfEdge>; 3];
}

impl CycleBuilder for PartialCycle {
fn add_half_edge(&mut self) -> Partial<HalfEdge> {
let mut new_half_edge = Partial::<HalfEdge>::new();

Expand Down Expand Up @@ -129,4 +123,46 @@ impl CycleBuilder for PartialCycle {

half_edge
}

fn update_as_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>> {
let mut half_edges = Vec::new();

for point in points {
let half_edge = self.add_half_edge_from_point_to_start(point);
half_edges.push(half_edge);
}

self.update_as_polygon();

half_edges
}

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

fn update_as_triangle_from_global_points(
&mut self,
points_global: [impl Into<Point<3>>; 3],
) -> [Partial<HalfEdge>; 3] {
let points_surface = self
.surface
.write()
.update_as_plane_from_points(points_global);
let mut edges = self.update_as_polygon_from_points(points_surface);

// None of the following should panic, as we just created a polygon from
// three points, so we should have exactly three edges.
let c = edges.pop().unwrap();
let b = edges.pop().unwrap();
let a = edges.pop().unwrap();
assert!(edges.pop().is_none());

[a, b, c]
}
}
Loading

0 comments on commit c471900

Please sign in to comment.