在凸包中获取点

时间:2018-11-10 00:07:22

标签: python python-3.x scipy

我有两个重叠的点T和B。

我想从T返回B的凸包内的所有点 我按如下方式计算凸包

from scipy.spatial import Convexhull
import numpy as np
T=np.asarray(T)
B=np.asarray(B)

Thull = ConvexHull(T)
Bhull = ConvexHull(B)

如何进行空间查询?

1 个答案:

答案 0 :(得分:0)

以下是您想要使用我在评论中发布的other question中定义的功能的示例:

from scipy.spatial import Delaunay
import numpy as np
import matplotlib.pyplot as plt

def in_hull(p, hull):
    """
    Test if points in `p` are in `hull`

    `p` should be a `NxK` coordinates of `N` points in `K` dimensions
    `hull` is either a scipy.spatial.Delaunay object or the `MxK` array of the 
    coordinates of `M` points in `K`dimensions for which Delaunay triangulation
    will be computed
    """
    if not isinstance(hull,Delaunay):
        hull = Delaunay(hull)

    return hull.find_simplex(p)>=0

T = np.random.rand(30,2)
B = T + np.array([[0.4, 0] for i in range(30)])

plt.plot(T[:,0], T[:,1], 'o')
plt.plot(B[:,0], B[:,1], 'o')


new_points = T[in_hull(T,B)]

plt.plot(new_points[:,0], new_points[:,1], 'x', markersize=8)

这会在T的船体中找到B的所有点,并将它们保存在new_points中。我也将其绘制出来,以便您看到结果