How to draw a line by clicking some scattered points

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?

#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

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

@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

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

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

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.

2 Likes

Thanks,Lasson.
I will try the method.
Btw,I have solved my issue from this post:Display model in slice viewer.

Best,
Chunbo

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
image

Thanks,
Chunbo

1 Like

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