// 引入VTK并初始化
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConvexPointSet.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3DMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkUnstructuredGrid.h>
int main()
{
// 创建一个凸多边形点集
vtkNew<vtkConvexPointSet> cps;
vtkNew<vtkPoints> points;
// 插入点的坐标
// 共有13个点,描述一个凸多边形
// 前8个点构成一个立方体,后5个点在底面中心和四个边的中心
points->InsertNextPoint(0, 0, 0);
points->InsertNextPoint(1, 0, 0);
points->InsertNextPoint(1, 1, 0);
points->InsertNextPoint(0, 1, 0);
points->InsertNextPoint(0, 0, 1);
points->InsertNextPoint(1, 0, 1);
points->InsertNextPoint(1, 1, 1);
points->InsertNextPoint(0, 1, 1);
points->InsertNextPoint(0.5, 0, 0);
points->InsertNextPoint(1, 0.5, 0);
points->InsertNextPoint(0.5, 1, 0);
points->InsertNextPoint(0, 0.5, 0);
points->InsertNextPoint(0.5, 0.5, 0);
for (int i = 0; i < 13; ++i)
{
// 将点的ID插入凸多边形点集
cps->GetPointIds()->InsertId(i, i);
}
// 创建一个非结构化网格并设置其几何数据
vtkNew<vtkUnstructuredGrid> ug;
ug->Allocate(1, 1);
ug->InsertNextCell(cps->GetCellType(), cps->GetPointIds());
ug->SetPoints(points);
// 创建命名颜色对象
vtkNew<vtkNamedColors> colors;
// 创建数据集映射器
vtkNew<vtkDataSetMapper> mapper;
mapper->SetInputData(ug);
// 创建演员(Actor)并设置其属性
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
actor->GetProperty()->SetLineWidth(3);
actor->GetProperty()->EdgeVisibilityOn();
// 为点创建球体源
vtkNew<vtkSphereSource> sphere;
sphere->SetPhiResolution(21);
sphere->SetThetaResolution(21);
sphere->SetRadius(.03);
// 创建用于存储点和球体的多边形数据
vtkNew<vtkPolyData> polyData;
polyData->SetPoints(points);
// 创建点的球体映射器
vtkNew<vtkGlyph3DMapper> pointMapper;
pointMapper->SetInputData(polyData);
pointMapper->SetSourceConnection(sphere->GetOutputPort());
// 创建点的演员并设置其属性
vtkNew<vtkActor> pointActor;
pointActor->SetMapper(pointMapper);
pointActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());
// 创建渲染器、渲染窗口和交互器
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetWindowName("ConvexPointSet");
renderWindow->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// 将演员添加到场景中
renderer->AddActor(actor);
renderer->AddActor(pointActor);
renderer->SetBackground(colors->GetColor3d("Silver").GetData());
// 重置相机位置并设置视角
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(210);
renderer->GetActiveCamera()->Elevation(30);
renderer->ResetCameraClippingRange();
// 渲染并交互
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}
几何对象的凸点集
最新推荐文章于 2024-01-04 16:30:24 发布