Another project
1use bone_types::{Point3, UnitVec3};
2
3#[derive(Copy, Clone, Debug, PartialEq)]
4pub struct MeshVertex {
5 position: Point3,
6 normal: UnitVec3,
7}
8
9impl MeshVertex {
10 #[must_use]
11 pub(crate) const fn new(position: Point3, normal: UnitVec3) -> Self {
12 Self { position, normal }
13 }
14
15 #[must_use]
16 pub const fn position(self) -> Point3 {
17 self.position
18 }
19
20 #[must_use]
21 pub const fn normal(self) -> UnitVec3 {
22 self.normal
23 }
24}
25
26#[derive(Clone, Debug, PartialEq)]
27pub struct TriMesh {
28 vertices: Vec<MeshVertex>,
29 triangles: Vec<[u32; 3]>,
30}
31
32impl TriMesh {
33 #[must_use]
34 pub fn vertices(&self) -> &[MeshVertex] {
35 &self.vertices
36 }
37
38 #[must_use]
39 pub fn triangles(&self) -> &[[u32; 3]] {
40 &self.triangles
41 }
42
43 pub(crate) fn from_grid(
44 columns: u32,
45 rows: u32,
46 vertex: impl Fn(u32, u32) -> MeshVertex,
47 ) -> Self {
48 let vertices = (0..rows)
49 .flat_map(|j| (0..columns).map(move |i| (i, j)))
50 .map(|(i, j)| vertex(i, j))
51 .collect();
52 let index = |i: u32, j: u32| j * columns + i;
53 let triangles = (0..rows.saturating_sub(1))
54 .flat_map(move |j| (0..columns.saturating_sub(1)).map(move |i| (i, j)))
55 .flat_map(|(i, j)| {
56 [
57 [index(i, j), index(i + 1, j), index(i + 1, j + 1)],
58 [index(i, j), index(i + 1, j + 1), index(i, j + 1)],
59 ]
60 })
61 .collect();
62 Self {
63 vertices,
64 triangles,
65 }
66 }
67}