Point Cloud Library (PCL)  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
sac_model_cylinder.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: sac_model_cylinder.h 3751 2011-12-31 23:18:12Z rusu $
00037  *
00038  */
00039 
00040 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
00041 #define PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
00042 
00043 #include <pcl/sample_consensus/sac_model.h>
00044 #include <pcl/sample_consensus/model_types.h>
00045 #include <boost/thread/mutex.hpp>
00046 #include <pcl/common/common.h>
00047 #include "pcl/common/distances.h"
00048 
00049 namespace pcl
00050 {
00064   template <typename PointT, typename PointNT>
00065   class SampleConsensusModelCylinder : public SampleConsensusModel<PointT>, public SampleConsensusModelFromNormals<PointT, PointNT>
00066   {
00067     using SampleConsensusModel<PointT>::input_;
00068     using SampleConsensusModel<PointT>::indices_;
00069     using SampleConsensusModel<PointT>::radius_min_;
00070     using SampleConsensusModel<PointT>::radius_max_;
00071     using SampleConsensusModelFromNormals<PointT, PointNT>::normals_;
00072     using SampleConsensusModelFromNormals<PointT, PointNT>::normal_distance_weight_;
00073 
00074     public:
00075       typedef typename SampleConsensusModel<PointT>::PointCloud PointCloud;
00076       typedef typename SampleConsensusModel<PointT>::PointCloudPtr PointCloudPtr;
00077       typedef typename SampleConsensusModel<PointT>::PointCloudConstPtr PointCloudConstPtr;
00078 
00079       typedef boost::shared_ptr<SampleConsensusModelCylinder> Ptr;
00080 
00084       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud) : SampleConsensusModel<PointT> (cloud), eps_angle_ (0) 
00085       {
00086         axis_.setZero ();
00087       }
00088 
00093       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, const std::vector<int> &indices) : SampleConsensusModel<PointT> (cloud, indices), eps_angle_ (0)
00094       {
00095         axis_.setZero ();
00096       }
00097 
00101       inline void 
00102       setEpsAngle (const double ea) { eps_angle_ = ea; }
00103 
00105       inline double 
00106       getEpsAngle () { return (eps_angle_); }
00107 
00111       inline void 
00112       setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
00113 
00115       inline Eigen::Vector3f 
00116       getAxis ()  { return (axis_); }
00117 
00123       void 
00124       getSamples (int &iterations, std::vector<int> &samples);
00125 
00132       bool 
00133       computeModelCoefficients (const std::vector<int> &samples, 
00134                                 Eigen::VectorXf &model_coefficients);
00135 
00140       void 
00141       getDistancesToModel (const Eigen::VectorXf &model_coefficients, 
00142                            std::vector<double> &distances);
00143 
00149       void 
00150       selectWithinDistance (const Eigen::VectorXf &model_coefficients, 
00151                             const double threshold, 
00152                             std::vector<int> &inliers);
00153 
00160       virtual int
00161       countWithinDistance (const Eigen::VectorXf &model_coefficients, 
00162                            const double threshold);
00163 
00170       void 
00171       optimizeModelCoefficients (const std::vector<int> &inliers, 
00172                                  const Eigen::VectorXf &model_coefficients, 
00173                                  Eigen::VectorXf &optimized_coefficients);
00174 
00175 
00182       void 
00183       projectPoints (const std::vector<int> &inliers, 
00184                      const Eigen::VectorXf &model_coefficients, 
00185                      PointCloud &projected_points, 
00186                      bool copy_data_fields = true);
00187 
00193       bool 
00194       doSamplesVerifyModel (const std::set<int> &indices, 
00195                             const Eigen::VectorXf &model_coefficients, 
00196                             const double threshold);
00197 
00199       inline pcl::SacModel 
00200       getModelType () const { return (SACMODEL_CYLINDER); }
00201 
00202     protected:
00207       double 
00208       pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients);
00209 
00216       inline void
00217       projectPointToLine (const Eigen::Vector4f &pt, 
00218                           const Eigen::Vector4f &line_pt, 
00219                           const Eigen::Vector4f &line_dir,
00220                           Eigen::Vector4f &pt_proj)
00221       {
00222         double k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
00223         // Calculate the projection of the point on the line
00224         pt_proj = line_pt + k * line_dir;
00225       }
00226 
00233       void 
00234       projectPointToCylinder (const Eigen::Vector4f &pt, 
00235                               const Eigen::VectorXf &model_coefficients, 
00236                               Eigen::Vector4f &pt_proj);
00237 
00239       std::string 
00240       getName () const { return ("SampleConsensusModelCylinder"); }
00241 
00242     protected:
00246       bool 
00247       isModelValid (const Eigen::VectorXf &model_coefficients);
00248 
00253       bool
00254       isSampleGood (const std::vector<int> &samples) const;
00255 
00256     private:
00258       Eigen::Vector3f axis_;
00259     
00261       double eps_angle_;
00262 
00264       boost::mutex tmp_mutex_;
00265 
00267       const std::vector<int> *tmp_inliers_;
00268 
00270       struct OptimizationFunctor : pcl::Functor<double>
00271       {
00278         OptimizationFunctor(int n, int m, pcl::SampleConsensusModelCylinder<PointT, PointNT> *model) : pcl::Functor<double>(m,n), model_(model) {}
00284         int operator() (const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
00285         {
00286           Eigen::Vector4f line_pt  (x[0], x[1], x[2], 0);
00287           Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
00288           
00289           for (int i = 0; i < m_values; ++i)
00290           {
00291             // dist = f - r
00292             Eigen::Vector4f pt (model_->input_->points[(*model_->tmp_inliers_)[i]].x,
00293                                 model_->input_->points[(*model_->tmp_inliers_)[i]].y,
00294                                 model_->input_->points[(*model_->tmp_inliers_)[i]].z, 0);
00295 
00296             fvec[i] = pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6];
00297           }
00298           return (0);
00299         }
00300 
00301         pcl::SampleConsensusModelCylinder<PointT, PointNT> *model_;
00302       };
00303   };
00304 }
00305 
00306 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines