How to resample curve nodes programmatically?

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?

Resampling is implemented in the curve node. 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.

1 Like

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

For example, if name of your curve node is ā€œCā€ then you can resample at 10mm sampling distance like this:

getNode('C').ResampleCurveWorld(10)
1 Like

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)

Extract centerline module already does this. You can use this module as is or extract the code that you need from it.

@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

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.

1 Like

Thanks, that worked.

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

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

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

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.

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).

A curve node stores control point positions and other properties of the curve.

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

See many Python examples for manipulating curves and other markups in the script repository.

2 Likes

Thanks! Very clear to me!