Skip to content

Commit

Permalink
Merge pull request #1479 from hannobraun/builder
Browse files Browse the repository at this point in the history
Make some small cleanups and improvements to the builder API
  • Loading branch information
hannobraun authored Jan 4, 2023
2 parents d560659 + 3a53f8f commit ea6747a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
7 changes: 0 additions & 7 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ impl CycleBuilder for PartialCycle {
&mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Vec<Partial<HalfEdge>> {
let mut points = points.into_iter().map(Into::into);

let mut half_edges = Vec::new();

if let Some(point) = points.next() {
let half_edge = self.add_half_edge_from_point_to_start(point);
half_edges.push(half_edge);
}

for point in points {
let half_edge = self.add_half_edge_from_point_to_start(point);
half_edges.push(half_edge);
Expand Down
19 changes: 16 additions & 3 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
builder::{
FaceBuilder, HalfEdgeBuilder, SurfaceBuilder, SurfaceVertexBuilder,
},
objects::{HalfEdge, Objects},
objects::{Face, HalfEdge, Objects},
partial::{
Partial, PartialCycle, PartialFace, PartialHalfEdge, PartialShell,
PartialSurface, PartialSurfaceVertex,
Expand All @@ -19,6 +19,12 @@ use crate::{

/// Builder API for [`PartialShell`]
pub trait ShellBuilder {
/// Add a face to the shell
///
/// The face will not be connected to any other faces that the shell might
/// already have.
fn add_face(&mut self) -> Partial<Face>;

/// Create a cube from the length of its edges
fn create_cube_from_edge_length(
edge_length: impl Into<Scalar>,
Expand All @@ -27,6 +33,12 @@ pub trait ShellBuilder {
}

impl ShellBuilder for PartialShell {
fn add_face(&mut self) -> Partial<Face> {
let face = Partial::default();
self.faces.push(face.clone());
face
}

fn create_cube_from_edge_length(
edge_length: impl Into<Scalar>,
objects: &mut Service<Objects>,
Expand Down Expand Up @@ -70,8 +82,9 @@ impl ShellBuilder for PartialShell {
});
let c = a + [Z, Z, edge_length];

let (surface, _) =
PartialSurface::plane_from_points([a, b, c]);
let mut surface = PartialSurface::default();
surface.update_as_plane_from_points([a, b, c]);

Partial::from_partial(surface)
})
.collect::<Vec<_>>();
Expand Down
27 changes: 11 additions & 16 deletions crates/fj-kernel/src/builder/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ pub trait SurfaceBuilder: Sized {
fn from_axes(u: GlobalPath, v: impl Into<Vector<3>>) -> Self;

/// Construct a plane from 3 points
fn plane_from_points(
fn update_as_plane_from_points(
&mut self,
points: [impl Into<Point<3>>; 3],
) -> (Self, [Point<2>; 3]);
) -> [Point<2>; 3];
}

impl SurfaceBuilder for PartialSurface {
Expand All @@ -25,25 +26,19 @@ impl SurfaceBuilder for PartialSurface {
}
}

fn plane_from_points(
fn update_as_plane_from_points(
&mut self,
points: [impl Into<Point<3>>; 3],
) -> (Self, [Point<2>; 3]) {
) -> [Point<2>; 3] {
let [a, b, c] = points.map(Into::into);

let (u, u_coords) = GlobalPath::line_from_points([a, b]);
let v = c - a;

let coords = {
let [a, b] = u_coords.map(|point| point.t);
[[a, Scalar::ZERO], [b, Scalar::ZERO], [a, Scalar::ONE]]
.map(Point::from)
};

(
Self {
geometry: Some(SurfaceGeometry { u, v }),
},
coords,
)
self.geometry = Some(SurfaceGeometry { u, v });

let [a, b] = u_coords.map(|point| point.t);
[[a, Scalar::ZERO], [b, Scalar::ZERO], [a, Scalar::ONE]]
.map(Point::from)
}
}

0 comments on commit ea6747a

Please sign in to comment.