只要给定一个有效的vtkDataSet对象,vtkUnstructuredGrid就可以创建出各类空间对象。点,线,面,体都不在话下。
下面的python例子使用vtk绘制正方体。

#!/usr/bin/env python

"""
This example shows how to create an unstructured grid.
"""

import vtk


def main():
    coords = [[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0], 
              [0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]]
    pts = [0, 1, 2, 3, 4, 5, 6, 7]

    renderer = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer( renderer )
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow( renWin )

    points = vtk.vtkPoints()    
    for i in range( 0, len(coords) ):
        points.InsertPoint( i, coords[i] )

    ugrid = vtk.vtkUnstructuredGrid()
    ugrid.Allocate( 1, 8 ) # There are 1 cell and 8 points 
    ugrid.InsertNextCell( vtk.VTK_HEXAHEDRON, 8, pts )
    ugrid.SetPoints( points )

    ugridMapper = vtk.vtkDataSetMapper()
    ugridMapper.SetInputData( ugrid )

    colors = vtk.vtkNamedColors()
    ugridActor = vtk.vtkActor()
    ugridActor.SetMapper( ugridMapper )
    ugridActor.GetProperty().SetColor( colors.GetColor3d("Peacock") )
    ugridActor.GetProperty().EdgeVisibilityOn()

    renderer.AddActor( ugridActor )
    renderer.SetBackground( colors.GetColor3d("Beige") )

    renderer.ResetCamera()
    renderer.GetActiveCamera().Elevation(60.0)
    renderer.GetActiveCamera().Azimuth(30.0)

    renWin.SetSize(640, 480)

    # Interact with the data.
    renWin.Render()

    iren.Start()

# If we open this file to execute, __name__ is '__main__', or the name is file name of .py
if __name__ == "__main__":
    main()




还一可以增加line:

    coords = [[0, 0, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0],
              [0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0],
              [2, -1, -1], [-1, 2, 2]]
    pts = [[0, 1, 2, 3, 4, 5, 6, 7], [8, 9]]
...
    ugrid.Allocate( 2 )
    ugrid.InsertNextCell( vtk.VTK_HEXAHEDRON, 8, pts[0] )
    ugrid.InsertNextCell( vtk.VTK_TRIANGLE, 2, pts[1] )



分类: python

发表评论

电子邮件地址不会被公开。 必填项已用*标注