在凸hu中获取点

2024-06-17 10:13:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两组重叠的点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)

如何进行空间查询?在


Tags: fromimportnumpyasnp空间scipyspatial
1条回答
网友
1楼 · 发布于 2024-06-17 10:13:45

下面是一个使用我在评论中发布的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)

这将在B的外壳中找到T的所有点,并将它们保存在new_points中。我也画出来了,这样你就能看到结果了

相关问题 更多 >