Create curve from many input points

Hi, Andras
I have a numpy array that contains some points. I want to create a vtkMRMLMarkupsCurveNode. So, I call the function curveNode.AddControlPoint() to add every point coordinate. But that’s too slow. What’s the better way to do this?
Thank you.

for pointIndex in range(len(centerlinePoints)):
    centerlineCurveNode.AddControlPoint(vtk.vtkVector3d(centerlinePoints[pointIndex][0], centerlinePoints[pointIndex][1], centerlinePoints[pointIndex][2]))

How many control points are you trying to add?

You may speed up node updates if you call wasModify=centerlineCurveNode.StartModify() before starting to add points and call centerlineCurveNode.EndModify(wasModify) when you are done.

If you have hundreds or thousands of points then you can update them all at once using
SetControlPointPositionsWorld (if you have your inputs as a numpy array then you need to convert the array to vtkPoints object first).

If you have thousands of points then probably you also want to reduce number of interpolated points using SetNumberOfPointsPerInterpolatingSegment().

1 Like

Hundreds of control points.
I adopt your sencond solution: SetControlPointPositionsWorld, That’s run fast. Thank you Andras.
Some code:

points = vtk.vtkPoints()
vtkpointsData = vtk.util.numpy_support.numpy_to_vtk(centerlinePoints, deep=1)
points.SetNumberOfPoints(len(centerlinePoints))
points.SetData(vtkpointsData)
centerlineCurveNode.SetControlPointPositionsWorld(points)
1 Like