# Intersection between model and volume to select voxels

**URL:** <https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256>\
**Category:** Development\
**Tags:** extensions-manager, 3d-model\
**Created:** [October 2, 2018, 1:45pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256 "2018-10-02T13:45:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [October 2, 2018, 1:45pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/1 "2018-10-02T13:45:12Z")

</div>

I have a generated a surface model by applying vtkMarchingCubes on a loaded volume. The next step I would like to perform is to identify which voxels intersect with the triangles that were generated. I was thinking of starting with iterating the vertices to see which voxels overlap and do some triangle intersect from there but I am not sure if that is the best method. Doe anybody know if there is a better way to select voxels that are inside, outside or on the contour of a model? I am using python.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 2, 2018, 1:49pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/2 "2018-10-02T13:49:00Z")

</div>

What are you trying to achieve in general? I have the feeling that there should be an easier way for that in Slicer compared to low-level VTK programming.

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [October 2, 2018, 1:58pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/3 "2018-10-02T13:58:12Z")

</div>

Yes, you might be right. I would like to dilate the outer layer of the scan to replace this gradient-artifact in the scan with the color of the inside voxels. So only the replacing of this thin outer layer and leave the inside voxels untouched.

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [October 2, 2018, 2:00pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/4 "2018-10-02T14:00:51Z")

</div>

I found this Module: [https://www.slicer.org/wiki/Documentation/Nightly/Extensions/VolumeClip](https://www.slicer.org/wiki/Documentation/Nightly/Extensions/VolumeClip)

So my guess is that in slicer there must be a way to identify which voxels are inside a model?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 2, 2018, 2:15pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/5 "2018-10-02T14:15:15Z")

</div>

There is a complete infrastructure for this in the past few years called Segmentations. If you convert your model to a segmentation then set the reference volume to your volume in Segmentations module, then you can do all kinds of computations and editing.

How I’d do it:

- Use latest 4.9.0 nightly
- Create new segmentation node in Segmentations module
- In Representations section make Closed surface as master (because you want to have control over the geometry of the labelmap afterwards)
- Import model in Import/Export section
- In Representations section long-click Create and choose Advanced create
- Click the only path in the top part
- Click Specify geometry button in the bottom part
- Select your volume

Now you have a segmentation node which contains the labelmap representation of your model in the exact grid as your volume. Now you can do all kinds of things with it in the Segment Editor module (click yes when it asks if you want to change master to labelmap), such as dilation in the Margin effect. If you only need the dilated part then you can use Logical operators to copy the original segment, dilate the copy, and subtract the original from the dilated. You can calculate statistics in the Segment Statistics module.

---

<div class="post-metadata">

**Author:** ![Niels](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Niels](https://discourse.slicer.org/u/Niels)\
**Post date:** [October 2, 2018, 2:44pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/6 "2018-10-02T14:44:18Z")

</div>

Hi cpinter, thanks for the quick reply. I am not familiar with segmentations, I will try the steps you specify. If I understand correctly a segment model is created from the model that can be assigned to the volume to perform segment operations? In the end I would like to automate this process in a script and use the marchingcubes isovalue as a guide for automatically regenerating this segmentation.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [October 2, 2018, 2:50pm UTC](https://discourse.slicer.org/t/intersection-between-model-and-volume-to-select-voxels/4256/7 "2018-10-02T14:50:51Z")

</div>

You can do the whole segmentation in Segment Editor, and then you don’t have to even start from a model. You can do all that from python.

Here’s some information about Segmentations:  
[https://www.slicer.org/wiki/Documentation/Nightly/Modules/Segmentations](https://www.slicer.org/wiki/Documentation/Nightly/Modules/Segmentations)  
And about driving Segment Editor from python:  
[https://discourse.slicer.org/search?q=segment%20editor%20python](https://discourse.slicer.org/search?q=segment%20editor%20python) (this is just a discourse search, please use this resource, a lot of questions have been answered already)  
I think for your use case you can do the segmentation as a simple threshold, it gives you the same geometry as an isosurface (both are directly based on voxel intensity).
