程序运行结果如下:
滚动鼠标可以放大和缩小
可以任意转动观察角度:
代码如下:
#include "stdafx.h"
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL)
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include <vtkImageData.h>
#include <vtkStructuredPoints.h>
#include <vtkImageReader.h>
#include <vtkVolumeRayCastCompositeFunction.h>
#include <vtkGPUVolumeRayCastMapper.h>
#include <vtkVolumeRayCastMapper.h>
#include <vtkColorTransferFunction.h>
#include <vtkPiecewiseFunction.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkVolumeProperty.h>
#include <vtkAxesActor.h>
#include <vtkImageShiftScale.h>
#include <vtkImageCast.h>
#include <vtkFixedPointVolumeRayCastMapper.h>
#include <vtkDICOMImageReader.h>
#include <vtkContourFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataMapper.h>
#include <vtkOutlineFilter.h>
#include <vtkCamera.h>
#include "vtkInteractorStyleTrackballCamera.h"
void main()
{
vtkRenderer *aRenderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(aRenderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkDICOMImageReader *v = vtkDICOMImageReader::New();//vtkDicomImageReader用于读取医学DICOM图像
v->SetDataByteOrderToLittleEndian();//设置为小端字节序
v->SetDirectoryName("D:\\project_vtk\\vtk_rebuilding_CT\\data");//文件目录路径
v->SetDataSpacing(3.2, 3.2, 1.5);//设置数据间距
//已知500对应病人的皮肤,抽取值为500的等值面。然后通过vtkPolyDataNormals在面上产生法线
vtkContourFilter *skinExtractor = vtkContourFilter::New();//过滤器vtkContourFilter用于从数据中抽取一系列等值面
skinExtractor->SetInputConnection(v->GetOutputPort());
skinExtractor->SetValue(0, 500);//对抽取等值面进行设置,表示从第0个抽取值开始,设为500
vtkPolyDataNormals *skinNormals = vtkPolyDataNormals::New();//在面上产生法线,使渲染过程中的表面着色光滑些
skinNormals->SetInputConnection(skinExtractor->GetOutputPort());
skinNormals->SetFeatureAngle(60.0);//设置特征角度
vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New();
skinMapper->SetInputConnection(skinNormals->GetOutputPort());
skinMapper->ScalarVisibilityOff();
vtkActor *skin = vtkActor::New();
skin->SetMapper(skinMapper);
//产生一个围绕着数据的框架
vtkOutlineFilter *outlineData = vtkOutlineFilter::New();
outlineData->SetInputConnection(v->GetOutputPort());
vtkPolyDataMapper *mapOutline = vtkPolyDataMapper::New();
mapOutline->SetInputConnection(outlineData->GetOutputPort());
vtkActor *outline = vtkActor::New();
outline->SetMapper(mapOutline);
outline->GetProperty()->SetColor(0, 0, 0);
//新建相机实例
/*
vtkCamera *aCamera = vtkCamera::New();
aCamera->SetViewUp(2, -1, -1); //取得摄像机方向0,0,-1 0,-1,-1 2,-1,-1
aCamera->SetPosition(0, 1, 0); //光源位置
aCamera->SetFocalPoint(0, 0, 0); //取焦点坐标
*/
aRenderer->AddActor(outline);
aRenderer->AddActor(skin);
//aRenderer->SetActiveCamera(aCamera);
aRenderer->ResetCamera();
//aCamera->Dolly(1.5);//将相机靠近焦点,以放大图像
aRenderer->SetBackground(1, 1, 1);//设置背景为白色
renWin->SetSize(600, 500);
//由于Dolly()方法移动了相机,所以要重设相机的剪切范围
aRenderer->ResetCameraClippingRange();
//设置交互方式
vtkInteractorStyleTrackballCamera *style =vtkInteractorStyleTrackballCamera::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();
v->Delete();
skinExtractor->Delete();
skinNormals->Delete();
skinMapper->Delete();
skin->Delete();
outlineData->Delete();
mapOutline->Delete();
outline->Delete();
//aCamera->Delete();
style->Delete();
iren->Delete();
renWin->Delete();
aRenderer->Delete();
}