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

相关推荐
ZYJCSZKJ1 天前
AI数字人实时交互系统的工程架构与多方言适配实践
人工智能·架构·交互·ai数字人直播系统
zcmodeltech1 天前
钢铁冶金沙盘模型控制系统设计与实现:高炉-连铸-热轧多工序联动方案
分布式·stm32·单片机·嵌入式硬件·交互
CappuccinoRose2 天前
JSON 数据交互规范
json·交互
论迹复利3 天前
TF-M SPE Mailbox IPC 交互流程与 Cache 维护详解
交互·bt·psa
元岳数字人小元4 天前
极简运维体系,数字人一体机适配中小场景升级
运维·人工智能·开源·人机交互·交互·源代码管理
见山是山-见水是水4 天前
鸿蒙Panel 滑动面板完全指南:半屏/全屏切换、拖拽交互与地图应用
华为·交互·harmonyos
ZYJCSZKJ5 天前
AI数字人直播系统的技术架构与多方言交互实现方案——基于广西区域商业场景的工程实践
人工智能·架构·交互
柳叶方舟9 天前
Nature Medicine IF=50.0 | 公众医疗助手LLM可靠性存隐忧:真人交互表现未优于对照组,现有评估基准失效
论文阅读·人工智能·深度学习·交互·健康医疗
dwm8888888810 天前
FluidVoice 智能语音交互落地实战指南
交互
人工智能研究所11 天前
GitHub 10k+ Star:用自然语言描述系统,AI 帮你生成可交互架构图
人工智能·github·交互·自然语言·系统架构图·archify