(32)VTK C++开发示例 ---背景纹理


更多精彩内容
👉内容导航 👈
👉VTK开发 👈

1. 概述

这段代码展示了如何使用 VTK 进行简单的 3D 数据可视化,包括点数据的插值、等值线生成和渲染设置,适用于学习 VTK 基础或快速创建可视化原型。

  1. 点和颜色的创建
    • 创建了两个点 (100, 0, 0)(300, 0, 0)
    • 为这些点分配了颜色(黑色和白色)以及标量值(0 和 1)。
  2. 数据处理
    • 使用 [vtkShepardMethod](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 对点数据进行插值,生成规则网格上的数据。
    • 使用 [vtkContourFilter](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 基于插值结果生成等值线,等值线的值为 0.250.50.75
  3. 可视化设置
    • 创建了两个vtkActor对象:
      • 第一个用于显示两个点,设置了点的颜色和大小。
      • 第二个用于显示等值线,设置了颜色映射和光照属性。
    • 使用 [vtkColorTransferFunction](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 定义了颜色映射,将标量值范围映射到 RGB 颜色(蓝色到红色)。
  4. 渲染设置
    • 创建了一个 [vtkRenderer](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 渲染器,添加了两个演员(点和等值线)。
    • 设置了渐变背景颜色(蓝色到品红色)。
    • 配置了相机的位置、焦点和视图方向。
  5. 渲染窗口和交互
    • 创建了一个 [vtkRenderWindow](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 渲染窗口,并将渲染器添加到窗口中。
    • 创建了一个 [vtkRenderWindowInteractor](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 交互器,用于处理用户交互。
    • 启动渲染和交互。

输出效果

  • 渲染窗口中显示两个点(一个黑色,一个白色)和基于插值生成的等值线。
  • 背景为蓝色到品红色的渐变。
  • 用户可以通过交互器旋转、缩放和移动视图。
环境说明
系统ubuntu22.04、windows11
cmake3.22、3.25
Qt5.14.2
编译器g++11.4、msvc2017
VTK9.4.1

2. CMake链接VTK

cmake_minimum_required(VERSION 3.20 FATAL_ERROR) # 设置CMake最低版本
project(vtk2) # 设置项目名称
# 查找VTK库
find_package(VTK COMPONENTS 
CommonColor
CommonCore
CommonDataModel
FiltersCore
FiltersGeneral
ImagingHybrid
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if(NOT VTK_FOUND)
message("VTK not found")
return()
endif()

add_executable(vtk2 main.cpp) # 添加可执行文件

target_link_libraries(vtk2 PRIVATE ${VTK_LIBRARIES}) # 链接VTK库
vtk_module_autoinit(TARGETS vtk2 MODULES ${VTK_LIBRARIES}) # 初始化VTK模块

3. main.cpp文件

/********************************************************************************
* 文件名:   main.cpp
* 创建时间: 2025-03-22 15:05:52
* 开发者:   MHF
* 邮箱:     1603291350@qq.com
* 功能:     
*********************************************************************************/
#include<iostream>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkColorTransferFunction.h>
#include <vtkContourFilter.h>
#include <vtkFloatArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkShepardMethod.h>
#include <vtkUnsignedCharArray.h>
#include <vtkVertexGlyphFilter.h>
#include <string>
using namespace std;

int main()
{
  vtkNew<vtkNamedColors> colors; 

  vtkNew<vtkPoints> points; // 创建点集
  points->InsertNextPoint(100, 0, 0);
  points->InsertNextPoint(300, 0, 0);

  unsigned char white[3] = {255, 255, 255}; // 白色
  unsigned char black[3] = {0, 0, 0}; // 黑色

  vtkNew<vtkUnsignedCharArray> colorsArray; // 创建颜色数组
  colorsArray->SetNumberOfComponents(3); // 设置颜色数组的组件数
  colorsArray->SetName("Colors"); // 设置颜色数组的名称
  colorsArray->InsertNextTypedTuple(black); // 插入黑色
  colorsArray->InsertNextTypedTuple(white); // 插入白色

  // 为pointdata创建一个标量数组,每个值表示距离。从第一个顶点开始的顶点
  vtkNew<vtkFloatArray> values; 
  values->SetNumberOfComponents(1); // 设置标量数组的组件数
  values->SetName("Values"); // 设置标量数组的名称
  values->InsertNextValue(0); // 插入第一个值
  values->InsertNextValue(1); // 插入第二个值

  vtkNew<vtkPolyData> polydata1; // 创建多边形数据
  polydata1->SetPoints(points); // 设置点集
  polydata1->GetPointData()->SetScalars(values); // 设置标量数据

  // 绘制两个点,一个黑色,一个白色
  vtkNew<vtkPolyData> polydata2; 
  polydata2->SetPoints(points);
  polydata2->GetPointData()->SetScalars(colorsArray); // 设置颜色数据

  vtkNew<vtkVertexGlyphFilter> glyphFilter1; // 创建顶点过滤器
  glyphFilter1->SetInputData(polydata2); // 设置输入数据
  glyphFilter1->Update(); // 更新顶点过滤器

  vtkNew<vtkPolyDataMapper> mapper1; // 创建多边形数据映射器
  mapper1->SetInputConnection(glyphFilter1->GetOutputPort()); // 设置输入连接

  vtkNew<vtkActor> actor1; // 创建演员
  actor1->SetMapper(mapper1); // 设置映射器
  actor1->GetProperty()->SetColor(colors->GetColor3d("red").GetData()); // 设置颜色
  actor1->GetProperty()->SetPointSize(5); // 设置点大小

  // 创建一个Shepard过滤器来插值正则化图像网格上的顶点。
  vtkNew<vtkShepardMethod> shepard; // 创建谢泼德方法,用于插值
  shepard->SetInputData(polydata1); // 设置输入数据
  // _arg1:X 轴方向的最小值。_arg2:Y 轴方向的最小值。_arg3:Z 轴方向的最小值。_arg4:X 轴方向的最大值。_arg5:Y 轴方向的最大值。_arg6:Z 轴方向的最大值。
  shepard->SetModelBounds(100, 300, -10, 10, -10, 10); // 设置模型边界
  shepard->SetSampleDimensions(2, 2, 2); // 设置采样维度
  shepard->SetMaximumDistance(1); // 设置最大距离

  // 绘制3个等值线,值为0.25,0.5,0.75
  vtkNew<vtkContourFilter> contour; // 创建等值线过滤器
  contour->SetInputConnection(shepard->GetOutputPort()); // 设置输入连接
  contour->SetNumberOfContours(3); // 设置等值线数,即等值线的数量
  contour->SetValue(0, 0.25); // 设置等值线的值
  contour->SetValue(1, 0.5);
  contour->SetValue(2, 0.75);
  contour->Update(); // 更新等值线过滤器

  vtkNew<vtkPolyDataMapper> mapper2; // 创建多边形数据映射器
  mapper2->SetInputConnection(contour->GetOutputPort()); // 设置输入连接
  mapper2->ScalarVisibilityOn(); // 设置标量可见
  mapper2->SetColorModeToMapScalars(); // 设置颜色模式为映射标量,即根据标量值映射颜色

  vtkNew<vtkActor> actor2; // 创建演员
  actor2->SetMapper(mapper2); // 设置映射器
  actor2->GetProperty()->SetAmbient(1); // 设置环境光系数,即光线照射到物体表面后,被物体表面反射的光线
  actor2->GetProperty()->SetDiffuse(0); // 设置漫反射系数,即光线照射到物体表面后,被物体表面反射的光线
  actor2->GetProperty()->SetSpecular(0); // 设置镜面反射系数,即光线照射到物体表面后,被物体表面反射的光线

  double* range = contour->GetOutput()->GetScalarRange(); // 获取标量范围
  vtkIdType nCells = contour->GetOutput()->GetNumberOfCells(); // 获取单元格数量

  double bounds[6]; // 创建边界数组
  for (vtkIdType i = 0; i < nCells; ++i) // 遍历单元格
  {
      if(i % 2)
      {
          contour->GetOutput()->GetCell(i)->GetBounds(bounds); // 获取边界
          cout << "Cell " << i << " Bounds: " << bounds[0] << " " << bounds[1] << " " << bounds[2] << " " << bounds[3] << " " << bounds[4] << " " << bounds[5] << endl;
      }
  }

  vtkNew<vtkColorTransferFunction> ctf; // 创建颜色传输函数
  ctf->SetColorSpaceToRGB(); // 设置颜色空间为RGB
  ctf->AddRGBPoint(range[0], 0, 0, 1); // 添加RGB点
  ctf->AddRGBPoint(range[1], 1, 0, 0); // 添加RGB点
  ctf->SetScaleToLinear(); // 设置线性缩放
  mapper2->SetLookupTable(ctf); // 设置查找表

  vtkNew<vtkRenderer> renderer; // 创建渲染器
  renderer->AddActor(actor1); // 添加演员
  renderer->AddActor(actor2); // 添加演员
  renderer->GradientBackgroundOn(); // 设置渐变背景
  renderer->SetBackground(colors->GetColor3d("Blue").GetData()); // 设置背景颜色
  renderer->SetBackground2(colors->GetColor3d("Magenta").GetData()); // 设置背景颜色

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer); // 添加渲染器
  renderWindow->SetWindowName("BackgroundTexture"); // 设置窗口名称

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow); // 设置渲染窗口

  auto camera = renderer->GetActiveCamera(); // 获取相机
  camera->SetPosition(450, 100, 100); // 设置相机位置
  camera->SetFocalPoint(200, 0, 0); // 设置焦点
  camera->SetViewUp(0, 0, 1); // 设置视图向上

  renderWindow->Render(); // 渲染
  renderWindowInteractor->Start(); // 开始

  return 0;
}

4. 演示效果

在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mahuifa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值