JFLib

physics/src/mesh.rs#

Source: lib/physics/src/mesh.rs

mesh -- a static triangle mesh: vertices in f32 (as render3d's meshes), a fixed BVH, the active edges,
and the queries against it in its own frame

Seams: every triangle is tested as the closed triangle it is, so a shape sweeping across a shared edge
meets the edge of one triangle or the other, there is no gap.  The opposite trouble -- a shape sliding over
a flat or concave edge between two triangles hits the edge itself with a normal that leans backwards
(a "ghost" collision) -- is handled by the active edges: an edge is active when the surface bends
outwards there by more than the threshold angle (or the edge is open or shared by more than two
triangles).  A contact on an inactive edge (or on a vertex whose two edges of this triangle are inactive)
gets the triangle's normal, and a cast ignores it unless the shape moves into that face.  As in Jolt's
"active edges"; the side matters, an edge that is concave from the front is convex from the back.
/// at most this many triangles in one mesh
MAX_TRIANGLES: usize = 1 << 28
/// cos 5 degrees: two triangles whose normals differ by less than this meet at a flat edge
ACTIVE_EDGE_COS: f64 = 0.996_194_698_091_745_5
  • enum MeshError why [TriMesh::new] refused the data

    • IndexCount -- the index count is not a multiple of 3, or there are more than [MAX_TRIANGLES] triangles
    • IndexRange -- an index is not below the vertex count
    • NotFinite -- a coordinate is NaN or infinite
  • struct MeshOptions how a [TriMesh] is built

    • active_edge_cos: f64 -- an edge between two triangles whose normals have a cosine of at least this is flat (inactive); above 1 every edge is active (no handling of ghost collisions)
    • max_leaf: usize -- at most so many triangles in a leaf of the tree
  • struct TriMesh A static triangle mesh with its tree. Triangles have two sides. Triangles without area (all three corners on a line) are left out: they cannot be hit and their edges belong to their neighbours.

impl TriMesh

  • fn new(positions: &[[f32; 3]], indices: &[u32]) -> Result<TriMesh, MeshError> a mesh from positions and a triangle list (three indices per triangle), with [MeshOptions]' defaults
  • fn with_options(positions: &[[f32; 3]], indices: &[u32], opt: &MeshOptions) -> Result<TriMesh, MeshError> a mesh as [TriMesh::new] makes it, with the threshold of the active edges and the leaf size of opt
  • fn triangle_count(&self) -> usize the number of triangles in the caller's list (with the ones left out for having no area)
  • fn stored_triangle_count(&self) -> usize the number of triangles in the tree
  • fn vertices(&self) -> &[[f32; 3]] the positions as given, in the mesh's frame
  • fn aabb(&self) -> Aabb the box around the mesh in its own frame
  • fn nodes(&self) -> &[BvhNode] the tree's nodes (the root first)
  • fn depth(&self) -> u32 the depth of the tree
  • fn triangles(&self) -> impl Iterator<Item = (u32, [Vec3; 3])> + '_ the stored triangles in tree order: their caller's index and corners (for brute-force references)
  • fn edge_flags(&self, id: u32) -> Option<u8> the active-edge bits of the triangle with the caller's index id (bit e: edge e from the front, bit 3 + e: from the back), None when it was left out
  • fn memory_bytes(&self) -> usize the bytes the mesh holds (vertices, triangles, ids, flags, nodes)

Source: docs/API.md in the JFLib repository.