# How to resample curve nodes programmatically?

**URL:** <https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395>\
**Category:** Development\
**Created:** [March 5, 2021, 6:43pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395 "2021-03-05T18:43:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 5, 2021, 6:43pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/1 "2021-03-05T18:43:34Z")

</div>

Operating system: windows 10  
Slicer version:4.11  
Expected behavior:  
Actual behavior:  
I just need to automatically resample a centerline curve to 20 control points from more than 100, similar to the resample section in markups module. Can anyone tell me where I can find documentation on the resample function or any code for it?

---

<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:** [March 5, 2021, 6:55pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/2 "2021-03-05T18:55:08Z")

</div>

Resampling is implemented in the [curve node](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Markups/MRML/vtkMRMLMarkupsCurveNode.cxx). Note that you can access this function from anywhere, not just from Slicer GUI and Python console, but from a command-line script, Jupyter, etc. so there should be no need to reimplement this feature outside Slicer.

---

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 5, 2021, 6:57pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/3 "2021-03-05T18:57:40Z")

</div>

> [@lassoan](#):
>
> an access this function from anywhere, not just from Slicer GUI and Python console, but from a command-line script

@lassoan I want to access it from the python console. Can you tell me how I can do that?

---

<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:** [March 5, 2021, 7:23pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/4 "2021-03-05T19:23:53Z")

</div>

For example, if name of your curve node is “C” then you can resample at 10mm sampling distance like this:

```python
getNode('C').ResampleCurveWorld(10)

```

---

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 10, 2021, 3:37pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/5 "2021-03-10T15:37:23Z")

</div>

Hey @lassoan , is there any way I can do this to vtkPolyData? I want to resample the vtkpolydata in the python code for extract centerline(centerline extraction logic)

---

<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:** [March 10, 2021, 3:43pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/6 "2021-03-10T15:43:10Z")

</div>

[Extract centerline](https://github.com/vmtk/SlicerExtension-VMTK/blob/master/ExtractCenterline/ExtractCenterline.py) module already does this. You can use this module as is or extract the code that you need from it.

---

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 10, 2021, 3:50pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/7 "2021-03-10T15:50:13Z")

</div>

@lassoan thanks. I see that extract centerline has these :  
centerlineFilter.SetCenterlineResampling(0)  
centerlineFilter.SetResamplingStepLength(1.0)

but they are set to 0 and 1. Do I have to change one of these for the actual resampling like resampleCurveWorld(10) above? I’m sorry if this is a stupid question

---

<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:** [March 10, 2021, 3:59pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/8 "2021-03-10T15:59:46Z")

</div>

You can resample the centerline either in centerlineFilter (by enabling CenterlineResampling and setting the ResamplingStepLength you need) or later using the curve node. Currently, no resampling is performed, so the distances in the output curve depend on the resolution of the input mesh only.

---

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 10, 2021, 4:56pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/9 "2021-03-10T16:56:51Z")

</div>

Thanks, that worked.

---

<div class="post-metadata">

**Author:** ![Chuan](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Chuan](https://discourse.slicer.org/u/Chuan)\
**Post date:** [September 20, 2023, 8:06pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/10 "2023-09-20T20:06:10Z")

</div>

Hi Akshay,

Recently I met a similar question with you. Basically, I have 50 nodes’ coordinates (with a dimension of 50\*3), and I want to resample this curve with 100 equidistance nodes.  
Do you know how to use Python to get there?

Best,  
Chuan

---

<div class="post-metadata">

**Author:** ![Chuan](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Chuan](https://discourse.slicer.org/u/Chuan)\
**Post date:** [September 21, 2023, 7:39am UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/11 "2023-09-21T07:39:58Z")

</div>

Hi Lassoan,

It looks very powerful, just I am sorry I still feel confused.  
Do you have an example of using ResampleCurveWorld in Python to resample my data?  
Because now I seem to have several questions.

1. It looks I need to call c program in Python for this function, but I am not sure.
2. I don’t know what is correct format of my curve node. Here you mentioned, ‘name of my curve node is C’  
I consider as C is an adarray with a dimension of 50\*3 (50 points with 3 dimension), is that correct?
3. And in python, what will be back after calling this function?

Best regards,  
Siyuan

---

<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 21, 2023, 2:46pm UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/12 "2023-09-21T14:46:13Z")

</div>

> [@Chuan](#):
>
> Do you have an example of using ResampleCurveWorld in Python to resample my data?

See above, for example: `getNode('C').ResampleCurveWorld(10)`

> [@Chuan](#):
>
> It looks I need to call c program in Python for this function, but I am not sure.

Many of core algorithms and data classes are implemented in C++, but all these classes are directly available in Python. You can call these functions the same way as any other Python function.

> [@Chuan](#):
>
> I don’t know what is correct format of my curve node. Here you mentioned, ‘name of my curve node is C’

In the example, I got the curve node (the object represents a curve) from the scene by its name (`C`). However, if you implement a module then the user will selec the curve node in the module’s GUI; or if you implement a processing script then probably you create the curve, so you have the object in a variable already (you don’t need to retrieve the node from the scene).

> [@Chuan](#):
>
> I consider as C is an adarray with a dimension of 50\*3 (50 points with 3 dimension), is that correct?

A [curve node](https://apidocs.slicer.org/master/classvtkMRMLMarkupsCurveNode.html) stores control point positions and other properties of the curve.

The easiest way to access the control point positions is via `slicer.util` functions:

- get point positions: [slicer.util.arrayFromMarkupsControlPoints()](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.arrayFromMarkupsControlPoints)
- set point positions: [slicer.util.updateMarkupsControlPointsFromArray()](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html#slicer.util.updateMarkupsControlPointsFromArray)

See many Python examples for manipulating curves and other markups in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#markups).

---

<div class="post-metadata">

**Author:** ![Chuan](https://avatars.discourse-cdn.com/v4/letter/c/4da419/32.png) [@Chuan](https://discourse.slicer.org/u/Chuan)\
**Post date:** [October 6, 2023, 8:45am UTC](https://discourse.slicer.org/t/how-to-resample-curve-nodes-programmatically/16395/13 "2023-10-06T08:45:40Z")

</div>

Thanks! Very clear to me!
