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
sourceimpl Mesh
impl Mesh
sourcepub fn new(input: &TriMesh) -> Self
pub fn new(input: &TriMesh) -> Self
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()
});
sourcepub fn export(&self) -> TriMesh
pub fn export(&self) -> TriMesh
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).
sourceimpl Mesh
impl Mesh
sourcepub fn no_vertices(&self) -> usize
pub fn no_vertices(&self) -> usize
Returns the number of vertices in the mesh.
sourcepub fn no_halfedges(&self) -> usize
pub fn no_halfedges(&self) -> usize
Returns the number of half-edges in the mesh.
sourceimpl Mesh
impl Mesh
sourcepub fn append(&mut self, other: &Self)
pub fn append(&mut self, other: &Self)
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.
sourceimpl Mesh
impl Mesh
sourcepub fn merge_overlapping_primitives(&mut self)
pub fn merge_overlapping_primitives(&mut self)
Merges overlapping faces, edges and vertices if it is possible without creating a non-manifold mesh.
sourceimpl Mesh
impl Mesh
sourcepub fn vertex_iter(&self) -> VertexIter
pub fn vertex_iter(&self) -> VertexIter
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);
}
sourcepub fn halfedge_iter(&self) -> HalfEdgeIter
pub fn halfedge_iter(&self) -> HalfEdgeIter
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;
sourcepub fn edge_iter(&self) -> EdgeIter<'_>ⓘNotable traits for EdgeIter<'a>impl<'a> Iterator for EdgeIter<'a> type Item = HalfEdgeID;
pub fn edge_iter(&self) -> EdgeIter<'_>ⓘNotable traits for EdgeIter<'a>impl<'a> Iterator for EdgeIter<'a> type Item = HalfEdgeID;
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;
sourcepub fn face_iter(&self) -> FaceIter
pub fn face_iter(&self) -> FaceIter
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);
}
sourcepub fn vertex_halfedge_iter(&self, vertex_id: VertexID) -> VertexHalfedgeIter<'_>ⓘNotable traits for VertexHalfedgeIter<'a>impl<'a> Iterator for VertexHalfedgeIter<'a> type Item = HalfEdgeID;
pub fn vertex_halfedge_iter(&self, vertex_id: VertexID) -> VertexHalfedgeIter<'_>ⓘNotable traits for VertexHalfedgeIter<'a>impl<'a> Iterator for VertexHalfedgeIter<'a> type Item = HalfEdgeID;
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;
sourcepub fn face_halfedge_iter(&self, face_id: FaceID) -> FaceHalfedgeIter<'_>ⓘNotable traits for FaceHalfedgeIter<'a>impl<'a> Iterator for FaceHalfedgeIter<'a> type Item = HalfEdgeID;
pub fn face_halfedge_iter(&self, face_id: FaceID) -> FaceHalfedgeIter<'_>ⓘNotable traits for FaceHalfedgeIter<'a>impl<'a> Iterator for FaceHalfedgeIter<'a> type Item = HalfEdgeID;
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);
}
sourceimpl Mesh
impl Mesh
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.
sourcepub fn walker_from_vertex(&self, vertex_id: VertexID) -> Walker<'_>
pub fn walker_from_vertex(&self, vertex_id: VertexID) -> Walker<'_>
Creates a Walker at the half-edge pointed to by the given vertex.
sourcepub fn walker_from_halfedge(&self, halfedge_id: HalfEdgeID) -> Walker<'_>
pub fn walker_from_halfedge(&self, halfedge_id: HalfEdgeID) -> Walker<'_>
Creates a Walker at the given half-edge.
sourcepub fn walker_from_face(&self, face_id: FaceID) -> Walker<'_>
pub fn walker_from_face(&self, face_id: FaceID) -> Walker<'_>
Creates a Walker at the half-edge pointed to by the given face.
sourceimpl Mesh
impl Mesh
sourcepub fn set_vertex_position(&mut self, vertex_id: VertexID, value: Vec3)
pub fn set_vertex_position(&mut self, vertex_id: VertexID, value: Vec3)
Moves the vertex to the specified position.
sourcepub fn flip_edge(&mut self, halfedge_id: HalfEdgeID) -> Result<(), Error>
pub fn flip_edge(&mut self, halfedge_id: HalfEdgeID) -> Result<(), Error>
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.
sourcepub fn split_edge(&mut self, halfedge_id: HalfEdgeID, position: Vec3) -> VertexID
pub fn split_edge(&mut self, halfedge_id: HalfEdgeID, position: Vec3) -> VertexID
Split the given edge into two. Returns the id of the new vertex positioned at the given position.
sourcepub fn split_face(&mut self, face_id: FaceID, position: Vec3) -> VertexID
pub fn split_face(&mut self, face_id: FaceID, position: Vec3) -> VertexID
Split the given face into three new faces. Returns the id of the new vertex positioned at the given position.
sourcepub fn collapse_edge(&mut self, halfedge_id: HalfEdgeID) -> VertexID
pub fn collapse_edge(&mut self, halfedge_id: HalfEdgeID) -> VertexID
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.
sourcepub fn add_vertex(&mut self, position: Vec3) -> VertexID
pub fn add_vertex(&mut self, position: Vec3) -> VertexID
Adds a vertex to the mesh which is not connected to anything. Usually used in combination with Mesh::add_face.
sourcepub fn add_face(
&mut self,
vertex_id1: VertexID,
vertex_id2: VertexID,
vertex_id3: VertexID
) -> FaceID
pub fn add_face(
&mut self,
vertex_id1: VertexID,
vertex_id2: VertexID,
vertex_id3: VertexID
) -> FaceID
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.
sourcepub fn remove_face(&mut self, face_id: FaceID)
pub fn remove_face(&mut self, face_id: FaceID)
Removes the given face and also the adjacent edges and vertices if they are not connected to any other face.
sourcepub fn remove_lonely_primitives(&mut self)
pub fn remove_lonely_primitives(&mut self)
Removes edges and vertices that are not connected to any face.
sourceimpl Mesh
impl Mesh
sourcepub fn flip_orientation(&mut self)
pub fn flip_orientation(&mut self)
Flip the orientation of all faces in the mesh, ie. such that the normal points in the opposite direction.
sourcepub fn fix_orientation(&mut self)
pub fn fix_orientation(&mut self)
Fix the orientation of all faces in the mesh such that the orientation of each pair of neighbouring faces is aligned.
sourceimpl Mesh
impl Mesh
sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns whether or not the mesh is closed, ie. contains no holes.
sourcepub fn connecting_edge(
&self,
vertex_id1: VertexID,
vertex_id2: VertexID
) -> Option<HalfEdgeID>
pub fn connecting_edge(
&self,
vertex_id1: VertexID,
vertex_id2: VertexID
) -> Option<HalfEdgeID>
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.
sourcepub fn is_vertex_on_boundary(&self, vertex_id: VertexID) -> bool
pub fn is_vertex_on_boundary(&self, vertex_id: VertexID) -> bool
Returns whether or not the vertex is on a boundary.
sourcepub fn is_edge_on_boundary(&self, halfedge_id: HalfEdgeID) -> bool
pub fn is_edge_on_boundary(&self, halfedge_id: HalfEdgeID) -> bool
Returns whether or not the edge is on a boundary.
sourcepub fn edge_vertices(&self, halfedge_id: HalfEdgeID) -> (VertexID, VertexID)
pub fn edge_vertices(&self, halfedge_id: HalfEdgeID) -> (VertexID, VertexID)
Returns the vertex id of the two adjacent vertices to the given edge.
sourcepub fn ordered_edge_vertices(
&self,
halfedge_id: HalfEdgeID
) -> (VertexID, VertexID)
pub fn ordered_edge_vertices(
&self,
halfedge_id: HalfEdgeID
) -> (VertexID, VertexID)
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
.
sourceimpl Mesh
impl Mesh
sourcepub fn vertex_position(&self, vertex_id: VertexID) -> Vec3
pub fn vertex_position(&self, vertex_id: VertexID) -> Vec3
Returns the vertex position.
sourcepub fn vertex_normal(&self, vertex_id: VertexID) -> Vec3
pub fn vertex_normal(&self, vertex_id: VertexID) -> Vec3
Returns the normal of the vertex given as the average of the normals of the neighbouring faces.
sourceimpl Mesh
impl Mesh
sourcepub fn edge_positions(&self, halfedge_id: HalfEdgeID) -> (Vec3, Vec3)
pub fn edge_positions(&self, halfedge_id: HalfEdgeID) -> (Vec3, Vec3)
Returns the two positions of the vertices of either end of the given halfedge.
sourcepub fn edge_length(&self, halfedge_id: HalfEdgeID) -> f64
pub fn edge_length(&self, halfedge_id: HalfEdgeID) -> f64
Returns the length of the specified edge
sourcepub fn edge_sqr_length(&self, halfedge_id: HalfEdgeID) -> f64
pub fn edge_sqr_length(&self, halfedge_id: HalfEdgeID) -> f64
Returns the squared length of the specified edge
sourceimpl Mesh
impl Mesh
sourcepub fn face_positions(&self, face_id: FaceID) -> (Vec3, Vec3, Vec3)
pub fn face_positions(&self, face_id: FaceID) -> (Vec3, Vec3, Vec3)
Returns the positions of the face vertices.
sourcepub fn face_direction(&self, face_id: FaceID) -> Vec3
pub fn face_direction(&self, face_id: FaceID) -> Vec3
Returns the unnormalized normal of the face.
sourcepub fn face_normal(&self, face_id: FaceID) -> Vec3
pub fn face_normal(&self, face_id: FaceID) -> Vec3
Returns the normal of the face.
sourcepub fn face_center(&self, face_id: FaceID) -> Vec3
pub fn face_center(&self, face_id: FaceID) -> Vec3
Returns the center of the face given as the average of its vertex positions.
sourceimpl Mesh
impl Mesh
sourcepub fn move_vertex_to(&mut self, vertex_id: VertexID, value: Vec3)
pub fn move_vertex_to(&mut self, vertex_id: VertexID, value: Vec3)
Moves the vertex to the specified position.
sourcepub fn move_vertex_by(&mut self, vertex_id: VertexID, value: Vec3)
pub fn move_vertex_by(&mut self, vertex_id: VertexID, value: Vec3)
Moves the vertex by the specified vector, i.e. the new position is mesh.vertex_position(vertex_id) + value
.
sourcepub fn scale(&mut self, scale: f64)
pub fn scale(&mut self, scale: f64)
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);
sourcepub fn non_uniform_scale(&mut self, scale_x: f64, scale_y: f64, scale_z: f64)
pub fn non_uniform_scale(&mut self, scale_x: f64, scale_y: f64, scale_z: f64)
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);
sourcepub fn translate(&mut self, translation: Vec3)
pub fn translate(&mut self, translation: Vec3)
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));
sourcepub fn rotate(&mut self, rotation: Mat3)
pub fn rotate(&mut self, rotation: Mat3)
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)));
sourcepub fn apply_transformation(&mut self, transformation: Mat4)
pub fn apply_transformation(&mut self, transformation: Mat4)
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)));
sourceimpl Mesh
impl Mesh
sourcepub fn axis_aligned_bounding_box(&self) -> AxisAlignedBoundingBox
pub fn axis_aligned_bounding_box(&self) -> AxisAlignedBoundingBox
Returns the smallest axis aligned box which contains the entire mesh, ie. the axis aligned bounding box.
sourceimpl Mesh
impl Mesh
sourcepub fn is_valid(&self) -> Result<(), Error>
pub fn is_valid(&self) -> Result<(), Error>
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.
sourceimpl Mesh
impl Mesh
sourcepub fn smooth_vertices(&mut self, factor: f64)
pub fn smooth_vertices(&mut self, factor: f64)
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.
sourcepub fn collapse_small_faces(&mut self, area_threshold: f64)
pub fn collapse_small_faces(&mut self, area_threshold: f64)
Collapse an edge of faces which has an area smaller than area_threshold
.
sourcepub fn flip_edges(&mut self, flatness_threshold: f64)
pub fn flip_edges(&mut self, flatness_threshold: f64)
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
sourceimpl Mesh
impl Mesh
sourcepub fn connected_component(&self, start_face_id: FaceID) -> HashSet<FaceID>
pub fn connected_component(&self, start_face_id: FaceID) -> HashSet<FaceID>
Finds the connected set of faces starting from the given face.
sourcepub fn connected_components(&self) -> Vec<HashSet<FaceID>>
pub fn connected_components(&self) -> Vec<HashSet<FaceID>>
Finds all the sets of connected faces.
sourcepub fn connected_component_with_limit(
&self,
start_face_id: FaceID,
limit: &dyn Fn(HalfEdgeID) -> bool
) -> HashSet<FaceID>
pub fn connected_component_with_limit(
&self,
start_face_id: FaceID,
limit: &dyn Fn(HalfEdgeID) -> bool
) -> HashSet<FaceID>
Finds the connected set of faces starting from the given face and limited by the given limit function.
sourcepub fn connected_components_with_limit(
&self,
limit: &dyn Fn(HalfEdgeID) -> bool
) -> Vec<HashSet<FaceID>>
pub fn connected_components_with_limit(
&self,
limit: &dyn Fn(HalfEdgeID) -> bool
) -> Vec<HashSet<FaceID>>
Finds all the sets of connected faces which are limited by the given limit function.
sourceimpl Mesh
impl Mesh
sourcepub fn ray_intersection(
&self,
ray_start_point: &Vec3,
ray_direction: &Vec3
) -> Option<Intersection>
pub fn ray_intersection(
&self,
ray_start_point: &Vec3,
ray_direction: &Vec3
) -> Option<Intersection>
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.
sourcepub fn face_ray_intersection(
&self,
face_id: FaceID,
ray_start_point: &Vec3,
ray_direction: &Vec3
) -> Option<Intersection>
pub fn face_ray_intersection(
&self,
face_id: FaceID,
ray_start_point: &Vec3,
ray_direction: &Vec3
) -> Option<Intersection>
Find the Intersection between the given face and ray. If the face is not intersected by the ray, None is returned.
sourcepub fn face_line_piece_intersection(
&self,
face_id: FaceID,
point0: &Vec3,
point1: &Vec3
) -> Option<Intersection>
pub fn face_line_piece_intersection(
&self,
face_id: FaceID,
point0: &Vec3,
point1: &Vec3
) -> Option<Intersection>
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.
sourcepub fn vertex_point_intersection(
&self,
vertex_id: VertexID,
point: &Vec3
) -> Option<Intersection>
pub fn vertex_point_intersection(
&self,
vertex_id: VertexID,
point: &Vec3
) -> Option<Intersection>
Find the Intersection between the given vertex and the point. If the vertex is not close to the point, None is returned.
sourcepub fn edge_point_intersection(
&self,
halfedge_id: HalfEdgeID,
point: &Vec3
) -> Option<Intersection>
pub fn edge_point_intersection(
&self,
halfedge_id: HalfEdgeID,
point: &Vec3
) -> Option<Intersection>
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.
sourcepub fn face_point_intersection(
&self,
face_id: FaceID,
point: &Vec3
) -> Option<Intersection>
pub fn face_point_intersection(
&self,
face_id: FaceID,
point: &Vec3
) -> Option<Intersection>
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.
sourceimpl Mesh
impl Mesh
sourcepub fn merge_with(&mut self, other: &Self)
pub fn merge_with(&mut self, other: &Self)
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.
sourceimpl Mesh
impl Mesh
sourcepub fn clone_subset(&self, is_included: &dyn Fn(&Mesh, FaceID) -> bool) -> Mesh
pub fn clone_subset(&self, is_included: &dyn Fn(&Mesh, FaceID) -> bool) -> Mesh
Clones a subset of this mesh defined by the is_included function.
sourcepub fn split(&self, is_at_split: &dyn Fn(&Mesh, HalfEdgeID) -> bool) -> Vec<Mesh>
pub fn split(&self, is_at_split: &dyn Fn(&Mesh, HalfEdgeID) -> bool) -> Vec<Mesh>
Splits the mesh into subsets bounded by the edges where the is_at_split function returns true.
sourcepub fn split_at_intersection(
&mut self,
other: &mut Mesh
) -> (Vec<Mesh>, Vec<Mesh>)
pub fn split_at_intersection(
&mut self,
other: &mut Mesh
) -> (Vec<Mesh>, Vec<Mesh>)
Splits the two meshes into subsets bounded by the intersection between the two meshes.
sourcepub fn split_primitives_at_intersection(&mut self, other: &mut Mesh)
pub fn split_primitives_at_intersection(&mut self, other: &mut Mesh)
Splits the primitives of the two meshes at the intersection between the two meshes.