Point Cloud Library (PCL)  1.10.1
fpfh.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_FPFH_H_
42 #define PCL_FEATURES_IMPL_FPFH_H_
43 
44 #include <pcl/features/fpfh.h>
45 #include <pcl/features/pfh_tools.h>
46 
47 //////////////////////////////////////////////////////////////////////////////////////////////
48 template <typename PointInT, typename PointNT, typename PointOutT> bool
50  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
51  int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4)
52 {
53  pcl::computePairFeatures (cloud.points[p_idx].getVector4fMap (), normals.points[p_idx].getNormalVector4fMap (),
54  cloud.points[q_idx].getVector4fMap (), normals.points[q_idx].getNormalVector4fMap (),
55  f1, f2, f3, f4);
56  return (true);
57 }
58 
59 //////////////////////////////////////////////////////////////////////////////////////////////
60 template <typename PointInT, typename PointNT, typename PointOutT> void
62  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
63  int p_idx, int row, const std::vector<int> &indices,
64  Eigen::MatrixXf &hist_f1, Eigen::MatrixXf &hist_f2, Eigen::MatrixXf &hist_f3)
65 {
66  Eigen::Vector4f pfh_tuple;
67  // Get the number of bins from the histograms size
68  // @TODO: use arrays
69  int nr_bins_f1 = static_cast<int> (hist_f1.cols ());
70  int nr_bins_f2 = static_cast<int> (hist_f2.cols ());
71  int nr_bins_f3 = static_cast<int> (hist_f3.cols ());
72 
73  // Factorization constant
74  float hist_incr = 100.0f / static_cast<float>(indices.size () - 1);
75 
76  // Iterate over all the points in the neighborhood
77  for (const auto &index : indices)
78  {
79  // Avoid unnecessary returns
80  if (p_idx == index)
81  continue;
82 
83  // Compute the pair P to NNi
84  if (!computePairFeatures (cloud, normals, p_idx, index, pfh_tuple[0], pfh_tuple[1], pfh_tuple[2], pfh_tuple[3]))
85  continue;
86 
87  // Normalize the f1, f2, f3 features and push them in the histogram
88  int h_index = static_cast<int> (std::floor (nr_bins_f1 * ((pfh_tuple[0] + M_PI) * d_pi_)));
89  if (h_index < 0) h_index = 0;
90  if (h_index >= nr_bins_f1) h_index = nr_bins_f1 - 1;
91  hist_f1 (row, h_index) += hist_incr;
92 
93  h_index = static_cast<int> (std::floor (nr_bins_f2 * ((pfh_tuple[1] + 1.0) * 0.5)));
94  if (h_index < 0) h_index = 0;
95  if (h_index >= nr_bins_f2) h_index = nr_bins_f2 - 1;
96  hist_f2 (row, h_index) += hist_incr;
97 
98  h_index = static_cast<int> (std::floor (nr_bins_f3 * ((pfh_tuple[2] + 1.0) * 0.5)));
99  if (h_index < 0) h_index = 0;
100  if (h_index >= nr_bins_f3) h_index = nr_bins_f3 - 1;
101  hist_f3 (row, h_index) += hist_incr;
102  }
103 }
104 
105 //////////////////////////////////////////////////////////////////////////////////////////////
106 template <typename PointInT, typename PointNT, typename PointOutT> void
108  const Eigen::MatrixXf &hist_f1, const Eigen::MatrixXf &hist_f2, const Eigen::MatrixXf &hist_f3,
109  const std::vector<int> &indices, const std::vector<float> &dists, Eigen::VectorXf &fpfh_histogram)
110 {
111  assert (indices.size () == dists.size ());
112  // @TODO: use arrays
113  double sum_f1 = 0.0, sum_f2 = 0.0, sum_f3 = 0.0;
114  float weight = 0.0, val_f1, val_f2, val_f3;
115 
116  // Get the number of bins from the histograms size
117  const auto nr_bins_f1 = hist_f1.cols ();
118  const auto nr_bins_f2 = hist_f2.cols ();
119  const auto nr_bins_f3 = hist_f3.cols ();
120  const auto nr_bins_f12 = nr_bins_f1 + nr_bins_f2;
121 
122  // Clear the histogram
123  fpfh_histogram.setZero (nr_bins_f1 + nr_bins_f2 + nr_bins_f3);
124 
125  // Use the entire patch
126  for (std::size_t idx = 0; idx < indices.size (); ++idx)
127  {
128  // Minus the query point itself
129  if (dists[idx] == 0)
130  continue;
131 
132  // Standard weighting function used
133  weight = 1.0f / dists[idx];
134 
135  // Weight the SPFH of the query point with the SPFH of its neighbors
136  for (std::size_t f1_i = 0; f1_i < nr_bins_f1; ++f1_i)
137  {
138  val_f1 = hist_f1 (indices[idx], f1_i) * weight;
139  sum_f1 += val_f1;
140  fpfh_histogram[f1_i] += val_f1;
141  }
142 
143  for (std::size_t f2_i = 0; f2_i < nr_bins_f2; ++f2_i)
144  {
145  val_f2 = hist_f2 (indices[idx], f2_i) * weight;
146  sum_f2 += val_f2;
147  fpfh_histogram[f2_i + nr_bins_f1] += val_f2;
148  }
149 
150  for (std::size_t f3_i = 0; f3_i < nr_bins_f3; ++f3_i)
151  {
152  val_f3 = hist_f3 (indices[idx], f3_i) * weight;
153  sum_f3 += val_f3;
154  fpfh_histogram[f3_i + nr_bins_f12] += val_f3;
155  }
156  }
157 
158  if (sum_f1 != 0)
159  sum_f1 = 100.0 / sum_f1; // histogram values sum up to 100
160  if (sum_f2 != 0)
161  sum_f2 = 100.0 / sum_f2; // histogram values sum up to 100
162  if (sum_f3 != 0)
163  sum_f3 = 100.0 / sum_f3; // histogram values sum up to 100
164 
165  // Adjust final FPFH values
166  const auto denormalize_with = [](auto factor)
167  {
168  return [=](const auto& data) { return data * factor; };
169  };
170 
171  auto last = fpfh_histogram.data ();
172  last = std::transform(last, last + nr_bins_f1, last, denormalize_with (sum_f1));
173  last = std::transform(last, last + nr_bins_f2, last, denormalize_with (sum_f2));
174  std::transform(last, last + nr_bins_f3, last, denormalize_with (sum_f3));
175 }
176 
177 //////////////////////////////////////////////////////////////////////////////////////////////
178 template <typename PointInT, typename PointNT, typename PointOutT> void
180  Eigen::MatrixXf &hist_f1, Eigen::MatrixXf &hist_f2, Eigen::MatrixXf &hist_f3)
181 {
182  // Allocate enough space to hold the NN search results
183  // \note This resize is irrelevant for a radiusSearch ().
184  std::vector<int> nn_indices (k_);
185  std::vector<float> nn_dists (k_);
186 
187  std::set<int> spfh_indices;
188  spfh_hist_lookup.resize (surface_->points.size ());
189 
190  // Build a list of (unique) indices for which we will need to compute SPFH signatures
191  // (We need an SPFH signature for every point that is a neighbor of any point in input_[indices_])
192  if (surface_ != input_ ||
193  indices_->size () != surface_->points.size ())
194  {
195  for (const auto& p_idx: *indices_)
196  {
197  if (this->searchForNeighbors (p_idx, search_parameter_, nn_indices, nn_dists) == 0)
198  continue;
199 
200  spfh_indices.insert (nn_indices.begin (), nn_indices.end ());
201  }
202  }
203  else
204  {
205  // Special case: When a feature must be computed at every point, there is no need for a neighborhood search
206  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
207  spfh_indices.insert (static_cast<int> (idx));
208  }
209 
210  // Initialize the arrays that will store the SPFH signatures
211  std::size_t data_size = spfh_indices.size ();
212  hist_f1.setZero (data_size, nr_bins_f1_);
213  hist_f2.setZero (data_size, nr_bins_f2_);
214  hist_f3.setZero (data_size, nr_bins_f3_);
215 
216  // Compute SPFH signatures for every point that needs them
217  std::size_t i = 0;
218  for (const auto& p_idx: spfh_indices)
219  {
220  // Find the neighborhood around p_idx
221  if (this->searchForNeighbors (*surface_, p_idx, search_parameter_, nn_indices, nn_dists) == 0)
222  continue;
223 
224  // Estimate the SPFH signature around p_idx
225  computePointSPFHSignature (*surface_, *normals_, p_idx, i, nn_indices, hist_f1, hist_f2, hist_f3);
226 
227  // Populate a lookup table for converting a point index to its corresponding row in the spfh_hist_* matrices
228  spfh_hist_lookup[p_idx] = i;
229  i++;
230  }
231 }
232 
233 //////////////////////////////////////////////////////////////////////////////////////////////
234 template <typename PointInT, typename PointNT, typename PointOutT> void
236 {
237  // Allocate enough space to hold the NN search results
238  // \note This resize is irrelevant for a radiusSearch ().
239  std::vector<int> nn_indices (k_);
240  std::vector<float> nn_dists (k_);
241 
242  std::vector<int> spfh_hist_lookup;
243  computeSPFHSignatures (spfh_hist_lookup, hist_f1_, hist_f2_, hist_f3_);
244 
245  output.is_dense = true;
246  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
247  if (input_->is_dense)
248  {
249  // Iterate over the entire index vector
250  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
251  {
252  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
253  {
254  for (Eigen::Index d = 0; d < fpfh_histogram_.size (); ++d)
255  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
256 
257  output.is_dense = false;
258  continue;
259  }
260 
261  // ... and remap the nn_indices values so that they represent row indices in the spfh_hist_* matrices
262  // instead of indices into surface_->points
263  for (auto &nn_index : nn_indices)
264  nn_index = spfh_hist_lookup[nn_index];
265 
266  // Compute the FPFH signature (i.e. compute a weighted combination of local SPFH signatures) ...
267  weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram_);
268 
269  // ...and copy it into the output cloud
270  std::copy_n(fpfh_histogram_.data (), fpfh_histogram_.size (), output.points[idx].histogram);
271  }
272  }
273  else
274  {
275  // Iterate over the entire index vector
276  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
277  {
278  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
279  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
280  {
281  for (Eigen::Index d = 0; d < fpfh_histogram_.size (); ++d)
282  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
283 
284  output.is_dense = false;
285  continue;
286  }
287 
288  // ... and remap the nn_indices values so that they represent row indices in the spfh_hist_* matrices
289  // instead of indices into surface_->points
290  for (auto &nn_index : nn_indices)
291  nn_index = spfh_hist_lookup[nn_index];
292 
293  // Compute the FPFH signature (i.e. compute a weighted combination of local SPFH signatures) ...
294  weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram_);
295 
296  // ...and copy it into the output cloud
297  std::copy_n(fpfh_histogram_.data (), fpfh_histogram_.size (), output.points[idx].histogram);
298  }
299  }
300 }
301 
302 #define PCL_INSTANTIATE_FPFHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::FPFHEstimation<T,NT,OutT>;
303 
304 #endif // PCL_FEATURES_IMPL_FPFH_H_
305 
pcl::PointCloud::points
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:397
pcl::isFinite
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
pcl::FPFHEstimation::PointCloudOut
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: fpfh.h:93
pcl::PointCloud< PointInT >
pcl::FPFHEstimation::computeSPFHSignatures
void computeSPFHSignatures(std::vector< int > &spf_hist_lookup, Eigen::MatrixXf &hist_f1, Eigen::MatrixXf &hist_f2, Eigen::MatrixXf &hist_f3)
Estimate the set of all SPFH (Simple Point Feature Histograms) signatures for the input cloud.
Definition: fpfh.hpp:179
pcl::FPFHEstimation::computePointSPFHSignature
void computePointSPFHSignature(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, int p_idx, int row, const std::vector< int > &indices, Eigen::MatrixXf &hist_f1, Eigen::MatrixXf &hist_f2, Eigen::MatrixXf &hist_f3)
Estimate the SPFH (Simple Point Feature Histograms) individual signatures of the three angular (f1,...
Definition: fpfh.hpp:61
pcl::FPFHEstimation::weightPointSPFHSignature
void weightPointSPFHSignature(const Eigen::MatrixXf &hist_f1, const Eigen::MatrixXf &hist_f2, const Eigen::MatrixXf &hist_f3, const std::vector< int > &indices, const std::vector< float > &dists, Eigen::VectorXf &fpfh_histogram)
Weight the SPFH (Simple Point Feature Histograms) individual histograms to create the final FPFH (Fas...
Definition: fpfh.hpp:107
pcl::computePairFeatures
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...
pcl::FPFHEstimation::computePairFeatures
bool computePairFeatures(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4)
Compute the 4-tuple representation containing the three angles and one distance between two points re...
Definition: fpfh.hpp:49
pcl::FPFHEstimation::computeFeature
void computeFeature(PointCloudOut &output) override
Estimate the Fast Point Feature Histograms (FPFH) descriptors at a set of points given by <setInputCl...
Definition: fpfh.hpp:235