VTK中一些功能

vkt中一些功能

获取渲染的shader

    renderWindow->Render();
    vtkOpenGLShaderCache* shadercache = renderWindow->GetShaderCache();
    vtkShaderProgram* shaderprogram = shadercache->GetLastShaderBound();
    std::cout << "FragmentShader:\n" << shaderprogram->GetFragmentShader()->GetSource();

opengl中保存数组为图片

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texturecolor);
    glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_FLOAT,texuturebufcolor);

    static int indexgg = 0;
    std::string filename = "IMGComputeShaderTest_" +
            std::to_string(indexgg) + ".ppm";
    ofstream outfile;
    outfile.open(filename.c_str());
    int nx = resolutionx;
    int ny = resolutiony;
    outfile << "P3\n" << nx << " " << ny << "\n255\n";
    for (int j = ny - 1; j >= 0; j--){
        for (int i = 0; i < nx; i++){
            int pixindex = (j*resolutionx+i)*4;
            int ir = int(255.99*texuturebufcolor[pixindex+0]);
            int ig = int(255.99*texuturebufcolor[pixindex+1]);
            int ib = int(255.99*texuturebufcolor[pixindex+2]);
            outfile << ir << " " << ig << " " << ib << "\n";
        }
    }
    outfile.close();
    indexgg++;

VTK中保存屏幕截图

vtkDemo地址:https://lorensen.github.io/VTKExamples/site/Cxx/Utilities/Screenshot/

关键代码:

将renderwindow中的图像转换为png
  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = 
    vtkSmartPointer<vtkWindowToImageFilter>::New();
  windowToImageFilter->SetInput(renderWindow);
#if VTK_MAJOR_VERSION > 8 || VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 1
  windowToImageFilter->SetScale(2); //image quality
#else
  windowToImageFilter->SetMagnification(2); //image quality
#endif
  windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
  windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
  windowToImageFilter->Update();

  vtkSmartPointer<vtkPNGWriter> writer = 
    vtkSmartPointer<vtkPNGWriter>::New();
  writer->SetFileName("screenshot2.png");
  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
  writer->Write();
将renderwindow中的图像转换为QPixmap或QImage
    /*将renderwindow中的图像转换为QPixmap或QImage*/
    vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
      vtkSmartPointer<vtkWindowToImageFilter>::New();
    windowToImageFilter->SetInput(renderwindow);
  #if VTK_MAJOR_VERSION > 8 || VTK_MAJOR_VERSION == 8 && VTK_MINOR_VERSION >= 1
    windowToImageFilter->SetScale(2); //image quality
  #else
    windowToImageFilter->SetMagnification(1); //image quality
  #endif
    windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
    windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
    windowToImageFilter->Update();

    vtkImageData* imageData = windowToImageFilter->GetOutput();

    int width = imageData->GetDimensions()[0];
    int height = imageData->GetDimensions()[1];

    qDebug() << width << "   " << height;
    QImage image( width, height, QImage::Format_RGB32 );
    QRgb *rgbPtr =
      reinterpret_cast<QRgb *>( image.bits() ) + width * ( height - 1 );
    unsigned char *colorsPtr =
      reinterpret_cast<unsigned char *>( imageData->GetScalarPointer() );

    // Loop over the vtkImageData contents.
    for ( int row = 0; row < height; row++ )
    {
      for ( int col = 0; col < width; col++ )
      {
        // Swap the vtkImageData RGB values with an equivalent QColor
        *( rgbPtr++ ) = QColor( colorsPtr[0], colorsPtr[1], colorsPtr[2] ).rgb();
        colorsPtr += imageData->GetNumberOfScalarComponents();
      }

      rgbPtr -= width * 2;
    }

    QPixmap pixmapToShow = QPixmap::fromImage( image );

通过相机旋转actor

关键代码

        int dx = nowposx - lastposx;
        int dy = nowposy - lastposy;

        int *size = renderwindow->GetSize();

        double delta_elevation = -20.0 / size[1];
        double delta_azimuth = -20.0 / size[0];

        double rxf = dx * delta_azimuth * 10.0;
        double ryf = dy * delta_elevation * 10.0;

        vtkCamera *camera = renderer->GetActiveCamera();
        camera->Azimuth(rxf);
        camera->Elevation(ryf);
        camera->OrthogonalizeViewUp();
        renderwindow->Render();

构造vtkImageData

    int dims[3] = {512,512,133};
    double spacing[3] = {0.7,0.7,2.5};
    vtkImageData* image = vtkImageData::New();
    image->SetDimensions(dims);
    image->SetSpacing(spacing);
    image->AllocateScalars(VTK_SHORT,1);
    short *ptr = static_cast<short*>(image->GetScalarPointer());
    vtkDataArray* dataArr = image->GetPointData()->GetScalars();
    for(int i=0;i<dataArr->GetNumberOfTuples();i++){
        dataArr->InsertComponent(i,0,0);
    }
    image->Modify();
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
VTK(可视化工具包)是一个开源的图形图像处理库,用于科学数据可视化和计算机图形应用开发。VTK资料包括VTK文手册,这是一本详细介绍VTK库的使用方法、函数和类的手册,并且使用文进行了翻译。 VTK文手册是VTK库的重要参考资料,对于学习和使用VTK来说至关重要。它提供了VTK库的详细说明,包括库的基本概念、常用函数和类的介绍,以及示例代码和用法说明。通过阅读VTK文手册,用户可以了解VTK的整体架构和基本原理,并能够快速上手使用VTK库进行科学数据可视化和图形图像处理。 VTK文手册的内容丰富全面,结构清晰易懂。它从VTK的安装和编译开始,详细介绍了VTK的数据模型、剖析器、可视化管线等核心功能。此外,手册还介绍了常用的模块和类,如几何模型、纹理贴图、体积渲染等,以及一些高级特性,如交互式操作和动画效果的实现。 VTK文手册不仅适用于初学者,对于有一定VTK经验的开发者来说,它也是一个非常好的参考文档。无论是进行科学数据的可视化展示,还是开发计算机图形应用程序,VTK文手册都能帮助用户快速理解和使用VTK库的功能,提高开发效率。 总之,VTK文手册是学习和应用VTK库的必备资料之一,它通过清晰的语言和丰富的示例帮助用户掌握VTK库的使用方法和技巧。无论是新手还是有经验的开发者,阅读VTK文手册都能对VTK库有更深入的理解,并快速上手使用VTK进行图形图像处理和科学数据可视化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力减肥的小胖子5

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

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

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

打赏作者

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

抵扣说明:

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

余额充值