Python vtk學習(1)
阿新 • • 發佈:2019-01-24
serve name 面向 net map .get ans input model
Vtk,(visualization toolkit)是一個開源的免費軟件系統,主要用於三維計算機圖形學、圖像處理和可視化。Vtk是在面向對象原理的基礎上設計和實現的,它的內核是用C++構建的,包含有大約250,000行代碼,2000多個類,還包含有幾個轉換界面,因此也可以自由的通過Java,Tcl/Tk和Python各種語言使用vtk。以下介紹VTK對於STL圖像的基本操作
STL圖像加載、縮放、旋轉
import os import vtk base_file = "E:\\study\\company\\model" test_file = "dongmai.stl" actor = vtk.vtkActor() def show(file_name): # load stl file reader = vtk.vtkSTLReader() reader.SetFileName(file_name) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(reader.GetOutputPort()) actor.SetMapper(mapper) # actor.RotateY(45) # Create a rendering window and renderer ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) # Create a renderwindowinteractor iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Assign actor to the renderer ren.AddActor(actor) # Enable user interface interactor iren.Initialize() renWin.Render() iren.Start() return iren # 繞x軸旋轉 def rotate_x(num): actor.RotateX(num) def rotate_y(num): actor.RotateY(num) def rotate_z(num): actor.RotateZ(num) # 設置方向 def set_origin(x, y, z): actor.SetOrientation(x, y, z) def get_origin(): return actor.GetOrientation() # 縮放 def set_scale(x, y, z): actor.SetScale(x, y, z) def main(): show(os.path.join(base_file, test_file)) if __name__ == ‘__main__‘: main()
顯示結果
鼠標事件監聽
# 監聽事件 class MyEvent(vtk.vtkInteractorStyleTrackballCamera): def __init__(self, parent=None): # 鼠標中鍵 self.AddObserver("MiddleButtonPressEvent", self.middle_button_press_event) self.AddObserver("MiddleButtonReleaseEvent", self.middle_button_release_event) # 鼠標左鍵 self.AddObserver("LeftButtonPressEvent", self.left_button_press_event) self.AddObserver("LeftButtonReleaseEvent", self.left_button_release_event) # 鼠標右鍵 self.AddObserver("RightButtonPressEvent", self.right_button_press_event) self.AddObserver("RightButtonReleaseEvent", self.right_button_release_event) def middle_button_press_event(self, obj, event): print("Middle Button pressed") self.OnMiddleButtonDown() return def middle_button_release_event(self, obj, event): print("Middle Button released") self.OnMiddleButtonUp() return def left_button_press_event(self, obj, event): print("Left Button pressed") self.OnLeftButtonDown() return def left_button_release_event(self, obj, event): print("Left Button released") self.OnLeftButtonUp() return def right_button_press_event(self, obj, event): print("right Button pressed") self.OnRightButtonDown() return def right_button_release_event(self, obj, event): print("right Button released") self.OnLeftButtonUp() return # 引入上一段代碼調用 iren.SetInteractorStyle(MyEvent())
結果
參考文檔
Python VTK Example
VTK筆記——模型的空間變換(Transform).平移、旋轉和縮放
Python vtk學習(1)