find_closest_cell

DataSet.find_closest_cell(point: Union[int, numpy.ndarray]) Union[int, numpy.ndarray]

Find index of closest cell in this mesh to the given point.

Parameters
pointiterable(float) or np.ndarray

Coordinates of point to query (length 3) or a numpy array of n points with shape (n, 3).

Returns
int or numpy.ndarray

Index or indices of the cell in this mesh that is closest to the given point.

Warning

This method may still return a valid cell index even if the point contains a value like numpy.inf or numpy.nan.

Examples

Find nearest cell on a sphere centered on the origin to the point [0.1, 0.2, 0.3].

>>> import pyvista
>>> mesh = pyvista.Sphere()
>>> point = [0.1, 0.2, 0.3]
>>> index = mesh.find_closest_cell(point)
>>> index
591

Make sure that this cell indeed is the closest to [0.1, 0.2, 0.3].

>>> import numpy as np
>>> cell_centers = mesh.cell_centers()
>>> relative_position = cell_centers.points - point
>>> distance = np.linalg.norm(relative_position, axis=1)
>>> np.argmin(distance)
591

Find the nearest cells to several random points that are centered on the origin.

>>> points = 2 * np.random.random((5000, 3)) - 1
>>> indices = mesh.find_closest_cell(points)
>>> indices.shape
(5000,)