Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
vfh.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: vfh.hpp 3755 2011-12-31 23:45:30Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_FEATURES_IMPL_VFH_H_
00041 #define PCL_FEATURES_IMPL_VFH_H_
00042 
00043 #include "pcl/features/vfh.h"
00044 #include "pcl/features/pfh.h"
00045 #include <pcl/common/common.h>
00046 
00048 template<typename PointInT, typename PointNT, typename PointOutT> void
00049 pcl::VFHEstimation<PointInT, PointNT, PointOutT>::computePointSPFHSignature (const Eigen::Vector4f &centroid_p,
00050                                                                              const Eigen::Vector4f &centroid_n,
00051                                                                              const pcl::PointCloud<PointInT> &cloud,
00052                                                                              const pcl::PointCloud<PointNT> &normals,
00053                                                                              const std::vector<int> &indices)
00054 {
00055   Eigen::Vector4f pfh_tuple;
00056   // Reset the whole thing
00057   hist_f1_.setZero (nr_bins_f1_);
00058   hist_f2_.setZero (nr_bins_f2_);
00059   hist_f3_.setZero (nr_bins_f3_);
00060   hist_f4_.setZero (nr_bins_f4_);
00061 
00062   // Get the bounding box of the current cluster
00063   //Eigen::Vector4f min_pt, max_pt;
00064   //pcl::getMinMax3D (cloud, indices, min_pt, max_pt);
00065   //double distance_normalization_factor = (std::max)((centroid_p - min_pt).norm (), (centroid_p - max_pt).norm ());
00066 
00067   //Instead of using the bounding box to normalize the VFH distance component, it is better to use the max_distance
00068   //from any point to centroid. VFH is invariant to rotation about the roll axis but the bounding box is not,
00069   //resulting in different normalization factors for point clouds that are just rotated about that axis.
00070 
00071   double distance_normalization_factor = 1.0;
00072   if (normalize_distances_) 
00073   {
00074     Eigen::Vector4f max_pt;
00075     pcl::getMaxDistance (cloud, indices, centroid_p, max_pt);
00076     max_pt[3] = 0;
00077     distance_normalization_factor = (centroid_p - max_pt).norm ();
00078   }
00079 
00080   // Factorization constant
00081   float hist_incr;
00082   if (normalize_bins_)
00083     hist_incr = 100.0 / (float)(indices.size () - 1);
00084   else
00085     hist_incr = 1.0;
00086 
00087   float hist_incr_size_component;
00088   if (size_component_)
00089     hist_incr_size_component = hist_incr;
00090   else
00091     hist_incr_size_component = 0.0;
00092 
00093   // Iterate over all the points in the neighborhood
00094   for (size_t idx = 0; idx < indices.size (); ++idx)
00095   {
00096     // Compute the pair P to NNi
00097     if (!computePairFeatures (centroid_p, centroid_n, cloud.points[indices[idx]].getVector4fMap (),
00098                               normals.points[indices[idx]].getNormalVector4fMap (), pfh_tuple[0], pfh_tuple[1],
00099                               pfh_tuple[2], pfh_tuple[3]))
00100       continue;
00101 
00102     // Normalize the f1, f2, f3, f4 features and push them in the histogram
00103     int h_index = floor (nr_bins_f1_ * ((pfh_tuple[0] + M_PI) * d_pi_));
00104     if (h_index < 0)
00105       h_index = 0;
00106     if (h_index >= nr_bins_f1_)
00107       h_index = nr_bins_f1_ - 1;
00108     hist_f1_ (h_index) += hist_incr;
00109 
00110     h_index = floor (nr_bins_f2_ * ((pfh_tuple[1] + 1.0) * 0.5));
00111     if (h_index < 0)
00112       h_index = 0;
00113     if (h_index >= nr_bins_f2_)
00114       h_index = nr_bins_f2_ - 1;
00115     hist_f2_ (h_index) += hist_incr;
00116 
00117     h_index = floor (nr_bins_f3_ * ((pfh_tuple[2] + 1.0) * 0.5));
00118     if (h_index < 0)
00119       h_index = 0;
00120     if (h_index >= nr_bins_f3_)
00121       h_index = nr_bins_f3_ - 1;
00122     hist_f3_ (h_index) += hist_incr;
00123 
00124     if (normalize_distances_)
00125       h_index = floor (nr_bins_f4_ * (pfh_tuple[3] / distance_normalization_factor));
00126     else
00127       h_index = pcl_round (pfh_tuple[3] * 100);
00128 
00129     if (h_index < 0)
00130       h_index = 0;
00131     if (h_index >= nr_bins_f4_)
00132       h_index = nr_bins_f4_ - 1;
00133 
00134     hist_f4_ (h_index) += hist_incr_size_component;
00135   }
00136 }
00138 template <typename PointInT, typename PointNT, typename PointOutT> void
00139 pcl::VFHEstimation<PointInT, PointNT, PointOutT>::computeFeature (PointCloudOut &output)
00140 {
00141   // ---[ Step 1a : compute the centroid in XYZ space
00142   Eigen::Vector4f xyz_centroid;
00143 
00144   if (use_given_centroid_) 
00145     xyz_centroid = centroid_to_use_;
00146   else
00147     compute3DCentroid (*surface_, *indices_, xyz_centroid);          // Estimate the XYZ centroid
00148 
00149   // ---[ Step 1b : compute the centroid in normal space
00150   Eigen::Vector4f normal_centroid = Eigen::Vector4f::Zero ();
00151   int cp = 0;
00152 
00153   // If the data is dense, we don't need to check for NaN
00154   if (use_given_normal_)
00155     normal_centroid = normal_to_use_;
00156   else
00157   {
00158     if (normals_->is_dense)
00159     {
00160       for (size_t i = 0; i < indices_->size (); ++i)
00161       {
00162         normal_centroid += normals_->points[(*indices_)[i]].getNormalVector4fMap ();
00163         cp++;
00164       }
00165     }
00166     // NaN or Inf values could exist => check for them
00167     else
00168     {
00169       for (size_t i = 0; i < indices_->size (); ++i)
00170       {
00171         if (!pcl_isfinite (normals_->points[(*indices_)[i]].normal[0])
00172             ||
00173             !pcl_isfinite (normals_->points[(*indices_)[i]].normal[1])
00174             ||
00175             !pcl_isfinite (normals_->points[(*indices_)[i]].normal[2]))
00176           continue;
00177         normal_centroid += normals_->points[(*indices_)[i]].getNormalVector4fMap ();
00178         cp++;
00179       }
00180     }
00181     normal_centroid /= cp;
00182   }
00183 
00184   // Compute the direction of view from the viewpoint to the centroid
00185   Eigen::Vector4f viewpoint (vpx_, vpy_, vpz_, 0);
00186   Eigen::Vector4f d_vp_p = viewpoint - xyz_centroid;
00187   d_vp_p.normalize ();
00188 
00189   // Estimate the SPFH at nn_indices[0] using the entire cloud
00190   computePointSPFHSignature (xyz_centroid, normal_centroid, *surface_, *normals_, *indices_);
00191 
00192   // We only output _1_ signature
00193   output.points.resize (1);
00194   output.width = 1;
00195   output.height = 1;
00196 
00197   // Estimate the FPFH at nn_indices[0] using the entire cloud and copy the resultant signature
00198   for (int d = 0; d < hist_f1_.size (); ++d)
00199     output.points[0].histogram[d + 0] = hist_f1_[d];
00200 
00201   size_t data_size = hist_f1_.size ();
00202   for (int d = 0; d < hist_f2_.size (); ++d)
00203     output.points[0].histogram[d + data_size] = hist_f2_[d];
00204 
00205   data_size += hist_f2_.size ();
00206   for (int d = 0; d < hist_f3_.size (); ++d)
00207     output.points[0].histogram[d + data_size] = hist_f3_[d];
00208 
00209   data_size += hist_f3_.size ();
00210   for (int d = 0; d < hist_f4_.size (); ++d)
00211     output.points[0].histogram[d + data_size] = hist_f4_[d];
00212 
00213   // ---[ Step 2 : obtain the viewpoint component
00214   hist_vp_.setZero (nr_bins_vp_);
00215 
00216   double hist_incr;
00217   if (normalize_bins_)
00218     hist_incr = 100.0 / (double)(indices_->size ());
00219   else
00220     hist_incr = 1.0;
00221 
00222   for (size_t i = 0; i < indices_->size (); ++i)
00223   {
00224     Eigen::Vector4f normal (normals_->points[(*indices_)[i]].normal[0],
00225                             normals_->points[(*indices_)[i]].normal[1],
00226                             normals_->points[(*indices_)[i]].normal[2], 0);
00227     // Normalize
00228     double alpha = (normal.dot (d_vp_p) + 1.0) * 0.5;
00229     int fi = floor (alpha * hist_vp_.size ());
00230     if (fi < 0)
00231       fi = 0;
00232     if (fi > ((int)hist_vp_.size () - 1))
00233       fi = hist_vp_.size () - 1;
00234     // Bin into the histogram
00235     hist_vp_ [fi] += hist_incr;
00236   }
00237   data_size += hist_f4_.size ();
00238   // Copy the resultant signature
00239   for (int d = 0; d < hist_vp_.size (); ++d)
00240     output.points[0].histogram[d + data_size] = hist_vp_[d];
00241 }
00242 
00243 #define PCL_INSTANTIATE_VFHEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::VFHEstimation<T,NT,OutT>;
00244 
00245 #endif    // PCL_FEATURES_IMPL_VFH_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines