#include
当前位置:   article > 正文

QT + vtk8.0显示点云数据_qt+vtk+pcl实现点云可视化

qt+vtk+pcl实现点云可视化

1、创建Widget项目;

2、在main.cpp中添加代码:

  1. #include <vtkAutoInit.h>
  2. VTK_MODULE_INIT(vtkRenderingOpenGL);
  3. VTK_MODULE_INIT(vtkInteractionStyle);
3、Widget.cpp代码如下:
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <vtkSmartPointer.h>
  4. #include <vtkSimplePointsReader.h>
  5. #include <vtkPolyDataMapper.h>
  6. #include <vtkActor.h>
  7. #include <vtkProperty.h>
  8. #include <vtkRenderer.h>
  9. #include <vtkRenderWindow.h>
  10. #include <vtkRenderWindowInteractor.h>
  11. #include <vtkVersion.h>
  12. #include <vtkSmartPointer.h>
  13. #include <vtkPoints.h>
  14. #include <vtkVertexGlyphFilter.h>
  15. #include <vtkPolyDataMapper.h>
  16. #include <vtkActor.h>
  17. #include <vtkRenderer.h>
  18. #include <vtkRenderWindow.h>
  19. #include <vtkRenderWindowInteractor.h>
  20. #include <sstream>
  21. Widget::Widget(QWidget *parent) :
  22. QWidget(parent),
  23. ui(new Ui::Widget)
  24. {
  25. ui->setupUi(this);
  26. std::string filename = "test6.txt";
  27. std::ifstream filestream(filename.c_str());
  28. std::string line;
  29. vtkSmartPointer<vtkPoints> points =
  30. vtkSmartPointer<vtkPoints>::New();
  31. while (std::getline(filestream, line))
  32. {
  33. double x, y, z;
  34. std::stringstream linestream;
  35. linestream << line;
  36. linestream >> x >> y >> z;
  37. printf("x = %f\n", x);
  38. printf("y = %f\n", y);
  39. printf("z = %f\n", z);
  40. points->InsertNextPoint(x, y, z);
  41. }
  42. filestream.close();
  43. vtkSmartPointer<vtkPolyData> polyData =
  44. vtkSmartPointer<vtkPolyData>::New();
  45. polyData->SetPoints(points);
  46. vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
  47. vtkSmartPointer<vtkVertexGlyphFilter>::New();
  48. #if VTK_MAJOR_VERSION <= 5
  49. glyphFilter->SetInputConnection(polyData->GetProducerPort());
  50. #else
  51. glyphFilter->SetInputData(polyData);
  52. #endif
  53. glyphFilter->Update();
  54. vtkSmartPointer<vtkPolyDataMapper> mapper =
  55. vtkSmartPointer<vtkPolyDataMapper>::New();
  56. mapper->SetInputConnection(glyphFilter->GetOutputPort());
  57. vtkSmartPointer<vtkActor> actor =
  58. vtkSmartPointer<vtkActor>::New();
  59. actor->SetMapper(mapper);
  60. vtkSmartPointer<vtkRenderer> renderer =
  61. vtkSmartPointer<vtkRenderer>::New();
  62. renderer->AddActor(actor);
  63. renderer->SetBackground(.1, .1, .1);
  64. ui->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
  65. }
  66. Widget::~Widget()
  67. {
  68. delete ui;
  69. }

4、显示效果如下:

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签