在Python中从3d点的凸起船体的距离

问题描述 投票:1回答:1

我正在寻找Python中3D点的ConvexHull对象的距离。

我找到了解决2D问题的问题:Distance to convexHullComputing the distance to a convex hull

但那些不包含3D解决方案。

import numpy as np
from scipy.spatial import ConvexHull

mat = np.random.rand(100,3)
hull = ConvexHull(mat)
points = np.random.rand(10,3)

有一个功能会很棒

dist(hull,points)

返回从点到凸包的距离列表,对于凸包内外的点具有不同的符号。

python 3d distance convex-hull
1个回答
1
投票

我们可以使用PyGEL 3d python库。

首先,使用pip install PyGEL3D安装它

二,代码:

import numpy as np
from scipy.spatial import ConvexHull
from PyGEL3D import gel

mat = np.random.rand(100, 3)
hull = ConvexHull(mat)
points = np.random.rand(10, 3)

def dist(hull, points):
    # Construct PyGEL Manifold from the convex hull
    m = gel.Manifold()
    for s in hull.simplices:
        m.add_face(hull.points[s])

    dist = gel.MeshDistance(m)
    res = []
    for p in points:
        # Get the distance to the point
        # But don't trust its sign, because of possible
        # wrong orientation of mesh face
        d = dist.signed_distance(p)

        # Correct the sign with ray inside test
        if dist.ray_inside_test(p):
            if d > 0:
                d *= -1
        else:
            if d < 0:
                d *= -1
        res.append(d)
    return np.array(res)

print(dist(hull, points))
© www.soinside.com 2019 - 2024. All rights reserved.