Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
rsd.hpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: rsd.hpp 3755 2011-12-31 23:45:30Z rusu $
00035  *
00036  */
00037 
00038 #ifndef PCL_FEATURES_IMPL_RSD_H_
00039 #define PCL_FEATURES_IMPL_RSD_H_
00040 
00041 #include <cfloat>
00042 #include "pcl/features/rsd.h"
00043 
00045 template <typename PointInT, typename PointNT, typename PointOutT> inline void
00046 pcl::computeRSD (const pcl::PointCloud<PointInT> &surface, const pcl::PointCloud<PointNT> &normals,
00047      const std::vector<int> &indices, double max_dist,
00048      int nr_subdiv, double plane_radius, PointOutT &radii, Eigen::MatrixXf *histogram)
00049 {
00050   // Check if the full histogram has to be saved or not
00051   if (histogram)
00052     *histogram = Eigen::MatrixXf::Zero (nr_subdiv, nr_subdiv);
00053 
00054   // Initialize minimum and maximum angle values in each distance bin
00055   std::vector<std::vector<double> > min_max_angle_by_dist (nr_subdiv);
00056   min_max_angle_by_dist[0].resize (2);
00057   min_max_angle_by_dist[0][0] = min_max_angle_by_dist[0][1] = 0.0;
00058   for (int di=1; di<nr_subdiv; di++)
00059   {
00060     min_max_angle_by_dist[di].resize (2);
00061     min_max_angle_by_dist[di][0] = +DBL_MAX;
00062     min_max_angle_by_dist[di][1] = -DBL_MAX;
00063   }
00064 
00065   // Compute distance by normal angle distribution for points
00066   std::vector<int>::const_iterator i, begin (indices.begin()), end (indices.end());
00067   for(i = begin+1; i != end; ++i)
00068   {
00069     // compute angle between the two lines going through normals (disregard orientation!)
00070     double cosine = normals.points[*i].normal[0] * normals.points[*begin].normal[0] +
00071                     normals.points[*i].normal[1] * normals.points[*begin].normal[1] +
00072                     normals.points[*i].normal[2] * normals.points[*begin].normal[2];
00073     if (cosine > 1) cosine = 1;
00074     if (cosine < -1) cosine = -1;
00075     double angle  = acos (cosine);
00076     if (angle > M_PI/2) angle = M_PI - angle; 
00077 
00078     // Compute point to point distance
00079     double dist = sqrt ((surface.points[*i].x - surface.points[*begin].x) * (surface.points[*i].x - surface.points[*begin].x) +
00080                         (surface.points[*i].y - surface.points[*begin].y) * (surface.points[*i].y - surface.points[*begin].y) +
00081                         (surface.points[*i].z - surface.points[*begin].z) * (surface.points[*i].z - surface.points[*begin].z));
00082 
00083     if (dist > max_dist)
00084       continue; 
00085 
00086     // compute bins and increase
00087     int bin_d = (int) floor (nr_subdiv * dist / max_dist);
00088     if (histogram)
00089     {
00090       int bin_a = (int) floor (nr_subdiv * angle / (M_PI/2));
00091       (*histogram)(bin_a, bin_d)++;
00092     }
00093 
00094     // update min-max values for distance bins
00095     if (min_max_angle_by_dist[bin_d][0] > angle) min_max_angle_by_dist[bin_d][0] = angle;
00096     if (min_max_angle_by_dist[bin_d][1] < angle) min_max_angle_by_dist[bin_d][1] = angle;
00097   }
00098 
00099   // Estimate radius from min and max lines
00100   double Amint_Amin = 0, Amint_d = 0;
00101   double Amaxt_Amax = 0, Amaxt_d = 0;
00102   for (int di=0; di<nr_subdiv; di++)
00103   {
00104     // combute the members of A'*A*r = A'*D
00105     if (min_max_angle_by_dist[di][1] >= 0)
00106     {
00107       double p_min = min_max_angle_by_dist[di][0];
00108       double p_max = min_max_angle_by_dist[di][1];
00109       double f = (di+0.5)*max_dist/nr_subdiv;
00110       Amint_Amin += p_min * p_min;
00111       Amint_d += p_min * f;
00112       Amaxt_Amax += p_max * p_max;
00113       Amaxt_d += p_max * f;
00114     }
00115   }
00116   float min_radius = Amint_Amin == 0 ? plane_radius : std::min (Amint_d/Amint_Amin, plane_radius);
00117   float max_radius = Amaxt_Amax == 0 ? plane_radius : std::min (Amaxt_d/Amaxt_Amax, plane_radius);
00118 
00119   // Small correction of the systematic error of the estimation (based on analysis with nr_subdiv_ = 5)
00120   min_radius *= 1.1;
00121   max_radius *= 0.9;
00122   if (min_radius < max_radius)
00123   {
00124     radii.r_min = min_radius;
00125     radii.r_max = max_radius;
00126   }
00127   else
00128   {
00129     radii.r_max = min_radius;
00130     radii.r_min = max_radius;
00131   }
00132 }
00133 
00135 template <typename PointInT, typename PointNT, typename PointOutT> void
00136 pcl::RSDEstimation<PointInT, PointNT, PointOutT>::computeFeature (PointCloudOut &output)
00137 {
00138   // Check if search_radius_ was set
00139   if (search_radius_ < 0)
00140   {
00141     PCL_ERROR ("[pcl::%s::computeFeature] A search radius needs to be set!\n", getClassName ().c_str ());
00142     output.width = output.height = 0;
00143     output.points.clear ();
00144     return;
00145   }
00146 
00147   // Allocate enough space to hold the results
00148   // \note resize is irrelevant for a radiusSearch ().
00149   std::vector<int> nn_indices;
00150   std::vector<float> nn_sqr_dists;
00151 
00152   // Resize the output histogram dataset
00153   if (save_histograms_)
00154     histograms_.resize (output.points.size ());
00155 
00156   // Iterating over the entire index vector
00157   for (size_t idx = 0; idx < indices_->size (); ++idx)
00158   {
00159     // Compute and store r_min and r_max in the output cloud
00160     this->searchForNeighbors ((*indices_)[idx], search_parameter_, nn_indices, nn_sqr_dists);
00161 
00162     // check if the full histogram has to be saved or not
00163     if (save_histograms_)
00164       computeRSD (*surface_, *normals_, nn_indices, search_radius_, nr_subdiv_, plane_radius_, output.points[idx], &(histograms_[idx]));
00165     else
00166       computeRSD (*surface_, *normals_, nn_indices, search_radius_, nr_subdiv_, plane_radius_, output.points[idx]);
00167   }
00168 }
00169 
00170 #define PCL_INSTANTIATE_RSDEstimation(T,NT,OutT) template class PCL_EXPORTS pcl::RSDEstimation<T,NT,OutT>;
00171 
00172 #endif    // PCL_FEATURES_IMPL_VFH_H_ 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines