python 使用 vtkPolyData 繪線

2021-10-09 21:00:24 字數 3622 閱讀 8590

vtk 可以通過vtkpolydata 繪製曲線以及三角麵片,下面的**簡單的繪製乙個線。

使用到如下 vtk的類:

**如下:

import vtk

import numpy as np

class

ployline

(vtk.vtkobject)

:""" 繪製中心線 """

def__init__

(self)

:# 構造2d的線

self.poly_data = vtk.vtkpolydata(

) coordinate = vtk.vtkcoordinate(

) coordinate.setcoordinatesystemtonormalizedviewport())

))) self.actor = vtk.vtkactor2d(

) self.actor.getproperty(

).setlinewidth(10)

self.actor.getproperty(

).setcolor(

0.1,

0.1,

0.1)

defset_line_color

(self, color)

:""" 設定線段顏色 """

self.actor.getproperty(

).setcolor(color[0]

, color[1]

, color[2]

)def

set_line_width

(self, width)

:""" 設定線段寬度 """

self.actor.getproperty(

).setlinewidth(width)

defset_visibility

(self, visible)

:""" 設定可見度 """

if visible:

self.actor.visibilityon(

)else

: self.actor.visibilityoff(

)def

get_actor

(self)

:""" 獲得actor """

return self.actor

defupdate

(self)

:""" 更新線段位置 """

self.poly_data.modified(

)def

set_ploy_data

(self, points)

:""" 設定中心線點的值 """

assert points.shape[0]

>

0, r'輸入的points 必須非空'

assert points.shape[1]

>=

2, r'輸入的points 必須為三維的。如果是二維的,則第三維補充為0'

num_points =

len(points)

print

(r'num_points {}'

.format

(num_points)

) pts = vtk.vtkpoints(

) lines = vtk.vtkcellarray(

)# initialize points

for idx in

range

(len

(points)):

if points.shape[1]

==2: pts.insertnextpoint(points[idx][0

], points[idx][1

],0.0)

print

(r'points {}'

.format

(points[idx]))

else

: pts.insertnextpoint(points[idx][0

], points[idx][1

], points[idx][2

])# initialize lines

for idx in

range

(len

(points)-1

):lines.insertnextcell(2)

lines.insertcellpoint(idx)

lines.insertcellpoint(idx +1)

self.poly_data.setpoints(pts)

self.poly_data.setlines(lines)

self.poly_data.modified(

)if __name__ ==

'__main__'

: ren = vtk.vtkrenderer(

) ren.setbackground(

0.1,

0.1,

0.1)

ren_win = vtk.vtkrenderwindow(

) ren_win.setwindowname(r'center_line'

) ren_win.setsize(

512,

512)

ren_win.addrenderer(ren)

ren_win_interactor = vtk.vtkrenderwindowinteractor(

) ren_win_interactor.setrenderwindow(ren_win)

interactor_style = vtk.vtkinteractorstyleimage(

) ren_win_interactor.setinteractorstyle(interactor_style)

ren_win_interactor.initialize(

) points =[[

0.5,

0.1],[

0.5,

0.2],[

0.5,

0.3],[

0.5,

0.4],[

0.5,

0.5],[

0.5,

0.6],[

0.5,

0.7],[

0.5,

0.8]

] vtk_ploy_line = ployline(

) ren.addactor(vtk_ploy_line.get_actor())

vtk_ploy_line.set_line_color(

[0.0

,1.0

,0.0])

vtk_ploy_line.set_ploy_data(np.array(points)

) vtk_ploy_line.update(

) ren.getactivecamera(

).zoom(

1.5)

ren_win.render(

) ren_win_interactor.start(

)

python元類的使用 python使用元類

原文 type 動態語言和靜態語言最大的不同,就是函式和類的定義,不是編譯時定義的,而是執行時動態建立的。比方說我們要定義乙個hello的class,就寫乙個hello.py模組 當python直譯器載入hello模組時,就會依次執行該模組的所有語句,執行結果就是動態建立出乙個hello的class...

Python精通 Python函式使用

在程式設計意義上的函式其實是指完成某種操作的 塊,當然這個是個人的理解,但是這個概念在所有的程式語言中都是通用的。這個 塊用來完成某寫特定的操作。但是在數學上的函式卻是表示某種對應關係,這兩者之間還是有一定的區別的。但是在某種角度上講我們所程式設計的這種 塊其實就是表示的是引數與返回值之間的關係。從...

python 元組使用 使用元組

usr bin python filename using tuple.py zoo wolf elephant penguin print number of animals in the zoo is len zoo new zoo monkey dolphin zoo print number...