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 3027 2011-11-01 04:04:27Z 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 {
00065   template <typename PointT, typename PointNT>
00066   class SampleConsensusModelCylinder : public SampleConsensusModel<PointT>, public SampleConsensusModelFromNormals<PointT, PointNT>
00067   {
00068     using SampleConsensusModel<PointT>::input_;
00069     using SampleConsensusModel<PointT>::indices_;
00070     using SampleConsensusModel<PointT>::radius_min_;
00071     using SampleConsensusModel<PointT>::radius_max_;
00072     using SampleConsensusModelFromNormals<PointT, PointNT>::normals_;
00073     using SampleConsensusModelFromNormals<PointT, PointNT>::normal_distance_weight_;
00074 
00075     public:
00076       typedef typename SampleConsensusModel<PointT>::PointCloud PointCloud;
00077       typedef typename SampleConsensusModel<PointT>::PointCloudPtr PointCloudPtr;
00078       typedef typename SampleConsensusModel<PointT>::PointCloudConstPtr PointCloudConstPtr;
00079 
00080       typedef boost::shared_ptr<SampleConsensusModelCylinder> Ptr;
00081 
00085       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud) : SampleConsensusModel<PointT> (cloud), eps_angle_ (0) 
00086       {
00087         axis_.setZero ();
00088       }
00089 
00094       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, const std::vector<int> &indices) : SampleConsensusModel<PointT> (cloud, indices), eps_angle_ (0)
00095       {
00096         axis_.setZero ();
00097       }
00098 
00102       inline void 
00103       setEpsAngle (const double ea) { eps_angle_ = ea; }
00104 
00106       inline double 
00107       getEpsAngle () { return (eps_angle_); }
00108 
00112       inline void 
00113       setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
00114 
00116       inline Eigen::Vector3f 
00117       getAxis ()  { return (axis_); }
00118 
00124       void 
00125       getSamples (int &iterations, std::vector<int> &samples);
00126 
00133       bool 
00134       computeModelCoefficients (const std::vector<int> &samples, 
00135                                 Eigen::VectorXf &model_coefficients);
00136 
00141       void 
00142       getDistancesToModel (const Eigen::VectorXf &model_coefficients, 
00143                            std::vector<double> &distances);
00144 
00150       void 
00151       selectWithinDistance (const Eigen::VectorXf &model_coefficients, 
00152                             const double threshold, 
00153                             std::vector<int> &inliers);
00154 
00161       virtual int
00162       countWithinDistance (const Eigen::VectorXf &model_coefficients, 
00163                            const double threshold);
00164 
00171       void 
00172       optimizeModelCoefficients (const std::vector<int> &inliers, 
00173                                  const Eigen::VectorXf &model_coefficients, 
00174                                  Eigen::VectorXf &optimized_coefficients);
00175 
00176 
00183       void 
00184       projectPoints (const std::vector<int> &inliers, 
00185                      const Eigen::VectorXf &model_coefficients, 
00186                      PointCloud &projected_points, 
00187                      bool copy_data_fields = true);
00188 
00194       bool 
00195       doSamplesVerifyModel (const std::set<int> &indices, 
00196                             const Eigen::VectorXf &model_coefficients, 
00197                             const double threshold);
00198 
00200       inline pcl::SacModel 
00201       getModelType () const { return (SACMODEL_CYLINDER); }
00202 
00203     protected:
00208       double 
00209       pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients);
00210 
00217       inline void
00218       projectPointToLine (const Eigen::Vector4f &pt, 
00219                           const Eigen::Vector4f &line_pt, 
00220                           const Eigen::Vector4f &line_dir,
00221                           Eigen::Vector4f &pt_proj)
00222       {
00223         double k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
00224         // Calculate the projection of the point on the line
00225         pt_proj = line_pt + k * line_dir;
00226       }
00227 
00234       void 
00235       projectPointToCylinder (const Eigen::Vector4f &pt, 
00236                               const Eigen::VectorXf &model_coefficients, 
00237                               Eigen::Vector4f &pt_proj);
00238 
00240       std::string 
00241       getName () const { return ("SampleConsensusModelCylinder"); }
00242 
00243     protected:
00247       bool 
00248       isModelValid (const Eigen::VectorXf &model_coefficients);
00249 
00254       bool
00255       isSampleGood (const std::vector<int> &samples) const;
00256 
00257     private:
00259       Eigen::Vector3f axis_;
00260     
00262       double eps_angle_;
00263 
00265       boost::mutex tmp_mutex_;
00266 
00268       const std::vector<int> *tmp_inliers_;
00269 
00271       struct OptimizationFunctor : pcl::Functor<double>
00272       {
00279         OptimizationFunctor(int n, int m, pcl::SampleConsensusModelCylinder<PointT, PointNT> *model) : pcl::Functor<double>(m,n), model_(model) {}
00285         int operator() (const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
00286         {
00287           Eigen::Vector4f line_pt  (x[0], x[1], x[2], 0);
00288           Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
00289           
00290           for (int i = 0; i < m_values; ++i)
00291           {
00292             // dist = f - r
00293             Eigen::Vector4f pt (model_->input_->points[(*model_->tmp_inliers_)[i]].x,
00294                                 model_->input_->points[(*model_->tmp_inliers_)[i]].y,
00295                                 model_->input_->points[(*model_->tmp_inliers_)[i]].z, 0);
00296 
00297             fvec[i] = pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6];
00298           }
00299           return (0);
00300         }
00301 
00302         pcl::SampleConsensusModelCylinder<PointT, PointNT> *model_;
00303       };
00304   };
00305 }
00306 
00307 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_