PCL 使用 VTK 和 QVTKOpenGLStereoWidget 的 QtCreator 小部件内的查看器

PCL viewer inside QtCreator widget with VTK and QVTKOpenGLStereoWidget

我需要使用 Qt 小部件制作图形 window,允许在其中表示我之前使用 PLC 库加载的点云。

这是我目前所掌握的不起作用(我基于教程和这个answer)。

我正在使用:

我使用 QVTKOpenGLStereoWidget 的原因是,据我所知,QVTKOpenGLWidget 和 QVTKWidget 都不再可用或将在未来的版本中停产。

Test.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++14

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    MainWindow.cpp

HEADERS += \
    MainWindow.h

FORMS += \
    MainWindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

# INCLUIR LAS LIREARÍAS PARA EL PRROGRAMA
VTK_VERSION = 9.1
PCL_VERSION = 1.12
CONFIG += link_pkgconfig

# LIBREARÍA DE ARCHIVOS DE EIGEN3
PKGCONFIG += eigen3

# LIBRERÍA DE ARCHIIVOS DE BOOST
INCLUDEPATH += /usr/include/boost

# LIBREARÍA DE REPRESENTACIÓN GRÁFICA VTK
INCLUDEPATH += /usr/local/include/vtk-$${VTK_VERSION}
LIBS += -L/usr/local/lib                            \
    -lvtkCommonColor-$${VTK_VERSION}                \
    -lvtkCommonCore-$${VTK_VERSION}                 \
    -lvtkFiltersSources-$${VTK_VERSION}             \
    -lvtkInteractionStyle-$${VTK_VERSION}           \
    -lvtkRenderingContextOpenGL2-$${VTK_VERSION}    \
    -lvtkRenderingCore-$${VTK_VERSION}              \
    -lvtkRenderingFreeType-$${VTK_VERSION}          \
    -lvtkRenderingGL2PSOpenGL2-$${VTK_VERSION}      \
    -lvtkRenderingOpenGL2-$${VTK_VERSION}           \
    -lvtkCommonExecutionModel-$${VTK_VERSION}       \
    -lvtkRenderingFreeType-$${VTK_VERSION}          \
    -lvtkInteractionStyle-$${VTK_VERSION}           \
    -lvtkRenderingOpenGL2-$${VTK_VERSION}           \
    -lvtkRenderingLOD-$${VTK_VERSION}               \
    -lvtkCommonDataModel-$${VTK_VERSION}            \
    -lvtkCommonMath-$${VTK_VERSION}                 \
    -lvtkViewsQt-$${VTK_VERSION}                    \
    #-lvtkGUISupportQtOpenGL-$${VTK_VERSION}         \
    -lvtkGUISupportQt-$${VTK_VERSION}

# LIBRERÍAS PARA EL TRBAJO CON NUBES DE PUNTOS.
INCLUDEPATH += /usr/local/include/pcl-$${PCL_VERSION}
LIBS += -L/usr/local/lib                                            \
    -lpcl_common -lpcl_features -lpcl_filters -lpcl_io_ply          \
    -lpcl_io -lpcl_kdtree -lpcl_keypoints -lpcl_ml                  \
    -lpcl_octree -lpcl_outofcore -lpcl_people -lpcl_recognition     \
    -lpcl_registration -lpcl_search -lpcl_segmentation -lpcl_stereo \
    -lpcl_surface -lpcl_tracking  -lpcl_visualization -lpcl_sample_consensus

MainWindow.ui

它只有一个 小部件 有一个 QVTKOpenGLSTereoWidget

MainWindow.h

我初始化了 VTK 库,包含了不同的库头文件,并创建了 PCL 查看器和基于 OpenGL 的 VTK 渲染器。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "pcl/visualization/cloud_viewer.h"
#include <pcl/visualization/pcl_visualizer.h>
#include "pcl/io/pcd_io.h"
#include "pcl/io/ply_io.h"
#include "pcl/io/obj_io.h"

#include "pcl/common/io.h"

#include "vtkCylinder.h"
#include "vtkCylinderSource.h"
#include "vtkRenderWindow.h"
#include <QVTKOpenGLWidget.h>
#include <QVTKOpenGLStereoWidget.h>
#include <QVTKOpenGLWindow.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);

using namespace pcl::visualization;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    pcl::visualization::PCLVisualizer::Ptr viewer;
    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow;

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

MainWindow.cpp

最后在 MainWindow 的构造函数中 class 我只是加载了文件(在本例中它是一个 PLY)然后我尝试在小部件中制作图形表示

#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
    // Cargar el objeto en dicha nube de putnos en el formato que se desee.
    pcl::io::loadPLYFile("/home/alejandro/Documentos/alejandro.ply",*cloud);
    viewer->addPointCloud(cloud);

    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    //renderWindow->AddRenderer(renderer); THIS MAKES AN ERROR!!
    viewer.reset(new PCLVisualizer(renderer, renderWindow, "viewer", false));
    ui->widget->setRenderWindow(viewer->getRenderWindow());
    ui->widget->update();

}
MainWindow::~MainWindow()
{
    delete ui;
}

Main.cpp

我什么都没改,都是默认的

#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

错误。

我遇到的主要错误是:/usr/local/include/vtk-9.1/vtkSmartPointer.h:225: 错误:嵌套名称说明符中使用的不完整类型‘vtkGenericOpenGLRenderWindow’ 在 /usr/local/include/pcl-1.12/pcl/visualization/point_cloud_geometry_handlers.h:49 包含的文件中, 来自 /usr/local/include/pcl-1.12/pcl/visualization/common/actor_map.h:40, 来自 /usr/local/include/pcl-1.12/pcl/visualization/pcl_visualizer.h:48, 来自 /usr/local/include/pcl-1.12/pcl/visualization/cloud_viewer.h:39, 来自../04_pcl_viewer_V3/MainWindow.h:6, 来自../04_pcl_viewer_V3/MainWindow.cpp:1: /usr/local/include/vtk-9.1/vtkSmartPointer.h:在“static vtkSmartPointer vtkSmartPointer::New() [with T = vtkGenericOpenGLRenderWindow]”的实例化中: ../04_pcl_viewer_V3/MainWindow.cpp:16:67: 从这里需要 /usr/local/include/vtk-9.1/vtkSmartPointer.h:225:69: 错误:嵌套名称说明符中使用了不完整类型“vtkGenericOpenGLRenderWindow” 225 |静态 vtkSmartPointer New() { return vtkSmartPointer(T::New(), NoReference()); } | ~~~~~~^~

我终于找到了问题的解决方案,所以我将其作为答案分享,以防它对其他人有用。

pclTest_V0.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++14

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    MainWindow.cpp

HEADERS += \
    MainWindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

# Incluir la librería y la ruta de VTK
VTK_VERSION = 9.1
INCLUDEPATH += /usr/local/include/vtk-$${VTK_VERSION}
LIBS += -L/usr/local/lib    \
    -lvtkCommonColor-$${VTK_VERSION}    \
    -lvtkCommonExecutionModel-$${VTK_VERSION}    \
    -lvtkCommonCore-$${VTK_VERSION}     \
    -lvtkCommonDataModel-$${VTK_VERSION}     \  # Para PCL
    -lvtkCommonMath-$${VTK_VERSION}     \       # Para PCL
    -lvtkFiltersCore-$${VTK_VERSION} \
    -lvtkFiltersSources-$${VTK_VERSION} \
    -lvtkInfovisCore-$${VTK_VERSION} \
    -lvtkInteractionStyle-$${VTK_VERSION} \
    -lvtkRenderingContextOpenGL2-$${VTK_VERSION} \
    -lvtkRenderingCore-$${VTK_VERSION} \
    -lvtkRenderingFreeType-$${VTK_VERSION} \
    -lvtkRenderingGL2PSOpenGL2-$${VTK_VERSION} \
    -lvtkRenderingOpenGL2-$${VTK_VERSION} \
    -lvtkViewsQt-$${VTK_VERSION} \
    -lvtkGUISupportQt-$${VTK_VERSION} \
    -lvtkRenderingQt-$${VTK_VERSION}

# Incluir el directorio de boost.
INCLUDEPATH += /usr/include/boost

# Incluir el direcotrio de eigen3.
INCLUDEPATH += /usr/include/eigen3

# Incluir las librerías y la ruta de PCL.
PCL_VERSION = 1.12
INCLUDEPATH += /usr/local/include/pcl-$${PCL_VERSION}
LIBS += -L/usr/local/lib \
    -lpcl_common -lpcl_io -lpcl_visualization

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "pcl/common/common_headers.h"
#include "pcl/features/normal_3d.h"
#include "pcl/io/obj_io.h"
#include "pcl/io/ply_io.h"
#include "pcl/visualization/pcl_visualizer.h"
#include "pcl/console/parse.h"

#include "QVTKOpenGLNativeWidget.h"
#include "vtkCamera.h"
#include "vtkCubeSource.h"
#include "vtkDataObjectToTable.h"
#include "vtkElevationFilter.h"
#include "vtkGenericOpenGLRenderWindow.h"
#include "vtkNamedColors.h"
#include "vtkNew.h"
#include "vtkPolyDataMapper.h"
#include "vtkQtTableView.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkVersion.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    pcl::visualization::PCLVisualizer::Ptr viewer;
};
#endif // MAINWINDOW_H

MainWindow.cpp

#include "MainWindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
    vtkNew<vtkRenderer> renderer;
    renderWindow->AddRenderer(renderer);

    // Crear una nube de puntos con los datos almacenados en un archivo
    // determinado.
    std::string name = "filename.ply";
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::io::loadPLYFile(name,*cloud);

    // Generar el visor de la nube de puntos para realizar la visualización.
    //viewer.reset(new pcl::visualization::PCLVisualizer("viewer",false));
    viewer.reset(new pcl::visualization::PCLVisualizer(renderer, renderWindow,
                                                       "viewer",false));
    viewer->addPointCloud(cloud);
    //ui->Viewer_widget->renderWindow()->AddRenderer(renderer);
    ui->Viewer_widget->setRenderWindow(viewer->getRenderWindow());
    ui->Viewer_widget->update();
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include "MainWindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

MainWindow.ui

具有 QVTKOpenGLNativeWidget.h 促销的小部件。