Skip to content

Commit

Permalink
Merge pull request #1495 from hannobraun/builder
Browse files Browse the repository at this point in the history
Simplify `FaceBuilder`
  • Loading branch information
hannobraun authored Jan 9, 2023
2 parents 1774f8e + ed00122 commit c97e5bd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 102 deletions.
10 changes: 7 additions & 3 deletions crates/fj-kernel/src/algorithms/intersect/curve_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ where
#[cfg(test)]
mod tests {
use crate::{
builder::{CurveBuilder, FaceBuilder},
builder::{CurveBuilder, CycleBuilder, FaceBuilder},
partial::{Partial, PartialCurve, PartialFace, PartialObject},
services::Services,
};
Expand Down Expand Up @@ -188,8 +188,12 @@ mod tests {
let face = {
let mut face = PartialFace::default();
face.exterior.write().surface = surface;
face.update_exterior_as_polygon_from_points(exterior);
face.add_interior_polygon_from_points(interior);
face.exterior
.write()
.update_as_polygon_from_points(exterior);
face.add_interior()
.write()
.update_as_polygon_from_points(interior);

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

use crate::{
algorithms::intersect::CurveFaceIntersection,
builder::{CurveBuilder, FaceBuilder},
builder::{CurveBuilder, CycleBuilder},
insert::Insert,
partial::{Partial, PartialCurve, PartialFace, PartialObject},
services::Services,
Expand All @@ -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_from_points(points);
face.exterior.write().update_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_from_points(points);
face.exterior.write().update_as_polygon_from_points(points);

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

use crate::{
algorithms::intersect::{face_point::FacePointIntersection, Intersect},
builder::FaceBuilder,
builder::CycleBuilder,
insert::Insert,
partial::{Partial, PartialFace, PartialObject},
services::Services,
Expand All @@ -148,7 +148,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[1., 1.],
[0., 2.],
Expand All @@ -169,7 +169,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[2., 1.],
[0., 2.],
Expand All @@ -193,7 +193,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[4., 2.],
[0., 4.],
[0., 0.],
Expand All @@ -217,7 +217,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[2., 1.],
[3., 0.],
Expand All @@ -242,7 +242,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[2., 1.],
[3., 1.],
Expand All @@ -267,7 +267,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[2., 1.],
[3., 1.],
Expand All @@ -293,7 +293,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[2., 0.],
[0., 1.],
Expand Down Expand Up @@ -327,7 +327,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[0., 0.],
[1., 0.],
[0., 1.],
Expand Down
16 changes: 8 additions & 8 deletions crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod tests {
},
transform::TransformObject,
},
builder::FaceBuilder,
builder::CycleBuilder,
insert::Insert,
partial::{Partial, PartialFace, PartialObject},
services::Services,
Expand All @@ -166,7 +166,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -189,7 +189,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -215,7 +215,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -238,7 +238,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.yz_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand Down Expand Up @@ -307,7 +307,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand All @@ -332,7 +332,7 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([
face.exterior.write().update_as_polygon_from_points([
[-1., -1.],
[1., -1.],
[1., 1.],
Expand Down
16 changes: 11 additions & 5 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod tests {

use crate::{
algorithms::{reverse::Reverse, transform::TransformObject},
builder::{FaceBuilder, HalfEdgeBuilder, SketchBuilder},
builder::{CycleBuilder, HalfEdgeBuilder, SketchBuilder},
insert::Insert,
partial::{
Partial, PartialFace, PartialHalfEdge, PartialObject, PartialSketch,
Expand Down Expand Up @@ -116,15 +116,18 @@ mod tests {

let mut bottom = PartialFace::default();
bottom.exterior.write().surface = Partial::from(surface.clone());
bottom.update_exterior_as_polygon_from_points(TRIANGLE);
bottom
.exterior
.write()
.update_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_from_points(TRIANGLE);
top.exterior.write().update_as_polygon_from_points(TRIANGLE);
let top = top
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -169,14 +172,17 @@ mod tests {
bottom.exterior.write().surface = Partial::from(
surface.clone().translate(DOWN, &mut services.objects),
);
bottom.update_exterior_as_polygon_from_points(TRIANGLE);
bottom
.exterior
.write()
.update_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_from_points(TRIANGLE);
top.exterior.write().update_as_polygon_from_points(TRIANGLE);
let top = top
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down
18 changes: 13 additions & 5 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ mod tests {

use crate::{
algorithms::approx::{Approx, Tolerance},
builder::FaceBuilder,
builder::{CycleBuilder, FaceBuilder},
insert::Insert,
objects::Face,
partial::{Partial, PartialFace, PartialObject},
Expand All @@ -99,7 +99,9 @@ mod tests {
let mut face = PartialFace::default();
face.exterior.write().surface =
Partial::from(services.objects.surfaces.xy_plane());
face.update_exterior_as_polygon_from_points([a, b, c, d]);
face.exterior
.write()
.update_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 +138,12 @@ 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_from_points([a, b, c, d]);
face.add_interior_polygon_from_points([e, f, g, h]);
face.exterior
.write()
.update_as_polygon_from_points([a, b, c, d]);
face.add_interior()
.write()
.update_as_polygon_from_points([e, f, g, h]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down Expand Up @@ -196,7 +202,9 @@ 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_from_points([a, b, c, d, e]);
face.exterior
.write()
.update_as_polygon_from_points([a, b, c, d, e]);
let face = face
.build(&mut services.objects)
.insert(&mut services.objects);
Expand Down
71 changes: 8 additions & 63 deletions crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,21 @@
use fj_math::Point;

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

use super::CycleBuilder;

/// Builder API for [`PartialFace`]
pub trait FaceBuilder {
/// Update the face exterior as a polygon from the provided points
fn update_exterior_as_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>>;

/// Update the face exterior as a polygon
fn update_exterior_as_polygon(&mut self);

/// Update the face exterior as a triangle, from 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_exterior_as_triangle_from_global_points(
&mut self,
points: [impl Into<Point<3>>; 3],
) -> [Partial<HalfEdge>; 3];

/// Add an interior polygon, from the provided points
fn add_interior_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
);
/// Add an interior cycle
fn add_interior(&mut self) -> Partial<Cycle>;
}

impl FaceBuilder for PartialFace {
fn update_exterior_as_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>> {
self.exterior.write().update_as_polygon_from_points(points)
}

fn update_exterior_as_polygon(&mut self) {
self.exterior.write().update_as_polygon()
}

fn update_exterior_as_triangle_from_global_points(
&mut self,
points_global: [impl Into<Point<3>>; 3],
) -> [Partial<HalfEdge>; 3] {
self.exterior
.write()
.update_as_triangle_from_global_points(points_global)
}

fn add_interior_polygon_from_points(
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) {
let mut cycle = PartialCycle {
fn add_interior(&mut self) -> Partial<Cycle> {
let cycle = Partial::from_partial(PartialCycle {
surface: self.exterior.read().surface.clone(),
..Default::default()
};
cycle.update_as_polygon_from_points(points);

self.interiors.push(Partial::from_partial(cycle));
});
self.interiors.push(cycle.clone());
cycle
}
}
Loading

0 comments on commit c97e5bd

Please sign in to comment.