sac_model_plane.h

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: sac_model_plane.h 1370 2011-06-19 01:06:01Z jspricke $
00035  *
00036  */
00037 
00038 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_
00039 #define PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_
00040 
00041 #include <pcl/sample_consensus/sac_model.h>
00042 #include <pcl/sample_consensus/model_types.h>
00043 
00044 namespace pcl
00045 {
00054   template <typename Point> inline double
00055   pointToPlaneDistanceSigned (const Point &p, double a, double b, double c, double d)
00056   {
00057     return (a * p.x + b * p.y + c * p.z + d);
00058   }
00059 
00065   template <typename Point> inline double
00066   pointToPlaneDistanceSigned (const Point &p, const Eigen::Vector4f &plane_coefficients)
00067   {
00068     return ( plane_coefficients[0] * p.x + plane_coefficients[1] * p.y + plane_coefficients[2] * p.z + plane_coefficients[3] );
00069   }
00070 
00079   template <typename Point> inline double
00080   pointToPlaneDistance (const Point &p, double a, double b, double c, double d)
00081   {
00082     return (fabs (pointToPlaneDistanceSigned (p, a, b, c, d)) );
00083   }
00084 
00090   template <typename Point> inline double
00091   pointToPlaneDistance (const Point &p, const Eigen::Vector4f &plane_coefficients)
00092   {
00093     return ( fabs (pointToPlaneDistanceSigned (p, plane_coefficients)) );
00094   }
00095 
00097 
00101   template <typename PointT>
00102   class SampleConsensusModelPlane : public SampleConsensusModel<PointT>
00103   {
00104     public:
00105       using SampleConsensusModel<PointT>::input_;
00106       using SampleConsensusModel<PointT>::indices_;
00107 
00108       typedef typename SampleConsensusModel<PointT>::PointCloud PointCloud;
00109       typedef typename SampleConsensusModel<PointT>::PointCloudPtr PointCloudPtr;
00110       typedef typename SampleConsensusModel<PointT>::PointCloudConstPtr PointCloudConstPtr;
00111 
00112       typedef boost::shared_ptr<SampleConsensusModelPlane> Ptr;
00113 
00117       SampleConsensusModelPlane (const PointCloudConstPtr &cloud) : SampleConsensusModel<PointT> (cloud) {};
00118 
00123       SampleConsensusModelPlane (const PointCloudConstPtr &cloud, const std::vector<int> &indices) : SampleConsensusModel<PointT> (cloud, indices) {};
00124 
00130       void getSamples (int &iterations, std::vector<int> &samples);
00131 
00138       bool computeModelCoefficients (const std::vector<int> &samples, Eigen::VectorXf &model_coefficients);
00139 
00144       void getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances);
00145 
00151       void selectWithinDistance (const Eigen::VectorXf &model_coefficients, double threshold, std::vector<int> &inliers);
00152 
00159       void optimizeModelCoefficients (const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients);
00160 
00167       void projectPoints (const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, PointCloud &projected_points, bool copy_data_fields = true);
00168 
00174       bool doSamplesVerifyModel (const std::set<int> &indices, const Eigen::VectorXf &model_coefficients, double threshold);
00175 
00177       inline pcl::SacModel getModelType () const { return (SACMODEL_PLANE); }
00178 
00179     protected:
00183       inline bool 
00184         isModelValid (const Eigen::VectorXf &model_coefficients)
00185       {
00186         // Needs a valid model coefficients
00187         if (model_coefficients.size () != 4)
00188         {
00189           PCL_ERROR ("[pcl::SampleConsensusModelPlane::isModelValid] Invalid number of model coefficients given (%lu)!\n", (unsigned long)model_coefficients.size ());
00190           return (false);
00191         }
00192         return (true);
00193       }
00194 
00195     private:
00200       bool
00201       isSampleGood(const std::vector<int> &samples) const;
00202   };
00203 }
00204 
00205 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_PLANE_H_