我们通常可以在空间中画出几个平面来进行切割,通常我们只需要设置平面的位置和法向量的方向就能够进行三维模型的切割。
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() # 自定义交互方式,也可以使用系统自带的,例如:vtk.vtkInteractorStyleTrackballCamera
style.renwin = renWin
iren.SetInteractorStyle(style)
# 读取DICOM数据
Reader = vtk.vtkDICOMImageReader()
Reader.SetDirectoryName("./publick")
Reader.Update()
contourfilter = vtk.vtkContourFilter()# 这是一个等值面滤波器
contourfilter.SetInputConnection(Reader.GetOutputPort())
contourfilter.SetValue(0, 1800)
conMapper = vtk.vtkPolyDataMapper()# 这是多边形数据映射
conMapper.SetInputConnection(contourfilter.GetOutputPort())
conMapper.ScalarVisibilityOff() # 模型的颜色会变成灰色
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) # 设置坐标轴的长度
axes.SetShaftTypeToCylinder()
axes.SetCylinderRadius(0.01)
axes.GetXAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone() # 禁用坐标轴标签的自动缩放
axes.GetYAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone()
axes.GetZAxisCaptionActor2D().GetTextActor().SetTextScaleModeToNone()
# 将坐标轴添加到渲染器
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()# 用于可视化实体
conActor.SetMapper(conMapper)
conActor.SetPosition(-75.375, -75.375, -51.375)
style.conActor = conActor
camera = vtk.vtkCamera()
# 设置相机的位置
camera.SetPosition(0, 0, 280)
# camera.SetPosition(0, 0, 1000)
# 设置相机的焦点(视线方向)
camera.SetFocalPoint(0, 0, 0)
# 设置相机的上方向
camera.SetViewUp(0, 1, 0) # 默认为Y轴是相机的上方向
arender.AddActor(conActor)
# arender.SetBackground(1,0,0)
arender.ResetCamera()
arender.SetActiveCamera(camera)
style.camera = camera
iren.Initialize()
iren.Start()