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

相关推荐
声网8 小时前
从语音识别到智能助手:Voice Agent 的技术进化与交互变革丨Voice Agent 学习笔记
学习·交互·语音识别
EndingCoder8 小时前
排序算法与前端交互优化
开发语言·前端·javascript·算法·排序算法·交互
MARS_AI_9 小时前
云蝠智能VoiceAgent重构企业电话客服体系
人工智能·自然语言处理·人机交互·交互·信息与通信
学术小八15 小时前
2025年人工智能、虚拟现实与交互设计国际学术会议
人工智能·交互·vr
南無忘码至尊1 天前
Unity 实现与 Ollama API 交互的实时流式响应处理
unity·游戏引擎·交互
Python智慧行囊2 天前
微信小程序交互精髓:点击操作与状态管理实战
微信小程序·小程序·交互
MARS_AI_3 天前
云蝠智能 Voice Agent 落地展会邀约场景:重构会展行业的智能交互范式
人工智能·自然语言处理·重构·交互·语音识别·信息与通信
AxureMost3 天前
Axure-9高级教程:Axure函数使用手册-免费
交互·axure
AxureMost6 天前
Axure常用交互功能案例-免费
交互·axure·photoshop
大美B端工场-B端系统美颜师7 天前
多模态数据融合预警:从IoT传感器到卫星监测的可视化方案升级
物联网·交互·数据预警