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