Point Cloud Library (PCL)  1.9.1-dev
pcl_base.hpp
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 #ifndef PCL_PCL_IMPL_BASE_HPP_
38 #define PCL_PCL_IMPL_BASE_HPP_
39 
40 #include <pcl/pcl_base.h>
41 #include <pcl/console/print.h>
42 #include <cstddef>
43 
44 ///////////////////////////////////////////////////////////////////////////////////////////
45 template <typename PointT>
47  : input_ ()
48  , use_indices_ (false)
49  , fake_indices_ (false)
50 {
51 }
52 
53 ///////////////////////////////////////////////////////////////////////////////////////////
54 template <typename PointT>
56  : input_ (base.input_)
57  , indices_ (base.indices_)
58  , use_indices_ (base.use_indices_)
60 {
61 }
62 
63 ///////////////////////////////////////////////////////////////////////////////////////////
64 template <typename PointT> void
66 {
67  input_ = cloud;
68 }
69 
70 ///////////////////////////////////////////////////////////////////////////////////////////
71 template <typename PointT> void
73 {
74  indices_ = indices;
75  fake_indices_ = false;
76  use_indices_ = true;
77 }
78 
79 ///////////////////////////////////////////////////////////////////////////////////////////
80 template <typename PointT> void
82 {
83  indices_.reset (new std::vector<int> (*indices));
84  fake_indices_ = false;
85  use_indices_ = true;
86 }
87 
88 ///////////////////////////////////////////////////////////////////////////////////////////
89 template <typename PointT> void
91 {
92  indices_.reset (new std::vector<int> (indices->indices));
93  fake_indices_ = false;
94  use_indices_ = true;
95 }
96 
97 ///////////////////////////////////////////////////////////////////////////////////////////
98 template <typename PointT> void
99 pcl::PCLBase<PointT>::setIndices (size_t row_start, size_t col_start, size_t nb_rows, size_t nb_cols)
100 {
101  if ((nb_rows > input_->height) || (row_start > input_->height))
102  {
103  PCL_ERROR ("[PCLBase::setIndices] cloud is only %d height", input_->height);
104  return;
105  }
106 
107  if ((nb_cols > input_->width) || (col_start > input_->width))
108  {
109  PCL_ERROR ("[PCLBase::setIndices] cloud is only %d width", input_->width);
110  return;
111  }
112 
113  size_t row_end = row_start + nb_rows;
114  if (row_end > input_->height)
115  {
116  PCL_ERROR ("[PCLBase::setIndices] %d is out of rows range %d", row_end, input_->height);
117  return;
118  }
119 
120  size_t col_end = col_start + nb_cols;
121  if (col_end > input_->width)
122  {
123  PCL_ERROR ("[PCLBase::setIndices] %d is out of columns range %d", col_end, input_->width);
124  return;
125  }
126 
127  indices_.reset (new std::vector<int>);
128  indices_->reserve (nb_cols * nb_rows);
129  for(size_t i = row_start; i < row_end; i++)
130  for(size_t j = col_start; j < col_end; j++)
131  indices_->push_back (static_cast<int> ((i * input_->width) + j));
132  fake_indices_ = false;
133  use_indices_ = true;
134 }
135 
136 ///////////////////////////////////////////////////////////////////////////////////////////
137 template <typename PointT> bool
139 {
140  // Check if input was set
141  if (!input_)
142  return (false);
143 
144  // If no point indices have been given, construct a set of indices for the entire input point cloud
145  if (!indices_)
146  {
147  fake_indices_ = true;
148  indices_.reset (new std::vector<int>);
149  try
150  {
151  indices_->resize (input_->points.size ());
152  }
153  catch (const std::bad_alloc&)
154  {
155  PCL_ERROR ("[initCompute] Failed to allocate %lu indices.\n", input_->points.size ());
156  }
157  for (size_t i = 0; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
158  }
159 
160  // If we have a set of fake indices, but they do not match the number of points in the cloud, update them
161  if (fake_indices_ && indices_->size () != input_->points.size ())
162  {
163  size_t indices_size = indices_->size ();
164  indices_->resize (input_->points.size ());
165  for (size_t i = indices_size; i < indices_->size (); ++i) { (*indices_)[i] = static_cast<int>(i); }
166  }
167 
168  return (true);
169 }
170 
171 ///////////////////////////////////////////////////////////////////////////////////////////
172 template <typename PointT> bool
174 {
175  return (true);
176 }
177 
178 #define PCL_INSTANTIATE_PCLBase(T) template class PCL_EXPORTS pcl::PCLBase<T>;
179 
180 #endif //#ifndef PCL_PCL_IMPL_BASE_HPP_
181 
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:154
bool use_indices_
Set to true if point indices are used.
Definition: pcl_base.h:157
bool initCompute()
This method should get called before starting the actual computation.
Definition: pcl_base.hpp:138
boost::shared_ptr< Indices > IndicesPtr
Definition: pcl_base.h:61
PCL base class.
Definition: pcl_base.h:69
boost::shared_ptr< PointIndices const > PointIndicesConstPtr
Definition: pcl_base.h:77
boost::shared_ptr< const Indices > IndicesConstPtr
Definition: pcl_base.h:62
bool deinitCompute()
This method should get called after finishing the actual computation.
Definition: pcl_base.hpp:173
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
Definition: pcl_base.hpp:72
PCLBase()
Empty constructor.
Definition: pcl_base.hpp:46
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:65
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:151
bool fake_indices_
If no set of indices are given, we construct a set of fake indices that mimic the input PointCloud...
Definition: pcl_base.h:160
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74