Point Cloud Library (PCL)  1.10.0-dev
fpfh_omp.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  * $Id$
38  *
39  */
40 
41 #ifndef PCL_FEATURES_IMPL_FPFH_OMP_H_
42 #define PCL_FEATURES_IMPL_FPFH_OMP_H_
43 
44 #include <numeric>
45 
46 #include <pcl/features/fpfh_omp.h>
47 
48 //////////////////////////////////////////////////////////////////////////////////////////////
49 template <typename PointInT, typename PointNT, typename PointOutT> void
51 {
52  if (nr_threads == 0)
53 #ifdef _OPENMP
54  threads_ = omp_get_num_procs();
55 #else
56  threads_ = 1;
57 #endif
58  else
59  threads_ = nr_threads;
60 }
61 
62 //////////////////////////////////////////////////////////////////////////////////////////////
63 template <typename PointInT, typename PointNT, typename PointOutT> void
65 {
66  std::vector<int> spfh_indices_vec;
67  std::vector<int> spfh_hist_lookup (surface_->points.size ());
68 
69  // Build a list of (unique) indices for which we will need to compute SPFH signatures
70  // (We need an SPFH signature for every point that is a neighbor of any point in input_[indices_])
71  if (surface_ != input_ ||
72  indices_->size () != surface_->points.size ())
73  {
74  std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
75  std::vector<float> nn_dists (k_);
76 
77  std::set<int> spfh_indices_set;
78  for (std::size_t idx = 0; idx < indices_->size (); ++idx)
79  {
80  int p_idx = (*indices_)[idx];
81  if (!isFinite ((*input_)[p_idx]) ||
82  this->searchForNeighbors (p_idx, search_parameter_, nn_indices, nn_dists) == 0)
83  continue;
84 
85  spfh_indices_set.insert (nn_indices.begin (), nn_indices.end ());
86  }
87  spfh_indices_vec.resize (spfh_indices_set.size ());
88  std::copy (spfh_indices_set.cbegin (), spfh_indices_set.cend (), spfh_indices_vec.begin ());
89  }
90  else
91  {
92  // Special case: When a feature must be computed at every point, there is no need for a neighborhood search
93  spfh_indices_vec.resize (indices_->size ());
94  std::iota(spfh_indices_vec.begin (), spfh_indices_vec.end (),
95  static_cast<decltype(spfh_indices_vec)::value_type>(0));
96  }
97 
98  // Initialize the arrays that will store the SPFH signatures
99  const auto data_size = spfh_indices_vec.size ();
100  hist_f1_.setZero (data_size, nr_bins_f1_);
101  hist_f2_.setZero (data_size, nr_bins_f2_);
102  hist_f3_.setZero (data_size, nr_bins_f3_);
103 
104  std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
105  std::vector<float> nn_dists (k_);
106 
107  // Compute SPFH signatures for every point that needs them
108 
109 #ifdef _OPENMP
110 #pragma omp parallel for shared (spfh_hist_lookup) private (nn_indices, nn_dists) num_threads(threads_)
111 #endif
112  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t> (spfh_indices_vec.size ()); ++i)
113  {
114  // Get the next point index
115  int p_idx = spfh_indices_vec[i];
116 
117  // Find the neighborhood around p_idx
118  if (!isFinite ((*input_)[p_idx]) ||
119  this->searchForNeighbors (*surface_, p_idx, search_parameter_, nn_indices, nn_dists) == 0)
120  continue;
121 
122  // Estimate the SPFH signature around p_idx
123  this->computePointSPFHSignature (*surface_, *normals_, p_idx, i, nn_indices, hist_f1_, hist_f2_, hist_f3_);
124 
125  // Populate a lookup table for converting a point index to its corresponding row in the spfh_hist_* matrices
126  spfh_hist_lookup[p_idx] = i;
127  }
128 
129  // Initialize the array that will store the FPFH signature
130  int nr_bins = nr_bins_f1_ + nr_bins_f2_ + nr_bins_f3_;
131 
132  nn_indices.clear();
133  nn_dists.clear();
134 
135  // Iterate over the entire index vector
136 #ifdef _OPENMP
137 #pragma omp parallel for shared (output) private (nn_indices, nn_dists) num_threads(threads_)
138 #endif
139  for (std::ptrdiff_t idx = 0; idx < static_cast<std::ptrdiff_t> (indices_->size ()); ++idx)
140  {
141  // Find the indices of point idx's neighbors...
142  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
143  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
144  {
145  for (int d = 0; d < nr_bins; ++d)
146  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
147 
148  output.is_dense = false;
149  continue;
150  }
151 
152 
153  // ... and remap the nn_indices values so that they represent row indices in the spfh_hist_* matrices
154  // instead of indices into surface_->points
155  for (int &nn_index : nn_indices)
156  nn_index = spfh_hist_lookup[nn_index];
157 
158  // Compute the FPFH signature (i.e. compute a weighted combination of local SPFH signatures) ...
159  Eigen::VectorXf fpfh_histogram = Eigen::VectorXf::Zero (nr_bins);
160  weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram);
161 
162  // ...and copy it into the output cloud
163  for (int d = 0; d < nr_bins; ++d)
164  output.points[idx].histogram[d] = fpfh_histogram[d];
165  }
166 
167 }
168 
169 #define PCL_INSTANTIATE_FPFHEstimationOMP(T,NT,OutT) template class PCL_EXPORTS pcl::FPFHEstimationOMP<T,NT,OutT>;
170 
171 #endif // PCL_FEATURES_IMPL_FPFH_OMP_H_
172 
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
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:397
void setNumberOfThreads(unsigned int nr_threads=0)
Initialize the scheduler and set the number of threads to use.
Definition: fpfh_omp.hpp:50
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:405
FPFHEstimationOMP estimates the Fast Point Feature Histogram (FPFH) descriptor for a given point clou...
Definition: fpfh_omp.h:74