Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vfh.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  * 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: vfh.hpp 6089 2012-07-02 18:38:08Z rusu $
38  *
39  */
40 
41 #ifndef PCL_FEATURES_IMPL_VFH_H_
42 #define PCL_FEATURES_IMPL_VFH_H_
43 
44 #include <pcl/features/vfh.h>
45 #include <pcl/features/pfh.h>
46 #include <pcl/common/common.h>
47 
49 template<typename PointInT, typename PointNT, typename PointOutT> bool
51 {
52  if (input_->points.size () < 2 || (surface_ && surface_->points.size () < 2))
53  {
54  PCL_ERROR ("[pcl::VFHEstimation::initCompute] Input dataset must have at least 2 points!\n");
55  return (false);
56  }
57  if (search_radius_ == 0 && k_ == 0)
58  k_ = 1;
59  return (Feature<PointInT, PointOutT>::initCompute ());
60 }
61 
63 template<typename PointInT, typename PointNT, typename PointOutT> void
65 {
66  if (!initCompute ())
67  {
68  output.width = output.height = 0;
69  output.points.clear ();
70  return;
71  }
72  // Copy the header
73  output.header = input_->header;
74 
75  // Resize the output dataset
76  // Important! We should only allocate precisely how many elements we will need, otherwise
77  // we risk at pre-allocating too much memory which could lead to bad_alloc
78  // (see http://dev.pointclouds.org/issues/657)
79  output.width = output.height = 1;
80  output.is_dense = input_->is_dense;
81  output.points.resize (1);
82 
83  // Perform the actual feature computation
84  computeFeature (output);
85 
87 }
88 
90 template<typename PointInT, typename PointNT, typename PointOutT> void
92  const Eigen::Vector4f &centroid_n,
93  const pcl::PointCloud<PointInT> &cloud,
94  const pcl::PointCloud<PointNT> &normals,
95  const std::vector<int> &indices)
96 {
97  Eigen::Vector4f pfh_tuple;
98  // Reset the whole thing
99  hist_f1_.setZero (nr_bins_f1_);
100  hist_f2_.setZero (nr_bins_f2_);
101  hist_f3_.setZero (nr_bins_f3_);
102  hist_f4_.setZero (nr_bins_f4_);
103 
104  // Get the bounding box of the current cluster
105  //Eigen::Vector4f min_pt, max_pt;
106  //pcl::getMinMax3D (cloud, indices, min_pt, max_pt);
107  //double distance_normalization_factor = (std::max)((centroid_p - min_pt).norm (), (centroid_p - max_pt).norm ());
108 
109  //Instead of using the bounding box to normalize the VFH distance component, it is better to use the max_distance
110  //from any point to centroid. VFH is invariant to rotation about the roll axis but the bounding box is not,
111  //resulting in different normalization factors for point clouds that are just rotated about that axis.
112 
113  double distance_normalization_factor = 1.0;
114  if (normalize_distances_)
115  {
116  Eigen::Vector4f max_pt;
117  pcl::getMaxDistance (cloud, indices, centroid_p, max_pt);
118  max_pt[3] = 0;
119  distance_normalization_factor = (centroid_p - max_pt).norm ();
120  }
121 
122  // Factorization constant
123  float hist_incr;
124  if (normalize_bins_)
125  hist_incr = 100.0f / static_cast<float> (indices.size () - 1);
126  else
127  hist_incr = 1.0f;
128 
129  float hist_incr_size_component;
130  if (size_component_)
131  hist_incr_size_component = hist_incr;
132  else
133  hist_incr_size_component = 0.0;
134 
135  // Iterate over all the points in the neighborhood
136  for (size_t idx = 0; idx < indices.size (); ++idx)
137  {
138  // Compute the pair P to NNi
139  if (!computePairFeatures (centroid_p, centroid_n, cloud.points[indices[idx]].getVector4fMap (),
140  normals.points[indices[idx]].getNormalVector4fMap (), pfh_tuple[0], pfh_tuple[1],
141  pfh_tuple[2], pfh_tuple[3]))
142  continue;
143 
144  // Normalize the f1, f2, f3, f4 features and push them in the histogram
145  int h_index = static_cast<int> (floor (nr_bins_f1_ * ((pfh_tuple[0] + M_PI) * d_pi_)));
146  if (h_index < 0)
147  h_index = 0;
148  if (h_index >= nr_bins_f1_)
149  h_index = nr_bins_f1_ - 1;
150  hist_f1_ (h_index) += hist_incr;
151 
152  h_index = static_cast<int> (floor (nr_bins_f2_ * ((pfh_tuple[1] + 1.0) * 0.5)));
153  if (h_index < 0)
154  h_index = 0;
155  if (h_index >= nr_bins_f2_)
156  h_index = nr_bins_f2_ - 1;
157  hist_f2_ (h_index) += hist_incr;
158 
159  h_index = static_cast<int> (floor (nr_bins_f3_ * ((pfh_tuple[2] + 1.0) * 0.5)));
160  if (h_index < 0)
161  h_index = 0;
162  if (h_index >= nr_bins_f3_)
163  h_index = nr_bins_f3_ - 1;
164  hist_f3_ (h_index) += hist_incr;
165 
166  if (normalize_distances_)
167  h_index = static_cast<int> (floor (nr_bins_f4_ * (pfh_tuple[3] / distance_normalization_factor)));
168  else
169  h_index = static_cast<int> (pcl_round (pfh_tuple[3] * 100));
170 
171  if (h_index < 0)
172  h_index = 0;
173  if (h_index >= nr_bins_f4_)
174  h_index = nr_bins_f4_ - 1;
175 
176  hist_f4_ (h_index) += hist_incr_size_component;
177  }
178 }
180 template <typename PointInT, typename PointNT, typename PointOutT> void
182 {
183  // ---[ Step 1a : compute the centroid in XYZ space
184  Eigen::Vector4f xyz_centroid;
185 
186  if (use_given_centroid_)
187  xyz_centroid = centroid_to_use_;
188  else
189  compute3DCentroid (*surface_, *indices_, xyz_centroid); // Estimate the XYZ centroid
190 
191  // ---[ Step 1b : compute the centroid in normal space
192  Eigen::Vector4f normal_centroid = Eigen::Vector4f::Zero ();
193  int cp = 0;
194 
195  // If the data is dense, we don't need to check for NaN
196  if (use_given_normal_)
197  normal_centroid = normal_to_use_;
198  else
199  {
200  if (normals_->is_dense)
201  {
202  for (size_t i = 0; i < indices_->size (); ++i)
203  {
204  normal_centroid += normals_->points[(*indices_)[i]].getNormalVector4fMap ();
205  cp++;
206  }
207  }
208  // NaN or Inf values could exist => check for them
209  else
210  {
211  for (size_t i = 0; i < indices_->size (); ++i)
212  {
213  if (!pcl_isfinite (normals_->points[(*indices_)[i]].normal[0])
214  ||
215  !pcl_isfinite (normals_->points[(*indices_)[i]].normal[1])
216  ||
217  !pcl_isfinite (normals_->points[(*indices_)[i]].normal[2]))
218  continue;
219  normal_centroid += normals_->points[(*indices_)[i]].getNormalVector4fMap ();
220  cp++;
221  }
222  }
223  normal_centroid /= static_cast<float> (cp);
224  }
225 
226  // Compute the direction of view from the viewpoint to the centroid
227  Eigen::Vector4f viewpoint (vpx_, vpy_, vpz_, 0);
228  Eigen::Vector4f d_vp_p = viewpoint - xyz_centroid;
229  d_vp_p.normalize ();
230 
231  // Estimate the SPFH at nn_indices[0] using the entire cloud
232  computePointSPFHSignature (xyz_centroid, normal_centroid, *surface_, *normals_, *indices_);
233 
234  // We only output _1_ signature
235  output.points.resize (1);
236  output.width = 1;
237  output.height = 1;
238 
239  // Estimate the FPFH at nn_indices[0] using the entire cloud and copy the resultant signature
240  for (int d = 0; d < hist_f1_.size (); ++d)
241  output.points[0].histogram[d + 0] = hist_f1_[d];
242 
243  size_t data_size = hist_f1_.size ();
244  for (int d = 0; d < hist_f2_.size (); ++d)
245  output.points[0].histogram[d + data_size] = hist_f2_[d];
246 
247  data_size += hist_f2_.size ();
248  for (int d = 0; d < hist_f3_.size (); ++d)
249  output.points[0].histogram[d + data_size] = hist_f3_[d];
250 
251  data_size += hist_f3_.size ();
252  for (int d = 0; d < hist_f4_.size (); ++d)
253  output.points[0].histogram[d + data_size] = hist_f4_[d];
254 
255  // ---[ Step 2 : obtain the viewpoint component
256  hist_vp_.setZero (nr_bins_vp_);
257 
258  double hist_incr;
259  if (normalize_bins_)
260  hist_incr = 100.0 / static_cast<double> (indices_->size ());
261  else
262  hist_incr = 1.0;
263 
264  for (size_t i = 0; i < indices_->size (); ++i)
265  {
266  Eigen::Vector4f normal (normals_->points[(*indices_)[i]].normal[0],
267  normals_->points[(*indices_)[i]].normal[1],
268  normals_->points[(*indices_)[i]].normal[2], 0);
269  // Normalize
270  double alpha = (normal.dot (d_vp_p) + 1.0) * 0.5;
271  int fi = static_cast<int> (floor (alpha * static_cast<double> (hist_vp_.size ())));
272  if (fi < 0)
273  fi = 0;
274  if (fi > (static_cast<int> (hist_vp_.size ()) - 1))
275  fi = static_cast<int> (hist_vp_.size ()) - 1;
276  // Bin into the histogram
277  hist_vp_ [fi] += static_cast<float> (hist_incr);
278  }
279  data_size += hist_f4_.size ();
280  // Copy the resultant signature
281  for (int d = 0; d < hist_vp_.size (); ++d)
282  output.points[0].histogram[d + data_size] = hist_vp_[d];
283 }
284 
285 #define PCL_INSTANTIATE_VFHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::VFHEstimation<T,NT,OutT>;
286 
287 #endif // PCL_FEATURES_IMPL_VFH_H_