I am having trouble understanding how to compute whether an n-dimensional point is within an n-dimensional ConvexHull.
A very similar question (the same) was asked here: What's an efficient way to find if a point lies in the convex hull of a point cloud?
However, the answers are confusing me or don't seem to work for me, and I have no idea why.
def in_hull(p, hull):
""" Copied and from the Top Original answer """
from scipy.spatial import Delaunay
if not isinstance(hull,Delaunay):
hull = Delaunay(hull)
return hull.find_simplex(p)>=0
This function was giving me a lot of false or unwanted results with real data, that I am using it on. However, when debugging I wrote a simple script to test some obvious expectation I would have:
If I construct a ConvexHull out of a group of points, when I check that group of points for "membership", they should all be "members".
results_all = []
for _ in range(5000):
cloud = np.random.rand(5000, 2)
result = in_hull(cloud, cloud)
results_all.append(np.all(result))
arr = np.array(results_all)
print(np.sum(np.logical_not(arr)))
While rare, this seems to fail on randomly generated data (3 out of 5000), the problem is bigger on real data. What I mean by fail, is that I actually get some situations, where not all points are considered members.
Is there something I am doing wrong? Or perhaps completely misunderstanding? I am fairly confused at this point, so would love an explanation of what is going on.
In the end, I want; given a ConvexHull, computed at some previous stage; be able to determine if points lie within the hull.