4 releases
0.2.2 | Aug 15, 2024 |
---|---|
0.2.1 | Aug 15, 2024 |
0.2.0 | Aug 15, 2024 |
0.1.0 | Aug 13, 2024 |
#48 in Data formats
41 downloads per month
Used in mass-props
24KB
282 lines
Polyhedral Mass Properties
Calculates the mass properties (mass, center of mass and inertia matrix/tensor) of triangle meshes.
The algorithm is based on the paper "Computing the Moment of Inertia of a Solid Defined by a Triangle Mesh" by Michael Kallay (Code).
Documentation
Documentation with examples is available on docs.rs 📚️
Demo binary
The binary mass-props
uses this library to calculate the mass properties of meshes in glTF files.
You can try it out to test the output of this library.
lib.rs
:
Calculates the mass properties (mass, center of mass and inertia matrix/tensor) of triangle meshes.
The algorithm is based on the paper "Computing the Moment of Inertia of a Solid Defined by a Triangle Mesh" by Michael Kallay (Code).
How it works
Each triangle in the mesh has its own contribution (TriangleContrib
) to the mass properties of the whole mesh.
These contributions can be calculated independently and then summed up (O(N)
where N
is the number of triangles in the mesh).
The sum can then be converted to the actual mass properties (MassProperties
) with MassProperties::from_contrib_sum
(O(1)
).
Examples
With vertices: &[[f64; 3]]
and indices: &[usize]
:
#
#
#
let contribution_sum = indices
.chunks_exact(3)
.map(|indices| {
TriangleContrib::new(
vertices[indices[0]],
vertices[indices[1]],
vertices[indices[2]],
)
})
.sum();
let mass_properties = MassProperties::from_contrib_sum(contribution_sum);
Iterator
If indices
is an iterator over usize
with a known length (e.g. implements ExactSizeIterator
), you can do the following without needing to collect the indices first:
#
#
#
let contribution_sum = (0..indices.len() / 3)
.map(|_| {
TriangleContrib::new(
vertices[indices.next().unwrap()],
vertices[indices.next().unwrap()],
vertices[indices.next().unwrap()],
)
})
.sum();
let mass_properties = MassProperties::from_contrib_sum(contribution_sum);
Multi-threading with Rayon
Calculating the mass properties can be easily multi-threaded by using Rayon.
#
#
#
use rayon::prelude::*;
let contribution_sum = indices
.par_chunks_exact(3) // par_chunks_exact instead of chunks_exact
.map(|indices| {
TriangleContrib::new(
vertices[indices[0]],
vertices[indices[1]],
vertices[indices[2]],
)
})
.sum();
let mass_properties = MassProperties::from_contrib_sum(contribution_sum);
Feature flags
std
: Enable the usage of the standard library. Enabled byfma
andgltf
.fma
: Use FMA instructions for better performance. Needs to be compiled withRUSTFLAGS="-C target-cpu=native"
.serde
: Implement theSerialize
andDeserialize
traits forInertia
andMassProperties
.gltf
: Enable calculating the mass properties for gltf/glb files usingMassProperties::from_gltf
.
Dependencies
~0–0.8MB
~17K SLoC