tri-mesh

Docs.rs Build Status

A triangle mesh data structure including basic operations.

Why another triangle mesh data structure crate you might ask. Well, if you want a more feature complete crate than halfedgemesh and a less generic crate than plexus, then tri-mesh is probably something for you!

Examples

Features

Please, see the documentation for more details.

Usage

Add the following to your Cargo.toml: toml [dependencies] tri-mesh = "0.3.0"

I have a mesh without normals, how can I use tri-mesh to compute them?

```rust use tri_mesh::prelude::*;

fn main() { // Construct a mesh from indices and positions buffers. let indices: Vec = vec![0, 1, 2, 0, 2, 3, 0, 3, 1]; let positions: Vec = vec![0.0, 0.0, 0.0, 1.0, 0.0, -0.5, -1.0, 0.0, -0.5, 0.0, 0.0, 1.0]; let mesh = MeshBuilder::new().withindices(indices).withpositions(positions).build().unwrap();

// Get the indices, positions and normal buffers
let indices_out = mesh.indices_buffer();
let positions_out = mesh.positions_buffer();
let normals_out = mesh.normals_buffer();

} ```

I need the bounding box of my mesh, how can I get that?

```rust use tri_mesh::prelude::*;

fn main() { // Construct any mesh, this time, we will construct a simple icosahedron let mesh = MeshBuilder::new().icosahedron().build().unwrap();

// Compute the extreme coordinates which defines the axis aligned bounding box..
let (min_coordinates, max_coordinates) = mesh.extreme_coordinates();

// .. or construct an actual mesh representing the axis aligned bounding box
let aabb = mesh.axis_aligned_bounding_box();

// Export the bounding box to an obj file
std::fs::write("foo.obj", mesh.parse_as_obj()).unwrap();

} ```

I want to stitch two meshes together, how do I do that?

```rust use tri_mesh::prelude::*;

fn main() { // Construct two meshes let mut mesh1 = MeshBuilder::new().cube().build().unwrap(); let mut mesh2 = MeshBuilder::new().cube().build().unwrap(); mesh2.translate(vec3(0.5, 0.5, 0.5));

// Split the two meshes at their intersection creating two sets of sub meshes
let (mut meshes1, mut meshes2) = mesh1.split_at_intersection(&mut mesh2);

// Choose two sub meshes to merge (here we just choose one sub mesh from each of the original meshes)
let mut result = meshes1.first().unwrap().clone();
result.merge_with(meshes2.first().unwrap()).unwrap();

} ```

How can I use tri-mesh to compute my own very special curvature measure?

```rust use tri_mesh::prelude::*;

fn main() { // Construct any mesh, for simplicity, let's use a cube mesh let mesh = MeshBuilder::new().cube().build().unwrap();

let mut curvature_measure = 0.0;
// Let's say that the curvature measure is a sum of a curvature measure for each vertex
// which means we need to visit all vertices
for vertex_id in mesh.vertex_iter()
{
    // Let's say that to compute the curvature of one vertex we need to visit the neighbouring faces
    // We will do that by iterating the half-edges pointing away from the vertex ..
    let mut curvature_measure_vertex = 0.0;
    for halfedge_id in mesh.vertex_halfedge_iter(vertex_id) {
        // .. and then create a walker from that halfedge and then get the face pointed to by that walker
        if let Some(face_id) = mesh.walker_from_halfedge(halfedge_id).face_id() {
            // Finally, insert the code for computing your special vertex curvature measure right here!
            // curvature_measure_vertex += ??; 
        }
    }
    curvature_measure += curvature_measure_vertex;
}

} ```