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  */
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 (std::size_t idx,
139  pcl::PCLPointCloud2 &blob,
140  Eigen::Vector4f &origin,
141  Eigen::Quaternionf &orientation) const;
142 
143  /** \brief Returns the size */
144  std::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  using ConstPtr = boost::shared_ptr<const PCDGrabber>;
163 
164  PCDGrabber (const std::string& pcd_path, float frames_per_second = 0, bool repeat = false);
165  PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second = 0, bool repeat = false);
166 
167  /** \brief Virtual destructor. */
168  ~PCDGrabber () throw ()
169  {
170  stop ();
171  }
172 
173  // Inherited from FileGrabber
174  const typename pcl::PointCloud<PointT>::ConstPtr
175  operator[] (std::size_t idx) const override;
176 
177  // Inherited from FileGrabber
178  std::size_t
179  size () const override;
180  protected:
181 
182  void
183  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const override;
184 
185  boost::signals2::signal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>* signal_;
186  boost::signals2::signal<void (const std::string&)>* file_name_signal_;
187 
188 #ifdef HAVE_OPENNI
189  boost::signals2::signal<void (const openni_wrapper::DepthImage::Ptr&)>* depth_image_signal_;
190  boost::signals2::signal<void (const openni_wrapper::Image::Ptr&)>* image_signal_;
191  boost::signals2::signal<void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)>* image_depth_image_signal_;
192 #endif
193  };
194 
195  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
196  template<typename PointT>
197  PCDGrabber<PointT>::PCDGrabber (const std::string& pcd_path, float frames_per_second, bool repeat)
198  : PCDGrabberBase (pcd_path, frames_per_second, repeat)
199  {
200  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
201  file_name_signal_ = createSignal<void (const std::string&)>();
202 #ifdef HAVE_OPENNI
203  depth_image_signal_ = createSignal <void (const openni_wrapper::DepthImage::Ptr&)> ();
204  image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&)> ();
205  image_depth_image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)> ();
206 #endif
207  }
208 
209  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
210  template<typename PointT>
211  PCDGrabber<PointT>::PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat)
212  : PCDGrabberBase (pcd_files, frames_per_second, repeat), signal_ ()
213  {
214  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
215  file_name_signal_ = createSignal<void (const std::string&)>();
216 #ifdef HAVE_OPENNI
217  depth_image_signal_ = createSignal <void (const openni_wrapper::DepthImage::Ptr&)> ();
218  image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&)> ();
219  image_depth_image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)> ();
220 #endif
221  }
222 
223  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
224  template<typename PointT> const typename pcl::PointCloud<PointT>::ConstPtr
225  PCDGrabber<PointT>::operator[] (std::size_t idx) const
226  {
227  pcl::PCLPointCloud2 blob;
228  Eigen::Vector4f origin;
229  Eigen::Quaternionf orientation;
230  getCloudAt (idx, blob, origin, orientation);
232  pcl::fromPCLPointCloud2 (blob, *cloud);
233  cloud->sensor_origin_ = origin;
234  cloud->sensor_orientation_ = orientation;
235  return (cloud);
236  }
237 
238  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
239  template <typename PointT> std::size_t
241  {
242  return (numFrames ());
243  }
244 
245  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
246  template<typename PointT> void
247  PCDGrabber<PointT>::publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const
248  {
250  pcl::fromPCLPointCloud2 (blob, *cloud);
251  cloud->sensor_origin_ = origin;
252  cloud->sensor_orientation_ = orientation;
253 
254  signal_->operator () (cloud);
255  if (file_name_signal_->num_slots() > 0)
256  file_name_signal_->operator()(file_name);
257 
258 #ifdef HAVE_OPENNI
259  // If dataset is not organized, return
260  if (!cloud->isOrganized ())
261  return;
262 
263  boost::shared_ptr<xn::DepthMetaData> depth_meta_data (new xn::DepthMetaData);
264  depth_meta_data->AllocateData (cloud->width, cloud->height);
265  XnDepthPixel* depth_map = depth_meta_data->WritableData ();
266  std::uint32_t k = 0;
267  for (std::uint32_t i = 0; i < cloud->height; ++i)
268  for (std::uint32_t j = 0; j < cloud->width; ++j)
269  {
270  depth_map[k] = static_cast<XnDepthPixel> ((*cloud)[k].z * 1000);
271  ++k;
272  }
273 
274  openni_wrapper::DepthImage::Ptr depth_image (new openni_wrapper::DepthImage (depth_meta_data, 0.075f, 525, 0, 0));
275  if (depth_image_signal_->num_slots() > 0)
276  depth_image_signal_->operator()(depth_image);
277 
278  // ---[ RGB special case
279  std::vector<pcl::PCLPointField> fields;
280  int rgba_index = pcl::getFieldIndex<PointT> ("rgb", fields);
281  if (rgba_index == -1)
282  rgba_index = pcl::getFieldIndex<PointT> ("rgba", fields);
283  if (rgba_index >= 0)
284  {
285  rgba_index = fields[rgba_index].offset;
286 
287  boost::shared_ptr<xn::ImageMetaData> image_meta_data (new xn::ImageMetaData);
288  image_meta_data->AllocateData (cloud->width, cloud->height, XN_PIXEL_FORMAT_RGB24);
289  XnRGB24Pixel* image_map = image_meta_data->WritableRGB24Data ();
290  k = 0;
291  for (std::uint32_t i = 0; i < cloud->height; ++i)
292  {
293  for (std::uint32_t j = 0; j < cloud->width; ++j)
294  {
295  // Fill r/g/b data, assuming that the order is BGRA
296  pcl::RGB rgb;
297  memcpy (&rgb, reinterpret_cast<const char*> (&cloud->points[k]) + rgba_index, sizeof (RGB));
298  image_map[k].nRed = static_cast<XnUInt8> (rgb.r);
299  image_map[k].nGreen = static_cast<XnUInt8> (rgb.g);
300  image_map[k].nBlue = static_cast<XnUInt8> (rgb.b);
301  ++k;
302  }
303  }
304 
305  openni_wrapper::Image::Ptr image (new openni_wrapper::ImageRGB24 (image_meta_data));
306  if (image_signal_->num_slots() > 0)
307  image_signal_->operator()(image);
308 
309  if (image_depth_image_signal_->num_slots() > 0)
310  image_depth_image_signal_->operator()(image, depth_image, 1.0f / 525.0f);
311  }
312 #endif
313  }
314 }
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:189
PCDGrabberBase(const PCDGrabberBase &src)
Copy constructor.
Definition: pcd_grabber.h:84
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:394
boost::signals2::signal< void(const openni_wrapper::Image::Ptr &, const openni_wrapper::DepthImage::Ptr &, float constant)> * image_depth_image_signal_
Definition: pcd_grabber.h:191
boost::signals2::signal< void(const openni_wrapper::Image::Ptr &)> * image_signal_
Definition: pcd_grabber.h:190
boost::signals2::signal< void(const typename pcl::PointCloud< PointT >::ConstPtr &)> * signal_
Definition: pcd_grabber.h:185
boost::shared_ptr< const PCDGrabber > ConstPtr
Definition: pcd_grabber.h:162
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
std::size_t numFrames() const
Returns the size.
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:197
std::size_t size() const override
size Returns the number of clouds currently loaded by the grabber
Definition: pcd_grabber.h:240
~PCDGrabber()
Virtual destructor.
Definition: pcd_grabber.h:168
This class provides methods to fill a depth or disparity image.
bool isOrganized() const
Return whether a dataset is organized (e.g., arranged in a structured grid).
Definition: point_cloud.h:315
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:397
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:405
A structure representing RGB color information.
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:247
boost::shared_ptr< PCDGrabber > Ptr
Definition: pcd_grabber.h:161
bool getCloudAt(std::size_t idx, pcl::PCLPointCloud2 &blob, Eigen::Vector4f &origin, Eigen::Quaternionf &orientation) const
Get cloud (in ROS form) at a particular location.
const pcl::PointCloud< PointT >::ConstPtr operator[](std::size_t idx) const override
operator[] Returns the idx-th cloud in the dataset, without bounds checking.
Definition: pcd_grabber.h:225
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:407
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:412
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:399
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:186
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:413
#define PCL_EXPORTS
Definition: pcl_macros.h:241