pub struct Mesh {
pub vertices: Vec<Vec3>,
pub indices: Vec<[usize; 3]>,
pub normals: Option<Vec<Vec3>>,
pub uvs: Option<Vec<Vec2>>,
}Expand description
Indexed triangle mesh with optional per-vertex normals and UVs.
normals and uvs, when present, are parallel to vertices.
Fields§
§vertices: Vec<Vec3>§indices: Vec<[usize; 3]>§normals: Option<Vec<Vec3>>§uvs: Option<Vec<Vec2>>Implementations§
Source§impl Mesh
impl Mesh
Sourcepub fn new(
vertices: Vec<Vec3>,
indices: Vec<[usize; 3]>,
) -> Result<Self, GeomError>
pub fn new( vertices: Vec<Vec3>, indices: Vec<[usize; 3]>, ) -> Result<Self, GeomError>
Builds a mesh, validating that every index is in range.
§Errors
Returns GeomError::InvalidArgument when a face references a
vertex index >= vertices.len().
Sourcepub fn triangles(&self) -> impl Iterator<Item = Triangle> + '_
pub fn triangles(&self) -> impl Iterator<Item = Triangle> + '_
Iterator over all faces as triangles.
Sourcepub fn to_triangles(&self) -> Vec<Triangle>
pub fn to_triangles(&self) -> Vec<Triangle>
All faces collected as triangles.
Sourcepub fn face_normals(&self) -> Vec<Vec3>
pub fn face_normals(&self) -> Vec<Vec3>
Unit normal of every face (zero vector for degenerate faces).
Sourcepub fn compute_vertex_normals(&mut self)
pub fn compute_vertex_normals(&mut self)
Computes area-weighted per-vertex normals and stores them in
self.normals.
Each face contributes its (unnormalized) cross product, whose magnitude is twice the face area, so large faces dominate.
Sourcepub fn surface_area(&self) -> f64
pub fn surface_area(&self) -> f64
Total surface area (sum of face areas).
Sourcepub fn volume(&self) -> f64
pub fn volume(&self) -> f64
Signed enclosed volume via the divergence theorem: V = Σ aᵢ · (bᵢ × cᵢ) / 6. Positive for a closed mesh with outward-facing (counterclockwise) triangles.
Sourcepub fn centroid(&self) -> Vec3
pub fn centroid(&self) -> Vec3
Centroid of the enclosed volume (center of mass at uniform density), from the signed tetrahedron decomposition against the origin.
§Panics
Panics when the signed volume is zero.
Sourcepub fn center_of_mass_surface(&self) -> Vec3
pub fn center_of_mass_surface(&self) -> Vec3
Area-weighted centroid of the surface (center of mass of a thin shell of uniform surface density).
§Panics
Panics when the total surface area is zero.
Sourcepub fn inertia_tensor(&self, density: f64) -> Mat3
pub fn inertia_tensor(&self, density: f64) -> Mat3
Inertia tensor about the center of mass of the enclosed solid at the given uniform density, by signed tetrahedron decomposition (equivalent to Mirtich’s polyhedral mass-property integrals).
Each face forms the tet (0, a, b, c); its second-moment (covariance) integral is det(J) · J C J^T where J = [a b c] and C is the canonical tetrahedron covariance (1/60 diagonal, 1/120 off-diagonal). Source: Mirtich, “Fast and Accurate Computation of Polyhedral Mass Properties”, JGT 1996.
§Panics
Panics when the signed volume is zero.
Sourcepub fn principal_inertia(&self, density: f64) -> ([f64; 3], Mat3)
pub fn principal_inertia(&self, density: f64) -> ([f64; 3], Mat3)
Principal moments of inertia (descending) and the rotation whose columns are the principal axes.
§Panics
Panics when the signed volume is zero.
Sourcepub fn bounding_box(&self) -> Aabb
pub fn bounding_box(&self) -> Aabb
Sourcepub fn bounding_sphere(&self) -> Sphere
pub fn bounding_sphere(&self) -> Sphere
Approximate minimal bounding sphere by Ritter’s two-pass algorithm (at most ~5% larger than optimal).
§Panics
Panics when the mesh has no vertices.
Sourcepub fn transform(&mut self, m: &Mat4)
pub fn transform(&mut self, m: &Mat4)
Applies a general 4x4 transform to vertices; normals are mapped by the normal matrix (inverse transpose) and renormalized.
§Panics
Panics (inside Mat4::normal_matrix) when the mesh has
normals and the linear part of m is singular.
Sourcepub fn rotate(&mut self, q: &Quaternion)
pub fn rotate(&mut self, q: &Quaternion)
Rotates vertices (and normals) about the origin.
Sourcepub fn merge(&mut self, other: &Mesh)
pub fn merge(&mut self, other: &Mesh)
Appends another mesh. Optional attributes are kept only when both meshes carry them.
Sourcepub fn flip_normals(&mut self)
pub fn flip_normals(&mut self)
Reverses the winding of every face and negates stored normals.
Sourcepub fn weld_vertices(&mut self, tol: f64) -> usize
pub fn weld_vertices(&mut self, tol: f64) -> usize
Merges vertices closer than tol (grid hashing with neighbor
search, so any pair within tol of a common representative
merges). Faces left with a repeated index are removed; stored
normals and UVs are dropped. Returns the number of vertices
removed.
§Panics
Panics unless tol > 0 and finite.
Sourcepub fn remove_unused_vertices(&mut self)
pub fn remove_unused_vertices(&mut self)
Removes vertices referenced by no face, compacting attributes.
Sourcepub fn remove_degenerate_triangles(&mut self, area_tol: f64) -> usize
pub fn remove_degenerate_triangles(&mut self, area_tol: f64) -> usize
Removes faces with area below area_tol or with repeated
indices; returns how many were removed.
Sourcepub fn edges(&self) -> Vec<(usize, usize)>
pub fn edges(&self) -> Vec<(usize, usize)>
Unique undirected edges as sorted (min, max) index pairs,
lexicographically ordered.
Sourcepub fn adjacency(&self) -> Vec<Vec<usize>>
pub fn adjacency(&self) -> Vec<Vec<usize>>
Vertex-to-neighbor-vertices adjacency (each list sorted, deduplicated).
Sourcepub fn face_adjacency(&self) -> Vec<[Option<usize>; 3]>
pub fn face_adjacency(&self) -> Vec<[Option<usize>; 3]>
For each face, the neighboring face across each of its edges
(v0,v1), (v1,v2), (v2,v0), or None on a boundary. When an
edge is shared by more than two faces, an arbitrary neighbor is
reported.
Sourcepub fn build_bvh(&self) -> Bvh
pub fn build_bvh(&self) -> Bvh
Builds a BVH over the faces (indices refer to face order).
§Panics
Panics when the mesh has no faces.
Sourcepub fn raycast(&self, r: &Ray, bvh: Option<&Bvh>) -> Option<(usize, RayHit)>
pub fn raycast(&self, r: &Ray, bvh: Option<&Bvh>) -> Option<(usize, RayHit)>
Nearest ray hit as (face index, hit). Pass a BVH built by
Mesh::build_bvh to accelerate; None falls back to brute
force.
Sourcepub fn sample_surface(&self, n: usize, rng: &mut Rng) -> Vec<Vec3>
pub fn sample_surface(&self, n: usize, rng: &mut Rng) -> Vec<Vec3>
Draws n points uniformly over the surface: faces are chosen
with probability proportional to area, positions by the
square-root barycentric warp.
§Panics
Panics when the total surface area is zero.
Sourcepub fn to_obj(&self) -> String
pub fn to_obj(&self) -> String
Serializes to Wavefront OBJ (1-indexed; vn/vt written when
present, referenced with the same index as the position).
Sourcepub fn from_obj(s: &str) -> Result<Self, GeomError>
pub fn from_obj(s: &str) -> Result<Self, GeomError>
Parses Wavefront OBJ. Faces with more than three corners are fan-triangulated. Normals and UVs are kept only when every face corner references the attribute with the same index as its position and the counts match; otherwise they are dropped. Negative (relative) indices are resolved against the counts seen so far.
§Errors
Returns GeomError::InvalidArgument on malformed numbers or
out-of-range indices.
Sourcepub fn to_stl_ascii(&self) -> String
pub fn to_stl_ascii(&self) -> String
Serializes to ASCII STL (facet normals recomputed from geometry).