| 1 |
""" |
| 2 |
.. _point_cloud_neighbors_example: |
| 3 |
|
| 4 |
Highlight Nearest Neighbors in a Point Cloud |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Use :func:`pyvista.DataSet.find_closest_point` to inspect local neighborhoods in |
| 8 |
a point cloud. |
| 9 |
""" |
| 10 |
|
| 11 |
import numpy as np |
| 12 |
import pyvista as pv |
| 13 |
from pyvista import examples |
| 14 |
|
| 15 |
# %% |
| 16 |
# Load a cosmological point cloud |
| 17 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 18 |
# :func:`~pyvista.examples.downloads.download_cloud_dark_matter` returns a |
| 19 |
# sampled N-body simulation. Pick a point near the middle of the cloud as |
| 20 |
# the query seed. |
| 21 |
|
| 22 |
cloud = examples.download_cloud_dark_matter() |
| 23 |
query = cloud.points[cloud.n_points // 2] |
| 24 |
|
| 25 |
neighbor_ids = cloud.find_closest_point(query, n=40) |
| 26 |
neighbors = pv.PolyData(cloud.points[neighbor_ids]) |
| 27 |
|
| 28 |
segments = np.vstack([np.vstack((query, point)) for point in neighbors.points]) |
| 29 |
connections = pv.line_segments_from_points(segments) |
| 30 |
|
| 31 |
pl = pv.Plotter() |
| 32 |
pl.add_points( |
| 33 |
cloud, |
| 34 |
color='lightgray', |
| 35 |
point_size=2, |
| 36 |
render_points_as_spheres=True, |
| 37 |
opacity=0.25, |
| 38 |
) |
| 39 |
pl.add_points( |
| 40 |
neighbors, |
| 41 |
color='tomato', |
| 42 |
point_size=14, |
| 43 |
render_points_as_spheres=True, |
| 44 |
) |
| 45 |
pl.add_points( |
| 46 |
np.array([query]), |
| 47 |
color='gold', |
| 48 |
point_size=18, |
| 49 |
render_points_as_spheres=True, |
| 50 |
) |
| 51 |
pl.add_mesh(connections, color='black', opacity=0.35) |
| 52 |
pl.show() |
| 53 |
|
| 54 |
|
| 55 |
# %% |
| 56 |
# Inspect the neighbor indices |
| 57 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 58 |
# The returned indices index back into the original point array. |
| 59 |
|
| 60 |
neighbor_ids |
| 61 |
# %% |
| 62 |
# .. tags:: plot |