[ create a new paste ] login | about

Link: http://codepad.org/VPOa13St    [ raw code | output | fork ]

Python, pasted on Jul 15:
#!/usr/bin/python 

import vtk
import math

def make_rwi():
    """
    makes renderer, window, interactor
    """
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(0.1, 0.2, 0.4)
    
    window = vtk.vtkRenderWindow()
    window.SetSize(600, 600)
    
    window.AddRenderer(renderer)
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow( window )    

    return (renderer, window, interactor)    
    
def gen_triangles(cd):
    clsdPoly = vtk.vtkPolyData()
    clsdPoly.SetPoints(cd.GetPoints())
    clsdPoly.SetPolys(cd.GetLines())

    triangles = vtk.vtkTriangleFilter()
    triangles.SetInput(clsdPoly)
    
    return triangles.GetOutput()

def make_contour_data():
    points = vtk.vtkPoints()
    lines  = vtk.vtkCellArray()
    
    n = 20
    
    for i in range(0, n):
        angle = 2.0 * math.pi * float(i) / float(n)
        points.InsertPoint(i, 0.1 * math.cos(angle), 0.1 * math.sin(angle), 0.0 )
        lines.InsertNextCell(i)

    lines.InsertNextCell(0)

    pd = vtk.vtkPolyData()
 
    pd.SetPoints(points)
    pd.SetLines(lines)
    
    return pd
    
def render_things(renderer, window, interactor):
    renderer.ResetCamera()
    window.Render()

    interactor.Initialize()
    interactor.Start()    
    
def write_poly(fname, pd):
    writer = vtk.vtkXMLPolyDataWriter()
    writer.SetInput(pd)
    writer.SetFileName(fname)
    writer.SetDataModeToAscii()
    writer.SetCompressorTypeToNone()
    writer.Write()

def All():
    renderer, window, interactor = make_rwi()

    cd = make_contour_data()
    #write_poly("before.vtp", cd)

    contourRep = vtk.vtkOrientedGlyphContourRepresentation()
    contourRep.GetLinesProperty().SetColor(1, 0, 0) # set color to red
    contourRep.GetProperty().SetColor(0,1,0)
    contourRep.GetActiveProperty().SetColor(0,0,0)
 
    contourWidget = vtk.vtkContourWidget()
    contourWidget.SetInteractor(interactor)
    contourWidget.SetRepresentation(contourRep)
    contourWidget.On()

    contourWidget.Initialize(cd, 1)
    contourWidget.CloseLoop()
    cd = contourWidget.GetRepresentation().GetContourRepresentationAsPolyData()
    write_poly("after.vtp", cd)
    
    contourWidget.Render()

    td = gen_triangles(cd)
    
    mapper = vtk.vtkDataSetMapper()
    mapper.SetInput(td)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(0,1,0)
    actor.GetProperty().SetOpacity(0.2)  #actor.GetProperty().SetRepresentationToWireframe()

    renderer.AddActor(actor)

    render_things(renderer, window, interactor)
    
if __name__ == "__main__":
    All()


Output:
1
2
3
4
Traceback (most recent call last):
  Line 3, in <module>
    import vtk
ImportError: No module named vtk


Create a new paste based on this one


Comments: