Point Cloud Library (PCL)  1.11.0-dev
pfh.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  */
38 
39 #pragma once
40 
41 #include <pcl/features/pfh.h>
42 
43 #include <pcl/common/point_tests.h> // for pcl::isFinite
44 
45 
46 //////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointInT, typename PointNT, typename PointOutT> bool
49  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
50  int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4)
51 {
52  pcl::computePairFeatures (cloud.points[p_idx].getVector4fMap (), normals.points[p_idx].getNormalVector4fMap (),
53  cloud.points[q_idx].getVector4fMap (), normals.points[q_idx].getNormalVector4fMap (),
54  f1, f2, f3, f4);
55  return (true);
56 }
57 
58 //////////////////////////////////////////////////////////////////////////////////////////////
59 template <typename PointInT, typename PointNT, typename PointOutT> void
61  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
62  const std::vector<int> &indices, int nr_split, Eigen::VectorXf &pfh_histogram)
63 {
64  int h_index, h_p;
65 
66  // Clear the resultant point histogram
67  pfh_histogram.setZero ();
68 
69  // Factorization constant
70  float hist_incr = 100.0f / static_cast<float> (indices.size () * (indices.size () - 1) / 2);
71 
72  std::pair<int, int> key;
73  bool key_found = false;
74 
75  // Iterate over all the points in the neighborhood
76  for (std::size_t i_idx = 0; i_idx < indices.size (); ++i_idx)
77  {
78  for (std::size_t j_idx = 0; j_idx < i_idx; ++j_idx)
79  {
80  // If the 3D points are invalid, don't bother estimating, just continue
81  if (!isFinite (cloud.points[indices[i_idx]]) || !isFinite (cloud.points[indices[j_idx]]))
82  continue;
83 
84  if (use_cache_)
85  {
86  // In order to create the key, always use the smaller index as the first key pair member
87  int p1, p2;
88  // if (indices[i_idx] >= indices[j_idx])
89  // {
90  p1 = indices[i_idx];
91  p2 = indices[j_idx];
92  // }
93  // else
94  // {
95  // p1 = indices[j_idx];
96  // p2 = indices[i_idx];
97  // }
98  key = std::pair<int, int> (p1, p2);
99 
100  // Check to see if we already estimated this pair in the global hashmap
101  std::map<std::pair<int, int>, Eigen::Vector4f, std::less<>, Eigen::aligned_allocator<std::pair<const std::pair<int, int>, Eigen::Vector4f> > >::iterator fm_it = feature_map_.find (key);
102  if (fm_it != feature_map_.end ())
103  {
104  pfh_tuple_ = fm_it->second;
105  key_found = true;
106  }
107  else
108  {
109  // Compute the pair NNi to NNj
110  if (!computePairFeatures (cloud, normals, indices[i_idx], indices[j_idx],
111  pfh_tuple_[0], pfh_tuple_[1], pfh_tuple_[2], pfh_tuple_[3]))
112  continue;
113 
114  key_found = false;
115  }
116  }
117  else
118  if (!computePairFeatures (cloud, normals, indices[i_idx], indices[j_idx],
119  pfh_tuple_[0], pfh_tuple_[1], pfh_tuple_[2], pfh_tuple_[3]))
120  continue;
121 
122  // Normalize the f1, f2, f3 features and push them in the histogram
123  f_index_[0] = static_cast<int> (std::floor (nr_split * ((pfh_tuple_[0] + M_PI) * d_pi_)));
124  if (f_index_[0] < 0) f_index_[0] = 0;
125  if (f_index_[0] >= nr_split) f_index_[0] = nr_split - 1;
126 
127  f_index_[1] = static_cast<int> (std::floor (nr_split * ((pfh_tuple_[1] + 1.0) * 0.5)));
128  if (f_index_[1] < 0) f_index_[1] = 0;
129  if (f_index_[1] >= nr_split) f_index_[1] = nr_split - 1;
130 
131  f_index_[2] = static_cast<int> (std::floor (nr_split * ((pfh_tuple_[2] + 1.0) * 0.5)));
132  if (f_index_[2] < 0) f_index_[2] = 0;
133  if (f_index_[2] >= nr_split) f_index_[2] = nr_split - 1;
134 
135  // Copy into the histogram
136  h_index = 0;
137  h_p = 1;
138  for (const int &d : f_index_)
139  {
140  h_index += h_p * d;
141  h_p *= nr_split;
142  }
143  pfh_histogram[h_index] += hist_incr;
144 
145  if (use_cache_ && !key_found)
146  {
147  // Save the value in the hashmap
148  feature_map_[key] = pfh_tuple_;
149 
150  // Use a maximum cache so that we don't go overboard on RAM usage
151  key_list_.push (key);
152  // Check to see if we need to remove an element due to exceeding max_size
153  if (key_list_.size () > max_cache_size_)
154  {
155  // Remove the oldest element.
156  feature_map_.erase (key_list_.front ());
157  key_list_.pop ();
158  }
159  }
160  }
161  }
162 }
163 
164 //////////////////////////////////////////////////////////////////////////////////////////////
165 template <typename PointInT, typename PointNT, typename PointOutT> void
167 {
168  // Clear the feature map
169  feature_map_.clear ();
170  std::queue<std::pair<int, int> > empty;
171  std::swap (key_list_, empty);
172 
173  pfh_histogram_.setZero (nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
174 
175  // Allocate enough space to hold the results
176  // \note This resize is irrelevant for a radiusSearch ().
177  std::vector<int> nn_indices (k_);
178  std::vector<float> nn_dists (k_);
179 
180  output.is_dense = true;
181  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
182  if (input_->is_dense)
183  {
184  // Iterating over the entire index vector
185  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
186  {
187  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
188  {
189  for (Eigen::Index d = 0; d < pfh_histogram_.size (); ++d)
190  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
191 
192  output.is_dense = false;
193  continue;
194  }
195 
196  // Estimate the PFH signature at each patch
197  computePointPFHSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfh_histogram_);
198 
199  // Copy into the resultant cloud
200  for (Eigen::Index d = 0; d < pfh_histogram_.size (); ++d)
201  output.points[idx].histogram[d] = pfh_histogram_[d];
202  }
203  }
204  else
205  {
206  // Iterating over the entire index vector
207  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
208  {
209  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
210  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
211  {
212  for (Eigen::Index d = 0; d < pfh_histogram_.size (); ++d)
213  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
214 
215  output.is_dense = false;
216  continue;
217  }
218 
219  // Estimate the PFH signature at each patch
220  computePointPFHSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfh_histogram_);
221 
222  // Copy into the resultant cloud
223  for (Eigen::Index d = 0; d < pfh_histogram_.size (); ++d)
224  output.points[idx].histogram[d] = pfh_histogram_[d];
225  }
226  }
227 }
228 
229 #define PCL_INSTANTIATE_PFHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PFHEstimation<T,NT,OutT>;
230 
pcl::PFHEstimation::computePointPFHSignature
void computePointPFHSignature(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, const std::vector< int > &indices, int nr_split, Eigen::VectorXf &pfh_histogram)
Estimate the PFH (Point Feature Histograms) individual signatures of the three angular (f1,...
Definition: pfh.hpp:60
pcl::PFHEstimation::PointCloudOut
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: pfh.h:95
pcl::PointCloud::points
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:411
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::PointCloud< PointInT >
M_PI
#define M_PI
Definition: pcl_macros.h:195
pcl::PFHEstimation::computeFeature
void computeFeature(PointCloudOut &output) override
Estimate the Point Feature Histograms (PFH) descriptors at a set of points given by <setInputCloud ()...
Definition: pfh.hpp:166
pcl::PFHEstimation::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: pfh.hpp:48
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...