sac_model_sphere.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_SPHERE_H_
00042 #define PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_H_
00043 
00044 #include <pcl/sample_consensus/sac_model.h>
00045 #include <pcl/sample_consensus/model_types.h>
00046 
00047 namespace pcl
00048 {
00059   template <typename PointT>
00060   class SampleConsensusModelSphere : public SampleConsensusModel<PointT>
00061   {
00062     public:
00063       using SampleConsensusModel<PointT>::input_;
00064       using SampleConsensusModel<PointT>::indices_;
00065       using SampleConsensusModel<PointT>::radius_min_;
00066       using SampleConsensusModel<PointT>::radius_max_;
00067       using SampleConsensusModel<PointT>::error_sqr_dists_;
00068 
00069       typedef typename SampleConsensusModel<PointT>::PointCloud PointCloud;
00070       typedef typename SampleConsensusModel<PointT>::PointCloudPtr PointCloudPtr;
00071       typedef typename SampleConsensusModel<PointT>::PointCloudConstPtr PointCloudConstPtr;
00072 
00073       typedef boost::shared_ptr<SampleConsensusModelSphere> Ptr;
00074 
00079       SampleConsensusModelSphere (const PointCloudConstPtr &cloud,
00080                                   bool random = false) 
00081         : SampleConsensusModel<PointT> (cloud, random), tmp_inliers_ ()
00082       {}
00083 
00089       SampleConsensusModelSphere (const PointCloudConstPtr &cloud, 
00090                                   const std::vector<int> &indices,
00091                                   bool random = false) 
00092         : SampleConsensusModel<PointT> (cloud, indices, random), tmp_inliers_ ()
00093       {}
00094       
00096       virtual ~SampleConsensusModelSphere () {}
00097 
00101       SampleConsensusModelSphere (const SampleConsensusModelSphere &source) :
00102         SampleConsensusModel<PointT> (), tmp_inliers_ () 
00103       {
00104         *this = source;
00105       }
00106 
00110       inline SampleConsensusModelSphere&
00111       operator = (const SampleConsensusModelSphere &source)
00112       {
00113         SampleConsensusModel<PointT>::operator=(source);
00114         tmp_inliers_ = source.tmp_inliers_;
00115         return (*this);
00116       }
00117 
00124       bool 
00125       computeModelCoefficients (const std::vector<int> &samples, 
00126                                 Eigen::VectorXf &model_coefficients);
00127 
00132       void 
00133       getDistancesToModel (const Eigen::VectorXf &model_coefficients, 
00134                            std::vector<double> &distances);
00135 
00141       void 
00142       selectWithinDistance (const Eigen::VectorXf &model_coefficients, 
00143                             const double threshold, 
00144                             std::vector<int> &inliers);
00145 
00152       virtual int
00153       countWithinDistance (const Eigen::VectorXf &model_coefficients, 
00154                            const double threshold);
00155 
00162       void 
00163       optimizeModelCoefficients (const std::vector<int> &inliers, 
00164                                  const Eigen::VectorXf &model_coefficients, 
00165                                  Eigen::VectorXf &optimized_coefficients);
00166 
00174       void 
00175       projectPoints (const std::vector<int> &inliers, 
00176                      const Eigen::VectorXf &model_coefficients, 
00177                      PointCloud &projected_points, 
00178                      bool copy_data_fields = true);
00179 
00185       bool 
00186       doSamplesVerifyModel (const std::set<int> &indices, 
00187                             const Eigen::VectorXf &model_coefficients, 
00188                             const double threshold);
00189 
00191       inline pcl::SacModel getModelType () const { return (SACMODEL_SPHERE); }
00192 
00193     protected:
00197       inline bool 
00198       isModelValid (const Eigen::VectorXf &model_coefficients)
00199       {
00200         // Needs a valid model coefficients
00201         if (model_coefficients.size () != 4)
00202         {
00203           PCL_ERROR ("[pcl::SampleConsensusModelSphere::isModelValid] Invalid number of model coefficients given (%zu)!\n", model_coefficients.size ());
00204           return (false);
00205         }
00206 
00207         if (radius_min_ != -std::numeric_limits<double>::max() && model_coefficients[3] < radius_min_)
00208           return (false);
00209         if (radius_max_ != std::numeric_limits<double>::max() && model_coefficients[3] > radius_max_)
00210           return (false);
00211 
00212         return (true);
00213       }
00214 
00219       bool
00220       isSampleGood(const std::vector<int> &samples) const;
00221 
00222     private:
00224       const std::vector<int> *tmp_inliers_;
00225 
00226 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
00227 #pragma GCC diagnostic ignored "-Weffc++"
00228 #endif
00229       struct OptimizationFunctor : pcl::Functor<float>
00230       {
00236         OptimizationFunctor (int m_data_points, pcl::SampleConsensusModelSphere<PointT> *model) : 
00237           pcl::Functor<float>(m_data_points), model_ (model) {}
00238 
00244         int 
00245         operator() (const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
00246         {
00247           Eigen::Vector4f cen_t;
00248           cen_t[3] = 0;
00249           for (int i = 0; i < values (); ++i)
00250           {
00251             // Compute the difference between the center of the sphere and the datapoint X_i
00252             cen_t[0] = model_->input_->points[(*model_->tmp_inliers_)[i]].x - x[0];
00253             cen_t[1] = model_->input_->points[(*model_->tmp_inliers_)[i]].y - x[1];
00254             cen_t[2] = model_->input_->points[(*model_->tmp_inliers_)[i]].z - x[2];
00255             
00256             // g = sqrt ((x-a)^2 + (y-b)^2 + (z-c)^2) - R
00257             fvec[i] = sqrtf (cen_t.dot (cen_t)) - x[3];
00258           }
00259           return (0);
00260         }
00261         
00262         pcl::SampleConsensusModelSphere<PointT> *model_;
00263       };
00264 #if defined BUILD_Maintainer && defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ > 3
00265 #pragma GCC diagnostic warning "-Weffc++"
00266 #endif
00267    };
00268 }
00269 
00270 #ifdef PCL_NO_PRECOMPILE
00271 #include <pcl/sample_consensus/impl/sac_model_sphere.hpp>
00272 #endif
00273 
00274 #endif  //#ifndef PCL_SAMPLE_CONSENSUS_MODEL_SPHERE_H_


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