我們通常可以在空間中畫出幾個(gè)平面來(lái)進(jìn)行切割租幕,通常我們只需要設(shè)置平面的位置和法向量的方向就能夠進(jìn)行三維模型的切割。
import vtk
import numpy as np
from time import time, sleep
arender = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(arender)
renWin.SetSize(500, 500)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
style = myInteractorStyle() # 自定義交互方式拧簸,也可以使用系統(tǒng)自帶的劲绪,例如:vtk.vtkInteractorStyleTrackballCamera
style.renwin = renWin
iren.SetInteractorStyle(style)
# 讀取DICOM數(shù)據(jù)
Reader = vtk.vtkDICOMImageReader()
Reader.SetDirectoryName("./publick")
Reader.Update()
contourfilter = vtk.vtkContourFilter()# 這是一個(gè)等值面濾波器
contourfilter.SetInputConnection(Reader.GetOutputPort())
contourfilter.SetValue(0, 1800)
conMapper = vtk.vtkPolyDataMapper()# 這是多邊形數(shù)據(jù)映射
conMapper.SetInputConnection(contourfilter.GetOutputPort())
conMapper.ScalarVisibilityOff() # 模型的顏色會(huì)變成灰色
plane1 = vtk.vtkPlane()
plane1.SetNormal(0.06, 0.75,-3)
plane1.SetOrigin(0,0,-20)
plane2 = vtk.vtkPlane()
plane2.SetNormal(0, -0.18, 1)
plane2.SetOrigin(0, 0, -32)
conMapper.AddClippingPlane(plane1)
conMapper.AddClippingPlane(plane2)
axes = vtk.vtkAxesActor()
axes.SetTotalLength(200.0, 200.0, 200.0) # 設(shè)置坐標(biāo)軸的長(zhǎng)度
axes.SetShaftTypeToCylinder()
axes.SetCylinderRadius(0.01)
axes.GetXAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone() # 禁用坐標(biāo)軸標(biāo)簽的自動(dòng)縮放
axes.GetYAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone()
axes.GetZAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone()
# 將坐標(biāo)軸添加到渲染器
arender.AddActor(axes)
boxFilter = vtk.vtkOutlineFilter()
boxFilter.SetInputConnection(Reader.GetOutputPort())
boxMapper = vtk.vtkPolyDataMapper()
boxMapper.SetInputConnection(boxFilter.GetOutputPort())
boxActor = vtk.vtkActor()
boxActor.SetMapper(boxMapper)
boxActor.GetProperty().SetColor(255, 255, 255)
arender.AddActor(boxActor)
conActor = vtk.vtkActor()# 用于可視化實(shí)體
conActor.SetMapper(conMapper)
conActor.SetPosition(-75.375, -75.375, -51.375)
style.conActor = conActor
camera = vtk.vtkCamera()
# 設(shè)置相機(jī)的位置
camera.SetPosition(0, 0, 280)
# camera.SetPosition(0, 0, 1000)
# 設(shè)置相機(jī)的焦點(diǎn)(視線方向)
camera.SetFocalPoint(0, 0, 0)
# 設(shè)置相機(jī)的上方向
camera.SetViewUp(0, 1, 0) # 默認(rèn)為Y軸是相機(jī)的上方向
arender.AddActor(conActor)
# arender.SetBackground(1,0,0)
arender.ResetCamera()
arender.SetActiveCamera(camera)
style.camera = camera
iren.Initialize()
iren.Start()