(40)VTK C++开发示例 ---集合

文章目录

    • [1. 概述](#1. 概述)
    • [2. CMake链接VTK](#2. CMake链接VTK)
    • [3. main.cpp文件](#3. main.cpp文件)
    • [4. 演示效果](#4. 演示效果)

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

1. 概述

使用vtkAssembly将球体和立方体组合成一个组件;

三维模型创建与组合

  • 创建了球体(vtkSphereSource)和立方体(vtkCubeSource)两种基础三维模型
  • 使用vtkAssembly将两个模型组合成一个装配体组件

几何变换处理

  • 应用vtkTransform进行坐标系变换
  • 将组合体整体平移5个单位(Translate(5.0, 0.0, 0.0)

可视化属性设置

  • 为模型设置颜色(MistyRose)和半透明效果(SetOpacity(0.5)
  • 渲染背景设置为石板灰(SlateGray)

交互式可视化

  • 创建完整的渲染管线(Renderer/RenderWindow/Interactor)
  • 支持三维场景的交互式查看(旋转/缩放/平移)
环境 说明
系统 ubuntu22.04、windows11
cmake 3.22、3.25
Qt 5.14.2
编译器 g++11.4、msvc2017
VTK 9.4.1

2. CMake链接VTK

shell 复制代码
cmake_minimum_required(VERSION 3.20 FATAL_ERROR) # 设置CMake最低版本
project(vtk2) # 设置项目名称
# 查找VTK库
find_package(VTK COMPONENTS 
CommonColor
CommonCore
CommonTransforms
FiltersSources
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文件

cpp 复制代码
/********************************************************************************
* 文件名:   main.cpp
* 创建时间: 2025-03-23 21:47:04
* 开发者:   MHF
* 邮箱:     [email protected]
* 功能:     
*********************************************************************************/
#include<iostream>
#include <vtkActor.h>
#include <vtkAssembly.h>
#include <vtkCubeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTransform.h>

using namespace std;

int main()
{
  vtkNew<vtkNamedColors> colors;
  vtkNew<vtkSphereSource> sphere; //创建球体

  vtkNew<vtkPolyDataMapper> sphereMapper; //创建球体映射器
  sphereMapper->SetInputConnection(sphere->GetOutputPort());

  vtkNew<vtkActor> sphereActor; //创建球体演员
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  vtkNew<vtkCubeSource> cube; //创建立方体
  cube->SetCenter(5.0, 0.0, 0.0); //设置立方体中心坐标

  vtkNew<vtkPolyDataMapper> cubeMapper; //创建立方体映射器
  cubeMapper->SetInputConnection(cube->GetOutputPort());

  vtkNew<vtkActor> cubeActor; //创建立方体演员
  cubeActor->SetMapper(cubeMapper);
  cubeActor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  /******************将球体和立方体组合成一个组件************************/ 
  vtkNew<vtkAssembly> assembly; //创建组装体
  assembly->AddPart(sphereActor); //添加球体演员
  assembly->AddPart(cubeActor); //添加立方体演员

  vtkNew<vtkTransform> transform; //创建变换
  transform->PostMultiply(); //后乘,后乘模式下,新的变换将发生在当前变换之后
  transform->Translate(5.0, 0.0, 0.0); //平移

  assembly->SetUserTransform(transform); //设置组装体的变换

  vtkNew<vtkPropCollection> collection; //创建属性集合
  collection->InitTraversal(); //初始化遍历
  for(vtkIdType i = 0; i < collection->GetNumberOfItems(); i++) //遍历演员
  {
      dynamic_cast<vtkActor*>(collection->GetNextProp())->GetProperty()->SetOpacity(0.5); //设置透明度
  }
  assembly->GetActors(collection); //获取组装体的演员

  vtkNew<vtkRenderer> renderer; //创建渲染器
  renderer->AddActor(assembly); //添加组装体
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData()); //设置背景颜色

  vtkNew<vtkRenderWindow> renderWindow; //创建渲染窗口
  renderWindow->AddRenderer(renderer); //添加渲染器

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor; //创建渲染窗口交互器
  renderWindowInteractor->SetRenderWindow(renderWindow); //设置渲染窗口

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

  return 0;
}

4. 演示效果



相关推荐
智驾16 分钟前
C++,设计模式,【建造者模式】
c++·设计模式·建造者模式
Invinciblenuonuo18 分钟前
实习技能记录【4】-----消息分发中的观察者模型
c++
Wabi_sabi_x1 小时前
C++设计模式:面向对象的八大设计原则之三
开发语言·c++·设计模式
qq_447429411 小时前
数据结构与算法:图论——最短路径
c语言·数据结构·c++·图论
yi个名字2 小时前
C++ STL vector容器详解:从原理到实践
开发语言·c++
xin007hoyo2 小时前
算法笔记.求约数
c++·笔记·算法
共享家95272 小时前
C++ 多态:原理、实现与应用
c++
虾球xz2 小时前
c++环境和vscode常用的一些有用插件
c++·ide·vscode
柏木乃一3 小时前
平衡二叉搜索树模拟实现1-------AVL树(插入,删除,查找)
c++·学习·程序人生·算法·二叉搜索树·avl树·平衡二叉搜索树
我命由我123453 小时前
C++ - 数据容器之 forward_list(创建与初始化、元素访问、容量判断、元素遍历、添加元素、删除元素)
c语言·开发语言·c++·后端·visualstudio·visual studio·后端开发