PCL viewer hangs with busy cursor

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

PCL viewer hangs with busy cursor

Rafid
Hi,

I'm trying to update pointcloud in real-time as it becomes available. I am using a separate thread for this purpose to spin the viewer but what i get is only black screen with busy cursor. I am using trunk version of the pcl compiled on vs2010 with cmake. To illustrate what i am trying to do i have a sample application as given below which is using the same concept as of visualization example but implementing it in a thread. I am chaning the pointcloud in the main thread and a separate thread is responsible for spinning. The problem is i think the thread is not working at all. If i am not wrong it was working in the installer version, but i would like to check if it is correct what i am doing and if yes then why don't it work?



#include <iostream>

#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>

boost::shared_ptr<pcl::visualization::PCLVisualizer> rgbVis (pcl::PointCloud<pcl::PointXYZRGBA>::ConstPtr cloud)
{
  // --------------------------------------------
  // -----Open 3D viewer and add point cloud-----
  // --------------------------------------------
  boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
  //viewer->setCameraPose(2,-1,-3,0,3.14,3.14,0,0,0,0);
 
  pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> rgb(cloud);
  viewer->addPointCloud<pcl::PointXYZRGBA> (cloud, rgb, "Cloud");
  viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "Cloud");
  viewer->resetCameraViewpoint("Cloud");
  viewer->addCoordinateSystem (1.0);
  viewer->initCameraParameters ();
  return (viewer);
}

boost::mutex updateModelMutex;
void viewerRunner(boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer)
{
        while (!viewer->wasStopped ())
    {
                viewer->spinOnce (100);
                boost::this_thread::sleep (boost::posix_time::microseconds (100000));
               
        }
}


void createCloud(pcl::PointCloud<pcl::PointXYZRGBA>::Ptr &point_cloud_ptr, float zmin,float zmax,float anglemin,float anglemax)
{
       
        // ------------------------------------
  // -----Create example point cloud-----
  // ------------------------------------
  pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
 
  std::cout << "Genarating example point clouds.\n\n";
  // We're going to make an ellipse extruded along the z-axis. The colour for
  // the XYZRGB cloud will gradually go from red to green to blue.
  uint8_t r(255), g(15), b(15);
  for (float z=zmin; z <= zmax; z += 0.05)
  {
    for (float angle=anglemin; angle <= anglemax; angle += 5.0)
    {
      pcl::PointXYZ basic_point;
      basic_point.x = 0.5 * cosf (pcl::deg2rad(angle));
      basic_point.y = sinf (pcl::deg2rad(angle));
      basic_point.z = z;
      basic_cloud_ptr->points.push_back(basic_point);

      pcl::PointXYZRGBA point;
      point.x = basic_point.x;
      point.y = basic_point.y;
      point.z = basic_point.z;
      uint32_t rgb = (static_cast<uint32_t>(r) << 16 |
              static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
      point.rgb = *reinterpret_cast<float*>(&rgb);
      point_cloud_ptr->points.push_back (point);
    }
    if (z < 0.0)
    {
      r -= 12;
      g += 12;
    }
    else
    {
      g -= 12;
      b += 12;
    }
  }
  basic_cloud_ptr->width = (int) basic_cloud_ptr->points.size ();
  basic_cloud_ptr->height = 1;
  point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
  point_cloud_ptr->height = 1;
}

int
main (int argc, char** argv)
{

        pcl::PointCloud<pcl::PointXYZRGBA>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGBA>);
       
        // ------------------------------------
  // -----Create example point cloud-----
  // ------------------------------------
  createCloud(point_cloud_ptr,-1.0,1.0,0.0,360.0);

  // Creating PCL Viewer window and thread
        boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
    viewer = rgbVis(point_cloud_ptr);
        boost::thread vthread(&viewerRunner,viewer);

int fcount =0;
  while (fcount<=3)
  {
    createCloud(point_cloud_ptr,-1.0-fcount,1.0+fcount,0.0,360.0);

        boost::mutex::scoped_lock updateLock(updateModelMutex);
        pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> rgb(point_cloud_ptr);
        viewer->updatePointCloud<pcl::PointXYZRGBA>(point_cloud_ptr,rgb,"Cloud");
        updateLock.unlock();

    boost::this_thread::sleep (boost::posix_time::microseconds (200000));
        fcount++;
  }
       
}


Best Regards,
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Sergey
Administrator
Hi Rafid,

Your example fails with an error from VTK on my laptop. The reason is that you do not lock the mutex in the viewerRunner() function. After I have added a scoped_lock there you example works fine. Does this solve your issue, or it still persists?

Regards,
Sergey
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
Thanks sergey,

I have enveloped the call to spinOnce with mutex scoped lock as follows, although it didn't get better and still hangs. I do not get any error in the shell, maybe since you are in linux so you are getting the messages but here it just gets silent and i cant drag the window or do anything at all with it, although terminal window remain alive but program never quits and doesn't show anything at all.


boost::mutex::scoped_lock updateLock(updateModelMutex);
viewer->spinOnce (100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
updateLock.unlock();

Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Sergey
Administrator
Rafid,

Well, there is another problem in your sample. As soon as the main is done with the short while loop, it exits the program and the extra thread that it has created gets killed, I suppose (which might explain the observed behavior). So try adding

  vthread.join();

in the end of your main.

Regards,
Sergey
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
I didn't use join as i was trying to achieve parallel threading rather worker thread architecture. Anyway i have added the join to check whether it helps but the problem still persist.

...
       
    boost::this_thread::sleep (boost::posix_time::microseconds (100000));
        fcount++;
  }
        vthread.join();

Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
Could it be due to some changes in the new pcl 1.7 version which is i guess the default one at the svn trunk? Because i remember it used to work with all-in-one installer version available for windows. Now that i am using this svn version i am getting the hanging behavior.

Is the svn trunk version compatible with windows at all? Since i remember the visualization is unchecked by default when using cmake to compile which might notion that there is some work going on?
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Radu B. Rusu
Administrator
Rafid,

The visualization definitely works well in Windows. If something happened on your machine, try a different revision of
trunk if you think that helps, but in general I don't think there should be any problems with the basic
components/modules such as visualization.

Please remember that TRUNK is _UNSTABLE_ and only developers are supposed to be using it! Please revert to our official
PCL releases (e.g., 1.6) if you want to develop applications on top. Being on trunk as an user is NOT recommended! While
we'll do our best to support you, because you're basically beta testing our latest changes, things might be more painful
(than you think) on your end ;)

Cheers,
Radu.
--
http://openperception.org

On 08/22/2012 08:30 AM, Rafid wrote:

> Could it be due to some changes in the new pcl 1.7 version which is i guess
> the default one at the svn trunk? Because i remember it used to work with
> all-in-one installer version available for windows. Now that i am using this
> svn version i am getting the hanging behavior.
>
> Is the svn trunk version compatible with windows at all? Since i remember
> the visualization is unchecked by default when using cmake to compile which
> might notion that there is some work going on?
>
>
>
> --
> View this message in context: http://www.pcl-users.org/PCL-viewer-hangs-with-busy-cursor-tp4021568p4021586.html
> Sent from the Point Cloud Library (PCL) Users mailing list archive at Nabble.com.
> _______________________________________________
> [hidden email] / http://pointclouds.org
> http://pointclouds.org/mailman/listinfo/pcl-users
>
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
This post was updated on .
Thanks Radu,

I moved to trunk because i needed kinfu application which was not in the windows distributed prebuilt libraries.  I can't see 1.6 in the branches at http://svn.pointclouds.org/pcl/branches/ 
 In 1.5 i dont even see gpu so kinfu couldn't be in that release.

Would you please take a look at the example and tell if it ought to work so that i should then concentrate on recompiling and reinstalling PCL? Compiling PCL is really toooo slow. Even i face problem with build times for applications using PCL taking usually upto 2-4 minutes for small applications such as above on my dual core.

Best Regards,

Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Radu B. Rusu
Administrator
Rafid,

You're right. KinFu or any other CUDA code hasn't reached the maturity that we want yet, so we haven't included it in an
official release, as we cannot guarantee support for it. All the CUDA code will most likely make its way into a release
when 2.0 comes out.

In the meantime, it is what it is. You cannot have both the newest unstable features from trunk, and not be exposed to
compiling from source. PCL is a voluntary open source project, and we try our best to move forward. If PCL takes too
long to compile on your machine, considering buying a more powerful machine, or compile only the parts that you want, or
use many of the compiler optimizations/tricks that we provide our web pages or support forums.

Cheers,
Radu.
--
http://openperception.org

On 08/22/2012 08:48 AM, Rafid wrote:

> Thanks Radu,
>
> I moved to trunk because i needed kinfu application which was not in the
> windows distributed prebuilt libraries.  I can't see 1.6 in the branches at
> http://svn.pointclouds.org/pcl/branches/
>   In 1.5 i dont even see gpu so kinfu couldn't be in that release.
>
> Would you please take a look at the example and tell if it ought to work so
> that i should then concentrate on recompiling and reinstalling PCL?
> Compiling PCL is really toooo slow. Even i face problem with build times
> which take upto 2-4 minutes on my dual core.
>
> Best Regards,
>
>
>
>
>
> --
> View this message in context: http://www.pcl-users.org/PCL-viewer-hangs-with-busy-cursor-tp4021568p4021589.html
> Sent from the Point Cloud Library (PCL) Users mailing list archive at Nabble.com.
> _______________________________________________
> [hidden email] / http://pointclouds.org
> http://pointclouds.org/mailman/listinfo/pcl-users
>
_______________________________________________
[hidden email] / http://pointclouds.org
http://pointclouds.org/mailman/listinfo/pcl-users
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
so i guess i should stick with the trunk version then :). what about running the visualizer in the separate thread as i tried to do in the sample application above? Is there a way to determine if it is because of the way i am doing it or it has something to do with the instability?
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
I tried to replace the visualization library with the 1.6 version but the problem still persist. So it must be the way i am doing it. Would anyone please tell me what is possibly going wrong here?
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Rafid
Debugging it revealed that the culprit statement is call to spinOnce(). As soon as it is called it never returns and thread becomes hanged.

void viewerRunner(boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer)
{
        while (!viewer->wasStopped ())
    {
                cout<<"Thread started"<<endl;
                if(update)
        {
                cout<<"Updating"<<endl;
                viewer->spinOnce (100);
               
                boost::mutex::scoped_lock updateLock(updateModelMutex);
                pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGBA> rgb(point_cloud_ptr);
                if(!viewer->updatePointCloud(point_cloud_ptr, "Cloud"))
                  viewer->addPointCloud(point_cloud_ptr, rgb, "Cloud");
                update = false;
                updateLock.unlock();
                }
                cout<<"Thread finished"<<endl;
        }
}


The output received for this function in the terminal is as follows:

<b>Genarating example point clouds.

Genarating example point clouds.

Thread started
Thread finished
Thread started
Thread finished
Thread started
Updating
Genarating example point clouds.

Genarating example point clouds.

Genarating example point clouds.
Reply | Threaded
Open this post in threaded view
|

Re: PCL viewer hangs with busy cursor

Sergey
Administrator
Hi Rafid,

Just in case you didn't see it. There is another discussion about running PCLVisualizer in a separate thread, perhaps it may help you.

Regards,
Sergey