openni_device.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011 Willow Garage, Inc.
5  * Suat Gedikli <gedikli@willowgarage.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef __OPENNI_IDEVICE_H__
39 #define __OPENNI_IDEVICE_H__
40 #include <map>
41 #include <vector>
42 #include <utility>
43 #include "openni_exception.h"
44 #include <XnCppWrapper.h>
45 #include <boost/noncopyable.hpp>
46 #include <boost/function.hpp>
47 #include <boost/thread.hpp>
48 #include <boost/thread/condition.hpp>
49 
51 
52 #ifndef _WIN32
53 #define __stdcall
54 #endif
55 
56 namespace openni_wrapper
57 {
58 class Image;
59 class DepthImage;
60 class IRImage;
66 class OpenNIDevice : public boost::noncopyable
67 {
68 public:
69  typedef boost::function<void(boost::shared_ptr<Image>, void* cookie) > ImageCallbackFunction;
70  typedef boost::function<void(boost::shared_ptr<DepthImage>, void* cookie) > DepthImageCallbackFunction;
71  typedef boost::function<void(boost::shared_ptr<IRImage>, void* cookie) > IRImageCallbackFunction;
72  typedef unsigned CallbackHandle;
73 
74 public:
75  virtual ~OpenNIDevice () throw ();
76  void shutdown ();
77 
78  virtual bool findCompatibleImageMode (const XnMapOutputMode& output_mode, XnMapOutputMode& mode ) const throw (OpenNIException);
79  virtual bool findCompatibleDepthMode (const XnMapOutputMode& output_mode, XnMapOutputMode& mode ) const throw (OpenNIException);
80 
81  virtual bool isImageModeSupported (const XnMapOutputMode& output_mode) const throw (OpenNIException);
82  virtual bool isDepthModeSupported (const XnMapOutputMode& output_mode) const throw (OpenNIException);
83 
84  virtual const XnMapOutputMode& getDefaultImageMode () const throw ();
85  virtual const XnMapOutputMode& getDefaultDepthMode () const throw ();
86  virtual const XnMapOutputMode& getDefaultIRMode () const throw ();
87 
88  virtual void setImageOutputMode (const XnMapOutputMode& output_mode) throw (OpenNIException);
89  virtual void setDepthOutputMode (const XnMapOutputMode& output_mode) throw (OpenNIException);
90  virtual void setIROutputMode (const XnMapOutputMode& output_mode) throw (OpenNIException);
91 
92  XnMapOutputMode getImageOutputMode () const throw (OpenNIException);
93  XnMapOutputMode getDepthOutputMode () const throw (OpenNIException);
94  XnMapOutputMode getIROutputMode () const throw (OpenNIException);
95 
96  virtual void setDepthRegistration (bool on_off) throw (OpenNIException);
97  bool isDepthRegistered () const throw (OpenNIException);
98  virtual bool isDepthRegistrationSupported () const throw (OpenNIException);
99 
100  virtual void setSynchronization (bool on_off) throw (OpenNIException);
101  virtual bool isSynchronized () const throw (OpenNIException);
102  virtual bool isSynchronizationSupported () const throw ();
103 
104  // just supported by primesense -> virtual
105  virtual bool isDepthCropped () const throw (OpenNIException);
106  virtual void setDepthCropping (unsigned x, unsigned y, unsigned width, unsigned height) throw (OpenNIException);
107  virtual bool isDepthCroppingSupported () const throw ();
108 
112  inline float getImageFocalLength (int output_x_resolution = 0) const throw ();
113 
117  inline float getDepthFocalLength (int output_x_resolution = 0) const throw ();
118  inline float getBaseline () const throw ();
119 
120  virtual void startImageStream () throw (OpenNIException);
121  virtual void stopImageStream () throw (OpenNIException);
122 
123  virtual void startDepthStream () throw (OpenNIException);
124  virtual void stopDepthStream () throw (OpenNIException);
125 
126  virtual void startIRStream () throw (OpenNIException);
127  virtual void stopIRStream () throw (OpenNIException);
128 
129  bool hasImageStream () const throw ();
130  bool hasDepthStream () const throw ();
131  bool hasIRStream () const throw ();
132 
133  virtual bool isImageStreamRunning () const throw (OpenNIException);
134  virtual bool isDepthStreamRunning () const throw (OpenNIException);
135  virtual bool isIRStreamRunning () const throw (OpenNIException);
136 
137  CallbackHandle registerImageCallback (const ImageCallbackFunction& callback, void* cookie = NULL) throw ();
138  template<typename T> CallbackHandle registerImageCallback (void (T::*callback)(boost::shared_ptr<Image>, void* cookie), T& instance, void* cookie = NULL) throw ();
139  bool unregisterImageCallback (const CallbackHandle& callbackHandle) throw ();
140 
141  CallbackHandle registerDepthCallback (const DepthImageCallbackFunction& callback, void* cookie = NULL) throw ();
142  template<typename T> CallbackHandle registerDepthCallback (void (T::*callback)(boost::shared_ptr<DepthImage>, void* cookie), T& instance, void* cookie = NULL) throw ();
143  bool unregisterDepthCallback (const CallbackHandle& callbackHandle) throw ();
144 
145  CallbackHandle registerIRCallback (const IRImageCallbackFunction& callback, void* cookie = NULL) throw ();
146  template<typename T> CallbackHandle registerIRCallback (void (T::*callback)(boost::shared_ptr<IRImage>, void* cookie), T& instance, void* cookie = NULL) throw ();
147  bool unregisterIRCallback (const CallbackHandle& callbackHandle) throw ();
148 
152  const char* getSerialNumber () throw ();
154  const char* getConnectionString () const throw ();
155 
156  const char* getVendorName () const throw ();
157  const char* getProductName () const throw ();
158  unsigned short getVendorID () const throw ();
159  unsigned short getProductID () const throw ();
160  unsigned char getBus () const throw ();
161  unsigned char getAddress () const throw ();
162 protected:
163  typedef boost::function<void(boost::shared_ptr<Image>) > ActualImageCallbackFunction;
164  typedef boost::function<void(boost::shared_ptr<DepthImage>) > ActualDepthImageCallbackFunction;
165  typedef boost::function<void(boost::shared_ptr<IRImage>) > ActualIRImageCallbackFunction;
166 
167  OpenNIDevice (xn::Context& context, const xn::NodeInfo& device_node, const xn::NodeInfo& image_node, const xn::NodeInfo& depth_node, const xn::NodeInfo& ir_node) throw (OpenNIException);
168  OpenNIDevice (xn::Context& context, const xn::NodeInfo& device_node, const xn::NodeInfo& depth_node, const xn::NodeInfo& ir_node) throw (OpenNIException);
169  OpenNIDevice (xn::Context& context) throw (OpenNIException);
170  static void __stdcall NewDepthDataAvailable (xn::ProductionNode& node, void* cookie) throw ();
171  static void __stdcall NewImageDataAvailable (xn::ProductionNode& node, void* cookie) throw ();
172  static void __stdcall NewIRDataAvailable (xn::ProductionNode& node, void* cookie) throw ();
173 
174  // This is a workaround, since in the NewDepthDataAvailable function WaitAndUpdateData leads to a dead-lock behaviour
175  // and retrieving image data without WaitAndUpdateData leads to incomplete images!!!
176  void ImageDataThreadFunction () throw (OpenNIException);
177  void DepthDataThreadFunction () throw (OpenNIException);
178  void IRDataThreadFunction () throw (OpenNIException);
179 
180  virtual bool isImageResizeSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const throw () = 0;
181 
182  void setRegistration (bool on_off) throw (OpenNIException);
183  virtual boost::shared_ptr<Image> getCurrentImage (boost::shared_ptr<xn::ImageMetaData> image_data) const throw () = 0;
184 
185  virtual void enumAvailableModes () throw (OpenNIException);
186  void Init () throw (OpenNIException);
187  // holds the callback functions together with custom data
188  // since same callback function can be registered multiple times with e.g. different custom data
189  // we use a map structure with a handle as the key
190  std::map< CallbackHandle, ActualImageCallbackFunction > image_callback_;
191  std::map< CallbackHandle, ActualDepthImageCallbackFunction > depth_callback_;
192  std::map< CallbackHandle, ActualIRImageCallbackFunction > ir_callback_;
193 
194  std::vector<XnMapOutputMode> available_image_modes_;
195  std::vector<XnMapOutputMode> available_depth_modes_;
196 
198  xn::Context& context_;
200  xn::NodeInfo device_node_info_;
201 
203  xn::DepthGenerator depth_generator_;
205  xn::ImageGenerator image_generator_;
207  xn::IRGenerator ir_generator_;
208 
209  XnCallbackHandle depth_callback_handle_;
210  XnCallbackHandle image_callback_handle_;
211  XnCallbackHandle ir_callback_handle_;
212 
216  float baseline_;
218  static const float rgb_focal_length_SXGA_;
219 
221  XnUInt64 shadow_value_;
224 
228 
229  bool quit_;
230  mutable boost::mutex image_mutex_;
231  mutable boost::mutex depth_mutex_;
232  mutable boost::mutex ir_mutex_;
233  boost::condition_variable image_condition_;
234  boost::condition_variable depth_condition_;
235  boost::condition_variable ir_condition_;
236  boost::thread_group data_threads_;
237 };
238 
239 float OpenNIDevice::getImageFocalLength (int output_x_resolution) const throw ()
240 {
241  if (output_x_resolution == 0)
242  output_x_resolution = getImageOutputMode ().nXRes;
243 
244  float scale = output_x_resolution / (float)XN_SXGA_X_RES;
245  return rgb_focal_length_SXGA_ * scale;
246 }
247 
248 float OpenNIDevice::getDepthFocalLength (int output_x_resolution) const throw ()
249 {
250  if (output_x_resolution == 0)
251  output_x_resolution = getDepthOutputMode ().nXRes;
252 
253  float scale = output_x_resolution / (float)XN_SXGA_X_RES;
254  if (isDepthRegistered ())
255  return rgb_focal_length_SXGA_ * scale;
256  else
257  return depth_focal_length_SXGA_ * scale;
258 }
259 
260 float OpenNIDevice::getBaseline () const throw ()
261 {
262  return baseline_;
263 }
264 
265 template<typename T> OpenNIDevice::CallbackHandle OpenNIDevice::registerImageCallback (void (T::*callback)(boost::shared_ptr<Image>, void* cookie), T& instance, void* custom_data) throw ()
266 {
267  image_callback_[image_callback_handle_counter_] = boost::bind (callback, boost::ref (instance), _1, custom_data);
269 }
270 
271 template<typename T> OpenNIDevice::CallbackHandle OpenNIDevice::registerDepthCallback (void (T::*callback)(boost::shared_ptr<DepthImage>, void* cookie), T& instance, void* custom_data) throw ()
272 {
273  depth_callback_[depth_callback_handle_counter_] = boost::bind ( callback, boost::ref (instance), _1, custom_data);
275 }
276 
277 template<typename T> OpenNIDevice::CallbackHandle OpenNIDevice::registerIRCallback (void (T::*callback)(boost::shared_ptr<IRImage>, void* cookie), T& instance, void* custom_data) throw ()
278 {
279  ir_callback_[ir_callback_handle_counter_] = boost::bind ( callback, boost::ref (instance), _1, custom_data);
281 }
282 
283 }
284 #endif // __OPENNI_IDEVICE_H__
virtual bool isImageResizeSupported(unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const =0
virtual bool isImageStreamRunning() const
float getImageFocalLength(int output_x_resolution=0) const
returns the focal length for the color camera in pixels. The pixels are assumed to be square...
boost::condition_variable depth_condition_
boost::function< void(boost::shared_ptr< IRImage >, void *cookie) > IRImageCallbackFunction
Definition: openni_device.h:71
unsigned short getVendorID() const
const char * getProductName() const
virtual void setSynchronization(bool on_off)
void setRegistration(bool on_off)
CallbackHandle registerIRCallback(const IRImageCallbackFunction &callback, void *cookie=NULL)
boost::function< void(boost::shared_ptr< Image >) > ActualImageCallbackFunction
std::map< CallbackHandle, ActualIRImageCallbackFunction > ir_callback_
virtual bool isDepthCropped() const
xn::IRGenerator ir_generator_
IR generator object.
OpenNIDevice::CallbackHandle image_callback_handle_counter_
virtual bool isDepthCroppingSupported() const
static void __stdcall NewIRDataAvailable(xn::ProductionNode &node, void *cookie)
Image class containing just a reference to image meta data. Thus this class just provides an interfac...
Definition: openni_image.h:54
virtual void setDepthCropping(unsigned x, unsigned y, unsigned width, unsigned height)
General exception class.
std::vector< XnMapOutputMode > available_image_modes_
virtual boost::shared_ptr< Image > getCurrentImage(boost::shared_ptr< xn::ImageMetaData > image_data) const =0
float baseline_
distance between the projector and the IR camera
bool unregisterImageCallback(const CallbackHandle &callbackHandle)
const char * getSerialNumber()
returns the serial number for device.
virtual bool isDepthStreamRunning() const
boost::condition_variable ir_condition_
boost::function< void(boost::shared_ptr< DepthImage >) > ActualDepthImageCallbackFunction
virtual void setImageOutputMode(const XnMapOutputMode &output_mode)
virtual bool isIRStreamRunning() const
virtual bool isDepthRegistrationSupported() const
virtual const XnMapOutputMode & getDefaultDepthMode() const
static void __stdcall NewImageDataAvailable(xn::ProductionNode &node, void *cookie)
virtual void setDepthOutputMode(const XnMapOutputMode &output_mode)
XnCallbackHandle ir_callback_handle_
boost::thread_group data_threads_
unsigned short getProductID() const
bool unregisterIRCallback(const CallbackHandle &callbackHandle)
xn::DepthGenerator depth_generator_
Depth generator object.
bool unregisterDepthCallback(const CallbackHandle &callbackHandle)
std::map< CallbackHandle, ActualImageCallbackFunction > image_callback_
#define __stdcall
Definition: openni_device.h:53
CallbackHandle registerDepthCallback(const DepthImageCallbackFunction &callback, void *cookie=NULL)
boost::condition_variable image_condition_
float depth_focal_length_SXGA_
focal length for IR camera producing depth information in native SXGA mode
xn::NodeInfo device_node_info_
node object for current device
virtual void setDepthRegistration(bool on_off)
XnCallbackHandle image_callback_handle_
virtual bool isSynchronizationSupported() const
static void __stdcall NewDepthDataAvailable(xn::ProductionNode &node, void *cookie)
XnMapOutputMode getIROutputMode() const
XnCallbackHandle depth_callback_handle_
xn::ImageGenerator image_generator_
Image generator object.
boost::function< void(boost::shared_ptr< IRImage >) > ActualIRImageCallbackFunction
boost::function< void(boost::shared_ptr< DepthImage >, void *cookie) > DepthImageCallbackFunction
Definition: openni_device.h:70
boost::function< void(boost::shared_ptr< Image >, void *cookie) > ImageCallbackFunction
Definition: openni_device.h:69
unsigned char getBus() const
std::map< CallbackHandle, ActualDepthImageCallbackFunction > depth_callback_
unsigned char getAddress() const
XnMapOutputMode getImageOutputMode() const
const char * getConnectionString() const
returns the connectionstring for current device, which has following format vendorID/productID@BusID/...
CallbackHandle registerImageCallback(const ImageCallbackFunction &callback, void *cookie=NULL)
virtual const XnMapOutputMode & getDefaultImageMode() const
const char * getVendorName() const
virtual void setIROutputMode(const XnMapOutputMode &output_mode)
float getDepthFocalLength(int output_x_resolution=0) const
returns the focal length for the IR camera in pixels. The pixels are assumed to be square...
Class representing an astract device for Primesense or MS Kinect devices.
Definition: openni_device.h:66
virtual bool isSynchronized() const
XnMapOutputMode getDepthOutputMode() const
xn::Context & context_
context to OpenNI driver
This class provides methods to fill a depth or disparity image.
virtual bool findCompatibleDepthMode(const XnMapOutputMode &output_mode, XnMapOutputMode &mode) const
OpenNIDevice::CallbackHandle depth_callback_handle_counter_
virtual bool isDepthModeSupported(const XnMapOutputMode &output_mode) const
virtual bool findCompatibleImageMode(const XnMapOutputMode &output_mode, XnMapOutputMode &mode) const
std::vector< XnMapOutputMode > available_depth_modes_
Class containing just a reference to IR meta data.
OpenNIDevice::CallbackHandle ir_callback_handle_counter_
def callback(data, args)
Definition: test_launch.py:14
virtual bool isImageModeSupported(const XnMapOutputMode &output_mode) const
virtual const XnMapOutputMode & getDefaultIRMode() const
static const float rgb_focal_length_SXGA_
focal length for regular camera producing color images in native SXGA mode


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Mon Jun 10 2019 14:15:53