Skip to main content

use_mesh/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4/// A mesh vertex index.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub struct MeshVertexIndex(pub usize);
7
8/// A mesh face index.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct MeshFaceIndex(pub usize);
11
12/// A lightweight mesh count summary.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct Mesh {
15    vertex_count: usize,
16    edge_count: usize,
17    face_count: usize,
18}
19
20impl Mesh {
21    /// Creates a mesh summary with positive counts.
22    #[must_use]
23    pub const fn new(vertex_count: usize, edge_count: usize, face_count: usize) -> Option<Self> {
24        if vertex_count > 0 && edge_count > 0 && face_count > 0 {
25            Some(Self {
26                vertex_count,
27                edge_count,
28                face_count,
29            })
30        } else {
31            None
32        }
33    }
34
35    /// Returns the vertex count.
36    #[must_use]
37    pub const fn vertex_count(self) -> usize {
38        self.vertex_count
39    }
40
41    /// Returns the edge count.
42    #[must_use]
43    pub const fn edge_count(self) -> usize {
44        self.edge_count
45    }
46
47    /// Returns the face count.
48    #[must_use]
49    pub const fn face_count(self) -> usize {
50        self.face_count
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::{Mesh, MeshFaceIndex, MeshVertexIndex};
57
58    #[test]
59    fn stores_mesh_counts_and_indices() {
60        let mesh = Mesh::new(8, 12, 6).expect("valid mesh");
61
62        assert_eq!(mesh.vertex_count(), 8);
63        assert_eq!(mesh.edge_count(), 12);
64        assert_eq!(mesh.face_count(), 6);
65        assert_eq!(MeshVertexIndex(1).0, 1);
66        assert_eq!(MeshFaceIndex(2).0, 2);
67    }
68}