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