Point Cloud Library (PCL)  1.14.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 #include <pcl/memory.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 Virtual destructor. */
81  ~PCDGrabberBase () noexcept override;
82 
83  /** \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. */
84  void
85  start () override;
86 
87  /** \brief Stops playing the list of PCD files if frames_per_second is > 0. Otherwise the method has no effect. */
88  void
89  stop () override;
90 
91  /** \brief Triggers a callback with new data */
92  virtual void
93  trigger ();
94 
95  /** \brief Indicates whether the grabber is streaming or not.
96  * \return true if grabber is started and hasn't run out of PCD files.
97  */
98  bool
99  isRunning () const override;
100 
101  /** \return The name of the grabber */
102  std::string
103  getName () const override;
104 
105  /** \brief Rewinds to the first PCD file in the list.*/
106  virtual void
107  rewind ();
108 
109  /** \brief Returns the frames_per_second. 0 if grabber is trigger-based */
110  float
111  getFramesPerSecond () const override;
112 
113  /** \brief Returns whether the repeat flag is on */
114  bool
115  isRepeatOn () const;
116 
117  /** \brief Get cloud (in ROS form) at a particular location */
118  bool
119  getCloudAt (std::size_t idx,
120  pcl::PCLPointCloud2 &blob,
121  Eigen::Vector4f &origin,
122  Eigen::Quaternionf &orientation) const;
123 
124  /** \brief Returns the size */
125  std::size_t
126  numFrames () const;
127 
128  private:
129  virtual void
130  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const = 0;
131 
132  // to separate and hide the implementation from interface: PIMPL
133  struct PCDGrabberImpl;
134  PCDGrabberImpl* impl_;
135  };
136 
137  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138  template <typename T> class PointCloud;
139  template <typename PointT> class PCDGrabber : public PCDGrabberBase, public FileGrabber<PointT>
140  {
141  public:
142  using Ptr = shared_ptr<PCDGrabber>;
143  using ConstPtr = shared_ptr<const PCDGrabber>;
144 
145  PCDGrabber (const std::string& pcd_path, float frames_per_second = 0, bool repeat = false);
146  PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second = 0, bool repeat = false);
147 
148  /** \brief Virtual destructor. */
149  ~PCDGrabber () noexcept override
150  {
151  stop ();
152  }
153 
154  // Inherited from FileGrabber
155  const typename pcl::PointCloud<PointT>::ConstPtr
156  operator[] (std::size_t idx) const override;
157 
158  // Inherited from FileGrabber
159  std::size_t
160  size () const override;
161  protected:
162 
163  void
164  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const override;
165 
166  boost::signals2::signal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>* signal_;
167  boost::signals2::signal<void (const std::string&)>* file_name_signal_;
168 
169 #ifdef HAVE_OPENNI
170  boost::signals2::signal<void (const openni_wrapper::DepthImage::Ptr&)>* depth_image_signal_;
171  boost::signals2::signal<void (const openni_wrapper::Image::Ptr&)>* image_signal_;
172  boost::signals2::signal<void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)>* image_depth_image_signal_;
173 #endif
174  };
175 
176  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
177  template<typename PointT>
178  PCDGrabber<PointT>::PCDGrabber (const std::string& pcd_path, float frames_per_second, bool repeat)
179  : PCDGrabberBase (pcd_path, frames_per_second, repeat)
180  {
181  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
182  file_name_signal_ = createSignal<void (const std::string&)>();
183 #ifdef HAVE_OPENNI
184  depth_image_signal_ = createSignal <void (const openni_wrapper::DepthImage::Ptr&)> ();
185  image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&)> ();
186  image_depth_image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)> ();
187 #endif
188  }
189 
190  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
191  template<typename PointT>
192  PCDGrabber<PointT>::PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat)
193  : PCDGrabberBase (pcd_files, frames_per_second, repeat), signal_ ()
194  {
195  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
196  file_name_signal_ = createSignal<void (const std::string&)>();
197 #ifdef HAVE_OPENNI
198  depth_image_signal_ = createSignal <void (const openni_wrapper::DepthImage::Ptr&)> ();
199  image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&)> ();
200  image_depth_image_signal_ = createSignal <void (const openni_wrapper::Image::Ptr&, const openni_wrapper::DepthImage::Ptr&, float constant)> ();
201 #endif
202  }
203 
204  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205  template<typename PointT> const typename pcl::PointCloud<PointT>::ConstPtr
206  PCDGrabber<PointT>::operator[] (std::size_t idx) const
207  {
208  pcl::PCLPointCloud2 blob;
209  Eigen::Vector4f origin;
210  Eigen::Quaternionf orientation;
211  getCloudAt (idx, blob, origin, orientation);
213  pcl::fromPCLPointCloud2 (blob, *cloud);
214  cloud->sensor_origin_ = origin;
215  cloud->sensor_orientation_ = orientation;
216  return (cloud);
217  }
218 
219  ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
220  template <typename PointT> std::size_t
222  {
223  return (numFrames ());
224  }
225 
226  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227  template<typename PointT> void
228  PCDGrabber<PointT>::publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation, const std::string& file_name) const
229  {
231  pcl::fromPCLPointCloud2 (blob, *cloud);
232  cloud->sensor_origin_ = origin;
233  cloud->sensor_orientation_ = orientation;
234 
235  signal_->operator () (cloud);
236  if (file_name_signal_->num_slots() > 0)
237  file_name_signal_->operator()(file_name);
238 
239 #ifdef HAVE_OPENNI
240  // If dataset is not organized, return
241  if (!cloud->isOrganized ())
242  return;
243 
244  shared_ptr<xn::DepthMetaData> depth_meta_data (new xn::DepthMetaData);
245  depth_meta_data->AllocateData (cloud->width, cloud->height);
246  XnDepthPixel* depth_map = depth_meta_data->WritableData ();
247  std::uint32_t k = 0;
248  for (std::uint32_t i = 0; i < cloud->height; ++i)
249  for (std::uint32_t j = 0; j < cloud->width; ++j)
250  {
251  depth_map[k] = static_cast<XnDepthPixel> ((*cloud)[k].z * 1000);
252  ++k;
253  }
254 
255  openni_wrapper::DepthImage::Ptr depth_image (new openni_wrapper::DepthImage (depth_meta_data, 0.075f, 525, 0, 0));
256  if (depth_image_signal_->num_slots() > 0)
257  depth_image_signal_->operator()(depth_image);
258 
259  // ---[ RGB special case
260  std::vector<pcl::PCLPointField> fields;
261  int rgba_index = pcl::getFieldIndex<PointT> ("rgb", fields);
262  if (rgba_index == -1)
263  rgba_index = pcl::getFieldIndex<PointT> ("rgba", fields);
264  if (rgba_index >= 0)
265  {
266  rgba_index = fields[rgba_index].offset;
267 
268  shared_ptr<xn::ImageMetaData> image_meta_data (new xn::ImageMetaData);
269  image_meta_data->AllocateData (cloud->width, cloud->height, XN_PIXEL_FORMAT_RGB24);
270  XnRGB24Pixel* image_map = image_meta_data->WritableRGB24Data ();
271  k = 0;
272  for (std::uint32_t i = 0; i < cloud->height; ++i)
273  {
274  for (std::uint32_t j = 0; j < cloud->width; ++j)
275  {
276  // Fill r/g/b data, assuming that the order is BGRA
277  pcl::RGB rgb;
278  memcpy (&rgb, reinterpret_cast<const char*> (&(*cloud)[k]) + rgba_index, sizeof (RGB));
279  image_map[k].nRed = static_cast<XnUInt8> (rgb.r);
280  image_map[k].nGreen = static_cast<XnUInt8> (rgb.g);
281  image_map[k].nBlue = static_cast<XnUInt8> (rgb.b);
282  ++k;
283  }
284  }
285 
286  openni_wrapper::Image::Ptr image (new openni_wrapper::ImageRGB24 (image_meta_data));
287  if (image_signal_->num_slots() > 0)
288  image_signal_->operator()(image);
289 
290  if (image_depth_image_signal_->num_slots() > 0)
291  image_depth_image_signal_->operator()(image, depth_image, 1.0f / 525.0f);
292  }
293 #endif
294  }
295 }
This class provides methods to fill a depth or disparity image.
pcl::shared_ptr< DepthImage > Ptr
pcl::shared_ptr< Image > Ptr
Definition: openni_image.h:61
This class provides methods to fill a RGB or Grayscale image buffer from underlying RGB24 image.
FileGrabber provides a container-style interface for grabbers which operate on fixed-size input.
Definition: file_grabber.h:54
Grabber interface for PCL 1.x device drivers.
Definition: grabber.h:60
boost::signals2::signal< T > * createSignal()
Definition: grabber.h:252
Base class for PCD file grabber.
Definition: pcd_grabber.h:64
PCDGrabberBase(const std::string &pcd_file, float frames_per_second, bool repeat)
Constructor taking just one PCD file or one TAR file containing multiple PCD files.
~PCDGrabberBase() noexcept override
Virtual destructor.
PCDGrabberBase(const std::vector< std::string > &pcd_files, float frames_per_second, bool repeat)
Constructor taking a list of paths to PCD files, that are played in the order they appear in the list...
shared_ptr< PCDGrabber > Ptr
Definition: pcd_grabber.h:142
boost::signals2::signal< void(const std::string &)> * file_name_signal_
Definition: pcd_grabber.h:167
boost::signals2::signal< void(const openni_wrapper::Image::Ptr &, const openni_wrapper::DepthImage::Ptr &, float constant)> * image_depth_image_signal_
Definition: pcd_grabber.h:172
boost::signals2::signal< void(const openni_wrapper::Image::Ptr &)> * image_signal_
Definition: pcd_grabber.h:171
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:206
boost::signals2::signal< void(const openni_wrapper::DepthImage::Ptr &)> * depth_image_signal_
Definition: pcd_grabber.h:170
PCDGrabber(const std::string &pcd_path, float frames_per_second=0, bool repeat=false)
Definition: pcd_grabber.h:178
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:228
boost::signals2::signal< void(const typename pcl::PointCloud< PointT >::ConstPtr &)> * signal_
Definition: pcd_grabber.h:166
~PCDGrabber() noexcept override
Virtual destructor.
Definition: pcd_grabber.h:149
std::size_t size() const override
size Returns the number of clouds currently loaded by the grabber
Definition: pcd_grabber.h:221
shared_ptr< const PCDGrabber > ConstPtr
Definition: pcd_grabber.h:143
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:408
std::uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:398
bool isOrganized() const
Return whether a dataset is organized (e.g., arranged in a structured grid).
Definition: point_cloud.h:310
std::uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:400
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:406
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
Defines functions, macros and traits for allocating and using memory.
Definition: bfgs.h:10
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map, const std::uint8_t *msg_data)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
Definition: conversions.h:229
#define PCL_EXPORTS
Definition: pcl_macros.h:323
A point structure representing Euclidean xyz coordinates, and the RGB color.
A structure representing RGB color information.