(32)VTK C++开发示例 ---背景纹理
文章目录
- 1. 概述
- 2. CMake链接VTK
- 3. main.cpp文件
- 4. 演示效果
更多精彩内容 |
---|
👉内容导航 👈 |
👉VTK开发 👈 |
1. 概述
这段代码展示了如何使用 VTK 进行简单的 3D 数据可视化,包括点数据的插值、等值线生成和渲染设置,适用于学习 VTK 基础或快速创建可视化原型。
- 点和颜色的创建:
- 创建了两个点
(100, 0, 0)
和(300, 0, 0)
。- 为这些点分配了颜色(黑色和白色)以及标量值(0 和 1)。
- 数据处理:
- 使用 [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.25
、0.5
和0.75
。- 可视化设置:
- 创建了两个vtkActor对象:
- 第一个用于显示两个点,设置了点的颜色和大小。
- 第二个用于显示等值线,设置了颜色映射和光照属性。
- 使用 [vtkColorTransferFunction](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 定义了颜色映射,将标量值范围映射到 RGB 颜色(蓝色到红色)。
- 渲染设置:
- 创建了一个 [vtkRenderer](vscode-file://vscode-app/d:/Microsoft VS Code/resources/app/out/vs/code/electron-sandbox/workbench/workbench.html) 渲染器,添加了两个演员(点和等值线)。
- 设置了渐变背景颜色(蓝色到品红色)。
- 配置了相机的位置、焦点和视图方向。
- 渲染窗口和交互:
- 创建了一个 [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 |
cmake | 3.22、3.25 |
Qt | 5.14.2 |
编译器 | g++11.4、msvc2017 |
VTK | 9.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.75vtkNew<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(); // 设置颜色空间为RGBctf->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;
}