| 1 |
""" |
| 2 |
.. _convex_hull_example: |
| 3 |
|
| 4 |
Wrap a Point Cloud in a Convex Hull |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Create a convex hull from a point cloud using tetrahedralization and surface |
| 8 |
extraction. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
from pyvista import examples |
| 14 |
|
| 15 |
# %% |
| 16 |
# Load a point cloud |
| 17 |
# ~~~~~~~~~~~~~~~~~~ |
| 18 |
# The :func:`~pyvista.examples.downloads.download_horse_points` dataset is a |
| 19 |
# dense scan of a horse statue. Subsample it to keep the hull geometry light. |
| 20 |
|
| 21 |
full_cloud = examples.download_horse_points() |
| 22 |
rng = np.random.default_rng(seed=2) |
| 23 |
sample_ids = rng.choice(full_cloud.n_points, size=4000, replace=False) |
| 24 |
cloud = pv.PolyData(full_cloud.points[sample_ids]) |
| 25 |
cloud |
| 26 |
|
| 27 |
|
| 28 |
# %% |
| 29 |
# Extract the outer hull |
| 30 |
# ~~~~~~~~~~~~~~~~~~~~~~ |
| 31 |
# A Delaunay tetrahedralization followed by surface extraction returns the |
| 32 |
# outer surface of the cloud. |
| 33 |
|
| 34 |
hull = cloud.delaunay_3d(alpha=cloud.length).extract_surface(algorithm=None) |
| 35 |
|
| 36 |
pl = pv.Plotter() |
| 37 |
pl.add_points( |
| 38 |
cloud, |
| 39 |
color='black', |
| 40 |
point_size=6, |
| 41 |
render_points_as_spheres=True, |
| 42 |
) |
| 43 |
pl.add_mesh(hull, color='royalblue', opacity=0.4, show_edges=True) |
| 44 |
pl.camera_position = pv.CameraPosition( |
| 45 |
position=(0.4, -0.5, 0.25), |
| 46 |
focal_point=cloud.center, |
| 47 |
viewup=(0, 0, 1), |
| 48 |
) |
| 49 |
pl.show() |
| 50 |
|
| 51 |
|
| 52 |
# %% |
| 53 |
# Inspect the wrapped surface |
| 54 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 55 |
# The result is a closed surface enclosing every input point. |
| 56 |
|
| 57 |
hull |
| 58 |
# %% |
| 59 |
# .. tags:: filter |