| 1 |
""" |
| 2 |
.. _subdivide_example: |
| 3 |
|
| 4 |
Subdivide Cells |
| 5 |
~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Increase the number of triangles in a single, connected triangular mesh. |
| 8 |
|
| 9 |
The :func:`pyvista.PolyDataFilters.subdivide` filter utilizes three different |
| 10 |
subdivision algorithms to subdivide a mesh's cells: `butterfly`, `loop`, |
| 11 |
or `linear`. |
| 12 |
""" |
| 13 |
|
| 14 |
import pyvista as pv |
| 15 |
from pyvista import examples |
| 16 |
|
| 17 |
# %% |
| 18 |
# First, let's load a **triangulated** mesh to subdivide. We can use the |
| 19 |
# :func:`pyvista.DataObjectFilters.triangulate` filter to ensure the mesh we are |
| 20 |
# using is purely triangles. |
| 21 |
mesh = examples.download_bunny_coarse().triangulate().clean() |
| 22 |
|
| 23 |
cpos = pv.CameraPosition( |
| 24 |
position=(-0.02788175062966399, 0.19293295656233056, 0.4334449972621349), |
| 25 |
focal_point=(-0.053260899930287015, 0.08881197167521734, -9.016948161029588e-05), |
| 26 |
viewup=(-0.10170607813337212, 0.9686438023715356, -0.22668272496584665), |
| 27 |
) |
| 28 |
|
| 29 |
# %% |
| 30 |
# Now, lets do a few subdivisions with the mesh and compare the results. |
| 31 |
# Below is a helper function to make a comparison plot of thee different |
| 32 |
# subdivisions. |
| 33 |
|
| 34 |
|
| 35 |
def plot_subdivisions(mesh, a, b): |
| 36 |
display_args = dict(show_edges=True, color=True) |
| 37 |
pl = pv.Plotter(shape=(3, 3)) |
| 38 |
|
| 39 |
for i in range(3): |
| 40 |
pl.subplot(i, 0) |
| 41 |
pl.add_mesh(mesh, **display_args) |
| 42 |
pl.add_text('Original Mesh') |
| 43 |
|
| 44 |
def row_plot(row, subfilter): |
| 45 |
subs = [a, b] |
| 46 |
for i in range(2): |
| 47 |
pl.subplot(row, i + 1) |
| 48 |
pl.add_mesh(mesh.subdivide(subs[i], subfilter=subfilter), **display_args) |
| 49 |
pl.add_text(f'{subfilter} subdivision of {subs[i]}') |
| 50 |
|
| 51 |
row_plot(0, 'linear') |
| 52 |
row_plot(1, 'butterfly') |
| 53 |
row_plot(2, 'loop') |
| 54 |
|
| 55 |
pl.link_views() |
| 56 |
pl.view_isometric() |
| 57 |
return pl |
| 58 |
|
| 59 |
|
| 60 |
# %% |
| 61 |
# Run the subdivisions for 1 and 3 levels. |
| 62 |
|
| 63 |
pl = plot_subdivisions(mesh, 1, 3) |
| 64 |
pl.camera_position = cpos |
| 65 |
pl.show() |
| 66 |
# %% |
| 67 |
# .. tags:: filter |