From c30828c7d4a1299865991756c5da246ec7dca947 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 17 May 2024 14:13:43 +0200 Subject: [PATCH 1/3] Refactor to prepare for follow-on change --- .../src/queries/all_half_edges_with_surface.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/fj-core/src/queries/all_half_edges_with_surface.rs b/crates/fj-core/src/queries/all_half_edges_with_surface.rs index 55fa85d9f..96620cb3b 100644 --- a/crates/fj-core/src/queries/all_half_edges_with_surface.rs +++ b/crates/fj-core/src/queries/all_half_edges_with_surface.rs @@ -17,15 +17,16 @@ impl AllHalfEdgesWithSurface for Face { &self, result: &mut Vec<(Handle, Handle)>, ) { - for cycle in self.region().all_cycles() { - result.extend( + self.region() + .all_cycles() + .map(|cycle| { cycle .half_edges() .iter() .cloned() - .map(|half_edge| (half_edge, self.surface().clone())), - ); - } + .map(|half_edge| (half_edge, self.surface().clone())) + }) + .for_each(|iter| result.extend(iter)) } } From eb32d919b131ad8616ed819ce0d3abdcc144892e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 17 May 2024 14:14:20 +0200 Subject: [PATCH 2/3] Refactor to simplify --- crates/fj-core/src/queries/all_half_edges_with_surface.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-core/src/queries/all_half_edges_with_surface.rs b/crates/fj-core/src/queries/all_half_edges_with_surface.rs index 96620cb3b..70e221a0f 100644 --- a/crates/fj-core/src/queries/all_half_edges_with_surface.rs +++ b/crates/fj-core/src/queries/all_half_edges_with_surface.rs @@ -19,14 +19,14 @@ impl AllHalfEdgesWithSurface for Face { ) { self.region() .all_cycles() - .map(|cycle| { + .flat_map(|cycle| { cycle .half_edges() .iter() .cloned() .map(|half_edge| (half_edge, self.surface().clone())) }) - .for_each(|iter| result.extend(iter)) + .for_each(|r| result.push(r)) } } From 61011b28d2808099de27938a11070ee2bae725ee Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 17 May 2024 14:19:04 +0200 Subject: [PATCH 3/3] Refactor query to not require allocations --- .../queries/all_half_edges_with_surface.rs | 32 ++++++++----------- crates/fj-core/src/validate/shell.rs | 8 ++--- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/crates/fj-core/src/queries/all_half_edges_with_surface.rs b/crates/fj-core/src/queries/all_half_edges_with_surface.rs index 70e221a0f..d368bf230 100644 --- a/crates/fj-core/src/queries/all_half_edges_with_surface.rs +++ b/crates/fj-core/src/queries/all_half_edges_with_surface.rs @@ -8,35 +8,29 @@ pub trait AllHalfEdgesWithSurface { /// Access all half-edges of the object, and the surface they're on fn all_half_edges_with_surface( &self, - result: &mut Vec<(Handle, Handle)>, - ); + ) -> impl Iterator, Handle)>; } impl AllHalfEdgesWithSurface for Face { fn all_half_edges_with_surface( &self, - result: &mut Vec<(Handle, Handle)>, - ) { - self.region() - .all_cycles() - .flat_map(|cycle| { - cycle - .half_edges() - .iter() - .cloned() - .map(|half_edge| (half_edge, self.surface().clone())) - }) - .for_each(|r| result.push(r)) + ) -> impl Iterator, Handle)> { + self.region().all_cycles().flat_map(|cycle| { + cycle + .half_edges() + .iter() + .cloned() + .map(|half_edge| (half_edge, self.surface().clone())) + }) } } impl AllHalfEdgesWithSurface for Shell { fn all_half_edges_with_surface( &self, - result: &mut Vec<(Handle, Handle)>, - ) { - for face in self.faces() { - face.all_half_edges_with_surface(result); - } + ) -> impl Iterator, Handle)> { + self.faces() + .into_iter() + .flat_map(|face| face.all_half_edges_with_surface()) } } diff --git a/crates/fj-core/src/validate/shell.rs b/crates/fj-core/src/validate/shell.rs index f06fb6054..7c7f73654 100644 --- a/crates/fj-core/src/validate/shell.rs +++ b/crates/fj-core/src/validate/shell.rs @@ -84,8 +84,8 @@ impl ShellValidationError { config: &ValidationConfig, errors: &mut Vec, ) { - let mut edges_and_surfaces = Vec::new(); - shell.all_half_edges_with_surface(&mut edges_and_surfaces); + let edges_and_surfaces = + shell.all_half_edges_with_surface().collect::>(); for (edge_a, surface_a) in &edges_and_surfaces { for (edge_b, surface_b) in &edges_and_surfaces { @@ -230,8 +230,8 @@ impl ShellValidationError { config: &ValidationConfig, errors: &mut Vec, ) { - let mut edges_and_surfaces = Vec::new(); - shell.all_half_edges_with_surface(&mut edges_and_surfaces); + let edges_and_surfaces = + shell.all_half_edges_with_surface().collect::>(); // This is O(N^2) which isn't great, but we can't use a HashMap since we // need to deal with float inaccuracies. Maybe we could use some smarter