From 7667c964b4194e89ead5711eaccfead3ff263081 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 7 Sep 2022 13:41:11 +0200 Subject: [PATCH 1/3] Add `Faces` --- crates/fj-kernel/src/objects/face.rs | 39 ++++++++++++++++++++++++++++ crates/fj-kernel/src/objects/mod.rs | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/fj-kernel/src/objects/face.rs b/crates/fj-kernel/src/objects/face.rs index 5cdbb82e9..b6ded2be2 100644 --- a/crates/fj-kernel/src/objects/face.rs +++ b/crates/fj-kernel/src/objects/face.rs @@ -1,3 +1,5 @@ +use std::collections::{btree_set, BTreeSet}; + use fj_interop::mesh::Color; use fj_math::Triangle; @@ -5,6 +7,43 @@ use crate::builder::FaceBuilder; use super::{Cycle, Surface}; +/// A collection of faces +#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub struct Faces { + inner: BTreeSet, +} + +impl Faces { + /// Create an empty instance of `Faces` + pub fn new() -> Self { + Self::default() + } +} + +impl Extend for Faces { + fn extend>(&mut self, iter: T) { + self.inner.extend(iter) + } +} + +impl IntoIterator for Faces { + type Item = Face; + type IntoIter = btree_set::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.inner.into_iter() + } +} + +impl<'a> IntoIterator for &'a Faces { + type Item = &'a Face; + type IntoIter = btree_set::Iter<'a, Face>; + + fn into_iter(self) -> Self::IntoIter { + self.inner.iter() + } +} + /// A face of a shape #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct Face { diff --git a/crates/fj-kernel/src/objects/mod.rs b/crates/fj-kernel/src/objects/mod.rs index b830b82c3..436055e12 100644 --- a/crates/fj-kernel/src/objects/mod.rs +++ b/crates/fj-kernel/src/objects/mod.rs @@ -18,7 +18,7 @@ pub use self::{ curve::{Curve, CurveKind, GlobalCurve}, cycle::Cycle, edge::{Edge, GlobalEdge, VerticesOfEdge}, - face::Face, + face::{Face, Faces}, shell::Shell, sketch::Sketch, solid::Solid, From c3641d6207b728314487caa7426fa7522897b16c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 7 Sep 2022 13:43:12 +0200 Subject: [PATCH 2/3] Use `Faces` to store `Face`s --- crates/fj-kernel/src/objects/shell.rs | 10 ++++------ crates/fj-kernel/src/objects/sketch.rs | 10 ++++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/crates/fj-kernel/src/objects/shell.rs b/crates/fj-kernel/src/objects/shell.rs index 0416999e8..aeea27335 100644 --- a/crates/fj-kernel/src/objects/shell.rs +++ b/crates/fj-kernel/src/objects/shell.rs @@ -1,8 +1,6 @@ -use std::collections::BTreeSet; - use crate::builder::ShellBuilder; -use super::Face; +use super::{face::Faces, Face}; /// A 3-dimensional closed shell /// @@ -12,7 +10,7 @@ use super::Face; /// currently validated. #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct Shell { - faces: BTreeSet, + faces: Faces, } impl Shell { @@ -24,7 +22,7 @@ impl Shell { /// Construct an empty instance of `Shell` pub fn new() -> Self { Self { - faces: BTreeSet::new(), + faces: Faces::new(), } } @@ -42,7 +40,7 @@ impl Shell { /// Access the shell's faces pub fn faces(&self) -> impl Iterator { - self.faces.iter() + (&self.faces).into_iter() } /// Convert the shell into a list of faces diff --git a/crates/fj-kernel/src/objects/sketch.rs b/crates/fj-kernel/src/objects/sketch.rs index f42a15776..0a6ea1da6 100644 --- a/crates/fj-kernel/src/objects/sketch.rs +++ b/crates/fj-kernel/src/objects/sketch.rs @@ -1,8 +1,6 @@ -use std::collections::BTreeSet; - use crate::builder::SketchBuilder; -use super::{Face, Surface}; +use super::{face::Faces, Face, Surface}; /// A 2-dimensional shape /// @@ -12,7 +10,7 @@ use super::{Face, Surface}; /// currently validated. #[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] pub struct Sketch { - faces: BTreeSet, + faces: Faces, } impl Sketch { @@ -24,7 +22,7 @@ impl Sketch { /// Construct an empty instance of `Sketch` pub fn new() -> Self { Self { - faces: BTreeSet::new(), + faces: Faces::new(), } } @@ -42,7 +40,7 @@ impl Sketch { /// Access the sketch's faces pub fn faces(&self) -> impl Iterator { - self.faces.iter() + (&self.faces).into_iter() } /// Convert the sketch into a list of faces From 508003fae1f7e25a87a86a311af193205758714e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 7 Sep 2022 13:44:04 +0200 Subject: [PATCH 3/3] Expose `Faces` to users of `Shell`/`Sketch` --- crates/fj-kernel/src/algorithms/transform.rs | 10 ++++++++-- crates/fj-kernel/src/objects/shell.rs | 8 ++++---- crates/fj-kernel/src/objects/sketch.rs | 8 ++++---- crates/fj-operations/src/lib.rs | 1 + 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/transform.rs b/crates/fj-kernel/src/algorithms/transform.rs index e0274dd4c..233b2e429 100644 --- a/crates/fj-kernel/src/algorithms/transform.rs +++ b/crates/fj-kernel/src/algorithms/transform.rs @@ -106,14 +106,20 @@ impl TransformObject for GlobalVertex { impl TransformObject for Shell { fn transform(self, transform: &Transform) -> Self { - let faces = self.into_faces().map(|face| face.transform(transform)); + let faces = self + .into_faces() + .into_iter() + .map(|face| face.transform(transform)); Self::new().with_faces(faces) } } impl TransformObject for Sketch { fn transform(self, transform: &Transform) -> Self { - let faces = self.into_faces().map(|face| face.transform(transform)); + let faces = self + .into_faces() + .into_iter() + .map(|face| face.transform(transform)); Self::new().with_faces(faces) } } diff --git a/crates/fj-kernel/src/objects/shell.rs b/crates/fj-kernel/src/objects/shell.rs index aeea27335..eff78abf5 100644 --- a/crates/fj-kernel/src/objects/shell.rs +++ b/crates/fj-kernel/src/objects/shell.rs @@ -39,13 +39,13 @@ impl Shell { } /// Access the shell's faces - pub fn faces(&self) -> impl Iterator { - (&self.faces).into_iter() + pub fn faces(&self) -> &Faces { + &self.faces } /// Convert the shell into a list of faces - pub fn into_faces(self) -> impl Iterator { - self.faces.into_iter() + pub fn into_faces(self) -> Faces { + self.faces } } diff --git a/crates/fj-kernel/src/objects/sketch.rs b/crates/fj-kernel/src/objects/sketch.rs index 0a6ea1da6..88082e34e 100644 --- a/crates/fj-kernel/src/objects/sketch.rs +++ b/crates/fj-kernel/src/objects/sketch.rs @@ -39,13 +39,13 @@ impl Sketch { } /// Access the sketch's faces - pub fn faces(&self) -> impl Iterator { - (&self.faces).into_iter() + pub fn faces(&self) -> &Faces { + &self.faces } /// Convert the sketch into a list of faces - pub fn into_faces(self) -> impl Iterator { - self.faces.into_iter() + pub fn into_faces(self) -> Faces { + self.faces } } diff --git a/crates/fj-operations/src/lib.rs b/crates/fj-operations/src/lib.rs index cb4ce5179..7fcbff25a 100644 --- a/crates/fj-operations/src/lib.rs +++ b/crates/fj-operations/src/lib.rs @@ -68,6 +68,7 @@ impl Shape for fj::Shape { .compute_brep(config, tolerance, debug_info)? .into_inner() .into_faces() + .into_iter() .collect::>() .validate_with_config(config), Self::Group(shape) => {