Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pcl_base.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  * $Id: pcl_base.h 5295 2012-03-25 19:03:32Z rusu $
37  *
38  */
39 #ifndef PCL_PCL_BASE_H_
40 #define PCL_PCL_BASE_H_
41 
42 #include <cstddef>
43 // Eigen includes
44 #include <pcl/common/eigen.h>
45 // STD includes
46 #include <vector>
47 
48 // Include PCL macros such as PCL_ERROR, etc
49 #include <pcl/pcl_macros.h>
50 
51 // Boost includes. Needed everywhere.
52 #include <boost/shared_ptr.hpp>
53 
54 // Point Cloud message includes. Needed everywhere.
56 #include <pcl/point_cloud.h>
57 #include <pcl/PointIndices.h>
58 #include <pcl/console/print.h>
59 
60 namespace pcl
61 {
62  // definitions used everywhere
63  typedef boost::shared_ptr <std::vector<int> > IndicesPtr;
64  typedef boost::shared_ptr <const std::vector<int> > IndicesConstPtr;
65 
67 
70  template <typename PointT>
71  class PCLBase
72  {
73  public:
75  typedef typename PointCloud::Ptr PointCloudPtr;
77 
80 
82  PCLBase () : input_ (), indices_ (), use_indices_ (false), fake_indices_ (false) {}
83 
85  PCLBase (const PCLBase& base)
86  : input_ (base.input_)
87  , indices_ (base.indices_)
88  , use_indices_ (base.use_indices_)
89  , fake_indices_ (base.fake_indices_)
90  {}
91 
93  virtual ~PCLBase()
94  {
95  input_.reset ();
96  indices_.reset ();
97  }
98 
102  virtual inline void
103  setInputCloud (const PointCloudConstPtr &cloud) { input_ = cloud; }
104 
106  inline PointCloudConstPtr const
107  getInputCloud () { return (input_); }
108 
112  inline void
113  setIndices (const IndicesPtr &indices)
114  {
115  indices_ = indices;
116  fake_indices_ = false;
117  use_indices_ = true;
118  }
119 
123  inline void
124  setIndices (const IndicesConstPtr &indices)
125  {
126  indices_.reset (new std::vector<int> (*indices));
127  fake_indices_ = false;
128  use_indices_ = true;
129  }
130 
134  inline void
136  {
137  indices_.reset (new std::vector<int> (indices->indices));
138  fake_indices_ = false;
139  use_indices_ = true;
140  }
141 
150  inline void
151  setIndices (size_t row_start, size_t col_start, size_t nb_rows, size_t nb_cols)
152  {
153  if ((nb_rows > input_->height) || (row_start > input_->height))
154  {
155  PCL_ERROR ("[PCLBase::setIndices] cloud is only %d height", input_->height);
156  return;
157  }
158 
159  if ((nb_cols > input_->width) || (col_start > input_->width))
160  {
161  PCL_ERROR ("[PCLBase::setIndices] cloud is only %d width", input_->width);
162  return;
163  }
164 
165  size_t row_end = row_start + nb_rows;
166  if (row_end > input_->height)
167  {
168  PCL_ERROR ("[PCLBase::setIndices] %d is out of rows range %d", row_end, input_->height);
169  return;
170  }
171 
172  size_t col_end = col_start + nb_cols;
173  if (col_end > input_->width)
174  {
175  PCL_ERROR ("[PCLBase::setIndices] %d is out of columns range %d", col_end, input_->width);
176  return;
177  }
178 
179  indices_.reset (new std::vector<int>);
180  indices_->reserve (nb_cols * nb_rows);
181  for(size_t i = row_start; i < row_end; i++)
182  for(size_t j = col_start; j < col_end; j++)
183  indices_->push_back (static_cast<int> ((i * input_->width) + j));
184  fake_indices_ = false;
185  use_indices_ = true;
186  }
187 
189  inline IndicesPtr const
190  getIndices () { return (indices_); }
191 
197  const PointT& operator[] (size_t pos)
198  {
199  return ((*input_)[(*indices_)[pos]]);
200  }
201 
202  protected:
204  PointCloudConstPtr input_;
205 
207  IndicesPtr indices_;
208 
210  bool use_indices_;
211 
213  bool fake_indices_;
214 
224  inline bool
225  initCompute ()
226  {
227  // Check if input was set
228  if (!input_)
229  return (false);
230 
231  // If no point indices have been given, construct a set of indices for the entire input point cloud
232  if (!indices_)
233  {
234  fake_indices_ = true;
235  indices_.reset (new std::vector<int>);
236  try
237  {
238  indices_->resize (input_->points.size ());
239  }
240  catch (std::bad_alloc)
241  {
242  PCL_ERROR ("[initCompute] Failed to allocate %zu indices.\n", input_->points.size ());
243  }
244  for (size_t i = 0; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
245  }
246 
247  // If we have a set of fake indices, but they do not match the number of points in the cloud, update them
248  if (fake_indices_ && indices_->size () != input_->points.size ())
249  {
250  size_t indices_size = indices_->size ();
251  indices_->resize (input_->points.size ());
252  for (size_t i = indices_size; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
253  }
254 
255  return (true);
256  }
257 
261  inline bool
262  deinitCompute ()
263  {
264  return (true);
265  }
266  public:
267  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
268  };
269 
271  template <>
272  class PCL_EXPORTS PCLBase<sensor_msgs::PointCloud2>
273  {
274  public:
278 
281 
283  PCLBase () : input_ (), indices_ (), use_indices_ (false), fake_indices_ (false),
284  field_sizes_ (0), x_idx_ (-1), y_idx_ (-1), z_idx_ (-1),
285  x_field_name_ ("x"), y_field_name_ ("y"), z_field_name_ ("z")
286  {};
287 
289  virtual ~PCLBase()
290  {
291  input_.reset ();
292  indices_.reset ();
293  }
294 
298  void
299  setInputCloud (const PointCloud2ConstPtr &cloud);
300 
302  inline PointCloud2ConstPtr const
303  getInputCloud () { return (input_); }
304 
308  inline void
309  setIndices (const IndicesPtr &indices)
310  {
311  indices_ = indices;
312  fake_indices_ = false;
313  use_indices_ = true;
314  }
315 
319  inline void
320  setIndices (const PointIndicesConstPtr &indices)
321  {
322  indices_.reset (new std::vector<int> (indices->indices));
323  fake_indices_ = false;
324  use_indices_ = true;
325  }
326 
328  inline IndicesPtr const
329  getIndices () { return (indices_); }
330 
331  protected:
333  PointCloud2ConstPtr input_;
334 
336  IndicesPtr indices_;
337 
339  bool use_indices_;
340 
342  bool fake_indices_;
343 
345  std::vector<int> field_sizes_;
346 
348  int x_idx_, y_idx_, z_idx_;
349 
351  std::string x_field_name_, y_field_name_, z_field_name_;
352 
353  bool initCompute ();
354  bool deinitCompute ();
355  public:
356  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
357  };
358 }
359 
360 #endif //#ifndef PCL_PCL_BASE_H_