diff --git a/crates/fj-kernel/src/algorithms/sweep/edge.rs b/crates/fj-kernel/src/algorithms/sweep/edge.rs index dffe0eda3..7b785e5a8 100644 --- a/crates/fj-kernel/src/algorithms/sweep/edge.rs +++ b/crates/fj-kernel/src/algorithms/sweep/edge.rs @@ -83,10 +83,10 @@ impl Sweep for (&HalfEdge, &Handle, &Surface, Option) { Some(boundary), objects, ) - .update_start_vertex(start_vertex); + .replace_start_vertex(start_vertex); let half_edge = if let Some(global_edge) = global_edge { - half_edge.update_global_form(global_edge) + half_edge.replace_global_form(global_edge) } else { half_edge }; diff --git a/crates/fj-kernel/src/builder/cycle.rs b/crates/fj-kernel/src/builder/cycle.rs index fa84e4cb2..3fff2c6b4 100644 --- a/crates/fj-kernel/src/builder/cycle.rs +++ b/crates/fj-kernel/src/builder/cycle.rs @@ -41,8 +41,8 @@ impl CycleBuilder { .circular_tuple_windows() .map(|((prev, _, _), (half_edge, curve, boundary))| { HalfEdge::unjoined(curve, boundary, objects) - .update_start_vertex(prev.start_vertex().clone()) - .update_global_form(half_edge.global_form().clone()) + .replace_start_vertex(prev.start_vertex().clone()) + .replace_global_form(half_edge.global_form().clone()) }) .collect(); diff --git a/crates/fj-kernel/src/operations/build/face.rs b/crates/fj-kernel/src/operations/build/face.rs index 076e80ca5..7d3cb6dda 100644 --- a/crates/fj-kernel/src/operations/build/face.rs +++ b/crates/fj-kernel/src/operations/build/face.rs @@ -30,7 +30,7 @@ pub trait BuildFace { ); if let Some(global_form) = global_form { - half_edge = half_edge.update_global_form(global_form); + half_edge = half_edge.replace_global_form(global_form); } half_edge.insert(objects) diff --git a/crates/fj-kernel/src/operations/update/cycle.rs b/crates/fj-kernel/src/operations/update/cycle.rs index d0c9df934..0aa9f8669 100644 --- a/crates/fj-kernel/src/operations/update/cycle.rs +++ b/crates/fj-kernel/src/operations/update/cycle.rs @@ -12,6 +12,10 @@ pub trait UpdateCycle { ) -> Cycle; /// Replace the provided half-edge + /// + /// # Panics + /// + /// Panics, unless this operation replaces exactly one half-edge. fn replace_half_edge( &self, original: &Handle, @@ -19,7 +23,11 @@ pub trait UpdateCycle { ) -> Cycle; /// Replace the half-edge at the given index - fn replace_nth_half_edge( + /// + /// # Panics + /// + /// Panics, unless this operation replaces exactly one half-edge. + fn update_nth_half_edge( &self, index: usize, f: impl FnMut(&Handle) -> Handle, @@ -40,30 +48,50 @@ impl UpdateCycle for Cycle { original: &Handle, replacement: Handle, ) -> Cycle { + let mut num_replacements = 0; + let half_edges = self.half_edges().map(|half_edge| { if half_edge.id() == original.id() { + num_replacements += 1; replacement.clone() } else { half_edge.clone() } }); - Cycle::new(half_edges) + let cycle = Cycle::new(half_edges); + + assert_eq!( + num_replacements, 1, + "Expected operation to replace exactly one half-edge" + ); + + cycle } - fn replace_nth_half_edge( + fn update_nth_half_edge( &self, index: usize, mut f: impl FnMut(&Handle) -> Handle, ) -> Cycle { + let mut num_replacements = 0; + let half_edges = self.half_edges().enumerate().map(|(i, half_edge)| { if i == index { + num_replacements += 1; f(half_edge) } else { half_edge.clone() } }); - Cycle::new(half_edges) + let cycle = Cycle::new(half_edges); + + assert_eq!( + num_replacements, 1, + "Expected operation to replace exactly one half-edge" + ); + + cycle } } diff --git a/crates/fj-kernel/src/operations/update/edge.rs b/crates/fj-kernel/src/operations/update/edge.rs index 74b4ce630..758fd0293 100644 --- a/crates/fj-kernel/src/operations/update/edge.rs +++ b/crates/fj-kernel/src/operations/update/edge.rs @@ -6,14 +6,14 @@ use crate::{ /// Update a [`HalfEdge`] pub trait UpdateHalfEdge { /// Update the start vertex of the half-edge - fn update_start_vertex(&self, start_vertex: Handle) -> HalfEdge; + fn replace_start_vertex(&self, start_vertex: Handle) -> HalfEdge; /// Update the global form of the half-edge - fn update_global_form(&self, global_form: Handle) -> HalfEdge; + fn replace_global_form(&self, global_form: Handle) -> HalfEdge; } impl UpdateHalfEdge for HalfEdge { - fn update_start_vertex(&self, start_vertex: Handle) -> HalfEdge { + fn replace_start_vertex(&self, start_vertex: Handle) -> HalfEdge { HalfEdge::new( self.curve(), self.boundary(), @@ -22,7 +22,7 @@ impl UpdateHalfEdge for HalfEdge { ) } - fn update_global_form(&self, global_form: Handle) -> HalfEdge { + fn replace_global_form(&self, global_form: Handle) -> HalfEdge { HalfEdge::new( self.curve(), self.boundary(), diff --git a/crates/fj-kernel/src/validate/shell.rs b/crates/fj-kernel/src/validate/shell.rs index e3ce07783..c466bfc01 100644 --- a/crates/fj-kernel/src/validate/shell.rs +++ b/crates/fj-kernel/src/validate/shell.rs @@ -213,11 +213,11 @@ mod tests { let invalid = valid.shell.update_face(&valid.face_abc, |face| { face.update_exterior(|cycle| { cycle - .replace_nth_half_edge(0, |half_edge| { + .update_nth_half_edge(0, |half_edge| { let global_form = GlobalEdge::new().insert(&mut services.objects); half_edge - .update_global_form(global_form) + .replace_global_form(global_form) .insert(&mut services.objects) }) .insert(&mut services.objects)