Search code examples
qt-creatorvtkqvtkwidgetpcl

PCL viewer inside QtCreator widget with VTK and QVTKOpenGLStereoWidget


I need to make a graphical window with a Qt widget that allows to represent inside it a point cloud that I have previously loaded using the PLC library.

Here's what I have so far that doesn't work (I based it on tutorials and this answer).

I'm using:

  • Ubuntu 20.04
  • Qt Creator 5.15
  • VTK 9.1
  • PCL 1.12

The reason I am using QVTKOpenGLStereoWidget is that as far as I read both QVTKOpenGLWidget and QVTKWidget are no longer available or will be discontinued in future releases.

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

It only has one widget that has a promote a QVTKOpenGLSTereoWidget

enter image description here

MainWindow.h

I did the initialization of the VTK library, the inclusion of the different library headers and created the PCL viewer and the VTK renderer based on OpenGL.

#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

Finally in the constructor of the MainWindow class I simply loaded the file (in this case it was a PLY) and then I tried to make the graphical representation in the widget

#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

I have not changed anything, it is default

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

ERROR.

The main error I have is: /usr/local/include/vtk-9.1/vtkSmartPointer.h:225: error: incomplete type ‘vtkGenericOpenGLRenderWindow’ used in nested name specifier In file included from /usr/local/include/pcl-1.12/pcl/visualization/point_cloud_geometry_handlers.h:49, from /usr/local/include/pcl-1.12/pcl/visualization/common/actor_map.h:40, from /usr/local/include/pcl-1.12/pcl/visualization/pcl_visualizer.h:48, from /usr/local/include/pcl-1.12/pcl/visualization/cloud_viewer.h:39, from ../04_pcl_viewer_V3/MainWindow.h:6, from ../04_pcl_viewer_V3/MainWindow.cpp:1: /usr/local/include/vtk-9.1/vtkSmartPointer.h: In instantiation of ‘static vtkSmartPointer vtkSmartPointer::New() [with T = vtkGenericOpenGLRenderWindow]’: ../04_pcl_viewer_V3/MainWindow.cpp:16:67: required from here /usr/local/include/vtk-9.1/vtkSmartPointer.h:225:69: error: incomplete type ‘vtkGenericOpenGLRenderWindow’ used in nested name specifier 225 | static vtkSmartPointer New() { return vtkSmartPointer(T::New(), NoReference()); } | ~~~~~~^~


Solution

  • I was finally able to find the solution to the problem so I am sharing it as an answer in case it could be useful for someone else.

    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

    Widget with QVTKOpenGLNativeWidget.h promotion.