vtk中,还记得点是怎么画的么?
points=vtk.vtkPoints()
用这个接口,将其实例化即可。
那么,这几个demo就见识一下画其他的东西
记住:要和第一篇的画点相互联系着看。
上代码:
# -*- coding: utf-8 -*-
# !/usr/bin/env python
import vtk
cube=vtk.vtkCubeSource()
cube.Update()
mapper=vtk.vtkPolyDataMapper()
# mapper 如果使用 SetInputData 来加载演员,必须要加上开头的 cube.Update(), 不然不会出现 cube
mapper.SetInputData(cube.GetOutput())
# mapper.SetInputConnection(cube.GetOutputPort())
actor=vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(10)
actor.GetProperty().SetColor(0.5,1.0,1.0)
ren=vtk.vtkRenderer()
ren.SetBackground(0.8,0.3,0.8)
ren.SetBackground(0.3,0.5,0.9)
ren.SetGradientBackground(1)
ren.AddActor(actor)
ren_window=vtk.vtkRenderWindow()
ren_window.SetWindowName('axiong first cube')
ren_window.SetSize(300,300)
ren_window.AddRenderer(ren)
interactor=vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(ren_window)
style=vtk.vtkInteractorStyleMultiTouchCamera()
interactor.SetInteractorStyle(style)
interactor.Initialize()
ren_window.Render()
interactor.Start()
其实和画点的区别就在于:
points_list=[[1,0,0],[0,0,1],[0,0,0]]
points=vtk.vtkPoints()
vertices=vtk.vtkCellArray()
for i in points_list:
point_id=points.InsertNextPoint(i)
vertices.InsertNextCell(1)
vertices.InsertCellPoint(point_id)
polydata=vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetVerts(vertices)
mapper=vtk.vtkPolyDataMapper()
mapper.SetInputData(polydata)
import vtk
cube=vtk.vtkCubeSource()
cube.Update()
mapper=vtk.vtkPolyDataMapper()
mapper.SetInputData(cube.GetOutput())
这个小demo是较简单的实现了一个正方体,你可以自己实现一下在这个ren_window里再生成一个立方体(多个演员actor),如法炮制,类似于点
另一个demo是生成一个柱体:
# -*- coding: utf-8 -*-
# !/usr/bin/env python
import vtk
cylinder=vtk.vtkCylinderSource()
cylinder.SetHeight(3.0)
cylinder.SetRadius(1.0)
# 横截面边数
cylinder.SetResolution(360)
mapper=vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cylinder.GetOutputPort())
actor=vtk.vtkActor()
actor.SetMapper(mapper)
ren=vtk.vtkRenderer()
ren.SetBackground(0.8,0.3,0.8)
ren.SetBackground(0.3,0.5,0.9)
ren.SetGradientBackground(1)
ren.AddActor(actor)
ren_window=vtk.vtkRenderWindow()
ren_window.SetWindowName('axiong first cube')
ren_window.SetSize(300,300)
ren_window.AddRenderer(ren)
interactor=vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(ren_window)
style=vtk.vtkInteractorStyleMultiTouchCamera()
interactor.SetInteractorStyle(style)
interactor.Initialize()
ren_window.Render()
interactor.Start()
代码不同处只是在于:
cylinder=vtk.vtkCylinderSource()
cylinder.SetHeight(3.0)
cylinder.SetRadius(1.0)
# 横截面边数
cylinder.SetResolution(360)
mapper=vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cylinder.GetOutputPort())
其他的操作,都和画点的操作一致。