Point Cloud Library (PCL)  1.9.1-dev
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 #pragma once
39 
40 #include <pcl/pcl_config.h>
41 
42 #include <pcl/common/io.h>
43 #include <pcl/io/grabber.h>
44 #include <pcl/io/file_grabber.h>
45 #include <pcl/common/time_trigger.h>
46 #include <pcl/conversions.h>
47 
48 #ifdef HAVE_OPENNI
49 #include <pcl/io/openni_camera/openni_image.h>
50 #include <pcl/io/openni_camera/openni_image_rgb24.h>
51 #include <pcl/io/openni_camera/openni_depth_image.h>
52 #endif
53 
54 #include <boost/shared_ptr.hpp>
55 
56 #include <string>
57 #include <vector>
58 
59 namespace pcl
60 {
61  /** \brief Base class for PCD file grabber.
62  * \ingroup io
63  */
64  class PCL_EXPORTS PCDGrabberBase : public Grabber
65  {
66  public:
67  /** \brief Constructor taking just one PCD file or one TAR file containing multiple PCD files.
68  * \param[in] pcd_file path to the PCD file
69  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next PCD in the list.
70  * \param[in] repeat whether to play PCD file in an endless loop or not.
71  */
72  PCDGrabberBase (const std::string& pcd_file, float frames_per_second, bool repeat);
73 
74  /** \brief Constructor taking a list of paths to PCD files, that are played in the order they appear in the list.
75  * \param[in] pcd_files vector of paths to PCD files.
76  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next PCD in the list.
77  * \param[in] repeat whether to play PCD file in an endless loop or not.
78  */
79  PCDGrabberBase (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat);
80 
81  /** \brief Copy constructor.
82  * \param[in] src the PCD Grabber base object to copy into this
83  */
84  PCDGrabberBase (const PCDGrabberBase &src) : impl_ ()
85  {
86  *this = src;
87  }
88 
89  /** \brief Copy operator.
90  * \param[in] src the PCD Grabber base object to copy into this
91  */
93  operator = (const PCDGrabberBase &src)
94  {
95  impl_ = src.impl_;
96  return (*this);
97  }
98 
99  /** \brief Virtual destructor. */
100  ~PCDGrabberBase () throw ();
101 
102  /** \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. */
103  void
104  start () override;
105 
106  /** \brief Stops playing the list of PCD files if frames_per_second is > 0. Otherwise the method has no effect. */
107  void
108  stop () override;
109 
110  /** \brief Triggers a callback with new data */
111  virtual void
112  trigger ();
113 
114  /** \brief Indicates whether the grabber is streaming or not.
115  * \return true if grabber is started and hasn't run out of PCD files.
116  */
117  bool
118  isRunning () const override;
119 
120  /** \return The name of the grabber */
121  std::string
122  getName () const override;
123 
124  /** \brief Rewinds to the first PCD file in the list.*/
125  virtual void
126  rewind ();
127 
128  /** \brief Returns the frames_per_second. 0 if grabber is trigger-based */
129  float
130  getFramesPerSecond () const override;
131 
132  /** \brief Returns whether the repeat flag is on */
133  bool
134  isRepeatOn () const;
135 
136  /** \brief Get cloud (in ROS form) at a particular location */
137  bool
138  getCloudAt (size_t idx,
139  pcl::PCLPointCloud2 &blob,
140  Eigen::Vector4f &origin,
141  Eigen::Quaternionf &orientation) const;
142 
143  /** \brief Returns the size */
144  size_t
145  numFrames () const;
146 
147  private:
148  virtual void
149  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const = 0;
150 
151  // to separate and hide the implementation from interface: PIMPL
152  struct PCDGrabberImpl;
153  PCDGrabberImpl* impl_;
154  };
155 
156  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
157  template <typename T> class PointCloud;
158  template <typename PointT> class PCDGrabber : public PCDGrabberBase, public FileGrabber<PointT>
159  {
160  public:
161  using Ptr = boost::shared_ptr<PCDGrabber>;
162 
163  PCDGrabber (const std::string& pcd_path, float frames_per_second = 0, bool repeat = false);
164  PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second = 0, bool repeat = false);
165 
166  /** \brief Virtual destructor. */
167  ~PCDGrabber () throw ()
168  {
169  stop ();
170  }
171 
172  // Inherited from FileGrabber
173  const typename pcl::PointCloud<PointT>::ConstPtr
174  operator[] (size_t idx) const override;
175 
176  // Inherited from FileGrabber
177  size_t
178  size () const override;
179  protected:
180 
181  void
182  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const override;
183 
184  boost::signals2::signal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>* signal_;
185  boost::signals2::signal<void (const std::string&)>* file_name_signal_;
186 
187 #ifdef HAVE_OPENNI
188  boost::signals2::signal<void (const openni_wrapper::DepthImage::Ptr&)>* depth_image_signal_;
189  boost::signals2::signal<void (const openni_wrapper::Image::Ptr&)>* image_signal_;
190  boost::signals2::signal<void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)>* image_depth_image_signal_;
191 #endif
192  };
193 
194  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
195  template<typename PointT>
196  PCDGrabber<PointT>::PCDGrabber (const std::string& pcd_path, float frames_per_second, bool repeat)
197  : PCDGrabberBase (pcd_path, frames_per_second, repeat)
198  {
199  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
200  file_name_signal_ = createSignal<void (const std::string&)>();
201 #ifdef HAVE_OPENNI
202  depth_image_signal_ = createSignal <void (const openni_wrapper::DepthImage::Ptr&)> ();
203  image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&)> ();
204  image_depth_image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)> ();
205 #endif
206  }
207 
208  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
209  template<typename PointT>
210  PCDGrabber<PointT>::PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat)
211  : PCDGrabberBase (pcd_files, frames_per_second, repeat), signal_ ()
212  {
213  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
214  file_name_signal_ = createSignal<void (const std::string&)>();
215 #ifdef HAVE_OPENNI
216  depth_image_signal_ = createSignal <void (const openni_wrapper::DepthImage::Ptr&)> ();
217  image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&)> ();
218  image_depth_image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)> ();
219 #endif
220  }
221 
222  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
223  template<typename PointT> const typename pcl::PointCloud<PointT>::ConstPtr
225  {
226  pcl::PCLPointCloud2 blob;
227  Eigen::Vector4f origin;
228  Eigen::Quaternionf orientation;
229  getCloudAt (idx, blob, origin, orientation);
231  pcl::fromPCLPointCloud2 (blob, *cloud);
232  cloud->sensor_origin_ = origin;
233  cloud->sensor_orientation_ = orientation;
234  return (cloud);
235  }
236 
237  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
238  template <typename PointT> size_t
240  {
241  return (numFrames ());
242  }
243 
244  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
245  template<typename PointT> void
246  PCDGrabber<PointT>::publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const
247  {
249  pcl::fromPCLPointCloud2 (blob, *cloud);
250  cloud->sensor_origin_ = origin;
251  cloud->sensor_orientation_ = orientation;
252 
253  signal_->operator () (cloud);
254  if (file_name_signal_->num_slots() > 0)
255  file_name_signal_->operator()(file_name);
256 
257 #ifdef HAVE_OPENNI
258  // If dataset is not organized, return
259  if (!cloud->isOrganized ())
260  return;
261 
262  boost::shared_ptr<xn::DepthMetaData> depth_meta_data (new xn::DepthMetaData);
263  depth_meta_data->AllocateData (cloud->width, cloud->height);
264  XnDepthPixel* depth_map = depth_meta_data->WritableData ();
265  uint32_t k = 0;
266  for (uint32_t i = 0; i < cloud->height; ++i)
267  for (uint32_t j = 0; j < cloud->width; ++j)
268  {
269  depth_map[k] = static_cast<XnDepthPixel> ((*cloud)[k].z * 1000);
270  ++k;
271  }
272 
273  openni_wrapper::DepthImage::Ptr depth_image (new openni_wrapper::DepthImage (depth_meta_data, 0.075f, 525, 0, 0));
274  if (depth_image_signal_->num_slots() > 0)
275  depth_image_signal_->operator()(depth_image);
276 
277  // ---[ RGB special case
278  std::vector<pcl::PCLPointField> fields;
279  int rgba_index = pcl::getFieldIndex (*cloud, "rgb", fields);
280  if (rgba_index == -1)
281  rgba_index = pcl::getFieldIndex (*cloud, "rgba", fields);
282  if (rgba_index >= 0)
283  {
284  rgba_index = fields[rgba_index].offset;
285 
286  boost::shared_ptr<xn::ImageMetaData> image_meta_data (new xn::ImageMetaData);
287  image_meta_data->AllocateData (cloud->width, cloud->height, XN_PIXEL_FORMAT_RGB24);
288  XnRGB24Pixel* image_map = image_meta_data->WritableRGB24Data ();
289  k = 0;
290  for (uint32_t i = 0; i < cloud->height; ++i)
291  {
292  for (uint32_t j = 0; j < cloud->width; ++j)
293  {
294  // Fill r/g/b data, assuming that the order is BGRA
295  pcl::RGB rgb;
296  memcpy (&rgb, reinterpret_cast<const char*> (&cloud->points[k]) + rgba_index, sizeof (RGB));
297  image_map[k].nRed = static_cast<XnUInt8> (rgb.r);
298  image_map[k].nGreen = static_cast<XnUInt8> (rgb.g);
299  image_map[k].nBlue = static_cast<XnUInt8> (rgb.b);
300  ++k;
301  }
302  }
303 
304  openni_wrapper::Image::Ptr image (new openni_wrapper::ImageRGB24 (image_meta_data));
305  if (image_signal_->num_slots() > 0)
306  image_signal_->operator()(image);
307 
308  if (image_depth_image_signal_->num_slots() > 0)
309  image_depth_image_signal_->operator()(image, depth_image, 1.0f / 525.0f);
310  }
311 #endif
312  }
313 }
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map...
Definition: conversions.h:168
boost::signals2::signal< void(const openni_wrapper::DepthImage::Ptr &)> * depth_image_signal_
Definition: pcd_grabber.h:188
PCDGrabberBase(const PCDGrabberBase &src)
Copy constructor.
Definition: pcd_grabber.h:84
int getFieldIndex(const pcl::PCLPointCloud2 &cloud, const std::string &field_name)
Get the index of a specified field (i.e., dimension/channel)
Definition: io.h:58
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
boost::signals2::signal< void(const openni_wrapper::Image::Ptr &, const openni_wrapper::DepthImage::Ptr &, float constant)> * image_depth_image_signal_
Definition: pcd_grabber.h:190
boost::signals2::signal< void(const openni_wrapper::Image::Ptr &)> * image_signal_
Definition: pcd_grabber.h:189
boost::signals2::signal< void(const typename pcl::PointCloud< PointT >::ConstPtr &)> * signal_
Definition: pcd_grabber.h:184
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
boost::shared_ptr< DepthImage > Ptr
boost::shared_ptr< Image > Ptr
Definition: openni_image.h:61
Grabber interface for PCL 1.x device drivers.
Definition: grabber.h:57
PCDGrabber(const std::string &pcd_path, float frames_per_second=0, bool repeat=false)
Definition: pcd_grabber.h:196
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:416
~PCDGrabber()
Virtual destructor.
Definition: pcd_grabber.h:167
This class provides methods to fill a depth or disparity image.
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:422
A structure representing RGB color information.
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:414
void publish(const pcl::PCLPointCloud2 &blob, const Eigen::Vector4f &origin, const Eigen::Quaternionf &orientation, const std::string &file_name) const override
Definition: pcd_grabber.h:246
boost::shared_ptr< PCDGrabber > Ptr
Definition: pcd_grabber.h:161
FileGrabber provides a container-style interface for grabbers which operate on fixed-size input...
Definition: file_grabber.h:53
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:424
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
Base class for PCD file grabber.
Definition: pcd_grabber.h:64
This class provides methods to fill a RGB or Grayscale image buffer from underlying RGB24 image...
PointCloud represents the base class in PCL for storing collections of 3D points. ...
boost::signals2::signal< void(const std::string &)> * file_name_signal_
Definition: pcd_grabber.h:185
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:430
bool isOrganized() const
Return whether a dataset is organized (e.g., arranged in a structured grid).
Definition: point_cloud.h:332
size_t size() const override
size Returns the number of clouds currently loaded by the grabber
Definition: pcd_grabber.h:239
const pcl::PointCloud< PointT >::ConstPtr operator[](size_t idx) const override
operator[] Returns the idx-th cloud in the dataset, without bounds checking.
Definition: pcd_grabber.h:224
bool getCloudAt(size_t idx, pcl::PCLPointCloud2 &blob, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation) const
Get cloud (in ROS form) at a particular location.
size_t numFrames() const
Returns the size.