Python 中 3d 中的点到凸包的距离
Distance to convex hull from point in 3d in Python
我正在 Python 中寻找从点到 3D ConvexHull 对象的距离。
我找到了解决二维问题的问题:
Distance to convexHull 和
Computing 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)
即returns从点到凸包的距离列表,对于凸包内外的点具有不同的符号。
我们可以为此使用 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))
from scipy.spatial import distance_matrix, distance
import numpy as np
#point from which distance is to be calculated
reference_point = np.array([1.28442705, 6.75384521e-01, 9.99999997e-07]).reshape(1,3)
#any point/points from convexhull
p = np.array([[1.2844270500,6.75384521e01,9.9999999707],
[1.2743135700,7.84526169e01,9.9999999707],[1.2844270500,6.7538452101,8.7603122001]])
distance_matrix = distance.cdist(reference_point, p, 'euclidean')
我正在 Python 中寻找从点到 3D ConvexHull 对象的距离。
我找到了解决二维问题的问题: Distance to convexHull 和 Computing 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)
即returns从点到凸包的距离列表,对于凸包内外的点具有不同的符号。
我们可以为此使用 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))
from scipy.spatial import distance_matrix, distance
import numpy as np
#point from which distance is to be calculated
reference_point = np.array([1.28442705, 6.75384521e-01, 9.99999997e-07]).reshape(1,3)
#any point/points from convexhull
p = np.array([[1.2844270500,6.75384521e01,9.9999999707],
[1.2743135700,7.84526169e01,9.9999999707],[1.2844270500,6.7538452101,8.7603122001]])
distance_matrix = distance.cdist(reference_point, p, 'euclidean')