Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fpfh_omp.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: fpfh_omp.hpp 5026 2012-03-12 02:51:44Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FEATURES_IMPL_FPFH_OMP_H_
41 #define PCL_FEATURES_IMPL_FPFH_OMP_H_
42 
43 #include <pcl/features/fpfh_omp.h>
44 
46 template <typename PointInT, typename PointNT, typename PointOutT> void
48 {
49  std::vector<int> spfh_indices_vec;
50  std::vector<int> spfh_hist_lookup (surface_->points.size ());
51 
52  // Build a list of (unique) indices for which we will need to compute SPFH signatures
53  // (We need an SPFH signature for every point that is a neighbor of any point in input_[indices_])
54  if (surface_ != input_ ||
55  indices_->size () != surface_->points.size ())
56  {
57  std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
58  std::vector<float> nn_dists (k_);
59 
60  std::set<int> spfh_indices_set;
61  for (size_t idx = 0; idx < indices_->size (); ++idx)
62  {
63  int p_idx = (*indices_)[idx];
64  if (this->searchForNeighbors (p_idx, search_parameter_, nn_indices, nn_dists) == 0)
65  continue;
66 
67  spfh_indices_set.insert (nn_indices.begin (), nn_indices.end ());
68  }
69  spfh_indices_vec.resize (spfh_indices_set.size ());
70  std::copy (spfh_indices_set.begin (), spfh_indices_set.end (), spfh_indices_vec.begin ());
71  }
72  else
73  {
74  // Special case: When a feature must be computed at every point, there is no need for a neighborhood search
75  spfh_indices_vec.resize (indices_->size ());
76  for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
77  spfh_indices_vec[idx] = idx;
78  }
79 
80  // Initialize the arrays that will store the SPFH signatures
81  size_t data_size = spfh_indices_vec.size ();
82  hist_f1_.setZero (data_size, nr_bins_f1_);
83  hist_f2_.setZero (data_size, nr_bins_f2_);
84  hist_f3_.setZero (data_size, nr_bins_f3_);
85 
86  // Compute SPFH signatures for every point that needs them
87 
88 #pragma omp parallel for schedule (dynamic, threads_)
89  for (int i = 0; i < static_cast<int> (spfh_indices_vec.size ()); ++i)
90  {
91  // Get the next point index
92  int p_idx = spfh_indices_vec[i];
93 
94  // Find the neighborhood around p_idx
95  std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
96  std::vector<float> nn_dists (k_);
97  if (this->searchForNeighbors (*surface_, p_idx, search_parameter_, nn_indices, nn_dists) == 0)
98  continue;
99 
100  // Estimate the SPFH signature around p_idx
101  this->computePointSPFHSignature (*surface_, *normals_, p_idx, i, nn_indices, hist_f1_, hist_f2_, hist_f3_);
102 
103  // Populate a lookup table for converting a point index to its corresponding row in the spfh_hist_* matrices
104  spfh_hist_lookup[p_idx] = i;
105  }
106 
107  // Intialize the array that will store the FPFH signature
108  int nr_bins = nr_bins_f1_ + nr_bins_f2_ + nr_bins_f3_;
109 
110  // Iterate over the entire index vector
111 #pragma omp parallel for schedule (dynamic, threads_)
112  for (int idx = 0; idx < static_cast<int> (indices_->size ()); ++idx)
113  {
114  // Find the indices of point idx's neighbors...
115  std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
116  std::vector<float> nn_dists (k_);
117  if (!isFinite ((*input_)[(*indices_)[idx]]) ||
118  this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists) == 0)
119  {
120  for (int d = 0; d < nr_bins; ++d)
121  output.points[idx].histogram[d] = std::numeric_limits<float>::quiet_NaN ();
122 
123  output.is_dense = false;
124  continue;
125  }
126 
127 
128  // ... and remap the nn_indices values so that they represent row indices in the spfh_hist_* matrices
129  // instead of indices into surface_->points
130  for (size_t i = 0; i < nn_indices.size (); ++i)
131  nn_indices[i] = spfh_hist_lookup[nn_indices[i]];
132 
133  // Compute the FPFH signature (i.e. compute a weighted combination of local SPFH signatures) ...
134  Eigen::VectorXf fpfh_histogram = Eigen::VectorXf::Zero (nr_bins);
135  weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram);
136 
137  // ...and copy it into the output cloud
138  for (int d = 0; d < nr_bins; ++d)
139  output.points[idx].histogram[d] = fpfh_histogram[d];
140  }
141 
142 }
143 
144 #define PCL_INSTANTIATE_FPFHEstimationOMP(T,NT,OutT) template class PCL_EXPORTS pcl::FPFHEstimationOMP<T,NT,OutT>;
145 
146 #endif // PCL_FEATURES_IMPL_FPFH_OMP_H_
147