Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AllHalfEdgesWithSurface query to not require allocations #2358

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions crates/fj-core/src/queries/all_half_edges_with_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +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<HalfEdge>, Handle<Surface>)>,
);
) -> impl Iterator<Item = (Handle<HalfEdge>, Handle<Surface>)>;
}

impl AllHalfEdgesWithSurface for Face {
fn all_half_edges_with_surface(
&self,
result: &mut Vec<(Handle<HalfEdge>, Handle<Surface>)>,
) {
for cycle in self.region().all_cycles() {
result.extend(
cycle
.half_edges()
.iter()
.cloned()
.map(|half_edge| (half_edge, self.surface().clone())),
);
}
) -> impl Iterator<Item = (Handle<HalfEdge>, Handle<Surface>)> {
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<HalfEdge>, Handle<Surface>)>,
) {
for face in self.faces() {
face.all_half_edges_with_surface(result);
}
) -> impl Iterator<Item = (Handle<HalfEdge>, Handle<Surface>)> {
self.faces()
.into_iter()
.flat_map(|face| face.all_half_edges_with_surface())
}
}
8 changes: 4 additions & 4 deletions crates/fj-core/src/validate/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ impl ShellValidationError {
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
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::<Vec<_>>();

for (edge_a, surface_a) in &edges_and_surfaces {
for (edge_b, surface_b) in &edges_and_surfaces {
Expand Down Expand Up @@ -230,8 +230,8 @@ impl ShellValidationError {
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
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::<Vec<_>>();

// 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
Expand Down