Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
fpfh_omp.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2011, Willow Garage, Inc.
00006  *
00007  *  All rights reserved.
00008  *
00009  *  Redistribution and use in source and binary forms, with or without
00010  *  modification, are permitted provided that the following conditions
00011  *  are met:
00012  *
00013  *   * Redistributions of source code must retain the above copyright
00014  *     notice, this list of conditions and the following disclaimer.
00015  *   * Redistributions in binary form must reproduce the above
00016  *     copyright notice, this list of conditions and the following
00017  *     disclaimer in the documentation and/or other materials provided
00018  *     with the distribution.
00019  *   * Neither the name of Willow Garage, Inc. nor the names of its
00020  *     contributors may be used to endorse or promote products derived
00021  *     from this software without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  * $Id: fpfh_omp.hpp 3755 2011-12-31 23:45:30Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_FEATURES_IMPL_FPFH_OMP_H_
00041 #define PCL_FEATURES_IMPL_FPFH_OMP_H_
00042 
00043 #include "pcl/features/fpfh_omp.h"
00044 
00046 template <typename PointInT, typename PointNT, typename PointOutT> void
00047 pcl::FPFHEstimationOMP<PointInT, PointNT, PointOutT>::computeFeature (PointCloudOut &output)
00048 {
00049   std::vector<int> spfh_indices_vec;
00050   std::vector<int> spfh_hist_lookup (surface_->points.size ());
00051 
00052   // Build a list of (unique) indices for which we will need to compute SPFH signatures
00053   // (We need an SPFH signature for every point that is a neighbor of any point in input_[indices_])
00054   if (surface_ != input_ ||
00055       indices_->size () != surface_->points.size ())
00056   { 
00057     std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
00058     std::vector<float> nn_dists (k_); 
00059 
00060     std::set<int> spfh_indices_set;
00061     for (size_t idx = 0; idx < indices_->size (); ++idx)
00062     {
00063       int p_idx = (*indices_)[idx];
00064       this->searchForNeighbors (p_idx, search_parameter_, nn_indices, nn_dists);
00065       
00066       spfh_indices_set.insert (nn_indices.begin (), nn_indices.end ());
00067     }
00068     spfh_indices_vec.resize (spfh_indices_set.size ());
00069     std::copy (spfh_indices_set.begin (), spfh_indices_set.end (), spfh_indices_vec.begin ());
00070   }
00071   else
00072   {
00073     // Special case: When a feature must be computed at every point, there is no need for a neighborhood search
00074     spfh_indices_vec.resize (indices_->size ());
00075     for (size_t idx = 0; idx < indices_->size (); ++idx)
00076       spfh_indices_vec[idx] = idx;
00077   }
00078 
00079   // Initialize the arrays that will store the SPFH signatures
00080   size_t data_size = spfh_indices_vec.size ();
00081   hist_f1_.setZero (data_size, nr_bins_f1_);
00082   hist_f2_.setZero (data_size, nr_bins_f2_);
00083   hist_f3_.setZero (data_size, nr_bins_f3_);
00084 
00085   // Compute SPFH signatures for every point that needs them
00086   
00087 #pragma omp parallel for schedule (dynamic, threads_)
00088   for (int i = 0; i < (int) spfh_indices_vec.size (); ++i)
00089   {
00090     // Get the next point index
00091     int p_idx = spfh_indices_vec[i];
00092 
00093     // Find the neighborhood around p_idx
00094     std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
00095     std::vector<float> nn_dists (k_); 
00096     this->searchForNeighbors (*surface_, p_idx, search_parameter_, nn_indices, nn_dists);
00097 
00098     // Estimate the SPFH signature around p_idx
00099     this->computePointSPFHSignature (*surface_, *normals_, p_idx, i, nn_indices, hist_f1_, hist_f2_, hist_f3_);
00100 
00101     // Populate a lookup table for converting a point index to its corresponding row in the spfh_hist_* matrices
00102     spfh_hist_lookup[p_idx] = i;
00103   }
00104 
00105   // Intialize the array that will store the FPFH signature
00106   int nr_bins = nr_bins_f1_ + nr_bins_f2_ + nr_bins_f3_;
00107 
00108   // Iterate over the entire index vector
00109 #pragma omp parallel for schedule (dynamic, threads_)
00110   for (int idx = 0; idx < (int) indices_->size (); ++idx)
00111   {
00112     // Find the indices of point idx's neighbors...
00113     std::vector<int> nn_indices (k_); // \note These resizes are irrelevant for a radiusSearch ().
00114     std::vector<float> nn_dists (k_); 
00115     this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_dists);
00116 
00117     // ... and remap the nn_indices values so that they represent row indices in the spfh_hist_* matrices 
00118     // instead of indices into surface_->points
00119     for (size_t i = 0; i < nn_indices.size (); ++i)
00120       nn_indices[i] = spfh_hist_lookup[nn_indices[i]];
00121 
00122     // Compute the FPFH signature (i.e. compute a weighted combination of local SPFH signatures) ...
00123     Eigen::VectorXf fpfh_histogram = Eigen::VectorXf::Zero (nr_bins);
00124     weightPointSPFHSignature (hist_f1_, hist_f2_, hist_f3_, nn_indices, nn_dists, fpfh_histogram);
00125 
00126     // ...and copy it into the output cloud
00127     for (int d = 0; d < fpfh_histogram.size (); ++d)
00128       output.points[idx].histogram[d] = fpfh_histogram[d];
00129   }
00130 
00131 }
00132 
00133 #define PCL_INSTANTIATE_FPFHEstimationOMP(T,NT,OutT) template class PCL_EXPORTS pcl::FPFHEstimationOMP<T,NT,OutT>;
00134 
00135 #endif    // PCL_FEATURES_IMPL_FPFH_OMP_H_ 
00136 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines