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