openni_device_oni.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011 2011 Willow Garage, Inc.
00005  *    Suat Gedikli <gedikli@willowgarage.com>
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 #include <openni_camera/openni_device_oni.h>
00038 #include <openni_camera/openni_image_rgb24.h>
00039 
00040 using namespace std;
00041 using namespace boost;
00042 
00043 namespace openni_wrapper
00044 {
00045 
00046 DeviceONI::DeviceONI(xn::Context& context, const std::string& file_name, bool repeat, bool streaming) throw (OpenNIException)
00047   : OpenNIDevice(context)
00048   , streaming_ (streaming)
00049   , depth_stream_running_ (false)
00050   , image_stream_running_ (false)
00051   , ir_stream_running_ (false)
00052 {
00053   XnStatus status;
00054   status = context_.OpenFileRecording(file_name.c_str());
00055   if (status != XN_STATUS_OK)
00056     THROW_OPENNI_EXCEPTION("Could not open ONI file. Reason: %s", xnGetStatusString(status));
00057 
00058   status = context.FindExistingNode(XN_NODE_TYPE_DEPTH, depth_generator_);
00059   if (status != XN_STATUS_OK)
00060     THROW_OPENNI_EXCEPTION("could not find depth stream in file %s. Reason: %s", file_name.c_str(), xnGetStatusString(status));
00061   else
00062   {
00063     available_depth_modes_.push_back(getDepthOutputMode());
00064     depth_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIDepthDataAvailable, this, depth_callback_handle_);
00065   }
00066 
00067   status = context.FindExistingNode(XN_NODE_TYPE_IMAGE, image_generator_);
00068   if (status == XN_STATUS_OK)
00069   {
00070     available_image_modes_.push_back(getImageOutputMode());
00071     image_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIImageDataAvailable, this, image_callback_handle_);
00072   }
00073   
00074   status = context.FindExistingNode(XN_NODE_TYPE_IR, ir_generator_);
00075   if (status == XN_STATUS_OK)
00076     ir_generator_.RegisterToNewDataAvailable ((xn::StateChangedHandler)NewONIIRDataAvailable, this, ir_callback_handle_); 
00077   
00078   status = context.FindExistingNode(XN_NODE_TYPE_PLAYER, player_);
00079   if (status != XN_STATUS_OK)
00080     THROW_OPENNI_EXCEPTION("Failed to find player node: %s\n", xnGetStatusString(status));
00081 
00082   device_node_info_ = player_.GetInfo();
00083 
00084   Init ();
00085 
00086   player_.SetRepeat(repeat);
00087   if (streaming_)
00088     player_thread_ = boost::thread (&DeviceONI::PlayerThreadFunction, this);
00089 }
00090 
00091 DeviceONI::~DeviceONI() throw ()
00092 {
00093   if (streaming_)
00094   {
00095     quit_ = true;
00096     player_thread_.join();
00097   }
00098 }
00099 
00100 void DeviceONI::startImageStream () throw (OpenNIException)
00101 {
00102   if (hasImageStream() && !image_stream_running_)
00103     image_stream_running_ = true;
00104 }
00105 
00106 void DeviceONI::stopImageStream () throw (OpenNIException)
00107 {
00108   if (hasImageStream() && image_stream_running_)
00109     image_stream_running_ = false;
00110 }
00111 
00112 void DeviceONI::startDepthStream () throw (OpenNIException)
00113 {
00114   if (hasDepthStream() && !depth_stream_running_)
00115     depth_stream_running_ = true;
00116 }
00117 
00118 void DeviceONI::stopDepthStream () throw (OpenNIException)
00119 {
00120   if (hasDepthStream() && depth_stream_running_)
00121     depth_stream_running_ = false;
00122 }
00123 
00124 void DeviceONI::startIRStream () throw (OpenNIException)
00125 {
00126   if (hasIRStream() && !ir_stream_running_)
00127     ir_stream_running_ = true;
00128 }
00129 
00130 void DeviceONI::stopIRStream () throw (OpenNIException)
00131 {
00132   if (hasIRStream() && ir_stream_running_)
00133     ir_stream_running_ = false;
00134 }
00135 
00136 bool DeviceONI::isImageStreamRunning () const throw (OpenNIException)
00137 {
00138  return image_stream_running_;
00139 }
00140 
00141 bool DeviceONI::isDepthStreamRunning () const throw (OpenNIException)
00142 {
00143   return depth_stream_running_;
00144 }
00145 
00146 bool DeviceONI::isIRStreamRunning () const throw (OpenNIException)
00147 {
00148   return ir_stream_running_;
00149 }
00150 
00151 bool DeviceONI::trigger () throw (OpenNIException)
00152 {
00153   if (player_.IsEOF())
00154     return false;
00155   
00156   if (streaming_)
00157     THROW_OPENNI_EXCEPTION ("Virtual device is in streaming mode. Trigger not available.");
00158   
00159   player_.ReadNext();
00160   return true;
00161 }
00162 
00163 bool DeviceONI::isStreaming () const throw (OpenNIException)
00164 {
00165   return streaming_;
00166 }
00167 
00168 void DeviceONI::PlayerThreadFunction() throw (OpenNIException)
00169 {
00170   quit_ = false;
00171   while (!quit_)
00172     player_.ReadNext();
00173 }
00174 
00175 void __stdcall DeviceONI::NewONIDepthDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
00176 {
00177   DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
00178   if (device->depth_stream_running_)
00179     device->depth_condition_.notify_all ();
00180 }
00181 
00182 void __stdcall DeviceONI::NewONIImageDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
00183 {
00184   DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
00185   if (device->image_stream_running_)
00186     device->image_condition_.notify_all ();
00187 }
00188 
00189 void __stdcall DeviceONI::NewONIIRDataAvailable (xn::ProductionNode& node, void* cookie) throw ()
00190 {
00191   DeviceONI* device = reinterpret_cast<DeviceONI*>(cookie);
00192   if (device->ir_stream_running_)
00193     device->ir_condition_.notify_all ();
00194 }
00195 
00196 boost::shared_ptr<Image> DeviceONI::getCurrentImage(boost::shared_ptr<xn::ImageMetaData> image_meta_data) const throw ()
00197 {
00198   return boost::shared_ptr<Image > (new ImageRGB24(image_meta_data));
00199 }
00200 
00201 bool DeviceONI::isImageResizeSupported(unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const throw ()
00202 {
00203   return ImageRGB24::resizingSupported (input_width, input_height, output_width, output_height);
00204 }
00205 
00206 }
00207 
00208 


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Tue Apr 18 2017 02:14:10