vtk 体绘制

#include "vtkProperty.h"
#include "vtkPolyDataNormals.h"
#include "vtkContourFilter.h"
#include "vtkMarchingCubes.h"
#include "vtkDICOMImageReader.h"
#include "vtkImageCast.h"
#include "vtkDecimatePro.h"
#include "vtkStripper.h"
#include "vtkImageShrink3D.h"
#include "vtkSmoothPolyDataFilter.h" 
#include "vtkTriangleFilter.h"
#include "vtkImageData.h"

#include "vtkPolyDataWriter.h"
#include <vtkBYUWriter.h> 
#include "vtkXMLPolyDataWriter.h"
#include "vtkDataSetWriter.h"
#include <vtkMCubesWriter.h> 
#include "vtkSTLWriter.h"
#include "vtkSmartPointer.h"
#include <vtkBMPReader.h>
#include "vtkJPEGReader.h"
#include "vtkOBJExporter.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderWindow.h"
#include "vtkPolyDataMapper.h"
#include "vtkOutlineFilter.h"
#include"vtkPolyDataReader.h"
#include"vtkCamera.h"
#include <vtkXMLPolyDataReader.h>
#include "vtkSTLReader.h"
#include "vtkShrinkPolyData.h"
#include<vtkVolume16Reader.h>
#include<vtkVolumeRayCastCompositeFunction.h>
#include<vtkPiecewiseFunction.h>
#include<vtkColorTransferFunction.h>
#include<vtkVolumeProperty.h>
#include<vtkVolumeRayCastMapper.h>
#include<vtkImageShiftScale.h>
#include"vtkInteractorStyleTrackballCamera.h"
#include <vtkAutoInit.h>//这个文件必须有,不然要报错
VTK_MODULE_INIT(vtkRenderingOpenGL)//同上

VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);

void test() //体绘制可用
{
    vtkRenderer *aRender = vtkRenderer::New();        //设置绘制类
    vtkRenderWindow*renWin = vtkRenderWindow::New();//设置绘制窗口
    renWin->AddRenderer(aRender); //装载绘制类
    vtkRenderWindowInteractor*iRen = vtkRenderWindowInteractor::New();//设置绘制窗口的交互
    iRen->SetRenderWindow(renWin); //装载绘制窗口
    vtkDICOMImageReader   *reader = vtkDICOMImageReader::New();

    reader->SetDirectoryName("E:\\DICOM\\tt");
    reader->SetDataScalarTypeToUnsignedShort();
    reader->SetDataByteOrderToLittleEndian();
    reader->Update();

    vtkImageShiftScale *shiftScale = vtkImageShiftScale::New();
    shiftScale->SetInputConnection(reader->GetOutputPort());
    shiftScale->SetOutputScalarTypeToUnsignedShort();//将数据转换为体绘制能处理的数据,后面代替reader

    vtkPiecewiseFunction*opacityTransferFunction = vtkPiecewiseFunction::New();//设置不透明度传递函数
    opacityTransferFunction->AddPoint(-1024, 0.0);
    opacityTransferFunction->AddPoint(1269, 0.17);
    opacityTransferFunction->AddPoint(2365, 0.46);
    opacityTransferFunction->AddPoint(3500, 0.0);
    opacityTransferFunction->AddSegment(-1024, 0, 1269, 0.17);
    opacityTransferFunction->AddSegment(1269, 0.17, 2365, 0.46);
    opacityTransferFunction->AddSegment(2365, 0.46, 3500, 0);
    vtkColorTransferFunction *colorTransferFunction = vtkColorTransferFunction::New();//设置颜色传递函数
    colorTransferFunction->AddRGBPoint(0, 0, 0, 0);//此处颜色设置为灰度值
    colorTransferFunction->AddRGBPoint(2150.0, 1.0, 0.0, 0.0);
    colorTransferFunction->AddRGBPoint(2800.0, 0.5, 0.77, 0.6);
    colorTransferFunction->AddRGBPoint(4095.0, 0.5, 0.9, 0.9);
    vtkPiecewiseFunction *gradientTransferFunction = vtkPiecewiseFunction::New();//设置梯度传递函数
    gradientTransferFunction->AddPoint(0, 2.0);
    gradientTransferFunction->AddPoint(500, 2.0);
    gradientTransferFunction->AddSegment(600, 0.73, 900, 0.9);
    gradientTransferFunction->AddPoint(1300, 0.1);
    vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();//定义并设置相关体属性
    volumeProperty->SetColor(colorTransferFunction);
    volumeProperty->SetScalarOpacity(opacityTransferFunction);
    volumeProperty->SetGradientOpacity(gradientTransferFunction);
    vtkVolumeRayCastCompositeFunction*compositeRaycastFunction = vtkVolumeRayCastCompositeFunction::New();//定义光线投射方法为合成体绘制方法
    vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New();
    volumeMapper->SetVolumeRayCastFunction(compositeRaycastFunction);//载入体绘制方法
    volumeMapper->SetInputConnection(shiftScale->GetOutputPort());
    volumeMapper->SetBlendModeToComposite();
    vtkVolume *volume = vtkVolume::New();//定义Volume
    volume->SetMapper(volumeMapper); 
    volume->SetProperty(volumeProperty);//设置体属性
    aRender->AddVolume(volume); //将Volume装载到绘制类中
    aRender->SetBackground(0.2, 0.3, 0.4);
    renWin->SetSize(500, 500);   //设置背景颜色和绘制窗口大小
    renWin->Render(); //窗口进行绘制
    iRen->Initialize();
    iRen->Start(); //初始化并进行交互绘制

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值