Point Cloud Library (PCL)  1.7.1
correspondence_rejection_sample_consensus.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 #ifndef PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_
41 #define PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_
42 
43 #include <pcl/registration/correspondence_rejection.h>
44 
45 #include <pcl/sample_consensus/ransac.h>
46 #include <pcl/sample_consensus/sac_model_registration.h>
47 #include <pcl/common/transforms.h>
48 
49 namespace pcl
50 {
51  namespace registration
52  {
53  /** \brief CorrespondenceRejectorSampleConsensus implements a correspondence rejection
54  * using Random Sample Consensus to identify inliers (and reject outliers)
55  * \author Dirk Holz
56  * \ingroup registration
57  */
58  template <typename PointT>
60  {
62  typedef typename PointCloud::Ptr PointCloudPtr;
63  typedef typename PointCloud::ConstPtr PointCloudConstPtr;
64 
65  public:
69 
70  typedef boost::shared_ptr<CorrespondenceRejectorSampleConsensus> Ptr;
71  typedef boost::shared_ptr<const CorrespondenceRejectorSampleConsensus> ConstPtr;
72 
73  /** \brief Empty constructor. Sets the inlier threshold to 5cm (0.05m),
74  * and the maximum number of iterations to 1000.
75  */
77  : inlier_threshold_ (0.05)
78  , max_iterations_ (1000) // std::numeric_limits<int>::max ()
79  , input_ ()
81  , target_ ()
83  , refine_ (false)
84  , save_inliers_ (false)
85  {
86  rejection_name_ = "CorrespondenceRejectorSampleConsensus";
87  }
88 
89  /** \brief Empty destructor. */
91 
92  /** \brief Get a list of valid correspondences after rejection from the original set of correspondences.
93  * \param[in] original_correspondences the set of initial correspondences given
94  * \param[out] remaining_correspondences the resultant filtered set of remaining correspondences
95  */
96  inline void
97  getRemainingCorrespondences (const pcl::Correspondences& original_correspondences,
98  pcl::Correspondences& remaining_correspondences);
99 
100  /** \brief Provide a source point cloud dataset (must contain XYZ data!)
101  * \param[in] cloud a cloud containing XYZ data
102  */
103  PCL_DEPRECATED (virtual void setInputCloud (const PointCloudConstPtr &cloud),
104  "[pcl::registration::CorrespondenceRejectorSampleConsensus::setInputCloud] setInputCloud is deprecated. Please use setInputSource instead.");
105 
106  /** \brief Get a pointer to the input point cloud dataset target. */
107  PCL_DEPRECATED (PointCloudConstPtr const getInputCloud (), "[pcl::registration::CorrespondenceRejectorSampleConsensus::getInputCloud] getInputCloud is deprecated. Please use getInputSource instead.");
108 
109  /** \brief Provide a source point cloud dataset (must contain XYZ data!)
110  * \param[in] cloud a cloud containing XYZ data
111  */
112  virtual inline void
113  setInputSource (const PointCloudConstPtr &cloud)
114  {
115  input_ = cloud;
116  }
117 
118  /** \brief Get a pointer to the input point cloud dataset target. */
119  inline PointCloudConstPtr const
120  getInputSource () { return (input_); }
121 
122  /** \brief Provide a target point cloud dataset (must contain XYZ data!)
123  * \param[in] cloud a cloud containing XYZ data
124  */
125  PCL_DEPRECATED (virtual void setTargetCloud (const PointCloudConstPtr &cloud), "[pcl::registration::CorrespondenceRejectorSampleConsensus::setTargetCloud] setTargetCloud is deprecated. Please use setInputTarget instead.");
126 
127  /** \brief Provide a target point cloud dataset (must contain XYZ data!)
128  * \param[in] cloud a cloud containing XYZ data
129  */
130  virtual inline void
131  setInputTarget (const PointCloudConstPtr &cloud) { target_ = cloud; }
132 
133  /** \brief Get a pointer to the input point cloud dataset target. */
134  inline PointCloudConstPtr const
135  getInputTarget () { return (target_ ); }
136 
137  /** \brief Set the maximum distance between corresponding points.
138  * Correspondences with distances below the threshold are considered as inliers.
139  * \param[in] threshold Distance threshold in the same dimension as source and target data sets.
140  */
141  inline void
142  setInlierThreshold (double threshold) { inlier_threshold_ = threshold; };
143 
144  /** \brief Get the maximum distance between corresponding points.
145  * \return Distance threshold in the same dimension as source and target data sets.
146  */
147  inline double
149 
150  /** \brief Set the maximum number of iterations.
151  * \param[in] max_iterations Maximum number if iterations to run
152  */
153  PCL_DEPRECATED (void setMaxIterations (int max_iterations), "[pcl::registration::CorrespondenceRejectorSampleConsensus::setMaxIterations] setMaxIterations is deprecated. Please use setMaximumIterations instead.");
154 
155  /** \brief Set the maximum number of iterations.
156  * \param[in] max_iterations Maximum number if iterations to run
157  */
158  inline void
159  setMaximumIterations (int max_iterations) { max_iterations_ = std::max (max_iterations, 0); }
160 
161  /** \brief Get the maximum number of iterations.
162  * \return max_iterations Maximum number if iterations to run
163  */
164  PCL_DEPRECATED (int getMaxIterations (), "[pcl::registration::CorrespondenceRejectorSampleConsensus::getMaxIterations] getMaxIterations is deprecated. Please use getMaximumIterations instead.");
165 
166  /** \brief Get the maximum number of iterations.
167  * \return max_iterations Maximum number if iterations to run
168  */
169  inline int
171 
172  /** \brief Get the best transformation after RANSAC rejection.
173  * \return The homogeneous 4x4 transformation yielding the largest number of inliers.
174  */
175  inline Eigen::Matrix4f
177 
178  /** \brief Specify whether the model should be refined internally using the variance of the inliers
179  * \param[in] refine true if the model should be refined, false otherwise
180  */
181  inline void
182  setRefineModel (const bool refine)
183  {
184  refine_ = refine;
185  }
186 
187  /** \brief Get the internal refine parameter value as set by the user using setRefineModel */
188  inline bool
189  getRefineModel () const
190  {
191  return (refine_);
192  }
193 
194  /** \brief Get the inlier indices found by the correspondence rejector. This information is only saved if setSaveInliers(true) was called in advance.
195  * \param[out] inlier_indices Indices for the inliers
196  */
197  inline void
198  getInliersIndices (std::vector<int> &inlier_indices) { inlier_indices = inlier_indices_; }
199 
200  /** \brief Set whether to save inliers or not
201  * \param[in] s True to save inliers / False otherwise
202  */
203  inline void
204  setSaveInliers (bool s) { save_inliers_ = s; }
205 
206  /** \brief Get whether the rejector is configured to save inliers */
207  inline bool
209 
210 
211  protected:
212 
213  /** \brief Apply the rejection algorithm.
214  * \param[out] correspondences the set of resultant correspondences.
215  */
216  inline void
218  {
220  }
221 
223 
225 
226  PointCloudConstPtr input_;
227  PointCloudPtr input_transformed_;
228  PointCloudConstPtr target_;
229 
230  Eigen::Matrix4f best_transformation_;
231 
232  bool refine_;
233  std::vector<int> inlier_indices_;
235 
236  public:
237  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
238  };
239  }
240 }
241 
242 #include <pcl/registration/impl/correspondence_rejection_sample_consensus.hpp>
243 
244 #endif // PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_