Point Cloud Library (PCL)  1.10.0-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 #include <pcl/make_shared.h>
48 
49 #ifdef HAVE_OPENNI
50 #include <pcl/io/openni_camera/openni_image.h>
51 #include <pcl/io/openni_camera/openni_image_rgb24.h>
52 #include <pcl/io/openni_camera/openni_depth_image.h>
53 #endif
54 
55 #include <string>
56 #include <vector>
57 
58 namespace pcl
59 {
60  /** \brief Base class for PCD file grabber.
61  * \ingroup io
62  */
64  {
65  public:
66  /** \brief Constructor taking just one PCD file or one TAR file containing multiple PCD files.
67  * \param[in] pcd_file path to the PCD file
68  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next PCD in the list.
69  * \param[in] repeat whether to play PCD file in an endless loop or not.
70  */
71  PCDGrabberBase (const std::string& pcd_file, float frames_per_second, bool repeat);
72 
73  /** \brief Constructor taking a list of paths to PCD files, that are played in the order they appear in the list.
74  * \param[in] pcd_files vector of paths to PCD files.
75  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next PCD in the list.
76  * \param[in] repeat whether to play PCD file in an endless loop or not.
77  */
78  PCDGrabberBase (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat);
79 
80  /** \brief Copy constructor.
81  * \param[in] src the PCD Grabber base object to copy into this
82  */
83  PCDGrabberBase (const PCDGrabberBase &src) : impl_ ()
84  {
85  *this = src;
86  }
87 
88  /** \brief Copy operator.
89  * \param[in] src the PCD Grabber base object to copy into this
90  */
92  operator = (const PCDGrabberBase &src)
93  {
94  impl_ = src.impl_;
95  return (*this);
96  }
97 
98  /** \brief Virtual destructor. */
99  ~PCDGrabberBase () noexcept;
100 
101  /** \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. */
102  void
103  start () override;
104 
105  /** \brief Stops playing the list of PCD files if frames_per_second is > 0. Otherwise the method has no effect. */
106  void
107  stop () override;
108 
109  /** \brief Triggers a callback with new data */
110  virtual void
111  trigger ();
112 
113  /** \brief Indicates whether the grabber is streaming or not.
114  * \return true if grabber is started and hasn't run out of PCD files.
115  */
116  bool
117  isRunning () const override;
118 
119  /** \return The name of the grabber */
120  std::string
121  getName () const override;
122 
123  /** \brief Rewinds to the first PCD file in the list.*/
124  virtual void
125  rewind ();
126 
127  /** \brief Returns the frames_per_second. 0 if grabber is trigger-based */
128  float
129  getFramesPerSecond () const override;
130 
131  /** \brief Returns whether the repeat flag is on */
132  bool
133  isRepeatOn () const;
134 
135  /** \brief Get cloud (in ROS form) at a particular location */
136  bool
137  getCloudAt (std::size_t idx,
138  pcl::PCLPointCloud2 &blob,
139  Eigen::Vector4f &origin,
140  Eigen::Quaternionf &orientation) const;
141 
142  /** \brief Returns the size */
143  std::size_t
144  numFrames () const;
145 
146  private:
147  virtual void
148  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const = 0;
149 
150  // to separate and hide the implementation from interface: PIMPL
151  struct PCDGrabberImpl;
152  PCDGrabberImpl* impl_;
153  };
154 
155  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
156  template <typename T> class PointCloud;
157  template <typename PointT> class PCDGrabber : public PCDGrabberBase, public FileGrabber<PointT>
158  {
159  public:
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 () noexcept
168  {
169  stop ();
170  }
171 
172  // Inherited from FileGrabber
173  const typename pcl::PointCloud<PointT>::ConstPtr
174  operator[] (std::size_t idx) const override;
175 
176  // Inherited from FileGrabber
177  std::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
224  PCDGrabber<PointT>::operator[] (std::size_t idx) const
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> std::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  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  std::uint32_t k = 0;
266  for (std::uint32_t i = 0; i < cloud->height; ++i)
267  for (std::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<PointT> ("rgb", fields);
280  if (rgba_index == -1)
281  rgba_index = pcl::getFieldIndex<PointT> ("rgba", fields);
282  if (rgba_index >= 0)
283  {
284  rgba_index = fields[rgba_index].offset;
285 
286  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 (std::uint32_t i = 0; i < cloud->height; ++i)
291  {
292  for (std::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 }
shared_ptr< PCDGrabber > Ptr
Definition: pcd_grabber.h:160
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:83
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:415
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:397
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
~PCDGrabber() noexcept
Virtual destructor.
Definition: pcd_grabber.h:167
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:196
std::size_t size() const override
size Returns the number of clouds currently loaded by the grabber
Definition: pcd_grabber.h:239
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:318
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:400
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:408
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:246
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:224
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:410
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:402
Base class for PCD file grabber.
Definition: pcd_grabber.h:63
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. ...
pcl::shared_ptr< Image > Ptr
Definition: openni_image.h:62
pcl::shared_ptr< DepthImage > Ptr
boost::signals2::signal< void(const std::string &)> * file_name_signal_
Definition: pcd_grabber.h:185
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:416
shared_ptr< const PCDGrabber > ConstPtr
Definition: pcd_grabber.h:161
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
#define PCL_EXPORTS
Definition: pcl_macros.h:253