PCL——VTK常见几何模型生成

目录

  • 一、 圆柱体
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 二、圆盘
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 三、十二面体
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 四、地球
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 五、椭圆圆柱
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 六、截头椎体
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 七、几何对象
    • 1、概述
    • 2、代码实现
    • 3、结果展示
  • 八、六面体
    • 1、概述
    • 2、代码实现
    • 3、结果展示

一、 圆柱体

1、概述

创建一个最小的可视化程序,演示了VTK的基本渲染和管道创建。

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


int main(int, char* [])
{
    vtkNew<vtkNamedColors> colors;

    // 设置背景颜色
    std::array<unsigned char, 4> bkg{ {26, 51, 102, 255} };
    colors->SetColor("BkgColor", bkg.data());

    // 创建一个具有八个面的圆柱模型
    vtkNew<vtkCylinderSource> cylinder;
    cylinder->SetResolution(8); // 设置圆柱面的个数
    // mapper负责将几何图形推送到图形库中。 它还可以进行颜色映射。
    vtkNew<vtkPolyDataMapper> cylinderMapper;
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
    // actor是一种分组机制:除了几何图形(映射器),它也有一个属性,变换矩阵,和/或纹理映射。  
    // 这里设置它的颜色,并围绕X和Y轴旋转它。 
    vtkNew<vtkActor> cylinderActor;
    cylinderActor->SetMapper(cylinderMapper);
    cylinderActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
    cylinderActor->RotateX(30.0);
    cylinderActor->RotateY(-45.0);
   
    // renderer用来生成显示的图像,它可以被认为是一个加入了演员的场景 
    vtkNew<vtkRenderer> renderer;
    renderer->AddActor(cylinderActor);
    renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
    // 通过访问相机并调用它的“Zoom”方法来放大一点。
    renderer->ResetCamera();
    renderer->GetActiveCamera()->Zoom(1.5);
    // RenderWindow是显示在电脑屏幕上实际的GUI窗口  
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetSize(600, 600);
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("Cylinder");

    // RenderWindowInteractor捕获鼠标事件和将执行适当的相机或演员操作,这取决于事件的性质。 
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);
    // 启动事件循环,并作为一个副作用导致初始渲染。 
    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

3、结果展示

PCL——VTK常见几何模型生成_第1张图片

二、圆盘

1、概述

vtkDiskSource对象创建一个中间有孔的多边形圆盘。圆盘的高度为零。用户可以指定圆盘的内外半径,以及多边形的径向和周向分辨率表示。

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include  // Disk头文件
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char* [])
{
	vtkNew<vtkNamedColors> colors;
	// ---------------------------生成圆盘-----------------------------------
	vtkNew<vtkDiskSource> diskSource;
	// ---------------------------可视化-------------------------------------
	// Create a mapper and actor.
	vtkNew<vtkPolyDataMapper> mapper;
	mapper->SetInputConnection(diskSource->GetOutputPort());

	vtkNew<vtkActor> actor;
	actor->GetProperty()->SetColor(colors->GetColor3d("Cornsilk").GetData());
	actor->SetMapper(mapper);

	// Create a renderer, render window, and interactor
	vtkNew<vtkRenderer> renderer;
	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->SetWindowName("Disk");
	renderWindow->AddRenderer(renderer);
	vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);

	// Add the actors to the scene
	renderer->AddActor(actor);
	renderer->SetBackground(colors->GetColor3d("DarkGreen").GetData());

	// Render and interact
	renderWindow->Render();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

3、结果展示

PCL——VTK常见几何模型生成_第2张图片

三、十二面体

1、概述

创建一个十二面体

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include  // 多面体头文件
#include 
#include 
#include 
#include 
#include 

namespace 
{
    vtkSmartPointer<vtkPolyhedron> MakeDodecahedron();
}

int main(int, char* [])
{
    vtkNew<vtkNamedColors> colors;

    auto dodecahedron = MakeDodecahedron();

    // Visualize
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputData(dodecahedron->GetPolyData());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(colors->GetColor3d("PapayaWhip").GetData());

    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetWindowName("Dodecahedron");
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize(640, 480);
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(colors->GetColor3d("CadetBlue").GetData());
    renderer->GetActiveCamera()->Azimuth(30);
    renderer->GetActiveCamera()->Elevation(30);

    renderer->ResetCamera();

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

namespace {

    vtkSmartPointer<vtkPolyhedron> MakeDodecahedron()
    {
        vtkSmartPointer<vtkPolyhedron> aDodecahedron =
            vtkSmartPointer<vtkPolyhedron>::New();

        for (int i = 0; i < 20; ++i)
        {
            aDodecahedron->GetPointIds()->InsertNextId(i);
        }

        aDodecahedron->GetPoints()->InsertNextPoint(1.21412, 0, 1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(0.375185, 1.1547, 1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(-0.982247, 0.713644, 1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(-0.982247, -0.713644, 1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(0.375185, -1.1547, 1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(1.96449, 0, 0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(0.607062, 1.86835, 0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(-1.58931, 1.1547, 0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(-1.58931, -1.1547, 0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(0.607062, -1.86835, 0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(1.58931, 1.1547, -0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(-0.607062, 1.86835, -0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(-1.96449, 0, -0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(-0.607062, -1.86835, -0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(1.58931, -1.1547, -0.375185);
        aDodecahedron->GetPoints()->InsertNextPoint(0.982247, 0.713644, -1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(-0.375185, 1.1547, -1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(-1.21412, 0, -1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(-0.375185, -1.1547, -1.58931);
        aDodecahedron->GetPoints()->InsertNextPoint(0.982247, -0.713644, -1.58931);

        vtkIdType faces[73] = { 12,                    // number of faces
                               5,  0,  1,  2,  3,  4, // number of ids on face, ids
                               5,  0,  5,  10, 6,  1,  5,  1,  6,  11, 7,  2,  5,  2,
                               7,  12, 8,  3,  5,  3,  8,  13, 9,  4,  5,  4,  9,  14,
                               5,  0,  5,  15, 10, 5,  14, 19, 5,  16, 11, 6,  10, 15,
                               5,  17, 12, 7,  11, 16, 5,  18, 13, 8,  12, 17, 5,  19,
                               14, 9,  13, 18, 5,  19, 18, 17, 16, 15 };

        aDodecahedron->SetFaces(faces);
        aDodecahedron->Initialize();

        return aDodecahedron;
    }
} // namespace

3、结果展示

PCL——VTK常见几何模型生成_第3张图片

四、地球

1、概述

创建一个地球

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include  // 相关头文件
#include 
#include 
#include  // 保存成png图片
#include 
#include 
#include 
#include 
#include 
#include 
#include  // 球
#include 

int main(int, char* [])
{

    vtkNew<vtkNamedColors> colors;

    // ----------------------地球资源---------------------------
    vtkNew<vtkEarthSource> earthSource;
    earthSource->OutlineOn();
    earthSource->Update();

    // -------------------创建一个球体-------------------------
    vtkNew<vtkSphereSource> sphere;
    sphere->SetThetaResolution(100);
    sphere->SetPhiResolution(100);
    sphere->SetRadius(earthSource->GetRadius());
    // ---------------------可视化-----------------------------
    // Create a mapper and actor
    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(earthSource->GetOutputPort());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

    vtkNew<vtkPolyDataMapper> sphereMapper;
    sphereMapper->SetInputConnection(sphere->GetOutputPort());

    vtkNew<vtkActor> sphereActor;
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(
        colors->GetColor3d("PeachPuff").GetData());

    // Create a renderer, render window, and interactor
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    // Add the actor to the scene
    renderer->AddActor(actor);
    renderer->AddActor(sphereActor);
    renderer->SetBackground(colors->GetColor3d("Black").GetData());

    renderWindow->SetSize(640, 480);
    renderWindow->SetWindowName("EarthSource");

    // Render and interact
    renderWindow->Render();

    // ------------------将显示结果保存为png图片------------------
    // screenshot code:
    vtkNew<vtkWindowToImageFilter> w2if ;
    w2if->SetInput(renderWindow);
    w2if->Update();
    vtkNew<vtkPNGWriter> writer;
    writer->SetFileName("TestEarthSource.png");
    writer->SetInputConnection(w2if->GetOutputPort());
    writer->Write();
    // begin interaction
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

3、结果展示

PCL——VTK常见几何模型生成_第4张图片

五、椭圆圆柱

1、概述

这个例子创建了一个椭圆形的横截面,并将其存储在vtkPolyData中。然后,vtkLinearExtrusionFilter通过沿着矢量挤压vtkPolyLine创建一个椭圆圆柱。该示例设置vtkActor的backface属性,以显示圆柱体的正面和背面。

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char*[])
{

    double angle = 0;
    double r1, r2;
    double centerX, centerY;
    r1 = 50;
    r2 = 30;
    centerX = 10.0;
    centerY = 5.0;

    vtkNew<vtkPoints> points;
    int id = 0;
    while (angle <= 2.0 * vtkMath::Pi() + (vtkMath::Pi() / 60.0))
    {
        points->InsertNextPoint(r1 * cos(angle) + centerX,
            r2 * sin(angle) + centerY, 0.0);
        angle = angle + (vtkMath::Pi() / 60.0);
        ++id;
    }

    vtkNew<vtkPolyLine> line;
    line->GetPointIds()->SetNumberOfIds(id);
    for (unsigned int i = 0; i < static_cast<unsigned int>(id); ++i)
    {
        line->GetPointIds()->SetId(i, i);
    }

    vtkNew<vtkCellArray> lines;
    lines->InsertNextCell(line);

    vtkNew<vtkPolyData> polyData;
    polyData->SetPoints(points);
    polyData->SetLines(lines);

    vtkNew<vtkLinearExtrusionFilter> extrude;
    extrude->SetInputData(polyData);
    extrude->SetExtrusionTypeToNormalExtrusion();
    extrude->SetVector(0, 0, 100.0);
    extrude->Update();

    vtkNew<vtkNamedColors> colors;

    vtkNew<vtkPolyDataMapper> lineMapper;
    lineMapper->SetInputData(polyData);

    vtkNew<vtkActor> lineActor;
    lineActor->SetMapper(lineMapper);
    lineActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());

    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(extrude->GetOutputPort());

    vtkNew<vtkProperty> back;
    back->SetColor(colors->GetColor3d("Tomato").GetData());

    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
    actor->SetBackfaceProperty(back);

    vtkNew<vtkRenderer> ren;
    ren->SetBackground(colors->GetColor3d("SlateGray").GetData());
    ren->AddActor(actor);
    ren->AddActor(lineActor);

    vtkNew<vtkRenderWindow> renWin;
    renWin->SetWindowName("EllipticalCylinder");
    renWin->AddRenderer(ren);
    renWin->SetSize(600, 600);

    vtkNew<vtkRenderWindowInteractor> iren;
    iren->SetRenderWindow(renWin);

    vtkNew<vtkInteractorStyleTrackballCamera> style;
    iren->SetInteractorStyle(style);

    vtkNew<vtkCamera> camera;
    camera->SetPosition(0, 1, 0);
    camera->SetFocalPoint(0, 0, 0);
    camera->SetViewUp(0, 0, 1);
    camera->Azimuth(30);
    camera->Elevation(30);

    ren->SetActiveCamera(camera);
    ren->ResetCamera();
    ren->ResetCameraClippingRange();

    renWin->Render();
    iren->Start();

    return EXIT_SUCCESS;
}


3、结果展示

PCL——VTK常见几何模型生成_第5张图片

六、截头椎体

1、概述

这个例子从一个摄像机中获取了一个截头椎体并显示在屏幕上。

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char* [])
{
	vtkNew<vtkNamedColors> colors;

	vtkNew<vtkCamera> camera;
	camera->SetClippingRange(.1, .4);
	double planesArray[24];

	camera->GetFrustumPlanes(1.0, planesArray);

	vtkNew<vtkPlanes> planes;
	planes->SetFrustumPlanes(planesArray);

	vtkNew<vtkFrustumSource> frustumSource;
	frustumSource->ShowLinesOff();
	frustumSource->SetPlanes(planes);

	vtkNew<vtkShrinkPolyData> shrink;
	shrink->SetInputConnection(frustumSource->GetOutputPort());
	shrink->SetShrinkFactor(.9);

	vtkNew<vtkPolyDataMapper> mapper;
	mapper->SetInputConnection(shrink->GetOutputPort());

	vtkNew<vtkProperty> back;
	back->SetColor(colors->GetColor3d("Tomato").GetData());

	vtkNew<vtkActor> actor;
	actor->SetMapper(mapper);
	actor->GetProperty()->EdgeVisibilityOn();
	actor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
	actor->SetBackfaceProperty(back);

	// a renderer and render window
	vtkNew<vtkRenderer> renderer;
	vtkNew<vtkRenderWindow> renderWindow;
	renderWindow->SetSize(640, 480);
	renderWindow->SetWindowName("Frustum");
	renderWindow->AddRenderer(renderer);

	// an interactor
	vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
	renderWindowInteractor->SetRenderWindow(renderWindow);

	// add the actors to the scene
	renderer->AddActor(actor);
	renderer->SetBackground(colors->GetColor3d("Silver").GetData());

	// Position the camera so that we can see the frustum
	renderer->GetActiveCamera()->SetPosition(1, 0, 0);
	renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
	renderer->GetActiveCamera()->SetViewUp(0, 1, 0);
	renderer->GetActiveCamera()->Azimuth(30);
	renderer->GetActiveCamera()->Elevation(30);
	renderer->ResetCamera();

	// render an image (lights and cameras are created automatically)
	renderWindow->Render();

	// begin mouse interaction
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

3、结果展示

PCL——VTK常见几何模型生成_第6张图片

七、几何对象

1、概述

创建并显示多个几何对象

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

int main(int, char* [])
{
    vtkNew<vtkNamedColors> colors;

    // Set the background color.
    std::array<unsigned char, 4> bkg{ {51, 77, 102, 255} };
    colors->SetColor("BkgColor", bkg.data());

    // 创建容器来保存3D对象生成器
    std::vector<vtkSmartPointer<vtkPolyDataAlgorithm>> geometricObjectSources;

    // 用要演示的各种对象源填充容器
    geometricObjectSources.push_back(vtkSmartPointer<vtkArrowSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkConeSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkCubeSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkCylinderSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkDiskSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkLineSource>::New());
    geometricObjectSources.push_back(
        vtkSmartPointer<vtkRegularPolygonSource>::New());
    geometricObjectSources.push_back(vtkSmartPointer<vtkSphereSource>::New());

    // 为每个管道的剩余节点创建容器 
    std::vector<vtkSmartPointer<vtkPolyDataMapper>> mappers;
    std::vector<vtkSmartPointer<vtkActor>> actors;
    std::vector<vtkSmartPointer<vtkTextMapper>> textmappers;
    std::vector<vtkSmartPointer<vtkActor2D>> textactors;

    // 创建一个公共文本属性
    vtkNew<vtkTextProperty> textProperty;
    textProperty->SetFontSize(16);
    textProperty->SetJustificationToCentered();
    textProperty->SetColor(colors->GetColor3d("LightGoldenrodYellow").GetData());

    // 为每个对象和相应的文本标签创建一个映射器和参与者
    for (unsigned int i = 0; i < geometricObjectSources.size(); i++)
    {
        geometricObjectSources[i]->Update();

        mappers.push_back(vtkSmartPointer<vtkPolyDataMapper>::New());
        mappers[i]->SetInputConnection(geometricObjectSources[i]->GetOutputPort());

        actors.push_back(vtkSmartPointer<vtkActor>::New());
        actors[i]->SetMapper(mappers[i]);
        actors[i]->GetProperty()->SetColor(
            colors->GetColor3d("PeachPuff").GetData());

        textmappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
        textmappers[i]->SetInput(
            geometricObjectSources[i]->GetClassName()); // set text label to the
                                                        // name of the object source
        textmappers[i]->SetTextProperty(textProperty);

        textactors.push_back(vtkSmartPointer<vtkActor2D>::New());
        textactors[i]->SetMapper(textmappers[i]);
        textactors[i]->SetPosition(120, 16); // Note: the position of an Actor2D is
                                             // specified in display coordinates
    }

    // Define size of the grid that will hold the objects
    int gridCols = 3;
    int gridRows = 3;
    // Define side length (in pixels) of each renderer square
    int rendererSize = 300;

    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetWindowName("GeometricObjectsDemo");
    renderWindow->SetSize(rendererSize * gridCols, rendererSize * gridRows);

    // Set up a grid of viewports for each renderer
    for (double row = 0; row < gridRows; row++)
    {
        for (double col = 0; col < gridCols; col++)
        {
            double index = row * gridCols + col;

            // Create a renderer for this grid cell
            vtkSmartPointer<vtkRenderer> renderer =
                vtkSmartPointer<vtkRenderer>::New();
            renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());

            // Set the renderer's viewport dimensions (xmin, ymin, xmax, ymax) within
            // the render window. Note that for the Y values, we need to subtract the
            // row index from gridRows because the viewport Y axis points upwards, but
            // we want to draw the grid from top to down
            double viewport[4] = { static_cast<double>(col) / gridCols,
                                  static_cast<double>(gridRows - row - 1) / gridRows,
                                  static_cast<double>(col + 1) / gridCols,
                                  static_cast<double>(gridRows - row) / gridRows };
            renderer->SetViewport(viewport);

            // Add the corresponding actor and label for this grid cell, if they exist
            if (index < geometricObjectSources.size())
            {
                renderer->AddActor(actors[index]);
                renderer->AddActor(textactors[index]);
                renderer->ResetCameraClippingRange();
            }

            renderWindow->AddRenderer(renderer);
        }
    }

    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    interactor->Start();

    return EXIT_SUCCESS;
}

3、结果展示

PCL——VTK常见几何模型生成_第7张图片

八、六面体

1、概述

六面体是一个主要的三维单元,由六个四边形面、12条边和8个顶点组成。这个六面体是由八个点的有序列表定义的。面和边不能与任何其他面和边相交,六面体必须是凸的。

2、代码实现

// 防止PCL可视化异常用的
#include 
#include  
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

int main(int, char* [])
{

    vtkNew<vtkNamedColors> colors;

    // 设置背景颜色
    std::array<unsigned char, 4> bkg{ {51, 77, 102, 255} };
    colors->SetColor("BkgColor", bkg.data());

    // 对于六面体; 设置8个点的坐标。 
    // 从外面看,这两个面必须是逆时针方向的。 
    std::vector<std::array<double, 3>> pointCoordinates;
    pointCoordinates.push_back({ {0.0, 0.0, 0.0} }); // Face 1
    pointCoordinates.push_back({ {1.0, 0.0, 0.0} });
    pointCoordinates.push_back({ {1.0, 1.0, 0.0} });
    pointCoordinates.push_back({ {0.0, 1.0, 0.0} });
    pointCoordinates.push_back({ {0.0, 0.0, 1.0} }); // Face 2
    pointCoordinates.push_back({ {1.0, 0.0, 1.0} });
    pointCoordinates.push_back({ {1.0, 1.0, 1.0} });
    pointCoordinates.push_back({ {0.0, 1.0, 1.0} });

    
    vtkNew<vtkPoints> points;
    vtkNew<vtkHexahedron> hex;
    // 生成点,并从这些点中创建六面体
    for (auto i = 0; i < pointCoordinates.size(); ++i)
    {
        points->InsertNextPoint(pointCoordinates[i].data());
        hex->GetPointIds()->SetId(i, i);
    }

    // 将六面体添加到元胞数组中。
    vtkNew<vtkCellArray> hexs;
    hexs->InsertNextCell(hex);

    // 将点和六面体添加到非结构化网格中。
    vtkNew<vtkUnstructuredGrid> uGrid;
    uGrid->SetPoints(points);
    uGrid->InsertNextCell(hex->GetCellType(), hex->GetPointIds());

    // 结果可视化
    vtkNew<vtkDataSetMapper> mapper;
    mapper->SetInputData(uGrid);

    vtkNew<vtkActor> actor;
    actor->GetProperty()->SetColor(colors->GetColor3d("PeachPuff").GetData());
    actor->SetMapper(mapper);

    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->SetWindowName("Hexahedron");
    renderWindow->SetSize(600, 600);
    renderWindow->AddRenderer(renderer);
    vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
    renderer->ResetCamera();
    renderer->GetActiveCamera()->Azimuth(30);
    renderer->GetActiveCamera()->Elevation(30);

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

3、结果展示

PCL——VTK常见几何模型生成_第8张图片

你可能感兴趣的:(CloudCompare,算法,计算机视觉,几何学,3d,c++)