当前位置:   article > 正文

Qt 如何使用VTK显示点云_qt vtk显示点云

qt vtk显示点云

开发环境

        ubuntu 20.04

        VTK 8.2

编译VTK

下载源码

        git clone --recursive https://gitlab.kitware.com/vtk/vtk.git

        使用版本管理工具,切换版本到8.2

更改编译选项,这里使用cmake-gui进行配置

1、编译类型修改为Release

2、安装路径可以设置,也可以不设置,不设置默认安装到/usr/local目录下

3、勾选VTK_Group_Qt

4、勾选Module_vtkGUISupportQtOpenGL

生成成功后,执行编译

cmake ..

make -j4

make install

 开发

CMakeLists 

  1. find_package(VTK 8.2 COMPONENTS vtkGUISupportQt
  2. vtkGUISupportQtOpenGL
  3. vtkRenderingGL2PSOpenGL2
  4. vtkRenderingVolumeOpenGL2 REQUIRED)
  5. complice_module(${PROJECT_NAME})
  6. target_link_libraries( ${PROJECT_NAME} PRIVATE ${VTK_LIBRARIES}

.h

  1. #include <QVTKOpenGLNativeWidget.h>
  2. #include <pcl/visualization/pcl_visualizer.h>
  3. #include <vtkGenericOpenGLRenderWindow.h>
  4. #include <vtkOrientationMarkerWidget.h>
  5. class CloudView : public QVTKOpenGLNativeWidget
  6. {
  7. Q_OBJECT
  8. public:
  9. explicit CloudView(QWidget* parent = nullptr);
  10. private:
  11. pcl::visualization::PCLVisualizer::Ptr m_viewer;
  12. vtkSmartPointer<vtkRenderer> m_render;
  13. vtkSmartPointer<vtkGenericOpenGLRenderWindow> m_renderwindow;
  14. vtkSmartPointer<vtkOrientationMarkerWidget> m_axes;
  15. };

.cpp

  1. #include "cloudview.h"
  2. #include <vtkAutoInit.h>
  3. VTK_MODULE_INIT(vtkRenderingOpenGL2)
  4. VTK_MODULE_INIT(vtkInteractionStyle)
  5. VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
  6. VTK_MODULE_INIT(vtkRenderingFreeType)
  7. #include <vtkAxesActor.h>
  8. CloudView::CloudView(QWidget* parent)
  9. : QVTKOpenGLNativeWidget(parent),
  10. m_render(vtkSmartPointer<vtkRenderer>::New()),
  11. m_renderwindow(vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New()),
  12. m_axes(vtkSmartPointer<vtkOrientationMarkerWidget>::New())
  13. {
  14. m_renderwindow->AddRenderer(m_render);
  15. m_viewer.reset(new pcl::visualization::PCLVisualizer(m_render, m_renderwindow, "viewer", false));
  16. m_viewer->setupInteractor(this->GetInteractor(), this->GetRenderWindow());
  17. this->SetRenderWindow(m_viewer->getRenderWindow());
  18. m_viewer->setBackgroundColor((double)150.0 / 255.0, (double)150.0 / 255.0, (double)150.0 / 255.0);
  19. vtkSmartPointer<vtkAxesActor> actor = vtkSmartPointer<vtkAxesActor>::New();
  20. m_axes->SetOutlineColor(0.9300, 0.5700, 0.1300);
  21. m_axes->SetOrientationMarker(actor);
  22. m_axes->SetInteractor(m_viewer->getRenderWindow()->GetInteractor());
  23. m_axes->SetViewport(0.9, 0, 1, 0.15);
  24. m_axes->SetEnabled(true);
  25. m_axes->InteractiveOn();
  26. m_axes->InteractiveOff();
  27. m_viewer->getRenderWindow()->Render();
  28. }
  29. void CloudView::addPointCloud(const Cloud::Ptr& cloud)
  30. {
  31. if (!m_viewer->contains(cloud->id().toStdString()))
  32. m_viewer->addPointCloud<PointXYZRGBN>(cloud, cloud->id().toStdString());
  33. else
  34. {
  35. pcl::visualization::PointCloudColorHandlerRGBField<PointXYZRGBN> rgb(cloud);
  36. m_viewer->updatePointCloud<PointXYZRGBN>(cloud, rgb, cloud->id().toStdString());
  37. }
  38. if (cloud->pointSize() != 1)
  39. m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,
  40. cloud->pointSize(), cloud->id().toStdString());
  41. if (cloud->opacity() != 1)
  42. m_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
  43. cloud->opacity(), cloud->id().toStdString());
  44. m_viewer->getRenderWindow()->Render();
  45. }

ui上拖拽一个openGl控件, 然后提升为CloudView,然后就可以显示点云数据了

  1. PointCloud::Ptr pointPtr;
  2. ui->cloudview->addPointCloud( cloud);
  3. ui->cloudview->resetCamera();

显示效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/535487
推荐阅读
相关标签
  

闽ICP备14008679号