# Check if points are in/out segments

**URL:** <https://discourse.slicer.org/t/check-if-points-are-in-out-segments/41011>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [January 9, 2025, 8:54am UTC](https://discourse.slicer.org/t/check-if-points-are-in-out-segments/41011 "2025-01-09T08:54:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SANTIAGO\_PENDON\_MING](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/santiago_pendon_ming/32/66060_2.png) [@SANTIAGO\_PENDON\_MING](https://discourse.slicer.org/u/SANTIAGO_PENDON_MING)\
**Post date:** [January 9, 2025, 8:54am UTC](https://discourse.slicer.org/t/check-if-points-are-in-out-segments/41011/1 "2025-01-09T08:54:22Z")

</div>

Hi to everyone. A quick question for today 🙂

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

 ![Captura de pantalla 2025-01-09 094830](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/0/e0a46efb3ce717f824c418cc620a7ae0f0fb6f21.png)

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.

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [January 10, 2025, 7:48am UTC](https://discourse.slicer.org/t/check-if-points-are-in-out-segments/41011/2 "2025-01-10T07:48:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [January 10, 2025, 8:13am UTC](https://discourse.slicer.org/t/check-if-points-are-in-out-segments/41011/3 "2025-01-10T08:13:54Z")

</div>

> [@SANTIAGO\_PENDON\_MING](#):
>
> are in/out

You may investigate vtkExtractEnclosedPoints and vtkSelectEnclosedPoints.

---

<div class="post-metadata">

**Author:** ![SANTIAGO\_PENDON\_MING](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/santiago_pendon_ming/32/66060_2.png) [@SANTIAGO\_PENDON\_MING](https://discourse.slicer.org/u/SANTIAGO_PENDON_MING)\
**Post date:** [January 10, 2025, 9:51am UTC](https://discourse.slicer.org/t/check-if-points-are-in-out-segments/41011/4 "2025-01-10T09:51:08Z")

</div>

> [@chir.set](#):
>
> vtkSelectEnclosedPoints

That was the solution!

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

```auto

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'}")

```
