Point Cloud Library (PCL)  1.8.1-dev
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
feature.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, 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  * $Id$
38  *
39  */
40 
41 #ifndef PCL_FEATURES_IMPL_FEATURE_H_
42 #define PCL_FEATURES_IMPL_FEATURE_H_
43 
44 #include <pcl/search/pcl_search.h>
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////
47 inline void
48 pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
49  const Eigen::Vector4f &point,
50  Eigen::Vector4f &plane_parameters, float &curvature)
51 {
52  solvePlaneParameters (covariance_matrix, plane_parameters [0], plane_parameters [1], plane_parameters [2], curvature);
53 
54  plane_parameters[3] = 0;
55  // Hessian form (D = nc . p_plane (centroid here) + p)
56  plane_parameters[3] = -1 * plane_parameters.dot (point);
57 }
58 
59 //////////////////////////////////////////////////////////////////////////////////////////////
60 inline void
61 pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
62  float &nx, float &ny, float &nz, float &curvature)
63 {
64  // Avoid getting hung on Eigen's optimizers
65 // for (int i = 0; i < 9; ++i)
66 // if (!pcl_isfinite (covariance_matrix.coeff (i)))
67 // {
68 // //PCL_WARN ("[pcl::solvePlaneParameteres] Covariance matrix has NaN/Inf values!\n");
69 // nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
70 // return;
71 // }
72  // Extract the smallest eigenvalue and its eigenvector
73  EIGEN_ALIGN16 Eigen::Vector3f::Scalar eigen_value;
74  EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
75  pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
76 
77  nx = eigen_vector [0];
78  ny = eigen_vector [1];
79  nz = eigen_vector [2];
80 
81  // Compute the curvature surface change
82  float eig_sum = covariance_matrix.coeff (0) + covariance_matrix.coeff (4) + covariance_matrix.coeff (8);
83  if (eig_sum != 0)
84  curvature = fabsf (eigen_value / eig_sum);
85  else
86  curvature = 0;
87 }
88 
89 //////////////////////////////////////////////////////////////////////////////////////////////
90 //////////////////////////////////////////////////////////////////////////////////////////////
91 //////////////////////////////////////////////////////////////////////////////////////////////
92 template <typename PointInT, typename PointOutT> bool
94 {
96  {
97  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
98  return (false);
99  }
100 
101  // If the dataset is empty, just return
102  if (input_->points.empty ())
103  {
104  PCL_ERROR ("[pcl::%s::compute] input_ is empty!\n", getClassName ().c_str ());
105  // Cleanup
106  deinitCompute ();
107  return (false);
108  }
109 
110  // If no search surface has been defined, use the input dataset as the search surface itself
111  if (!surface_)
112  {
113  fake_surface_ = true;
114  surface_ = input_;
115  }
116 
117  // Check if a space search locator was given
118  if (!tree_)
119  {
120  if (surface_->isOrganized () && input_->isOrganized ())
121  tree_.reset (new pcl::search::OrganizedNeighbor<PointInT> ());
122  else
123  tree_.reset (new pcl::search::KdTree<PointInT> (false));
124  }
125 
126  if (tree_->getInputCloud () != surface_) // Make sure the tree searches the surface
127  tree_->setInputCloud (surface_);
128 
129 
130  // Do a fast check to see if the search parameters are well defined
131  if (search_radius_ != 0.0)
132  {
133  if (k_ != 0)
134  {
135  PCL_ERROR ("[pcl::%s::compute] ", getClassName ().c_str ());
136  PCL_ERROR ("Both radius (%f) and K (%d) defined! ", search_radius_, k_);
137  PCL_ERROR ("Set one of them to zero first and then re-run compute ().\n");
138  // Cleanup
139  deinitCompute ();
140  return (false);
141  }
142  else // Use the radiusSearch () function
143  {
144  search_parameter_ = search_radius_;
145  // Declare the search locator definition
146  int (KdTree::*radiusSearchSurface)(const PointCloudIn &cloud, int index, double radius,
147  std::vector<int> &k_indices, std::vector<float> &k_distances,
148  unsigned int max_nn) const = &pcl::search::Search<PointInT>::radiusSearch;
149  search_method_surface_ = boost::bind (radiusSearchSurface, boost::ref (tree_), _1, _2, _3, _4, _5, 0);
150  }
151  }
152  else
153  {
154  if (k_ != 0) // Use the nearestKSearch () function
155  {
156  search_parameter_ = k_;
157  // Declare the search locator definition
158  int (KdTree::*nearestKSearchSurface)(const PointCloudIn &cloud, int index, int k, std::vector<int> &k_indices,
159  std::vector<float> &k_distances) const = &KdTree::nearestKSearch;
160  search_method_surface_ = boost::bind (nearestKSearchSurface, boost::ref (tree_), _1, _2, _3, _4, _5);
161  }
162  else
163  {
164  PCL_ERROR ("[pcl::%s::compute] Neither radius nor K defined! ", getClassName ().c_str ());
165  PCL_ERROR ("Set one of them to a positive number first and then re-run compute ().\n");
166  // Cleanup
167  deinitCompute ();
168  return (false);
169  }
170  }
171  return (true);
172 }
173 
174 //////////////////////////////////////////////////////////////////////////////////////////////
175 template <typename PointInT, typename PointOutT> bool
177 {
178  // Reset the surface
179  if (fake_surface_)
180  {
181  surface_.reset ();
182  fake_surface_ = false;
183  }
184  return (true);
185 }
186 
187 //////////////////////////////////////////////////////////////////////////////////////////////
188 template <typename PointInT, typename PointOutT> void
190 {
191  if (!initCompute ())
192  {
193  output.width = output.height = 0;
194  output.points.clear ();
195  return;
196  }
197 
198  // Copy the header
199  output.header = input_->header;
200 
201  // Resize the output dataset
202  if (output.points.size () != indices_->size ())
203  output.points.resize (indices_->size ());
204 
205  // Check if the output will be computed for all points or only a subset
206  // If the input width or height are not set, set output width as size
207  if (indices_->size () != input_->points.size () || input_->width * input_->height == 0)
208  {
209  output.width = static_cast<uint32_t> (indices_->size ());
210  output.height = 1;
211  }
212  else
213  {
214  output.width = input_->width;
215  output.height = input_->height;
216  }
217  output.is_dense = input_->is_dense;
218 
219  // Perform the actual feature computation
220  computeFeature (output);
221 
222  deinitCompute ();
223 }
224 
225 //////////////////////////////////////////////////////////////////////////////////////////////
226 //////////////////////////////////////////////////////////////////////////////////////////////
227 //////////////////////////////////////////////////////////////////////////////////////////////
228 template <typename PointInT, typename PointNT, typename PointOutT> bool
230 {
232  {
233  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
234  return (false);
235  }
236 
237  // Check if input normals are set
238  if (!normals_)
239  {
240  PCL_ERROR ("[pcl::%s::initCompute] No input dataset containing normals was given!\n", getClassName ().c_str ());
242  return (false);
243  }
244 
245  // Check if the size of normals is the same as the size of the surface
246  if (normals_->points.size () != surface_->points.size ())
247  {
248  PCL_ERROR ("[pcl::%s::initCompute] ", getClassName ().c_str ());
249  PCL_ERROR ("The number of points in the input dataset (%u) differs from ", surface_->points.size ());
250  PCL_ERROR ("the number of points in the dataset containing the normals (%u)!\n", normals_->points.size ());
252  return (false);
253  }
254 
255  return (true);
256 }
257 
258 //////////////////////////////////////////////////////////////////////////////////////////////
259 //////////////////////////////////////////////////////////////////////////////////////////////
260 //////////////////////////////////////////////////////////////////////////////////////////////
261 template <typename PointInT, typename PointLT, typename PointOutT> bool
263 {
265  {
266  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
267  return (false);
268  }
269 
270  // Check if input normals are set
271  if (!labels_)
272  {
273  PCL_ERROR ("[pcl::%s::initCompute] No input dataset containing labels was given!\n", getClassName ().c_str ());
275  return (false);
276  }
277 
278  // Check if the size of normals is the same as the size of the surface
279  if (labels_->points.size () != surface_->points.size ())
280  {
281  PCL_ERROR ("[pcl::%s::initCompute] The number of points in the input dataset differs from the number of points in the dataset containing the labels!\n", getClassName ().c_str ());
283  return (false);
284  }
285 
286  return (true);
287 }
288 
289 //////////////////////////////////////////////////////////////////////////////////////////////
290 //////////////////////////////////////////////////////////////////////////////////////////////
291 //////////////////////////////////////////////////////////////////////////////////////////////
292 template <typename PointInT, typename PointRFT> bool
294  const LRFEstimationPtr& lrf_estimation)
295 {
296  if (frames_never_defined_)
297  frames_.reset ();
298 
299  // Check if input frames are set
300  if (!frames_)
301  {
302  if (!lrf_estimation)
303  {
304  PCL_ERROR ("[initLocalReferenceFrames] No input dataset containing reference frames was given!\n");
305  return (false);
306  } else
307  {
308  //PCL_WARN ("[initLocalReferenceFrames] No input dataset containing reference frames was given! Proceed using default\n");
309  PointCloudLRFPtr default_frames (new PointCloudLRF());
310  lrf_estimation->compute (*default_frames);
311  frames_ = default_frames;
312  }
313  }
314 
315  // Check if the size of frames is the same as the size of the input cloud
316  if (frames_->points.size () != indices_size)
317  {
318  if (!lrf_estimation)
319  {
320  PCL_ERROR ("[initLocalReferenceFrames] The number of points in the input dataset differs from the number of points in the dataset containing the reference frames!\n");
321  return (false);
322  } else
323  {
324  //PCL_WARN ("[initLocalReferenceFrames] The number of points in the input dataset differs from the number of points in the dataset containing the reference frames! Proceed using default\n");
325  PointCloudLRFPtr default_frames (new PointCloudLRF());
326  lrf_estimation->compute (*default_frames);
327  frames_ = default_frames;
328  }
329  }
330 
331  return (true);
332 }
333 
334 #endif //#ifndef PCL_FEATURES_IMPL_FEATURE_H_
335 
search::KdTree is a wrapper class which inherits the pcl::KdTree class for performing search function...
Definition: kdtree.h:62
struct pcl::PointXYZIEdge EIGEN_ALIGN16
virtual bool initCompute()
This method should get called before starting the actual computation.
Definition: feature.hpp:229
void solvePlaneParameters(const Eigen::Matrix3f &covariance_matrix, const Eigen::Vector4f &point, Eigen::Vector4f &plane_parameters, float &curvature)
Solve the eigenvalues and eigenvectors of a given 3x3 covariance matrix, and estimate the least-squar...
Definition: feature.hpp:48
virtual bool deinitCompute()
This method should get called after ending the actual computation.
Definition: feature.hpp:176
virtual bool initCompute()
This method should get called before starting the actual computation.
Definition: feature.hpp:262
uint32_t width
The point cloud width (if organized as an image-structure).
Definition: point_cloud.h:413
pcl::PCLHeader header
The point cloud header.
Definition: point_cloud.h:407
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
Feature< PointInT, PointRFT >::Ptr LRFEstimationPtr
Check if frames_ has been correctly initialized and compute it if needed.
Definition: feature.h:491
PCL base class.
Definition: pcl_base.h:68
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
Definition: eigen.hpp:251
virtual bool initCompute()
This method should get called before starting the actual computation.
Definition: feature.hpp:93
virtual bool initLocalReferenceFrames(const size_t &indices_size, const LRFEstimationPtr &lrf_estimation=LRFEstimationPtr())
Definition: feature.hpp:293
OrganizedNeighbor is a class for optimized nearest neigbhor search in organized point clouds...
Definition: organized.h:62
Feature represents the base feature class.
Definition: feature.h:105
void compute(PointCloudOut &output)
Base method for feature estimation for all points given in <setInputCloud (), setIndices ()> using th...
Definition: feature.hpp:189
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:418
PointCloudLRF::Ptr PointCloudLRFPtr
Definition: feature.h:451
uint32_t height
The point cloud height (if organized as an image-structure).
Definition: point_cloud.h:415