简体   繁体   English

计算到凸包的距离

[英]Computing the distance to a convex hull

I am using the ConvexHull class of scipy to construct a convex hull for a set of points. 我使用ConvexHull类scipy为一组点构造一个凸包。 I am interested in a way to compute the minimum distance of a new point P from the convex hull. 我感兴趣的是计算新的P点与凸包的最小距离的方法。

With the help of the internet and a little tweaking by myself I came up with this formula to compute the distance of a point P or a set of points points to the convex hull facets: 在互联网的帮助和我自己的一点调整后,我想出了这个公式来计算点P的距离或一组点的到凸面的小平面:

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

For a convex hull in 2D the equation above will result in the following plot: 对于2D中的凸包,上面的等式将得到以下图:

与凸壳的距离

As you can see the result is pretty good and correct for points within the convex hull (The distance here is negative and would need to be multiplied with -1 ). 正如您所看到的,对于凸包内的点,结果非常好并且正确(这里的距离是负的,需要乘以-1 )。 It is also correct for points that are closest to a facet but incorrect for points that are closest to a vertex of the convex hull. 对于最接近小平面但对于最接近凸包顶点的点不正确的点也是正确的。 (I marked these regions with the dashed lines) For these points the correct minimum distance would be the minimum distance to the convex hull vertices. (我用虚线标记这些区域)对于这些点,正确的最小距离是到凸包顶点的最小距离。

How can I distinguish between points that are closest to a facet or closest to a vertex to correctly compute the minimum distance to the convex hull for a point P or a set of points points in an n-Dimensional space (At least 3D)? 如何区分最接近小平面或最接近顶点的点,以正确计算点P的凸包的最小距离或n维空间中的一组点的 (至少3D)?

if the points of the convex hull are given as a NX2 array and the point is given as p=[x,y] 如果凸包的点作为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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM