mesh_graph/ops/
delete.rs

1use hashbrown::HashSet;
2
3use crate::{FaceId, HalfedgeId, MeshGraph, VertexId};
4
5impl MeshGraph {
6    /// Deletes a face from the mesh graph.
7    ///
8    /// It also deletes the vertices and halfedges that are no
9    /// longer connected to any other faces.
10    ///
11    /// Returns the ids of the removed vertices and halfedges.
12    pub fn delete_face(&mut self, face_id: FaceId) -> (Vec<VertexId>, Vec<HalfedgeId>) {
13        let face = self.faces[face_id];
14
15        let vertices = face.vertices(self);
16        let halfedges = face.halfedges(self);
17
18        let mut deleted_halfedges = HashSet::with_capacity(4);
19
20        for he_id in halfedges {
21            let mut halfedge = self.halfedges[he_id];
22            let twin_id = halfedge.twin();
23
24            if self.halfedges[twin_id].is_boundary() {
25                deleted_halfedges.insert(he_id);
26                deleted_halfedges.insert(twin_id);
27            } else {
28                halfedge.face = None;
29            }
30        }
31
32        let mut deleted_vertices = Vec::with_capacity(3);
33
34        for &vertex_id in &vertices {
35            let mut all_deleted = true;
36            for he_id in self.vertices[vertex_id].outgoing_halfedges(self) {
37                if !deleted_halfedges.contains(&he_id) {
38                    all_deleted = false;
39                    break;
40                }
41            }
42
43            if all_deleted {
44                self.positions.remove(vertex_id);
45                if let Some(normals) = &mut self.vertex_normals {
46                    normals.remove(vertex_id);
47                }
48                self.vertices.remove(vertex_id);
49
50                deleted_vertices.push(vertex_id);
51            }
52        }
53
54        for &he_id in &deleted_halfedges {
55            self.halfedges.remove(he_id);
56        }
57
58        self.faces.remove(face_id);
59
60        (deleted_vertices, Vec::from_iter(deleted_halfedges))
61    }
62}