在QVTK小部件中绘制和刷新PCL pointcloud

时间:2013-07-19 06:36:22

标签: qt vtk point-cloud-library qvtkwidget

我的问题是关于PointClouds库(PCL)和QVTK小部件。

我在MainWindow小部件中有一个QVTK小部件。每次从磁盘(或ToF摄像头)加载新的pointcloud时,都会调用绘制我的pointclouds的方法:

void CamWindow::on_PointCloudChanged()
{
      //pclPointCloud is declared as "pcl::PointCloud<pcl::PointXYZ>::Ptr pclPointCloud;". When that variable is updated, this method is called.
      //pclVisorName is global std::string, with "visor" as content
      //pclPointCloudName is global std::string, with "MyPointCloud" as content
      pcl::visualization::PCLVisualizer localPclVisor = pcl::visualization::PCLVisualizer(pclVisorName,false);
      localPclVisor.addPointCloud<pcl::PointXYZ>(pclPointCloud,pclPointCloudName);
      //localPclVisor.setBackgroundColor(1,1,0);
      vtkSmartPointer<vtkRenderWindow> localRenderWindow = localPclVisor.getRenderWindow();
      ui->QvtkPointCloud->SetRenderWindow(localRenderWindow);
      ui->QvtkPointCloud->update();

    existPclPointCloud = true;
}

这样可以,但每次调用此方法时,相机位置,pointview和其他内容都会重置。我想只改变pointcloud来查看,而不需要重置位置,颜色等。我怎样才能实现这个目标?

我尝试过这样的事情:

std::string                         pclPointCloudName;
pcl::PointCloud<pcl::PointXYZ>::Ptr pclPointCloud;
pcl::visualization::PCLVisualizer   pclVisor;
std::string                         pclVisorName;
vtkSmartPointer<vtkRenderWindow>    renderWindow;

(将pointcloud表示组件声明为全局变量)

void CamWindow::on_PointCloudChanged()
{
      //pclPointCloud is declared as "pcl::PointCloud<pcl::PointXYZ>::Ptr pclPointCloud;". When that variable is updated, this method is called.
      //pclVisorName is global std::string, with "visor" as content
      //pclPointCloudName is global std::string, with "MyPointCloud" as content
    if (existPclPointCloud)
    {
        localPclVisor.updatePointCloud<pcl::PointXYZ>(pclPointCloud,pclPointCloudName);
    }
    else
    {
      pcl::visualization::PCLVisualizer pclVisor = pcl::visualization::PCLVisualizer(pclVisorName,false);
      pclVisor.addPointCloud<pcl::PointXYZ>(pclPointCloud,pclPointCloudName);
      vtkSmartPointer<vtkRenderWindow> renderWindow = pclVisor.getRenderWindow();
      ui->QvtkPointCloud->SetRenderWindow(renderWindow);        
    }
     ui->QvtkPointCloud->update();

    existPclPointCloud = true;
}

但这不起作用......将QVTK和PCL组件声明为全局变量(在MainWindow Widget中)使该程序在QVTK小部件中不显示任何内容!为什么呢?

[我也尝试了几种变体,但看起来每个PCL + VTK + QT变量必须在方法中是本地的才能工作。花了几个小时寻找和搜索关于VTK Visualizer,QVTK方法,对象等的文档,没有运气]

我理解与PCL + VTK + QT相关的变量和对象永远不会为空或为空(因为它们在MainWindow中是全局的!!)...

我的环境是Ubuntu 12.04 LTS,Qt Creator 2.5.2,64位。 PCL 1.6,VTK 5.8

1 个答案:

答案 0 :(得分:1)

如果我理解您的实施正确,每次您想要更新点云(PC)时,都会调用CamWindow::on_PointCloudChanged()并初始化新的pcl::visualization::PCLVisualizer,这就是您丢失所有相机设置的原因。

也许这会有所帮助。在任何方法之外将可视化器声明为全局变量

pcl::visualization::PCLVisualizer pclVisor ("visor", false);

然后编写一个与此类似的init方法,您必须在开头调用一次:

void CamWindow::init() {
   vtkSmartPointer<vtkRenderWindow> renderWindow = pclVisor.getRenderWindow();
   this->SetRenderWindow(renderWindow);

   this->show();
   this->update();
}

现在,每次创建cloud0时,您都可以更新名为new_pc的电脑:

void CamWindow::on_PointCloudChanged(pcl::PointCloud<pcl::PointXYZ>::Ptr new_pc) {
   if (!pc->empty()) {

      // in case there is no pc called `cloud0` - create one,
      // otherwise just update it
      if (!pclVisor.updatePointCloud<pcl::PointXYZ>(new_pc, "cloud0")) {
         pclVisor.addPointCloud<pcl::PointXYZ>(new_pc, "cloud0");
      } else {
         pclVisor.updatePointCloud<pcl::PointXYZ>(new_pc, "cloud0");
      } 

      this->update();
   } else {
      std::cerr << "There is nothing to add to the visualizer!" << std::endl;
   }
}

我没有编译代码,所以随时编辑我的答案,万一你使用它并发现任何错误。但这应该是基本的想法。