| 1 |
""" |
| 2 |
.. _cell_centers_example: |
| 3 |
|
| 4 |
Extract Cell Centers |
| 5 |
~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Extract the coordinates of the centers of all cells or faces in a mesh. |
| 8 |
|
| 9 |
Here we use :func:`cell_centers <pyvista.DataObjectFilters.cell_centers>`. |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
import pyvista as pv |
| 14 |
|
| 15 |
# sphinx_gallery_thumbnail_number = 3 |
| 16 |
from pyvista import examples |
| 17 |
|
| 18 |
# %% |
| 19 |
# First let's fetch the centers of a mesh with 2D geometries (a surface) |
| 20 |
mesh = examples.download_teapot() |
| 21 |
|
| 22 |
cpos = pv.CameraPosition( |
| 23 |
position=(6.192871661244108, 5.687542355343226, -4.95345468836544), |
| 24 |
focal_point=(0.48853358141600634, 1.2019347531215714, 0.1656178278582367), |
| 25 |
viewup=(-0.40642070472687936, 0.8621356761976646, 0.30256286387543047), |
| 26 |
) |
| 27 |
|
| 28 |
centers = mesh.cell_centers() |
| 29 |
|
| 30 |
pl = pv.Plotter() |
| 31 |
pl.add_mesh(mesh, show_edges=True, line_width=1) |
| 32 |
pl.add_mesh(centers, color='r', point_size=8.0, render_points_as_spheres=True) |
| 33 |
pl.show(cpos=cpos) |
| 34 |
|
| 35 |
|
| 36 |
# %% |
| 37 |
# We can also do this for full 3D meshes. |
| 38 |
|
| 39 |
grid = examples.download_letter_a() |
| 40 |
|
| 41 |
cpos = pv.CameraPosition( |
| 42 |
position=(2.704583323659036, 0.7822568412034183, 1.7251126717482546), |
| 43 |
focal_point=(3.543391913452799, 0.31117673768140197, 0.16407006760146028), |
| 44 |
viewup=(0.1481171795711516, 0.96599698246102, -0.2119224645762945), |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
centers = grid.cell_centers() |
| 49 |
|
| 50 |
pl = pv.Plotter() |
| 51 |
pl.add_mesh(grid, show_edges=True, opacity=0.5, line_width=1) |
| 52 |
pl.add_mesh(centers, color='r', point_size=8.0, render_points_as_spheres=True) |
| 53 |
pl.show(cpos=cpos) |
| 54 |
|
| 55 |
# %% |
| 56 |
|
| 57 |
pl = pv.Plotter() |
| 58 |
pl.add_mesh(grid.extract_all_edges(), color='k', line_width=1) |
| 59 |
pl.add_mesh(centers, color='r', point_size=8.0, render_points_as_spheres=True) |
| 60 |
pl.show(cpos=cpos) |
| 61 |
|
| 62 |
|
| 63 |
# %% |
| 64 |
# Edge centers |
| 65 |
# ~~~~~~~~~~~~ |
| 66 |
# You can use :func:`cell_centers <pyvista.DataObjectFilters.cell_centers>` in |
| 67 |
# combination with :func:`extract_all_edges |
| 68 |
# <pyvista.DataObjectFilters.extract_all_edges>` to get the center of all edges of |
| 69 |
# a mesh. |
| 70 |
|
| 71 |
# create a simple mesh and extract all the edges and then centers of the mesh. |
| 72 |
mesh = pv.Cube() |
| 73 |
edge_centers = mesh.extract_all_edges().cell_centers().points |
| 74 |
|
| 75 |
# Plot the edge centers |
| 76 |
pl = pv.Plotter() |
| 77 |
pl.add_mesh(mesh, show_edges=True, line_width=5) |
| 78 |
pl.add_points( |
| 79 |
edge_centers, |
| 80 |
render_points_as_spheres=True, |
| 81 |
color='r', |
| 82 |
point_size=20, |
| 83 |
) |
| 84 |
pl.show() |
| 85 |
|
| 86 |
|
| 87 |
# %% |
| 88 |
# Add labels to cells |
| 89 |
# ~~~~~~~~~~~~~~~~~~~ |
| 90 |
# There is not a method to add labels to cells. |
| 91 |
# If you want to label it, you need to extract the position to label it. |
| 92 |
|
| 93 |
# sphinx_gallery_start_ignore |
| 94 |
PYVISTA_GALLERY_FORCE_STATIC = True |
| 95 |
# sphinx_gallery_end_ignore |
| 96 |
grid = pv.ImageData(dimensions=(10, 10, 1)) |
| 97 |
points = grid.cell_centers().points |
| 98 |
|
| 99 |
pl = pv.Plotter() |
| 100 |
pl.add_mesh(grid, show_edges=True) |
| 101 |
pl.add_point_labels(points, labels=[f'{i}' for i in range(points.shape[0])]) |
| 102 |
pl.show(cpos='xy') |
| 103 |
# %% |
| 104 |
# .. tags:: filter |