Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
feature.hpp
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-2011, 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: feature.hpp 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_FEATURE_H_
41 #define PCL_FEATURES_IMPL_FEATURE_H_
42 
43 #include <pcl/search/pcl_search.h>
44 
46 inline void
47 pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
48  const Eigen::Vector4f &point,
49  Eigen::Vector4f &plane_parameters, float &curvature)
50 {
51  solvePlaneParameters (covariance_matrix, plane_parameters [0], plane_parameters [1], plane_parameters [2], curvature);
52 
53  plane_parameters[3] = 0;
54  // Hessian form (D = nc . p_plane (centroid here) + p)
55  plane_parameters[3] = -1 * plane_parameters.dot (point);
56 }
57 
59 inline void
60 pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
61  float &nx, float &ny, float &nz, float &curvature)
62 {
63  // Avoid getting hung on Eigen's optimizers
64 // for (int i = 0; i < 9; ++i)
65 // if (!pcl_isfinite (covariance_matrix.coeff (i)))
66 // {
67 // //PCL_WARN ("[pcl::solvePlaneParameteres] Covariance matrix has NaN/Inf values!\n");
68 // nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
69 // return;
70 // }
71  // Extract the smallest eigenvalue and its eigenvector
72  EIGEN_ALIGN16 Eigen::Vector3f::Scalar eigen_value;
73  EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
74  pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
75 
76  nx = eigen_vector [0];
77  ny = eigen_vector [1];
78  nz = eigen_vector [2];
79 
80  // Compute the curvature surface change
81  float eig_sum = covariance_matrix.coeff (0) + covariance_matrix.coeff (4) + covariance_matrix.coeff (8);
82  if (eig_sum != 0)
83  curvature = fabsf (eigen_value / eig_sum);
84  else
85  curvature = 0;
86 }
87 
91 template <typename PointInT, typename PointOutT> bool
93 {
94  if (!PCLBase<PointInT>::initCompute ())
95  {
96  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
97  return (false);
98  }
99 
100  // If the dataset is empty, just return
101  if (input_->points.empty ())
102  {
103  PCL_ERROR ("[pcl::%s::compute] input_ is empty!\n", getClassName ().c_str ());
104  // Cleanup
105  deinitCompute ();
106  return (false);
107  }
108 
109  // If no search surface has been defined, use the input dataset as the search surface itself
110  if (!surface_)
111  {
112  fake_surface_ = true;
113  surface_ = input_;
114  }
115 
116  // Check if a space search locator was given
117  if (!tree_)
118  {
119  if (surface_->isOrganized () && input_->isOrganized ())
120  tree_.reset (new pcl::search::OrganizedNeighbor<PointInT> ());
121  else
122  tree_.reset (new pcl::search::KdTree<PointInT> (false));
123  }
124 
125  if (tree_->getInputCloud () != surface_) // Make sure the tree searches the surface
126  tree_->setInputCloud (surface_);
127 
128 
129  // Do a fast check to see if the search parameters are well defined
130  if (search_radius_ != 0.0)
131  {
132  if (k_ != 0)
133  {
134  PCL_ERROR ("[pcl::%s::compute] ", getClassName ().c_str ());
135  PCL_ERROR ("Both radius (%f) and K (%d) defined! ", search_radius_, k_);
136  PCL_ERROR ("Set one of them to zero first and then re-run compute ().\n");
137  // Cleanup
138  deinitCompute ();
139  return (false);
140  }
141  else // Use the radiusSearch () function
142  {
143  search_parameter_ = search_radius_;
144  // Declare the search locator definition
145  int (KdTree::*radiusSearchSurface)(const PointCloudIn &cloud, int index, double radius,
146  std::vector<int> &k_indices, std::vector<float> &k_distances,
147  unsigned int max_nn) const = &pcl::search::Search<PointInT>::radiusSearch;
148  search_method_surface_ = boost::bind (radiusSearchSurface, boost::ref (tree_), _1, _2, _3, _4, _5, 0);
149  }
150  }
151  else
152  {
153  if (k_ != 0) // Use the nearestKSearch () function
154  {
155  search_parameter_ = k_;
156  // Declare the search locator definition
157  int (KdTree::*nearestKSearchSurface)(const PointCloudIn &cloud, int index, int k, std::vector<int> &k_indices,
158  std::vector<float> &k_distances) const = &KdTree::nearestKSearch;
159  search_method_surface_ = boost::bind (nearestKSearchSurface, boost::ref (tree_), _1, _2, _3, _4, _5);
160  }
161  else
162  {
163  PCL_ERROR ("[pcl::%s::compute] Neither radius nor K defined! ", getClassName ().c_str ());
164  PCL_ERROR ("Set one of them to a positive number first and then re-run compute ().\n");
165  // Cleanup
166  deinitCompute ();
167  return (false);
168  }
169  }
170  return (true);
171 }
172 
174 template <typename PointInT, typename PointOutT> bool
176 {
177  // Reset the surface
178  if (fake_surface_)
179  {
180  surface_.reset ();
181  fake_surface_ = false;
182  }
183  return (true);
184 }
185 
187 template <typename PointInT, typename PointOutT> void
189 {
190  if (!initCompute ())
191  {
192  output.width = output.height = 0;
193  output.points.clear ();
194  return;
195  }
196 
197  // Copy the header
198  output.header = input_->header;
199 
200  // Resize the output dataset
201  if (output.points.size () != indices_->size ())
202  output.points.resize (indices_->size ());
203  // Check if the output will be computed for all points or only a subset
204  if (indices_->size () != input_->points.size ())
205  {
206  output.width = static_cast<int> (indices_->size ());
207  output.height = 1;
208  }
209  else
210  {
211  output.width = input_->width;
212  output.height = input_->height;
213  }
214  output.is_dense = input_->is_dense;
215 
216  // Perform the actual feature computation
217  computeFeature (output);
218 
219  deinitCompute ();
220 }
221 
223 template <typename PointInT, typename PointOutT> void
225 {
226  if (!initCompute ())
227  {
228  output.width = output.height = 0;
229  output.points.resize (0, 0);
230  return;
231  }
232 
233  // Copy the properties
234 //#ifndef USE_ROS
235 // output.properties.acquisition_time = input_->header.stamp;
236 //#endif
237  output.properties.sensor_origin = input_->sensor_origin_;
238  output.properties.sensor_orientation = input_->sensor_orientation_;
239 
240  // Check if the output will be computed for all points or only a subset
241  if (indices_->size () != input_->points.size ())
242  {
243  output.width = static_cast<int> (indices_->size ());
244  output.height = 1;
245  }
246  else
247  {
248  output.width = input_->width;
249  output.height = input_->height;
250  }
251 
252  output.is_dense = input_->is_dense;
253 
254  // Perform the actual feature computation
255  computeFeatureEigen (output);
256 
257  deinitCompute ();
258 }
259 
263 template <typename PointInT, typename PointNT, typename PointOutT> bool
265 {
267  {
268  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
269  return (false);
270  }
271 
272  // Check if input normals are set
273  if (!normals_)
274  {
275  PCL_ERROR ("[pcl::%s::initCompute] No input dataset containing normals was given!\n", getClassName ().c_str ());
276  Feature<PointInT, PointOutT>::deinitCompute ();
277  return (false);
278  }
279 
280  // Check if the size of normals is the same as the size of the surface
281  if (normals_->points.size () != surface_->points.size ())
282  {
283  PCL_ERROR ("[pcl::%s::initCompute] ", getClassName ().c_str ());
284  PCL_ERROR ("The number of points in the input dataset (%u) differs from ", surface_->points.size ());
285  PCL_ERROR ("the number of points in the dataset containing the normals (%u)!\n", normals_->points.size ());
286  Feature<PointInT, PointOutT>::deinitCompute ();
287  return (false);
288  }
289 
290  return (true);
291 }
292 
296 template <typename PointInT, typename PointLT, typename PointOutT> bool
298 {
299  if (!Feature<PointInT, PointOutT>::initCompute ())
300  {
301  PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
302  return (false);
303  }
304 
305  // Check if input normals are set
306  if (!labels_)
307  {
308  PCL_ERROR ("[pcl::%s::initCompute] No input dataset containing labels was given!\n", getClassName ().c_str ());
309  Feature<PointInT, PointOutT>::deinitCompute ();
310  return (false);
311  }
312 
313  // Check if the size of normals is the same as the size of the surface
314  if (labels_->points.size () != surface_->points.size ())
315  {
316  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 ());
317  Feature<PointInT, PointOutT>::deinitCompute ();
318  return (false);
319  }
320 
321  return (true);
322 }
323 
327 template <typename PointInT, typename PointRFT> bool
329  const LRFEstimationPtr& lrf_estimation)
330 {
331  if (frames_never_defined_)
332  frames_.reset ();
333 
334  // Check if input frames are set
335  if (!frames_)
336  {
337  if (!lrf_estimation)
338  {
339  PCL_ERROR ("[initLocalReferenceFrames] No input dataset containing reference frames was given!\n");
340  return (false);
341  } else
342  {
343  //PCL_WARN ("[initLocalReferenceFrames] No input dataset containing reference frames was given! Proceed using default\n");
344  PointCloudLRFPtr default_frames (new PointCloudLRF());
345  lrf_estimation->compute (*default_frames);
346  frames_ = default_frames;
347  }
348  }
349 
350  // Check if the size of frames is the same as the size of the input cloud
351  if (frames_->points.size () != indices_size)
352  {
353  if (!lrf_estimation)
354  {
355  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");
356  return (false);
357  } else
358  {
359  //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");
360  PointCloudLRFPtr default_frames (new PointCloudLRF());
361  lrf_estimation->compute (*default_frames);
362  frames_ = default_frames;
363  }
364  }
365 
366  return (true);
367 }
368 
369 #endif //#ifndef PCL_FEATURES_IMPL_FEATURE_H_
370