Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
openni_grabber.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2011, Willow Garage, Inc.
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 #include <pcl/pcl_config.h>
39 #ifdef HAVE_OPENNI
40 
41 #ifndef __PCL_IO_OPENNI_GRABBER__
42 #define __PCL_IO_OPENNI_GRABBER__
43 
44 #include <Eigen/Core>
45 #include <pcl/io/grabber.h>
51 #include <string>
52 #include <deque>
53 #include <boost/thread/mutex.hpp>
55 
56 namespace pcl
57 {
58  struct PointXYZ;
59  struct PointXYZRGB;
60  struct PointXYZRGBA;
61  struct PointXYZI;
62  template <typename T> class PointCloud;
63 
68  class PCL_EXPORTS OpenNIGrabber : public Grabber
69  {
70  public:
71 
72  typedef enum
73  {
74  OpenNI_Default_Mode = 0, // This can depend on the device. For now all devices (PSDK, Xtion, Kinect) its VGA@30Hz
75  OpenNI_SXGA_15Hz = 1, // Only supported by the Kinect
76  OpenNI_VGA_30Hz = 2, // Supported by PSDK, Xtion and Kinect
77  OpenNI_VGA_25Hz = 3, // Supportged by PSDK and Xtion
78  OpenNI_QVGA_25Hz = 4, // Supported by PSDK and Xtion
79  OpenNI_QVGA_30Hz = 5, // Supported by PSDK, Xtion and Kinect
80  OpenNI_QVGA_60Hz = 6, // Supported by PSDK and Xtion
81  OpenNI_QQVGA_25Hz = 7, // Not supported -> using software downsampling (only for integer scale factor and only NN)
82  OpenNI_QQVGA_30Hz = 8, // Not supported -> using software downsampling (only for integer scale factor and only NN)
83  OpenNI_QQVGA_60Hz = 9 // Not supported -> using software downsampling (only for integer scale factor and only NN)
84  } Mode;
85 
86  //define callback signature typedefs
87  typedef void (sig_cb_openni_image) (const boost::shared_ptr<openni_wrapper::Image>&);
88  typedef void (sig_cb_openni_depth_image) (const boost::shared_ptr<openni_wrapper::DepthImage>&);
89  typedef void (sig_cb_openni_ir_image) (const boost::shared_ptr<openni_wrapper::IRImage>&);
90  typedef void (sig_cb_openni_image_depth_image) (const boost::shared_ptr<openni_wrapper::Image>&, const boost::shared_ptr<openni_wrapper::DepthImage>&, float constant) ;
91  typedef void (sig_cb_openni_ir_depth_image) (const boost::shared_ptr<openni_wrapper::IRImage>&, const boost::shared_ptr<openni_wrapper::DepthImage>&, float constant) ;
92  typedef void (sig_cb_openni_point_cloud) (const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZ> >&);
93  typedef void (sig_cb_openni_point_cloud_rgb) (const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGB> >&);
94  typedef void (sig_cb_openni_point_cloud_rgba) (const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZRGBA> >&);
95  typedef void (sig_cb_openni_point_cloud_i) (const boost::shared_ptr<const pcl::PointCloud<pcl::PointXYZI> >&);
96  typedef void (sig_cb_openni_point_cloud_eigen) (const boost::shared_ptr<const pcl::PointCloud<Eigen::MatrixXf> >&);
97 
98  public:
104  OpenNIGrabber (const std::string& device_id = "",
105  const Mode& depth_mode = OpenNI_Default_Mode,
106  const Mode& image_mode = OpenNI_Default_Mode);
107 
109  virtual ~OpenNIGrabber () throw ();
110 
112  virtual void
113  start ();
114 
116  virtual void
117  stop ();
118 
120  virtual bool
121  isRunning () const;
122 
123  virtual std::string
124  getName () const;
125 
127  virtual float
128  getFramesPerSecond () const;
129 
131  inline boost::shared_ptr<openni_wrapper::OpenNIDevice>
132  getDevice () const;
133 
135  std::vector<std::pair<int, XnMapOutputMode> >
136  getAvailableDepthModes () const;
137 
139  std::vector<std::pair<int, XnMapOutputMode> >
140  getAvailableImageModes () const;
141 
142  private:
144  void
145  onInit (const std::string& device_id, const Mode& depth_mode, const Mode& image_mode);
146 
148  void
149  setupDevice (const std::string& device_id, const Mode& depth_mode, const Mode& image_mode);
150 
152  void
153  updateModeMaps ();
154 
156  void
157  startSynchronization ();
158 
160  void
161  stopSynchronization ();
162 
164  bool
165  mapConfigMode2XnMode (int mode, XnMapOutputMode &xnmode) const;
166 
167  // callback methods
169  void
170  imageCallback (boost::shared_ptr<openni_wrapper::Image> image, void* cookie);
171 
173  void
174  depthCallback (boost::shared_ptr<openni_wrapper::DepthImage> depth_image, void* cookie);
175 
177  void
178  irCallback (boost::shared_ptr<openni_wrapper::IRImage> ir_image, void* cookie);
179 
181  void
182  imageDepthImageCallback (const boost::shared_ptr<openni_wrapper::Image> &image,
183  const boost::shared_ptr<openni_wrapper::DepthImage> &depth_image);
184 
186  void
187  irDepthImageCallback (const boost::shared_ptr<openni_wrapper::IRImage> &image,
188  const boost::shared_ptr<openni_wrapper::DepthImage> &depth_image);
189 
191  virtual void
192  signalsChanged ();
193 
194  // helper methods
195 
197  virtual inline void
198  checkImageAndDepthSynchronizationRequired ();
199 
201  virtual inline void
202  checkImageStreamRequired ();
203 
205  virtual inline void
206  checkDepthStreamRequired ();
207 
209  virtual inline void
210  checkIRStreamRequired ();
211 
213  boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> >
214  convertToXYZPointCloud (const boost::shared_ptr<openni_wrapper::DepthImage> &depth) const;
215 
217  template <typename PointT> typename pcl::PointCloud<PointT>::Ptr
218  convertToXYZRGBPointCloud (const boost::shared_ptr<openni_wrapper::Image> &image,
219  const boost::shared_ptr<openni_wrapper::DepthImage> &depth_image) const;
221  boost::shared_ptr<pcl::PointCloud<pcl::PointXYZI> >
222  convertToXYZIPointCloud (const boost::shared_ptr<openni_wrapper::IRImage> &image,
223  const boost::shared_ptr<openni_wrapper::DepthImage> &depth_image) const;
224 
225  Synchronizer<boost::shared_ptr<openni_wrapper::Image>, boost::shared_ptr<openni_wrapper::DepthImage> > rgb_sync_;
226  Synchronizer<boost::shared_ptr<openni_wrapper::IRImage>, boost::shared_ptr<openni_wrapper::DepthImage> > ir_sync_;
227 
233  boost::shared_ptr<pcl::PointCloud<Eigen::MatrixXf> >
234  convertToEigenPointCloud (const boost::shared_ptr<openni_wrapper::Image> &image,
235  const boost::shared_ptr<openni_wrapper::DepthImage> &depth_image) const;
236 
238  boost::shared_ptr<openni_wrapper::OpenNIDevice> device_;
239 
240  std::string rgb_frame_id_;
241  std::string depth_frame_id_;
242  unsigned image_width_;
243  unsigned image_height_;
244  unsigned depth_width_;
245  unsigned depth_height_;
246 
247  bool image_required_;
248  bool depth_required_;
249  bool ir_required_;
250  bool sync_required_;
251 
252  boost::signals2::signal<sig_cb_openni_image>* image_signal_;
253  boost::signals2::signal<sig_cb_openni_depth_image>* depth_image_signal_;
254  boost::signals2::signal<sig_cb_openni_ir_image>* ir_image_signal_;
255  boost::signals2::signal<sig_cb_openni_image_depth_image>* image_depth_image_signal_;
256  boost::signals2::signal<sig_cb_openni_ir_depth_image>* ir_depth_image_signal_;
257  boost::signals2::signal<sig_cb_openni_point_cloud>* point_cloud_signal_;
258  boost::signals2::signal<sig_cb_openni_point_cloud_i>* point_cloud_i_signal_;
259  boost::signals2::signal<sig_cb_openni_point_cloud_rgb>* point_cloud_rgb_signal_;
260  boost::signals2::signal<sig_cb_openni_point_cloud_rgba>* point_cloud_rgba_signal_;
261  boost::signals2::signal<sig_cb_openni_point_cloud_eigen>* point_cloud_eigen_signal_;
262 
263  struct modeComp
264  {
265 
266  bool operator () (const XnMapOutputMode& mode1, const XnMapOutputMode & mode2) const
267  {
268  if (mode1.nXRes < mode2.nXRes)
269  return true;
270  else if (mode1.nXRes > mode2.nXRes)
271  return false;
272  else if (mode1.nYRes < mode2.nYRes)
273  return true;
274  else if (mode1.nYRes > mode2.nYRes)
275  return false;
276  else if (mode1.nFPS < mode2.nFPS)
277  return true;
278  else
279  return false;
280  }
281  } ;
282  std::map<int, XnMapOutputMode> config2xn_map_;
283 
284  openni_wrapper::OpenNIDevice::CallbackHandle depth_callback_handle;
285  openni_wrapper::OpenNIDevice::CallbackHandle image_callback_handle;
286  openni_wrapper::OpenNIDevice::CallbackHandle ir_callback_handle;
287  bool running_;
288 
289  public:
290  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
291  };
292 
293  boost::shared_ptr<openni_wrapper::OpenNIDevice>
294  OpenNIGrabber::getDevice () const
295  {
296  return device_;
297  }
298 
299 } // namespace pcl
300 #endif // __PCL_IO_OPENNI_GRABBER__
301 #endif // HAVE_OPENNI