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