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 
00047 #include <vtkRenderWindow.h>
00048 
00050 OpenNIPassthrough::OpenNIPassthrough (pcl::OpenNIGrabber& grabber) 
00051   : vis_ ()
00052   , grabber_(grabber)
00053   , device_id_ ()
00054   , cloud_pass_()
00055   , pass_ ()
00056   , mtx_ ()
00057   , ui_ (new Ui::MainWindow)
00058   , vis_timer_ (new QTimer (this))
00059 {
00060   // Create a timer and fire it up every 5ms
00061   vis_timer_->start (5);
00062 
00063   connect (vis_timer_, SIGNAL (timeout ()), this, SLOT (timeoutSlot ()));
00064 
00065   ui_->setupUi (this);
00066 
00067   this->setWindowTitle ("PCL OpenNI PassThrough Viewer");
00068   vis_.reset (new pcl::visualization::PCLVisualizer ("", false));
00069   ui_->qvtk_widget->SetRenderWindow (vis_->getRenderWindow ());
00070   vis_->setupInteractor (ui_->qvtk_widget->GetInteractor (), ui_->qvtk_widget->GetRenderWindow ());
00071   vis_->getInteractorStyle ()->setKeyboardModifier (pcl::visualization::INTERACTOR_KB_MOD_SHIFT);
00072   ui_->qvtk_widget->update (); 
00073 
00074   // Start the OpenNI data acquision
00075   boost::function<void (const CloudConstPtr&)> f = boost::bind (&OpenNIPassthrough::cloud_cb, this, _1);
00076   boost::signals2::connection c = grabber_.registerCallback (f);
00077 
00078   grabber_.start ();
00079 
00080   // Set defaults
00081   pass_.setFilterFieldName ("z");
00082   pass_.setFilterLimits (0.5, 5.0);
00083   
00084   ui_->fieldValueSlider->setRange (5, 50);
00085   ui_->fieldValueSlider->setValue (50);
00086   connect (ui_->fieldValueSlider, SIGNAL (valueChanged (int)), this, SLOT (adjustPassThroughValues (int)));
00087 }
00088 
00090 void
00091 OpenNIPassthrough::cloud_cb (const CloudConstPtr& cloud)
00092 {
00093   QMutexLocker locker (&mtx_);  
00094   FPS_CALC ("computation");
00095 
00096   // Computation goes here
00097   cloud_pass_.reset (new Cloud);
00098   pass_.setInputCloud (cloud);
00099   pass_.filter (*cloud_pass_);
00100 }
00101 
00103 void
00104 OpenNIPassthrough::timeoutSlot ()
00105 {
00106   if (!cloud_pass_)
00107   {
00108     boost::this_thread::sleep (boost::posix_time::milliseconds (1));
00109     return;
00110   }
00111 
00112   CloudPtr temp_cloud;
00113   {
00114     QMutexLocker locker (&mtx_);
00115     temp_cloud.swap (cloud_pass_); 
00116   }
00117   // Add to the 3D viewer
00118   if (!vis_->updatePointCloud (temp_cloud, "cloud_pass"))
00119   {
00120     vis_->addPointCloud (temp_cloud, "cloud_pass");
00121     vis_->resetCameraViewpoint ("cloud_pass");
00122   }
00123   FPS_CALC ("visualization");
00124   ui_->qvtk_widget->update ();
00125 }
00126 
00127 int 
00128 main (int argc, char ** argv)
00129 {
00130   // Initialize QT
00131   QApplication app (argc, argv); 
00132 
00133   // Open the first available camera
00134   pcl::OpenNIGrabber grabber ("#1");
00135   // Check if an RGB stream is provided
00136   if (!grabber.providesCallback<pcl::OpenNIGrabber::sig_cb_openni_point_cloud_rgb> ())
00137   {
00138     PCL_ERROR ("Device #1 does not provide an RGB stream!\n");
00139     return (-1);
00140   }
00141 
00142   OpenNIPassthrough v (grabber);
00143   v.show ();
00144   return (app.exec ());
00145 }


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