Skip to content

Commit

Permalink
Merge pull request #1297 from hannobraun/objects
Browse files Browse the repository at this point in the history
Simplify `Cycle` and `Face`
  • Loading branch information
hannobraun authored Nov 2, 2022
2 parents 7354de8 + df03f2c commit d717e85
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 37 deletions.
4 changes: 1 addition & 3 deletions crates/fj-kernel/src/algorithms/reverse/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use super::Reverse;

impl Reverse for Handle<Cycle> {
fn reverse(self, objects: &Objects) -> Result<Self, ValidationError> {
let surface = self.surface().clone();

let mut edges = self
.half_edges()
.cloned()
Expand All @@ -18,6 +16,6 @@ impl Reverse for Handle<Cycle> {

edges.reverse();

Ok(objects.cycles.insert(Cycle::new(surface, edges))?)
Ok(objects.cycles.insert(Cycle::new(edges))?)
}
}
17 changes: 6 additions & 11 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ impl Sweep for (Handle<HalfEdge>, Color) {
points_curve_and_surface,
));

objects.curves.insert(Curve::new(
surface.clone(),
path,
global,
))?
objects.curves.insert(Curve::new(surface, path, global))?
};

let global = objects.global_edges.insert(GlobalEdge::new(
Expand Down Expand Up @@ -176,7 +172,7 @@ impl Sweep for (Handle<HalfEdge>, Color) {
i += 1;
}

objects.cycles.insert(Cycle::new(surface, edges))?
objects.cycles.insert(Cycle::new(edges))?
};

Ok(Face::builder(objects)
Expand Down Expand Up @@ -242,7 +238,7 @@ mod tests {
.build(&objects)?
.reverse(&objects)?;
let side_down = HalfEdge::partial()
.with_surface(Some(surface.clone()))
.with_surface(Some(surface))
.with_back_vertex(Some(Vertex::partial().with_surface_form(
Some(bottom.back().surface_form().clone()),
)))
Expand All @@ -253,10 +249,9 @@ mod tests {
.build(&objects)?
.reverse(&objects)?;

let cycle = objects.cycles.insert(Cycle::new(
surface,
[bottom, side_up, top, side_down],
))?;
let cycle = objects
.cycles
.insert(Cycle::new([bottom, side_up, top, side_down]))?;

Face::builder(&objects).with_exterior(cycle).build()
};
Expand Down
5 changes: 1 addition & 4 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ impl<'a> ShellBuilder<'a> {

Face::builder(self.objects)
.with_exterior(
self.objects
.cycles
.insert(Cycle::new(surface, edges))
.unwrap(),
self.objects.cycles.insert(Cycle::new(edges)).unwrap(),
)
.build()
};
Expand Down
26 changes: 16 additions & 10 deletions crates/fj-kernel/src/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use super::{HalfEdge, Surface};
/// A cycle of connected half-edges
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Cycle {
surface: Handle<Surface>,
half_edges: Vec<Handle<HalfEdge>>,
}

Expand All @@ -18,14 +17,18 @@ impl Cycle {
///
/// # Panics
///
/// Panics, if `half_edges` does not yield at least one half-edge.
///
/// Panic, if the end of each half-edge does not connect to the beginning of
/// the next one.
pub fn new(
surface: Handle<Surface>,
half_edges: impl IntoIterator<Item = Handle<HalfEdge>>,
) -> Self {
pub fn new(half_edges: impl IntoIterator<Item = Handle<HalfEdge>>) -> Self {
let half_edges = half_edges.into_iter().collect::<Vec<_>>();

let surface = match half_edges.first() {
Some(half_edge) => half_edge.surface().clone(),
None => panic!("Cycle must contain at least one half-edge"),
};

// Verify, that the curves of all edges are defined in the correct
// surface.
for edge in &half_edges {
Expand Down Expand Up @@ -64,15 +67,18 @@ impl Cycle {
}
}

Self {
surface,
half_edges,
}
Self { half_edges }
}

/// Access the surface that this cycle is in
pub fn surface(&self) -> &Handle<Surface> {
&self.surface
if let Some(half_edge) = self.half_edges.first() {
return half_edge.surface();
}

unreachable!(
"Cycle has no half-edges, which the constructor should prevent."
)
}

/// Access the half-edges that make up the cycle
Expand Down
7 changes: 6 additions & 1 deletion crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use crate::storage::{Handle, HandleWrapper};

use super::{Curve, GlobalCurve, GlobalVertex, Vertex};
use super::{Curve, GlobalCurve, GlobalVertex, Surface, Vertex};

/// A half-edge
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -46,6 +46,11 @@ impl HalfEdge {
front
}

/// Access the surface that the half-edge's curve is defined in
pub fn surface(&self) -> &Handle<Surface> {
self.curve().surface()
}

/// Access the global form of this half-edge
pub fn global_form(&self) -> &Handle<GlobalEdge> {
&self.global_form
Expand Down
6 changes: 2 additions & 4 deletions crates/fj-kernel/src/objects/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use super::{Cycle, Objects, Surface};
/// [`Shell`]: super::Shell
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Face {
surface: Handle<Surface>,
exterior: Handle<Cycle>,
interiors: Vec<Handle<Cycle>>,
color: Color,
Expand Down Expand Up @@ -64,7 +63,7 @@ impl Face {
the_interiors: impl IntoIterator<Item = Handle<Cycle>>,
color: Color,
) -> Self {
let surface = exterior.surface().clone();
let surface = exterior.surface();
let mut interiors = Vec::new();

for interior in the_interiors.into_iter() {
Expand All @@ -83,7 +82,6 @@ impl Face {
}

Self {
surface,
exterior,
interiors,
color,
Expand All @@ -92,7 +90,7 @@ impl Face {

/// Access this face's surface
pub fn surface(&self) -> &Handle<Surface> {
&self.surface
self.exterior().surface()
}

/// Access the cycle that bounds the face on the outside
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl PartialCycle {
half_edges
};

Ok(objects.cycles.insert(Cycle::new(surface, half_edges))?)
Ok(objects.cycles.insert(Cycle::new(half_edges))?)
}
}

Expand Down
5 changes: 2 additions & 3 deletions crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ impl Shape for fj::Sketch {
// none need to be added here.

let half_edge = HalfEdge::partial()
.with_surface(Some(surface.clone()))
.with_surface(Some(surface))
.as_circle_from_radius(circle.radius(), objects)?
.build(objects)?;
let cycle =
objects.cycles.insert(Cycle::new(surface, [half_edge]))?;
let cycle = objects.cycles.insert(Cycle::new([half_edge]))?;

Face::builder(objects)
.with_exterior(cycle)
Expand Down

0 comments on commit d717e85

Please sign in to comment.