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 , preview_ ()
00013 {
00014
00015 boost::function<void (const PointCloudConstPtr&)> frame_cb = boost::bind (&OpenNICapture::onNewFrame, this, _1);
00016
00017 grabber_.registerCallback (frame_cb);
00018 grabber_.start ();
00019 }
00020
00021 OpenNICapture::~OpenNICapture ()
00022 {
00023
00024 grabber_.stop ();
00025 if (preview_)
00026 delete preview_;
00027 }
00028
00029 void
00030 OpenNICapture::setTriggerMode (bool use_trigger)
00031 {
00032 use_trigger_ = use_trigger;
00033 }
00034
00035 const PointCloudPtr
00036 OpenNICapture::snap ()
00037 {
00038 if (use_trigger_)
00039 {
00040 if (!preview_)
00041 {
00042
00043 preview_ = new pcl::visualization::PCLVisualizer ();
00044
00045 boost::function<void (const pcl::visualization::KeyboardEvent&)> keyboard_cb =
00046 boost::bind (&OpenNICapture::onKeyboardEvent, this, _1);
00047
00048 preview_->registerKeyboardCallback (keyboard_cb);
00049 }
00050 waitForTrigger ();
00051 }
00052
00053 int old_frame = frame_counter_;
00054 while (frame_counter_ == old_frame) continue;
00055 return (most_recent_frame_);
00056 }
00057
00058 const PointCloudPtr
00059 OpenNICapture::snapAndSave (const std::string & filename)
00060 {
00061 PointCloudPtr snapped_frame = snap ();
00062 if (snapped_frame)
00063 pcl::io::savePCDFile (filename, *snapped_frame);
00064 return (snapped_frame);
00065 }
00066
00067
00068 void
00069 OpenNICapture::onNewFrame (const PointCloudConstPtr &cloud)
00070 {
00071 mutex_.lock ();
00072 ++frame_counter_;
00073 most_recent_frame_ = boost::make_shared<PointCloud> (*cloud);
00074 mutex_.unlock ();
00075 }
00076
00077 void
00078 OpenNICapture::onKeyboardEvent (const pcl::visualization::KeyboardEvent & event)
00079 {
00080
00081 mutex_.lock ();
00082 if (event.keyDown () && event.getKeySym () == "space")
00083 {
00084 trigger_ = true;
00085 }
00086 mutex_.unlock ();
00087 }
00088
00090 void
00091 OpenNICapture::waitForTrigger ()
00092 {
00093
00094 trigger_ = false;
00095
00096 int last_frame = frame_counter_;
00097
00098
00099 while (!trigger_)
00100 {
00101
00102 if (frame_counter_ > last_frame)
00103 {
00104 last_frame = frame_counter_;
00105 if (most_recent_frame_)
00106 {
00107 mutex_.lock ();
00108 if (!preview_->updatePointCloud (most_recent_frame_, "preview"))
00109 {
00110 preview_->addPointCloud (most_recent_frame_, "preview");
00111 preview_->resetCameraViewpoint ("preview");
00112 }
00113 mutex_.unlock ();
00114 }
00115 preview_->spinOnce ();
00116 }
00117 }
00118 }