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  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  * $Id$
00038  *
00039  */
00040 
00041 #ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
00042 #define PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_
00043 
00044 #include <pcl/sample_consensus/sac_model.h>
00045 #include <pcl/sample_consensus/model_types.h>
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     public:
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       using SampleConsensusModel<PointT>::error_sqr_dists_;
00075 
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 
00086       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, bool random = false) 
00087         : SampleConsensusModel<PointT> (cloud, random)
00088         , SampleConsensusModelFromNormals<PointT, PointNT> ()
00089         , axis_ (Eigen::Vector3f::Zero ())
00090         , eps_angle_ (0)
00091         , tmp_inliers_ ()
00092       {
00093       }
00094 
00100       SampleConsensusModelCylinder (const PointCloudConstPtr &cloud, 
00101                                     const std::vector<int> &indices,
00102                                     bool random = false) 
00103         : SampleConsensusModel<PointT> (cloud, indices, random)
00104         , SampleConsensusModelFromNormals<PointT, PointNT> ()
00105         , axis_ (Eigen::Vector3f::Zero ())
00106         , eps_angle_ (0)
00107         , tmp_inliers_ ()
00108       {
00109       }
00110 
00114       SampleConsensusModelCylinder (const SampleConsensusModelCylinder &source) :
00115         SampleConsensusModel<PointT> (),
00116         SampleConsensusModelFromNormals<PointT, PointNT> (), 
00117         axis_ (Eigen::Vector3f::Zero ()),
00118         eps_angle_ (0),
00119         tmp_inliers_ ()
00120       {
00121         *this = source;
00122       }
00123       
00125       virtual ~SampleConsensusModelCylinder () {}
00126 
00130       inline SampleConsensusModelCylinder&
00131       operator = (const SampleConsensusModelCylinder &source)
00132       {
00133         SampleConsensusModel<PointT>::operator=(source);
00134         axis_ = source.axis_;
00135         eps_angle_ = source.eps_angle_;
00136         tmp_inliers_ = source.tmp_inliers_;
00137         return (*this);
00138       }
00139 
00143       inline void 
00144       setEpsAngle (const double ea) { eps_angle_ = ea; }
00145 
00147       inline double 
00148       getEpsAngle () { return (eps_angle_); }
00149 
00153       inline void 
00154       setAxis (const Eigen::Vector3f &ax) { axis_ = ax; }
00155 
00157       inline Eigen::Vector3f 
00158       getAxis ()  { return (axis_); }
00159 
00166       bool 
00167       computeModelCoefficients (const std::vector<int> &samples, 
00168                                 Eigen::VectorXf &model_coefficients);
00169 
00174       void 
00175       getDistancesToModel (const Eigen::VectorXf &model_coefficients, 
00176                            std::vector<double> &distances);
00177 
00183       void 
00184       selectWithinDistance (const Eigen::VectorXf &model_coefficients, 
00185                             const double threshold, 
00186                             std::vector<int> &inliers);
00187 
00194       virtual int
00195       countWithinDistance (const Eigen::VectorXf &model_coefficients, 
00196                            const double threshold);
00197 
00204       void 
00205       optimizeModelCoefficients (const std::vector<int> &inliers, 
00206                                  const Eigen::VectorXf &model_coefficients, 
00207                                  Eigen::VectorXf &optimized_coefficients);
00208 
00209 
00216       void 
00217       projectPoints (const std::vector<int> &inliers, 
00218                      const Eigen::VectorXf &model_coefficients, 
00219                      PointCloud &projected_points, 
00220                      bool copy_data_fields = true);
00221 
00227       bool 
00228       doSamplesVerifyModel (const std::set<int> &indices, 
00229                             const Eigen::VectorXf &model_coefficients, 
00230                             const double threshold);
00231 
00233       inline pcl::SacModel 
00234       getModelType () const { return (SACMODEL_CYLINDER); }
00235 
00236     protected:
00241       double 
00242       pointToLineDistance (const Eigen::Vector4f &pt, const Eigen::VectorXf &model_coefficients);
00243 
00250       inline void
00251       projectPointToLine (const Eigen::Vector4f &pt, 
00252                           const Eigen::Vector4f &line_pt, 
00253                           const Eigen::Vector4f &line_dir,
00254                           Eigen::Vector4f &pt_proj)
00255       {
00256         float k = (pt.dot (line_dir) - line_pt.dot (line_dir)) / line_dir.dot (line_dir);
00257         // Calculate the projection of the point on the line
00258         pt_proj = line_pt + k * line_dir;
00259       }
00260 
00267       void 
00268       projectPointToCylinder (const Eigen::Vector4f &pt, 
00269                               const Eigen::VectorXf &model_coefficients, 
00270                               Eigen::Vector4f &pt_proj);
00271 
00273       std::string 
00274       getName () const { return ("SampleConsensusModelCylinder"); }
00275 
00276     protected:
00280       bool 
00281       isModelValid (const Eigen::VectorXf &model_coefficients);
00282 
00287       bool
00288       isSampleGood (const std::vector<int> &samples) const;
00289 
00290     private:
00292       Eigen::Vector3f axis_;
00293     
00295       double eps_angle_;
00296 
00298       const std::vector<int> *tmp_inliers_;
00299 
00300 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
00301 #pragma GCC diagnostic ignored "-Weffc++"
00302 #endif
00303 
00304       struct OptimizationFunctor : pcl::Functor<float>
00305       {
00311         OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelCylinder<PointT, PointNT> *model) : 
00312           pcl::Functor<float> (m_data_points), model_ (model) {}
00313 
00319         int 
00320         operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
00321         {
00322           Eigen::Vector4f line_pt  (x[0], x[1], x[2], 0);
00323           Eigen::Vector4f line_dir (x[3], x[4], x[5], 0);
00324           
00325           for (int i = 0; i < values (); ++i)
00326           {
00327             // dist = f - r
00328             Eigen::Vector4f pt (model_->input_->points[(*model_->tmp_inliers_)[i]].x,
00329                                 model_->input_->points[(*model_->tmp_inliers_)[i]].y,
00330                                 model_->input_->points[(*model_->tmp_inliers_)[i]].z, 0);
00331 
00332             fvec[i] = static_cast<float> (pcl::sqrPointToLineDistance (pt, line_pt, line_dir) - x[6]*x[6]);
00333           }
00334           return (0);
00335         }
00336 
00337         pcl::SampleConsensusModelCylinder<PointT, PointNT> *model_;
00338       };
00339 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
00340 #pragma GCC diagnostic warning "-Weffc++"
00341 #endif
00342   };
00343 }
00344 
00345 #ifdef PCL_NO_PRECOMPILE
00346 #include <pcl/sample_consensus/impl/sac_model_cylinder.hpp>
00347 #endif
00348 
00349 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_CYLINDER_H_


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:32:16