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