您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么使用Python VTK高亮顯示actor的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
前言:
VTK,(visualizationtoolkit)是一個開放資源的免費軟件系統(tǒng),主要用于三維計算機圖形學(xué)、圖像處理和可視化。Vtk是在面向?qū)ο笤淼幕A(chǔ)上設(shè)計和實現(xiàn)的,它的內(nèi)核是用C++構(gòu)建的,包含有大約250,000行代碼,2000多個類,還包含有幾個轉(zhuǎn)換界面,因此也可以自由的通過Java,Tcl/Tk和Python各種語言使用vtk。
主要函數(shù)介紹:
NewPickedActor.GetProperty(): 通過該函數(shù),可以設(shè)置actor的性質(zhì),如顏色、表面樣式等。
vtk.vtkSphereSource(): 創(chuàng)建球體的函數(shù),文中通過一個for循環(huán)創(chuàng)建了10個球體。
vtk.vtkMinimalStandardRandomSequence(): VTK的隨機數(shù)生成器,用于代碼中的十個球體,隨機生成球體的大小和位置。
MouseInteractorHighLightActor: 定義actor操作方法,這個是一個鼠標(biāo)操作控件的控制方法。
leftButtonPressEvent(self, obj, event): 這是一個事件觸發(fā)函數(shù),當(dāng)鼠標(biāo)左鍵點擊對應(yīng)的actor時,會觸發(fā)函數(shù),對點的actor高亮顯示。
主要代碼如下:
#!/usr/bin/env python # noinspection PyUnresolvedReferences import vtk colors = vtk.vtkNamedColors() NUMBER_OF_SPHERES = 10 class MouseInteractorHighLightActor(vtk.vtkInteractorStyleTrackballCamera): def __init__(self, parent=None): self.AddObserver("LeftButtonPressEvent", self.leftButtonPressEvent) self.LastPickedActor = None self.LastPickedProperty = vtk.vtkProperty() def leftButtonPressEvent(self, obj, event): clickPos = self.GetInteractor().GetEventPosition() picker = vtk.vtkPropPicker() picker.Pick(clickPos[0], clickPos[1], 0, self.GetDefaultRenderer()) # 創(chuàng)建一個新的actor self.NewPickedActor = picker.GetActor() # If something was selected if self.NewPickedActor: # If we picked something before, reset its property if self.LastPickedActor: self.LastPickedActor.GetProperty().DeepCopy(self.LastPickedProperty) # Save the property of the picked actor so that we can # restore it next time self.LastPickedProperty.DeepCopy(self.NewPickedActor.GetProperty()) # 高亮選中球體,并顯示邊緣 self.NewPickedActor.GetProperty().SetColor(colors.GetColor3d('Red')) self.NewPickedActor.GetProperty().SetDiffuse(1.0) self.NewPickedActor.GetProperty().SetSpecular(0.0) self.NewPickedActor.GetProperty().EdgeVisibilityOn() # 保存最后一個選中的actor self.LastPickedActor = self.NewPickedActor self.OnLeftButtonDown() return def main(): # 創(chuàng)建render和window renderer = vtk.vtkRenderer() renderer.SetBackground(colors.GetColor3d('SteelBlue')) renwin = vtk.vtkRenderWindow() renwin.AddRenderer(renderer) renwin.SetSize(640, 480) renwin.SetWindowName('HighlightPickedActor') # 建立interactor(交互操作) interactor = vtk.vtkRenderWindowInteractor() interactor.SetRenderWindow(renwin) # 交互操作方法 style = MouseInteractorHighLightActor() style.SetDefaultRenderer(renderer) interactor.SetInteractorStyle(style) randomSequence = vtk.vtkMinimalStandardRandomSequence() # randomSequence.SetSeed(1043618065) # randomSequence.SetSeed(5170) randomSequence.SetSeed(8775070) # 添加球體 for i in range(NUMBER_OF_SPHERES): source = vtk.vtkSphereSource() # random position and radius x = randomSequence.GetRangeValue(-5.0, 5.0) randomSequence.Next() y = randomSequence.GetRangeValue(-5.0, 5.0) randomSequence.Next() z = randomSequence.GetRangeValue(-5.0, 5.0) randomSequence.Next() radius = randomSequence.GetRangeValue(0.5, 1.0) randomSequence.Next() source.SetRadius(radius) source.SetCenter(x, y, z) source.SetPhiResolution(11) source.SetThetaResolution(21) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(source.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) r = randomSequence.GetRangeValue(0.4, 1.0) randomSequence.Next() g = randomSequence.GetRangeValue(0.4, 1.0) randomSequence.Next() b = randomSequence.GetRangeValue(0.4, 1.0) randomSequence.Next() actor.GetProperty().SetDiffuseColor(r, g, b) actor.GetProperty().SetDiffuse(.8) actor.GetProperty().SetSpecular(.5) actor.GetProperty().SetSpecularColor(colors.GetColor3d('White')) actor.GetProperty().SetSpecularPower(30.0) renderer.AddActor(actor) # 運行 interactor.Initialize() renwin.Render() interactor.Start() if __name__ == '__main__': main()
顯示結(jié)果如下: 未選擇球體:
已選取球體后:
以上就是“怎么使用Python VTK高亮顯示actor”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。