VTK系列69_VTK格式文件的读取与渲染显示

该代码示例演示了如何使用VTK库读取VTK格式的医学图像文件,并进行渲染与显示。首先创建渲染器、渲染窗口和交互器,然后使用vtkPolyDataReader读取文件,通过vtkPolyDataMapper和vtkActor进行数据映射和渲染。同时,还设置了相机视角并调整了渲染窗口的背景色和大小。最后,启动交互事件循环。
摘要由CSDN通过智能技术生成

 实例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();

}

          

本例程配套素材见源码整理文章下载(点击进入)

VTK系列目录:

1 VTK基本概念

2 VTK图像处理

3 VTK图形处理

4 VTK体绘制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦我飞也

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值