| 1 |
""" |
| 2 |
.. _decimate_example: |
| 3 |
|
| 4 |
Decimation |
| 5 |
~~~~~~~~~~ |
| 6 |
|
| 7 |
Decimate a mesh |
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
# sphinx_gallery_thumbnail_number = 4 |
| 12 |
import numpy as np |
| 13 |
import pyvista as pv |
| 14 |
from pyvista import examples |
| 15 |
|
| 16 |
mesh = examples.download_face() |
| 17 |
|
| 18 |
# Define a camera position that shows this mesh properly |
| 19 |
cpos = pv.CameraPosition( |
| 20 |
position=(0.4, -0.07, -0.31), focal_point=(0.05, -0.13, -0.06), viewup=(-0.1, 1, 0.08) |
| 21 |
) |
| 22 |
dargs = dict(show_edges=True, color=True) |
| 23 |
|
| 24 |
# Preview the mesh |
| 25 |
mesh.plot(cpos=cpos, **dargs) |
| 26 |
|
| 27 |
# %% |
| 28 |
# Now let's define a target reduction and compare the |
| 29 |
# :func:`pyvista.PolyDataFilters.decimate` and |
| 30 |
# :func:`pyvista.PolyDataFilters.decimate_pro` filters. |
| 31 |
target_reduction = 0.7 |
| 32 |
print(f'Reducing {target_reduction * 100.0} percent out of the original mesh') |
| 33 |
|
| 34 |
# %% |
| 35 |
decimated = mesh.decimate(target_reduction) |
| 36 |
|
| 37 |
decimated.plot(cpos=cpos, **dargs) |
| 38 |
|
| 39 |
|
| 40 |
# %% |
| 41 |
pro_decimated = mesh.decimate_pro(target_reduction, preserve_topology=True) |
| 42 |
|
| 43 |
pro_decimated.plot(cpos=cpos, **dargs) |
| 44 |
|
| 45 |
|
| 46 |
# %% |
| 47 |
# Side by side comparison: |
| 48 |
|
| 49 |
# sphinx_gallery_start_ignore |
| 50 |
# text missing in interactive |
| 51 |
PYVISTA_GALLERY_FORCE_STATIC = True |
| 52 |
# sphinx_gallery_end_ignore |
| 53 |
|
| 54 |
pl = pv.Plotter(shape=(1, 3)) |
| 55 |
pl.add_mesh(mesh, **dargs) |
| 56 |
pl.add_text('Input mesh', font_size=24) |
| 57 |
pl.camera_position = cpos |
| 58 |
pl.reset_camera() |
| 59 |
pl.subplot(0, 1) |
| 60 |
pl.add_mesh(decimated, **dargs) |
| 61 |
pl.add_text('Decimated mesh', font_size=24) |
| 62 |
pl.camera_position = cpos |
| 63 |
pl.reset_camera() |
| 64 |
pl.subplot(0, 2) |
| 65 |
pl.add_mesh(pro_decimated, **dargs) |
| 66 |
pl.add_text('Pro Decimated mesh', font_size=24) |
| 67 |
pl.camera_position = cpos |
| 68 |
pl.reset_camera() |
| 69 |
pl.link_views() |
| 70 |
pl.show() |
| 71 |
|
| 72 |
# %% |
| 73 |
# Decimate Polyline Mesh |
| 74 |
# ---------------------- |
| 75 |
# |
| 76 |
# Generate a fairly slow spiral polyline mesh. |
| 77 |
|
| 78 |
n_points = 100 |
| 79 |
n_rotations = 5 |
| 80 |
phi = np.linspace(0, 2 * np.pi * n_rotations, n_points) |
| 81 |
ratio = 1.1 |
| 82 |
r = ratio ** (phi) |
| 83 |
|
| 84 |
points = np.zeros((n_points, 3)) |
| 85 |
points[:, 0] = r * np.cos(phi) |
| 86 |
points[:, 1] = r * np.sin(phi) |
| 87 |
|
| 88 |
spiral = pv.PolyData(points, lines=np.append([n_points], np.arange(n_points))) |
| 89 |
|
| 90 |
# %% |
| 91 |
# Construct a reusable plotting function for future use. |
| 92 |
|
| 93 |
|
| 94 |
def compare_decimation(spiral, decimated): |
| 95 |
pl = pv.Plotter() |
| 96 |
pl.add_mesh(spiral, line_width=5, color='r', label='Original') |
| 97 |
pl.add_mesh(decimated, line_width=3, color='k', label='Decimated') |
| 98 |
pl.view_xy() |
| 99 |
pl.add_legend(face='line', size=(0.25, 0.25)) |
| 100 |
|
| 101 |
|
| 102 |
# %% |
| 103 |
# Decimate using :func:`pyvista.PolyDataFilters.decimate_polyline` filter by |
| 104 |
# target of 50%. |
| 105 |
|
| 106 |
decimated = spiral.decimate_polyline(0.5) |
| 107 |
print(f'Original # of points: {spiral.n_points}') |
| 108 |
print(f'Decimated # of points: {decimated.n_points}') |
| 109 |
|
| 110 |
# %% |
| 111 |
# The decimation looks OK at this level of reduction. |
| 112 |
|
| 113 |
compare_decimation(spiral, decimated) |
| 114 |
|
| 115 |
# %% |
| 116 |
# Using a larger level of reduction, 80%, leads to a much coarser level of |
| 117 |
# representation. |
| 118 |
|
| 119 |
decimated = spiral.decimate_polyline(0.8) |
| 120 |
print(f'Original # of points: {spiral.n_points}') |
| 121 |
print(f'Decimated # of points: {decimated.n_points}') |
| 122 |
|
| 123 |
# %% |
| 124 |
# The structure of the inner part of the spiral is completely |
| 125 |
# lost. |
| 126 |
|
| 127 |
compare_decimation(spiral, decimated) |
| 128 |
|
| 129 |
# %% |
| 130 |
# To avoid errors of quickly changing features, use the ``maximum_error`` |
| 131 |
# parameter. It is in units of fraction of the largest length of the |
| 132 |
# bounding box. Note that it limits the level of reduction achieved. |
| 133 |
|
| 134 |
decimated = spiral.decimate_polyline(0.8, maximum_error=0.5) |
| 135 |
print(f'Original # of points: {spiral.n_points}') |
| 136 |
print(f'Decimated # of points: {decimated.n_points}') |
| 137 |
|
| 138 |
# %% |
| 139 |
# The structure of the inner part of the spiral is captured adequately. |
| 140 |
|
| 141 |
compare_decimation(spiral, decimated) |
| 142 |
|
| 143 |
# %% |
| 144 |
# .. tags:: filter |