当前位置: 首页 > article >正文

VTK的学习方法-第一类型应用

相信很多做研究的人都在使用VTK,其实VTK的学习分成两类。一类就是使用VTK中现有的算法来完成自己的工作,比如数据的显示和渲染。另外一类是需要继承里面的算法类,自己根据自己的需求来重新写一个算法。

对于第一种类型的应用,不要觉得使用C++来写代码会很复杂,其实不复杂。此种应用一般都是从数据源出发,然后顺着数据流管线一路往下走,最后显示。是面向过程的。

图1 简单的VTK管线流

下面的一个例子,就是这样的一个简单的应用,使用了AppendFilter算法。

#include <vtkActor.h>
#include <vtkAppendFilter.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3DMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointSource.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkUnstructuredGrid.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <iostream>

vtkSmartPointer<vtkActor> Point2Glyph(vtkPoints* points,double const& scale);

int main(int,char**)
{
    // Create 9 points
    vtkNew<vtkPointSource> pointSource;
    pointSource->SetNumberOfPoints(9);
    pointSource->Update();

    vtkSmartPointer<vtkPolyData> polydata=pointSource->GetOutput();
    std::cout<<"There are "<<polydata->GetNumberOfPoints()
            <<" points in the polydata set."<<std::endl;

    // Create 2 points in a vtkUnstructuredGrid
    vtkNew<vtkPoints> points;
    points->InsertNextPoint(0,0,0);
    points->InsertNextPoint(0,0,1);

    vtkNew<vtkUnstructuredGrid> ug;
    ug->SetPoints(points);
    std::cout<<"There are "<<ug->GetNumberOfPoints()
            <<" points in the unstructured grid."<<std::endl;

    // Combine the two data sets
    vtkNew<vtkAppendFilter> appendFilter;
    appendFilter->AddInputData(polydata);
    appendFilter->AddInputData(ug);
    appendFilter->Update();

    vtkSmartPointer<vtkUnstructuredGrid> combined=appendFilter->GetOutput();
    std::cout<<"There are "<<combined->GetNumberOfPoints()
            <<" points combined."<<std::endl;

    // Create a mapper and actor
    vtkNew<vtkNamedColors> colors;
    vtkNew<vtkDataSetMapper> mapper;
    mapper->SetInputConnection(appendFilter->GetOutputPort());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkActor> sphereActor=Point2Glyph(appendFilter->GetOutput()->GetPoints(),0.25);
    sphereActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());

    // Create renderer window and interactor
    vtkNew<vtkRenderer> ren;
    vtkNew<vtkRenderWindow> renWin;
    renWin->SetSize(1024,1024);
    renWin->AddRenderer(ren);
    vtkNew<vtkRenderWindowInteractor> iren;
    iren->SetRenderWindow(renWin);
    vtkNew<vtkInteractorStyleTrackballCamera> style;
    iren->SetInteractorStyle(style);

    ren->AddActor(actor);
    ren->AddActor(sphereActor);
    ren->SetBackground(colors->GetColor3d("RoyalBlue").GetData());
    iren->Initialize();
    iren->Start();

    return 0;
}

vtkSmartPointer<vtkActor> Point2Glyph(vtkPoints* points,double const& scale){
    double* bounds=points->GetBounds();
    double maxLen=0;
    for(int i=1;i<3;++i){
        maxLen=std::max(bounds[i+1]-bounds[i],maxLen);
    }
    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->SetRadius(scale*maxLen);
    sphereSource->SetThetaResolution(30);
    sphereSource->SetPhiResolution(30);

    vtkNew<vtkPolyData> pd;
    pd->SetPoints(points);

    vtkNew<vtkGlyph3DMapper> mapper;
    mapper->SetInputData(pd);
    mapper->SetSourceConnection(sphereSource->GetOutputPort());
    mapper->ScalarVisibilityOff();
    mapper->ScalingOff();

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    return actor;
}

输出的结果如下:

图2 输出结果


http://www.kler.cn/a/355047.html

相关文章:

  • 004-spring-注解aop的使用
  • VMware虚拟机超详细安装Linux教程(最新版)
  • C语言结构体位定义(位段)的实际作用深入分析
  • 预览和下载 (pc和微信小程序)
  • springboot项目对数据库密码加解密
  • 顶顶通呼叫中心中间件mod_cti模块安全增强,预防盗打风险(mod_cti基于FreeSWITCH)
  • 后端——eclipse实现前端后端的交互(1)
  • SpringCloud学习记录|day5
  • 常用的字符集(ASCII、GBK)
  • 速卖通关键字搜索接口技术解析及Python代码示例
  • leetcode哈希表(三)-两数之和
  • Nordic 学习小记录
  • R语言:ERGM指数随机图模型
  • Navigation2 算法流程
  • IDEA启动报错,java: OutOfMemoryError: insufficient memory
  • FLINK SQL 元数据持久化扩展
  • 如何将本地磁盘镜像包部署到docker中(以mysql5_7.tar.gz为例)
  • SpringBoot智能推荐:健康生活新趋势
  • 深入理解Python中的字符串:str()、字符提取、replace()替换及内存分析
  • JS爬虫实战之Tiktok中sec_id获取
  • JavaScript网页设计案例:构建动态交互的在线图书管理系统
  • 3万字66道Java基础面试题总结(2024版本)
  • 个人用数据挖掘笔记(待补充)
  • vb6 MSHFlexGrid1表格导出数据到电子表格 解决只能导出一次问题
  • ubuntu安装mysql5.7
  • RAID 矩阵