From 60cbde8a5a4e1549253f7c57cc6bdc0d3b09a390 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 9 Feb 2024 13:10:04 +0100 Subject: [PATCH 1/5] Merge `update_half_edge` and `replace_half_edge` --- crates/fj-core/src/operations/build/shell.rs | 30 ++++++++------ crates/fj-core/src/operations/join/cycle.rs | 14 ++++--- crates/fj-core/src/operations/update/cycle.rs | 39 +++---------------- crates/fj-core/src/validate/shell.rs | 15 ++++--- 4 files changed, 40 insertions(+), 58 deletions(-) diff --git a/crates/fj-core/src/operations/build/shell.rs b/crates/fj-core/src/operations/build/shell.rs index e111964ee..bc8d39f6c 100644 --- a/crates/fj-core/src/operations/build/shell.rs +++ b/crates/fj-core/src/operations/build/shell.rs @@ -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( @@ -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( @@ -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( @@ -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( diff --git a/crates/fj-core/src/operations/join/cycle.rs b/crates/fj-core/src/operations/join/cycle.rs index 9aab3e8a3..4a7aac165 100644 --- a/crates/fj-core/src/operations/join/cycle.rs +++ b/crates/fj-core/src/operations/join/cycle.rs @@ -119,7 +119,8 @@ 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() @@ -127,16 +128,17 @@ impl JoinCycle for Cycle { .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)] }, ) }, diff --git a/crates/fj-core/src/operations/update/cycle.rs b/crates/fj-core/src/operations/update/cycle.rs index 3367d3b42..0172f8d60 100644 --- a/crates/fj-core/src/operations/update/cycle.rs +++ b/crates/fj-core/src/operations/update/cycle.rs @@ -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( &self, handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> 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( - &self, - handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self; } @@ -53,26 +36,14 @@ impl UpdateCycle for Cycle { Cycle::new(half_edges) } - fn update_half_edge( - &self, - handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> Self { - let edges = self - .half_edges() - .replace(handle, [update(handle)]) - .expect("Half-edge not found"); - Cycle::new(edges) - } - - fn replace_half_edge( + fn update_half_edge( &self, handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self { let edges = self .half_edges() - .replace(handle, replace(handle)) + .replace(handle, update(handle)) .expect("Half-edge not found"); Cycle::new(edges) } diff --git a/crates/fj-core/src/validate/shell.rs b/crates/fj-core/src/validate/shell.rs index f4f59740c..d6b92140b 100644 --- a/crates/fj-core/src/validate/shell.rs +++ b/crates/fj-core/src/validate/shell.rs @@ -424,11 +424,12 @@ mod tests { .update_half_edge( cycle.half_edges().nth_circular(0), |edge| { - edge.update_path(|path| path.reverse()) + [edge + .update_path(|path| path.reverse()) .update_boundary(|boundary| { boundary.reverse() }) - .insert(&mut core.services) + .insert(&mut core.services)] }, ) .insert(&mut core.services) @@ -486,10 +487,12 @@ mod tests { .update_half_edge( cycle.half_edges().nth_circular(0), |edge| { - edge.update_curve(|_| { - Curve::new().insert(&mut core.services) - }) - .insert(&mut core.services) + [edge + .update_curve(|_| { + Curve::new() + .insert(&mut core.services) + }) + .insert(&mut core.services)] }, ) .insert(&mut core.services) From 9152ab36f6aa61aae5c230ea78423ef495bb13aa Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 9 Feb 2024 13:12:52 +0100 Subject: [PATCH 2/5] Merge `update_interior` and `replace_interior` --- .../fj-core/src/operations/update/region.rs | 39 +++---------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/crates/fj-core/src/operations/update/region.rs b/crates/fj-core/src/operations/update/region.rs index 8403d8ab9..eb26853e8 100644 --- a/crates/fj-core/src/operations/update/region.rs +++ b/crates/fj-core/src/operations/update/region.rs @@ -27,27 +27,10 @@ pub trait UpdateRegion { /// /// Panics, if the update results in a duplicate object. #[must_use] - fn update_interior( + fn update_interior( &self, handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> 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( - &self, - handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self; } @@ -68,26 +51,14 @@ impl UpdateRegion for Region { Region::new(self.exterior().clone(), interiors, self.color()) } - fn update_interior( - &self, - handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> Self { - let interiors = self - .interiors() - .replace(handle, [update(handle)]) - .expect("Cycle not found"); - Region::new(self.exterior().clone(), interiors, self.color()) - } - - fn replace_interior( + fn update_interior( &self, handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; 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()) } From 353d77e01df5a6f2dc6453c76420358d952bebd3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 9 Feb 2024 13:15:50 +0100 Subject: [PATCH 3/5] Merge `update_face` and `replace_face` --- crates/fj-core/src/operations/holes.rs | 77 +++++++++-------- crates/fj-core/src/operations/split/face.rs | 2 +- crates/fj-core/src/operations/update/shell.rs | 39 ++------- crates/fj-core/src/validate/shell.rs | 82 ++++++++++--------- models/color/src/lib.rs | 9 +- 5 files changed, 97 insertions(+), 112 deletions(-) diff --git a/crates/fj-core/src/operations/holes.rs b/crates/fj-core/src/operations/holes.rs index 91dc5b919..06c2d4f1a 100644 --- a/crates/fj-core/src/operations/holes.rs +++ b/crates/fj-core/src/operations/holes.rs @@ -63,17 +63,22 @@ impl AddHole for Shell { .collect::>(); 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) } @@ -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) } diff --git a/crates/fj-core/src/operations/split/face.rs b/crates/fj-core/src/operations/split/face.rs index bb5553d0f..713e42caa 100644 --- a/crates/fj-core/src/operations/split/face.rs +++ b/crates/fj-core/src/operations/split/face.rs @@ -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) } diff --git a/crates/fj-core/src/operations/update/shell.rs b/crates/fj-core/src/operations/update/shell.rs index 7b19737ee..79f7a7c14 100644 --- a/crates/fj-core/src/operations/update/shell.rs +++ b/crates/fj-core/src/operations/update/shell.rs @@ -17,27 +17,10 @@ pub trait UpdateShell { /// /// Panics, if the update results in a duplicate object. #[must_use] - fn update_face( + fn update_face( &self, handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> 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( - &self, - handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self; /// Remove a face from the shell @@ -51,26 +34,14 @@ impl UpdateShell for Shell { Shell::new(faces) } - fn update_face( - &self, - handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> Self { - let faces = self - .faces() - .replace(handle, [update(handle)]) - .expect("Face not found"); - Shell::new(faces) - } - - fn replace_face( + fn update_face( &self, handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self { let faces = self .faces() - .replace(handle, replace(handle)) + .replace(handle, update(handle)) .expect("Face not found"); Shell::new(faces) } diff --git a/crates/fj-core/src/validate/shell.rs b/crates/fj-core/src/validate/shell.rs index d6b92140b..387c730f5 100644 --- a/crates/fj-core/src/validate/shell.rs +++ b/crates/fj-core/src/validate/shell.rs @@ -417,26 +417,27 @@ mod tests { &mut core, ); let invalid = valid.shell.update_face(&valid.abc.face, |face| { - face.update_region(|region| { - region - .update_exterior(|cycle| { - cycle - .update_half_edge( - cycle.half_edges().nth_circular(0), - |edge| { - [edge - .update_path(|path| path.reverse()) - .update_boundary(|boundary| { - boundary.reverse() - }) - .insert(&mut core.services)] - }, - ) - .insert(&mut core.services) - }) - .insert(&mut core.services) - }) - .insert(&mut core.services) + [face + .update_region(|region| { + region + .update_exterior(|cycle| { + cycle + .update_half_edge( + cycle.half_edges().nth_circular(0), + |edge| { + [edge + .update_path(|path| path.reverse()) + .update_boundary(|boundary| { + boundary.reverse() + }) + .insert(&mut core.services)] + }, + ) + .insert(&mut core.services) + }) + .insert(&mut core.services) + }) + .insert(&mut core.services)] }); valid.shell.validate_and_return_first_error()?; @@ -480,26 +481,27 @@ mod tests { &mut core, ); let invalid = valid.shell.update_face(&valid.abc.face, |face| { - face.update_region(|region| { - region - .update_exterior(|cycle| { - cycle - .update_half_edge( - cycle.half_edges().nth_circular(0), - |edge| { - [edge - .update_curve(|_| { - Curve::new() - .insert(&mut core.services) - }) - .insert(&mut core.services)] - }, - ) - .insert(&mut core.services) - }) - .insert(&mut core.services) - }) - .insert(&mut core.services) + [face + .update_region(|region| { + region + .update_exterior(|cycle| { + cycle + .update_half_edge( + cycle.half_edges().nth_circular(0), + |edge| { + [edge + .update_curve(|_| { + Curve::new() + .insert(&mut core.services) + }) + .insert(&mut core.services)] + }, + ) + .insert(&mut core.services) + }) + .insert(&mut core.services) + }) + .insert(&mut core.services)] }); valid.shell.validate_and_return_first_error()?; diff --git a/models/color/src/lib.rs b/models/color/src/lib.rs index e5199d83e..f62f356d4 100644 --- a/models/color/src/lib.rs +++ b/models/color/src/lib.rs @@ -14,10 +14,11 @@ pub fn model(core: &mut fj::core::Instance) -> Solid { cuboid.update_shell(cuboid.shells().only(), |shell| { let shell = shell.update_face(shell.faces().first(), |face| { - face.update_region(|region| { - region.set_color([0., 1., 0.]).insert(&mut core.services) - }) - .insert(&mut core.services) + [face + .update_region(|region| { + region.set_color([0., 1., 0.]).insert(&mut core.services) + }) + .insert(&mut core.services)] }); // Split colored face, to make sure the same color is applied to the From 4c66edb70148cf57ec57dc1939bbb0d53ffb21a2 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 9 Feb 2024 13:17:40 +0100 Subject: [PATCH 4/5] Merge `update_region` and `replace_region` --- .../fj-core/src/operations/update/sketch.rs | 39 +++---------------- 1 file changed, 5 insertions(+), 34 deletions(-) diff --git a/crates/fj-core/src/operations/update/sketch.rs b/crates/fj-core/src/operations/update/sketch.rs index 6022cafde..0c26966ac 100644 --- a/crates/fj-core/src/operations/update/sketch.rs +++ b/crates/fj-core/src/operations/update/sketch.rs @@ -17,27 +17,10 @@ pub trait UpdateSketch { /// /// Panics, if the update results in a duplicate object. #[must_use] - fn update_region( + fn update_region( &self, handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> 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 - /// - /// Panics, if the object can't be found. - /// - /// Panics, if the update results in a duplicate object. - #[must_use] - fn replace_region( - &self, - handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self; } @@ -46,26 +29,14 @@ impl UpdateSketch for Sketch { Sketch::new(self.regions().iter().cloned().chain([region])) } - fn update_region( - &self, - handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> Self { - let regions = self - .regions() - .replace(handle, [update(handle)]) - .expect("Region not found"); - Sketch::new(regions) - } - - fn replace_region( + fn update_region( &self, handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self { let regions = self .regions() - .replace(handle, replace(handle)) + .replace(handle, update(handle)) .expect("Region not found"); Sketch::new(regions) } From 9d27be133f69af7f01061cae726b176ecb75f1ab Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 9 Feb 2024 13:18:48 +0100 Subject: [PATCH 5/5] Merge `update_shell` and `replace_shell` --- crates/fj-core/src/operations/update/solid.rs | 39 +++---------------- models/color/src/lib.rs | 2 +- models/holes/src/lib.rs | 4 +- models/split/src/lib.rs | 4 +- 4 files changed, 10 insertions(+), 39 deletions(-) diff --git a/crates/fj-core/src/operations/update/solid.rs b/crates/fj-core/src/operations/update/solid.rs index 13f8a3748..eb54f2287 100644 --- a/crates/fj-core/src/operations/update/solid.rs +++ b/crates/fj-core/src/operations/update/solid.rs @@ -20,27 +20,10 @@ pub trait UpdateSolid { /// /// Panics, if the update results in a duplicate object. #[must_use] - fn update_shell( + fn update_shell( &self, handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> 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 - /// - /// Panics, if the object can't be found. - /// - /// Panics, if the update results in a duplicate object. - #[must_use] - fn replace_shell( - &self, - handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self; } @@ -53,26 +36,14 @@ impl UpdateSolid for Solid { Solid::new(shells) } - fn update_shell( - &self, - handle: &Handle, - update: impl FnOnce(&Handle) -> Handle, - ) -> Self { - let shells = self - .shells() - .replace(handle, [update(handle)]) - .expect("Shell not found"); - Solid::new(shells) - } - - fn replace_shell( + fn update_shell( &self, handle: &Handle, - replace: impl FnOnce(&Handle) -> [Handle; N], + update: impl FnOnce(&Handle) -> [Handle; N], ) -> Self { let shells = self .shells() - .replace(handle, replace(handle)) + .replace(handle, update(handle)) .expect("Shell not found"); Solid::new(shells) } diff --git a/models/color/src/lib.rs b/models/color/src/lib.rs index f62f356d4..a0689f1cc 100644 --- a/models/color/src/lib.rs +++ b/models/color/src/lib.rs @@ -38,6 +38,6 @@ pub fn model(core: &mut fj::core::Instance) -> Solid { shell }; - shell.insert(&mut core.services) + [shell.insert(&mut core.services)] }) } diff --git a/models/holes/src/lib.rs b/models/holes/src/lib.rs index 070048fbd..a9d0461ff 100644 --- a/models/holes/src/lib.rs +++ b/models/holes/src/lib.rs @@ -40,7 +40,7 @@ pub fn model( .nth(5) .expect("Expected shell to have top face"); - shell + [shell .add_through_hole( [ HoleLocation { @@ -55,6 +55,6 @@ pub fn model( radius, core, ) - .insert(&mut core.services) + .insert(&mut core.services)] }) } diff --git a/models/split/src/lib.rs b/models/split/src/lib.rs index f25410605..6b209cb0b 100644 --- a/models/split/src/lib.rs +++ b/models/split/src/lib.rs @@ -24,8 +24,8 @@ pub fn model( let (shell, [face, _]) = shell.split_face(face, line, core); - shell + [shell .sweep_face_of_shell(face, [0., 0., -size / 2.], core) - .insert(&mut core.services) + .insert(&mut core.services)] }) }