【问题标题】:Find the projection of a point on the convex hull with Scipy用 Scipy 在凸包上找到一个点的投影
【发布时间】:2017-07-04 01:03:58
【问题描述】:

从一组点来看,我得到了带有scipy.spatial 的凸包,可以使用Delaunay 或ConvexHull(来自qhull 库)。 现在我想将这个凸包外的一个点投影到外壳上(即外壳上与外面的点距离最小的点)。

这是我目前的代码:

from scipy.spatial import Delaunay, ConvexHull
import numpy as np

hu = np.random.rand(10, 2) ## the set of points to get the hull from
pt = np.array([1.1, 0.5]) ## a point outside
pt2 = np.array([0.4, 0.4]) ## a point inside
hull = ConvexHull(hu) ## get only the convex hull
#hull2 = Delaunay(hu) ## or get the full Delaunay triangulation

import matplotlib.pyplot as plt
plt.plot(hu[:,0], hu[:,1], "ro") ## plot all points
#plt.triplot(hu[:,0], hu[:,1], hull2.simplices.copy()) ## plot the Delaunay triangulation
## Plot the convexhull
for simplex in hull.simplices:
    plt.plot(hu[simplex,0], hu[simplex,1], "ro-")

## Plot the points inside and outside the convex hull 
plt.plot(pt[0], pt[1], "bs")
plt.plot(pt2[0], pt2[1], "bs")
plt.show()

有了图片可能更容易,我想从凸包外的蓝点获取绿色的 x 和 y 坐标。这个例子是二维的,但我也需要在更高维度上应用它。 感谢您的帮助。

编辑:问题已在此处解决,但我无法实施:https://mathoverflow.net/questions/118088/projection-of-a-point-to-a-convex-hull-in-d-dimensions

【问题讨论】:

  • 可以从ConvexHull.equations得到凸包的超平面,这样就可以计算出点到每个超平面的距离。然后,您可以使用距离找到最近的超平面。但是,您应该按照自己的方式定义坐标,因为您只有法线向量。

标签: python scipy delaunay qhull


【解决方案1】:

我在回答自己。 正如 0Tech 指出的那样,ConvexHull.equations 为您提供每个平面的平面方程(因此在 2d 中——因此是一条线),其形式为:[A, B, C]。因此,该平面由

定义
A*x + B*y + C = 0

在平面上投影点 P=(x0, y0) 解释here。您希望向量上的一个点平行于平面向量 (A, B) 并通过该点投影到 P,这条线由 t 参数化:

P_proj = (x, y) = (x0 + A*t, y0 + B*t)

然后你希望你的点在平面上并使用全平面方程来做到这一点:

A*(x0 + A*t) + B*(y0 + B*t) + C = 0
=> t=-(C + A*x0 + B*y0)/(A**2+B**2)

在(笨拙的)python 中,它适用于任何维度:

from scipy.spatial import Delaunay, ConvexHull
import numpy as np

hu = np.random.rand(10, 2) ## the set of points to get the hull from
pt = np.array([1.1, 0.5]) ## a point outside
pt2 = np.array([0.4, 0.4]) ## a point inside
hull = ConvexHull(hu) ## get only the convex hull
#hull2 = Delaunay(hu) ## or get the full Delaunay triangulation

import matplotlib.pyplot as plt
plt.plot(hu[:,0], hu[:,1], "ro") ## plot all points
#plt.triplot(hu[:,0], hu[:,1], hull2.simplices.copy()) ## plot the Delaunay triangulation
## Plot the convexhull
for simplex in hull.simplices:
    plt.plot(hu[simplex,0], hu[simplex,1], "ro-")

## Plot the points inside and outside the convex hull 
plt.plot(pt[0], pt[1], "bs")
plt.plot(pt2[0], pt2[1], "bs")

for eq in hull.equations:
    t = -(eq[-1] + np.dot(eq[:-1], pt))/(np.sum(eq[:-1]**2))
    pt_proj = pt + eq[:-1]*t
    plt.plot(pt_proj[0], pt_proj[1], "gD-.")
plt.show()

浏览stackoverflow,让我找到了另一种解决方案,它的优点是使用线段而不是线,因此其中一个线段上的投影始终位于线段上:

def min_distance(pt1, pt2, p):
    """ return the projection of point p (and the distance) on the closest edge formed by the two points pt1 and pt2"""
    l = np.sum((pt2-pt1)**2) ## compute the squared distance between the 2 vertices
    t = np.max([0., np.min([1., np.dot(p-pt1, pt2-pt1) /l])]) # I let the answer of question 849211 explains this
    proj = pt1 + t*(pt2-pt1) ## project the point
    return proj, np.sum((proj-p)**2) ## return the projection and the point

然后我们可以浏览每个顶点并投影点:

for i in range(len(hull.vertices)):
    pt_proj, d = min_distance(hu[hull.vertices[i]], hu[hull.vertices[(i+1)%len(hull.vertices)]], pt)
    plt.plot([pt[0], pt_proj[0]], [pt[1], pt_proj[1]], "c<:")

还有图片,每个平面(线)右侧的蓝色点的投影,第一种方法为绿色,第二种方法为青色:

【讨论】:

  • 第一个解决方案适用于任何维度,但投影不一定在凸包上。
  • 查看答案math.stackexchange.com/questions/2146961/…,它给出了一个使用带约束的二次优化的解决方案,但对于高维空间来说这可能太慢了。
【解决方案2】:

由于这里(或任何地方)似乎仍然没有一个好的答案,我关注this post(以及其他)并使用二次规划解决了这个问题:

$$ \begin{align}\text{minimize} & \quad \frac{1}{2} x^{T} \mathbf{I} x - z^{T} x\\ \text{subject to} & \quad C x \leq b\\ \end{align} $$

其中$C$ 是正规方程,$b$ 是偏移量。这里的主要区别是我将点投影到凸包内,而不一定要投影到它上面。即凸包内的点将保持不变,而凸包外的点将投影到凸包中最近的点(始终位于其表面上)。通过删除这个等式约束,这是一个相对简单的二次规划问题,我使用quadprog 包解决了这个问题。

理论上可能有更快的方法来做到这一点,但这种方法足够快、简单且健壮:

import numpy as np
from scipy.spatial import ConvexHull
from quadprog import solve_qp

def proj2hull(z, equations):
    """
    Project `z` to the convex hull defined by the
    hyperplane equations of the facets

    Arguments
        z: array, shape (ndim,)
        equations: array shape (nfacets, ndim + 1)

    Returns
        x: array, shape (ndim,)
    """
    G = np.eye(len(z), dtype=float)
    a = np.array(z, dtype=float)
    C = np.array(-equations[:, :-1], dtype=float)
    b = np.array(equations[:, -1], dtype=float)
    x, f, xu, itr, lag, act = solve_qp(G, a, C.T, b, meq=0, factorized=True)
    return x

一个简单的例子:

X = np.random.normal(size=(1000, 5))
z = np.random.normal(scale=2, size=(5))
hull = ConvexHull(X)
y = proj2hull(z, hull.equations)

(编辑:抱歉,Latex 似乎没有格式化)

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 1970-01-01
    • 2021-10-23
    • 2021-08-22
    • 2013-01-21
    • 2022-01-23
    • 1970-01-01
    • 2015-10-12
    • 2015-11-27
    相关资源
    最近更新 更多