Struct tri_mesh::Mesh

source ·
pub struct Mesh { /* private fields */ }
Expand description

A representation of a triangle mesh which is efficient for calculating on and making changes to a mesh.

Use Mesh::new to construct a new mesh. Use Mesh::export to export the mesh to a format that is efficient for visualization.

Basic functionality:

Simple operations

Advanced operations

Implementations

Constructs a new Mesh from a three_d_asset::TriMesh which can either be manually constructed or loaded via the three_d_asset::io module.

Examples
let model: three_d_asset::Model =
    three_d_asset::io::load_and_deserialize("cube.obj").expect("Failed loading asset");
let mesh = Mesh::new(&model.geometries[0]);
let mesh = Mesh::new(&three_d_asset::TriMesh::sphere(4));
let mesh = Mesh::new(&three_d_asset::TriMesh {
    positions: three_d_asset::Positions::F64(vec![vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0)]),
    ..Default::default()
});

Exports the Mesh into a three_d_asset::TriMesh that contain the raw buffer data. The three_d_asset::TriMesh can then for example be visualized or saved to disk (using the three_d_asset::io module).

Returns the vertex position.

Returns the number of vertices in the mesh.

Returns the number of edges in the mesh.

Returns the number of half-edges in the mesh.

Returns the number of faces in the mesh.

Appends the other mesh to this mesh without creating a connection between them. Use merge_with if merging of overlapping primitives is desired, thereby creating a connection. All the primitives of the other mesh are copied to the current mesh and the other mesh is therefore not changed.

Merges overlapping faces, edges and vertices if it is possible without creating a non-manifold mesh.

Iterator over the vertex ids.

Examples
let mut sum_vertex_positions = Vec3::zero();
for vertex_id in mesh.vertex_iter() {
    sum_vertex_positions += mesh.vertex_position(vertex_id);
}

Iterator over the half-edge ids.

Note: Each edge is visited two times, one for each half-edge. If you want to visit the edges only once, then use edge_iter instead.

Examples
let mut halfedge_length_average = 0.0;
let mut i = 0;
for halfedge_id in mesh.halfedge_iter() {
    halfedge_length_average += mesh.edge_length(halfedge_id);
    i += 1;
}
halfedge_length_average /= i as f64;

Iterator over the edges given as a half-edge id.

Note: Each edge is visited once. If you want to visit both half-edges of an edge, then use halfedge_iter instead.

Examples
let mut edge_length_average = 0.0;
let mut i = 0;
for halfedge_id in mesh.edge_iter() {
    edge_length_average += mesh.edge_length(halfedge_id);
    i += 1;
}
edge_length_average /= i as f64;

Iterator over the face ids.

Examples
let mut sum_face_area = 0.0;
for face_id in mesh.face_iter() {
    sum_face_area += mesh.face_area(face_id);
}

Iterator over the half-edges which starts in the given vertex, ie. the one-ring.

Note: If the given vertex is the only connection between two or more separate sets of faces, then this iterator will only iterate the half-edges in one of the sets. If the vertex is on the boundary, all half-edges are visited.

Examples
let mut one_ring_average_position = Vec3::zero();
let mut i = 0;
for halfedge_id in mesh.vertex_halfedge_iter(vertex_id) {
    let walker = mesh.walker_from_halfedge(halfedge_id);
    one_ring_average_position += mesh.vertex_position(walker.vertex_id().unwrap());
    i = i+1;
}
one_ring_average_position /= i as f64;

Iterator over the three half-edges connected to the given face.

Examples
let mut face_circumference = 0.0f64;
for halfedge_id in mesh.face_halfedge_iter(face_id) {
    face_circumference += mesh.edge_length(halfedge_id);
}

Traversal

Methods to construct a Walker which is used for easy and efficient traversal of the mesh. See Walker for more information and examples. Also see Connectivity for common connectivity utility functionality.

Creates a Walker at the half-edge pointed to by the given vertex.

Creates a Walker at the given half-edge.

Creates a Walker at the half-edge pointed to by the given face.

Moves the vertex to the specified position.

Flip the given edge such that the edge after the flip is connected to the other pair of the four vertices connected to the two adjacent faces.

  /\          /|\
 /  \        / | \
/____\  --> /  |  \
\    /      \  |  /
 \  /        \ | /
  \/          \|/
Error

Returns an error if trying to flip an edge on the boundary or the flip will connect two vertices that are already connected by another edge.

Split the given edge into two. Returns the id of the new vertex positioned at the given position.

Split the given face into three new faces. Returns the id of the new vertex positioned at the given position.

Collapses the given edge. Consequently, the to adjacent faces are removed and the two adjacent vertices are merged into one vertex which position is the average of the original vertex positions. Returns the merged vertex.

Note: This might make some faces degenerate or produce edges and vertices that are not connected.

Adds a vertex to the mesh which is not connected to anything. Usually used in combination with Mesh::add_face.

Adds a face to the mesh and connects it to the given vertices which can be created using the Mesh::add_vertex method. Also creates edges between the vertices if they do not already exist.

Removes the given face and also the adjacent edges and vertices if they are not connected to any other face.

Removes edges and vertices that are not connected to any face.

Flip the orientation of all faces in the mesh, ie. such that the normal points in the opposite direction.

Fix the orientation of all faces in the mesh such that the orientation of each pair of neighbouring faces is aligned.

Returns whether or not the mesh is closed, ie. contains no holes.

Returns the connecting edge between the two vertices or None if no edge is found.

Note: This method assumes that the mesh is properly connected. See vertex_halfedge_iter for more information.

Returns whether or not the vertex is on a boundary.

Returns whether or not the edge is on a boundary.

Returns the vertex id of the two adjacent vertices to the given edge.

Returns the vertex id of the two adjacent vertices to the given edge and ordered such that ordered_edge_vertices.0 < ordered_edge_vertices.1.

Returns the vertex id of the three connected vertices to the given face.

Returns the vertex id of the three connected vertices to the given face and ordered such that ordered_face_vertices.0 < ordered_face_vertices.1 < ordered_face_vertices.2.

Returns the vertex position.

Returns the normal of the vertex given as the average of the normals of the neighbouring faces.

Returns the two positions of the vertices of either end of the given halfedge.

Returns the length of the specified edge

Returns the squared length of the specified edge

Returns the positions of the face vertices.

Returns the unnormalized normal of the face.

Returns the normal of the face.

Returns the area of the face.

Returns the center of the face given as the average of its vertex positions.

Moves the vertex to the specified position.

Moves the vertex by the specified vector, i.e. the new position is mesh.vertex_position(vertex_id) + value.

Scales the entire mesh by multiplying scale to each vertex position.

Examples
    let mut mesh: Mesh = three_d_asset::TriMesh::sphere(4).into();
    mesh.scale(2.0);

Scales the entire mesh by multiplying scale_x to the x component of each vertex position, scale_y to the y component and scale_z to the z component.

Examples
    let mut mesh: Mesh = three_d_asset::TriMesh::sphere(4).into();
    mesh.non_uniform_scale(2.0, 1.0, 1.0);

Translates the entire mesh by applying the translation to each vertex position.

Examples
    let mut mesh: Mesh = three_d_asset::TriMesh::sphere(4).into();
    mesh.translate(vec3(2.5, -1.0, 0.0));

Rotates the entire mesh by applying the given rotation to each vertex position.

Examples
    let mut mesh: Mesh = three_d_asset::TriMesh::sphere(4).into();
    mesh.apply_transformation(Mat4::from_angle_y(degrees(360.0)));

Transforms the entire mesh by applying the transformation to each vertex position.

Examples
    let mut mesh: Mesh = three_d_asset::TriMesh::sphere(4).into();
    mesh.apply_transformation(Mat4::from_translation(vec3(2.5, -1.0, 0.0)));

Returns the smallest axis aligned box which contains the entire mesh, ie. the axis aligned bounding box.

WARNING: DO NOT USE IN PRODUCTION!

This method tests if the mesh is valid, i.e. has correct connectivity and orientation and contains no degenerate triangles. Intended only to be used in development and unit tests.

Errors

If the mesh is not valid, an Error::MeshIsInvalid error with a description of the problem is returned.

Moves the vertices to pos + factor * (avg_pos - pos) where pos is the current position and avg_pos is the average position of the neighbouring vertices.

Collapse an edge of faces which has an area smaller than area_threshold.

Flip all edges in the mesh

  • which is not on the boundary
  • where the flip will improve the sum of the quality of the two faces adjacent to the edge (The face quality is given as the circumscribed radius divided by the inscribed radius)
  • where the dot product between the normals of the adjacent faces is smaller than flattness_threshold (1: Completely flat, 0: 90 degrees angle between normals)
  • where the flip will not result in inverted triangles

Finds the connected set of faces starting from the given face.

Finds all the sets of connected faces.

Finds the connected set of faces starting from the given face and limited by the given limit function.

Finds all the sets of connected faces which are limited by the given limit function.

Find the Intersection between any face in the mesh and the given ray. If the ray intersects multiple faces, the face closest to the starting point in the direction of the ray is returned. If no faces are intersected, None is returned.

Find the Intersection between the given face and ray. If the face is not intersected by the ray, None is returned.

Find the Intersection between the given face and line piece. If the face is not intersected by the line piece, None is returned.

Note: Intersections, where the line piece is in the plane spanned by the face, are not yet fully handled.

Find the Intersection between the given vertex and the point. If the vertex is not close to the point, None is returned.

Find the Intersection (the primitive is either a vertex or edge) between the given edge and the point. If the edge is not close to the point, None is returned.

Find the Intersection (the primitive is either a vertex, edge or face) between the given face and the point. If the face is not close to the point, None is returned.

Merges the mesh together with the other mesh. The other mesh primitives are copied to the current mesh (and other is therefore not changed) followed by merging of overlapping primitives.

Clones a subset of this mesh defined by the is_included function.

Splits the mesh into subsets bounded by the edges where the is_at_split function returns true.

Splits the two meshes into subsets bounded by the intersection between the two meshes.

Splits the primitives of the two meshes at the intersection between the two meshes.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.