# How to draw a line by clicking some scattered points

**URL:** https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225
**Category:** Development
**Created:** [March 2, 2018, 5:51am UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225 "2018-03-02T05:51:39Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![spring](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@spring](https://discourse.slicer.org/u/spring)
#### Post date: [March 2, 2018, 5:51am UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/1 "2018-03-02T05:51:39Z")

</div>

Hi All,

I want to draw a line by connecting some scattered points which I have clicked in the view.

I can get each points RAS position, but it seems can not display the final line in Slicer, below is my code for drawing line, could someone share some guidance?

```auto
#coding----
def drawPolyLine(points):    
 #points is the list which contains each points pos,likes [(0.0,1.0),(1.0,2.0)],just one slice in the view.
    polyLine = vtk.vtkPolyLineSource()
    for i, (x, y) in enumerate(points):
        polyLine.SetPoint(i, x, y, 0)
    polyLine.SetNumberOfPoints(len(points))
    polyLine.SetClosed(True)
    polyLine.Update()
    polyData = polyLine.GetOutput()
    return showPolyData(polyData)

def showPolyData(polyData):
    model = slicer.vtkMRMLModelNode()
    if isinstance(polyData, vtk.vtkAlgorithmOutput):
        model.SetPolyDataConnection(polyData)
    elif isinstance(polyData, vtk.vtkPolyData):
        # print("type and polyData={},{}".format(type(polyData),polyData))
        model.SetAndObservePolyData(polyData)
    else:
        raise Exception('input error!')
    model.SetScene(slicer.mrmlScene)
    modelDisplay = slicer.vtkMRMLModelDisplayNode()
    modelDisplay.SetOpacity(1.0)
    modelDisplay.SetColor([1, 1, 0])
    modelDisplay.SetSliceIntersectionVisibility(True) # Show in slice view
    modelDisplay.SetVisibility(True) # Hide in 3D view
    modelDisplay.SetScene(slicer.mrmlScene)
    slicer.mrmlScene.AddNode(modelDisplay)
    slicer.mrmlScene.AddNode(model)
    transform = slicer.vtkMRMLLinearTransformNode()
    slicer.mrmlScene.AddNode(transform)
    model.SetAndObserveDisplayNodeID(modelDisplay.GetID())
    model.SetAndObserveTransformNodeID(transform.GetID())
    return model

```

Thanks in advance!  
Chunbo

---

<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 2, 2018, 12:48pm UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/2 "2018-03-02T12:48:49Z")

</div>

You can use MarkupsToModel extension to create curves from markup fiducial or model points. It supports linear and various smooth interpolating and approximating methods.

---

<div class="post-metadata">

### Author: ![spring](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@spring](https://discourse.slicer.org/u/spring)
#### Post date: [March 5, 2018, 7:19am UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/3 "2018-03-05T07:19:16Z")

</div>

@lassoan Thanks for your answer.

I have tried MarkupsToModel,it really can draw the straight line by clicking scattered pointes.  
Just have one issue:if I want to delete the total polydata,I should switch to Models then select corresponding model to delete it,correct?

I have implemented a simple module,which can draw some scattered points,then connect neighboring points with a straight line, used a polydata to show these lines at last,likes below screenshot:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/c/dc9100881f80d25f618732b540240c292154047a.jpeg)

Since I want to setAndobserve this polydata in Slicer,the issue: this Polydata can not be displayed,since the area is 0,likes below screenshot:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/9/89618657c020ce9d33b6b3c7c14988842069539c.png)

Not sure if some parameters are incorrect? Or if there is some examples which can create a polydata and can be set and observed in Slicer?

Best，  
Chunbo

---

<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, 2018, 1:40pm UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/4 "2018-03-05T13:40:17Z")

</div>

If you set the output to be a closed surface instead of a line then you’ll get the surface area. Both sides of the surface will be included in the computation, so you probably want to divide the reported surface area by 2.

---

<div class="post-metadata">

### Author: ![spring](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@spring](https://discourse.slicer.org/u/spring)
#### Post date: [March 5, 2018, 2:54pm UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/5 "2018-03-05T14:54:14Z")

</div>

Thanks,Lasson.  
I will try the method.  
Btw,I have solved my issue from this post:[Display model in slice viewer](https://discourse.slicer.org/t/display-model-in-slice-viewer/208/2).

Best,  
Chunbo

---

<div class="post-metadata">

### Author: ![spring](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@spring](https://discourse.slicer.org/u/spring)
#### Post date: [March 6, 2018, 8:34am UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/6 "2018-03-06T08:34:55Z")

</div>

Hi lassoan,  
I used the following codes :  
inputMarkups = getNode(‘F’)

outputModel = slicer.mrmlScene.AddNode(slicer.vtkMRMLModelNode())  
outputModel.CreateDefaultDisplayNodes()  
outputModel.GetDisplayNode().SetSliceIntersectionVisibility(True)  
outputModel.GetDisplayNode().SetColor(1,0,0)

markupsToModel = slicer.mrmlScene.AddNode(slicer.vtkMRMLMarkupsToModelNode())  
markupsToModel.SetAutoUpdateOutput(True)  
markupsToModel.SetAndObserveModelNodeID(outputModel.GetID())  
markupsToModel.SetAndObserveMarkupsNodeID(inputMarkups.GetID())

* * *

It can create curves with clicking points,one issue is:the curve is convex hull,can we change it to false?  
since we found some points can not be seen,pls see below screenshots:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/e/6eb3b366235afa9ad7ed8c352cc01e1b442febbb.png)  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/9/09ddfdafeccf8b6114b1ae58a5fb4cb20504dc42.png)

Thanks,  
Chunbo

---

<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, 2018, 2:06pm UTC](https://discourse.slicer.org/t/how-to-draw-a-line-by-clicking-some-scattered-points/2225/7 "2018-03-10T14:06:48Z")

</div>

Do you need a line/curve connecting points? If yes, then set ModelType to Curve and probably enable TubeLoop as well.

If you want to reconstruct a surface from a sparse set of points then Delaunay triangulation is used. You can tune DelaunayAlpha parameter to control how much concavity is allowed in the reconstructed surface (if alpha is set to 0 then the result will be a convex hull).
