Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pfh.hpp
Go to the documentation of this file.
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  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id: pfh.hpp 5026 2012-03-12 02:51:44Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_PFH_H_
41 #define PCL_FEATURES_IMPL_PFH_H_
42 
43 #include <pcl/features/pfh.h>
44 
46 template <typename PointInT, typename PointNT, typename PointOutT> bool
48  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
49  int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4)
50 {
51  pcl::computePairFeatures (cloud.points[p_idx].getVector4fMap (), normals.points[p_idx].getNormalVector4fMap (),
52  cloud.points[q_idx].getVector4fMap (), normals.points[q_idx].getNormalVector4fMap (),
53  f1, f2, f3, f4);
54  return (true);
55 }
56 
58 template <typename PointInT, typename PointNT, typename PointOutT> void
60  const pcl::PointCloud<PointInT> &cloud, const pcl::PointCloud<PointNT> &normals,
61  const std::vector<int> &indices, int nr_split, Eigen::VectorXf &pfh_histogram)
62 {
63  int h_index, h_p;
64 
65  // Clear the resultant point histogram
66  pfh_histogram.setZero ();
67 
68  // Factorization constant
69  float hist_incr = 100.0f / static_cast<float> (indices.size () * (indices.size () - 1) / 2);
70 
71  std::pair<int, int> key;
72  // Iterate over all the points in the neighborhood
73  for (size_t i_idx = 0; i_idx < indices.size (); ++i_idx)
74  {
75  for (size_t j_idx = 0; j_idx < i_idx; ++j_idx)
76  {
77  // If the 3D points are invalid, don't bother estimating, just continue
78  if (!isFinite (cloud.points[indices[i_idx]]) || !isFinite (cloud.points[indices[j_idx]]))
79  continue;
80 
81  if (use_cache_)
82  {
83  // In order to create the key, always use the smaller index as the first key pair member
84  int p1, p2;
85  // if (indices[i_idx] >= indices[j_idx])
86  // {
87  p1 = indices[i_idx];
88  p2 = indices[j_idx];
89  // }
90  // else
91  // {
92  // p1 = indices[j_idx];
93  // p2 = indices[i_idx];
94  // }
95  key = std::pair<int, int> (p1, p2);
96 
97  // Check to see if we already estimated this pair in the global hashmap
98  std::map<std::pair<int, int>, Eigen::Vector4f, std::less<std::pair<int, int> >, Eigen::aligned_allocator<Eigen::Vector4f> >::iterator fm_it = feature_map_.find (key);
99  if (fm_it != feature_map_.end ())
100  pfh_tuple_ = fm_it->second;
101  else
102  {
103  // Compute the pair NNi to NNj
104  if (!computePairFeatures (cloud, normals, indices[i_idx], indices[j_idx],
105  pfh_tuple_[0], pfh_tuple_[1], pfh_tuple_[2], pfh_tuple_[3]))
106  continue;
107  }
108  }
109  else
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  // Normalize the f1, f2, f3 features and push them in the histogram
115  f_index_[0] = static_cast<int> (floor (nr_split * ((pfh_tuple_[0] + M_PI) * d_pi_)));
116  if (f_index_[0] < 0) f_index_[0] = 0;
117  if (f_index_[0] >= nr_split) f_index_[0] = nr_split - 1;
118 
119  f_index_[1] = static_cast<int> (floor (nr_split * ((pfh_tuple_[1] + 1.0) * 0.5)));
120  if (f_index_[1] < 0) f_index_[1] = 0;
121  if (f_index_[1] >= nr_split) f_index_[1] = nr_split - 1;
122 
123  f_index_[2] = static_cast<int> (floor (nr_split * ((pfh_tuple_[2] + 1.0) * 0.5)));
124  if (f_index_[2] < 0) f_index_[2] = 0;
125  if (f_index_[2] >= nr_split) f_index_[2] = nr_split - 1;
126 
127  // Copy into the histogram
128  h_index = 0;
129  h_p = 1;
130  for (int d = 0; d < 3; ++d)
131  {
132  h_index += h_p * f_index_[d];
133  h_p *= nr_split;
134  }
135  pfh_histogram[h_index] += hist_incr;
136 
137  if (use_cache_)
138  {
139  // Save the value in the hashmap
140  feature_map_[key] = pfh_tuple_;
141 
142  // Use a maximum cache so that we don't go overboard on RAM usage
143  key_list_.push (key);
144  // Check to see if we need to remove an element due to exceeding max_size
145  if (key_list_.size () > max_cache_size_)
146  {
147  // Remove the last element.
148  feature_map_.erase (key_list_.back ());
149  key_list_.pop ();
150  }
151  }
152  }
153  }
154 }
155 
157 template <typename PointInT, typename PointNT, typename PointOutT> void
159 {
160  // Clear the feature map
161  feature_map_.clear ();
162  std::queue<std::pair<int, int> > empty;
163  std::swap (key_list_, empty);
164 
165  pfh_histogram_.setZero (nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
166 
167  // Allocate enough space to hold the results
168  // \note This resize is irrelevant for a radiusSearch ().
169  std::vector<int> nn_indices (k_);
170  std::vector<float> nn_dists (k_);
171 
172  output.is_dense = true;
173  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
174  if (input_->is_dense)
175  {
176  // Iterating over the entire index vector
177  for (size_t idx = 0; idx < indices_->size (); ++idx)
178  {
179  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
180  {
181  for (int d = 0; d < pfh_histogram_.size (); ++d)
182  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
183 
184  output.is_dense = false;
185  continue;
186  }
187 
188  // Estimate the PFH signature at each patch
189  computePointPFHSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfh_histogram_);
190 
191  // Copy into the resultant cloud
192  for (int d = 0; d < pfh_histogram_.size (); ++d)
193  output.points[idx].histogram[d] = pfh_histogram_[d];
194  }
195  }
196  else
197  {
198  // Iterating over the entire index vector
199  for (size_t idx = 0; idx < indices_->size (); ++idx)
200  {
201  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
202  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
203  {
204  for (int d = 0; d < pfh_histogram_.size (); ++d)
205  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
206 
207  output.is_dense = false;
208  continue;
209  }
210 
211  // Estimate the PFH signature at each patch
212  computePointPFHSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfh_histogram_);
213 
214  // Copy into the resultant cloud
215  for (int d = 0; d < pfh_histogram_.size (); ++d)
216  output.points[idx].histogram[d] = pfh_histogram_[d];
217  }
218  }
219 }
220 
222 template <typename PointInT, typename PointNT> void
224 {
225  // Set up the output channels
226  output.channels["pfh"].name = "pfh";
227  output.channels["pfh"].offset = 0;
228  output.channels["pfh"].size = 4;
229  output.channels["pfh"].count = nr_subdiv_ * nr_subdiv_ * nr_subdiv_;
230  output.channels["pfh"].datatype = sensor_msgs::PointField::FLOAT32;
231 
232  // Clear the feature map
233  feature_map_.clear ();
234  std::queue<std::pair<int, int> > empty;
235  std::swap (key_list_, empty);
236  pfh_histogram_.setZero (nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
237 
238  // Allocate enough space to hold the results
239  output.points.resize (indices_->size (), nr_subdiv_ * nr_subdiv_ * nr_subdiv_);
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  output.is_dense = true;
245  // Save a few cycles by not checking every point for NaN/Inf values if the cloud is set to dense
246  if (input_->is_dense)
247  {
248  // Iterating over the entire index vector
249  for (size_t idx = 0; idx < indices_->size (); ++idx)
250  {
251  if (this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
252  {
253  output.points.row (idx).setConstant (std::numeric_limits<float>::quiet_NaN ());
254  output.is_dense = false;
255  continue;
256  }
257 
258  // Estimate the PFH signature at each patch
259  computePointPFHSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfh_histogram_);
260  output.points.row (idx) = pfh_histogram_;
261  }
262  }
263  else
264  {
265  // Iterating over the entire index vector
266  for (size_t idx = 0; idx < indices_->size (); ++idx)
267  {
268  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
269  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
270  {
271  output.points.row (idx).setConstant (std::numeric_limits<float>::quiet_NaN ());
272  output.is_dense = false;
273  continue;
274  }
275 
276  // Estimate the PFH signature at each patch
277  computePointPFHSignature (*surface_, *normals_, nn_indices, nr_subdiv_, pfh_histogram_);
278  output.points.row (idx) = pfh_histogram_;
279  }
280  }
281 }
282 
283 #define PCL_INSTANTIATE_PFHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::PFHEstimation<T,NT,OutT>;
284 
285 #endif // PCL_FEATURES_IMPL_PFH_H_
286