如何使用Python?VTK高亮顯示actor
前言:
VTK,(visualizationtoolkit)是一個(gè)開放資源的免費(fèi)軟件系統(tǒng),主要用于三維計(jì)算機(jī)圖形學(xué)、圖像處理和可視化。Vtk是在面向?qū)ο笤淼幕A(chǔ)上設(shè)計(jì)和實(shí)現(xiàn)的,它的內(nèi)核是用C++構(gòu)建的,包含有大約250,000行代碼,2000多個(gè)類,還包含有幾個(gè)轉(zhuǎn)換界面,因此也可以自由的通過(guò)Java,Tcl/Tk和Python各種語(yǔ)言使用vtk。
主要函數(shù)介紹:
NewPickedActor.GetProperty(): 通過(guò)該函數(shù),可以設(shè)置actor的性質(zhì),如顏色、表面樣式等。
vtk.vtkSphereSource(): 創(chuàng)建球體的函數(shù),文中通過(guò)一個(gè)for循環(huán)創(chuàng)建了10個(gè)球體。
vtk.vtkMinimalStandardRandomSequence(): VTK的隨機(jī)數(shù)生成器,用于代碼中的十個(gè)球體,隨機(jī)生成球體的大小和位置。
MouseInteractorHighLightActor: 定義actor操作方法,這個(gè)是一個(gè)鼠標(biāo)操作控件的控制方法。
leftButtonPressEvent(self, obj, event): 這是一個(gè)事件觸發(fā)函數(shù),當(dāng)鼠標(biāo)左鍵點(diǎn)擊對(duì)應(yīng)的actor時(shí),會(huì)觸發(fā)函數(shù),對(duì)點(diǎn)的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)建一個(gè)新的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()
# 保存最后一個(gè)選中的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)
# 運(yùn)行
interactor.Initialize()
renwin.Render()
interactor.Start()
if __name__ == '__main__':
main()
顯示結(jié)果如下: 未選擇球體:

已選取球體后:

到此這篇關(guān)于如何使用Python VTK高亮顯示actor的文章就介紹到這了,更多相關(guān)Python VTK高亮顯示actor內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用pyaudio實(shí)現(xiàn)錄音功能
pyaudio是一個(gè)跨平臺(tái)的音頻I/O庫(kù),使用PyAudio可以在Python程序中播放和錄制音頻,本文將利用它實(shí)現(xiàn)錄音功能,并做到停止說(shuō)話時(shí)自動(dòng)結(jié)束2023-05-05
Python實(shí)戰(zhàn)之大魚吃小魚游戲的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Python制作一個(gè)經(jīng)典游戲之大魚吃小魚,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-04-04
使用Python集合顯著優(yōu)化算法性能的實(shí)戰(zhàn)案例
掌握?Python?中的?set?數(shù)據(jù)結(jié)構(gòu),是算法和數(shù)據(jù)結(jié)構(gòu)的基本功,今天我們從一個(gè)實(shí)戰(zhàn)案例出發(fā),探討如何利用Python集合顯著優(yōu)化算法性能,感興趣的同學(xué)跟著小編一起來(lái)探討吧2023-06-06
Python統(tǒng)計(jì)學(xué)一數(shù)據(jù)的概括性度量詳解
這篇文章主要介紹了Python統(tǒng)計(jì)學(xué)一數(shù)據(jù)的概括性度量詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python Pillow.Image 圖像保存和參數(shù)選擇方式
今天小編就為大家分享一篇Python Pillow.Image 圖像保存和參數(shù)選擇方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
PyTorch中torch.manual_seed()的用法實(shí)例詳解
在Pytorch中可以通過(guò)相關(guān)隨機(jī)數(shù)來(lái)生成張量,并且可以指定生成隨機(jī)數(shù)的分布函數(shù)等,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.manual_seed()用法的相關(guān)資料,需要的朋友可以參考下2022-06-06
Deepsort + Yolo 實(shí)現(xiàn)行人檢測(cè)和軌跡追蹤的方法
這篇文章主要介紹了Deepsort + Yolo 實(shí)現(xiàn)行人檢測(cè)和軌跡追蹤,本項(xiàng)目通過(guò)采用深度學(xué)習(xí)方法實(shí)現(xiàn)YOLO算法行人檢測(cè)和deepsort算法對(duì)人員定位的和軌跡跟蹤,需要的朋友可以參考下2021-09-09
OpenCV機(jī)器學(xué)習(xí)MeanShift算法筆記分享
這篇文章主要介紹了OpenCV機(jī)器學(xué)習(xí)MeanShift算法筆記分享,有需要的朋友可以借鑒參考下,希望可以對(duì)各位讀者的OpenCV算法學(xué)習(xí)能夠有所幫助2021-09-09

