| 1 |
""" |
| 2 |
.. _compare_threshold_filters_example: |
| 3 |
|
| 4 |
Compare threshold filters |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Multiple filters exist to exclude or highlight scalar values. |
| 8 |
The goal of this example is to compare some of these filters to show how each can be used. |
| 9 |
We will be differentiating them based on the input type they take, |
| 10 |
as well as the output type they produce. |
| 11 |
""" |
| 12 |
|
| 13 |
import numpy as np |
| 14 |
|
| 15 |
# sphinx_gallery_thumbnail_number = 1 |
| 16 |
import pyvista as pv |
| 17 |
from pyvista import examples |
| 18 |
|
| 19 |
# %% |
| 20 |
# Volume Data to Volume Data |
| 21 |
# ++++++++++++++++++++++++++ |
| 22 |
# Some filters generate `pyvista.Volume` out of `pyvista.Volume`. We will explore 3: |
| 23 |
# :func:`pyvista.DataSetFilters.threshold` |
| 24 |
# :func:`pyvista.ImageDataFilters.image_threshold` |
| 25 |
# :func:`pyvista.ImageDataFilters.select_values` |
| 26 |
# Note how `pyvista.DataSetFilters.threshold` keeps the dynamic |
| 27 |
# of the image for the voxels above the value |
| 28 |
# while image_threshold produces an all-or-nothing result. |
| 29 |
# Note the little specks for the `select_values`. |
| 30 |
# It is because it only selects the value (or values) that is (are) specified. |
| 31 |
method_map = {'default': 0, 'threshold': 1, 'image_threshold': 2, 'select_values': 3} |
| 32 |
step = -80 |
| 33 |
value = 155 |
| 34 |
outlines_mesh = pv.PolyData() |
| 35 |
volume = pv.examples.download_carotid() |
| 36 |
volume_outline = pv.Box(volume.bounds) |
| 37 |
volume_outline.cell_data['method'] = np.full( |
| 38 |
(volume_outline.n_cells), method_map['default'] |
| 39 |
) |
| 40 |
outlines_mesh += volume_outline |
| 41 |
thresholded_vol = volume.threshold(value) |
| 42 |
thresholded_vol = thresholded_vol.translate([step, 0, 0]) |
| 43 |
thresholded_vol_outline = pv.Box(thresholded_vol.bounds) |
| 44 |
thresholded_vol_outline.cell_data['method'] = np.full( |
| 45 |
(thresholded_vol_outline.n_cells), method_map['threshold'] |
| 46 |
) |
| 47 |
outlines_mesh += thresholded_vol_outline |
| 48 |
image_thresholded_vol = volume.image_threshold(value, in_value=volume['scalars'].max()) |
| 49 |
image_thresholded_vol = image_thresholded_vol.translate([2 * step, 0, 0]) |
| 50 |
image_thresholded_vol_outline = pv.Box(image_thresholded_vol.bounds) |
| 51 |
image_thresholded_vol_outline.cell_data['method'] = np.full( |
| 52 |
(image_thresholded_vol_outline.n_cells), method_map['image_threshold'] |
| 53 |
) |
| 54 |
outlines_mesh += image_thresholded_vol_outline |
| 55 |
select_values_vol = volume.select_values(value) |
| 56 |
select_values_vol = select_values_vol.translate([3 * step, 0, 0]) |
| 57 |
select_values_vol_outline = pv.Box(select_values_vol.bounds) |
| 58 |
select_values_vol_outline.cell_data['method'] = np.full( |
| 59 |
(select_values_vol_outline.n_cells), method_map['select_values'] |
| 60 |
) |
| 61 |
outlines_mesh += select_values_vol_outline |
| 62 |
pl = pv.Plotter() |
| 63 |
pl.add_volume(volume) |
| 64 |
pl.add_volume(thresholded_vol) |
| 65 |
pl.add_volume(image_thresholded_vol) |
| 66 |
pl.add_volume(select_values_vol) |
| 67 |
outlines_mesh.set_active_scalars('method') |
| 68 |
colored_outline_mesh, color_map = outlines_mesh.color_labels( |
| 69 |
output_scalars='method', return_dict=True |
| 70 |
) |
| 71 |
legend_map = dict(zip(method_map.keys(), color_map.values(), strict=True)) |
| 72 |
pl.add_mesh(colored_outline_mesh, style='wireframe', rgb=True, line_width=5) |
| 73 |
pl.add_legend(legend_map) |
| 74 |
cpos = pv.CameraPosition( |
| 75 |
position=(20, -390, -100.0), focal_point=(20, 100.0, 20.0), viewup=(0, 0, -1) |
| 76 |
) |
| 77 |
pl.camera_position = cpos |
| 78 |
pl.show() |
| 79 |
# %% |
| 80 |
# Volume Data to Unstructured Grid |
| 81 |
# ++++++++++++++++++++++++++++++++ |
| 82 |
# Some filters generate a `pyvista.UnstructuredGrid` |
| 83 |
# out of a`pyvista.Volume`. |
| 84 |
# We will explore 3: |
| 85 |
# :func:`pyvista.DataSetFilters.threshold` |
| 86 |
# :func:`pyvista.DataSetFilters.extract_values` |
| 87 |
# :func:`pyvista.DataSetFilters.clip_scalar` |
| 88 |
# Note the shape of the produced meshes. |
| 89 |
# `clip_scalar` produces `pyvista.CellType.WEDGE` |
| 90 |
# and `pyvista.CellType.TETRA` |
| 91 |
# while `extract_values` and `threshold` |
| 92 |
# produces `pyvista.CellType.VOXEL`. |
| 93 |
|
| 94 |
method_map = {'threshold': 0, 'extract_values': 1, 'clip_scalar': 2} |
| 95 |
volume = pv.examples.download_carotid() |
| 96 |
step = -80 |
| 97 |
values = (155, 580) |
| 98 |
mesh = pv.UnstructuredGrid() |
| 99 |
pl = pv.Plotter() |
| 100 |
clipped_vol = volume.clip_scalar(value=values) |
| 101 |
clipped_vol.cell_data['method'] = np.full( |
| 102 |
(clipped_vol.number_of_cells), method_map['clip_scalar'] |
| 103 |
) |
| 104 |
mesh += clipped_vol |
| 105 |
extracted_values_vol = volume.extract_values(values) |
| 106 |
extracted_values_vol.cell_data['method'] = np.full( |
| 107 |
(extracted_values_vol.number_of_cells), method_map['extract_values'] |
| 108 |
) |
| 109 |
extracted_values_vol.points[:, 0] += step |
| 110 |
mesh += extracted_values_vol |
| 111 |
|
| 112 |
thresholded_vol = volume.threshold(values) |
| 113 |
thresholded_vol.cell_data['method'] = np.full( |
| 114 |
(thresholded_vol.number_of_cells), method_map['threshold'] |
| 115 |
) |
| 116 |
thresholded_vol.points[:, 0] += step * 2 |
| 117 |
mesh += thresholded_vol |
| 118 |
mesh.set_active_scalars('method') |
| 119 |
colored_mesh, color_map = mesh.color_labels(output_scalars='method', return_dict=True) |
| 120 |
legend_map = dict(zip(method_map.keys(), color_map.values(), strict=True)) |
| 121 |
pl.add_mesh(colored_mesh, style='wireframe', rgb=True) |
| 122 |
pl.add_legend(legend_map) |
| 123 |
cpos = pv.CameraPosition( |
| 124 |
position=(55.2, -385.3, -119.1), focal_point=(55.2, 104.7, 0.9), viewup=(0, 0, -1) |
| 125 |
) |
| 126 |
pl.camera_position = cpos |
| 127 |
pl.show() |
| 128 |
# %% |
| 129 |
# Unstructured Grid to Unstructured Grid |
| 130 |
# ++++++++++++++++++++++++++++++++++++++ |
| 131 |
# Some filters generate a `pyvista.UnstructuredGrid` |
| 132 |
# out of a`pyvista.UnstructuredGrid`. |
| 133 |
# We will explore 2: |
| 134 |
# :func:`pyvista.DataSetFilters.threshold` |
| 135 |
# :func:`pyvista.DataSetFilters.clip_scalar` |
| 136 |
# Notice how threshold keeps the cells which correspond |
| 137 |
# to the specified value range while `clip_scalar` generates |
| 138 |
# a "clean" cut, which modifies the cells at the boundaries of |
| 139 |
# the clip. |
| 140 |
values = (1, 2) |
| 141 |
step = 5 |
| 142 |
surface = examples.download_foot_bones() |
| 143 |
method_map = {'threshold': 1, 'clip_scalar': 2} |
| 144 |
all_filtered_meshes = pv.UnstructuredGrid() |
| 145 |
surface.point_data['x'] = surface.points[:, 0] |
| 146 |
surface.set_active_scalars('x') |
| 147 |
thresholded_surf = surface.threshold(values) |
| 148 |
thresholded_surf.points[:, 2] += step |
| 149 |
thresholded_surf.cell_data['method'] = np.full( |
| 150 |
(thresholded_surf.number_of_cells), method_map['threshold'] |
| 151 |
) |
| 152 |
all_filtered_meshes += thresholded_surf |
| 153 |
clipped_surf = surface.clip_scalar(value=values) |
| 154 |
clipped_surf.points[:, 2] += 2 * step |
| 155 |
clipped_surf.cell_data['method'] = np.full( |
| 156 |
(clipped_surf.number_of_cells), method_map['clip_scalar'] |
| 157 |
) |
| 158 |
all_filtered_meshes += clipped_surf |
| 159 |
all_filtered_meshes.set_active_scalars('method') |
| 160 |
colored_mesh, color_map = all_filtered_meshes.color_labels( |
| 161 |
output_scalars='method', return_dict=True |
| 162 |
) |
| 163 |
legend_map = dict(zip(method_map.keys(), color_map.values(), strict=True)) |
| 164 |
pl = pv.Plotter() |
| 165 |
pl.add_mesh(surface, show_edges=True) |
| 166 |
pl.add_mesh(colored_mesh, rgb=True, show_edges=True) |
| 167 |
pl.add_legend(legend_map) |
| 168 |
cpos = pv.CameraPosition( |
| 169 |
position=(6.5, 29.4, 14.9), focal_point=(-1.1, -3.9, 6.0), viewup=(0.96, -0.3, 0.1) |
| 170 |
) |
| 171 |
pl.camera_position = cpos |
| 172 |
pl.show() |
| 173 |
# %% |
| 174 |
# .. tags:: load |