pythonscipyconvex-hull

In scipy.spatial.Delaunay what does find_simplex() method return?


I need to find whether some of my points are inside or outside a convex hull and I was using this answer: (enter link description here).

But when I test it, the array that is returned is slightly confusing. For example if I create two identical arrays, use one to create a hull and then test whether points of the second are in that hull I get the following:

from scipy.spatial import Delaunay
pts_outer = np.array([[0, 0], [0, 5], [5, 0], [5, 5]])
pts_inner = pts_outer
hull = Delaunay(pts_outer)
hull.find_simplex(pts_inner)

Out[29]: array([0, 0, 1, 1], dtype=int32)

The documentation for the method only says that it returns: "Indices of simplices containing each point. Points outside the triangulation get the value -1".

I understand that 1 is point is inside the hull, -1 is the point outside, but what is 0? Is it point lying on the boundary of the hull? But then why only two points go it? It should be all of them.

If I slightly modify the test array:

pts_inner = np.array([[0, 0], [0, 5], [5, -1], [5, 5]])
hull.find_simplex(pts_inner)

Out[31]: array([ 0,  0, -1,  0], dtype=int32)

First two points got the same indices, third point got a -1 just as planned, but the fourth point has changed to 0 for some reason, even though both the hull and the point are exactly the same.

Does anyone know how to interpret these results?


Solution

  • tl;dr: It returns the index of a triangle containing the point. And it does not always pick the same index if there are multiple triangles containing it.

    I think you misunderstood "It returns:Indices of simplices containing each point. Points outside the triangulation get the value -1."

    My interpretation is that Delaunay(pts_outer) triangulates your rectangle with two triangles with the indices 0 and 1 respectively. Then hull.find_simplex(pts_inner) retruning [0, 0, 1, 1] means that your first two points are in triangle 0 and the 2nd two are in triangle one.

    Finally it is a bit odd that find_simplex now tells you your point [5, 5] is in triangle 1. But that is not incorrect since the point [5, 5] is in both triangles.