【问题标题】:Computing the distance to a convex hull计算到凸包的距离
【发布时间】:2017-04-21 08:57:15
【问题描述】:

我正在使用 scipy 的 ConvexHull 类为一组点构造一个凸包。我对计算新点 P 到凸包的最小距离的方法很感兴趣。

借助互联网和我自己的一些调整,我想出了这个公式来计算一个点 P 或一组点 points 到凸包面:

np.max(np.dot(self.equations[:, :-1], points.T).T + self.equations[:, -1], axis=-1)

对于 2D 中的凸包,上面的等式将得出以下图:

如您所见,对于凸包内的点,结果非常好且正确(此处的距离为负数,需要乘以 -1)。对于最接近小平面的点也是正确的,但对于最接近凸包顶点的点不正确。 (我用虚线标记了这些区域)对于这些点,正确的最小距离是到凸包顶点的最小距离。

如何区分最接近小平面或最接近顶点的点,以正确计算点 P 或一组点 points 到凸包的最小距离 在 n 维空间(至少 3D)中?

【问题讨论】:

  • 对每个凸包段使用点到段公式并取最小值
  • @user4421975 您能否详细说明您的评论?什么是分段公式的点?
  • 对于每个点,使用stackoverflow.com/questions/849211/…计算它到凸包的每个线段的距离,并取最近的一个

标签: python numpy scipy distance convex-hull


【解决方案1】:

如果凸包的点以 NX2 数组的形式给出并且该点以 p=[x,y] 的形式给出

import math
#from http://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
def dist(x1,y1, x2,y2, x3,y3): # x3,y3 is the point
    px = x2-x1
    py = y2-y1

    something = px*px + py*py

    u =  ((x3 - x1) * px + (y3 - y1) * py) / float(something)

    if u > 1:
        u = 1
    elif u < 0:
        u = 0

    x = x1 + u * px
    y = y1 + u * py

    dx = x - x3
    dy = y - y3

    # Note: If the actual distance does not matter,
    # if you only want to compare what this function
    # returns to other results of this function, you
    # can just return the squared distance instead
    # (i.e. remove the sqrt) to gain a little performance

    dist = math.sqrt(dx*dx + dy*dy)

    return dist

dists=[]
for i in range(len(points)-1):
    dists.append(dist(points[i][0],points[i][1],points[i+1][0],points[i+1][1],p[0],p[1]))
dist = min(dists)

【讨论】:

  • 感谢您的回答。这也可以转换为 n 维空间吗?或者至少3d?我没有在我的问题中提到这一点,并将相应地编辑问题。
  • 如果你谷歌一下,我相信你可以改变公式以适应 3d
  • 可以在 min 后移动 sqrt 以减少操作。
猜你喜欢
  • 2014-07-19
  • 2020-12-21
  • 2019-08-22
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多