c++user-interfacegraphics3dpoint-cloud-library

PCLVisualizer flashing when using .spinOnce()


Beginner here...basically I want do visualize a pointcloud with the PCLVisualizer, while executing code alongside the visualization.

So far i tried to use the CloudViewer(same behaviour) and also the following code which is based on the PCL_Tutorials and also preferred since its threadsafe:

#include <pcl/visualization/pcl_visualizer.h>

int 
main ()
{
    pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
    viewer.setBackgroundColor (1.0, 0.5, 1.0);
    pcl::PointXYZ o;
    o.x = 1.0;
    o.y = 0;
    o.z = 0;
    viewer.addSphere (o, 0.25, "sphere", 0);
    while (!viewer.wasStopped ())
    {
        // do something else
        viewer.spinOnce();
    }
    return 0;
}

However if i run this code, the viewer closes and opens on every .spinOnce() call. Is it supposed to act like this? Can i use .spin() on a single thread instead and do computations on a different thread?

I get the feeling that this should work without issues (window should stay open), but i'm missing something...some hints would be very appreciated and thanks in advance.

PS: I'm currently on PCL 1.12.0 under Fedora 39. Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(pcl_viewer)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    add_compile_options(-Wall -Wextra -Wpedantic -g -fdiagnostics-color)
endif()

find_package(PCL 1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})

link_directories(${PCL_LIBRARY_DIRS})

add_definitions(${PCL_DEFINITIONS})

add_executable(${CMAKE_PROJECT_NAME} src/main.cpp)
target_link_libraries (${CMAKE_PROJECT_NAME} ${PCL_LIBRARIES})

Solution

  • I think this is because PCL 1.12.0 is rather old, and VTK 9.2.6 (used by PCL for visualizing) is rather new, and they don't work properly together. There was a change in VTK (in 9.1.0 I believe), which made a change in PCL necessary. The PCL change was release in PCL 1.13.0. So try using a newer PCL version (e.g. compiled from source). Apparently there is also a pull request to update the PCL version in fedora packages: https://src.fedoraproject.org/rpms/pcl/pull-request/9