geom3d/
mesh.rs

1use crate::{Grid, Point3};
2
3#[derive(Clone)]
4pub struct TriangleMesh {
5    pub vertices: Vec<Point3>,
6    /// Indices of points forming triangle list
7    pub triangles: Vec<u32>,
8}
9
10impl TriangleMesh {
11    pub fn triangle_count(&self) -> usize {
12        self.triangles.len() / 3
13    }
14}
15
16impl From<Grid<Point3>> for TriangleMesh {
17    fn from(grid: Grid<Point3>) -> TriangleMesh {
18        let mut triangles = Vec::with_capacity((grid.rows() - 1) * (grid.cols() - 1) * 6);
19        for row in 0..grid.rows() - 1 {
20            for col in 0..grid.cols() - 1 {
21                // first triangle
22                triangles.push((row * grid.cols() + col) as u32);
23                triangles.push(((row + 1) * grid.cols() + col) as u32);
24                triangles.push((row * grid.cols() + col + 1) as u32);
25                // second triangle
26                triangles.push((row * grid.cols() + col + 1) as u32);
27                triangles.push(((row + 1) * grid.cols() + col) as u32);
28                triangles.push(((row + 1) * grid.cols() + col + 1) as u32);
29            }
30        }
31        TriangleMesh {
32            vertices: grid.into_vec(),
33            triangles,
34        }
35    }
36}