| 1 |
""" |
| 2 |
.. _extract_surface_example: |
| 3 |
|
| 4 |
Extract Surface |
| 5 |
--------------- |
| 6 |
|
| 7 |
You can extract the surface of nearly any object within ``pyvista`` |
| 8 |
using the :meth:`~pyvista.DataObjectFilters.extract_surface` filter. |
| 9 |
""" |
| 10 |
|
| 11 |
# sphinx_gallery_thumbnail_number = 2 |
| 12 |
import numpy as np |
| 13 |
import pyvista as pv |
| 14 |
from pyvista import CellType |
| 15 |
|
| 16 |
# %% |
| 17 |
# Surface extraction of nonlinear cells |
| 18 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 |
# Here we create a single :attr:`~pyvista.CellType.QUADRATIC_HEXAHEDRON` cell and then |
| 20 |
# extract its surface to demonstrate how to extract the surface of an |
| 21 |
# :class:`~pyvista.UnstructuredGrid`. First define points of a linear cell: |
| 22 |
|
| 23 |
lin_pts = np.array( |
| 24 |
[ |
| 25 |
[-1, -1, -1], # point 0 |
| 26 |
[1, -1, -1], # point 1 |
| 27 |
[1, 1, -1], # point 2 |
| 28 |
[-1, 1, -1], # point 3 |
| 29 |
[-1, -1, 1], # point 4 |
| 30 |
[1, -1, 1], # point 5 |
| 31 |
[1, 1, 1], # point 6 |
| 32 |
[-1, 1, 1], # point 7 |
| 33 |
], |
| 34 |
np.double, |
| 35 |
) |
| 36 |
|
| 37 |
# %% |
| 38 |
# Next, define the "midside" points of a quad cell. See the definition of a |
| 39 |
# :vtk:`vtkQuadraticHexahedron`. |
| 40 |
|
| 41 |
quad_pts = np.array( |
| 42 |
[ |
| 43 |
(lin_pts[1] + lin_pts[0]) / 2, # between point 0 and 1 |
| 44 |
(lin_pts[1] + lin_pts[2]) / 2, # between point 1 and 2 |
| 45 |
(lin_pts[2] + lin_pts[3]) / 2, # and so on... |
| 46 |
(lin_pts[3] + lin_pts[0]) / 2, |
| 47 |
(lin_pts[4] + lin_pts[5]) / 2, |
| 48 |
(lin_pts[5] + lin_pts[6]) / 2, |
| 49 |
(lin_pts[6] + lin_pts[7]) / 2, |
| 50 |
(lin_pts[7] + lin_pts[4]) / 2, |
| 51 |
(lin_pts[0] + lin_pts[4]) / 2, |
| 52 |
(lin_pts[1] + lin_pts[5]) / 2, |
| 53 |
(lin_pts[2] + lin_pts[6]) / 2, |
| 54 |
(lin_pts[3] + lin_pts[7]) / 2, |
| 55 |
], |
| 56 |
) |
| 57 |
|
| 58 |
# %% |
| 59 |
# We introduce a minor variation to the location of the mid-side points |
| 60 |
# seed the random numbers for reproducibility |
| 61 |
rng = np.random.default_rng(seed=0) |
| 62 |
quad_pts += rng.random(quad_pts.shape) * 0.3 |
| 63 |
pts = np.vstack((lin_pts, quad_pts)) |
| 64 |
|
| 65 |
# %% |
| 66 |
# Create the grid |
| 67 |
cells = np.hstack((20, np.arange(20))).astype(np.int64, copy=False) |
| 68 |
celltypes = np.array([CellType.QUADRATIC_HEXAHEDRON]) |
| 69 |
grid = pv.UnstructuredGrid(cells, celltypes, pts) |
| 70 |
|
| 71 |
# %% |
| 72 |
# Finally, extract the surface and plot it. |
| 73 |
# Note that the `'dataset_surface'` algorithm is necessary to use when generating surfaces |
| 74 |
# from non-linear cells. Setting ``algorithm=None`` also works. |
| 75 |
surf = grid.extract_surface(algorithm='dataset_surface') |
| 76 |
surf.plot(show_scalar_bar=False) |
| 77 |
|
| 78 |
# %% |
| 79 |
# Nonlinear Surface Subdivision |
| 80 |
# ============================= |
| 81 |
# Should your UnstructuredGrid contain quadratic cells, you can |
| 82 |
# generate a smooth surface based on the position of the |
| 83 |
# "mid-edge" nodes. This allows the plotting of cells |
| 84 |
# containing curvature. For additional reference, please see: |
| 85 |
# https://prod.sandia.gov/techlib-noauth/access-control.cgi/2004/041617.pdf |
| 86 |
|
| 87 |
surf_subdivided = grid.extract_surface( |
| 88 |
algorithm='dataset_surface', nonlinear_subdivision=5 |
| 89 |
) |
| 90 |
surf_subdivided.plot(show_scalar_bar=False) |
| 91 |
|
| 92 |
# %% |
| 93 |
# .. _compare_surface_extract_algorithms: |
| 94 |
# |
| 95 |
# Compare Surface Extraction Algorithms |
| 96 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 97 |
# The filter :meth:`~pyvista.DataObjectFilters.extract_surface` provides the option to |
| 98 |
# select which internal VTK algorithm to use for surface extraction: |
| 99 |
# :vtk:`vtkGeometryFilter` or :vtk:`vtkDataSetSurfaceFilter`. Both algorithms produce |
| 100 |
# similar surfaces, but they differ in important ways. As the following examples will |
| 101 |
# demonstrate, it is generally preferable to use the geometry algorithm. |
| 102 |
# |
| 103 |
# Structural Preservation |
| 104 |
# ======================= |
| 105 |
# The geometry algorithm preserves structure when converting between mesh types, whereas |
| 106 |
# the dataset surface algorithm does not. |
| 107 |
# |
| 108 |
# For example, let's create a simple :class:`~pyvista.PolyData` mesh using |
| 109 |
# :meth:`~pyvista.Cone` and cast it to :class:`~pyvista.UnstructuredGrid`. |
| 110 |
|
| 111 |
poly = pv.Cone() |
| 112 |
ugrid = poly.cast_to_unstructured_grid() |
| 113 |
|
| 114 |
# %% |
| 115 |
# If we convert it back to a surface, the geometry algorithm returns the original surface |
| 116 |
# with the same order of points and the same cell connectivity arrays. |
| 117 |
|
| 118 |
poly_geometry = ugrid.extract_surface( |
| 119 |
algorithm='geometry', pass_cellid=False, pass_pointid=False |
| 120 |
) |
| 121 |
assert poly_geometry == poly |
| 122 |
|
| 123 |
# %% |
| 124 |
# But the dataset surface algorithm does not. |
| 125 |
|
| 126 |
poly_surface = ugrid.extract_surface( |
| 127 |
algorithm='dataset_surface', pass_cellid=False, pass_pointid=False |
| 128 |
) |
| 129 |
assert poly_surface != poly |
| 130 |
|
| 131 |
# %% |
| 132 |
# Compare the point ids of the original mesh to the generated surfaces. |
| 133 |
pl = pv.Plotter(shape=(1, 3)) |
| 134 |
|
| 135 |
pl.subplot(0, 0) |
| 136 |
pl.add_mesh(poly_geometry) |
| 137 |
pl.add_title("'geometry'\nalgorithm", font_size=14) |
| 138 |
pl.add_point_labels(poly_geometry, range(poly_geometry.n_points), font_size=36) |
| 139 |
|
| 140 |
pl.subplot(0, 1) |
| 141 |
pl.add_mesh(poly) |
| 142 |
pl.add_title('original surface', font_size=14) |
| 143 |
pl.add_point_labels(poly, range(poly.n_points), font_size=36) |
| 144 |
|
| 145 |
pl.subplot(0, 2) |
| 146 |
pl.add_mesh(poly_surface) |
| 147 |
pl.add_title("'dataset_surface'\nalgorithm", font_size=14) |
| 148 |
pl.add_point_labels(poly_surface, range(poly_surface.n_points), font_size=36) |
| 149 |
|
| 150 |
pl.link_views() |
| 151 |
pl.show() |
| 152 |
|
| 153 |
# %% |
| 154 |
# Closed Surface Generation |
| 155 |
# ========================= |
| 156 |
# The geometry algorithm generates closed surfaces in cases where a closed surface is |
| 157 |
# expected, whereas the dataset surface algorithm may not. For example, extract the |
| 158 |
# surface of :class:`~pyvista.ImageData` comprised of a single |
| 159 |
# :attr:`~pyvista.CellType.VOXEL` cell. |
| 160 |
|
| 161 |
grid = pv.ImageData(dimensions=(2, 2, 2)) |
| 162 |
assert grid.n_cells == 1 |
| 163 |
assert grid.distinct_cell_types == {CellType.VOXEL} |
| 164 |
assert grid.max_cell_dimensionality == 3 |
| 165 |
|
| 166 |
# %% |
| 167 |
# Both algorithms convert the single 3D cell into a cube with six 2D |
| 168 |
# :attr:`~pyvista.CellType.QUAD` faces. |
| 169 |
|
| 170 |
poly_geometry = grid.extract_surface(algorithm='geometry') |
| 171 |
assert poly_geometry.n_cells == 6 |
| 172 |
assert poly_geometry.distinct_cell_types == {CellType.QUAD} |
| 173 |
assert poly_geometry.max_cell_dimensionality == 2 |
| 174 |
|
| 175 |
poly_surface = grid.extract_surface(algorithm='dataset_surface') |
| 176 |
assert poly_surface.n_cells == 6 |
| 177 |
assert poly_surface.distinct_cell_types == {CellType.QUAD} |
| 178 |
assert poly_geometry.max_cell_dimensionality == 2 |
| 179 |
|
| 180 |
# %% |
| 181 |
# However, the geometry algorithm returns a closed surface with eight points and no |
| 182 |
# open edges. |
| 183 |
assert poly_geometry.n_points == 8 |
| 184 |
assert poly_geometry.n_open_edges == 0 |
| 185 |
|
| 186 |
# %% |
| 187 |
# In contrast, the dataset surface algorithm returns a surface with duplicate points and |
| 188 |
# many open edges. |
| 189 |
assert poly_surface.n_points == 24 |
| 190 |
assert poly_surface.n_open_edges == 24 |
| 191 |
|
| 192 |
# %% |
| 193 |
# This can be fixed with a call to :meth:`~pyvista.PolyDataFilters.clean`, however. |
| 194 |
|
| 195 |
cleaned = poly_surface.clean() |
| 196 |
assert cleaned.n_points == 8 |
| 197 |
assert cleaned.n_open_edges == 0 |
| 198 |
|
| 199 |
# %% |
| 200 |
# Note that a closed surface is important for some calculations. E.g. the filter |
| 201 |
# :meth:`~pyvista.DataSetFilters.select_interior_points` requires a closed surface by |
| 202 |
# default, and properties like :attr:`~pyvista.PolyData.volume` assume the input is a |
| 203 |
# closed surface. |
| 204 |
|
| 205 |
# %% |
| 206 |
# .. tags:: filter |