mesh_graph/elements/
face.rs

1use glam::Vec3;
2
3use crate::MeshGraph;
4
5use super::{FaceId, HalfedgeId, VertexId};
6
7#[derive(Default, Debug, Clone, Copy)]
8pub struct Face {
9    /// One of the halfedges of the face.
10    /// Serves as a starting point for traversing the face's edges and vertices
11    pub halfedge: HalfedgeId,
12
13    /// The index of the face in the qbvh
14    pub index: usize,
15
16    /// The associated face id
17    pub id: FaceId,
18}
19
20impl parry3d::partitioning::IndexedData for Face {
21    fn default() -> Self {
22        Default::default()
23    }
24
25    fn index(&self) -> usize {
26        self.index
27    }
28}
29
30impl Face {
31    // TODO : create iterator instead of returning a Vec
32    /// Returns the three halfedges that form this face
33    pub fn halfedges(&self, mesh_graph: &MeshGraph) -> Vec<HalfedgeId> {
34        // TODO :this currently only works for triangle meshes
35        let mut edges = Vec::with_capacity(3);
36        edges.push(self.halfedge);
37
38        let he = mesh_graph.halfedges[self.halfedge];
39        let next_id = he.next.unwrap();
40        edges.push(next_id);
41        edges.push(mesh_graph.halfedges[next_id].next.unwrap());
42
43        edges
44    }
45
46    // TODO : create iterator instead of returning a Vec
47    /// Returns the three corner vertices of this face.
48    pub fn vertices(&self, mesh_graph: &MeshGraph) -> Vec<VertexId> {
49        let mut vertices = Vec::with_capacity(3);
50
51        for halfedge in self.halfedges(mesh_graph) {
52            vertices.push(mesh_graph.halfedges[halfedge].end_vertex);
53        }
54
55        vertices
56    }
57
58    /// Center positions of this face.
59    pub fn center(&self, mesh_graph: &MeshGraph) -> Vec3 {
60        let mut sum = Vec3::ZERO;
61        for vertex in self.vertices(mesh_graph) {
62            sum += mesh_graph.positions[vertex];
63        }
64        sum / 3.0
65    }
66}