# Cross section analysis in VMTK module

**URL:** <https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097>\
**Category:** Support\
**Created:** [February 1, 2024, 8:20pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097 "2024-02-01T20:20:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michaelli/32/67910_2.png) [@michaelli](https://discourse.slicer.org/u/michaelli)\
**Post date:** [February 1, 2024, 8:20pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/1 "2024-02-01T20:20:56Z")

</div>

![Screenshot_20240113_152913](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/0/a0510361618b1a688ccbdb35b9c1178c8040cac4.png)  
I can convert a cross section and convert to a segmentation and then convert it back to a model; then go to the next cross section by browsing in the cross sectional analysis module. Finally, I can save them to STL. This is tedious. Is there is way to export multiple cross sections at a time? Say, I want to resample the centerline to have 20 control points, then export all the cross sections at these control points.

Also, how to get the center (coordinates) of a cross section?

---

<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:** [February 1, 2024, 9:01pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/2 "2024-02-01T21:01:12Z")

</div>

> [@michaelli](#):
>
> Is there is way to export multiple cross sections at a time?

If I have fully understood your requirements, this step can be done through scripting. A good start is as follows:

```auto
import CrossSectionAnalysis
logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()
centerline = getNode("Centerline curve (0)")
segmentation = getNode("Segmentation")
logic.setInputCenterlineNode(centerline)
logic.setLumenSurface(segmentation, "Segment_1")
logic.run()
for i in range(20):
    crossSectionPolyData = logic.computeCrossSectionPolydata(i)
    # Add your export code to STL here

```

You must add more code to export the crossSectionPolyData to STL.

> [@michaelli](#):
>
> how to get the center (coordinates) of a cross section?

For me, ‘centre’ refers to a regular geometry, like circle, rectangle, cube… Do you mean the control point coordinates of your resampled curve? They are available in the ‘Markups’ module.

---

<div class="post-metadata">

**Author:** ![michaelli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michaelli/32/67910_2.png) [@michaelli](https://discourse.slicer.org/u/michaelli)\
**Post date:** [February 1, 2024, 10:00pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/3 "2024-02-01T22:00:50Z")

</div>

![Screenshot_20240201_165426](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/5/f5cedbcbc0ee38343ece42f08abda8c24e0c5623.png)

@chir.set, thank you so much! Your method seems promising. I’m not familiar with script, but I get your code run in the python console and it seems working. Can you tell me how to export crossSectionPolyData to STL, how to convert them to models so that I can view them in Slicer?

For the second question about the center, if the sections are exactly extracted at the centerline control points, then I can get the coordinates from the centerline table.

---

<div class="post-metadata">

**Author:** ![michaelli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michaelli/32/67910_2.png) [@michaelli](https://discourse.slicer.org/u/michaelli)\
**Post date:** [February 1, 2024, 10:01pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/4 "2024-02-01T22:01:54Z")

</div>

As you can see in the snapshot, I have the centerline extracted and resampled.

---

<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:** [February 1, 2024, 10:33pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/5 "2024-02-01T22:33:07Z")

</div>

> [@michaelli](#):
>
> convert them to models

To create a model from polydata:

`model = slicer.modules.models.logic().AddModel(crossSectionPolyData)`

As for the STL thing, I’m not familiar with it. May be someone else could kick in.

---

<div class="post-metadata">

**Author:** ![michaelli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michaelli/32/67910_2.png) [@michaelli](https://discourse.slicer.org/u/michaelli)\
**Post date:** [February 1, 2024, 10:54pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/6 "2024-02-01T22:54:38Z")

</div>

![1](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/e/9ead32637a5d3d3ea4277ab2636a3848cfeca450.png)  
Cool. I got 20 cross sections in a second. Unfortunately, they are not at the control points. Did I miss something with my centerline curve? I just resampled it to have 20 control points.

---

<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:** [February 2, 2024, 7:49am UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/7 "2024-02-02T07:49:33Z")

</div>

> [@michaelli](#):
>
> Did I miss something with my centerline curve?

Not really, the code works on curve points while you are expecting a result at control points. The update below should do:

```auto
import CrossSectionAnalysis
logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()
centerline = getNode("Centerline curve (0)")
segmentation = getNode("Segmentation")
logic.setInputCenterlineNode(centerline)
logic.setLumenSurface(segmentation, "Segment_1")
logic.run()
numberOfControlPoints = centerline.GetNumberOfControlPoints()
controlPointCoordinates = [0.0, 0.0, 0.0]
for i in range(numberOfControlPoints):
    centerline.GetNthControlPointPositionWorld(i, controlPointCoordinates)
    pointId = centerline.GetCurveWorld().FindPoint(controlPointCoordinates)
    crossSectionPolyData = logic.computeCrossSectionPolydata(pointId)
    model = slicer.modules.models.logic().AddModel(crossSectionPolyData)

```

---

<div class="post-metadata">

**Author:** ![michaelli](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michaelli/32/67910_2.png) [@michaelli](https://discourse.slicer.org/u/michaelli)\
**Post date:** [February 2, 2024, 1:40pm UTC](https://discourse.slicer.org/t/cross-section-analysis-in-vmtk-module/34097/8 "2024-02-02T13:40:11Z")

</div>

I really appreciate your great help. It works perfectly. I got the cross sections at the desired positions.

I would like to share the complete code with renaming and saving the models here in case it is of interest to other people.

```auto
import CrossSectionAnalysis
logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()
centerline = getNode("Centerline curve (0)")
segmentation = getNode("Segmentation")
logic.setInputCenterlineNode(centerline)
logic.setLumenSurface(segmentation, "Segment_1")
logic.run()
numberOfControlPoints = centerline.GetNumberOfControlPoints()
controlPointCoordinates = [0.0, 0.0, 0.0]
for i in range(numberOfControlPoints):
    centerline.GetNthControlPointPositionWorld(i, controlPointCoordinates)
    pointId = centerline.GetCurveWorld().FindPoint(controlPointCoordinates)
    crossSectionPolyData = logic.computeCrossSectionPolydata(pointId)
    model = slicer.modules.models.logic().AddModel(crossSectionPolyData)
    
    model.SetName(f"Secion{i}")
    
    filename = os.path.basename(model.GetName())
    filepath = filename + ".stl"
    slicer.util.exportNode(model, filepath)    

```
