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