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 可视化,并添加仿射变换控件以进行对象操作。

相关推荐
程序猿追2 天前
在 HarmonyOS 手机上从零实现一个手绘涂鸦板——Canvas 绘制原理与触摸交互实践
智能手机·交互·harmonyos
nashane2 天前
HarmonyOS 6学习:旋转动画优化与长截图性能调优——打造丝滑交互体验的深度实践
学习·交互·harmonyos·harmonyos 5
ZC跨境爬虫2 天前
UI前端美化技能提升日志day7:(原生苹方字体全局适配+合规页脚完整像素级落地)
前端·javascript·ui·html·交互
Ulyanov3 天前
《PySide6 GUI开发指南:QML核心与实践》 第十篇:综合实战——构建完整的跨平台个人管理应用
开发语言·python·qt·ui·交互·qml·雷达电子战系统仿真
想你依然心痛3 天前
HarmonyOS 6(API 23)实战:基于 Face AR & Body AR 的“空间交互设计工作台“——PC端手势驱动3D建模系统
ar·交互·harmonyos·悬浮导航·沉浸光感
cy_cy0023 天前
科技展厅借数字化实现跨越式发展
大数据·科技·人机交互·交互·软件构建
梓贤Vigo4 天前
【Axure原型分享】拖拉拽动态调整页面布局-动态面板
交互·产品经理·axure·原型
木斯佳4 天前
HarmonyOS 纸感交互实战:把天气卡片做成便利贴撕下效果
华为·交互·harmonyos
Ulyanov4 天前
《PySide6 GUI开发指南:QML核心与实践》 第五篇:Python与QML深度融合——数据绑定与交互
开发语言·python·qt·ui·交互·雷达电子战系统仿真
不喝水就会渴5 天前
从基础到实战:鸿蒙 ArkUI 属性动画开发指南
华为·交互·动画·harmonyos