Skip to content

Commit

Permalink
Merge pull request #2033 from hannobraun/update
Browse files Browse the repository at this point in the history
Round out update API
  • Loading branch information
hannobraun authored Sep 27, 2023
2 parents 8267c76 + 61a6520 commit 6c3e64a
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 13 deletions.
20 changes: 10 additions & 10 deletions crates/fj-core/src/operations/update/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait UpdateCycle {
#[must_use]
fn add_edges(&self, edges: impl IntoIterator<Item = Handle<Edge>>) -> Self;

/// Update the provided edge
/// Update an edge of the cycle
///
/// # Panics
///
Expand All @@ -19,24 +19,24 @@ pub trait UpdateCycle {
#[must_use]
fn update_edge(
&self,
edge: &Handle<Edge>,
handle: &Handle<Edge>,
update: impl FnOnce(&Handle<Edge>) -> Handle<Edge>,
) -> Self;

/// Replace the provided edge
/// Replace an edge of the cycle
///
/// This is a more general version of [`UpdateCycle::update_edge`] which can
/// replace a single edge with multiple others.
///
/// # Panics
///
/// Uses [`Handles::update`] internally, and panics for the same reasons.
/// Uses [`Handles::replace`] internally, and panics for the same reasons.
///
/// [`Handles::update`]: crate::objects::Handles::update
/// [`Handles::replace`]: crate::objects::Handles::replace
#[must_use]
fn replace_edge<const N: usize>(
&self,
edge: &Handle<Edge>,
handle: &Handle<Edge>,
replace: impl FnOnce(&Handle<Edge>) -> [Handle<Edge>; N],
) -> Self;
}
Expand All @@ -49,19 +49,19 @@ impl UpdateCycle for Cycle {

fn update_edge(
&self,
edge: &Handle<Edge>,
handle: &Handle<Edge>,
update: impl FnOnce(&Handle<Edge>) -> Handle<Edge>,
) -> Self {
let edges = self.edges().update(edge, update);
let edges = self.edges().update(handle, update);
Cycle::new(edges)
}

fn replace_edge<const N: usize>(
&self,
edge: &Handle<Edge>,
handle: &Handle<Edge>,
replace: impl FnOnce(&Handle<Edge>) -> [Handle<Edge>; N],
) -> Self {
let edges = self.edges().replace(edge, replace);
let edges = self.edges().replace(handle, replace);
Cycle::new(edges)
}
}
49 changes: 49 additions & 0 deletions crates/fj-core/src/operations/update/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ pub trait UpdateRegion {
&self,
interiors: impl IntoIterator<Item = Handle<Cycle>>,
) -> Self;

/// Update an interior cycle of the region
///
/// # Panics
///
/// Uses [`Handles::update`] internally, and panics for the same reasons.
///
/// [`Handles::update`]: crate::objects::Handles::update
#[must_use]
fn update_interior(
&self,
handle: &Handle<Cycle>,
update: impl FnOnce(&Handle<Cycle>) -> Handle<Cycle>,
) -> Self;

/// Replace an interior cycle of the region
///
/// This is a more general version of [`UpdateRegion::update_interior`]
/// which can replace a single cycle with multiple others.
///
/// # Panics
///
/// Uses [`Handles::replace`] internally, and panics for the same reasons.
///
/// [`Handles::replace`]: crate::objects::Handles::replace
#[must_use]
fn replace_interior<const N: usize>(
&self,
handle: &Handle<Cycle>,
replace: impl FnOnce(&Handle<Cycle>) -> [Handle<Cycle>; N],
) -> Self;
}

impl UpdateRegion for Region {
Expand All @@ -36,4 +67,22 @@ impl UpdateRegion for Region {
let interiors = self.interiors().iter().cloned().chain(interiors);
Region::new(self.exterior().clone(), interiors, self.color())
}

fn update_interior(
&self,
handle: &Handle<Cycle>,
update: impl FnOnce(&Handle<Cycle>) -> Handle<Cycle>,
) -> Self {
let interiors = self.interiors().update(handle, update);
Region::new(self.exterior().clone(), interiors, self.color())
}

fn replace_interior<const N: usize>(
&self,
handle: &Handle<Cycle>,
replace: impl FnOnce(&Handle<Cycle>) -> [Handle<Cycle>; N],
) -> Self {
let interiors = self.interiors().replace(handle, replace);
Region::new(self.exterior().clone(), interiors, self.color())
}
}
32 changes: 29 additions & 3 deletions crates/fj-core/src/operations/update/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,27 @@ pub trait UpdateShell {
#[must_use]
fn update_face(
&self,
face: &Handle<Face>,
handle: &Handle<Face>,
update: impl FnOnce(&Handle<Face>) -> Handle<Face>,
) -> Self;

/// Replace a face of the shell
///
/// This is a more general version of [`UpdateShell::update_face`] which can
/// replace a single face with multiple others.
///
/// # Panics
///
/// Uses [`Handles::replace`] internally, and panics for the same reasons.
///
/// [`Handles::replace`]: crate::objects::Handles::replace
#[must_use]
fn replace_face<const N: usize>(
&self,
handle: &Handle<Face>,
replace: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
) -> Self;

/// Remove a face from the shell
#[must_use]
fn remove_face(&self, handle: &Handle<Face>) -> Self;
Expand All @@ -36,10 +53,19 @@ impl UpdateShell for Shell {

fn update_face(
&self,
face: &Handle<Face>,
handle: &Handle<Face>,
update: impl FnOnce(&Handle<Face>) -> Handle<Face>,
) -> Self {
let faces = self.faces().update(face, update);
let faces = self.faces().update(handle, update);
Shell::new(faces)
}

fn replace_face<const N: usize>(
&self,
handle: &Handle<Face>,
replace: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
) -> Self {
let faces = self.faces().replace(handle, replace);
Shell::new(faces)
}

Expand Down
49 changes: 49 additions & 0 deletions crates/fj-core/src/operations/update/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,59 @@ pub trait UpdateSketch {
/// Add a region to the sketch
#[must_use]
fn add_region(&self, region: Handle<Region>) -> Self;

/// Update a region of the sketch
///
/// # Panics
///
/// Uses [`Handles::update`] internally, and panics for the same reasons.
///
/// [`Handles::update`]: crate::objects::Handles::update
#[must_use]
fn update_region(
&self,
handle: &Handle<Region>,
update: impl FnOnce(&Handle<Region>) -> Handle<Region>,
) -> Self;

/// Replace a region of the sketch
///
/// This is a more general version of [`UpdateSketch::update_region`] which
/// can replace a single edge with multiple others.
///
/// # Panics
///
/// Uses [`Handles::replace`] internally, and panics for the same reasons.
///
/// [`Handles::replace`]: crate::objects::Handles::replace
#[must_use]
fn replace_region<const N: usize>(
&self,
handle: &Handle<Region>,
replace: impl FnOnce(&Handle<Region>) -> [Handle<Region>; N],
) -> Self;
}

impl UpdateSketch for Sketch {
fn add_region(&self, region: Handle<Region>) -> Self {
Sketch::new(self.regions().iter().cloned().chain([region]))
}

fn update_region(
&self,
handle: &Handle<Region>,
update: impl FnOnce(&Handle<Region>) -> Handle<Region>,
) -> Self {
let regions = self.regions().update(handle, update);
Sketch::new(regions)
}

fn replace_region<const N: usize>(
&self,
handle: &Handle<Region>,
replace: impl FnOnce(&Handle<Region>) -> [Handle<Region>; N],
) -> Self {
let regions = self.regions().replace(handle, replace);
Sketch::new(regions)
}
}
49 changes: 49 additions & 0 deletions crates/fj-core/src/operations/update/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ pub trait UpdateSolid {
&self,
shells: impl IntoIterator<Item = Handle<Shell>>,
) -> Self;

/// Update a shell of the solid
///
/// # Panics
///
/// Uses [`Handles::update`] internally, and panics for the same reasons.
///
/// [`Handles::update`]: crate::objects::Handles::update
#[must_use]
fn update_shell(
&self,
handle: &Handle<Shell>,
update: impl FnOnce(&Handle<Shell>) -> Handle<Shell>,
) -> Self;

/// Replace a shell of the solid
///
/// This is a more general version of [`UpdateSolid::update_shell`] which
/// can replace a single edge with multiple others.
///
/// # Panics
///
/// Uses [`Handles::replace`] internally, and panics for the same reasons.
///
/// [`Handles::replace`]: crate::objects::Handles::replace
#[must_use]
fn replace_shell<const N: usize>(
&self,
handle: &Handle<Shell>,
replace: impl FnOnce(&Handle<Shell>) -> [Handle<Shell>; N],
) -> Self;
}

impl UpdateSolid for Solid {
Expand All @@ -21,4 +52,22 @@ impl UpdateSolid for Solid {
let shells = self.shells().iter().cloned().chain(shells);
Solid::new(shells)
}

fn update_shell(
&self,
handle: &Handle<Shell>,
update: impl FnOnce(&Handle<Shell>) -> Handle<Shell>,
) -> Self {
let shells = self.shells().update(handle, update);
Solid::new(shells)
}

fn replace_shell<const N: usize>(
&self,
handle: &Handle<Shell>,
replace: impl FnOnce(&Handle<Shell>) -> [Handle<Shell>; N],
) -> Self {
let shells = self.shells().replace(handle, replace);
Solid::new(shells)
}
}

0 comments on commit 6c3e64a

Please sign in to comment.