Skip to content

Commit

Permalink
Merge pull request #1388 from hannobraun/builder
Browse files Browse the repository at this point in the history
Simplify old builder structs
  • Loading branch information
hannobraun authored Nov 24, 2022
2 parents bfbc46b + d2f6443 commit 0b679e8
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 84 deletions.
14 changes: 7 additions & 7 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Sweep for Handle<Face> {
}
}

Ok(Shell::builder(objects).with_faces(faces).build())
Ok(Shell::builder().with_faces(faces).build(objects))
}
}

Expand Down Expand Up @@ -102,10 +102,10 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let solid = Sketch::builder(&objects)
let solid = Sketch::builder()
.with_surface(surface.clone())
.with_polygon_from_points(TRIANGLE)
.build()
.with_polygon_from_points(TRIANGLE, &objects)
.build(&objects)
.sweep(UP, &objects)?;

let bottom = Face::partial()
Expand Down Expand Up @@ -149,10 +149,10 @@ mod tests {
let objects = Objects::new();

let surface = objects.surfaces.xy_plane();
let solid = Sketch::builder(&objects)
let solid = Sketch::builder()
.with_surface(surface.clone())
.with_polygon_from_points(TRIANGLE)
.build()
.with_polygon_from_points(TRIANGLE, &objects)
.build(&objects)
.sweep(DOWN, &objects)?;

let bottom = Face::partial()
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/sweep/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ impl Sweep for Handle<Sketch> {
shells.push(shell);
}

Ok(Solid::builder(objects).with_shells(shells).build())
Ok(Solid::builder().with_shells(shells).build(objects))
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/transform/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ impl TransformObject for Handle<Shell> {
face.transform(transform, objects)
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Shell::builder(objects).with_faces(faces).build())
Ok(Shell::builder().with_faces(faces).build(objects))
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/transform/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ impl TransformObject for Handle<Sketch> {
.cloned()
.map(|face| face.transform(transform, objects))
.collect::<Result<Vec<_>, _>>()?;
Ok(Sketch::builder(objects).with_faces(faces).build())
Ok(Sketch::builder().with_faces(faces).build(objects))
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/transform/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ impl TransformObject for Handle<Solid> {
shell.transform(transform, objects)
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Solid::builder(objects).with_shells(faces).build())
Ok(Solid::builder().with_shells(faces).build(objects))
}
}
68 changes: 32 additions & 36 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ use crate::{
/// API for building a [`Shell`]
///
/// Also see [`Shell::builder`].
pub struct ShellBuilder<'a> {
/// The stores that the created objects are put in
pub objects: &'a Objects,

pub struct ShellBuilder {
/// The faces that make up the [`Shell`]
pub faces: FaceSet,
}

impl<'a> ShellBuilder<'a> {
impl ShellBuilder {
/// Build the [`Shell`] with the provided faces
pub fn with_faces(
mut self,
Expand All @@ -41,6 +38,7 @@ impl<'a> ShellBuilder<'a> {
pub fn with_cube_from_edge_length(
mut self,
edge_length: impl Into<Scalar>,
objects: &Objects,
) -> Self {
let edge_length = edge_length.into();

Expand All @@ -49,11 +47,10 @@ impl<'a> ShellBuilder<'a> {
let h = edge_length / 2.;

let bottom = {
let surface = self
.objects
let surface = objects
.surfaces
.xy_plane()
.translate([Z, Z, -h], self.objects)
.translate([Z, Z, -h], objects)
.unwrap();

Face::partial()
Expand All @@ -64,9 +61,9 @@ impl<'a> ShellBuilder<'a> {
[h, h],
[-h, h],
])
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
};

Expand All @@ -82,9 +79,9 @@ impl<'a> ShellBuilder<'a> {
let c = a + [Z, Z, edge_length];

PartialSurface::plane_from_points([a, b, c])
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
})
.collect::<Vec<_>>();
Expand All @@ -102,9 +99,9 @@ impl<'a> ShellBuilder<'a> {
surface.clone(),
[[Z, Z], [edge_length, Z]],
)
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -138,9 +135,9 @@ impl<'a> ShellBuilder<'a> {
..Default::default()
}
.update_as_line_segment()
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -190,9 +187,9 @@ impl<'a> ShellBuilder<'a> {
..Default::default()
}
.update_as_line_segment()
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
})
.collect::<Vec<_>>()
Expand Down Expand Up @@ -223,9 +220,9 @@ impl<'a> ShellBuilder<'a> {
..Default::default()
}
.update_as_line_segment()
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
})
.collect::<Vec<_>>();
Expand All @@ -238,28 +235,27 @@ impl<'a> ShellBuilder<'a> {
.map(|(((bottom, side_up), top), side_down)| {
let cycle = Cycle::partial()
.with_half_edges([bottom, side_up, top, side_down])
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap();

Face::partial()
.with_exterior(cycle)
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
});

(sides, tops)
};

let top = {
let surface = self
.objects
let surface = objects
.surfaces
.xy_plane()
.translate([Z, Z, h], self.objects)
.translate([Z, Z, h], objects)
.unwrap();

let mut top_edges = top_edges;
Expand All @@ -283,9 +279,9 @@ impl<'a> ShellBuilder<'a> {
surface: Some(surface.clone()),
global_form: vertex.global_form().clone().into(),
}
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
});

Expand Down Expand Up @@ -317,18 +313,18 @@ impl<'a> ShellBuilder<'a> {
..Default::default()
}
.update_as_line_segment()
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap(),
);
}

Face::partial()
.with_exterior(Cycle::new(edges).insert(self.objects).unwrap())
.build(self.objects)
.with_exterior(Cycle::new(edges).insert(objects).unwrap())
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()
};

Expand All @@ -340,7 +336,7 @@ impl<'a> ShellBuilder<'a> {
}

/// Build the [`Shell`]
pub fn build(self) -> Handle<Shell> {
Shell::new(self.faces).insert(self.objects).unwrap()
pub fn build(self, objects: &Objects) -> Handle<Shell> {
Shell::new(self.faces).insert(objects).unwrap()
}
}
16 changes: 7 additions & 9 deletions crates/fj-kernel/src/builder/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ use super::FaceBuilder;
/// API for building a [`Sketch`]
///
/// Also see [`Sketch::builder`].
pub struct SketchBuilder<'a> {
/// The stores that the created objects are put in
pub objects: &'a Objects,

pub struct SketchBuilder {
/// The surface that the [`Sketch`] is defined in
pub surface: Option<Handle<Surface>>,

/// The faces that make up the [`Sketch`]
pub faces: FaceSet,
}

impl<'a> SketchBuilder<'a> {
impl SketchBuilder {
/// Build the [`Sketch`] with the provided [`Surface`]
pub fn with_surface(mut self, surface: Handle<Surface>) -> Self {
self.surface = Some(surface);
Expand All @@ -43,6 +40,7 @@ impl<'a> SketchBuilder<'a> {
pub fn with_polygon_from_points(
mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
objects: &Objects,
) -> Self {
let surface = self
.surface
Expand All @@ -51,15 +49,15 @@ impl<'a> SketchBuilder<'a> {
self.faces.extend([Face::partial()
.with_surface(surface.clone())
.with_exterior_polygon_from_points(points)
.build(self.objects)
.build(objects)
.unwrap()
.insert(self.objects)
.insert(objects)
.unwrap()]);
self
}

/// Build the [`Sketch`]
pub fn build(self) -> Handle<Sketch> {
Sketch::new(self.faces).insert(self.objects).unwrap()
pub fn build(self, objects: &Objects) -> Handle<Sketch> {
Sketch::new(self.faces).insert(objects).unwrap()
}
}
18 changes: 8 additions & 10 deletions crates/fj-kernel/src/builder/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ use crate::{
/// API for building a [`Solid`]
///
/// Also see [`Solid::builder`].
pub struct SolidBuilder<'a> {
/// The stores that the created objects are put in
pub objects: &'a Objects,

pub struct SolidBuilder {
/// The shells that make up the [`Solid`]
pub shells: BTreeSet<Handle<Shell>>,
}

impl<'a> SolidBuilder<'a> {
impl SolidBuilder {
/// Build the [`Solid`] with the provided shells
pub fn with_shells(
mut self,
Expand All @@ -33,16 +30,17 @@ impl<'a> SolidBuilder<'a> {
pub fn with_cube_from_edge_length(
mut self,
edge_length: impl Into<Scalar>,
objects: &Objects,
) -> Self {
let shell = Shell::builder(self.objects)
.with_cube_from_edge_length(edge_length)
.build();
let shell = Shell::builder()
.with_cube_from_edge_length(edge_length, objects)
.build(objects);
self.shells.insert(shell);
self
}

/// Build the [`Solid`]
pub fn build(self) -> Handle<Solid> {
Solid::new(self.shells).insert(self.objects).unwrap()
pub fn build(self, objects: &Objects) -> Handle<Solid> {
Solid::new(self.shells).insert(objects).unwrap()
}
}
14 changes: 7 additions & 7 deletions crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ mod tests {
fn shell() {
let objects = Objects::new();

let object = Shell::builder(&objects)
.with_cube_from_edge_length(1.)
.build();
let object = Shell::builder()
.with_cube_from_edge_length(1., &objects)
.build(&objects);

assert_eq!(24, object.curve_iter().count());
assert_eq!(6, object.cycle_iter().count());
Expand All @@ -554,7 +554,7 @@ mod tests {
.with_exterior_polygon_from_points([[0., 0.], [1., 0.], [0., 1.]])
.build(&objects)?
.insert(&objects)?;
let object = Sketch::builder(&objects).with_faces([face]).build();
let object = Sketch::builder().with_faces([face]).build(&objects);

assert_eq!(3, object.curve_iter().count());
assert_eq!(1, object.cycle_iter().count());
Expand All @@ -575,9 +575,9 @@ mod tests {
fn solid() {
let objects = Objects::new();

let object = Solid::builder(&objects)
.with_cube_from_edge_length(1.)
.build();
let object = Solid::builder()
.with_cube_from_edge_length(1., &objects)
.build(&objects);

assert_eq!(24, object.curve_iter().count());
assert_eq!(6, object.cycle_iter().count());
Expand Down
Loading

0 comments on commit 0b679e8

Please sign in to comment.