Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up API of object types #881

Merged
merged 8 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn sweep(
}
}

Solid::from_faces(target)
Solid::new().with_faces(target)
}

fn create_bottom_faces(
Expand Down Expand Up @@ -300,7 +300,7 @@ mod tests {
[1., 0.],
[0., 1.],
]);
let sketch = Sketch::from_faces([face]);
let sketch = Sketch::new().with_faces([face]);

let solid =
super::sweep(sketch, direction, tolerance, Color([255, 0, 0, 255]));
Expand Down
14 changes: 4 additions & 10 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,15 @@ impl TransformObject for GlobalVertex {

impl TransformObject for Sketch {
fn transform(self, transform: &Transform) -> Self {
let faces = self
.into_faces()
.into_iter()
.map(|face| face.transform(transform));
Self::from_faces(faces)
let faces = self.into_faces().map(|face| face.transform(transform));
Self::new().with_faces(faces)
}
}

impl TransformObject for Solid {
fn transform(self, transform: &Transform) -> Self {
let faces = self
.into_faces()
.into_iter()
.map(|face| face.transform(transform));
Self::from_faces(faces)
let faces = self.into_faces().map(|face| face.transform(transform));
Self::new().with_faces(faces)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ impl SolidBuilder {
let faces =
planes.map(|plane| Face::build(plane).polygon_from_points(points));

Solid::from_faces(faces)
Solid::new().with_faces(faces)
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ mod tests {
[1., 0.],
[0., 1.],
]);
let object = Sketch::from_faces([face]);
let object = Sketch::new().with_faces([face]);

assert_eq!(3, object.curve_iter().count());
assert_eq!(1, object.cycle_iter().count());
Expand Down
10 changes: 5 additions & 5 deletions crates/fj-kernel/src/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub struct Cycle {
}

impl Cycle {
/// Build a cycle using [`CycleBuilder`]
pub fn build(surface: Surface) -> CycleBuilder {
CycleBuilder::new(surface)
}

/// Create a new cycle
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Expand All @@ -33,11 +38,6 @@ impl Cycle {
self
}

/// Build a cycle using [`CycleBuilder`]
pub fn build(surface: Surface) -> CycleBuilder {
CycleBuilder::new(surface)
}

/// Access edges that make up the cycle
pub fn edges(&self) -> impl Iterator<Item = &Edge> + '_ {
self.edges.iter()
Expand Down
10 changes: 5 additions & 5 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ pub struct Edge {
}

impl Edge {
/// Create a new instance
pub fn new(curve: Local<Curve<2>>, vertices: VerticesOfEdge) -> Self {
Self { curve, vertices }
}

/// Build an edge using [`EdgeBuilder`]
pub fn build() -> EdgeBuilder {
EdgeBuilder
}

/// Create a new instance
pub fn new(curve: Local<Curve<2>>, vertices: VerticesOfEdge) -> Self {
Self { curve, vertices }
}

/// Access the curve that defines the edge's geometry
///
/// The edge can be a segment of the curve that is bounded by two vertices,
Expand Down
12 changes: 6 additions & 6 deletions crates/fj-kernel/src/objects/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub struct Face {
}

impl Face {
/// Build a face using [`FaceBuilder`]
pub fn build(surface: Surface) -> FaceBuilder {
FaceBuilder::new(surface)
}

/// Construct a new instance of `Face`
///
/// Creates the face with no exteriors, no interiors and the default color.
Expand All @@ -27,7 +32,7 @@ impl Face {
}
}

/// Contact an instance that uses triangle representation
/// Construct an instance that uses triangle representation
pub fn from_triangles(triangles: TriRep) -> Self {
Self {
representation: Representation::TriRep(triangles),
Expand Down Expand Up @@ -70,11 +75,6 @@ impl Face {
self
}

/// Build a face using [`FaceBuilder`]
pub fn build(surface: Surface) -> FaceBuilder {
FaceBuilder::new(surface)
}

/// Access this face's surface
pub fn surface(&self) -> &Surface {
&self.brep().surface
Expand Down
29 changes: 23 additions & 6 deletions crates/fj-kernel/src/objects/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ pub struct Sketch {
}

impl Sketch {
/// Construct a sketch from faces
pub fn from_faces(
/// Construct an empty instance of `Sketch`
pub fn new() -> Self {
Self {
faces: BTreeSet::new(),
}
}

/// Add faces to the sketch
///
/// Consumes the sketch and returns the updated instance.
pub fn with_faces(
mut self,
faces: impl IntoIterator<Item = impl Into<Face>>,
) -> Self {
let faces = faces.into_iter().map(Into::into).collect();
Self { faces }
let faces = faces.into_iter().map(Into::into);
self.faces.extend(faces);
self
}

/// Access the sketch's faces
Expand All @@ -28,7 +39,13 @@ impl Sketch {
}

/// Convert the sketch into a list of faces
pub fn into_faces(self) -> BTreeSet<Face> {
self.faces
pub fn into_faces(self) -> impl Iterator<Item = Face> {
self.faces.into_iter()
}
}

impl Default for Sketch {
fn default() -> Self {
Self::new()
}
}
37 changes: 27 additions & 10 deletions crates/fj-kernel/src/objects/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,43 @@ pub struct Solid {
}

impl Solid {
/// Construct a solid from faces
pub fn from_faces(
faces: impl IntoIterator<Item = impl Into<Face>>,
) -> Self {
let faces = faces.into_iter().map(Into::into).collect();
Self { faces }
}

/// Build a solid using [`SolidBuilder`]
pub fn build() -> SolidBuilder {
SolidBuilder
}

/// Construct an empty instance of `Solid`
pub fn new() -> Self {
Self {
faces: BTreeSet::new(),
}
}

/// Add faces to the solid
///
/// Consumes the solid and returns the updated instance.
pub fn with_faces(
mut self,
faces: impl IntoIterator<Item = impl Into<Face>>,
) -> Self {
let faces = faces.into_iter().map(Into::into);
self.faces.extend(faces);
self
}

/// Access the solid's faces
pub fn faces(&self) -> impl Iterator<Item = &Face> {
self.faces.iter()
}

/// Convert the solid into a list of faces
pub fn into_faces(self) -> BTreeSet<Face> {
self.faces
pub fn into_faces(self) -> impl Iterator<Item = Face> {
self.faces.into_iter()
}
}

impl Default for Solid {
fn default() -> Self {
Self::new()
}
}
2 changes: 1 addition & 1 deletion crates/fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Shape for fj::Difference2d {
);
}

let difference = Sketch::from_faces(faces);
let difference = Sketch::new().with_faces(faces);
validate(difference, config)
}

Expand Down
2 changes: 0 additions & 2 deletions crates/fj-operations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ impl Shape for fj::Shape {
.compute_brep(config, tolerance, debug_info)?
.into_inner()
.into_faces()
.into_iter()
.collect(),
config,
),
Expand All @@ -79,7 +78,6 @@ impl Shape for fj::Shape {
.compute_brep(config, tolerance, debug_info)?
.into_inner()
.into_faces()
.into_iter()
.collect(),
config,
),
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Shape for fj::Sketch {
}
};

let sketch = Sketch::from_faces([face]);
let sketch = Sketch::new().with_faces([face]);
validate(sketch, config)
}

Expand Down