Search code examples
c++exceptionnew-operatorpoint-cloud-library

Visualise normals of PointNormal point cloud in PCL


I am trying to visualize normals that are contained in a pcl::PointNormal point cloud. I try to do this with following code:

std::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
std::mutex viewerMutex;

void viewerThreadFunction() {
    while(true) {
        if(viewer->wasStopped()) break;
        viewerMutex.lock();
        viewer->spinOnce();
        viewerMutex.unlock();
    }
}

int main() {
    viewer = std::shared_ptr<pcl::visualization::PCLVisualizer>(
                    new pcl::visualization::PCLVisualizer("Viewer"));
    viewer->setBackgroundColor(0, 0, 0);

    pcl::PointCloud<pcl::PointNormal>::Ptr cloud(new pcl::PointCloud<pcl::PointNormal>);

    viewer->addPointCloudNormals<pcl::PointNormal, pcl::PointNormal> (cloud, cloud, 25, 0.15, "normals"); // It throws an exception here:

    std::thread viewerThread{viewerThreadFunction};

    while(true) {
        // populate the point cloud
        viewerMutex.lock();
        viewer->removePointCloud("normals");
        viewer->addPointCloudNormals<pcl::PointNormal, pcl::PointNormal> (cloud, cloud, 25, 0.15, "normals");
        viewerMutex.unlock();
    }
}

I get an exception:

terminate called after throwing an instance of 'std::bad_array_new_length'
    what(): std::bad_array_new_length
Aborted (core dumped)

I tried to rewrite the program so, that viewer->addPointCloudNormals is called only on a populated point cloud, but it did not help.


Solution

  • Your viewer might be missing the actual point cloud data.

    try adding

    viewer->addPointCloud<pcl::PointNormal>(cloud, "foo", 1);
    

    before calling addpointcloudnormals