Point Cloud Library (PCL)  1.7.0
pcd_grabber.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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 the copyright holder(s) 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 
40 #ifndef PCL_IO_PCD_GRABBER_H_
41 #define PCL_IO_PCD_GRABBER_H_
42 
43 #include <pcl/common/io.h>
44 #include <pcl/io/grabber.h>
45 #include <pcl/io/file_grabber.h>
46 #include <pcl/common/time_trigger.h>
47 #include <string>
48 #include <vector>
49 #include <pcl/conversions.h>
50 
51 #ifdef HAVE_OPENNI
52 #include <pcl/io/openni_camera/openni_image.h>
53 #include <pcl/io/openni_camera/openni_image_rgb24.h>
54 #include <pcl/io/openni_camera/openni_depth_image.h>
55 #endif
56 
57 namespace pcl
58 {
59  /** \brief Base class for PCD file grabber.
60  * \ingroup io
61  */
62  class PCL_EXPORTS PCDGrabberBase : public Grabber
63  {
64  public:
65  /** \brief Constructor taking just one PCD file or one TAR file containing multiple PCD files.
66  * \param[in] pcd_file path to the PCD file
67  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next PCD in the list.
68  * \param[in] repeat whether to play PCD file in an endless loop or not.
69  */
70  PCDGrabberBase (const std::string& pcd_file, float frames_per_second, bool repeat);
71 
72  /** \brief Constructor taking a list of paths to PCD files, that are played in the order they appear in the list.
73  * \param[in] pcd_files vector of paths to PCD files.
74  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next PCD in the list.
75  * \param[in] repeat whether to play PCD file in an endless loop or not.
76  */
77  PCDGrabberBase (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat);
78 
79  /** \brief Copy constructor.
80  * \param[in] src the PCD Grabber base object to copy into this
81  */
82  PCDGrabberBase (const PCDGrabberBase &src) : Grabber (), impl_ ()
83  {
84  *this = src;
85  }
86 
87  /** \brief Copy operator.
88  * \param[in] src the PCD Grabber base object to copy into this
89  */
91  operator = (const PCDGrabberBase &src)
92  {
93  impl_ = src.impl_;
94  return (*this);
95  }
96 
97  /** \brief Virtual destructor. */
98  virtual ~PCDGrabberBase () throw ();
99 
100  /** \brief Starts playing the list of PCD files if frames_per_second is > 0. Otherwise it works as a trigger: publishes only the next PCD file in the list. */
101  virtual void
102  start ();
103 
104  /** \brief Stops playing the list of PCD files if frames_per_second is > 0. Otherwise the method has no effect. */
105  virtual void
106  stop ();
107 
108  /** \brief Triggers a callback with new data */
109  virtual void
110  trigger ();
111 
112  /** \brief Indicates whether the grabber is streaming or not.
113  * \return true if grabber is started and hasn't run out of PCD files.
114  */
115  virtual bool
116  isRunning () const;
117 
118  /** \return The name of the grabber */
119  virtual std::string
120  getName () const;
121 
122  /** \brief Rewinds to the first PCD file in the list.*/
123  virtual void
124  rewind ();
125 
126  /** \brief Returns the frames_per_second. 0 if grabber is trigger-based */
127  virtual float
128  getFramesPerSecond () const;
129 
130  /** \brief Returns whether the repeat flag is on */
131  bool
132  isRepeatOn () const;
133 
134  /** \brief Get cloud (in ROS form) at a particular location */
135  bool
136  getCloudAt (size_t idx,
137  pcl::PCLPointCloud2 &blob,
138  Eigen::Vector4f &origin,
139  Eigen::Quaternionf &orientation) const;
140 
141  /** \brief Returns the size */
142  size_t
143  numFrames () const;
144 
145  private:
146  virtual void
147  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const = 0;
148 
149  // to separate and hide the implementation from interface: PIMPL
150  struct PCDGrabberImpl;
151  PCDGrabberImpl* impl_;
152  };
153 
154  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
155  template <typename T> class PointCloud;
156  template <typename PointT> class PCDGrabber : public PCDGrabberBase, public FileGrabber<PointT>
157  {
158  public:
159  PCDGrabber (const std::string& pcd_path, float frames_per_second = 0, bool repeat = false);
160  PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second = 0, bool repeat = false);
161 
162  /** \brief Virtual destructor. */
163  virtual ~PCDGrabber () throw () {}
164 
165  // Inherited from FileGrabber
166  const boost::shared_ptr< const pcl::PointCloud<PointT> >
167  operator[] (size_t idx) const;
168 
169  // Inherited from FileGrabber
170  size_t
171  size () const;
172  protected:
173 
174  virtual void
175  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const;
176 
177  boost::signals2::signal<void (const boost::shared_ptr<const pcl::PointCloud<PointT> >&)>* signal_;
178 
179 #ifdef HAVE_OPENNI
180  boost::signals2::signal<void (const boost::shared_ptr<openni_wrapper::DepthImage>&)>* depth_image_signal_;
181  boost::signals2::signal<void (const boost::shared_ptr<openni_wrapper::Image>&)>* image_signal_;
182  boost::signals2::signal<void (const boost::shared_ptr<openni_wrapper::Image>&, const boost::shared_ptr<openni_wrapper::DepthImage>&, float constant)>* image_depth_image_signal_;
183 #endif
184  };
185 
186  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
187  template<typename PointT>
188  PCDGrabber<PointT>::PCDGrabber (const std::string& pcd_path, float frames_per_second, bool repeat)
189  : PCDGrabberBase (pcd_path, frames_per_second, repeat)
190  {
191  signal_ = createSignal<void (const boost::shared_ptr<const pcl::PointCloud<PointT> >&)>();
192 #ifdef HAVE_OPENNI
193  depth_image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::DepthImage>&)> ();
194  image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::Image>&)> ();
195  image_depth_image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::Image>&, const boost::shared_ptr<openni_wrapper::DepthImage>&, float constant)> ();
196 #endif
197  }
198 
199  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
200  template<typename PointT>
201  PCDGrabber<PointT>::PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat)
202  : PCDGrabberBase (pcd_files, frames_per_second, repeat), signal_ ()
203  {
204  signal_ = createSignal<void (const boost::shared_ptr<const pcl::PointCloud<PointT> >&)>();
205 #ifdef HAVE_OPENNI
206  depth_image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::DepthImage>&)> ();
207  image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::Image>&)> ();
208  image_depth_image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::Image>&, const boost::shared_ptr<openni_wrapper::DepthImage>&, float constant)> ();
209 #endif
210  }
211 
212  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
213  template<typename PointT> const boost::shared_ptr< const pcl::PointCloud<PointT> >
215  {
216  pcl::PCLPointCloud2 blob;
217  Eigen::Vector4f origin;
218  Eigen::Quaternionf orientation;
219  getCloudAt (idx, blob, origin, orientation);
221  pcl::fromPCLPointCloud2 (blob, *cloud);
222  cloud->sensor_origin_ = origin;
223  cloud->sensor_orientation_ = orientation;
224  return (cloud);
225  }
226 
227  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
228  template <typename PointT> size_t
230  {
231  return (numFrames ());
232  }
233 
234  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
235  template<typename PointT> void
236  PCDGrabber<PointT>::publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const
237  {
239  pcl::fromPCLPointCloud2 (blob, *cloud);
240  cloud->sensor_origin_ = origin;
241  cloud->sensor_orientation_ = orientation;
242 
243  signal_->operator () (cloud);
244 
245 #ifdef HAVE_OPENNI
246  // If dataset is not organized, return
247  if (!cloud->isOrganized ())
248  return;
249 
250  boost::shared_ptr<xn::DepthMetaData> depth_meta_data (new xn::DepthMetaData);
251  depth_meta_data->AllocateData (cloud->width, cloud->height);
252  XnDepthPixel* depth_map = depth_meta_data->WritableData ();
253  uint32_t k = 0;
254  for (uint32_t i = 0; i < cloud->height; ++i)
255  for (uint32_t j = 0; j < cloud->width; ++j)
256  {
257  depth_map[k] = static_cast<XnDepthPixel> ((*cloud)[k].z * 1000);
258  ++k;
259  }
260 
261  boost::shared_ptr<openni_wrapper::DepthImage> depth_image (new openni_wrapper::DepthImage (depth_meta_data, 0.075f, 525, 0, 0));
262  if (depth_image_signal_->num_slots() > 0)
263  depth_image_signal_->operator()(depth_image);
264 
265  // ---[ RGB special case
266  std::vector<pcl::PCLPointField> fields;
267  int rgba_index = pcl::getFieldIndex (*cloud, "rgb", fields);
268  if (rgba_index == -1)
269  rgba_index = pcl::getFieldIndex (*cloud, "rgba", fields);
270  if (rgba_index >= 0)
271  {
272  rgba_index = fields[rgba_index].offset;
273 
274  boost::shared_ptr<xn::ImageMetaData> image_meta_data (new xn::ImageMetaData);
275  image_meta_data->AllocateData (cloud->width, cloud->height, XN_PIXEL_FORMAT_RGB24);
276  XnRGB24Pixel* image_map = image_meta_data->WritableRGB24Data ();
277  k = 0;
278  for (uint32_t i = 0; i < cloud->height; ++i)
279  {
280  for (uint32_t j = 0; j < cloud->width; ++j)
281  {
282  // Fill r/g/b data, assuming that the order is BGRA
283  pcl::RGB rgb;
284  memcpy (&rgb, reinterpret_cast<const char*> (&cloud->points[k]) + rgba_index, sizeof (RGB));
285  image_map[k].nRed = static_cast<XnUInt8> (rgb.r);
286  image_map[k].nGreen = static_cast<XnUInt8> (rgb.g);
287  image_map[k].nBlue = static_cast<XnUInt8> (rgb.b);
288  ++k;
289  }
290  }
291 
292  boost::shared_ptr<openni_wrapper::Image> image (new openni_wrapper::ImageRGB24 (image_meta_data));
293  if (image_signal_->num_slots() > 0)
294  image_signal_->operator()(image);
295 
296  if (image_depth_image_signal_->num_slots() > 0)
297  image_depth_image_signal_->operator()(image, depth_image, 1.0f / 525.0f);
298  }
299 #endif
300  }
301 }
302 #endif