# How to judge whether a point in a space is in the ROI?

**URL:** <https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795>\
**Category:** Development\
**Tags:** python\
**Created:** [September 22, 2021, 12:09am UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795 "2021-09-22T00:09:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 22, 2021, 12:09am UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795/1 "2021-09-22T00:09:41Z")

</div>

How to judge whether a point in a space is in the ROI?

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 22, 2021, 10:14am UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795/2 "2021-09-22T10:14:53Z")

</div>

没人回…换一个方式吧.怎么得到ROI的bounds,就是说那6个顶点的坐标呢

> No reply… I change the qestion to … how to get the bounds of the ROI, that is to say the coordinates of the 6 vertices?..

我想把ROI先转换成vtkcube, 再用`IsInsideSurface`这个方法去判断,可是没成功…哪位大神帮帮我

> I want to convert the ROI to vtkcube first, and then use the method `IsInsideSurface` in [VTK: vtkSelectEnclosedPoints Class Reference](https://vtk.org/doc/release/5.10/html/classvtkSelectEnclosedPoints.html) to judge, but it didn’t work…Which god can help me?

@lassoan@Juicy@jamesobutler @jcfr @pieper

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 22, 2021, 10:24am UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795/3 "2021-09-22T10:24:30Z")

</div>

我试了一下`GetPolyData()`这个方法,得到了一条线段

> I tried the method `GetPolyData()` to annotation ROI and got a line segment…

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 22, 2021, 12:23pm UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795/4 "2021-09-22T12:23:54Z")

</div>

The annotations ROI stores center and corner point (radius) so you can check if the point is inside center +/- radius in each dimension.

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#write-annotation-roi-to-json-file](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#write-annotation-roi-to-json-file)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 22, 2021, 5:02pm UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795/5 "2021-09-22T17:02:30Z")

</div>

If you use the new markups ROI node then you can get an implicit function that you can use to quickly query points (and also use in all VTK filters that take implicit functions as inputs). Example:

```python
roiNode = getNode('R')
point = [73,45,-1181]

planes = vtk.vtkPlanes()
roiNode.GetPlanes(planes)
isInside = planes.EvaluateFunction(point) < 0
print(f"Point {point} is {'inside' if isInside else 'outside'}")

```

The sign of the implicit function returns distance from the ROI boundary. Negative value means that the point is inside.

---

<div class="post-metadata">

**Author:** ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)\
**Post date:** [September 22, 2021, 9:43pm UTC](https://discourse.slicer.org/t/how-to-judge-whether-a-point-in-a-space-is-in-the-roi/19795/7 "2021-09-22T21:43:34Z")

</div>

> [@lassoan](#):
>
> ```auto
> roiNode = getNode('R')
> point = [73,45,-1181]
> 
> planes = vtk.vtkPlanes()
> roiNode.GetPlanes(planes)
> isInside = planes.EvaluateFunction(point) < 0
> print(f"Point {point} is {'inside' if isInside else 'outside'}")
> 
> ```

This method is wonderful! Thank you God
