Skip to content

Commit

Permalink
Merge pull request #593 from hannobraun/face
Browse files Browse the repository at this point in the history
Make some cleanups in `Face`
  • Loading branch information
hannobraun authored May 16, 2022
2 parents e768135 + 8c8060f commit 4d1a69d
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 79 deletions.
36 changes: 11 additions & 25 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,15 @@ pub fn sweep_shape(
let interiors_top = source_to_top.interiors_for_face(&face_source);

target
.insert(Face::Face {
surface: surface_bottom,
exteriors: exteriors_bottom,
interiors: interiors_bottom,
.insert(Face::new(
surface_bottom,
exteriors_bottom,
interiors_bottom,
color,
})
))
.unwrap();
target
.insert(Face::Face {
surface: surface_top,
exteriors: exteriors_top,
interiors: interiors_top,
color,
})
.insert(Face::new(surface_top, exteriors_top, interiors_top, color))
.unwrap();
}

Expand Down Expand Up @@ -213,12 +208,7 @@ pub fn sweep_shape(
.unwrap();

target
.insert(Face::Face {
surface,
exteriors: vec![cycle],
interiors: Vec::new(),
color,
})
.insert(Face::new(surface, vec![cycle], Vec::new(), color))
.unwrap();
}
}
Expand Down Expand Up @@ -276,7 +266,7 @@ impl Relation {
};

exteriors
.iter()
.as_handle()
.map(|cycle| self.cycles.get(cycle).unwrap().clone())
.collect()
}
Expand All @@ -292,7 +282,7 @@ impl Relation {
};

interiors
.iter()
.as_handle()
.map(|cycle| self.cycles.get(cycle).unwrap().clone())
.collect()
}
Expand Down Expand Up @@ -385,12 +375,8 @@ mod tests {
let surface = if reverse { surface.reverse() } else { surface };
let surface = shape.insert(surface)?;

let abc = Face::Face {
surface,
exteriors: vec![cycles],
interiors: Vec::new(),
color: [255, 0, 0, 255],
};
let abc =
Face::new(surface, vec![cycles], Vec::new(), [255, 0, 0, 255]);

let face = shape.insert(abc)?;

Expand Down
29 changes: 12 additions & 17 deletions crates/fj-kernel/src/shape/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,7 @@ mod tests {
let cycle = shape.insert(cycle)?;
assert!(shape.get_handle(&cycle.get()).as_ref() == Some(&cycle));

let face = Face::Face {
surface,
exteriors: Vec::new(),
interiors: Vec::new(),
color: [0, 0, 0, 0],
};
let face = Face::new(surface, Vec::new(), Vec::new(), [0, 0, 0, 0]);
assert!(shape.get_handle(&face).is_none());

let face = shape.insert(face)?;
Expand Down Expand Up @@ -341,12 +336,12 @@ mod tests {

// Nothing has been added to `shape`. Should fail.
let err = shape
.insert(Face::Face {
surface: surface.clone(),
exteriors: vec![cycle.clone()],
interiors: Vec::new(),
color: [255, 0, 0, 255],
})
.insert(Face::new(
surface.clone(),
vec![cycle.clone()],
Vec::new(),
[255, 0, 0, 255],
))
.unwrap_err();
assert!(err.missing_surface(&surface));
assert!(err.missing_cycle(&cycle));
Expand All @@ -355,12 +350,12 @@ mod tests {
let cycle = shape.add_cycle()?;

// Everything has been added to `shape` now. Should work!
shape.insert(Face::Face {
shape.insert(Face::new(
surface,
exteriors: vec![cycle],
interiors: Vec::new(),
color: [255, 0, 0, 255],
})?;
vec![cycle],
Vec::new(),
[255, 0, 0, 255],
))?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/shape/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Validate for Face {
if !stores.surfaces.contains(surface) {
missing_surface = Some(surface.clone());
}
for cycle in exteriors.iter().chain(interiors) {
for cycle in exteriors.as_handle().chain(interiors.as_handle()) {
if !stores.cycles.contains(cycle) {
missing_cycles.insert(cycle.clone());
}
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/topology/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ impl<'r> FaceBuilder<'r> {
interiors.push(cycle);
}

self.shape.insert(Face::Face {
self.shape.insert(Face::new(
surface,
exteriors,
interiors,
color: [255, 0, 0, 255],
})
[255, 0, 0, 255],
))
}
}
50 changes: 42 additions & 8 deletions crates/fj-kernel/src/topology/faces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum Face {
///
/// It might be less error-prone to specify the cycles in surface
/// coordinates.
exteriors: Vec<Handle<Cycle<3>>>,
exteriors: CyclesInFace,

/// The cycles that bound the face on the inside
///
Expand All @@ -50,7 +50,7 @@ pub enum Face {
/// # Implementation note
///
/// See note on `exterior` field.
interiors: Vec<Handle<Cycle<3>>>,
interiors: CyclesInFace,

/// The color of the face
color: [u8; 4],
Expand All @@ -66,6 +66,23 @@ pub enum Face {
}

impl Face {
/// Construct a new instance of `Face`
pub fn new(
surface: Handle<Surface>,
exteriors: impl IntoIterator<Item = Handle<Cycle<3>>>,
interiors: impl IntoIterator<Item = Handle<Cycle<3>>>,
color: [u8; 4],
) -> Self {
let exteriors = CyclesInFace::from_canonical(exteriors);
let interiors = CyclesInFace::from_canonical(interiors);

Self::Face {
surface,
exteriors,
interiors,
color,
}
}
/// Build a face using the [`FaceBuilder`] API
pub fn builder(surface: Surface, shape: &mut Shape) -> FaceBuilder {
FaceBuilder::new(surface, shape)
Expand All @@ -92,9 +109,7 @@ impl Face {
/// [`Handle`]s.
pub fn exteriors(&self) -> impl Iterator<Item = Cycle<3>> + '_ {
match self {
Self::Face { exteriors, .. } => {
exteriors.iter().map(|handle| handle.get())
}
Self::Face { exteriors, .. } => exteriors.as_canonical(),
_ => {
// No code that still uses triangle representation is calling
// this method.
Expand All @@ -109,9 +124,7 @@ impl Face {
/// [`Handle`]s.
pub fn interiors(&self) -> impl Iterator<Item = Cycle<3>> + '_ {
match self {
Self::Face { interiors, .. } => {
interiors.iter().map(|handle| handle.get())
}
Self::Face { interiors, .. } => interiors.as_canonical(),
_ => {
// No code that still uses triangle representation is calling
// this method.
Expand Down Expand Up @@ -145,3 +158,24 @@ impl Hash for Face {
}
}
}

/// A list of cycles, as they are stored in `Face`
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct CyclesInFace(Vec<Handle<Cycle<3>>>);

impl CyclesInFace {
fn from_canonical(
cycles: impl IntoIterator<Item = Handle<Cycle<3>>>,
) -> Self {
Self(cycles.into_iter().collect())
}

fn as_canonical(&self) -> impl Iterator<Item = Cycle<3>> + '_ {
self.as_handle().map(|edge| edge.get())
}

/// Access an iterator over handles to the cycles
pub fn as_handle(&self) -> impl Iterator<Item = &'_ Handle<Cycle<3>>> + '_ {
self.0.iter()
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/topology/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ pub use self::{
builder::{CycleBuilder, EdgeBuilder, FaceBuilder, VertexBuilder},
cycles::Cycle,
edges::Edge,
faces::Face,
faces::{CyclesInFace, Face},
vertices::Vertex,
};
9 changes: 2 additions & 7 deletions crates/fj-operations/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ impl ToShape for fj::Circle {
.unwrap();
shape.insert(Cycle::new(vec![edge])).unwrap();

let cycles = shape.cycles().collect();
let cycles = shape.cycles();
let surface = shape.insert(Surface::xy_plane()).unwrap();
shape
.insert(Face::Face {
exteriors: cycles,
interiors: Vec::new(),
surface,
color: self.color(),
})
.insert(Face::new(surface, cycles, Vec::new(), self.color()))
.unwrap();

shape
Expand Down
7 changes: 1 addition & 6 deletions crates/fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ impl ToShape for fj::Difference2d {
let surface = shape.insert(face_a.surface()).unwrap();

shape
.insert(Face::Face {
surface,
exteriors,
interiors,
color: self.color(),
})
.insert(Face::new(surface, exteriors, interiors, self.color()))
.unwrap();

shape
Expand Down
20 changes: 9 additions & 11 deletions crates/fj-operations/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,16 @@ fn copy_shape(orig: Shape, target: &mut Shape) {
color,
} => {
target
.insert(Face::Face {
surface: surfaces[&surface].clone(),
exteriors: exteriors
.iter()
.map(|cycle| cycles[cycle].clone())
.collect(),
interiors: interiors
.iter()
.map(|cycle| cycles[cycle].clone())
.collect(),
.insert(Face::new(
surfaces[&surface].clone(),
exteriors
.as_handle()
.map(|cycle| cycles[cycle].clone()),
interiors
.as_handle()
.map(|cycle| cycles[cycle].clone()),
color,
})
))
.unwrap();
}
face @ Face::Triangles(_) => {
Expand Down

0 comments on commit 4d1a69d

Please sign in to comment.