Skip to main content

Mesh

Struct Mesh 

Source
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

Source

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().

Source

pub fn triangle(&self, i: usize) -> Triangle

The i-th face as a Triangle.

§Panics

Panics when i >= self.indices.len().

Source

pub fn triangles(&self) -> impl Iterator<Item = Triangle> + '_

Iterator over all faces as triangles.

Source

pub fn to_triangles(&self) -> Vec<Triangle>

All faces collected as triangles.

Source

pub fn face_normals(&self) -> Vec<Vec3>

Unit normal of every face (zero vector for degenerate faces).

Source

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.

Source

pub fn surface_area(&self) -> f64

Total surface area (sum of face areas).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn bounding_box(&self) -> Aabb

Axis-aligned bounding box of all vertices.

§Panics

Panics when the mesh has no vertices.

Source

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.

Source

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.

Source

pub fn translate(&mut self, offset: Vec3)

Translates every vertex by offset.

Source

pub fn scale(&mut self, factor: f64)

Uniformly scales every vertex about the origin.

Source

pub fn rotate(&mut self, q: &Quaternion)

Rotates vertices (and normals) about the origin.

Source

pub fn merge(&mut self, other: &Mesh)

Appends another mesh. Optional attributes are kept only when both meshes carry them.

Source

pub fn flip_normals(&mut self)

Reverses the winding of every face and negates stored normals.

Source

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.

Source

pub fn remove_unused_vertices(&mut self)

Removes vertices referenced by no face, compacting attributes.

Source

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.

Source

pub fn edges(&self) -> Vec<(usize, usize)>

Unique undirected edges as sorted (min, max) index pairs, lexicographically ordered.

Source

pub fn adjacency(&self) -> Vec<Vec<usize>>

Vertex-to-neighbor-vertices adjacency (each list sorted, deduplicated).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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.

Source

pub fn to_stl_ascii(&self) -> String

Serializes to ASCII STL (facet normals recomputed from geometry).

Trait Implementations§

Source§

impl Clone for Mesh

Source§

fn clone(&self) -> Mesh

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mesh

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Mesh

Source§

fn eq(&self, other: &Mesh) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Mesh

Auto Trait Implementations§

§

impl Freeze for Mesh

§

impl RefUnwindSafe for Mesh

§

impl Send for Mesh

§

impl Sync for Mesh

§

impl Unpin for Mesh

§

impl UnsafeUnpin for Mesh

§

impl UnwindSafe for Mesh

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.