20170622使用VTK庫畫一個立方體
阿新 • • 發佈:2019-02-17
最近開始學習用python進行科學計算三維視覺化,這個算是一個起步的小東西,貼出程式碼來mark一下這兩天的學習成果!
重難點在於安裝各種庫,呼呼,配置環境真的是一項大工程Orz。
安裝流程:
1、示例系統環境:
Windows10 64位 + Python3.6,在庫檔案所在目錄裡依次安裝:
VTK-7.1.1-cp36-cp36m-win_amd64.whl
numpy-1.12.1+mkl-cp36-cp36m-win_amd64.whl
traits-4.6.0-cp36-cp36m-win_amd64.whl
mayavi-4.5.0+vtk71-cp36-cp36m-win_amd64.whl
PyQt4-4.11.4-cp36-cp36m-win_amd64.whl
相關庫下載地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/
2、示例系統環境:
MacOS 10.12.4 + Anaconda2(Spyder),在終端依次執行:
conda install vtk
conda install numpy
conda install traits
conda install mayavi
conda install PyQt
程式程式碼如下:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
>>> from tvtk.tools import tvtk_doc
>>> tvtk_doc.main()
>>> from tvtk.api import tvtk
>>> s = tvtk.CubeSource(x_length=1.0, y_length=2.0, z_length=3.0)
>>> print(s)
vtkCubeSource (047A7BF0)
Debug: Off
Modified Time: 1903614
Reference Count: 2
Registered Events:
Registered Observers:
vtkObserver (077A1C80)
Event: 33
EventName: ModifiedEvent
Command: 0602BD38
Priority: 0
Tag: 1
Executive: 0479D990
ErrorCode: No error
Information: 0779AA38
AbortExecute: Off
Progress: 0
Progress Text: (None)
X Length: 1
Y Length: 2
Z Length: 3
Center: (0, 0, 0)
Output Points Precision: 0
>>> m = tvtk.PolyDataMapper(input_connection=s.output_port)
>>> a = tvtk.Actor(mapper=m)
>>> r = tvtk.Renderer(background=(0,0,0))
>>> r.add_actor(a)
>>> w = tvtk.RenderWindow(size=(300,300))
>>> w.add_renderer(r)
>>> i = tvtk.RenderWindowInteractor(render_window=w)
>>> i.initialize()
>>> i.start()