Skip to main content

Conversion Functions

mesh_to_poly

def mesh_to_poly(mesh: Mesh) -> pv.PolyData
Converts a mesh to PyVista PolyData format.
mesh
Mesh
Input mesh as tuple, PolyData, or object with vertices/faces or polydata attribute
Returns: pv.PolyData - PyVista PolyData representation of the mesh
Example
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.
mesh
Mesh
Input mesh
Returns: np.ndarray - Array of shape (N, 2) containing edge vertex indices
Example
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.
mesh
Mesh
Input mesh
Returns: csr_array - Sparse adjacency matrix with edge lengths as weights
Example
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.
poly
pv.PolyData
PyVista PolyData object
Returns: Mesh - Tuple of (vertices, faces)
Example
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.
mesh
Mesh
Input mesh to repair
**kwargs
dict
Additional keyword arguments passed to MeshFix.repair()
Returns: Mesh - Repaired mesh as tuple of (vertices, faces)
Example
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.
points
np.ndarray
Array of points to project
mesh
Mesh
Target mesh
distance_threshold
float
default:"None"
Maximum distance for valid projections. Points beyond this distance are assigned index -1
return_distances
bool
default:"False"
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)
Example
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.
points
np.ndarray
Array of points
center_point
np.ndarray
Center point coordinates
Returns: np.ndarray - Array of distances
Example
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

component_size_transform

def component_size_transform(mesh, indices=None)
Returns the size of each connected component in a mesh.
mesh
Mesh
Input mesh
indices
np.ndarray
default:"None"
Vertex indices to query. If None, uses all vertices
Returns: np.ndarray - Array of component sizes for each queried index
Example
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.
mesh
Mesh
Input mesh
labels
np.ndarray or pd.Series
Label for each vertex
Returns: np.ndarray - Component labels where vertices with the same label and connectivity share the same component ID
Example
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.
mesh
Mesh
Input mesh
size_threshold
int
default:"100"
Minimum component size to include. If None, includes all components
Yields: Mesh - Individual connected component meshes
Example
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.
mesh
Mesh
Input mesh
Returns: Mesh - Largest connected component
Example
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.
mesh
Mesh
Input mesh
size_threshold
int
default:"100"
Minimum component size to keep
Returns: Tuple[Mesh, np.ndarray]
  • Filtered mesh
  • Indices of kept vertices in original mesh
Example
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.
mesh
Mesh
Input mesh
indices
np.ndarray
Vertex indices to include (can be boolean mask or integer indices)
Returns: Mesh - Submesh containing only faces where all vertices are in the subset
Example
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.
mesh
Mesh
Input mesh
indices
np.ndarray
Vertex indices to include
Returns: Tuple[Mesh, np.ndarray]
  • Submesh including faces with any vertex in indices
  • New vertex indices in the submesh
Example
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.
meshes
list[Mesh]
List of meshes to combine
Returns: Mesh - Combined mesh with all vertices and faces
Example
from meshmash.utils import combine_meshes

mesh1 = (vertices1, faces1)
mesh2 = (vertices2, faces2)
mesh3 = (vertices3, faces3)

combined = combine_meshes([mesh1, mesh2, mesh3])

Transformations

scale_mesh

def scale_mesh(mesh: Mesh, scale: float) -> Mesh
Scales a mesh by a given factor.
mesh
Mesh
Input mesh
scale
float
Scale factor
Returns: Mesh - Scaled mesh
Example
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.
condensed_labels
np.ndarray
Labels for the condensed mesh
mapping
np.ndarray
Mapping from full mesh indices to condensed mesh indices
Returns: np.ndarray - Expanded labels with -1 for unmapped vertices
Example
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.
x
np.ndarray
Array of labels
Returns: np.ndarray - Labels with shuffled values
Example
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.
edges
np.ndarray
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)
Example
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.
graph
tuple
Tuple of (vertices, edges)
Returns: csr_array - Sparse adjacency matrix
Example
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)

Build docs developers (and LLMs) love