• (八)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控件的添加请看前面详细介绍的文章,下面只放了主要代码。

    头文件引用

    1. #include "vtkAutoInit.h" // vtk初始化的方式
    2. VTK_MODULE_INIT(vtkRenderingOpenGL2); // 渲染
    3. VTK_MODULE_INIT(vtkInteractionStyle); // 相互做用方式
    4. VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); //
    5. VTK_MODULE_INIT(vtkRenderingFreeType);
    6. #include
    7. #include "vtkActor.h"
    8. #include "vtkPolyDataMapper.h"
    9. #include "vtkRenderWindow.h"
    10. #include "vtkRenderer.h"
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include
    22. #include
    23. #include
    24. #include

    主要显示代码

    1. //显示
    2. vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();//在构造函数中进行初始化
    3. vtkNew<vtkGenericOpenGLRenderWindow> renwindow;
    4. renwindow->AddRenderer(renderer);
    5. ui->vtk_widget->SetRenderWindow(renwindow.Get());
    6. //
    7. vtkSmartPointer<vtkPoints> points1 = vtkSmartPointer<vtkPoints>::New();
    8. vtkSmartPointer<vtkCellArray> vertices1 = vtkSmartPointer<vtkCellArray>::New();
    9. for (int i = 0; i<20; i++)
    10. {
    11. vtkIdType pid[1];
    12. pid[0] = points1->InsertNextPoint(i, 0, 0);
    13. vertices1->InsertNextCell(1, pid);
    14. }
    15. vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
    16. polyData->SetPoints(points1);
    17. polyData->SetVerts(vertices1);
    18. vtkSmartPointer<vtkPolyDataMapper> mapper_point = vtkSmartPointer<vtkPolyDataMapper>::New();
    19. mapper_point->SetInputData(polyData);
    20. vtkSmartPointer<vtkActor> actor_point = vtkSmartPointer<vtkActor>::New();
    21. actor_point->SetMapper(mapper_point);
    22. actor_point->GetProperty()->SetPointSize(3);
    23. actor_point->GetProperty()->SetColor(1,0,0);
    24. renderer->AddActor(actor_point);
    25. //
    26. vtkSmartPointer<vtkSphereSource> Sphere = vtkSmartPointer<vtkSphereSource>::New();
    27. Sphere->SetCenter(0.5,0,0);
    28. Sphere->SetRadius(0.5);
    29. Sphere->SetPhiResolution(32);
    30. Sphere->SetThetaResolution(32);
    31. vtkSmartPointer<vtkPolyDataMapper> mapper_Sphere = vtkSmartPointer<vtkPolyDataMapper>::New();
    32. mapper_Sphere->SetInputConnection(Sphere->GetOutputPort());
    33. vtkSmartPointer<vtkActor> actor_Sphere = vtkSmartPointer<vtkActor>::New();
    34. actor_Sphere->SetMapper(mapper_Sphere);
    35. renderer->AddActor(actor_Sphere);
    36. //
    37. vtkSmartPointer<vtkCylinderSource> Cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    38. Cylinder->SetCenter(1.5,0,0);
    39. Cylinder->SetHeight(1);
    40. Cylinder->SetResolution(8);
    41. Cylinder->SetRadius(0.5);
    42. Cylinder->SetCapping(1);
    43. vtkSmartPointer<vtkPolyDataMapper> mapper_Cylinder = vtkSmartPointer<vtkPolyDataMapper>::New();
    44. mapper_Cylinder->SetInputConnection(Cylinder->GetOutputPort());
    45. vtkSmartPointer<vtkActor> actor_Cylinder = vtkSmartPointer<vtkActor>::New();
    46. actor_Cylinder->SetMapper(mapper_Cylinder);
    47. renderer->AddActor(actor_Cylinder);
    48. //
    49. vtkSmartPointer<vtkConeSource> Cone = vtkSmartPointer<vtkConeSource>::New();
    50. Cone->SetCenter(2.5,0,0);
    51. Cone->SetHeight(1);
    52. Cone->SetResolution(4);
    53. Cone->SetAngle(45);
    54. Cone->SetRadius(1);
    55. Cone->SetCapping(1);
    56. vtkSmartPointer<vtkPolyDataMapper> mapper_Cone = vtkSmartPointer<vtkPolyDataMapper>::New();
    57. mapper_Cone->SetInputConnection(Cone->GetOutputPort());
    58. vtkSmartPointer<vtkActor> actor_Cone = vtkSmartPointer<vtkActor>::New();
    59. actor_Cone->SetMapper(mapper_Cone);
    60. renderer->AddActor(actor_Cone);
    61. //立方体
    62. vtkSmartPointer<vtkCubeSource> Cube = vtkSmartPointer<vtkCubeSource>::New();
    63. //Cube->SetCenter(3.5,0,0);
    64. //Cube->SetXLength(1);
    65. //Cube->SetYLength(2);
    66. //Cube->SetZLength(2);
    67. Cube->SetBounds(3,4,-0.5,0.5,-0.5,0.5);
    68. vtkSmartPointer<vtkPolyDataMapper> mapper_Cube = vtkSmartPointer<vtkPolyDataMapper>::New();
    69. mapper_Cube->SetInputConnection(Cube->GetOutputPort());
    70. vtkSmartPointer<vtkActor> actor_Cube = vtkSmartPointer<vtkActor>::New();
    71. actor_Cube->SetMapper(mapper_Cube);
    72. renderer->AddActor(actor_Cube);
    73. //
    74. vtkSmartPointer<vtkPlaneSource> Plane = vtkSmartPointer<vtkPlaneSource>::New();
    75. Plane->SetOrigin(5,0,-0.5);
    76. Plane->SetPoint1(5,0,0.5);
    77. Plane->SetPoint2(4,0,-0.5);
    78. // Plane->SetCenter(1,0,0);
    79. // Plane->SetNormal(2,0,0);
    80. // Plane->Push(4);
    81. vtkSmartPointer<vtkPolyDataMapper> mapper_Plane = vtkSmartPointer<vtkPolyDataMapper>::New();
    82. mapper_Plane->SetInputConnection(Plane->GetOutputPort());
    83. vtkSmartPointer<vtkActor> actor_Plane = vtkSmartPointer<vtkActor>::New();
    84. actor_Plane->SetMapper(mapper_Plane);
    85. renderer->AddActor(actor_Plane);
    86. //线
    87. vtkSmartPointer<vtkLineSource> Line = vtkSmartPointer<vtkLineSource>::New();
    88. // Line->SetPoint1(6,0,0.5);
    89. // Line->SetPoint2(5,0,-0.5);
    90. vtkSmartPointer<vtkPoints> points_Line = vtkSmartPointer<vtkPoints>::New();
    91. for (int i = 0; i<3; i++)
    92. {
    93. points_Line->InsertNextPoint(5 + i/2, 0, 0);
    94. }
    95. Line->SetPoints(points_Line);
    96. vtkSmartPointer<vtkPolyDataMapper> mapper_Line = vtkSmartPointer<vtkPolyDataMapper>::New();
    97. mapper_Line->SetInputConnection(Line->GetOutputPort());
    98. vtkSmartPointer<vtkActor> actor_Line = vtkSmartPointer<vtkActor>::New();
    99. actor_Line->SetMapper(mapper_Line);
    100. renderer->AddActor(actor_Line);
    101. //
    102. vtkSmartPointer<vtkDiskSource> Disk = vtkSmartPointer<vtkDiskSource>::New();
    103. Disk->SetInnerRadius(0.25);
    104. Disk->SetOuterRadius(0.5);
    105. Disk->SetCircumferentialResolution(100);
    106. vtkSmartPointer<vtkPolyDataMapper> mapper_Disk = vtkSmartPointer<vtkPolyDataMapper>::New();
    107. mapper_Disk->SetInputConnection(Disk->GetOutputPort());
    108. vtkSmartPointer<vtkActor> actor_Disk = vtkSmartPointer<vtkActor>::New();
    109. actor_Disk->SetPosition(6.5,0,0);
    110. actor_Disk->SetMapper(mapper_Disk);
    111. renderer->AddActor(actor_Disk);
    112. //散点
    113. vtkSmartPointer<vtkPointSource> Point = vtkSmartPointer<vtkPointSource>::New();
    114. Point->SetCenter(7.5,0,0);
    115. Point->SetRadius(0.5);
    116. vtkSmartPointer<vtkPolyDataMapper> mapper_Point = vtkSmartPointer<vtkPolyDataMapper>::New();
    117. mapper_Point->SetInputConnection(Point->GetOutputPort());
    118. vtkSmartPointer<vtkActor> actor_Point = vtkSmartPointer<vtkActor>::New();
    119. actor_Point->SetMapper(mapper_Point);
    120. renderer->AddActor(actor_Point);
    121. // //文本
    122. // vtkSmartPointer<vtkTextSource> Text = vtkSmartPointer<vtkTextSource>::New();
    123. // Text->SetText("test");
    124. // Text->SetForegroundColor(0,1,0);
    125. // Text->SetBackgroundColor(0,0,1);
    126. // Text->SetBacking(0);
    127. // vtkSmartPointer<vtkPolyDataMapper> mapper_Text = vtkSmartPointer<vtkPolyDataMapper>::New();
    128. // mapper_Text->SetInputConnection(Text->GetOutputPort());
    129. // vtkSmartPointer<vtkActor> actor_Text = vtkSmartPointer<vtkActor>::New();
    130. // actor_Text->SetMapper(mapper_Text);
    131. // actor_Text->SetPosition(5.5,0,0);
    132. // renderer->AddActor(actor_Text);
    133. //箭头
    134. vtkSmartPointer<vtkArrowSource> Arrow = vtkSmartPointer<vtkArrowSource>::New();
    135. Arrow->SetTipLength(0.8);
    136. Arrow->SetTipRadius(0.15);
    137. Arrow->SetTipResolution(5);
    138. Arrow->SetShaftRadius(0.05);
    139. Arrow->SetShaftResolution(100);
    140. //Arrow->SetInvert(1);
    141. vtkSmartPointer<vtkPolyDataMapper> mapper_Arrow = vtkSmartPointer<vtkPolyDataMapper>::New();
    142. mapper_Arrow->SetInputConnection(Arrow->GetOutputPort());
    143. vtkSmartPointer<vtkActor> actor_Arrow = vtkSmartPointer<vtkActor>::New();
    144. actor_Arrow->SetPosition(8,0,0);
    145. actor_Arrow->GetProperty()->SetPointSize(2);
    146. actor_Arrow->SetMapper(mapper_Arrow);
    147. renderer->AddActor(actor_Arrow);

    效果展示

  • 相关阅读:
    Arduino框架下ESP32使用固件自带的SD库的总结
    git学习笔记
    L2-031 深入虎穴
    【百元级商用RV1106 Linux开发板只需69元】Luckfox Pico Pro/Luckfox Pico Max 39元版本大升级版本
    如何正确的写出第一个java程序:hello java
    用Java实现贪吃蛇小游戏
    GIS工具maptalks开发手册(二)03——渲染面
    基于单片机的太阳能灯(热释电)电路设计(#0222)
    ESP8266--SDK开发(系统任务)
    【GlobalMapper精品教程】020:Lidar点云数据分类(自动分类、手动分类)案例详解
  • 原文地址:https://blog.csdn.net/m0_67254672/article/details/133950136