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