pyvista.DataSet.find_closest_point

pyvista.DataSet.find_closest_point#

DataSet.find_closest_point(point: Iterable[float], n: int = 1) int | VectorLike[int][source]#

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

If wanting to query many points, use a KDTree with scipy or another library as those implementations will be easier to work with.

See: pyvista/pyvista-support#107

Parameters:
pointsequence[float]

Length 3 coordinate of the point to query.

nint, optional

If greater than 1, returns the indices of the n closest points.

Returns:
int

The index of the point in this mesh that is closest to the given point.

Examples

Find the index of the closest point to (0, 1, 0).

>>> import pyvista as pv
>>> mesh = pv.Sphere()
>>> index = mesh.find_closest_point((0, 1, 0))
>>> index
239

Get the coordinate of that point.

>>> mesh.points[index]
pyvista_ndarray([-0.05218758,  0.49653167,  0.02706946], dtype=float32)