ifc_lite_geometry/mesh_check/
shape.rs1use crate::mesh::Mesh;
13
14const SLIVER_ASPECT: f64 = 50.0;
17
18#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct TriangleShape {
21 pub slivers: usize,
23 pub max_aspect: f64,
25}
26
27pub fn triangle_shape(mesh: &Mesh) -> TriangleShape {
30 let point = |i: u32| -> Option<[f64; 3]> {
31 let start = (i as usize).checked_mul(3)?;
32 let p = mesh.positions.get(start..start.checked_add(3)?)?;
33 Some([f64::from(p[0]), f64::from(p[1]), f64::from(p[2])])
34 };
35 let mut shape = TriangleShape { slivers: 0, max_aspect: 0.0 };
36 for t in mesh.indices.chunks_exact(3) {
37 let (Some(a), Some(b), Some(c)) = (point(t[0]), point(t[1]), point(t[2])) else {
38 continue;
39 };
40 let sub = |p: [f64; 3], q: [f64; 3]| [p[0] - q[0], p[1] - q[1], p[2] - q[2]];
41 let dot = |p: [f64; 3], q: [f64; 3]| p[0] * q[0] + p[1] * q[1] + p[2] * q[2];
42 let (u, v, w) = (sub(b, a), sub(c, a), sub(c, b));
43 let n = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]];
44 let twice_area = dot(n, n).sqrt();
45 if twice_area <= 0.0 {
46 continue;
47 }
48 let aspect = dot(u, u).max(dot(v, v)).max(dot(w, w)) / twice_area;
50 shape.max_aspect = shape.max_aspect.max(aspect);
51 shape.slivers += usize::from(aspect > SLIVER_ASPECT);
52 }
53 shape
54}