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