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!

Check out this morph tool (and source code) for a live example of using this crate.

Usage

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

Features

Please, see the documentation for more details.

Examples

Example #1: Computing the vertex normals

```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's say that we want to visualise this model, then we need the indices and position buffer..
let indices = mesh.indices_buffer();
let positions = mesh.positions_buffer();

// .. but wait, we also need the normals. 
// Let's compute the normals for each vertex and put it in an array..
let mut normals = Vec::with_capacity(3 * mesh.no_vertices());
for vertex_id in mesh.vertex_iter()
{
    let mut normal = Vec3::zero();
    for halfedge_id in mesh.vertex_halfedge_iter(vertex_id) {
        if let Some(face_id) = mesh.walker_from_halfedge(halfedge_id).face_id() {
            normal += mesh.face_normal(face_id)
        }
    }
    normal.normalize();
    normals.push(normal.x);
    normals.push(normal.y);
    normals.push(normal.z);
}

// .. we could simplify that using the vertex_normal method..
let mut normals = Vec::with_capacity(3 * mesh.no_vertices());
for vertex_id in mesh.vertex_iter()
{
    let normal = mesh.vertex_normal(vertex_id);
    normals.push(normal.x);
    normals.push(normal.y);
    normals.push(normal.z);
}

// .. or simply just use the built-in method
let normals = mesh.normals_buffer();

} ```

Example #2: Computing the bounding box

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

fn main() { // Construct any mesh, this time, we will construct a mesh from positions and indices 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();

// Compute the extreme coordinates which defines the axis aligned bounding box..
let mut min_coordinates = vec3(std::f32::MAX, std::f32::MAX, std::f32::MAX);
let mut max_coordinates = vec3(std::f32::MIN, std::f32::MIN, std::f32::MIN);
for vertex_id in mesh.vertex_iter()
{
    let position = mesh.vertex_position(vertex_id);
    for i in 0..3 {
        min_coordinates[i] = min_coordinates[i].min(position[i]);
        max_coordinates[i] = max_coordinates[i].max(position[i]);
    }
}

// .. or use the built-in method..
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();
assert_eq!((min_coordinates, max_coordinates), aabb.extreme_coordinates());

} ```

Please, see the documentation for more examples.