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

相关推荐
AxureMost2 天前
Axure常用交互功能案例-免费
交互·axure·photoshop
大美B端工场-B端系统美颜师3 天前
多模态数据融合预警:从IoT传感器到卫星监测的可视化方案升级
物联网·交互·数据预警
小彭努力中3 天前
141.在 Vue 3 中使用 OpenLayers Link 交互:把地图中心点 / 缩放级别 / 旋转角度实时写进 URL,并同步解析显示
前端·javascript·vue.js·交互
梓贤Vigo3 天前
【Axure高保真原型】动态打字输入效果
交互·产品经理·axure·原型
梓贤Vigo3 天前
【Axure视频教程】大小图轮播
交互·产品经理·axure·原型·中继器
hi星尘3 天前
深度解析:Java内部类与外部类的交互机制
java·开发语言·交互
哈市雪花3 天前
相机:Camera原理讲解(使用OpenGL+QT开发三维CAD)
qt·3d·交互·相机·图形学·opengl·视角
just小千3 天前
重学React(二):添加交互
javascript·react.js·交互
TiAmo zhang3 天前
人机融合智能 | 人智交互中的人类状态识别
人机交互·交互
温轻舟4 天前
3D词云图
前端·javascript·3d·交互·词云图·温轻舟