Point Cloud Library (PCL)  1.5.1
 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 4702 2012-02-23 09:39:33Z gedikli $
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 <pcl/common/common.h>
00046 #include "pcl/common/distances.h"
00047 
00048 namespace pcl
00049 {
00063   template <typename PointT, typename PointNT>
00064   class SampleConsensusModelCylinder : public SampleConsensusModel<PointT>, public SampleConsensusModelFromNormals<PointT, PointNT>
00065   {
00066     using SampleConsensusModel<PointT>::input_;
00067     using SampleConsensusModel<PointT>::indices_;
00068     using SampleConsensusModel<PointT>::radius_min_;
00069     using SampleConsensusModel<PointT>::radius_max_;
00070     using SampleConsensusModelFromNormals<PointT, PointNT>::normals_;
00071     using SampleConsensusModelFromNormals<PointT, PointNT>::normal_distance_weight_;
00072 
00073     public:
00074       typedef typename SampleConsensusModel<PointT>::PointCloud PointCloud;
00075       typedef typename SampleConsensusModel<PointT>::PointCloudPtr PointCloudPtr;
00076       typedef typename SampleConsensusModel<PointT>::PointCloudConstPtr PointCloudConstPtr;
00077 
00078       typedef boost::shared_ptr<SampleConsensusModelCylinder> Ptr;
00079 
00083       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud) : SampleConsensusModel<PointT> (cloud), eps_angle_ (0) 
00084       {
00085         axis_.setZero ();
00086       }
00087 
00092       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, const std::vector<int> &indices) : SampleConsensusModel<PointT> (cloud, indices), eps_angle_ (0)
00093       {
00094         axis_.setZero ();
00095       }
00096 
00100       inline void 
00101       setEpsAngle (const double ea) { eps_angle_ = ea; }
00102 
00104       inline double 
00105       getEpsAngle () { return (eps_angle_); }
00106 
00110       inline void 
00111       setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
00112 
00114       inline Eigen::Vector3f 
00115       getAxis ()  { return (axis_); }
00116 
00122       void 
00123       getSamples (int &iterations, std::vector<int> &samples);
00124 
00131       bool 
00132       computeModelCoefficients (const std::vector<int> &samples, 
00133                                 Eigen::VectorXf &model_coefficients);
00134 
00139       void 
00140       getDistancesToModel (const Eigen::VectorXf &model_coefficients, 
00141                            std::vector<double> &distances);
00142 
00148       void 
00149       selectWithinDistance (const Eigen::VectorXf &model_coefficients, 
00150                             const double threshold, 
00151                             std::vector<int> &inliers);
00152 
00159       virtual int
00160       countWithinDistance (const Eigen::VectorXf &model_coefficients, 
00161                            const double threshold);
00162 
00169       void 
00170       optimizeModelCoefficients (const std::vector<int> &inliers, 
00171                                  const Eigen::VectorXf &model_coefficients, 
00172                                  Eigen::VectorXf &optimized_coefficients);
00173 
00174 
00181       void 
00182       projectPoints (const std::vector<int> &inliers, 
00183                      const Eigen::VectorXf &model_coefficients, 
00184                      PointCloud &projected_points, 
00185                      bool copy_data_fields = true);
00186 
00192       bool 
00193       doSamplesVerifyModel (const std::set<int> &indices, 
00194                             const Eigen::VectorXf &model_coefficients, 
00195                             const double threshold);
00196 
00198       inline pcl::SacModel 
00199       getModelType () const { return (SACMODEL_CYLINDER); }
00200 
00201     protected:
00206       double 
00207       pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients);
00208 
00215       inline void
00216       projectPointToLine (const Eigen::Vector4f &pt, 
00217                           const Eigen::Vector4f &line_pt, 
00218                           const Eigen::Vector4f &line_dir,
00219                           Eigen::Vector4f &pt_proj)
00220       {
00221         double k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
00222         // Calculate the projection of the point on the line
00223         pt_proj = line_pt + k * line_dir;
00224       }
00225 
00232       void 
00233       projectPointToCylinder (const Eigen::Vector4f &pt, 
00234                               const Eigen::VectorXf &model_coefficients, 
00235                               Eigen::Vector4f &pt_proj);
00236 
00238       std::string 
00239       getName () const { return ("SampleConsensusModelCylinder"); }
00240 
00241     protected:
00245       bool 
00246       isModelValid (const Eigen::VectorXf &model_coefficients);
00247 
00252       bool
00253       isSampleGood (const std::vector<int> &samples) const;
00254 
00255     private:
00257       Eigen::Vector3f axis_;
00258     
00260       double eps_angle_;
00261 
00263       const std::vector<int> *tmp_inliers_;
00264 
00266       struct OptimizationFunctor : pcl::Functor<float>
00267       {
00273         OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCylinder<PointT, PointNT> *model) : 
00274           pcl::Functor<float> (m_data_points), model_ (model) {}
00275 
00281         int 
00282         operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
00283         {
00284           Eigen::Vector4f line_pt  (x[0], x[1], x[2], 0);
00285           Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
00286           
00287           for (int i = 0; i < values (); ++i)
00288           {
00289             // dist = f - r
00290             Eigen::Vector4f pt (model_->input_->points[(*model_->tmp_inliers_)[i]].x,
00291                                 model_->input_->points[(*model_->tmp_inliers_)[i]].y,
00292                                 model_->input_->points[(*model_->tmp_inliers_)[i]].z, 0);
00293 
00294             fvec[i] = pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6];
00295           }
00296           return (0);
00297         }
00298 
00299         pcl::SampleConsensusModelCylinder<PointT, PointNT> *model_;
00300       };
00301   };
00302 }
00303 
00304 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_