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