Skip to main content

runmat_geometry_core/model/
mesh.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
4#[serde(rename_all = "snake_case")]
5pub enum MeshKind {
6    Surface,
7    Volume,
8}
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct MeshDescriptor {
12    pub mesh_id: String,
13    pub kind: MeshKind,
14    pub vertex_count: u64,
15    pub element_count: u64,
16}
17
18#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
19pub struct SurfaceMesh {
20    pub mesh_id: String,
21    pub vertices: Vec<[f64; 3]>,
22    pub triangles: Vec<[u32; 3]>,
23}
24
25impl SurfaceMesh {
26    pub fn new(
27        mesh_id: impl Into<String>,
28        vertices: Vec<[f64; 3]>,
29        triangles: Vec<[u32; 3]>,
30    ) -> Self {
31        Self {
32            mesh_id: mesh_id.into(),
33            vertices,
34            triangles,
35        }
36    }
37
38    pub fn validate(&self) -> Result<(), &'static str> {
39        if self.mesh_id.is_empty() {
40            return Err("surface mesh id must not be empty");
41        }
42        if self
43            .vertices
44            .iter()
45            .flatten()
46            .any(|coordinate| !coordinate.is_finite())
47        {
48            return Err("surface mesh vertices must be finite");
49        }
50        let vertex_count = self.vertices.len();
51        if self
52            .triangles
53            .iter()
54            .flatten()
55            .any(|index| *index as usize >= vertex_count)
56        {
57            return Err("surface mesh triangle index out of bounds");
58        }
59        Ok(())
60    }
61}