python VTK(二十八) ----体绘制 不规则网格数据体绘制技术

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
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法、投影四面体法
python VTK(二十八) ----体绘制 不规则网格数据体绘制技术_第1张图片
python VTK(二十八) ----体绘制 不规则网格数据体绘制技术_第2张图片
python VTK(二十八) ----体绘制 不规则网格数据体绘制技术_第3张图片

你可能感兴趣的:(python,开发语言,vtk,计算机视觉,图像处理)