Skip to content

Commit

Permalink
Merge pull request #2200 from hannobraun/update
Browse files Browse the repository at this point in the history
Merge redundant methods of `update` operations
  • Loading branch information
hannobraun authored Feb 9, 2024
2 parents fcdbda6 + 9d27be1 commit 93c27fd
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 268 deletions.
30 changes: 18 additions & 12 deletions crates/fj-core/src/operations/build/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ pub trait BuildShell {
.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge| {
edge.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)
[edge
.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)]
},
)
.join_to(
Expand All @@ -173,8 +174,9 @@ pub trait BuildShell {
.update_half_edge(
cycle.half_edges().nth_circular(1),
|edge| {
edge.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)
[edge
.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)]
},
)
.join_to(
Expand All @@ -186,8 +188,9 @@ pub trait BuildShell {
.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge| {
edge.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)
[edge
.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)]
},
)
.join_to(
Expand All @@ -207,22 +210,25 @@ pub trait BuildShell {
.update_half_edge(
cycle.half_edges().nth_circular(0),
|edge| {
edge.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)
[edge
.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)]
},
)
.update_half_edge(
cycle.half_edges().nth_circular(1),
|edge| {
edge.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)
[edge
.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)]
},
)
.update_half_edge(
cycle.half_edges().nth_circular(2),
|edge| {
edge.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)
[edge
.reverse_curve_coordinate_systems(core)
.insert(&mut core.services)]
},
)
.join_to(
Expand Down
77 changes: 44 additions & 33 deletions crates/fj-core/src/operations/holes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,22 @@ impl AddHole for Shell {
.collect::<Vec<_>>();

self.update_face(location.face, |face| {
face.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(entry.clone(), entry.path(), entry.boundary())],
core,
)
.insert(&mut core.services)])
.insert(&mut core.services)
})
.insert(&mut core.services)
[face
.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(
entry.clone(),
entry.path(),
entry.boundary(),
)],
core,
)
.insert(&mut core.services)])
.insert(&mut core.services)
})
.insert(&mut core.services)]
})
.add_faces(hole)
}
Expand Down Expand Up @@ -131,30 +136,36 @@ impl AddHole for Shell {
.only();

self.update_face(entry_location.face, |face| {
face.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(entry.clone(), entry.path(), entry.boundary())],
core,
)
.insert(&mut core.services)])
.insert(&mut core.services)
})
.insert(&mut core.services)
[face
.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(
entry.clone(),
entry.path(),
entry.boundary(),
)],
core,
)
.insert(&mut core.services)])
.insert(&mut core.services)
})
.insert(&mut core.services)]
})
.update_face(exit_location.face, |face| {
face.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(exit.clone(), exit.path(), exit.boundary())],
core,
)
.insert(&mut core.services)])
.insert(&mut core.services)
})
.insert(&mut core.services)
[face
.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(exit.clone(), exit.path(), exit.boundary())],
core,
)
.insert(&mut core.services)])
.insert(&mut core.services)
})
.insert(&mut core.services)]
})
.add_faces(hole)
}
Expand Down
14 changes: 8 additions & 6 deletions crates/fj-core/src/operations/join/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,26 @@ impl JoinCycle for Cycle {
.update_half_edge(
self.half_edges().nth_circular(index),
|edge| {
edge.update_curve(|_| edge_other.curve().clone())
[edge
.update_curve(|_| edge_other.curve().clone())
.update_start_vertex(|_| {
other
.half_edges()
.nth_circular(index_other + 1)
.start_vertex()
.clone()
})
.insert(&mut core.services)
.insert(&mut core.services)]
},
)
.update_half_edge(
self.half_edges().nth_circular(index + 1),
|edge| {
edge.update_start_vertex(|_| {
edge_other.start_vertex().clone()
})
.insert(&mut core.services)
[edge
.update_start_vertex(|_| {
edge_other.start_vertex().clone()
})
.insert(&mut core.services)]
},
)
},
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-core/src/operations/split/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl SplitFace for Shell {

let faces = [split_face_a, split_face_b];
let self_ = self_
.replace_face(updated_face_after_split_edges, |_| faces.clone());
.update_face(updated_face_after_split_edges, |_| faces.clone());

(self_, faces)
}
Expand Down
39 changes: 5 additions & 34 deletions crates/fj-core/src/operations/update/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,10 @@ pub trait UpdateCycle {
///
/// Panics, if the update results in a duplicate object.
#[must_use]
fn update_half_edge(
fn update_half_edge<const N: usize>(
&self,
handle: &Handle<HalfEdge>,
update: impl FnOnce(&Handle<HalfEdge>) -> Handle<HalfEdge>,
) -> Self;

/// Replace an edge of the cycle
///
/// This is a more general version of [`UpdateCycle::update_half_edge`]
/// which can replace a single edge with multiple others.
///
/// # Panics
///
/// Panics, if the object can't be found.
///
/// Panics, if the update results in a duplicate object.
#[must_use]
fn replace_half_edge<const N: usize>(
&self,
handle: &Handle<HalfEdge>,
replace: impl FnOnce(&Handle<HalfEdge>) -> [Handle<HalfEdge>; N],
update: impl FnOnce(&Handle<HalfEdge>) -> [Handle<HalfEdge>; N],
) -> Self;
}

Expand All @@ -53,26 +36,14 @@ impl UpdateCycle for Cycle {
Cycle::new(half_edges)
}

fn update_half_edge(
&self,
handle: &Handle<HalfEdge>,
update: impl FnOnce(&Handle<HalfEdge>) -> Handle<HalfEdge>,
) -> Self {
let edges = self
.half_edges()
.replace(handle, [update(handle)])
.expect("Half-edge not found");
Cycle::new(edges)
}

fn replace_half_edge<const N: usize>(
fn update_half_edge<const N: usize>(
&self,
handle: &Handle<HalfEdge>,
replace: impl FnOnce(&Handle<HalfEdge>) -> [Handle<HalfEdge>; N],
update: impl FnOnce(&Handle<HalfEdge>) -> [Handle<HalfEdge>; N],
) -> Self {
let edges = self
.half_edges()
.replace(handle, replace(handle))
.replace(handle, update(handle))
.expect("Half-edge not found");
Cycle::new(edges)
}
Expand Down
39 changes: 5 additions & 34 deletions crates/fj-core/src/operations/update/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,10 @@ pub trait UpdateRegion {
///
/// Panics, if the update results in a duplicate object.
#[must_use]
fn update_interior(
fn update_interior<const N: usize>(
&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
///
/// Panics, if the object can't be found.
///
/// Panics, if the update results in a duplicate object.
#[must_use]
fn replace_interior<const N: usize>(
&self,
handle: &Handle<Cycle>,
replace: impl FnOnce(&Handle<Cycle>) -> [Handle<Cycle>; N],
update: impl FnOnce(&Handle<Cycle>) -> [Handle<Cycle>; N],
) -> Self;
}

Expand All @@ -68,26 +51,14 @@ impl UpdateRegion for Region {
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()
.replace(handle, [update(handle)])
.expect("Cycle not found");
Region::new(self.exterior().clone(), interiors, self.color())
}

fn replace_interior<const N: usize>(
fn update_interior<const N: usize>(
&self,
handle: &Handle<Cycle>,
replace: impl FnOnce(&Handle<Cycle>) -> [Handle<Cycle>; N],
update: impl FnOnce(&Handle<Cycle>) -> [Handle<Cycle>; N],
) -> Self {
let interiors = self
.interiors()
.replace(handle, replace(handle))
.replace(handle, update(handle))
.expect("Cycle not found");
Region::new(self.exterior().clone(), interiors, self.color())
}
Expand Down
39 changes: 5 additions & 34 deletions crates/fj-core/src/operations/update/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,10 @@ pub trait UpdateShell {
///
/// Panics, if the update results in a duplicate object.
#[must_use]
fn update_face(
fn update_face<const N: usize>(
&self,
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
///
/// Panics, if the object can't be found.
///
/// Panics, if the update results in a duplicate object.
#[must_use]
fn replace_face<const N: usize>(
&self,
handle: &Handle<Face>,
replace: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
update: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
) -> Self;

/// Remove a face from the shell
Expand All @@ -51,26 +34,14 @@ impl UpdateShell for Shell {
Shell::new(faces)
}

fn update_face(
&self,
handle: &Handle<Face>,
update: impl FnOnce(&Handle<Face>) -> Handle<Face>,
) -> Self {
let faces = self
.faces()
.replace(handle, [update(handle)])
.expect("Face not found");
Shell::new(faces)
}

fn replace_face<const N: usize>(
fn update_face<const N: usize>(
&self,
handle: &Handle<Face>,
replace: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
update: impl FnOnce(&Handle<Face>) -> [Handle<Face>; N],
) -> Self {
let faces = self
.faces()
.replace(handle, replace(handle))
.replace(handle, update(handle))
.expect("Face not found");
Shell::new(faces)
}
Expand Down
Loading

0 comments on commit 93c27fd

Please sign in to comment.