Skip to content

Instantly share code, notes, and snippets.

@UnaNancyOwen
Last active May 19, 2016 04:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UnaNancyOwen/f7cae236b16ac308155b to your computer and use it in GitHub Desktop.
Save UnaNancyOwen/f7cae236b16ac308155b to your computer and use it in GitHub Desktop.
Drawing the Point Cloud using PCLVisualizer with Kinect v1 Grabber
#include "stdafx.h"
// Disable Error C4996 that occur when using Boost.Signals2.
#ifdef _DEBUG
#define _SCL_SECURE_NO_WARNINGS
#endif
#include "kinect_grabber.h"
#include <pcl/visualization/pcl_visualizer.h>
template <typename PointType>
class Viewer
{
typedef pcl::PointCloud<PointType> PointCloud;
typedef typename PointCloud::ConstPtr ConstPtr;
public:
Viewer( pcl::Grabber& grabber )
: viewer( new pcl::visualization::PCLVisualizer( "Point Cloud Viewer" ) )
, grabber( grabber )
{
}
void run()
{
viewer->registerMouseCallback( &Viewer::mouse_callback, *this );
viewer->registerKeyboardCallback( &Viewer::keyboard_callback, *this );
boost::function<void( const ConstPtr& ) > callback = boost::bind( &Viewer::cloud_callback, this, _1 );
boost::signals2::connection connection = grabber.registerCallback( callback );
grabber.start();
while( !viewer->wasStopped() ){
viewer->spinOnce();
ConstPtr cloud;
if( mutex.try_lock() ){
buffer.swap( cloud );
mutex.unlock();
}
if( cloud ){
if( !viewer->updatePointCloud( cloud, "Cloud" ) ){
viewer->addPointCloud( cloud, "Cloud" );
viewer->resetCameraViewpoint( "Cloud" );
}
}
if( GetKeyState( VK_ESCAPE ) < 0 ){
break;
}
}
grabber.stop();
if( connection.connected() ){
connection.disconnect();
}
}
private:
void cloud_callback( const ConstPtr& cloud )
{
boost::mutex::scoped_lock lock( mutex );
buffer = cloud;
}
void keyboard_callback( const pcl::visualization::KeyboardEvent& event, void* )
{
if( event.getKeyCode() && event.keyDown() ){
std::cout << "Key : " << event.getKeyCode() << std::endl;
}
}
void mouse_callback( const pcl::visualization::MouseEvent& event, void* )
{
if( event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && event.getButton() == pcl::visualization::MouseEvent::LeftButton ){
std::cout << "Mouse : " << event.getX() << ", " << event.getY() << std::endl;
}
}
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
pcl::Grabber& grabber;
boost::mutex mutex;
ConstPtr buffer;
};
int _tmain( int argc, _TCHAR* argv[] )
{
boost::shared_ptr<pcl::Grabber> grabber = boost::make_shared<pcl::KinectGrabber>();
Viewer<pcl::PointXYZRGB> viewer( *grabber );
viewer.run();
return 0;
}
#include "stdafx.h"
// Disable Error C4996 that occur when using Boost.Signals2.
#ifdef _DEBUG
#define _SCL_SECURE_NO_WARNINGS
#endif
#include "kinect_grabber.h"
#include <pcl/visualization/pcl_visualizer.h>
boost::mutex mutex;
void keyboard_callback( const pcl::visualization::KeyboardEvent& event, void* )
{
if( event.getKeyCode() && event.keyDown() ){
std::cout << "Key : " << event.getKeyCode() << std::endl;
}
}
void mouse_callback( const pcl::visualization::MouseEvent& event, void* )
{
if( event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && event.getButton() == pcl::visualization::MouseEvent::LeftButton ){
std::cout << "Mouse : " << event.getX() << ", " << event.getY() << std::endl;
}
}
int _tmain( int argc, _TCHAR* argv[] )
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer( new pcl::visualization::PCLVisualizer( "Point Cloud Viewer" ) );
viewer->registerKeyboardCallback( &keyboard_callback, "keyboard" );
viewer->registerMouseCallback( &mouse_callback, "mouse" );
pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud;
boost::function<void( const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& )> function =
[&cloud]( const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr &ptr ){
boost::mutex::scoped_lock lock( mutex );
cloud = ptr;
};
boost::shared_ptr<pcl::Grabber> grabber = boost::make_shared<pcl::KinectGrabber>();
boost::signals2::connection connection = grabber->registerCallback( function );
grabber->start();
while( !viewer->wasStopped() ){
viewer->spinOnce();
if( cloud ){
if( !viewer->updatePointCloud( cloud, "cloud" ) ){
viewer->addPointCloud( cloud, "cloud" );
viewer->resetCameraViewpoint( "cloud" );
}
}
if( GetKeyState( VK_ESCAPE ) < 0 ){
break;
}
}
grabber->stop();
if( connection.connected() ){
connection.disconnect();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment