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