Check if points are in/out segments

Hi to everyone. A quick question for today :slight_smile:

My goal is to know what points (contained in a fiducialsNode) are in/out one segment. Like in the photo:

At this moment I use the labelmap representation of the segment and the points in IJK coordinates. I’m wondering if exist a way to do it directly using the points in RAS coordinates and the segment.

Thanks.

Hi,

This is a very interesting topic. I find it particularly useful for cleaning geometries and point clouds. Please let me know if you’ve managed to resolve the issue.

Thanks.

1 Like

You may investigate vtkExtractEnclosedPoints and vtkSelectEnclosedPoints.

That was the solution!

The vtkExtractEnclosedPoints I couldn’t make it work by the way. The basic code I made is:



segmentNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")  



segmentId = segmentNode.GetSegmentation().GetNthSegmentID(0)  
geometry = vtk.vtkPolyData()
segmentNode.GetClosedSurfaceRepresentation(segmentId, geometry)




points_array = np.load("puntos.npy")  




points = vtk.vtkPoints()
for point in points_array:
    points.InsertNextPoint(point)


pointCloud = vtk.vtkPolyData()
pointCloud.SetPoints(points)


enclosedPointsFilter = vtk.vtkSelectEnclosedPoints()
enclosedPointsFilter.SetInputData(pointCloud)
enclosedPointsFilter.SetSurfaceData(geometry)  
enclosedPointsFilter.Update()


inside_mask = []
for i in range(points.GetNumberOfPoints()):
    is_inside = enclosedPointsFilter.IsInside(i)  fuera
    inside_mask.append(is_inside)
    print(f"Point{i}: {'Inside' if is_inside else 'Outside'}")