Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
feature.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: feature.hpp 4702 2012-02-23 09:39:33Z gedikli $
00037  *
00038  */
00039 
00040 #ifndef PCL_FEATURES_IMPL_FEATURE_H_
00041 #define PCL_FEATURES_IMPL_FEATURE_H_
00042 
00043 #include <pcl/search/pcl_search.h>
00044 
00046 inline void
00047 pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
00048                            const Eigen::Vector4f &point,
00049                            Eigen::Vector4f &plane_parameters, float &curvature)
00050 {
00051   solvePlaneParameters (covariance_matrix, plane_parameters [0], plane_parameters [1], plane_parameters [2], curvature);
00052 
00053   plane_parameters[3] = 0;
00054   // Hessian form (D = nc . p_plane (centroid here) + p)
00055   plane_parameters[3] = -1 * plane_parameters.dot (point);
00056 }
00057 
00059 inline void
00060 pcl::solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix,
00061                            float &nx, float &ny, float &nz, float &curvature)
00062 {
00063   // Avoid getting hung on Eigen's optimizers
00064 //  for (int i = 0; i < 9; ++i)
00065 //    if (!pcl_isfinite (covariance_matrix.coeff (i)))
00066 //    {
00067 //      //PCL_WARN ("[pcl::solvePlaneParameteres] Covariance matrix has NaN/Inf values!\n");
00068 //      nx = ny = nz = curvature = std::numeric_limits<float>::quiet_NaN ();
00069 //      return;
00070 //    }
00071   // Extract the smallest eigenvalue and its eigenvector
00072   EIGEN_ALIGN16 Eigen::Vector3f::Scalar eigen_value;
00073   EIGEN_ALIGN16 Eigen::Vector3f eigen_vector;
00074   pcl::eigen33 (covariance_matrix, eigen_value, eigen_vector);
00075 
00076   nx = eigen_vector [0];
00077   ny = eigen_vector [1];
00078   nz = eigen_vector [2];
00079 
00080   // Compute the curvature surface change
00081   float eig_sum = covariance_matrix.coeff (0) + covariance_matrix.coeff (4) + covariance_matrix.coeff (8);
00082   if (eig_sum != 0)
00083     curvature = fabs ( eigen_value / eig_sum );
00084   else
00085     curvature = 0;
00086 }
00087 
00091 template <typename PointInT, typename PointOutT> bool
00092 pcl::Feature<PointInT, PointOutT>::initCompute ()
00093 {
00094   if (!PCLBase<PointInT>::initCompute ())
00095   {
00096     PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
00097     return (false);
00098   }
00099 
00100   // If the dataset is empty, just return
00101   if (input_->points.empty ())
00102   {
00103     PCL_ERROR ("[pcl::%s::compute] input_ is empty!\n", getClassName ().c_str ());
00104     // Cleanup
00105     deinitCompute ();
00106     return (false);
00107   }
00108 
00109   // If no search surface has been defined, use the input dataset as the search surface itself
00110   if (!surface_)
00111   {
00112     fake_surface_ = true;
00113     surface_ = input_;
00114   }
00115 
00116   // Check if a space search locator was given
00117   if (!tree_)
00118   {
00119     if (surface_->isOrganized () && input_->isOrganized ())
00120       tree_.reset (new pcl::search::OrganizedNeighbor<PointInT> ());
00121     else
00122       tree_.reset (new pcl::search::KdTree<PointInT> (false));
00123   }
00124   // Send the surface dataset to the spatial locator
00125   tree_->setInputCloud (surface_);
00126 
00127   // Do a fast check to see if the search parameters are well defined
00128   if (search_radius_ != 0.0)
00129   {
00130     if (k_ != 0)
00131     {
00132       PCL_ERROR ("[pcl::%s::compute] ", getClassName ().c_str ());
00133       PCL_ERROR ("Both radius (%f) and K (%d) defined! ", search_radius_, k_);
00134       PCL_ERROR ("Set one of them to zero first and then re-run compute ().\n");
00135       // Cleanup
00136       deinitCompute ();
00137       return (false);
00138     }
00139     else // Use the radiusSearch () function
00140     {
00141       search_parameter_ = search_radius_;
00142       if (surface_ == input_) // if the two surfaces are the same
00143       {
00144         // Declare the search locator definition
00145         int (KdTree::*radiusSearch)(int index, double radius, std::vector<int> &k_indices,
00146                                     std::vector<float> &k_distances, unsigned int max_nn) const = &KdTree::radiusSearch;
00147         search_method_ = boost::bind (radiusSearch, boost::ref (tree_), _1, _2, _3, _4, 0);
00148       }
00149 
00150       // Declare the search locator definition
00151       int (KdTree::*radiusSearchSurface)(const PointCloudIn &cloud, int index, double radius,
00152                                          std::vector<int> &k_indices, std::vector<float> &k_distances,
00153                                          unsigned int max_nn) const = &pcl::search::Search<PointInT>::radiusSearch;
00154       search_method_surface_ = boost::bind (radiusSearchSurface, boost::ref (tree_), _1, _2, _3, _4, _5, 0);
00155     }
00156   }
00157   else
00158   {
00159     if (k_ != 0) // Use the nearestKSearch () function
00160     {
00161       search_parameter_ = k_;
00162       if (surface_ == input_) // if the two surfaces are the same
00163       {
00164         // Declare the search locator definition
00165         int (KdTree::*nearestKSearch)(int index, int k, std::vector<int> &k_indices,
00166                                       std::vector<float> &k_distances) const = &KdTree::nearestKSearch;
00167         search_method_ = boost::bind (nearestKSearch, boost::ref (tree_), _1, _2, _3, _4);
00168       }
00169       // Declare the search locator definition
00170       int (KdTree::*nearestKSearchSurface)(const PointCloudIn &cloud, int index, int k, std::vector<int> &k_indices,
00171                                            std::vector<float> &k_distances) const = &KdTree::nearestKSearch;
00172       search_method_surface_ = boost::bind (nearestKSearchSurface, boost::ref (tree_), _1, _2, _3, _4, _5);
00173     }
00174     else
00175     {
00176       PCL_ERROR ("[pcl::%s::compute] Neither radius nor K defined! ", getClassName ().c_str ());
00177       PCL_ERROR ("Set one of them to a positive number first and then re-run compute ().\n");
00178       // Cleanup
00179       deinitCompute ();
00180       return (false);
00181     }
00182   }
00183   return (true);
00184 }
00185 
00187 template <typename PointInT, typename PointOutT> bool
00188 pcl::Feature<PointInT, PointOutT>::deinitCompute ()
00189 {
00190   // Reset the surface
00191   if (fake_surface_)
00192   {
00193     surface_.reset ();
00194     fake_surface_ = false;
00195   }
00196   return (true);
00197 }
00198 
00200 template <typename PointInT, typename PointOutT> void
00201 pcl::Feature<PointInT, PointOutT>::compute (PointCloudOut &output)
00202 {
00203   if (!initCompute ())
00204   {
00205     output.width = output.height = 0;
00206     output.points.clear ();
00207     return;
00208   }
00209 
00210   // Copy the header
00211   output.header = input_->header;
00212 
00213   // Resize the output dataset
00214   if (output.points.size () != indices_->size ())
00215     output.points.resize (indices_->size ());
00216   // Check if the output will be computed for all points or only a subset
00217   if (indices_->size () != input_->points.size ())
00218   {
00219     output.width = (int) indices_->size ();
00220     output.height = 1;
00221   }
00222   else
00223   {
00224     output.width = input_->width;
00225     output.height = input_->height;
00226   }
00227   output.is_dense = input_->is_dense;
00228 
00229   // Perform the actual feature computation
00230   computeFeature (output);
00231 
00232   deinitCompute ();
00233 }
00234 
00236 template <typename PointInT, typename PointOutT> void
00237 pcl::Feature<PointInT, PointOutT>::computeEigen (pcl::PointCloud<Eigen::MatrixXf> &output)
00238 {
00239   if (!initCompute ())
00240   {
00241     output.width = output.height = 0;
00242     output.points.resize (0, 0);
00243     return;
00244   }
00245 
00246   // Copy the properties
00247 //#ifndef USE_ROS
00248 //  output.properties.acquisition_time = input_->header.stamp;
00249 //#endif
00250   output.properties.sensor_origin = input_->sensor_origin_;
00251   output.properties.sensor_orientation = input_->sensor_orientation_;
00252 
00253   // Check if the output will be computed for all points or only a subset
00254   if (indices_->size () != input_->points.size ())
00255   {
00256     output.width = (int) indices_->size ();
00257     output.height = 1;
00258   }
00259   else
00260   {
00261     output.width = input_->width;
00262     output.height = input_->height;
00263   }
00264 
00265   output.is_dense = input_->is_dense;
00266 
00267   // Perform the actual feature computation
00268   computeFeatureEigen (output);
00269 
00270   deinitCompute ();
00271 }
00272 
00276 template <typename PointInT, typename PointNT, typename PointOutT> bool
00277 pcl::FeatureFromNormals<PointInT, PointNT, PointOutT>::initCompute ()
00278 {
00279   if (!Feature<PointInT, PointOutT>::initCompute ())
00280   {
00281     PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
00282     return (false);
00283   }
00284 
00285   // Check if input normals are set
00286   if (!normals_)
00287   {
00288     PCL_ERROR ("[pcl::%s::initCompute] No input dataset containing normals was given!\n", getClassName ().c_str ());
00289     Feature<PointInT, PointOutT>::deinitCompute ();
00290     return (false);
00291   }
00292 
00293   // Check if the size of normals is the same as the size of the surface
00294   if (normals_->points.size () != surface_->points.size ())
00295   {
00296     PCL_ERROR ("[pcl::%s::initCompute] ", getClassName ().c_str ());
00297     PCL_ERROR ("The number of points in the input dataset (%u) differs from ", surface_->points.size ());
00298     PCL_ERROR ("the number of points in the dataset containing the normals (%u)!\n", normals_->points.size ());
00299     Feature<PointInT, PointOutT>::deinitCompute ();
00300     return (false);
00301   }
00302 
00303   return (true);
00304 }
00305 
00309 template <typename PointInT, typename PointLT, typename PointOutT> bool
00310 pcl::FeatureFromLabels<PointInT, PointLT, PointOutT>::initCompute ()
00311 {
00312   if (!Feature<PointInT, PointOutT>::initCompute ())
00313   {
00314     PCL_ERROR ("[pcl::%s::initCompute] Init failed.\n", getClassName ().c_str ());
00315     return (false);
00316   }
00317 
00318   // Check if input normals are set
00319   if (!labels_)
00320   {
00321     PCL_ERROR ("[pcl::%s::initCompute] No input dataset containing labels was given!\n", getClassName ().c_str ());
00322     Feature<PointInT, PointOutT>::deinitCompute ();
00323     return (false);
00324   }
00325 
00326   // Check if the size of normals is the same as the size of the surface
00327   if (labels_->points.size () != surface_->points.size ())
00328   {
00329     PCL_ERROR ("[pcl::%s::initCompute] The number of points in the input dataset differs from the number of points in the dataset containing the labels!\n", getClassName ().c_str ());
00330     Feature<PointInT, PointOutT>::deinitCompute ();
00331     return (false);
00332   }
00333 
00334   return (true);
00335 }
00336 
00337 #endif  //#ifndef PCL_FEATURES_IMPL_FEATURE_H_
00338