码迷,mamicode.com
首页 > 其他好文 > 详细

VTK 学习初步

时间:2016-05-13 00:47:15      阅读:466      评论:0      收藏:0      [点我收藏+]

标签:

VTK相关的基础知识

VTK,visualization toolkit,可视化工具包,主要应用于三维计算机图形、图像处理和可视化方面。它在三维函数库OpenGL的基础上采用面向对象的设计方法发展而来,且具有跨平台的特性。
VTK面向对象,含有大量的对象模型。
源对象是可视化流水线的起点,映射器(Mapper)对象是可视化流水线的终点,是图形模型和可视化模型之间的接口.
回调(或用户方法): 观察者监控一个对象所有被调用的事件,如果正在监控的一个事件被触发,一个与之相
应的回调函数就会被调用。
图形模型:
Renderer 渲染器,vtkRenderWindow 渲染窗口

可视化模型:
vtkDataObject 可以被看作是一个二进制大块(blob)
vtkProcessObject 过程对象一般也称为过滤器,按照某种运算法则对数据对象进行处理

VTK的非安装配置

如果有现成的含有dll文件(通常在bin文件夹中)同时想要不安装其他软件:
新建工程,配置附加包含目录,假设VTK文件夹放于C盘,右击相应的解决方案,配置属性——配置属性——C/C++——常规 输入: C:\VTK\include\vtk-5.10
C/C++同级目录下:链接器——常规——附加库目录: C:\VTK\lib\vtk-5.10
C/C++同级目录下:链接器——输入——附加依赖项:
LSDyna-gd.lib
MapReduceMPI-gd.lib
mpistubs-gd.lib
vtkalglib-gd.lib
vtkCharts-gd.lib
vtkCommon-gd.lib
vtkDICOMParser-gd.lib
vtkexoIIc-gd.lib
vtkexpat-gd.lib
vtkFiltering-gd.lib
vtkfreetype-gd.lib
vtkftgl-gd.lib
vtkGenericFiltering-gd.lib
vtkGeovis-gd.lib
vtkGraphics-gd.lib
vtkhdf5_hl-gd.lib
vtkhdf5-gd.lib
vtkHybrid-gd.lib
vtkImaging-gd.lib
vtkInfovis-gd.lib
vtkIO-gd.lib
vtkjpeg-gd.lib
vtklibxml2-gd.lib
vtkmetaio-gd.lib
vtkNetCDF_cxx-gd.lib
vtkNetCDF-gd.lib
vtkpng-gd.lib
vtkproj4-gd.lib
vtkRendering-gd.lib
vtksqlite-gd.lib
vtksys-gd.lib
vtktiff-gd.lib
vtkverdict-gd.lib
vtkViews-gd.lib
vtkVolumeRendering-gd.lib
vtkWidgets-gd.lib
vtkzlib-gd.lib

丢失vtkCommon-gd.dll (或其他相关文件)的解决:
复制文件到C:\windows\System32 与C:\windows\sysWOW64 (64位系统文件夹)再注册:
以管理员的方式打开cmd并输入:C:\windows\system32>regsvr32 C:\Windows\System32\vtkCommon-gd.dll

dll 一次性全注册:
在cmd中输入:
for %1 in (%windir%\system32 *.dll) do regsvr32.exe /s %1
回车运行。

最简单的例子

仅仅出现黑色窗口:

#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
int main(){
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    renWin->Render();
    std::cin.get();
    return 0;
}  

VTK读取图像与另存图像

简单读取显示并另存:

#include <vtkSmartPointer.h>
#include <vtkPNGWriter.h>
#include <vtkImageViewer2.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkTexture.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkJPEGReader.h>

int main(int, char *[])
{
    vtkSmartPointer< vtkJPEGReader > reader =
        vtkSmartPointer< vtkJPEGReader >::New();
    reader->SetFileName("D:\\pic.JPG");

    //显示图片
    vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New();
    viewer->SetInputConnection(reader->GetOutputPort());
    vtkSmartPointer<vtkRenderWindowInteractor> windownInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    viewer->SetupInteractor(windownInteractor);
    viewer->Render();
    viewer->GetRenderer()->ResetCamera();
    viewer->Render();

    //把JPG另存为PNG文件
    vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
    writer->SetFileName("D:\\pic.PNG");
    writer->SetInputConnection(reader->GetOutputPort());
    writer->Write();

    std::cin.get();
    return EXIT_SUCCESS;
}

技术分享

使用vtkImageViewer2是一种简单的图片显示方法,另一种较复杂的显示途径是 vtkImageActor. 不过需要建立一套 渲染管线

#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>
#include <vtkImageActor.h>
#include <vtkImageReader2Factory.h>

int main(int argc, char* argv[])
{
    vtkSmartPointer<vtkJPEGReader> reader =
        vtkSmartPointer<vtkJPEGReader>::New();
    reader->SetFileName("D:\\pic.JPG");
    reader->Update();

    vtkSmartPointer<vtkImageActor> imgActor =
        vtkSmartPointer<vtkImageActor>::New();
    imgActor->SetInput(reader->GetOutput());

    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(imgActor);
    renderer->SetBackground(1.0, 1.0, 1.0);

    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(640, 480);
    renderWindow->Render();
    renderWindow->SetWindowName("my_pic");

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    /*vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();
    //注释代码与是否实现三维旋转相关
    renderWindowInteractor->SetInteractorStyle(style);*/
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Initialize();

    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

技术分享

交互问题:我们可以定义vtkInteratorStyle子类进行鼠标、键盘的消息响应

vtk图像创建

手动创建图像

vtkSmartPointer<vtkImageData>img =
        vtkSmartPointer<vtkImageData>::New();
    img->SetDimensions(10, 10, 10);
    img->SetScalarTypeToUnsignedChar();
    img->SetNumberOfScalarComponents(1);
    img->AllocateScalars();  //分配标量
    unsigned char *ptr = (unsigned char *)img->GetScalarPointer();
    for (int i = 0; i < 10 * 10 * 10; i++){
         *ptr++ = (i&1)==0? 0 : 255;
    }

不可缺少的场景设置:

//--------------------------------------------------------------------------------------------------
// Create actors
    vtkSmartPointer<vtkImageActor> redActor =
        vtkSmartPointer<vtkImageActor>::New();
    redActor->SetInput(img);

    // Define viewport ranges
    // (xmin, ymin, xmax, ymax)
    double redViewport[4] = { 0.0, 0.0, 1.0, 1.0 };
    // Setup renderers
    vtkSmartPointer<vtkRenderer> redRenderer =
        vtkSmartPointer<vtkRenderer>::New();
    redRenderer->SetViewport(redViewport);
    redRenderer->AddActor(redActor);
    redRenderer->ResetCamera();
    redRenderer->SetBackground(1.0, 1.0, 1.0);

    // Setup render window
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(redRenderer);
    renderWindow->SetSize(640, 480);
    renderWindow->Render();
    renderWindow->SetWindowName("my_pictrue");

    // Setup render window interactor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> style =
        vtkSmartPointer<vtkInteractorStyleImage>::New();

    renderWindowInteractor->SetInteractorStyle(style);
    // Render and start interaction
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Initialize();
    renderWindowInteractor->Start();
//------------------------------------------------------------------------------------------------

技术分享

自己写vtk文件,使用paraview打开

画三个点并增添拓扑结构,用软件paraview打开

#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataWriter.h>
#include <vtkLine.h>
#include <vtkCellArray.h>

int main(int argc, char* argv[])
{
    vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
    points->InsertNextPoint(1.0, 0.0, 0.0); // ID start at: 0
    points->InsertNextPoint(1.0, 1.0, 0.0);
    points->InsertNextPoint(0.0, 0.0, 0.0);

    vtkSmartPointer<vtkLine> line0 = vtkSmartPointer<vtkLine>::New();
    line0->GetPointIds()->SetId(0, 0); // 第二个参数是点的ID
    line0->GetPointIds()->SetId(1, 1);

    vtkSmartPointer<vtkLine> line1 = vtkSmartPointer<vtkLine>::New();
    line1->GetPointIds()->SetId(0, 0); // 第二个参数是点的ID
    line1->GetPointIds()->SetId(1, 2);

    vtkSmartPointer<vtkLine> line2 = vtkSmartPointer<vtkLine>::New();
    line2->GetPointIds()->SetId(0, 1); // 第二个参数是点的ID
    line2->GetPointIds()->SetId(1, 2);

    //将线段放入一个vtkCellArray数组中
    vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
    lines->InsertNextCell(line0);
    lines->InsertNextCell(line1);
    lines->InsertNextCell(line2);

    //把点和线统统放进数据集中
    vtkSmartPointer<vtkPolyData> Polydata = vtkSmartPointer<vtkPolyData>::New();
    Polydata->SetPoints(points);
    Polydata->SetLines(lines);

    vtkSmartPointer<vtkPolyDataWriter> writer = vtkSmartPointer<vtkPolyDataWriter>::New();
    writer->SetFileName("triangle.vtk");
    writer->SetInput(Polydata);
    writer->Write();
    return EXIT_SUCCESS;
}

技术分享

VTK相关学习URL:

http://www.vtk.org/
http://www.vtk.org/doc/nightly/html/index.html

VTK 学习初步

标签:

原文地址:http://blog.csdn.net/thearcticocean/article/details/51347170

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!