Refinement#
- class medicalimageanalysis.utils.mesh.surface.Refinement(mesh)[source]
Bases:
objectProvides tools for refining surface meshes: smoothing, clustering, decimation and fin pruning, plus quality metrics used to check the result.
- Parameters:
mesh (pyvista.PolyData or pyvista.DataSet) – Input surface. Non-PolyData datasets are converted with
extract_surface. A copy is stored, so the input is never modified.
Examples
>>> import pyvista as pv >>> from medicalimageanalysis.utils.mesh.surface import Refinement >>> refine = Refinement(pv.Sphere(theta_resolution=32, phi_resolution=32)) >>> smooth_mesh = refine.smooth() >>> smooth_mesh.n_points 962
- cluster(points=None, points_per_ml=None, min_points=2000, max_points=7000, complexity_weight=0.0, complexity_metric='compactness', min_multiplier=0.9, max_multiplier=1.1, max_tries=5)[source]
Clusters the mesh with pyacvd and makes sure the result is a closed, edge-manifold triangle surface.
Workflow#
Target point count:
pointsif given; otherwisecompute_volume_points()ifpoints_per_mlis given (scaled by complexity whencomplexity_weight > 0); otherwisecompute_points().If the target is >= the current point count, the mesh is kept as-is (‘native’).
pyacvd is run up to
max_triestimes with target counts of 0, -1%, +2%, -3%, +4%, … around the target (a different count gives a different partition). The first clean result is returned.Otherwise every failed candidate is passed through
prune_fins(), fewest defects first, and the first one that comes out clean is returned.Last resort:
decimate_prowith topology preservation, pruned if needed. A warning is raised if it is still not clean.
- param points:
Fixed target number of points. Cannot be combined with
points_per_ml.- type points:
int, optional
- param points_per_ml:
Volume-based target, see
compute_volume_points(). Only used whenpointsis None.- type points_per_ml:
float, optional
- param min_points:
Bounds on the volume-based target.
- type min_points:
int
- param max_points:
Bounds on the volume-based target.
- type max_points:
int
- param complexity_weight:
Complexity scaling of the volume-based target. 0 (default) disables it.
- type complexity_weight:
float
- param complexity_metric:
Metric passed to
complexity_score().- type complexity_metric:
str
- param min_multiplier:
Bounds on the complexity multiplier.
- type min_multiplier:
float
- param max_multiplier:
Bounds on the complexity multiplier.
- type max_multiplier:
float
- param max_tries:
Number of pyacvd attempts before fin pruning.
- type max_tries:
int
- returns:
mesh – Clustered mesh (also stored in self.mesh). The path that produced it is stored in self.method.
- rtype:
pyvista.PolyData
- raises ValueError:
If both
pointsandpoints_per_mlare given.
Examples
>>> import pyvista as pv >>> refine = Refinement(pv.Sphere(radius=30, theta_resolution=100, phi_resolution=100)) >>> mesh = refine.cluster(points_per_ml=8.0, min_points=1500, max_points=2500) >>> refine.method, refine.surface_defects(), mesh.n_points ('pyacvd', 0, 1500) >>> _ = refine.cluster(points=50000) >>> refine.method 'native' >>> refine.cluster(points=1000, points_per_ml=8.0) Traceback (most recent call last): ... ValueError: Give either points or points_per_ml, not both
- complexity_score(metric='compactness')[source]
Measures how far the shape is from a sphere.
'compactness'is(area / volume^(2/3)) / (36 pi)^(1/3) - 1: 0 for a sphere and larger for elongated or folded shapes. Requires a closed surface for the volume to be meaningful.- Parameters:
metric ({'compactness'}) – Complexity metric.
- Returns:
Complexity score (>= 0).
- Return type:
- Raises:
ValueError – If metric is not supported.
Examples
>>> import pyvista as pv >>> round(Refinement(pv.Sphere(theta_resolution=100, phi_resolution=100)).complexity_score(), 3) 0.0 >>> round(Refinement(pv.Cube()).complexity_score(), 3) 0.241
- compute_points()[source]
Computes a target number of points as 10 * sqrt(current point count).
- Returns:
Target number of points for clustering/decimation.
- Return type:
Examples
>>> import pyvista as pv >>> refine = Refinement(pv.Sphere(theta_resolution=32, phi_resolution=32)) >>> refine.compute_points() 310
- compute_point_percentage()[source]
Computes the fraction of points to remove so that
compute_points()points remain.- Returns:
Fraction to remove (0-1).
- Return type:
Examples
>>> import pyvista as pv >>> refine = Refinement(pv.Sphere(theta_resolution=32, phi_resolution=32)) >>> round(refine.compute_point_percentage(), 3) 0.678
- compute_volume_points(points_per_ml=8.0, min_points=2000, max_points=7000, complexity_weight=0.0, complexity_metric='compactness', min_multiplier=0.9, max_multiplier=1.1)[source]
Computes a target point count from the enclosed volume, optionally scaled up for complex shapes.
base = clip(points_per_ml * volume_ml, min_points, max_points), thenpoints = clip(base * clip(1 + complexity_weight * complexity_score, min_multiplier, max_multiplier), min_points, max_points). Withcomplexity_weight=0(default) the result is just the volume estimate.- Parameters:
points_per_ml (float) – Points per mL of enclosed volume (mesh volume in mm^3 / 1000).
min_points (int) – Bounds on the returned count, applied before and after the complexity scaling.
max_points (int) – Bounds on the returned count, applied before and after the complexity scaling.
complexity_weight (float) – Weight on
complexity_score(). 0 disables the scaling.complexity_metric (str) – Metric passed to
complexity_score().min_multiplier (float) – Bounds on the complexity multiplier. The score is >= 0, so the multiplier never drops below 1 and
min_multiplieronly matters if it is set above 1.max_multiplier (float) – Bounds on the complexity multiplier. The score is >= 0, so the multiplier never drops below 1 and
min_multiplieronly matters if it is set above 1.
- Returns:
Target number of points.
- Return type:
Examples
>>> import pyvista as pv >>> refine = Refinement(pv.ParametricEllipsoid(60, 30, 20)) >>> refine.compute_volume_points(points_per_ml=30) 4520 >>> refine.compute_volume_points(points_per_ml=30, complexity_weight=0.12) 4605
- decimate(percent=None)[source]
Decimates (reduces) mesh complexity by removing points and faces.
- Parameters:
percent (float, optional) – Fraction of the faces to remove (0-1). If None,
compute_point_percentage()is used.- Returns:
mesh – Decimated mesh (also stored in self.mesh).
- Return type:
pyvista.PolyData
Examples
>>> import pyvista as pv >>> refine = Refinement(pv.Sphere(theta_resolution=32, phi_resolution=32)) >>> refine.decimate(0.5).n_cells 960
- mean_edge_length()[source]
Computes the mean length of the unique edges of the mesh.
- Returns:
Mean edge length (mesh units).
- Return type:
Examples
>>> import pyvista as pv >>> round(Refinement(pv.Cube()).mean_edge_length(), 3) 1.138
- prune_fins(max_iter=20)[source]
Iteratively strips faces that have a free edge (zero-thickness flaps), then drops unused points.
Faces are only removed, never added, so holes are not filled; a surface with real holes is eroded until it is closed or
max_iteris reached.- Parameters:
max_iter (int) – Maximum number of strip passes.
- Returns:
mesh – Pruned mesh (also stored in self.mesh).
- Return type:
pyvista.PolyData
Examples
>>> import numpy as np >>> import pyvista as pv >>> sphere = pv.Sphere() >>> fin = np.vstack([sphere.points, [[2.0, 0.0, 0.0]]]) >>> faces = np.vstack([sphere.regular_faces, [[0, 1, len(fin) - 1]]]) >>> refine = Refinement(pv.PolyData.from_regular_faces(fin, faces)) >>> refine.surface_defects() 4 >>> refine.prune_fins().n_cells == sphere.n_cells, refine.surface_defects() (True, 0)
- smooth(iterations=20, angle=60, passband=0.001)[source]
Smooths the mesh geometry using a windowed sinc filter.
- Parameters:
- Returns:
mesh – Smoothed mesh (also stored in self.mesh). Point count and connectivity are unchanged.
- Return type:
pyvista.PolyData
Examples
>>> import pyvista as pv >>> refine = Refinement(pv.Sphere(theta_resolution=32, phi_resolution=32)) >>> refine.smooth(iterations=10).n_cells 1920
- surface_defects()[source]
Counts surface defects of the current mesh, for QA and failure detection.
- Returns:
0 for a closed, edge-manifold, all-triangle surface; otherwise the number of boundary + non-manifold edges (+1 if the face count is odd). -1 if the mesh is empty, has lines/vertices, or is not all triangles.
- Return type:
Examples
>>> import pyvista as pv >>> Refinement(pv.Sphere()).surface_defects() 0 >>> Refinement(pv.Plane()).surface_defects() -1 >>> Refinement(pv.Plane().triangulate()).surface_defects() 40