Skip to content

Commit

Permalink
can remove_useless_vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jul 24, 2024
1 parent 03cfd86 commit 92fa829
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions src/merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,61 @@ impl Mesh {
/// This merge neighbouring polygons when possible, keeping them convex.
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub fn merge_polygons(&mut self) -> bool {
self.layers
!self
.layers
.iter_mut()
.map(|layer| layer.merge_polygons())
.any(|m| m)
.all(|m| !m)
}

/// Remove vertices that are not used by any polygon, and update indexes.
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub fn remove_useless_vertices(&mut self) -> bool {
!self
.layers
.iter_mut()
.map(|layer| layer.remove_useless_vertices())
.all(|m| !m)
}
}

impl Layer {
/// Remove vertices that are not used by any polygon, and update indexes.
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub fn remove_useless_vertices(&mut self) -> bool {
let mut removed = false;
let mut new_indexes = vec![u32::MAX; self.vertices.len()];
let mut kept = 0;
for (i, vertex) in self.vertices.iter().enumerate() {
if vertex.polygons.len() == 0 {
removed = true;
} else if vertex.polygons == [u32::MAX] {
removed = true;
} else {
new_indexes[i] = kept;
kept += 1;
}
}
for polygon in self.polygons.iter_mut() {
for vertex in polygon.vertices.iter_mut() {
*vertex = new_indexes[*vertex as usize];
}
}
self.vertices = self
.vertices
.iter()
.enumerate()
.filter_map(|(i, _)| {
if new_indexes[i] != u32::MAX {
Some(self.vertices[i].clone())
} else {
None
}
})
.collect();
removed
}

/// Merge polygons.
///
/// This merge neighbouring polygons when possible, keeping them convex.
Expand Down

0 comments on commit 92fa829

Please sign in to comment.