Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pcd_grabber.h
Go to the documentation of this file.
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 Willow Garage, Inc. 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/io/grabber.h>
45 #include <string>
46 #include <vector>
47 #include <pcl/ros/conversions.h>
48 
49 #ifdef HAVE_OPENNI
51 #endif
52 
53 namespace pcl
54 {
59  {
60  public:
66  PCDGrabberBase (const std::string& pcd_file, float frames_per_second, bool repeat);
67 
73  PCDGrabberBase (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat);
74 
78  PCDGrabberBase (const PCDGrabberBase &src) : impl_ ()
79  {
80  *this = src;
81  }
82 
87  operator = (const PCDGrabberBase &src)
88  {
89  impl_ = src.impl_;
90  return (*this);
91  }
92 
94  virtual ~PCDGrabberBase () throw ();
95 
97  virtual void
98  start ();
99 
101  virtual void
102  stop ();
103 
105  virtual void
106  trigger ();
107 
111  virtual bool
112  isRunning () const;
113 
115  virtual std::string
116  getName () const;
117 
119  virtual void
120  rewind ();
121 
123  virtual float
124  getFramesPerSecond () const;
125 
127  bool
128  isRepeatOn () const;
129 
130  private:
131  virtual void
132  publish (const sensor_msgs::PointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const = 0;
133 
134  // to separate and hide the implementation from interface: PIMPL
135  struct PCDGrabberImpl;
136  PCDGrabberImpl* impl_;
137  };
138 
140  template <typename T> class PointCloud;
141  template <typename PointT> class PCDGrabber : public PCDGrabberBase
142  {
143  public:
144  PCDGrabber (const std::string& pcd_path, float frames_per_second = 0, bool repeat = false);
145  PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second = 0, bool repeat = false);
146  protected:
147  virtual void
148  publish (const sensor_msgs::PointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const;
149 
150  boost::signals2::signal<void (const boost::shared_ptr<const pcl::PointCloud<PointT> >&)>* signal_;
151 
152 #ifdef HAVE_OPENNI
153  boost::signals2::signal<void (const boost::shared_ptr<openni_wrapper::DepthImage>&)>* depth_image_signal_;
154 #endif
155  };
156 
158  template<typename PointT>
159  PCDGrabber<PointT>::PCDGrabber (const std::string& pcd_path, float frames_per_second, bool repeat)
160  : PCDGrabberBase (pcd_path, frames_per_second, repeat)
161  {
162  signal_ = createSignal<void (const boost::shared_ptr<const pcl::PointCloud<PointT> >&)>();
163 #ifdef HAVE_OPENNI
164  depth_image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::DepthImage>&)> ();
165 #endif
166  }
167 
169  template<typename PointT>
170  PCDGrabber<PointT>::PCDGrabber (const std::vector<std::string>& pcd_files, float frames_per_second, bool repeat)
171  : PCDGrabberBase (pcd_files, frames_per_second, repeat), signal_ ()
172  {
173  signal_ = createSignal<void (const boost::shared_ptr<const pcl::PointCloud<PointT> >&)>();
174 #ifdef HAVE_OPENNI
175  depth_image_signal_ = createSignal <void (const boost::shared_ptr<openni_wrapper::DepthImage>&)> ();
176 #endif
177  }
178 
180  template<typename PointT> void
181  PCDGrabber<PointT>::publish (const sensor_msgs::PointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const
182  {
184  pcl::fromROSMsg (blob, *cloud);
185  cloud->sensor_origin_ = origin;
186  cloud->sensor_orientation_ = orientation;
187 
188  signal_->operator () (cloud);
189 
190 #ifdef HAVE_OPENNI
191  // If dataset is not organized, return
192  if (!cloud->isOrganized ())
193  return;
194 
195  boost::shared_ptr<xn::DepthMetaData> depth_meta_data (new xn::DepthMetaData);
196  depth_meta_data->AllocateData (cloud->width, cloud->height);
197  XnDepthPixel* depth_map = depth_meta_data->WritableData ();
198  uint32_t k = 0;
199  for (uint32_t i = 0; i < cloud->height; ++i)
200  for (uint32_t j = 0; j < cloud->width; ++j)
201  {
202  depth_map[k] = static_cast<XnDepthPixel> ((*cloud)[k].z * 1000);
203  ++k;
204  }
205 
206  boost::shared_ptr<openni_wrapper::DepthImage> depth_image (new openni_wrapper::DepthImage (depth_meta_data, 0.075f, 525, 0, 0));
207  if (depth_image_signal_->num_slots() > 0)
208  depth_image_signal_->operator()(depth_image);
209 #endif
210  }
211 }
212 #endif