VTK 体绘制(VTK8.2亲测可运行)

核心逻辑代码

//注册两个工厂类,这样可以构造对应的抽象类,否则抽象类new出来是空指针
vtkObjectFactory::RegisterFactory(vtkRenderingOpenGL2ObjectFactory::New());
vtkObjectFactory::RegisterFactory(vtkRenderingVolumeOpenGL2ObjectFactory::New());

//读取DICOM图像数据
vtkSmartPointer<vtkDICOMImageReader> reader = 
vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName("文件");

//对图像数据进行光滑处理
vtkSmartPointer<vtkImageGaussianSmooth> gaussianSmoothFilter = vtkSmartPointer<vtkImageGaussianSmooth>::New();
gaussianSmoothFilter->SetInputConnection(reader->GetOutputPort());//三维方向平滑

//vtkVolumeProperty 设置Volume属性
vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
volumeProperty->SetInterpolationTypeToLinear();

//vtkPiecewiseFunction
vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
volumeProperty->SetScalarOpacity(compositeOpacity);

//vtkColorTransferFunction
vtkSmartPointer<vtkColorTransferFunction> color = vtkSmartPointer<vtkColorTransferFunction>::New();
volumeProperty->SetColor(color);

//vtkVolume 设置Volume对象
vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);

//vtkRenderer 
vtkSmartPointer<vtkRenderer> aRenderer = 
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = 
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(aRenderer);
//vtkRenderWindowInteractor
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);

//构造三维体模型外围线框
vtkSmartPointer<vtkOutlineFilter> outlineData = vtkSmartPointer<vtkOutlineFilter>::New();//线框
outlineData->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> mapOutline = vtkSmartPointer<vtkPolyDataMapper>::New();//将输入的数据转换为几何图元(点/线/多边形)进行渲染
mapOutline->SetInputConnection(outlineData->GetOutputPort());

//构造actor对象
vtkSmartPointer<vtkActor> outline = vtkSmartPointer<vtkActor>::New();
outline->SetMapper(mapOutline);

//该方法用于将vtkProp类型的对象添加到渲染场景
aRenderer->AddVolume(volume);
aRenderer->AddActor(outline);

//设置相机跟踪模式
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);//定义交互器样式

renWin->Render();
iren->Initialize();
iren->Start();


 完整代码

//体绘制
#include<vtkRenderWindowInteractor.h>
#include<vtkDICOMImageReader.h>
#include<vtkCamera.h>
#include<vtkActor.h>
#include<vtkRenderer.h>
#include<vtkVolumeProperty.h>
#include<vtkProperty.h>
#include<vtkPolyDataNormals.h>
#include<vtkImageShiftScale.h>
#include <vtkFixedPointVolumeRayCastMapper.h>
#include<vtkPiecewiseFunction.h>
#include<vtkColorTransferFunction.h>
#include<vtkRenderWindow.h>
#include<vtkImageCast.h>
#include<vtkOBJExporter.h>
#include<vtkOutlineFilter.h>
#include<vtkPolyDataMapper.h>
#include <vtkFixedPointVolumeRayCastMIPHelper.h>
#include<vtkInteractorStyleTrackballCamera.h>
#include<vtkRenderingVolumeOpenGL2ObjectFactory.h>
#include<vtkRenderingOpenGL2ObjectFactory.h>
#include<vtkMetaImageReader.h>
#include<vtkLODProp3D.h>


//体绘制加速

#include<vtkGPUVolumeRayCastMapper.h>

#include<iostream>

int main()
{
	//注册两个工厂类,这样可以构造对应的抽象类,否则抽象类new出来是空指针
	vtkObjectFactory::RegisterFactory(vtkRenderingOpenGL2ObjectFactory::New());
	vtkObjectFactory::RegisterFactory(vtkRenderingVolumeOpenGL2ObjectFactory::New());


	//vtkImageData 读取数据
	vtkSmartPointer<vtkDICOMImageReader> reader = vtkSmartPointer<vtkDICOMImageReader>::New();
	reader->SetDirectoryName("D:/sus7");
	reader->SetDataByteOrderToLittleEndian();
	//vtkFixedPointVolumeRayCastMapper 固定像素点的体数据映射器
	vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> volumeMapper = vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
	volumeMapper->SetInputData(reader->GetOutput());
		

	//vtkVolumeProperty 设置Volume属性
	vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
	volumeProperty->SetInterpolationTypeToLinear();
	volumeProperty->ShadeOn();
	volumeProperty->SetAmbient(0.4);
	volumeProperty->SetDiffuse(0.6);
	volumeProperty->SetSpecular(0.2);
	//vtkPiecewiseFunction
	vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
	compositeOpacity->AddPoint(70, 0.00);
	compositeOpacity->AddPoint(90, 0.40);
	compositeOpacity->AddPoint(180, 0.60);
	volumeProperty->SetScalarOpacity(compositeOpacity);
	//vtkColorTransferFunction
	vtkSmartPointer<vtkColorTransferFunction> color = vtkSmartPointer<vtkColorTransferFunction>::New();
	color->AddRGBPoint(0.000, 0.00, 0.00, 0.00);
	color->AddRGBPoint(64.00, 1.00, 0.52, 0.30);
	color->AddRGBPoint(190.0, 1.00, 1.00, 1.00);
	color->AddRGBPoint(220.0, 1.00, 1.00, 1.00);
	volumeProperty->SetColor(color);


	//vtkVolume 设置Volume
	vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
	volume->SetMapper(volumeMapper);
	volume->SetProperty(volumeProperty);


	//vtkRenderer 
	vtkSmartPointer<vtkRenderer> aRenderer = vtkSmartPointer<vtkRenderer>::New();
	vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
	renWin->AddRenderer(aRenderer);
	//vtkRenderWindowInteractor
	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(renWin);

	//构造三维体模型外围线框
	vtkSmartPointer<vtkOutlineFilter> outlineData = vtkSmartPointer<vtkOutlineFilter>::New();//线框
	outlineData->SetInputConnection(reader->GetOutputPort());
	vtkSmartPointer<vtkPolyDataMapper> mapOutline = vtkSmartPointer<vtkPolyDataMapper>::New();//该类用于渲染多边形几何数据,将输入的数据转换为几何图元(点/线/多边形)进行渲染
	mapOutline->SetInputConnection(outlineData->GetOutputPort());
	vtkSmartPointer<vtkActor> outline = vtkSmartPointer<vtkActor>::New();
	outline->SetMapper(mapOutline);
	outline->GetProperty()->SetColor(0, 0, 0);//背景纯黑色;


	//该方法用于将vtkProp类型的对象添加到渲染场景
	aRenderer->AddVolume(volume);
	aRenderer->AddActor(outline);
	aRenderer->SetBackground(1, 1, 1);
	aRenderer->ResetCamera();


    //设置窗口大小
	renWin->SetSize(800, 800);
	renWin->SetWindowName("测试");

	//设置相机跟踪模式
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	iren->SetInteractorStyle(style);//定义交互器样式
	//重设相机的剪切范围
	aRenderer->ResetCameraClippingRange();


	//
	renWin->Render();
	iren->Initialize();
	iren->Start();


	return EXIT_SUCCESS;

}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值