pyvista.DataSet.cell_neighbors_levels#

DataSet.cell_neighbors_levels(
ind: int,
connections: str = 'points',
n_levels: int = 1,
) Generator[list[int], None, None][source]#

Get consecutive levels of cell neighbors.

Parameters:
indint

Cell ID.

connectionsstr, default: “points”

Describe how the neighbor cell(s) must be connected to the current cell to be considered as a neighbor. Can be either 'points', 'edges' or 'faces'.

n_levelsint, default: 1

Number of levels to search for cell neighbors. When equal to 1, it is equivalent to pyvista.DataSet.cell_neighbors().

Returns:
generator[list[int]]

A generator of list of cell IDs for each level.

Examples

Get the cell neighbors IDs starting from the 0-th cell up until the third level.

>>> import pyvista as pv
>>> mesh = pv.Sphere(theta_resolution=10)
>>> nbr_levels = mesh.cell_neighbors_levels(
...     0, connections="edges", n_levels=3
... )
>>> nbr_levels = list(nbr_levels)
>>> nbr_levels[0]
[1, 21, 9]
>>> nbr_levels[1]
[2, 8, 74, 75, 20, 507]
>>> nbr_levels[2]
[128, 129, 3, 453, 7, 77, 23, 506]

Visualize these cells IDs.

>>> from functools import partial
>>> pv.global_theme.color_cycler = [
...     'red',
...     'green',
...     'blue',
...     'purple',
... ]
>>> pl = pv.Plotter()
>>>
>>> # Define partial function to add point labels
>>> add_point_labels = partial(
...     pl.add_point_labels,
...     text_color="white",
...     font_size=40,
...     shape=None,
...     show_points=False,
... )
>>>
>>> # Add the 0-th cell to the plotter
>>> cell = mesh.extract_cells(0)
>>> _ = pl.add_mesh(cell, show_edges=True)
>>> _ = add_point_labels(cell.cell_centers().points, labels=["0"])
>>> other_ids = [0]
>>>
>>> # Add the neighbors to the plot
>>> neighbors = mesh.cell_neighbors_levels(
...     0, connections="edges", n_levels=3
... )
>>> for i, ids in enumerate(neighbors, start=1):
...     cells = mesh.extract_cells(ids)
...     _ = pl.add_mesh(cells, show_edges=True)
...     _ = add_point_labels(
...         cells.cell_centers().points, labels=[f"{i}"] * len(ids)
...     )
...     other_ids.extend(ids)
...
>>>
>>> # Add the cell IDs that are not neighbors (ie. the rest of the sphere)
>>> cells = mesh.extract_cells(other_ids, invert=True)
>>> _ = pl.add_mesh(cells, color="white", show_edges=True)
>>>
>>> pl.view_xy()
>>> pl.camera.zoom(6.0)
>>> pl.show()
../../../_images/pyvista-DataSet-cell_neighbors_levels-1_00_00.png