Refactor query to not require allocations

This commit is contained in:
Hanno Braun 2024-05-17 14:19:04 +02:00
parent eb32d919b1
commit 61011b28d2
2 changed files with 17 additions and 23 deletions

View File

@ -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<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>)>,
) {
self.region()
.all_cycles()
.flat_map(|cycle| {
) -> 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()))
})
.for_each(|r| result.push(r))
}
}
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())
}
}

View File

@ -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 {
@ -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