pcl+vtk(八)vtk常用类的常用函数介绍(附带代码示例)

vtk中类的说明以及函数使用

https://vtk.org/doc/nightly/html/annotated.html

一、vtkObject派生类

1.vtkPoints 点

InsertNextPoint(double, double, double):插入点。

2.vtkCellArray 单元数组

InsertNextCell (vtkIdType npts, const vtkIdType *pts):插入单元。

3.vtkPolyData 数据集

SetRadius(double):设置球体半径,默认值为 0.5。
SetCenter(double, double, double):设置球体的中心,默认值为 0,0,0。
SetPhiResolution(int):设置纬度方向上的点数,默认值为 8,当为32为是圆滑的。
SetThetaResolution(int):设置经度方向上的点数,默认值为 8,当为32为是圆滑的。

4.vtkConeSource 锥

SetCenter(double, double, double):设置锥体的中心,默认值为 0,0,0。
SetHeight(double):设置锥体的高度,默认值为 1。
SetResolution(double):设置锥体的面数,默认值为 6,当面数足够大时即为圆锥。
SetAngle(double):设置锥体的角度。
SetRadius(double):设置锥体的半径,默认值为 0.5。
SetCapping(bool):设置锥体的底面是否显示,默认值为 1

5.vtkCylinderSource 柱

SetCenter(double, double, double):设置柱体的中心,默认值为 0,0,0。
SetHeight(double):设置柱体的高度,默认值为 1。
SetResolution(double):设置柱体的面数,默认值为 6,当面数足够大时即为圆柱。
SetAngle(double):设置柱体的角度。
SetRadius(double):设置柱体的半径,默认值为 0.5。
SetCapping(bool):设置柱体的底面是否显示,默认值为 1。

6.vtkCubeSource 立方体

方法1:根据中心点长宽高确定立方体。
SetCenter(double, double, double):设置立方体的中心,默认值为 0,0,0。
SetXLength(double):设置立方体的X轴长度。
SetYLength(double):设置立方体的Y轴宽度。
SetZLength(double):设置立方体的Z轴高度。
方法2:根据xyz边界确定一个立方体。
SetBounds(double xMin, double xMax, double yMin, double yMax, double zMin, double zMax):设置立方体的边界。

7.vtkPlaneSource 面

方法1:使用 SetOrigin() SetPoint1() 和SetPoint2()确定不在一条直线上的三个点,决定一个四边形平面
SetOrigin(double, double, double):设置起点。
SetPoint1(double, double, double):设置第一个点。
SetPoint2(double, double, double):设置第二个点。
方法2:第一,SetNormal(),指定平面的法向量。沿着法向量旋转平面。第二,SetCenter(),平移到指定的中心点。第三种方法,Push(),可以沿着平面法向量平移一定的距离。
SetCenter(double, double, double):设置中心点。
SetNormal(double, double, double):设置法向量。
Push(double):设置平移距离。

8.vtkLineSource 线

SetPoint1(double, double, double):设置线的第一个点。
SetPoint2(double, double, double):设置线的第二个点。
SetPoints(vtkPoints *):设置点集。

9.vtkDiskSource 环

SetCenter(double, double, double):设置环中心点。
SetInnerRadius(double):设置内环半径。
SetOuterRadius(double):设置外环半径。
SetCircumferentialResolution(double):设置圆周方向上的点数。

10.vtkPointSource 散点

在指定中心点周围的指定半径内创建用户指定数量的点。默认情况下,点在球体内的位置是随机的。
SetRadius(double):设置半径。
SetCenter(double, double, double):设置中心。

11.vtkTextSource 文本

SetText(const char *):设置文本。
SetForegroundColor(double, double, double):设置文本颜色。
SetBackgroundColor(double, double, double):设置背景颜色。
SetBacking(bool):设置是否显示背景。

12.vtkArrowSource 箭头(测试着看总长度为1)

SetTipLength(double):设置尖端长度,测试看取值范围为0~1。
SetTipRadius(double):设置尖端半径。
SetTipResolution(double):设置尖端面数。
SetShaftRadius(double):设置轴半径。
SetShaftResolution(double):设置轴面数。
SetInvert(bool):设置反转箭头。

二、vtkDataObject派生类

1.vtkPolyData 数据集

SetPoints(vtkPoints*):设置点集。

SetVerts(vtkCellArray* v):设置定义顶点的单元阵列。

三、vtkPolyDataMapper 映射

SetInputData(vtkPolyData *in):设置数据集。

SetInputConnection(vtkAlgorithmOutput* input):设置给定输入端口索引的连接。

四、vtkActor 演员

SetMapper(vtkMapper *):设置添加映射器。

GetProperty():获取属性。

SetPosition(double, double, double):设置位置。

VisibilityOff():设置不可见。

VisibilityOn():设置可见。

RotateX(double):沿x轴旋转角度。

RotateY(double):沿y轴旋转角度。

RotateZ(double):沿z轴旋转角度。

五、vtkRenderer 渲染器

AddActor(vtkProp *p):添加演员。

六、vtkGenericOpenGLRenderWindow 渲染窗口

AddRenderer(vtkRenderer *):添加渲染器

七、代码示例

具体有关库的引用,main.cpp的修改,以及QVTKOpenGLNativeWidget控件的添加请看前面详细介绍的文章,下面只放了主要代码。

头文件引用

#include "vtkAutoInit.h"   // vtk初始化的方式
VTK_MODULE_INIT(vtkRenderingOpenGL2);   // 渲染
VTK_MODULE_INIT(vtkInteractionStyle);   // 相互做用方式
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);  //
VTK_MODULE_INIT(vtkRenderingFreeType);
#include <vtkSmartPointer.h>
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkCubeSource.h>
#include <vtkSphereSource.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkConeSource.h>
#include <vtkCylinderSource.h>
#include <vtkCubeSource.h>
#include <vtkPlaneSource.h>
#include <vtkLineSource.h>
#include <vtkTextSource.h>
#include <vtkDiskSource.h>
#include <vtkPointSource.h>
#include <vtkArrowSource.h>

主要显示代码

    //显示
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();//在构造函数中进行初始化
    vtkNew<vtkGenericOpenGLRenderWindow> renwindow;
    renwindow->AddRenderer(renderer);
    ui->vtk_widget->SetRenderWindow(renwindow.Get());

    //点
    vtkSmartPointer<vtkPoints> points1 = vtkSmartPointer<vtkPoints>::New();
    vtkSmartPointer<vtkCellArray> vertices1  = vtkSmartPointer<vtkCellArray>::New();
    for (int i = 0; i<20; i++)
    {
        vtkIdType pid[1];
        pid[0] =  points1->InsertNextPoint(i, 0, 0);
        vertices1->InsertNextCell(1, pid);
    }
    vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
    polyData->SetPoints(points1);
    polyData->SetVerts(vertices1);
    vtkSmartPointer<vtkPolyDataMapper> mapper_point = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_point->SetInputData(polyData);
    vtkSmartPointer<vtkActor> actor_point = vtkSmartPointer<vtkActor>::New();
    actor_point->SetMapper(mapper_point);
    actor_point->GetProperty()->SetPointSize(3);
    actor_point->GetProperty()->SetColor(1,0,0);
    renderer->AddActor(actor_point);

    //球
    vtkSmartPointer<vtkSphereSource> Sphere = vtkSmartPointer<vtkSphereSource>::New();
    Sphere->SetCenter(0.5,0,0);
    Sphere->SetRadius(0.5);
    Sphere->SetPhiResolution(32);
    Sphere->SetThetaResolution(32);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Sphere = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Sphere->SetInputConnection(Sphere->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Sphere = vtkSmartPointer<vtkActor>::New();
    actor_Sphere->SetMapper(mapper_Sphere);
    renderer->AddActor(actor_Sphere);

    //柱
    vtkSmartPointer<vtkCylinderSource> Cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    Cylinder->SetCenter(1.5,0,0);
    Cylinder->SetHeight(1);
    Cylinder->SetResolution(8);
    Cylinder->SetRadius(0.5);
    Cylinder->SetCapping(1);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Cylinder = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Cylinder->SetInputConnection(Cylinder->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Cylinder = vtkSmartPointer<vtkActor>::New();
    actor_Cylinder->SetMapper(mapper_Cylinder);
    renderer->AddActor(actor_Cylinder);

    //锥
    vtkSmartPointer<vtkConeSource> Cone = vtkSmartPointer<vtkConeSource>::New();
    Cone->SetCenter(2.5,0,0);
    Cone->SetHeight(1);
    Cone->SetResolution(4);
    Cone->SetAngle(45);
    Cone->SetRadius(1);
    Cone->SetCapping(1);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Cone = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Cone->SetInputConnection(Cone->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Cone = vtkSmartPointer<vtkActor>::New();
    actor_Cone->SetMapper(mapper_Cone);
    renderer->AddActor(actor_Cone);

    //立方体
    vtkSmartPointer<vtkCubeSource> Cube = vtkSmartPointer<vtkCubeSource>::New();
    //Cube->SetCenter(3.5,0,0);
    //Cube->SetXLength(1);
    //Cube->SetYLength(2);
    //Cube->SetZLength(2);
    Cube->SetBounds(3,4,-0.5,0.5,-0.5,0.5);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Cube = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Cube->SetInputConnection(Cube->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Cube = vtkSmartPointer<vtkActor>::New();
    actor_Cube->SetMapper(mapper_Cube);
    renderer->AddActor(actor_Cube);

    //面
    vtkSmartPointer<vtkPlaneSource> Plane = vtkSmartPointer<vtkPlaneSource>::New();
    Plane->SetOrigin(5,0,-0.5);
    Plane->SetPoint1(5,0,0.5);
    Plane->SetPoint2(4,0,-0.5);
//    Plane->SetCenter(1,0,0);
//    Plane->SetNormal(2,0,0);
//    Plane->Push(4);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Plane = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Plane->SetInputConnection(Plane->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Plane = vtkSmartPointer<vtkActor>::New();
    actor_Plane->SetMapper(mapper_Plane);
    renderer->AddActor(actor_Plane);

    //线
    vtkSmartPointer<vtkLineSource> Line = vtkSmartPointer<vtkLineSource>::New();
//    Line->SetPoint1(6,0,0.5);
//    Line->SetPoint2(5,0,-0.5);
    vtkSmartPointer<vtkPoints> points_Line = vtkSmartPointer<vtkPoints>::New();
    for (int i = 0; i<3; i++)
    {
        points_Line->InsertNextPoint(5 + i/2, 0, 0);
    }
    Line->SetPoints(points_Line);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Line = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Line->SetInputConnection(Line->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Line = vtkSmartPointer<vtkActor>::New();
    actor_Line->SetMapper(mapper_Line);
    renderer->AddActor(actor_Line);

    //环
    vtkSmartPointer<vtkDiskSource> Disk = vtkSmartPointer<vtkDiskSource>::New();
    Disk->SetInnerRadius(0.25);
    Disk->SetOuterRadius(0.5);
    Disk->SetCircumferentialResolution(100);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Disk = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Disk->SetInputConnection(Disk->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Disk = vtkSmartPointer<vtkActor>::New();
    actor_Disk->SetPosition(6.5,0,0);
    actor_Disk->SetMapper(mapper_Disk);
    renderer->AddActor(actor_Disk);

    //散点
    vtkSmartPointer<vtkPointSource> Point = vtkSmartPointer<vtkPointSource>::New();
    Point->SetCenter(7.5,0,0);
    Point->SetRadius(0.5);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Point = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Point->SetInputConnection(Point->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Point = vtkSmartPointer<vtkActor>::New();
    actor_Point->SetMapper(mapper_Point);
    renderer->AddActor(actor_Point);

//    //文本
//    vtkSmartPointer<vtkTextSource> Text = vtkSmartPointer<vtkTextSource>::New();
//    Text->SetText("test");
//    Text->SetForegroundColor(0,1,0);
//    Text->SetBackgroundColor(0,0,1);
//    Text->SetBacking(0);
//    vtkSmartPointer<vtkPolyDataMapper> mapper_Text = vtkSmartPointer<vtkPolyDataMapper>::New();
//    mapper_Text->SetInputConnection(Text->GetOutputPort());

//    vtkSmartPointer<vtkActor> actor_Text = vtkSmartPointer<vtkActor>::New();
//    actor_Text->SetMapper(mapper_Text);
//    actor_Text->SetPosition(5.5,0,0);
//    renderer->AddActor(actor_Text);

    //箭头
    vtkSmartPointer<vtkArrowSource> Arrow = vtkSmartPointer<vtkArrowSource>::New();
    Arrow->SetTipLength(0.8);
    Arrow->SetTipRadius(0.15);
    Arrow->SetTipResolution(5);
    Arrow->SetShaftRadius(0.05);
    Arrow->SetShaftResolution(100);
    //Arrow->SetInvert(1);
    vtkSmartPointer<vtkPolyDataMapper> mapper_Arrow = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper_Arrow->SetInputConnection(Arrow->GetOutputPort());

    vtkSmartPointer<vtkActor> actor_Arrow = vtkSmartPointer<vtkActor>::New();
    actor_Arrow->SetPosition(8,0,0);
    actor_Arrow->GetProperty()->SetPointSize(2);
    actor_Arrow->SetMapper(mapper_Arrow);
    renderer->AddActor(actor_Arrow);

效果展示

猜你喜欢

转载自blog.csdn.net/m0_67254672/article/details/133950136