x可以是一个点也可以是一堆点,要找x邻域内的点。
r是搜索的半径。
eps是一个非负的float型小数,如果最近邻的点距离比r/(1+eps)还大,则不再进行搜索。
返回找到的点的索引。
from scipy.spatial import cKDTree
#point cloud data -> kdtree
pcl_kdtree = cKDTree(pcd_reconstruction_points)
indices = pcl_kdtree.query_ball_point(voxel_center, 50)
count = len(indices)
>>> import numpy as np
>>> from scipy import spatial
>>> x, y = np.mgrid[0:4, 0:4]
>>> x,y
(array([[0, 0, 0, 0],
[1, 1, 1, 1],
[2, 2, 2, 2],
[3, 3, 3, 3]]), array([[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3],
[0, 1, 2, 3]]))
>>> x.ravel()
array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])
>>> points = np.c_[x.ravel(), y.ravel()]
>>> points
array([[0, 0],
[0, 1],
[0, 2],
[0, 3],
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[3, 0],
[3, 1],
[3, 2],
[3, 3]])
>>> tree = spatial.cKDTree(points)
>>> tree
<scipy.spatial.ckdtree.cKDTree object at 0x000001A2EF116F20>
>>> tree.query_ball_point([2, 0], 1)
[4, 8, 9, 12]
>>> points[4]
array([1, 0])
>>> points[8]
array([2, 0])
>>> points[9]
array([2, 1])
>>> points[12]
array([3, 0])
上面是二维空间的例子,可以看到找(2,0)邻域内距离是1的点,返回了包括自身在内的4个点的索引。