From 55626f2e952b3ff9c09b8ef47907055d89548056 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 21 Apr 2023 09:40:58 +0200 Subject: [PATCH 1/4] Panic, to catch errors in cycle updates --- .../fj-kernel/src/operations/update/cycle.rs | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/operations/update/cycle.rs b/crates/fj-kernel/src/operations/update/cycle.rs index d0c9df934..5b6736c0c 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,6 +23,10 @@ pub trait UpdateCycle { ) -> Cycle; /// Replace the half-edge at the given index + /// + /// # Panics + /// + /// Panics, unless this operation replaces exactly one half-edge. fn replace_nth_half_edge( &self, index: usize, @@ -40,15 +48,25 @@ 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( @@ -56,14 +74,24 @@ impl UpdateCycle for Cycle { 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 } } From 08640e81004c07a2a6ff499d3eb35043c4019803 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 21 Apr 2023 10:49:21 +0200 Subject: [PATCH 2/4] Rename trait method to match new nomenclature --- crates/fj-kernel/src/algorithms/sweep/edge.rs | 2 +- crates/fj-kernel/src/builder/cycle.rs | 2 +- crates/fj-kernel/src/operations/update/edge.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep/edge.rs b/crates/fj-kernel/src/algorithms/sweep/edge.rs index dffe0eda3..f3b64f828 100644 --- a/crates/fj-kernel/src/algorithms/sweep/edge.rs +++ b/crates/fj-kernel/src/algorithms/sweep/edge.rs @@ -83,7 +83,7 @@ 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) diff --git a/crates/fj-kernel/src/builder/cycle.rs b/crates/fj-kernel/src/builder/cycle.rs index fa84e4cb2..1c2671fd1 100644 --- a/crates/fj-kernel/src/builder/cycle.rs +++ b/crates/fj-kernel/src/builder/cycle.rs @@ -41,7 +41,7 @@ impl CycleBuilder { .circular_tuple_windows() .map(|((prev, _, _), (half_edge, curve, boundary))| { HalfEdge::unjoined(curve, boundary, objects) - .update_start_vertex(prev.start_vertex().clone()) + .replace_start_vertex(prev.start_vertex().clone()) .update_global_form(half_edge.global_form().clone()) }) .collect(); diff --git a/crates/fj-kernel/src/operations/update/edge.rs b/crates/fj-kernel/src/operations/update/edge.rs index 74b4ce630..15f9d934b 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; } 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(), From 51845929b945f6a8f181f0bdd45e621ae7c4ca00 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 21 Apr 2023 10:49:21 +0200 Subject: [PATCH 3/4] Rename trait method to match new nomenclature --- crates/fj-kernel/src/algorithms/sweep/edge.rs | 2 +- crates/fj-kernel/src/builder/cycle.rs | 2 +- crates/fj-kernel/src/operations/build/face.rs | 2 +- crates/fj-kernel/src/operations/update/edge.rs | 4 ++-- crates/fj-kernel/src/validate/shell.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep/edge.rs b/crates/fj-kernel/src/algorithms/sweep/edge.rs index f3b64f828..7b785e5a8 100644 --- a/crates/fj-kernel/src/algorithms/sweep/edge.rs +++ b/crates/fj-kernel/src/algorithms/sweep/edge.rs @@ -86,7 +86,7 @@ impl Sweep for (&HalfEdge, &Handle, &Surface, Option) { .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 1c2671fd1..3fff2c6b4 100644 --- a/crates/fj-kernel/src/builder/cycle.rs +++ b/crates/fj-kernel/src/builder/cycle.rs @@ -42,7 +42,7 @@ impl CycleBuilder { .map(|((prev, _, _), (half_edge, curve, boundary))| { HalfEdge::unjoined(curve, boundary, objects) .replace_start_vertex(prev.start_vertex().clone()) - .update_global_form(half_edge.global_form().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/edge.rs b/crates/fj-kernel/src/operations/update/edge.rs index 15f9d934b..758fd0293 100644 --- a/crates/fj-kernel/src/operations/update/edge.rs +++ b/crates/fj-kernel/src/operations/update/edge.rs @@ -9,7 +9,7 @@ pub trait UpdateHalfEdge { 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 { @@ -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..e61bbf675 100644 --- a/crates/fj-kernel/src/validate/shell.rs +++ b/crates/fj-kernel/src/validate/shell.rs @@ -217,7 +217,7 @@ mod tests { 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) From 4e085256c3e991145b6ea2444d2672f87ae4bcd0 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 21 Apr 2023 10:49:21 +0200 Subject: [PATCH 4/4] Rename trait method to match new nomenclature --- crates/fj-kernel/src/operations/update/cycle.rs | 4 ++-- crates/fj-kernel/src/validate/shell.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-kernel/src/operations/update/cycle.rs b/crates/fj-kernel/src/operations/update/cycle.rs index 5b6736c0c..0aa9f8669 100644 --- a/crates/fj-kernel/src/operations/update/cycle.rs +++ b/crates/fj-kernel/src/operations/update/cycle.rs @@ -27,7 +27,7 @@ pub trait UpdateCycle { /// # Panics /// /// Panics, unless this operation replaces exactly one half-edge. - fn replace_nth_half_edge( + fn update_nth_half_edge( &self, index: usize, f: impl FnMut(&Handle) -> Handle, @@ -69,7 +69,7 @@ impl UpdateCycle for Cycle { cycle } - fn replace_nth_half_edge( + fn update_nth_half_edge( &self, index: usize, mut f: impl FnMut(&Handle) -> Handle, diff --git a/crates/fj-kernel/src/validate/shell.rs b/crates/fj-kernel/src/validate/shell.rs index e61bbf675..c466bfc01 100644 --- a/crates/fj-kernel/src/validate/shell.rs +++ b/crates/fj-kernel/src/validate/shell.rs @@ -213,7 +213,7 @@ 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