VTK 示例 基本的流程-事件交互、球体、

流程可以总结如下:

  1. 导入所需的头文件: 首先,导入了一系列 VTK 头文件,这些文件包含了所需的类和函数声明。

  2. 创建对象: 创建了两个球体(一个较大,一个较小),一个平面,以及一个用于连接球体的 vtkAppendPolyData 对象。

  3. 设置可视化对象的属性: 设置了球体和平面的属性,例如颜色和显示模式。

  4. 创建渲染器和渲染窗口: 创建了一个渲染器和一个渲染窗口,并将球体和平面添加到渲染器中。还设置了渲染器的背景色。

  5. 创建交互器: 创建了一个交互器,并将其与渲染窗口关联。设置交互器的样式为 TrackballCamera。

  6. 创建仿射变换控件: 创建了一个仿射变换控件,并将其与渲染器和球体关联。同时,创建了一个回调函数用于处理控件的交互事件。

  7. 启动交互模式: 最后,启动交互器,进入交互模式。用户可以通过交互器与场景中的对象进行交互,包括旋转、平移和缩放。

整个流程涵盖了创建可视化场景、添加交互式控件以及处理用户交互的过程,使用户能够与场景中的对象进行实时交互。

这段代码是一个简单的使用 VTK(Visualization Toolkit)创建交互式 3D 可视化的示例。让我们逐行分析它,并解释每个部分的作用。

包含头文件

cpp 复制代码
#include <vtkActor.h>
#include <vtkAffineRepresentation2D.h>
#include <vtkAffineWidget.h>
#include <vtkAppendPolyData.h>
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleSwitch.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTransform.h>

#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkLightCollection.h>
#include <vtkLightActor.h>

#include <vtkPolyLine.h>

这些是用于创建和操作 VTK 中的各种对象所需的头文件。

主函数

cpp 复制代码
int main(int, char*[])

程序的入口点。

cpp 复制代码
vtkNew<vtkNamedColors> colors;

创建一个 VTK 颜色对象,用于设置可视化对象的颜色。

cpp 复制代码
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();

vtkNew<vtkSphereSource> sphereSource2;
sphereSource2->SetRadius(0.075);
sphereSource2->SetCenter(0, 0.5, 0);
sphereSource2->Update();

创建两个球体,一个是较大的,一个较小的。较小的球体位于较大球体的顶部,用作旋转时的参考点。

cpp 复制代码
vtkNew<vtkAppendPolyData> append;
append->AddInputConnection(sphereSource->GetOutputPort());
append->AddInputConnection(sphereSource2->GetOutputPort());

将两个球体连接成一个 vtkPolyData 对象。

cpp 复制代码
vtkNew<vtkPlaneSource> planeSource;
planeSource->SetXResolution(4);
planeSource->SetYResolution(4);
planeSource->SetOrigin(-1, -1, 0);
planeSource->SetPoint1(1, -1, 0);
planeSource->SetPoint2(-1, 1, 0);

创建一个平面,它位于较大的球体之上,并具有 4x4 的子部分。

cpp 复制代码
vtkNew<vtkPolyDataMapper> planeMapper;
planeMapper->SetInputConnection(planeSource->GetOutputPort());
vtkNew<vtkActor> planeActor;
planeActor->SetMapper(planeMapper);
planeActor->GetProperty()->SetRepresentationToWireframe();
planeActor->GetProperty()->SetColor(colors->GetColor3d("Red").GetData());

为平面创建一个 Mapper 和 Actor,使其以线框模式显示,并设置颜色为红色。

cpp 复制代码
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(append->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);

为球体创建一个 Mapper 和 Actor。

cpp 复制代码
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("AffineWidget");

renderer->AddActor(actor);
renderer->AddActor(planeActor);
renderer->GradientBackgroundOn();
renderer->SetBackground(colors->GetColor3d("LightSkyBlue").GetData());
renderer->SetBackground2(colors->GetColor3d("MidnightBlue").GetData());

创建一个渲染器和渲染窗口,将球体和平面添加到渲染器中,并设置渐变背景色。

cpp 复制代码
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
dynamic_cast<vtkInteractorStyleSwitch*>(renderWindowInteractor->GetInteractorStyle())->SetCurrentStyleToTrackballCamera();

创建一个交互器,并将其与渲染窗口关联。设置交互器的样式为 TrackballCamera。

cpp 复制代码
vtkNew<vtkAffineWidget> affineWidget;
affineWidget->SetInteractor(renderWindowInteractor);
affineWidget->CreateDefaultRepresentation();
dynamic_cast<vtkAffineRepresentation2D*>(affineWidget->GetRepresentation())->PlaceWidget(actor->GetBounds());

vtkNew<vtkAffineCallback> affineCallback;
affineCallback->Actor = actor;
affineCallback->AffineRep = dynamic_cast<vtkAffineRepresentation2D*>(affineWidget->GetRepresentation());

affineWidget->AddObserver(vtkCommand::InteractionEvent, affineCallback);
affineWidget->AddObserver(vtkCommand::EndInteractionEvent, affineCallback);

renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindow->Render();
affineWidget->On();

创建一个仿射变换控件,并将其与渲染器和球体关联。定义一个回调函数,用于处理交互事件。最后,启动交互器,进入交互模式。

cpp 复制代码
void* parentid = renderWindow->GetGenericParentId();
void* windowsid = renderWindow->GetGenericWindowId();

获取渲染窗口的父 ID 和窗口 ID。

cpp 复制代码
renderWindowInteractor->Start();

开始交互,进入事件循环。

cpp 复制代码
return EXIT_SUCCESS;

程序正常退出。

回调函数

cpp 复制代码
namespace {
void vtkAffineCallback::Execute(vtkObject*, unsigned long vtkNotUsed(event), void*)
{
  this->AffineRep->GetTransform(this->Transform);
  this->Actor->SetUserTransform(this->Transform);
}
} // namespace

定义了一个回调函数,用于处理仿射变换控件的交互事件。当用户与控件交互时,将获取控件的变换并将其应用于球体对象。

这就是代码的详细解释。通过这个示例,您可以了解如何使用 VTK 创建交互式 3D 可视化,并添加仿射变换控件以进行对象操作。

相关推荐
世人万千丶3 小时前
鸿蒙跨端框架 Flutter 学习 iverpod 实战:超越 Provider 的响应式状态管理
学习·flutter·华为·交互·harmonyos·鸿蒙
Curvatureflight1 天前
前端性能优化指南:从加载到交互的每一毫秒
前端·性能优化·交互
方见华Richard2 天前
自指宇宙学:存在如何通过自我描述而实在化V0.2
人工智能·交互·原型模式·空间计算
工业HMI实战笔记2 天前
HMI权限分级设计:兼顾安全与操作效率的平衡术
运维·数据库·安全·ui·自动化·人机交互·交互
向前V2 天前
Flutter for OpenHarmony数独游戏App实战:单元格交互与选中
flutter·游戏·交互
Coder_Boy_2 天前
基于SpringAI的在线考试系统-核心模块的数据模型交互关系
java·数据库·人工智能·spring boot·交互
世人万千丶2 天前
鸿蒙跨端框架 Flutter 学习 Day 4:网络交互——HTTP 请求基础与数据反序列化实战
网络·学习·flutter·ui·交互·harmonyos·鸿蒙
世人万千丶3 天前
鸿蒙跨端框架 Flutter 学习 Day 3:综合实践——多维数据流与实时交互实验室
学习·flutter·华为·交互·harmonyos·鸿蒙
大雷神3 天前
Harmony App 开发中Flutter 与鸿蒙原生交互传参教程
flutter·交互·harmonyos
cy_cy0023 天前
展厅多屏联动推动智慧展示创新
科技·人机交互·交互·软件构建