I am looking for distance from a point to a ConvexHull object in 3D in Python.
I found the questions that solve the problem in 2D: Distance to convexHull and Computing the distance to a convex hull
But those do not contain a solution for 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)
It would be great to have a function
dist(hull,points)
that returns a list of distances from the points to the convex hull, having different signs for points within and outside of the convex hull.
We can use PyGEL 3d python library for this.
First, install it with pip install PyGEL3D
Second, the code:
import numpy as np
from scipy.spatial import ConvexHull
from pygel3d import hmesh
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 = hmesh.Manifold()
for s in hull.simplices:
m.add_face(hull.points[s])
dist = hmesh.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))