Search code examples
pythonpyvista

How to check if a single point is inside a surface in pyvista?


So I have a closed mesh and a single point. How to check if one is inside another?

I tried:

def is_inside(point):
    points = pv.PolyData([point,])  
    point_in_question = points.points[0]
    select = mesh_model.select_enclosed_points(points)
    inside = select.threshold(0.5)
    if len(inside.points) >0:
            print(len(inside.points))
            print(f"inside atom {i}")
    else:
            print("outside")

yet I get not one but 1000+ points in len(inside.points). So how to check if one single point is inside a mesh?


Solution

  • As I suggested in comments, you should swap the point query mesh and the closed surface for the filter:

    import pyvista as pv
    
    mesh_model = pv.Sphere()
    points = [[0, 0, 0], [10, 10, 10]]
    points_poly = pv.PolyData(points)
    select = points_poly.select_enclosed_points(mesh_model)
    

    Now select has the 'SelectedPoints' point array which is 1 for the first point (inside) and 0 for the second point (outside):

    >>> select['SelectedPoints']
    array([1, 0], dtype=uint8)
    

    If you get an error about the surface not being closed (as you noted in comments), that means that your mesh_model is not closed. You can check

    mesh_model.n_open_edges
    

    which should be 0 for a manifold mesh.