Conversion Functions
mesh_to_poly
def mesh_to_poly(mesh: Mesh) -> pv.PolyData
Converts a mesh to PyVista PolyData format.
Input mesh as tuple, PolyData, or object with vertices/faces or polydata attribute
Returns: pv.PolyData - PyVista PolyData representation of the mesh
from meshmash.utils import mesh_to_poly
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0]])
faces = np.array([[0, 1, 2]])
mesh = (vertices, faces)
poly = mesh_to_poly(mesh)
mesh_to_edges
def mesh_to_edges(mesh: Mesh) -> np.ndarray
Extracts all edges from a mesh.
Returns: np.ndarray - Array of shape (N, 2) containing edge vertex indices
from meshmash.utils import mesh_to_edges
edges = mesh_to_edges(mesh)
print(edges.shape) # (num_edges, 2)
mesh_to_adjacency
def mesh_to_adjacency(mesh: Mesh) -> csr_array
Computes the adjacency matrix of a mesh weighted by edge lengths.
Returns: csr_array - Sparse adjacency matrix with edge lengths as weights
from meshmash.utils import mesh_to_adjacency
adj = mesh_to_adjacency(mesh)
print(adj.shape) # (num_vertices, num_vertices)
poly_to_mesh
def poly_to_mesh(poly: pv.PolyData) -> Mesh
Converts PyVista PolyData back to mesh tuple format.
Returns: Mesh - Tuple of (vertices, faces)
from meshmash.utils import poly_to_mesh, mesh_to_poly
poly = mesh_to_poly(mesh)
vertices, faces = poly_to_mesh(poly)
Mesh Repair
fix_mesh
def fix_mesh(mesh, **kwargs)
Repairs mesh defects using pymeshfix.
Additional keyword arguments passed to MeshFix.repair()
Returns: Mesh - Repaired mesh as tuple of (vertices, faces)
from meshmash.utils import fix_mesh
fixed_mesh = fix_mesh(mesh, verbose=True)
Point and Projection Operations
project_points_to_mesh
def project_points_to_mesh(
points, mesh, distance_threshold=None, return_distances=False
)
Projects points to the nearest vertices on a mesh.
Array of points to project
Maximum distance for valid projections. Points beyond this distance are assigned index -1
Whether to return distances along with indices
Returns: np.ndarray or Tuple[np.ndarray, np.ndarray]
- If
return_distances=False: Array of nearest vertex indices
- If
return_distances=True: Tuple of (indices, distances)
from meshmash.utils import project_points_to_mesh
points = np.array([[0.1, 0.1, 0.0], [0.5, 0.5, 0.0]])
indices = project_points_to_mesh(points, mesh)
# With distance threshold
indices, distances = project_points_to_mesh(
points, mesh, distance_threshold=1.0, return_distances=True
)
compute_distances_to_point
def compute_distances_to_point(points, center_point)
Computes the Euclidean distance of each point to a center point.
Returns: np.ndarray - Array of distances
from meshmash.utils import compute_distances_to_point
points = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
center = np.array([0, 0, 0])
distances = compute_distances_to_point(points, center)
Component Analysis
def component_size_transform(mesh, indices=None)
Returns the size of each connected component in a mesh.
Vertex indices to query. If None, uses all vertices
Returns: np.ndarray - Array of component sizes for each queried index
from meshmash.utils import component_size_transform
component_sizes = component_size_transform(mesh)
print(f"Vertex 0 is in component of size: {component_sizes[0]}")
get_label_components
def get_label_components(mesh, labels) -> np.ndarray
Returns the connected components of a mesh which share the same label.
Returns: np.ndarray - Component labels where vertices with the same label and connectivity share the same component ID
from meshmash.utils import get_label_components
labels = np.array([0, 0, 1, 1, 1, 0])
components = get_label_components(mesh, labels)
mesh_connected_components
def mesh_connected_components(mesh, size_threshold=100)
Generator that yields individual connected components as separate meshes.
Minimum component size to include. If None, includes all components
Yields: Mesh - Individual connected component meshes
from meshmash.utils import mesh_connected_components
for component_mesh in mesh_connected_components(mesh, size_threshold=50):
print(f"Component has {len(component_mesh[0])} vertices")
largest_mesh_component
def largest_mesh_component(mesh: Mesh) -> Mesh
Extracts the largest connected component from a mesh.
Returns: Mesh - Largest connected component
from meshmash.utils import largest_mesh_component
largest = largest_mesh_component(mesh)
threshold_mesh_by_component_size
def threshold_mesh_by_component_size(mesh, size_threshold=100)
Filters mesh to only include components above a size threshold.
Minimum component size to keep
Returns: Tuple[Mesh, np.ndarray]
- Filtered mesh
- Indices of kept vertices in original mesh
from meshmash.utils import threshold_mesh_by_component_size
filtered_mesh, kept_indices = threshold_mesh_by_component_size(mesh, size_threshold=200)
Mesh Subsetting
subset_mesh_by_indices
def subset_mesh_by_indices(mesh: Mesh, indices: np.ndarray) -> Mesh
Creates a submesh containing only specified vertices and their associated faces.
Vertex indices to include (can be boolean mask or integer indices)
Returns: Mesh - Submesh containing only faces where all vertices are in the subset
from meshmash.utils import subset_mesh_by_indices
# Using integer indices
indices = np.array([0, 1, 2, 5, 6])
submesh = subset_mesh_by_indices(mesh, indices)
# Using boolean mask
mask = component_sizes > 100
submesh = subset_mesh_by_indices(mesh, mask)
rough_subset_mesh_by_indices
def rough_subset_mesh_by_indices(mesh: Mesh, indices: np.ndarray)
Creates a submesh including faces that have any vertex in the specified indices.
Vertex indices to include
Returns: Tuple[Mesh, np.ndarray]
- Submesh including faces with any vertex in indices
- New vertex indices in the submesh
from meshmash.utils import rough_subset_mesh_by_indices
indices = np.array([0, 1, 2])
submesh, new_indices = rough_subset_mesh_by_indices(mesh, indices)
Mesh Combination
combine_meshes
def combine_meshes(meshes)
Combines multiple meshes into a single mesh.
List of meshes to combine
Returns: Mesh - Combined mesh with all vertices and faces
from meshmash.utils import combine_meshes
mesh1 = (vertices1, faces1)
mesh2 = (vertices2, faces2)
mesh3 = (vertices3, faces3)
combined = combine_meshes([mesh1, mesh2, mesh3])
scale_mesh
def scale_mesh(mesh: Mesh, scale: float) -> Mesh
Scales a mesh by a given factor.
Returns: Mesh - Scaled mesh
from meshmash.utils import scale_mesh
# Scale mesh to 2x size
scaled_mesh = scale_mesh(mesh, 2.0)
# Scale mesh to half size
smaller_mesh = scale_mesh(mesh, 0.5)
Label Operations
expand_labels
def expand_labels(condensed_labels: np.ndarray, mapping: np.ndarray) -> np.ndarray
Expands a condensed set of labels for a reduced mesh to the full mesh.
Labels for the condensed mesh
Mapping from full mesh indices to condensed mesh indices
Returns: np.ndarray - Expanded labels with -1 for unmapped vertices
from meshmash.utils import expand_labels
condensed_labels = np.array([0, 1, 1, 2])
mapping = np.array([0, 0, 1, -1, 2, 3]) # -1 means unmapped
full_labels = expand_labels(condensed_labels, mapping)
shuffle_label_mapping
def shuffle_label_mapping(x)
Randomly permutes label values while preserving label assignments.
Returns: np.ndarray - Labels with shuffled values
from meshmash.utils import shuffle_label_mapping
labels = np.array([0, 0, 1, 1, 2, 2])
shuffled = shuffle_label_mapping(labels)
# Might become [2, 2, 0, 0, 1, 1] - values shuffled but grouping preserved
Graph Utilities
edges_to_lines
def edges_to_lines(edges: np.ndarray) -> np.ndarray
Converts edge array to PyVista lines format.
Array of shape (N, 2) containing edge vertex indices
Returns: np.ndarray - Array with shape (N, 3) where first column is 2 (line cell size)
from meshmash.utils import edges_to_lines
edges = np.array([[0, 1], [1, 2], [2, 0]])
lines = edges_to_lines(edges)
print(lines) # [[2, 0, 1], [2, 1, 2], [2, 2, 0]]
graph_to_adjacency
def graph_to_adjacency(graph: tuple) -> csr_array
Converts a graph represented as (vertices, edges) to an adjacency matrix.
Tuple of (vertices, edges)
Returns: csr_array - Sparse adjacency matrix
from meshmash.utils import graph_to_adjacency
vertices = np.array([[0, 0], [1, 0], [1, 1]])
edges = np.array([[0, 1], [1, 2]])
graph = (vertices, edges)
adj = graph_to_adjacency(graph)