openni_capture.cpp
Go to the documentation of this file.
00001 #include "openni_capture.h"
00002 #include <pcl/io/pcd_io.h>
00003 #include <boost/thread/mutex.hpp>
00004 #include <boost/make_shared.hpp>
00005 
00006 OpenNICapture::OpenNICapture (const std::string& device_id)
00007   : grabber_ (device_id)
00008   , most_recent_frame_ ()
00009   , frame_counter_ (0)
00010   , use_trigger_ (false)
00011   , trigger_ (false)
00012 {
00013   // Register a callback function to our OpenNI grabber...
00014   boost::function<void (const PointCloudConstPtr&)> frame_cb = boost::bind (&OpenNICapture::onNewFrame, this, _1);
00015   // ... and start grabbing frames
00016   grabber_.registerCallback (frame_cb);
00017   grabber_.start ();
00018 }
00019 
00020 OpenNICapture::~OpenNICapture ()
00021 {
00022   // Stop the grabber when shutting down
00023   grabber_.stop ();
00024 }
00025 
00026 void
00027 OpenNICapture::setTriggerMode (bool use_trigger)
00028 {
00029   use_trigger_ = use_trigger;
00030 }
00031 
00032 const PointCloudPtr
00033 OpenNICapture::snap ()
00034 {
00035   if (use_trigger_)
00036   {
00037     if (!preview_)
00038     {
00039       // Initialize the visualizer ONLY if use_trigger is set to true
00040       preview_ = boost::shared_ptr<pcl::visualization::PCLVisualizer> (new pcl::visualization::PCLVisualizer ());
00041 
00042       boost::function<void (const pcl::visualization::KeyboardEvent&)> keyboard_cb =
00043         boost::bind (&OpenNICapture::onKeyboardEvent, this, _1);
00044 
00045       preview_->registerKeyboardCallback (keyboard_cb);
00046     }
00047     waitForTrigger ();
00048   }
00049   // Wait for a fresh frame
00050   int old_frame = frame_counter_;
00051   while (frame_counter_ == old_frame) continue;
00052   return (most_recent_frame_);
00053 }
00054 
00055 const PointCloudPtr
00056 OpenNICapture::snapAndSave (const std::string & filename)
00057 {
00058   PointCloudPtr snapped_frame = snap ();
00059   if (snapped_frame)
00060     pcl::io::savePCDFile (filename, *snapped_frame);
00061   return (snapped_frame);
00062 }
00063 
00064 
00065 void
00066 OpenNICapture::onNewFrame (const PointCloudConstPtr &cloud)
00067 {
00068   mutex_.lock ();
00069   ++frame_counter_;
00070   most_recent_frame_ = boost::make_shared<PointCloud> (*cloud); // Make a copy of the frame
00071   mutex_.unlock ();
00072 }
00073 
00074 void
00075 OpenNICapture::onKeyboardEvent (const pcl::visualization::KeyboardEvent & event)
00076 {
00077   // When the spacebar is pressed, trigger a frame capture
00078   mutex_.lock ();
00079   if (event.keyDown () && event.getKeySym () == "space")
00080   {
00081     trigger_ = true;
00082   }
00083   mutex_.unlock ();
00084 }
00085 
00087 void
00088 OpenNICapture::waitForTrigger ()
00089 {
00090   // Reset the trigger state
00091   trigger_ = false;
00092 
00093   int last_frame = frame_counter_;
00094 
00095   // Now wait for the trigger to be flipped
00096   while (!trigger_)
00097   {
00098     // Update the preview window on new frames
00099     if (frame_counter_ > last_frame)
00100     {
00101       last_frame = frame_counter_;
00102       if (most_recent_frame_)
00103       {
00104         mutex_.lock ();
00105         if (!preview_->updatePointCloud (most_recent_frame_, "preview"))
00106         {
00107           preview_->addPointCloud (most_recent_frame_, "preview");
00108           preview_->resetCameraViewpoint ("preview");
00109         }
00110         mutex_.unlock ();
00111       }
00112       preview_->spinOnce ();
00113     }
00114   }
00115 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:26:21