VTK在python環境下的安裝和調用


 

vtk,很好玩的一個可視化工具,有python接口。

一、安裝:

  1、平台:window7, python2.7

  2、下載vtk: vtkpython-7.1.1-Windows-64bit.exe (http://www.vtk.org/files/release/7.1/vtkpython-7.1.1-Windows-64bit.exe 或 http://download.csdn.net/detail/orangefish8_zz/9805120)

  3、vtk.exe按步驟安裝

  4、配置環境變量:

    PATH中增加vtk\bin的路徑: ...\VTK 7.1.1\bin;

    新建環境變量PYTHONPATH: ...\VTK 7.1.1\bin; ...\VTK 7.1.1\bin\Lib\site-packages;

  5、測試:python shell中輸入import vtk, 無報錯則安裝成功。

 

二、調用實例:

  此處實現了官網中八邊圓柱形的示例(http://www.vtk.org/Wiki/VTK/Examples/Python/Cylinder)

 1 #!/usr/bin/python 
 2 #-*-coding:utf-8-*- 
 3 import vtk 
 4 from vtk.util.colors import tomato 
 5 
 6 cylinder = vtk.vtkCylinderSource() #創建圓柱 
 7 cylinder.SetResolution(8) #設置為八邊 
 8 
 9 #映射。 
10 #mapper的作用:把幾何圖形映射到圖形庫中,也可以做顏色映射。 
11 cylinderMapper = vtk.vtkPolyDataMapper() cylinderMapper.SetInputConnection(cylinder.GetOutputPort()) 
12 
13 #設置圖形參數,如顏色、角度等。 
14 #actor是一個分組機制,其中包含幾何映射,變換矩陣和紋理貼圖。 
15 cylinderActor = vtk.vtkActor() 
16 cylinderActor.SetMapper(cylinderMapper) cylinderActor.GetProperty().SetColor(tomato) #設置顏色 cylinderActor.RotateX(30.0) 
17 cylinderActor.RotateY(-45.0) #設置使它旋轉為22.5度 
18 
19 #創建圖形結構 
20 ren = vtk.vtkRenderer() #渲染器 
21 renWin = vtk.vtkRenderWindow() #渲染窗口 
22 renWin.AddRenderer(ren) 
23 iren = vtk.vtkRenderWindowInteractor() #關聯事件(捕獲鼠標等) iren.SetRenderWindow(renWin) 
24 
25 #將actor加入到render中,並設置背景顏色和背景框大小 ren.AddActor(cylinderActor) ren.SetBackground(0.1, 0.2, 0.4) renWin.SetSize(200, 200) 
26 
27 iren.Initialize() #初始化,必須有! 
28 
29 ren.ResetCamera() 
30 ren.GetActiveCamera().Zoom(1.5) #調用變焦的方法(zoom)放大圖形為初始的1.5倍 
31 renWin.Render() 
32 
33 iren.Start()
View Code

  運行程序,會出現如下圖所示的——藍色背景下的西紅柿顏色(-_-|||)的八邊形圓柱體,隨着鼠標的拖動會做出相應的變幻。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM