openni_passthrough.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2009-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *      
00036  */
00037 
00038 #include <pcl/apps/openni_passthrough.h>
00039 // QT4
00040 #include <QApplication>
00041 #include <QMutexLocker>
00042 #include <QEvent>
00043 #include <QObject>
00044 // PCL
00045 #include <pcl/console/parse.h>
00046 
00048 OpenNIPassthrough::OpenNIPassthrough (pcl::OpenNIGrabber& grabber) 
00049   : vis_ ()
00050   , grabber_(grabber)
00051   , device_id_ ()
00052   , cloud_pass_()
00053   , pass_ ()
00054   , mtx_ ()
00055   , ui_ (new Ui::MainWindow)
00056   , vis_timer_ (new QTimer (this))
00057 {
00058   // Create a timer and fire it up every 5ms
00059   vis_timer_->start (5);
00060 
00061   connect (vis_timer_, SIGNAL (timeout ()), this, SLOT (timeoutSlot ()));
00062 
00063   ui_->setupUi (this);
00064 
00065   this->setWindowTitle ("PCL OpenNI PassThrough Viewer");
00066   vis_.reset (new pcl::visualization::PCLVisualizer ("", false));
00067   ui_->qvtk_widget->SetRenderWindow (vis_->getRenderWindow ());
00068   vis_->setupInteractor (ui_->qvtk_widget->GetInteractor (), ui_->qvtk_widget->GetRenderWindow ());
00069   vis_->getInteractorStyle ()->setKeyboardModifier (pcl::visualization::INTERACTOR_KB_MOD_SHIFT);
00070   ui_->qvtk_widget->update (); 
00071 
00072   // Start the OpenNI data acquision
00073   boost::function<void (const CloudConstPtr&)> f = boost::bind (&OpenNIPassthrough::cloud_cb, this, _1);
00074   boost::signals2::connection c = grabber_.registerCallback (f);
00075 
00076   grabber_.start ();
00077 
00078   // Set defaults
00079   pass_.setFilterFieldName ("z");
00080   pass_.setFilterLimits (0.5, 5.0);
00081   
00082   ui_->fieldValueSlider->setRange (5, 50);
00083   ui_->fieldValueSlider->setValue (50);
00084   connect (ui_->fieldValueSlider, SIGNAL (valueChanged (int)), this, SLOT (adjustPassThroughValues (int)));
00085 }
00086 
00088 void
00089 OpenNIPassthrough::cloud_cb (const CloudConstPtr& cloud)
00090 {
00091   QMutexLocker locker (&mtx_);  
00092   FPS_CALC ("computation");
00093 
00094   // Computation goes here
00095   cloud_pass_.reset (new Cloud);
00096   pass_.setInputCloud (cloud);
00097   pass_.filter (*cloud_pass_);
00098 }
00099 
00101 void
00102 OpenNIPassthrough::timeoutSlot ()
00103 {
00104   if (!cloud_pass_)
00105   {
00106     boost::this_thread::sleep (boost::posix_time::milliseconds (1));
00107     return;
00108   }
00109 
00110   CloudPtr temp_cloud;
00111   {
00112     QMutexLocker locker (&mtx_);
00113     temp_cloud.swap (cloud_pass_); 
00114   }
00115   // Add to the 3D viewer
00116   if (!vis_->updatePointCloud (temp_cloud, "cloud_pass"))
00117   {
00118     vis_->addPointCloud (temp_cloud, "cloud_pass");
00119     vis_->resetCameraViewpoint ("cloud_pass");
00120   }
00121   FPS_CALC ("visualization");
00122   ui_->qvtk_widget->update ();
00123 }
00124 
00125 int 
00126 main (int argc, char ** argv)
00127 {
00128   // Initialize QT
00129   QApplication app (argc, argv); 
00130 
00131   // Open the first available camera
00132   pcl::OpenNIGrabber grabber ("#1");
00133   // Check if an RGB stream is provided
00134   if (!grabber.providesCallback<pcl::OpenNIGrabber::sig_cb_openni_point_cloud_rgb> ())
00135   {
00136     PCL_ERROR ("Device #1 does not provide an RGB stream!\n");
00137     return (-1);
00138   }
00139 
00140   OpenNIPassthrough v (grabber);
00141   v.show ();
00142   return (app.exec ());
00143 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:16:03