mesh_graph/integrations/
mod.rs

1use glam::Vec3;
2use slotmap::SecondaryMap;
3use tracing::{error, instrument};
4
5use crate::MeshGraph;
6
7#[cfg(feature = "bevy")]
8pub mod bevy;
9
10/// Classical indexed mesh representation
11#[derive(Clone, Debug)]
12pub struct VertexIndexBuffers {
13    /// Vertex positions, one per vertex.
14    pub positions: Vec<Vec3>,
15    /// Vertex normals, one per vertex.
16    pub normals: Vec<Vec3>,
17    /// Indices: 3*N where N is the number of triangles. Indices point to
18    /// elements of `positions` and `normals`.
19    pub indices: Vec<u32>,
20}
21
22impl From<MeshGraph> for VertexIndexBuffers {
23    #[instrument(skip(mesh_graph))]
24    fn from(mut mesh_graph: MeshGraph) -> VertexIndexBuffers {
25        if mesh_graph.vertex_normals.is_none() {
26            mesh_graph.compute_vertex_normals();
27        }
28        let vertex_normals = mesh_graph.vertex_normals.as_ref().unwrap();
29
30        let mut vertex_id_to_index = SecondaryMap::default();
31        let mut positions = vec![];
32        let mut normals = vec![];
33        let mut indices = vec![];
34
35        for (vertex_id, pos) in &mesh_graph.positions {
36            vertex_id_to_index.insert(vertex_id, positions.len() as u32);
37            positions.push(*pos);
38            normals.push(vertex_normals.get(vertex_id).copied().unwrap_or_else(|| {
39                error!("Normal not found");
40                Vec3::ZERO
41            }));
42        }
43
44        for face in mesh_graph.faces.values() {
45            for vertex in face.vertices(&mesh_graph) {
46                let index = vertex_id_to_index[vertex];
47                indices.push(index);
48            }
49        }
50
51        VertexIndexBuffers {
52            indices,
53            positions,
54            normals,
55        }
56    }
57}