Point Cloud Library (PCL)  1.9.1-dev
stereo_grabber.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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/io/grabber.h>
41 #include <pcl/common/time_trigger.h>
42 #include <pcl/point_cloud.h>
43 #include <pcl/conversions.h>
44 #include <pcl/stereo/stereo_matching.h>
45 
46 namespace pcl
47 {
48  /** \brief Base class for Stereo file grabber.
49  * \ingroup io
50  */
52  {
53  public:
54  /** \brief Constructor taking just one Stereo pair.
55  * \param[in] pair_files the name of the the stereo (left + right) images.
56  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next pair in the list.
57  * \param[in] repeat whether to play files in an endless loop or not.
58  */
59  StereoGrabberBase (const std::pair<std::string, std::string>& pair_files, float frames_per_second, bool repeat);
60 
61  /** \brief Constructor taking a list of paths to Stereo pair files, that are played in the order they appear in the list.
62  * \param[in] files vector of paths to stereo (left+right) images.
63  * \param[in] frames_per_second frames per second. If 0, start() functions like a trigger, publishing the next pair in the list.
64  * \param[in] repeat whether to play files in an endless loop or not.
65  */
66  StereoGrabberBase (const std::vector<std::pair<std::string, std::string> >& files, float frames_per_second, bool repeat);
67 
68  /** \brief Copy constructor.
69  * \param[in] src the Stereo Grabber base object to copy into this
70  */
71  StereoGrabberBase (const StereoGrabberBase &src) : impl_ ()
72  {
73  *this = src;
74  }
75 
76  /** \brief Copy operator.
77  * \param[in] src the Stereo Grabber base object to copy into this
78  */
80  operator = (const StereoGrabberBase &src)
81  {
82  impl_ = src.impl_;
83  return (*this);
84  }
85 
86  /** \brief Virtual destructor. */
87  ~StereoGrabberBase () throw ();
88 
89  /** \brief Starts playing the list of Stereo images if frames_per_second is > 0. Otherwise it works as a trigger: publishes only the next pair in the list. */
90  void
91  start () override;
92 
93  /** \brief Stops playing the list of Stereo images if frames_per_second is > 0. Otherwise the method has no effect. */
94  void
95  stop () override;
96 
97  /** \brief Triggers a callback with new data */
98  virtual void
99  trigger ();
100 
101  /** \brief whether the grabber is started (publishing) or not.
102  * \return true only if publishing.
103  */
104  bool
105  isRunning () const override;
106 
107  /** \return The name of the grabber */
108  std::string
109  getName () const override;
110 
111  /** \brief Rewinds to the first pair of files in the list.*/
112  virtual void
113  rewind ();
114 
115  /** \brief Returns the frames_per_second. 0 if grabber is trigger-based */
116  float
117  getFramesPerSecond () const override;
118 
119  /** \brief Returns whether the repeat flag is on */
120  bool
121  isRepeatOn () const;
122 
123  private:
124  virtual void
125  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const = 0;
126 
127  // to separate and hide the implementation from interface: PIMPL
128  struct StereoGrabberImpl;
129  StereoGrabberImpl* impl_;
130  };
131 
132  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
133  template <typename PointT> class StereoGrabber : public StereoGrabberBase
134  {
135  public:
136  StereoGrabber (const std::pair<std::string, std::string> & pair_files, float frames_per_second = 0, bool repeat = false);
137  StereoGrabber (const std::vector<std::pair<std::string, std::string> >& files, float frames_per_second = 0, bool repeat = false);
138  protected:
139  void
140  publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const override;
141 
142  boost::signals2::signal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>* signal_;
143  };
144 
145  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
146  template<typename PointT>
147  StereoGrabber<PointT>::StereoGrabber (const std::pair<std::string, std::string>& pair_files, float frames_per_second, bool repeat)
148  : StereoGrabberBase (pair_files, frames_per_second, repeat)
149  {
150  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPtr&)>();
151  }
152 
153  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
154  template<typename PointT>
155  StereoGrabber<PointT>::StereoGrabber (const std::vector<std::pair<std::string, std::string> >& files, float frames_per_second, bool repeat)
156  : StereoGrabberBase (files, frames_per_second, repeat), signal_ ()
157  {
158  signal_ = createSignal<void (const typename pcl::PointCloud<PointT>::ConstPTr&)>();
159  }
160 
161  ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162  template<typename PointT> void
163  StereoGrabber<PointT>::publish (const pcl::PCLPointCloud2& blob, const Eigen::Vector4f& origin, const Eigen::Quaternionf& orientation) const
164  {
166  pcl::fromPCLPointCloud2 (blob, *cloud);
167  cloud->sensor_origin_ = origin;
168  cloud->sensor_orientation_ = orientation;
169 
170  signal_->operator () (cloud);
171  }
172 }
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
StereoGrabber(const std::pair< std::string, std::string > &pair_files, float frames_per_second=0, bool repeat=false)
void publish(const pcl::PCLPointCloud2 &blob, const Eigen::Vector4f &origin, const Eigen::Quaternionf &orientation) const override
StereoGrabberBase(const StereoGrabberBase &src)
Copy constructor.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
boost::signals2::signal< void(const typename pcl::PointCloud< PointT >::ConstPtr &)> * signal_
Grabber interface for PCL 1.x device drivers.
Definition: grabber.h:57
Eigen::Vector4f sensor_origin_
Sensor acquisition pose (origin/translation).
Definition: point_cloud.h:434
Eigen::Quaternionf sensor_orientation_
Sensor acquisition pose (rotation).
Definition: point_cloud.h:436
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:441
PointCloud represents the base class in PCL for storing collections of 3D points. ...
Base class for Stereo file grabber.
#define PCL_EXPORTS
Definition: pcl_macros.h:226