| 1 |
""" |
| 2 |
.. _connectivity_example: |
| 3 |
|
| 4 |
Connectivity |
| 5 |
~~~~~~~~~~~~ |
| 6 |
|
| 7 |
This example highlights some applications of the |
| 8 |
:func:`~pyvista.DataSetFilters.connectivity` filter. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
# %% |
| 13 |
# Remove Noisy Isosurfaces |
| 14 |
# ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 15 |
# |
| 16 |
# Use connectivity to remove noisy isosurfaces. |
| 17 |
# |
| 18 |
# This section is similar to `this VTK example <https://kitware.github.io/vtk-examples/site/Python/VisualizationAlgorithms/PineRootConnectivity/>`__. |
| 19 |
|
| 20 |
# sphinx_gallery_thumbnail_number = 2 |
| 21 |
import numpy as np |
| 22 |
import pyvista as pv |
| 23 |
from pyvista import examples |
| 24 |
|
| 25 |
# %% |
| 26 |
# Load a dataset with noisy isosurfaces. |
| 27 |
pine_roots = examples.download_pine_roots() |
| 28 |
|
| 29 |
# Plot the raw data |
| 30 |
cpos = pv.CameraPosition( |
| 31 |
position=(40.6018, -280.533, 47.0172), |
| 32 |
focal_point=(40.6018, 37.2813, 50.1953), |
| 33 |
viewup=(0.0, 0.0, 1.0), |
| 34 |
) |
| 35 |
pl = pv.Plotter() |
| 36 |
pl.add_mesh(pine_roots, color='#965434') |
| 37 |
pl.add_mesh(pine_roots.outline()) |
| 38 |
pl.show(cpos=cpos) |
| 39 |
|
| 40 |
# %% |
| 41 |
# The plotted mesh is very noisy. We can extract the largest connected |
| 42 |
# isosurface using the ``'largest'`` ``extraction_mode`` of the |
| 43 |
# :func:`~pyvista.DataSetFilters.connectivity` filter. Equivalently, |
| 44 |
# :func:`~pyvista.DataSetFilters.extract_largest` may also be used. |
| 45 |
|
| 46 |
# Grab the largest connected volume present |
| 47 |
largest = pine_roots.connectivity('largest') |
| 48 |
# or: largest = mesh.extract_largest() |
| 49 |
|
| 50 |
pl = pv.Plotter() |
| 51 |
pl.add_mesh(largest, color='#965434') |
| 52 |
pl.add_mesh(pine_roots.outline()) |
| 53 |
pl.camera_position = cpos |
| 54 |
pl.show() |
| 55 |
|
| 56 |
|
| 57 |
# %% |
| 58 |
# Extract Small Regions |
| 59 |
# ~~~~~~~~~~~~~~~~~~~~~ |
| 60 |
# |
| 61 |
# Use connectivity to extract the smaller 'noisy' regions that were |
| 62 |
# removed in the remove noisy isosurfaces example above. |
| 63 |
# |
| 64 |
# First, get a list of all region ids. |
| 65 |
all_regions = pine_roots.connectivity('all') |
| 66 |
region_ids = np.unique(all_regions['RegionId']) |
| 67 |
|
| 68 |
# %% |
| 69 |
# Since the region IDs are sorted in descending order (by cell count), |
| 70 |
# we can extract all regions *except* for the largest one using the |
| 71 |
# ``'specified'`` ``extraction_mode`` of the :func:`~pyvista.DataSetFilters.connectivity` |
| 72 |
# filter. |
| 73 |
noise_region_ids = region_ids[1::] # All region ids except '0' |
| 74 |
noise = pine_roots.connectivity('specified', noise_region_ids) |
| 75 |
|
| 76 |
# %% |
| 77 |
# Plot the noisy regions using :meth:`~pyvista.DataSetFilters.color_labels`. |
| 78 |
# For context, also plot the largest region. |
| 79 |
pl = pv.Plotter() |
| 80 |
pl.add_mesh(noise.color_labels()) |
| 81 |
pl.add_mesh(largest, color='lightgray', opacity=0.1) |
| 82 |
pl.add_mesh(pine_roots.outline()) |
| 83 |
pl.camera_position = cpos |
| 84 |
pl.show() |
| 85 |
|
| 86 |
|
| 87 |
# %% |
| 88 |
# Label Disconnected Regions |
| 89 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 |
# |
| 91 |
# Use connectivity to label all disconnected regions. |
| 92 |
# |
| 93 |
# This section is similar to `this VTK example <https://examples.vtk.org/site/Cxx/PolyData/ColorDisconnectedRegionsDemo/>`__. |
| 94 |
# |
| 95 |
# First, load a dataset with disconnected regions. |
| 96 |
mesh = examples.download_foot_bones() |
| 97 |
|
| 98 |
# %% |
| 99 |
# Extract all regions. |
| 100 |
conn = mesh.connectivity('all') |
| 101 |
|
| 102 |
# %% |
| 103 |
# Plot the labeled regions using :meth:`~pyvista.DataSetFilters.color_labels`. |
| 104 |
|
| 105 |
colored = conn.color_labels() |
| 106 |
cpos = pv.CameraPosition( |
| 107 |
position=(10.5, 12.2, 18.3), focal_point=(0.0, 0.0, 0.0), viewup=(0.0, 1.0, 0.0) |
| 108 |
) |
| 109 |
colored.plot(cpos=cpos) |
| 110 |
|
| 111 |
|
| 112 |
# %% |
| 113 |
# Extract Regions From Seed Points |
| 114 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 115 |
# |
| 116 |
# Use connectivity to extract regions of interest using scalar data and |
| 117 |
# seed points. |
| 118 |
# |
| 119 |
# First, create a dataset with salient features. Here, we create hills |
| 120 |
# and use curvature to define its peaks and valleys. |
| 121 |
mesh = pv.ParametricRandomHills() |
| 122 |
mesh['Curvature'] = mesh.curvature() |
| 123 |
|
| 124 |
# %% |
| 125 |
# Visualize the peaks and valleys. |
| 126 |
# Peaks have large positive curvature (i.e. are convex). |
| 127 |
# Valleys have large negative curvature (i.e. are concave). |
| 128 |
# Flat regions have curvature close to zero. |
| 129 |
mesh.plot( |
| 130 |
clim=[-0.5, 0.5], |
| 131 |
cmap='bwr', |
| 132 |
below_color='blue', |
| 133 |
above_color='red', |
| 134 |
) |
| 135 |
|
| 136 |
# %% |
| 137 |
# Extract a region of interest using the |
| 138 |
# ``'point_seed'`` ``extraction_mode`` of the :func:`~pyvista.DataSetFilters.connectivity` |
| 139 |
# filter. Let's extract the steepest peak using a seed point where the |
| 140 |
# curvature is maximized. |
| 141 |
|
| 142 |
# Get seed point |
| 143 |
peak_point_id = np.argmax(mesh['Curvature']) |
| 144 |
|
| 145 |
# Define a scalar range of the region to extract |
| 146 |
data_min, data_max = mesh.get_data_range() |
| 147 |
peak_range = [0.2, data_max] # Peak if curvature > 0.2 |
| 148 |
|
| 149 |
peak_mesh = mesh.connectivity('point_seed', peak_point_id, scalar_range=peak_range) |
| 150 |
|
| 151 |
# %% |
| 152 |
# Let's also extract the closest valley to the steepest peak using the |
| 153 |
# ``'closest'`` ``extraction_mode`` of the :func:`~pyvista.DataSetFilters.connectivity` |
| 154 |
# filter. |
| 155 |
valley_range = [data_min, -0.2] # Valley if curvature < -0.2 |
| 156 |
peak_point = mesh.points[peak_point_id] |
| 157 |
valley_mesh = mesh.connectivity('closest', peak_point, scalar_range=valley_range) |
| 158 |
|
| 159 |
# %% |
| 160 |
# Plot extracted regions. |
| 161 |
pl = pv.Plotter() |
| 162 |
_ = pl.add_mesh(mesh, style='wireframe', color='lightgray') |
| 163 |
_ = pl.add_mesh(peak_mesh, color='red', label='Steepest Peak') |
| 164 |
_ = pl.add_mesh(valley_mesh, color='blue', label='Closest Valley') |
| 165 |
_ = pl.add_legend() |
| 166 |
pl.show() |
| 167 |
# %% |
| 168 |
# .. tags:: filter |