import vtk
renderer = vtk.vtkRenderer()
renderer.SetBackground(1.0, 1.0, 1.0)
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.SetDesiredUpdateRate(3)
reader = vtk.vtkStructuredPointsReader()
reader.SetFileName(r'E:/ironProt.vtk')
thresh = vtk.vtkThreshold()
thresh.ThresholdByUpper(80)
thresh.AllScalarsOff()
thresh.SetInputConnection(reader.GetOutputPort())
trifilter = vtk.vtkDataSetTriangleFilter()
trifilter.SetInputConnection(thresh.GetOutputPort())
opacityTransferFunction = vtk.vtkPiecewiseFunction()
opacityTransferFunction.AddPoint(80.0, 0.0)
opacityTransferFunction.AddPoint(120.0, 0.2)
opacityTransferFunction.AddPoint(255.0, 0.2)
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.AddRGBPoint(80.0, 0.0, 0.0, 1.0)
colorTransferFunction.AddRGBPoint(120.0, 0.0, 0.0, 1.0)
colorTransferFunction.AddRGBPoint(160.0, 1.0, 0.0, 0.0)
colorTransferFunction.AddRGBPoint(200.0, 0.0, 1.0, 0.0)
colorTransferFunction.AddRGBPoint(255.0, 0.0, 1.0, 1.0)
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.ShadeOff()
volumeProperty.SetInterpolationTypeToLinear()
volume = vtk.vtkVolume()
volume.SetProperty(volumeProperty)
RenderType = 3
if RenderType == 1:
volumeMapper = vtk.vtkUnstructuredGridVolumeRayCastMapper()
'''
vtkUnstructuredGridVolumeRayCastMapper实现了基于软件实现的不规则网格光线投射算法
'''
volumeMapper.SetInputConnection(trifilter.GetOutputPort())
volume.SetMapper(volumeMapper)
renderer.AddVolume(volume)
elif RenderType == 2:
volumeMapper = vtk.vtkUnstructuredGridVolumeZSweepMapper()
'''
可以在任何平台下运行的体绘制方法。速度是最慢的,但是相对于光线投射方法,该方法对内存的需求较小,可以用来渲染大数据
'''
volumeMapper.SetInputConnection(trifilter.GetOutputPort())
volume.SetMapper(volumeMapper)
renderer.AddVolume(volume)
elif RenderType == 3:
volumeMapper = vtk.vtkProjectedTetrahedraMapper()
volumeMapper.SetInputConnection(trifilter.GetOutputPort())
volume.SetMapper(volumeMapper)
renderer.AddVolume(volume)
elif RenderType == 4:
volumeMapper = vtk.vtkHAVSVolumeMapper()
volumeMapper.SetInputConnection(trifilter.GetOutputPort())
volumeMapper.SetLevelOfDetail(False)
volumeMapper.SetGPUDataStructures(False)
volumeMapper.SetKBufferSizeTo2()
volume.SetMapper(volumeMapper)
renderWindow.Render()
supported = volumeMapper.SupportedByHardware(renderer)
if supported:
renderer.AddVolume(volume)
volumeMapper.SetLevelOfDetail(False)
volumeMapper.SetKBufferSizeTo2()
renderWindow.SetSize(640, 480)
renderWindow.Render()
renderWindow.SetWindowName('UnstructuredGridVolumeRendering')
renderWindow.Render()
renderWindowInteractor.Start()
按顺序依次是光线投射法、ZSweep法、投影四面体法