Point Cloud Library (PCL)  1.7.2
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 ("[pcl::registration::CorrespondenceRejectorSampleConsensus::setInputCloud] setInputCloud is deprecated. Please use setInputSource instead.")
104  virtual void
105  setInputCloud (const PointCloudConstPtr &cloud);
106 
107  /** \brief Get a pointer to the input point cloud dataset target. */
108  PCL_DEPRECATED ("[pcl::registration::CorrespondenceRejectorSampleConsensus::getInputCloud] getInputCloud is deprecated. Please use getInputSource instead.")
109  PointCloudConstPtr const
110  getInputCloud ();
111 
112  /** \brief Provide a source point cloud dataset (must contain XYZ data!)
113  * \param[in] cloud a cloud containing XYZ data
114  */
115  virtual inline void
116  setInputSource (const PointCloudConstPtr &cloud)
117  {
118  input_ = cloud;
119  }
120 
121  /** \brief Get a pointer to the input point cloud dataset target. */
122  inline PointCloudConstPtr const
123  getInputSource () { return (input_); }
124 
125  /** \brief Provide a target point cloud dataset (must contain XYZ data!)
126  * \param[in] cloud a cloud containing XYZ data
127  */
128  PCL_DEPRECATED ("[pcl::registration::CorrespondenceRejectorSampleConsensus::setTargetCloud] setTargetCloud is deprecated. Please use setInputTarget instead.")
129  virtual void
130  setTargetCloud (const PointCloudConstPtr &cloud);
131 
132  /** \brief Provide a target point cloud dataset (must contain XYZ data!)
133  * \param[in] cloud a cloud containing XYZ data
134  */
135  virtual inline void
136  setInputTarget (const PointCloudConstPtr &cloud) { target_ = cloud; }
137 
138  /** \brief Get a pointer to the input point cloud dataset target. */
139  inline PointCloudConstPtr const
140  getInputTarget () { return (target_ ); }
141 
142 
143  /** \brief See if this rejector requires source points */
144  bool
146  { return (true); }
147 
148  /** \brief Blob method for setting the source cloud */
149  void
151  {
152  PointCloudPtr cloud (new PointCloud);
153  fromPCLPointCloud2 (*cloud2, *cloud);
154  setInputSource (cloud);
155  }
156 
157  /** \brief See if this rejector requires a target cloud */
158  bool
160  { return (true); }
161 
162  /** \brief Method for setting the target cloud */
163  void
165  {
166  PointCloudPtr cloud (new PointCloud);
167  fromPCLPointCloud2 (*cloud2, *cloud);
168  setInputTarget (cloud);
169  }
170 
171  /** \brief Set the maximum distance between corresponding points.
172  * Correspondences with distances below the threshold are considered as inliers.
173  * \param[in] threshold Distance threshold in the same dimension as source and target data sets.
174  */
175  inline void
176  setInlierThreshold (double threshold) { inlier_threshold_ = threshold; };
177 
178  /** \brief Get the maximum distance between corresponding points.
179  * \return Distance threshold in the same dimension as source and target data sets.
180  */
181  inline double
183 
184  /** \brief Set the maximum number of iterations.
185  * \param[in] max_iterations Maximum number if iterations to run
186  */
187  PCL_DEPRECATED ("[pcl::registration::CorrespondenceRejectorSampleConsensus::setMaxIterations] setMaxIterations is deprecated. Please use setMaximumIterations instead.")
188  void
189  setMaxIterations (int max_iterations);
190 
191  /** \brief Set the maximum number of iterations.
192  * \param[in] max_iterations Maximum number if iterations to run
193  */
194  inline void
195  setMaximumIterations (int max_iterations) { max_iterations_ = std::max (max_iterations, 0); }
196 
197  /** \brief Get the maximum number of iterations.
198  * \return max_iterations Maximum number if iterations to run
199  */
200  PCL_DEPRECATED ("[pcl::registration::CorrespondenceRejectorSampleConsensus::getMaxIterations] getMaxIterations is deprecated. Please use getMaximumIterations instead.")
201  int
202  getMaxIterations ();
203 
204  /** \brief Get the maximum number of iterations.
205  * \return max_iterations Maximum number if iterations to run
206  */
207  inline int
209 
210  /** \brief Get the best transformation after RANSAC rejection.
211  * \return The homogeneous 4x4 transformation yielding the largest number of inliers.
212  */
213  inline Eigen::Matrix4f
215 
216  /** \brief Specify whether the model should be refined internally using the variance of the inliers
217  * \param[in] refine true if the model should be refined, false otherwise
218  */
219  inline void
220  setRefineModel (const bool refine)
221  {
222  refine_ = refine;
223  }
224 
225  /** \brief Get the internal refine parameter value as set by the user using setRefineModel */
226  inline bool
227  getRefineModel () const
228  {
229  return (refine_);
230  }
231 
232  /** \brief Get the inlier indices found by the correspondence rejector. This information is only saved if setSaveInliers(true) was called in advance.
233  * \param[out] inlier_indices Indices for the inliers
234  */
235  inline void
236  getInliersIndices (std::vector<int> &inlier_indices) { inlier_indices = inlier_indices_; }
237 
238  /** \brief Set whether to save inliers or not
239  * \param[in] s True to save inliers / False otherwise
240  */
241  inline void
242  setSaveInliers (bool s) { save_inliers_ = s; }
243 
244  /** \brief Get whether the rejector is configured to save inliers */
245  inline bool
247 
248 
249  protected:
250 
251  /** \brief Apply the rejection algorithm.
252  * \param[out] correspondences the set of resultant correspondences.
253  */
254  inline void
256  {
258  }
259 
261 
263 
264  PointCloudConstPtr input_;
265  PointCloudPtr input_transformed_;
266  PointCloudConstPtr target_;
267 
268  Eigen::Matrix4f best_transformation_;
269 
270  bool refine_;
271  std::vector<int> inlier_indices_;
273 
274  public:
275  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
276  };
277  }
278 }
279 
280 #include <pcl/registration/impl/correspondence_rejection_sample_consensus.hpp>
281 
282 #endif // PCL_REGISTRATION_CORRESPONDENCE_REJECTION_SAMPLE_CONSENSUS_H_