Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
feature.h
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.h 3755 2011-12-31 23:45:30Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_FEATURE_H_
00041 #define PCL_FEATURE_H_
00042 
00043 #include <boost/function.hpp>
00044 #include <boost/bind.hpp>
00045 #include <boost/mpl/size.hpp>
00046 
00047 // PCL includes
00048 #include <pcl/pcl_base.h>
00049 #include <pcl/common/eigen.h>
00050 #include <pcl/common/centroid.h>
00051 #include <pcl/search/search.h>
00052 
00053 namespace pcl
00054 {
00066   inline void 
00067   solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix, 
00068                         const Eigen::Vector4f &point, 
00069                         Eigen::Vector4f &plane_parameters, float &curvature);
00070 
00083   inline void 
00084   solvePlaneParameters (const Eigen::Matrix3f &covariance_matrix, 
00085                         float &nx, float &ny, float &nz, float &curvature);
00086 
00090 
00103   template <typename PointInT, typename PointOutT>
00104   class Feature : public PCLBase<PointInT>
00105   {
00106     public:
00107       using PCLBase<PointInT>::indices_;
00108       using PCLBase<PointInT>::input_;
00109 
00110       typedef PCLBase<PointInT> BaseClass;
00111 
00112       typedef boost::shared_ptr< Feature<PointInT, PointOutT> > Ptr;
00113       typedef boost::shared_ptr< const Feature<PointInT, PointOutT> > ConstPtr;
00114       
00115       typedef typename pcl::search::Search<PointInT> KdTree;
00116       typedef typename pcl::search::Search<PointInT>::Ptr KdTreePtr;
00117 
00118       typedef pcl::PointCloud<PointInT> PointCloudIn;
00119       typedef typename PointCloudIn::Ptr PointCloudInPtr;
00120       typedef typename PointCloudIn::ConstPtr PointCloudInConstPtr;
00121 
00122       typedef pcl::PointCloud<PointOutT> PointCloudOut;
00123 
00124       typedef boost::function<int (size_t, double, std::vector<int> &, std::vector<float> &)> SearchMethod;
00125       typedef boost::function<int (const PointCloudIn &cloud, size_t index, double, std::vector<int> &, std::vector<float> &)> SearchMethodSurface;
00126     
00127     public:
00129       Feature () : surface_(), tree_(), search_parameter_(0), search_radius_(0), k_(0), fake_surface_(false)
00130       {}
00131 
00139       inline void
00140       setSearchSurface (const PointCloudInConstPtr &cloud)
00141       {
00142         surface_ = cloud;
00143         fake_surface_ = false;
00144         //use_surface_  = true;
00145       }
00146 
00148       inline PointCloudInConstPtr 
00149       getSearchSurface () { return (surface_); }
00150 
00154       inline void 
00155       setSearchMethod (const KdTreePtr &tree) { tree_ = tree; }
00156 
00158       inline KdTreePtr 
00159       getSearchMethod () { return (tree_); }
00160 
00162       inline double 
00163       getSearchParameter () { return (search_parameter_); }
00164 
00168       inline void 
00169       setKSearch (int k) { k_ = k; }
00170 
00172       inline int 
00173       getKSearch () { return (k_); }
00174 
00179       inline void 
00180       setRadiusSearch (double radius) { search_radius_ = radius; }
00181 
00183       inline double 
00184       getRadiusSearch () { return (search_radius_); }
00185 
00191       void 
00192       compute (PointCloudOut &output);
00193 
00199       void 
00200       compute (pcl::PointCloud<Eigen::MatrixXf> &output);
00201 
00212       inline int
00213       searchForNeighbors (size_t index, double parameter, 
00214                           std::vector<int> &indices, std::vector<float> &distances) const
00215       {
00216         if (surface_ == input_)       // if the two surfaces are the same
00217           return (search_method_ (index, parameter, indices, distances));
00218         else
00219           return (search_method_surface_ (*input_, index, parameter, indices, distances));
00220       }
00221 
00233       inline int
00234       searchForNeighbors (const PointCloudIn &cloud, size_t index, double parameter, 
00235                           std::vector<int> &indices, std::vector<float> &distances) const
00236       {
00237         return (search_method_surface_ (cloud, index, parameter, indices, distances));
00238       }
00239 
00240     protected:
00242       std::string feature_name_;
00243 
00245       SearchMethod search_method_;
00246 
00248       SearchMethodSurface search_method_surface_;
00249 
00253       PointCloudInConstPtr surface_;
00254 
00256       KdTreePtr tree_;
00257 
00259       double search_parameter_;
00260 
00262       double search_radius_;
00263 
00265       int k_;
00266 
00268       inline const std::string& 
00269       getClassName () const { return (feature_name_); }
00270 
00272       virtual inline bool
00273       initCompute ();
00274 
00276       virtual inline bool
00277       deinitCompute ();
00278 
00280       bool fake_surface_;
00281 
00282     private:
00286       virtual void 
00287       computeFeature (PointCloudOut &output) = 0;
00288       
00292       virtual void 
00293       computeFeature (pcl::PointCloud<Eigen::MatrixXf> &output) = 0;
00294 
00295     public:
00296       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00297   };
00298 
00299 
00303   template <typename PointInT, typename PointNT, typename PointOutT>
00304   class FeatureFromNormals : public Feature<PointInT, PointOutT>
00305   {
00306     typedef typename Feature<PointInT, PointOutT>::PointCloudIn PointCloudIn;
00307     typedef typename PointCloudIn::Ptr PointCloudInPtr;
00308     typedef typename PointCloudIn::ConstPtr PointCloudInConstPtr;
00309     typedef typename Feature<PointInT, PointOutT>::PointCloudOut PointCloudOut;
00310 
00311     public:
00312       typedef typename pcl::PointCloud<PointNT> PointCloudN;
00313       typedef typename PointCloudN::Ptr PointCloudNPtr;
00314       typedef typename PointCloudN::ConstPtr PointCloudNConstPtr;
00315 
00316       typedef boost::shared_ptr< FeatureFromNormals<PointInT, PointNT, PointOutT> > Ptr;
00317       typedef boost::shared_ptr< const FeatureFromNormals<PointInT, PointNT, PointOutT> > ConstPtr;
00318 
00319       // Members derived from the base class
00320       using Feature<PointInT, PointOutT>::input_;
00321       using Feature<PointInT, PointOutT>::surface_;
00322       using Feature<PointInT, PointOutT>::getClassName;
00323 
00325       FeatureFromNormals () {}
00326 
00334       inline void 
00335       setInputNormals (const PointCloudNConstPtr &normals) { normals_ = normals; }
00336 
00338       inline PointCloudNConstPtr 
00339       getInputNormals () { return (normals_); }
00340 
00341     protected:     
00345       PointCloudNConstPtr normals_;
00346 
00348       virtual bool
00349       initCompute ();
00350 
00351     public:
00352       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00353   };
00354 
00358   template <typename PointInT, typename PointLT, typename PointOutT>
00359   class FeatureFromLabels : public Feature<PointInT, PointOutT>
00360   {
00361     typedef typename Feature<PointInT, PointOutT>::PointCloudIn PointCloudIn;
00362     typedef typename PointCloudIn::Ptr PointCloudInPtr;
00363     typedef typename PointCloudIn::ConstPtr PointCloudInConstPtr;
00364 
00365     typedef typename pcl::PointCloud<PointLT> PointCloudL;
00366     typedef typename PointCloudL::Ptr PointCloudNPtr;
00367     typedef typename PointCloudL::ConstPtr PointCloudLConstPtr;
00368 
00369     typedef typename Feature<PointInT, PointOutT>::PointCloudOut PointCloudOut;
00370 
00371     public:
00372       typedef boost::shared_ptr< FeatureFromLabels<PointInT, PointLT, PointOutT> > Ptr;
00373       typedef boost::shared_ptr< const FeatureFromLabels<PointInT, PointLT, PointOutT> > ConstPtr;
00374 
00375       // Members derived from the base class
00376       using Feature<PointInT, PointOutT>::input_;
00377       using Feature<PointInT, PointOutT>::surface_;
00378       using Feature<PointInT, PointOutT>::getClassName;
00379       using Feature<PointInT, PointOutT>::k_;
00380 
00382       FeatureFromLabels ()
00383       {
00384         k_ = 1; // Search tree is not always used here.
00385       }
00386 
00393       inline void
00394       setInputLabels (const PointCloudLConstPtr &labels) { labels_ = labels; }
00395 
00397       inline PointCloudLConstPtr
00398       getInputLabels () { return (labels_); }
00399 
00400     protected:
00404       PointCloudLConstPtr labels_;
00405 
00407       virtual bool
00408       initCompute ();
00409 
00410     public:
00411       EIGEN_MAKE_ALIGNED_OPERATOR_NEW
00412   };
00413 }
00414 
00415 #include "pcl/features/impl/feature.hpp"
00416 
00417 #endif  //#ifndef PCL_FEATURE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines