实例10:VTK格式文件的读取与渲染显示
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include "vtkRenderer.h"//绘制器
#include "vtkRenderWindow.h"//绘制窗口
#include "vtkRenderWindowInteractor.h"//加入交互机制类
#include "vtkDICOMImageReader.h"//DCM医学文件读取类
#include "vtkPolyDataMapper.h"//数据映射
#include "vtkActor.h"//演员
#include "vtkOutlineFilter.h"
#include "vtkCamera.h"//照相机
#include "vtkProperty.h"//属性设置类
#include "vtkPolyDataNormals.h"//vtkPolyData为多边形数据
#include "vtkContourFilter.h"//等值面提取类(可以接受任何的数据类型生成等值线或者等值面)
#include "vtkPolyDataWriter.h"//保存为.vtk图像类
#include "vtkPolyDataReader.h"//读取.vtk图像类
void main()
{
// Create the renderer, the render window, and the interactor. The renderer
// draws into the render window, the interactor enables mouse- and
// keyboard-based interaction with the data within the render window.
//创建渲染器、渲染窗口和交互器。
//绘制到渲染窗口,交互器启用,渲染窗口内的数据进行基于鼠标和键盘的交互。
vtkRenderer* aRenderer = vtkRenderer::New();
vtkRenderWindow* renWin = vtkRenderWindow::New();
renWin->AddRenderer(aRenderer);
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
//读取vtk格式图像
vtkPolyDataReader* v16 = vtkPolyDataReader::New();
v16->SetFileName("spine.vtk");
vtkPolyDataMapper* skinMapper = vtkPolyDataMapper::New();
skinMapper->SetInputConnection(v16->GetOutputPort());
skinMapper->ScalarVisibilityOff();//这样不会带颜色
vtkActor* skin = vtkActor::New();
skin->SetMapper(skinMapper);
// An outline provides context around the data.
vtkOutlineFilter* outlineData = vtkOutlineFilter::New();
outlineData->SetInputConnection(v16->GetOutputPort());
vtkPolyDataMapper* mapOutline = vtkPolyDataMapper::New();
mapOutline->SetInputConnection(outlineData->GetOutputPort());
vtkActor* outline = vtkActor::New();
outline->SetMapper(mapOutline);
outline->GetProperty()->SetColor(0, 0, 0);
// It is convenient to create an initial view of the data. The FocalPoint
// and Position form a vector direction. Later on (ResetCamera() method)
// this vector is used to position the camera to look at the data in
// this direction.
//相机设置
vtkCamera* aCamera = vtkCamera::New();
aCamera->SetViewUp(0, 0, -1);
aCamera->SetPosition(0, 1, 0);
aCamera->SetFocalPoint(0, 0, 0);
aCamera->ComputeViewPlaneNormal();
// Actors are added to the renderer. An initial camera view is created.
// The Dolly() method moves the camera towards the FocalPoint,
// thereby enlarging the image.
//渲染
aRenderer->AddActor(outline);
aRenderer->AddActor(skin);
aRenderer->SetActiveCamera(aCamera);
aRenderer->ResetCamera();
aCamera->Dolly(1.5);
// Set a background color for the renderer and set the size of the
// render window (expressed in pixels).
//设置背景色和窗口大小
aRenderer->SetBackground(1, 1, 1);
renWin->SetSize(640, 480);
// Note that when camera movement occurs (as it does in the Dolly()
// method), the clipping planes often need adjusting. Clipping planes
// consist of two planes: near and far along the view direction. The
// near plane clips out objects in front of the plane; the far plane
// clips out objects behind the plane. This way only what is drawn
// between the planes is actually rendered.
aRenderer->ResetCameraClippingRange();
// Initialize the event loop and then start it.
iren->Initialize();
iren->Start();
// It is important to delete all objects created previously to prevent
// memory leaks. In this case, since the program is on its way to
// exiting, it is not so important. But in applications it is
// essential.
//释放内存
v16->Delete();
/*skinExtractor->Delete();
skinNormals->Delete();*/
skinMapper->Delete();
skin->Delete();
outlineData->Delete();
mapOutline->Delete();
outline->Delete();
aCamera->Delete();
iren->Delete();
renWin->Delete();
aRenderer->Delete();
}