# Create curve from many input points

**URL:** https://discourse.slicer.org/t/create-curve-from-many-input-points/9673
**Category:** Development
**Tags:** markups
**Created:** [December 31, 2019, 10:32am UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673 "2019-12-31T10:32:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![RayCui](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/raycui/32/4517_2.png) [@RayCui](https://discourse.slicer.org/u/RayCui)
#### Post date: [December 31, 2019, 10:32am UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/1 "2019-12-31T10:32:26Z")

</div>

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]))
```

---

<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: [December 31, 2019, 3:07pm UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/2 "2019-12-31T15:07:25Z")

</div>

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](https://apidocs.slicer.org/master/classvtkMRMLMarkupsNode.html#ab0146489b4bc1beef9760f73cce7ad4d) (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()`.

---

<div class="post-metadata">

### Author: ![RayCui](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/raycui/32/4517_2.png) [@RayCui](https://discourse.slicer.org/u/RayCui)
#### Post date: [January 2, 2020, 2:31am UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/3 "2020-01-02T02:31:15Z")

</div>

Hundreds of control points.  
I adopt your sencond solution: [SetControlPointPositionsWorld](https://apidocs.slicer.org/master/classvtkMRMLMarkupsNode.html#ab0146489b4bc1beef9760f73cce7ad4d), 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)
```

---

<div class="post-metadata">

### Author: ![yee\_lu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yee_lu/32/9292_2.png) [@yee\_lu](https://discourse.slicer.org/u/yee_lu)
#### Post date: [December 21, 2020, 5:02pm UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/4 "2020-12-21T17:02:05Z")

</div>

Hello! When I input your code in the python interrupter , I get “name ‘centerlineCurveNode’ is not defined”. Can you tell me how to import centerlineCurveNode? Thank you in advance.

---

<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: [December 21, 2020, 5:45pm UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/5 "2020-12-21T17:45:23Z")

</div>

Replace `centerlineCurveNode` with your curve node. You can get a curve node from its name by calling:

```
centerlineCurveNode = getNode('NameOfMyCurveNode')
```

---

<div class="post-metadata">

### Author: ![yee\_lu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yee_lu/32/9292_2.png) [@yee\_lu](https://discourse.slicer.org/u/yee_lu)
#### Post date: [March 1, 2021, 6:10am UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/6 "2021-03-01T06:10:45Z")

</div>

Hello！ I run the code above , but I didn’t see anything in 3Dslicer， how to show the curve? Thank you in advance!

---

<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 1, 2021, 2:47pm UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/7 "2021-03-01T14:47:00Z")

</div>

Here is a complete example that works fine in recent Slicer versions:

```python
# Create random numpy array to use as input
import numpy as np
pointPositions = np.random.uniform(-50,50,size=[15,3])

# Create curve from numpy array
curveNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsCurveNode")
slicer.util.updateMarkupsControlPointsFromArray(curveNode, pointPositions)

```

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/f/df86fb0cfa10c10371018afa410d866bccea5699.png)

---

<div class="post-metadata">

### Author: ![Greydon\_Gilmore](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/greydon_gilmore/32/6863_2.png) [@Greydon\_Gilmore](https://discourse.slicer.org/u/Greydon_Gilmore)
#### Post date: [March 16, 2021, 5:20am UTC](https://discourse.slicer.org/t/create-curve-from-many-input-points/9673/8 "2021-03-16T05:20:12Z")

</div>

How would you then assign labels to the points? Would you iterate through each Nth point to add the label (via `SetNthControlPointLabel`)?

I have hundreds of points but also need to label each one.

Greydon

```auto

```
