Point Cloud Library (PCL)  1.7.1
pcl_base.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  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 #ifndef PCL_PCL_BASE_H_
39 #define PCL_PCL_BASE_H_
40 
41 #if defined __GNUC__
42 # pragma GCC system_header
43 #endif
44 
45 // Include PCL macros such as PCL_ERROR, etc
46 #include <pcl/pcl_macros.h>
47 
48 #include <boost/shared_ptr.hpp>
49 #include <Eigen/StdVector>
50 #include <Eigen/Core>
51 
52 // Point Cloud message includes. Needed everywhere.
53 #include <pcl/point_cloud.h>
54 #include <pcl/PointIndices.h>
55 #include <pcl/PCLPointCloud2.h>
56 
57 namespace pcl
58 {
59  // definitions used everywhere
60  typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
61  typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
62 
63  /////////////////////////////////////////////////////////////////////////////////////////
64  /** \brief PCL base class. Implements methods that are used by most PCL algorithms.
65  * \ingroup common
66  */
67  template <typename PointT>
68  class PCLBase
69  {
70  public:
72  typedef typename PointCloud::Ptr PointCloudPtr;
74 
75  typedef boost::shared_ptr<PointIndices> PointIndicesPtr;
76  typedef boost::shared_ptr<PointIndices const> PointIndicesConstPtr;
77 
78  /** \brief Empty constructor. */
79  PCLBase ();
80 
81  /** \brief Copy constructor. */
82  PCLBase (const PCLBase& base);
83 
84  /** \brief Destructor. */
85  virtual ~PCLBase ()
86  {
87  input_.reset ();
88  indices_.reset ();
89  }
90 
91  /** \brief Provide a pointer to the input dataset
92  * \param[in] cloud the const boost shared pointer to a PointCloud message
93  */
94  virtual void
95  setInputCloud (const PointCloudConstPtr &cloud);
96 
97  /** \brief Get a pointer to the input point cloud dataset. */
98  inline PointCloudConstPtr const
99  getInputCloud () { return (input_); }
100 
101  /** \brief Provide a pointer to the vector of indices that represents the input data.
102  * \param[in] indices a pointer to the indices that represent the input data.
103  */
104  virtual void
105  setIndices (const IndicesPtr &indices);
106 
107  /** \brief Provide a pointer to the vector of indices that represents the input data.
108  * \param[in] indices a pointer to the indices that represent the input data.
109  */
110  virtual void
111  setIndices (const IndicesConstPtr &indices);
112 
113  /** \brief Provide a pointer to the vector of indices that represents the input data.
114  * \param[in] indices a pointer to the indices that represent the input data.
115  */
116  virtual void
117  setIndices (const PointIndicesConstPtr &indices);
118 
119  /** \brief Set the indices for the points laying within an interest region of
120  * the point cloud.
121  * \note you shouldn't call this method on unorganized point clouds!
122  * \param[in] row_start the offset on rows
123  * \param[in] col_start the offset on columns
124  * \param[in] nb_rows the number of rows to be considered row_start included
125  * \param[in] nb_cols the number of columns to be considered col_start included
126  */
127  virtual void
128  setIndices (size_t row_start, size_t col_start, size_t nb_rows, size_t nb_cols);
129 
130  /** \brief Get a pointer to the vector of indices used. */
131  inline IndicesPtr const
132  getIndices () { return (indices_); }
133 
134  /** \brief Override PointCloud operator[] to shorten code
135  * \note this method can be called instead of (*input_)[(*indices_)[pos]]
136  * or input_->points[(*indices_)[pos]]
137  * \param[in] pos position in indices_ vector
138  */
139  inline const PointT& operator[] (size_t pos)
140  {
141  return ((*input_)[(*indices_)[pos]]);
142  }
143 
144  protected:
145  /** \brief The input point cloud dataset. */
147 
148  /** \brief A pointer to the vector of point indices to use. */
150 
151  /** \brief Set to true if point indices are used. */
153 
154  /** \brief If no set of indices are given, we construct a set of fake indices that mimic the input PointCloud. */
156 
157  /** \brief This method should get called before starting the actual computation.
158  *
159  * Internally, initCompute() does the following:
160  * - checks if an input dataset is given, and returns false otherwise
161  * - checks whether a set of input indices has been given. Returns true if yes.
162  * - if no input indices have been given, a fake set is created, which will be used until:
163  * - either a new set is given via setIndices(), or
164  * - a new cloud is given that has a different set of points. This will trigger an update on the set of fake indices
165  */
166  bool
167  initCompute ();
168 
169  /** \brief This method should get called after finishing the actual computation.
170  */
171  bool
172  deinitCompute ();
173 
174  public:
175  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
176  };
177 
178  /////////////////////////////////////////////////////////////////////////////////////////
179  template <>
180  class PCL_EXPORTS PCLBase<pcl::PCLPointCloud2>
181  {
182  public:
184  typedef boost::shared_ptr<PCLPointCloud2> PCLPointCloud2Ptr;
185  typedef boost::shared_ptr<PCLPointCloud2 const> PCLPointCloud2ConstPtr;
186 
187  typedef boost::shared_ptr<PointIndices> PointIndicesPtr;
188  typedef boost::shared_ptr<PointIndices const> PointIndicesConstPtr;
189 
190  /** \brief Empty constructor. */
191  PCLBase ();
192 
193  /** \brief destructor. */
194  virtual ~PCLBase()
195  {
196  input_.reset ();
197  indices_.reset ();
198  }
199 
200  /** \brief Provide a pointer to the input dataset
201  * \param cloud the const boost shared pointer to a PointCloud message
202  */
203  void
204  setInputCloud (const PCLPointCloud2ConstPtr &cloud);
205 
206  /** \brief Get a pointer to the input point cloud dataset. */
207  inline PCLPointCloud2ConstPtr const
208  getInputCloud () { return (input_); }
209 
210  /** \brief Provide a pointer to the vector of indices that represents the input data.
211  * \param[in] indices a pointer to the indices that represent the input data.
212  */
213  void
214  setIndices (const IndicesPtr &indices);
215 
216  /** \brief Provide a pointer to the vector of indices that represents the input data.
217  * \param[in] indices a pointer to the indices that represent the input data.
218  */
219  void
220  setIndices (const PointIndicesConstPtr &indices);
221 
222  /** \brief Get a pointer to the vector of indices used. */
223  inline IndicesPtr const
224  getIndices () { return (indices_); }
225 
226  protected:
227  /** \brief The input point cloud dataset. */
229 
230  /** \brief A pointer to the vector of point indices to use. */
232 
233  /** \brief Set to true if point indices are used. */
235 
236  /** \brief If no set of indices are given, we construct a set of fake indices that mimic the input PointCloud. */
238 
239  /** \brief The size of each individual field. */
240  std::vector<int> field_sizes_;
241 
242  /** \brief The x-y-z fields indices. */
243  int x_idx_, y_idx_, z_idx_;
244 
245  /** \brief The desired x-y-z field names. */
246  std::string x_field_name_, y_field_name_, z_field_name_;
247 
248  bool initCompute ();
249  bool deinitCompute ();
250  public:
251  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
252  };
253 }
254 
255 #ifdef PCL_NO_PRECOMPILE
256 #include <pcl/impl/pcl_base.hpp>
257 #endif
258 
259 #endif //#ifndef PCL_PCL_BASE_H_