网站优化及推广方案,从化做网站建设,扬中网站开发,网站建设项目前分析流程可以总结如下#xff1a; 导入所需的头文件#xff1a; 首先#xff0c;导入了一系列 VTK 头文件#xff0c;这些文件包含了所需的类和函数声明。 创建对象#xff1a; 创建了两个球体#xff08;一个较大#xff0c;一个较小#xff09;#xff0c;一个平面 导入所需的头文件 首先导入了一系列 VTK 头文件这些文件包含了所需的类和函数声明。 创建对象 创建了两个球体一个较大一个较小一个平面以及一个用于连接球体的 vtkAppendPolyData 对象。 设置可视化对象的属性 设置了球体和平面的属性例如颜色和显示模式。 创建渲染器和渲染窗口 创建了一个渲染器和一个渲染窗口并将球体和平面添加到渲染器中。还设置了渲染器的背景色。 创建交互器 创建了一个交互器并将其与渲染窗口关联。设置交互器的样式为 TrackballCamera。 创建仿射变换控件 创建了一个仿射变换控件并将其与渲染器和球体关联。同时创建了一个回调函数用于处理控件的交互事件。 启动交互模式 最后启动交互器进入交互模式。用户可以通过交互器与场景中的对象进行交互包括旋转、平移和缩放。
整个流程涵盖了创建可视化场景、添加交互式控件以及处理用户交互的过程使用户能够与场景中的对象进行实时交互。
这段代码是一个简单的使用 VTKVisualization Toolkit创建交互式 3D 可视化的示例。让我们逐行分析它并解释每个部分的作用。
包含头文件
#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 中的各种对象所需的头文件。
主函数
int main(int, char*[])程序的入口点。
vtkNewvtkNamedColors colors;创建一个 VTK 颜色对象用于设置可视化对象的颜色。
vtkNewvtkSphereSource sphereSource;
sphereSource-Update();vtkNewvtkSphereSource sphereSource2;
sphereSource2-SetRadius(0.075);
sphereSource2-SetCenter(0, 0.5, 0);
sphereSource2-Update();创建两个球体一个是较大的一个较小的。较小的球体位于较大球体的顶部用作旋转时的参考点。
vtkNewvtkAppendPolyData append;
append-AddInputConnection(sphereSource-GetOutputPort());
append-AddInputConnection(sphereSource2-GetOutputPort());将两个球体连接成一个 vtkPolyData 对象。
vtkNewvtkPlaneSource planeSource;
planeSource-SetXResolution(4);
planeSource-SetYResolution(4);
planeSource-SetOrigin(-1, -1, 0);
planeSource-SetPoint1(1, -1, 0);
planeSource-SetPoint2(-1, 1, 0);创建一个平面它位于较大的球体之上并具有 4x4 的子部分。
vtkNewvtkPolyDataMapper planeMapper;
planeMapper-SetInputConnection(planeSource-GetOutputPort());
vtkNewvtkActor planeActor;
planeActor-SetMapper(planeMapper);
planeActor-GetProperty()-SetRepresentationToWireframe();
planeActor-GetProperty()-SetColor(colors-GetColor3d(Red).GetData());为平面创建一个 Mapper 和 Actor使其以线框模式显示并设置颜色为红色。
vtkNewvtkPolyDataMapper mapper;
mapper-SetInputConnection(append-GetOutputPort());
vtkNewvtkActor actor;
actor-SetMapper(mapper);为球体创建一个 Mapper 和 Actor。
vtkNewvtkRenderer renderer;
vtkNewvtkRenderWindow 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());创建一个渲染器和渲染窗口将球体和平面添加到渲染器中并设置渐变背景色。
vtkNewvtkRenderWindowInteractor renderWindowInteractor;
renderWindowInteractor-SetRenderWindow(renderWindow);
dynamic_castvtkInteractorStyleSwitch*(renderWindowInteractor-GetInteractorStyle())-SetCurrentStyleToTrackballCamera();创建一个交互器并将其与渲染窗口关联。设置交互器的样式为 TrackballCamera。
vtkNewvtkAffineWidget affineWidget;
affineWidget-SetInteractor(renderWindowInteractor);
affineWidget-CreateDefaultRepresentation();
dynamic_castvtkAffineRepresentation2D*(affineWidget-GetRepresentation())-PlaceWidget(actor-GetBounds());vtkNewvtkAffineCallback affineCallback;
affineCallback-Actor actor;
affineCallback-AffineRep dynamic_castvtkAffineRepresentation2D*(affineWidget-GetRepresentation());affineWidget-AddObserver(vtkCommand::InteractionEvent, affineCallback);
affineWidget-AddObserver(vtkCommand::EndInteractionEvent, affineCallback);renderWindow-Render();
renderWindowInteractor-Initialize();
renderWindow-Render();
affineWidget-On();创建一个仿射变换控件并将其与渲染器和球体关联。定义一个回调函数用于处理交互事件。最后启动交互器进入交互模式。
void* parentid renderWindow-GetGenericParentId();
void* windowsid renderWindow-GetGenericWindowId();获取渲染窗口的父 ID 和窗口 ID。
renderWindowInteractor-Start();开始交互进入事件循环。
return EXIT_SUCCESS;程序正常退出。
回调函数
namespace {
void vtkAffineCallback::Execute(vtkObject*, unsigned long vtkNotUsed(event), void*)
{this-AffineRep-GetTransform(this-Transform);this-Actor-SetUserTransform(this-Transform);
}
} // namespace定义了一个回调函数用于处理仿射变换控件的交互事件。当用户与控件交互时将获取控件的变换并将其应用于球体对象。
这就是代码的详细解释。通过这个示例您可以了解如何使用 VTK 创建交互式 3D 可视化并添加仿射变换控件以进行对象操作。