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 Face constructor #1125

Merged
merged 3 commits into from
Sep 21, 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: 1 addition & 3 deletions crates/fj-kernel/src/algorithms/reverse/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use super::Reverse;

impl Reverse for Face {
fn reverse(self) -> Self {
let surface = *self.surface();

let exterior = self.exterior().clone().reverse();
let interiors = self.interiors().map(|cycle| cycle.clone().reverse());

Face::new(surface, exterior)
Face::from_exterior(exterior)
.with_interiors(interiors)
.with_color(self.color())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Sweep for (HalfEdge, Color) {
Cycle::new(surface, edges)
};

Face::new(surface, cycle).with_color(color)
Face::from_exterior(cycle).with_color(color)
}
}

Expand Down Expand Up @@ -212,7 +212,7 @@ mod tests {

let cycle = Cycle::new(surface, [bottom, right, top, left]);

Face::new(surface, cycle)
Face::from_exterior(cycle)
};

assert_eq!(face, expected_face);
Expand Down
4 changes: 1 addition & 3 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ impl TransformObject for Face {
type Transformed = Self;

fn transform(self, transform: &Transform, stores: &Stores) -> Self {
let surface = self.surface().transform(transform, stores);

let exterior = self.exterior().clone().transform(transform, stores);
let interiors = self
.interiors()
.map(|cycle| cycle.clone().transform(transform, stores));

let color = self.color();

Face::new(surface, exterior)
Face::from_exterior(exterior)
.with_interiors(interiors)
.with_color(color)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ impl<'a> FaceBuilder<'a> {
let exterior = self
.exterior
.expect("Can't build `Face` without exterior cycle");
Face::new(self.surface, exterior).with_interiors(self.interiors)
Face::from_exterior(exterior).with_interiors(self.interiors)
}
}
100 changes: 50 additions & 50 deletions crates/fj-kernel/src/objects/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,6 @@ use crate::{builder::FaceBuilder, stores::Stores};

use super::{Cycle, Surface};

/// A collection of faces
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Faces {
inner: BTreeSet<Face>,
}

impl Faces {
/// Create an empty instance of `Faces`
pub fn new() -> Self {
Self::default()
}

/// Find the given face
pub fn find(&self, face: &Face) -> Option<Face> {
for f in self {
if f == face {
return Some(f.clone());
}
}

None
}
}

impl Extend<Face> for Faces {
fn extend<T: IntoIterator<Item = Face>>(&mut self, iter: T) {
self.inner.extend(iter)
}
}

impl IntoIterator for Faces {
type Item = Face;
type IntoIter = btree_set::IntoIter<Face>;

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 {
Expand All @@ -79,9 +31,9 @@ impl Face {
///
/// Creates the face with no interiors and the default color. This can be
/// overridden using the `with_` methods.
pub fn new(surface: Surface, exterior: Cycle) -> Self {
pub fn from_exterior(exterior: Cycle) -> Self {
Self {
surface,
surface: *exterior.surface(),
exterior,
interiors: Vec::new(),
color: Color::default(),
Expand Down Expand Up @@ -165,6 +117,54 @@ impl Face {
}
}

/// A collection of faces
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Faces {
inner: BTreeSet<Face>,
}

impl Faces {
/// Create an empty instance of `Faces`
pub fn new() -> Self {
Self::default()
}

/// Find the given face
pub fn find(&self, face: &Face) -> Option<Face> {
for f in self {
if f == face {
return Some(f.clone());
}
}

None
}
}

impl Extend<Face> for Faces {
fn extend<T: IntoIterator<Item = Face>>(&mut self, iter: T) {
self.inner.extend(iter)
}
}

impl IntoIterator for Faces {
type Item = Face;
type IntoIter = btree_set::IntoIter<Face>;

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()
}
}

/// The handedness of a face's coordinate system
///
/// See [`Face::coord_handedness`].
Expand Down
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 @@ -83,7 +83,7 @@ impl Shape for fj::Difference2d {
);

faces.push(
Face::new(*surface, exterior)
Face::from_exterior(exterior)
.with_interiors(interiors)
.with_color(Color(self.color())),
);
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 @@ -30,7 +30,7 @@ impl Shape for fj::Sketch {
.build_circle_from_radius(circle.radius());
let cycle = Cycle::new(surface, [half_edge]);

Face::new(surface, cycle).with_color(Color(self.color()))
Face::from_exterior(cycle).with_color(Color(self.color()))
}
fj::Chain::PolyChain(poly_chain) => {
let points =
Expand Down