openni_device_primesense.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) 2011, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  */
00038 #include <pcl/pcl_config.h>
00039 #ifdef HAVE_OPENNI
00040 
00041 #ifdef __GNUC__
00042 #pragma GCC diagnostic ignored "-Wold-style-cast"
00043 #endif
00044 
00045 #include <pcl/io/openni_camera/openni_device_primesense.h>
00046 #include <pcl/io/openni_camera/openni_image_yuv_422.h>
00047 #include <iostream>
00048 #include <sstream>
00049 #include <pcl/io/boost.h>
00050 
00052 openni_wrapper::DevicePrimesense::DevicePrimesense (
00053     xn::Context& context, 
00054     const xn::NodeInfo& device_node, 
00055     const xn::NodeInfo& image_node, 
00056     const xn::NodeInfo& depth_node, 
00057     const xn::NodeInfo& ir_node) : OpenNIDevice (context, device_node, image_node, depth_node, ir_node)
00058 {
00059   // setup stream modes
00060   enumAvailableModes ();
00061   setDepthOutputMode (getDefaultDepthMode ());
00062   setImageOutputMode (getDefaultImageMode ());
00063   setIROutputMode (getDefaultIRMode ());
00064 
00065   boost::unique_lock<boost::mutex> image_lock (image_mutex_);
00066   XnStatus status = image_generator_.SetIntProperty ("InputFormat", 5);
00067   if (status != XN_STATUS_OK)
00068     THROW_OPENNI_EXCEPTION ("Error setting the image input format to Uncompressed YUV422. Reason: %s", xnGetStatusString (status));
00069 
00070   status = image_generator_.SetPixelFormat (XN_PIXEL_FORMAT_YUV422);
00071   if (status != XN_STATUS_OK)
00072     THROW_OPENNI_EXCEPTION ("Failed to set image pixel format to YUV422. Reason: %s", xnGetStatusString (status));
00073 
00074   image_lock.unlock ();
00075 
00076   boost::lock_guard<boost::mutex> depth_lock (depth_mutex_);
00077   status = depth_generator_.SetIntProperty ("RegistrationType", 1);
00078   if (status != XN_STATUS_OK)
00079     THROW_OPENNI_EXCEPTION ("Error setting the registration type. Reason: %s", xnGetStatusString (status));
00080 }
00081 
00083 openni_wrapper::DevicePrimesense::~DevicePrimesense () throw ()
00084 {
00085   setDepthRegistration (false);
00086   setSynchronization (false);
00087 
00088   depth_mutex_.lock ();
00089   depth_generator_.UnregisterFromNewDataAvailable (depth_callback_handle_);
00090   depth_mutex_.unlock ();
00091 
00092   image_mutex_.lock ();
00093   image_generator_.UnregisterFromNewDataAvailable (image_callback_handle_);
00094   image_mutex_.unlock ();
00095 }
00096 
00098 bool 
00099 openni_wrapper::DevicePrimesense::isImageResizeSupported (
00100     unsigned input_width, 
00101     unsigned input_height, 
00102     unsigned output_width, 
00103     unsigned output_height) const throw ()
00104 {
00105   return (ImageYUV422::resizingSupported (input_width, input_height, output_width, output_height));
00106 }
00107 
00109 void 
00110 openni_wrapper::DevicePrimesense::enumAvailableModes () throw ()
00111 {
00112   XnMapOutputMode output_mode;
00113   available_image_modes_.clear ();
00114   available_depth_modes_.clear ();
00115 
00116   // Depth Modes
00117   output_mode.nFPS = 30;
00118   output_mode.nXRes = XN_VGA_X_RES;
00119   output_mode.nYRes = XN_VGA_Y_RES;
00120   available_depth_modes_.push_back (output_mode);
00121 
00122   output_mode.nFPS = 25;
00123   output_mode.nXRes = XN_VGA_X_RES;
00124   output_mode.nYRes = XN_VGA_Y_RES;
00125   available_depth_modes_.push_back (output_mode);
00126 
00127   output_mode.nFPS = 25;
00128   output_mode.nXRes = XN_QVGA_X_RES;
00129   output_mode.nYRes = XN_QVGA_Y_RES;
00130   available_depth_modes_.push_back (output_mode);
00131 
00132   output_mode.nFPS = 30;
00133   output_mode.nXRes = XN_QVGA_X_RES;
00134   output_mode.nYRes = XN_QVGA_Y_RES;
00135   available_depth_modes_.push_back (output_mode);
00136 
00137   output_mode.nFPS = 60;
00138   output_mode.nXRes = XN_QVGA_X_RES;
00139   output_mode.nYRes = XN_QVGA_Y_RES;
00140   available_depth_modes_.push_back (output_mode);
00141 
00142   // RGB Modes
00143   output_mode.nFPS = 30;
00144   output_mode.nXRes = XN_VGA_X_RES;
00145   output_mode.nYRes = XN_VGA_Y_RES;
00146   available_image_modes_.push_back (output_mode);
00147 
00148   output_mode.nFPS = 25;
00149   output_mode.nXRes = XN_VGA_X_RES;
00150   output_mode.nYRes = XN_VGA_Y_RES;
00151   available_image_modes_.push_back (output_mode);
00152 
00153   output_mode.nFPS = 25;
00154   output_mode.nXRes = XN_QVGA_X_RES;
00155   output_mode.nYRes = XN_QVGA_Y_RES;
00156   available_image_modes_.push_back (output_mode);
00157 
00158   output_mode.nFPS = 30;
00159   output_mode.nXRes = XN_QVGA_X_RES;
00160   output_mode.nYRes = XN_QVGA_Y_RES;
00161   available_image_modes_.push_back (output_mode);
00162 
00163   output_mode.nFPS = 60;
00164   output_mode.nXRes = XN_QVGA_X_RES;
00165   output_mode.nYRes = XN_QVGA_Y_RES;
00166   available_image_modes_.push_back (output_mode);
00167 }
00168 
00170 boost::shared_ptr<openni_wrapper::Image> 
00171 openni_wrapper::DevicePrimesense::getCurrentImage (boost::shared_ptr<xn::ImageMetaData> image_data) const throw ()
00172 {
00173   return (boost::shared_ptr<openni_wrapper::Image> (new ImageYUV422 (image_data)));
00174 }
00175 
00177 void 
00178 openni_wrapper::DevicePrimesense::startImageStream ()
00179 {
00180   // Suat: Ugly workaround... but on some usb-ports its not possible to start the image stream after the depth stream.
00181   // turning on and off registration solves for some reason the problem!
00182 
00183   if (isDepthStreamRunning ())
00184   {
00185     if (isDepthRegistered ())
00186     {
00187      // Reset the view point
00188       setDepthRegistration (false);
00189 
00190       // Reset the view point
00191       setDepthRegistration (true);
00192 
00193      // Reset the view point
00194       setDepthRegistration (false);
00195 
00196       // Start the stream
00197       OpenNIDevice::startImageStream ();
00198 
00199       // Register the stream
00200       setDepthRegistration (true);
00201     }
00202     else
00203     {
00204       // Reset the view point
00205       setDepthRegistration (true);
00206       // Reset the view point
00207       setDepthRegistration (false);
00208 
00209       // Start the stream
00210       OpenNIDevice::startImageStream ();
00211     }
00212   }
00213   else
00214     // Start the stream
00215     OpenNIDevice::startImageStream ();
00216 }
00217 
00219 void 
00220 openni_wrapper::DevicePrimesense::startDepthStream ()
00221 {
00222   if (isDepthRegistered ())
00223   {
00224     // Reset the view point
00225     setDepthRegistration (false);
00226 
00227     // Start the stream
00228     OpenNIDevice::startDepthStream ();
00229 
00230     // Register the stream
00231     setDepthRegistration (true);
00232   }
00233   else
00234     // Start the stream
00235     OpenNIDevice::startDepthStream ();
00236 }
00237 
00238 #endif


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