pyvista.DataSet.find_containing_cell

pyvista.DataSet.find_containing_cell#

DataSet.find_containing_cell(
point: VectorLike[float] | MatrixLike[float],
) int | NumpyArray[int][source]#

Find index of a cell that contains the given point.

Parameters:
pointVectorLike[float] | MatrixLike[float],

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

Returns:
int or numpy.ndarray

Index or indices of the cell in this mesh that contains the given point.

Changed in version 0.35.0: Inputs of shape (1, 3) now return a numpy.ndarray of shape (1,).

Examples

A unit square with 16 equal sized cells is created and a cell containing the point [0.3, 0.3, 0.0] is found.

>>> import pyvista as pv
>>> mesh = pv.ImageData(dimensions=[5, 5, 1], spacing=[1 / 4, 1 / 4, 0])
>>> mesh
ImageData...
>>> mesh.find_containing_cell([0.3, 0.3, 0.0])
5

A point outside the mesh domain will return -1.

>>> mesh.find_containing_cell([0.3, 0.3, 1.0])
-1

Find the cells that contain 1000 random points inside the mesh.

>>> import numpy as np
>>> points = np.random.default_rng().random((1000, 3))
>>> indices = mesh.find_containing_cell(points)
>>> indices.shape
(1000,)