Point Cloud Library (PCL)  1.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
pca.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: pca.h 4702 2012-02-23 09:39:33Z gedikli $
00037  */
00038 
00039 #ifndef PCL_PCA_H
00040 #define PCL_PCA_H
00041 
00042 #include <pcl/pcl_base.h>
00043 #include <pcl/pcl_macros.h>
00044 
00045 namespace pcl 
00046 {
00059   template <typename PointT>
00060   class PCA : public pcl::PCLBase <PointT>
00061   {
00062     public:
00063       typedef pcl::PCLBase <PointT> Base;
00064       typedef typename Base::PointCloud PointCloud;
00065       typedef typename Base::PointCloudPtr PointCloudPtr;
00066       typedef typename Base::PointCloudConstPtr PointCloudConstPtr;
00067       typedef typename Base::PointIndicesPtr PointIndicesPtr;
00068       typedef typename Base::PointIndicesConstPtr PointIndicesConstPtr;
00069 
00070       using Base::input_;
00071       using Base::indices_;
00072       using Base::initCompute;
00073       using Base::setInputCloud;
00074 
00076       enum FLAG 
00077       {
00079         increase, 
00081         preserve
00082       };
00083     
00087       PCA (bool basis_only = false)
00088         : Base ()
00089         , compute_done_ (false)
00090         , basis_only_ (basis_only) 
00091       {}
00092       
00097       PCL_DEPRECATED (PCA (const pcl::PointCloud<PointT>& X, bool basis_only = false), 
00098                       "Use PCA (bool basis_only); setInputCloud (X.makeShared ()); instead");
00099 
00103       PCA (PCA const & pca) 
00104         : Base (pca)
00105         , eigenvectors_ (pca.eigenvectors_)
00106         , coefficients_ (pca.coefficients_)
00107         , mean_ (pca.mean_)
00108         , eigenvalues_  (pca.eigenvalues_)
00109       {}
00110 
00114       inline PCA& operator= (PCA const & pca) 
00115       {
00116         eigenvectors_ = pca.eigenvectors;
00117         coefficients_ = pca.coefficients;
00118         eigenvalues_  = pca.eigenvalues;
00119         mean_         = pca.mean;
00120         return (*this);
00121       }
00122       
00126       inline void 
00127       setInputCloud (const PointCloudConstPtr &cloud) 
00128       { 
00129         Base::setInputCloud (cloud);
00130         compute_done_ = false;
00131       }
00132 
00136       inline Eigen::Vector4f& 
00137       getMean () 
00138       {
00139         if (!compute_done_)
00140           initCompute ();
00141         if (!compute_done_)
00142           PCL_THROW_EXCEPTION (InitFailedException, 
00143                                "[pcl::PCA::getMean] PCA initCompute failed");
00144         return (mean_);
00145       }
00146 
00150       inline Eigen::Matrix3f& 
00151       getEigenVectors () 
00152       {
00153         if (!compute_done_)
00154           initCompute ();
00155         if (!compute_done_)
00156           PCL_THROW_EXCEPTION (InitFailedException, 
00157                                "[pcl::PCA::getEigenVectors] PCA initCompute failed");
00158         return (eigenvectors_);
00159       }
00160       
00164       inline Eigen::Vector3f& 
00165       getEigenValues ()
00166       {
00167         if (!compute_done_)
00168           initCompute ();
00169         if (!compute_done_)
00170           PCL_THROW_EXCEPTION (InitFailedException, 
00171                                "[pcl::PCA::getEigenVectors] PCA getEigenValues failed");
00172         return (eigenvalues_);
00173       }
00174       
00178       inline Eigen::MatrixXf& 
00179       getCoefficients () 
00180       {
00181         if (!compute_done_)
00182           initCompute ();
00183         if (!compute_done_)
00184           PCL_THROW_EXCEPTION (InitFailedException, 
00185                                "[pcl::PCA::getEigenVectors] PCA getCoefficients failed");
00186         return (coefficients_);
00187       }
00188             
00194       inline void 
00195       update (const PointT& input, FLAG flag = preserve);
00196       
00202       inline void 
00203       project (const PointT& input, PointT& projection);
00204 
00210       inline void
00211       project (const PointCloud& input, PointCloud& projection);
00212       
00218       inline void 
00219       reconstruct (const PointT& projection, PointT& input);
00220 
00226       inline void
00227       reconstruct (const PointCloud& projection, PointCloud& input);
00228 
00229     private:
00230       inline bool
00231       initCompute ();
00232 
00233       bool compute_done_;
00234       bool basis_only_;
00235       Eigen::Matrix3f eigenvectors_;
00236       Eigen::MatrixXf coefficients_;
00237       Eigen::Vector4f mean_;
00238       Eigen::Vector3f eigenvalues_;
00239   }; // class PCA
00240 } // namespace pcl
00241 
00242 #include "pcl/common/impl/pca.hpp"
00243 
00244 #endif // PCL_PCA_H
00245