Point Cloud Library (PCL)  1.11.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 
41 #pragma once
42 
43 
44 #include <pcl/memory.h>
45 #include <pcl/registration/correspondence_rejection.h>
46 
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  using PointCloudPtr = typename PointCloud::Ptr;
63  using PointCloudConstPtr = typename PointCloud::ConstPtr;
64 
65  public:
69 
70  using Ptr = shared_ptr<CorrespondenceRejectorSampleConsensus<PointT> >;
71  using ConstPtr = shared_ptr<const CorrespondenceRejectorSampleConsensus<PointT> >;
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_ ()
82  , refine_ (false)
83  , save_inliers_ (false)
84  {
85  rejection_name_ = "CorrespondenceRejectorSampleConsensus";
86  }
87 
88  /** \brief Empty destructor. */
90 
91  /** \brief Get a list of valid correspondences after rejection from the original set of correspondences.
92  * \param[in] original_correspondences the set of initial correspondences given
93  * \param[out] remaining_correspondences the resultant filtered set of remaining correspondences
94  */
95  inline void
96  getRemainingCorrespondences (const pcl::Correspondences& original_correspondences,
97  pcl::Correspondences& remaining_correspondences) override;
98 
99  /** \brief Provide a source point cloud dataset (must contain XYZ data!)
100  * \param[in] cloud a cloud containing XYZ data
101  */
102  virtual inline void
103  setInputSource (const PointCloudConstPtr &cloud)
104  {
105  input_ = cloud;
106  }
107 
108  /** \brief Get a pointer to the input point cloud dataset target. */
109  inline PointCloudConstPtr const
110  getInputSource () { return (input_); }
111 
112  /** \brief Provide a target point cloud dataset (must contain XYZ data!)
113  * \param[in] cloud a cloud containing XYZ data
114  */
115  virtual inline void
116  setInputTarget (const PointCloudConstPtr &cloud) { target_ = cloud; }
117 
118  /** \brief Get a pointer to the input point cloud dataset target. */
119  inline PointCloudConstPtr const
120  getInputTarget () { return (target_ ); }
121 
122 
123  /** \brief See if this rejector requires source points */
124  bool
125  requiresSourcePoints () const override
126  { return (true); }
127 
128  /** \brief Blob method for setting the source cloud */
129  void
131  {
132  PointCloudPtr cloud (new PointCloud);
133  fromPCLPointCloud2 (*cloud2, *cloud);
134  setInputSource (cloud);
135  }
136 
137  /** \brief See if this rejector requires a target cloud */
138  bool
139  requiresTargetPoints () const override
140  { return (true); }
141 
142  /** \brief Method for setting the target cloud */
143  void
145  {
146  PointCloudPtr cloud (new PointCloud);
147  fromPCLPointCloud2 (*cloud2, *cloud);
148  setInputTarget (cloud);
149  }
150 
151  /** \brief Set the maximum distance between corresponding points.
152  * Correspondences with distances below the threshold are considered as inliers.
153  * \param[in] threshold Distance threshold in the same dimension as source and target data sets.
154  */
155  inline void
156  setInlierThreshold (double threshold) { inlier_threshold_ = threshold; };
157 
158  /** \brief Get the maximum distance between corresponding points.
159  * \return Distance threshold in the same dimension as source and target data sets.
160  */
161  inline double
163 
164  /** \brief Set the maximum number of iterations.
165  * \param[in] max_iterations Maximum number if iterations to run
166  */
167  inline void
168  setMaximumIterations (int max_iterations) { max_iterations_ = std::max (max_iterations, 0); }
169 
170  /** \brief Get the maximum number of iterations.
171  * \return max_iterations Maximum number if iterations to run
172  */
173  inline int
175 
176  /** \brief Get the best transformation after RANSAC rejection.
177  * \return The homogeneous 4x4 transformation yielding the largest number of inliers.
178  */
179  inline Eigen::Matrix4f
181 
182  /** \brief Specify whether the model should be refined internally using the variance of the inliers
183  * \param[in] refine true if the model should be refined, false otherwise
184  */
185  inline void
186  setRefineModel (const bool refine)
187  {
188  refine_ = refine;
189  }
190 
191  /** \brief Get the internal refine parameter value as set by the user using setRefineModel */
192  inline bool
193  getRefineModel () const
194  {
195  return (refine_);
196  }
197 
198  /** \brief Get the inlier indices found by the correspondence rejector. This information is only saved if setSaveInliers(true) was called in advance.
199  * \param[out] inlier_indices Indices for the inliers
200  */
201  inline void
202  getInliersIndices (std::vector<int> &inlier_indices) { inlier_indices = inlier_indices_; }
203 
204  /** \brief Set whether to save inliers or not
205  * \param[in] s True to save inliers / False otherwise
206  */
207  inline void
208  setSaveInliers (bool s) { save_inliers_ = s; }
209 
210  /** \brief Get whether the rejector is configured to save inliers */
211  inline bool
213 
214 
215  protected:
216 
217  /** \brief Apply the rejection algorithm.
218  * \param[out] correspondences the set of resultant correspondences.
219  */
220  inline void
221  applyRejection (pcl::Correspondences &correspondences) override
222  {
224  }
225 
227 
229 
230  PointCloudConstPtr input_;
231  PointCloudPtr input_transformed_;
232  PointCloudConstPtr target_;
233 
234  Eigen::Matrix4f best_transformation_;
235 
236  bool refine_;
237  std::vector<int> inlier_indices_;
239 
240  public:
242  };
243  }
244 }
245 
246 #include <pcl/registration/impl/correspondence_rejection_sample_consensus.hpp>
pcl::registration::CorrespondenceRejector::Ptr
shared_ptr< CorrespondenceRejector > Ptr
Definition: correspondence_rejection.h:61
pcl::registration::CorrespondenceRejectorSampleConsensus::setTargetPoints
void setTargetPoints(pcl::PCLPointCloud2::ConstPtr cloud2) override
Method for setting the target cloud.
Definition: correspondence_rejection_sample_consensus.h:144
pcl
Definition: convolution.h:46
pcl::registration::CorrespondenceRejectorSampleConsensus::setSourcePoints
void setSourcePoints(pcl::PCLPointCloud2::ConstPtr cloud2) override
Blob method for setting the source cloud.
Definition: correspondence_rejection_sample_consensus.h:130
pcl::registration::CorrespondenceRejector::ConstPtr
shared_ptr< const CorrespondenceRejector > ConstPtr
Definition: correspondence_rejection.h:62
pcl::registration::CorrespondenceRejectorSampleConsensus::getInlierThreshold
double getInlierThreshold()
Get the maximum distance between corresponding points.
Definition: correspondence_rejection_sample_consensus.h:162
pcl::registration::CorrespondenceRejector::getClassName
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: correspondence_rejection.h:132
pcl::registration::CorrespondenceRejectorSampleConsensus::target_
PointCloudConstPtr target_
Definition: correspondence_rejection_sample_consensus.h:232
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:181
pcl::registration::CorrespondenceRejectorSampleConsensus::input_transformed_
PointCloudPtr input_transformed_
Definition: correspondence_rejection_sample_consensus.h:231
pcl::registration::CorrespondenceRejectorSampleConsensus::getMaximumIterations
int getMaximumIterations()
Get the maximum number of iterations.
Definition: correspondence_rejection_sample_consensus.h:174
pcl::registration::CorrespondenceRejectorSampleConsensus::setInputTarget
virtual void setInputTarget(const PointCloudConstPtr &cloud)
Provide a target point cloud dataset (must contain XYZ data!)
Definition: correspondence_rejection_sample_consensus.h:116
pcl::registration::CorrespondenceRejectorSampleConsensus::getInliersIndices
void getInliersIndices(std::vector< int > &inlier_indices)
Get the inlier indices found by the correspondence rejector.
Definition: correspondence_rejection_sample_consensus.h:202
pcl::registration::CorrespondenceRejectorSampleConsensus::requiresTargetPoints
bool requiresTargetPoints() const override
See if this rejector requires a target cloud.
Definition: correspondence_rejection_sample_consensus.h:139
pcl::registration::CorrespondenceRejectorSampleConsensus::getBestTransformation
Eigen::Matrix4f getBestTransformation()
Get the best transformation after RANSAC rejection.
Definition: correspondence_rejection_sample_consensus.h:180
pcl::registration::CorrespondenceRejectorSampleConsensus::setMaximumIterations
void setMaximumIterations(int max_iterations)
Set the maximum number of iterations.
Definition: correspondence_rejection_sample_consensus.h:168
pcl::registration::CorrespondenceRejectorSampleConsensus::getInputSource
PointCloudConstPtr const getInputSource()
Get a pointer to the input point cloud dataset target.
Definition: correspondence_rejection_sample_consensus.h:110
pcl::registration::CorrespondenceRejectorSampleConsensus::rejection_name_
std::string rejection_name_
The name of the rejection method.
Definition: correspondence_rejection.h:185
pcl::registration::CorrespondenceRejectorSampleConsensus::refine_
bool refine_
Definition: correspondence_rejection_sample_consensus.h:236
pcl::registration::CorrespondenceRejectorSampleConsensus::CorrespondenceRejectorSampleConsensus
CorrespondenceRejectorSampleConsensus()
Empty constructor.
Definition: correspondence_rejection_sample_consensus.h:76
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition: PCLPointCloud2.h:35
pcl::registration::CorrespondenceRejectorSampleConsensus::getRefineModel
bool getRefineModel() const
Get the internal refine parameter value as set by the user using setRefineModel.
Definition: correspondence_rejection_sample_consensus.h:193
pcl::registration::CorrespondenceRejectorSampleConsensus::setInlierThreshold
void setInlierThreshold(double threshold)
Set the maximum distance between corresponding points.
Definition: correspondence_rejection_sample_consensus.h:156
PCL_MAKE_ALIGNED_OPERATOR_NEW
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
pcl::registration::CorrespondenceRejectorSampleConsensus::input_correspondences_
CorrespondencesConstPtr input_correspondences_
The input correspondences.
Definition: correspondence_rejection.h:188
pcl::registration::CorrespondenceRejectorSampleConsensus::applyRejection
void applyRejection(pcl::Correspondences &correspondences) override
Apply the rejection algorithm.
Definition: correspondence_rejection_sample_consensus.h:221
pcl::registration::CorrespondenceRejectorSampleConsensus::requiresSourcePoints
bool requiresSourcePoints() const override
See if this rejector requires source points.
Definition: correspondence_rejection_sample_consensus.h:125
pcl::registration::CorrespondenceRejectorSampleConsensus
CorrespondenceRejectorSampleConsensus implements a correspondence rejection using Random Sample Conse...
Definition: correspondence_rejection_sample_consensus.h:60
pcl::registration::CorrespondenceRejectorSampleConsensus::getSaveInliers
bool getSaveInliers()
Get whether the rejector is configured to save inliers.
Definition: correspondence_rejection_sample_consensus.h:212
pcl::registration::CorrespondenceRejectorSampleConsensus::best_transformation_
Eigen::Matrix4f best_transformation_
Definition: correspondence_rejection_sample_consensus.h:234
pcl::registration::CorrespondenceRejectorSampleConsensus::~CorrespondenceRejectorSampleConsensus
~CorrespondenceRejectorSampleConsensus()
Empty destructor.
Definition: correspondence_rejection_sample_consensus.h:89
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
pcl::registration::CorrespondenceRejectorSampleConsensus::setSaveInliers
void setSaveInliers(bool s)
Set whether to save inliers or not.
Definition: correspondence_rejection_sample_consensus.h:208
pcl::registration::CorrespondenceRejectorSampleConsensus::inlier_indices_
std::vector< int > inlier_indices_
Definition: correspondence_rejection_sample_consensus.h:237
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:430
pcl::registration::CorrespondenceRejectorSampleConsensus::getRemainingCorrespondences
void getRemainingCorrespondences(const pcl::Correspondences &original_correspondences, pcl::Correspondences &remaining_correspondences) override
Get a list of valid correspondences after rejection from the original set of correspondences.
Definition: correspondence_rejection_sample_consensus.hpp:56
pcl::registration::CorrespondenceRejectorSampleConsensus::max_iterations_
int max_iterations_
Definition: correspondence_rejection_sample_consensus.h:228
pcl::registration::CorrespondenceRejectorSampleConsensus::input_
PointCloudConstPtr input_
Definition: correspondence_rejection_sample_consensus.h:230
pcl::registration::CorrespondenceRejectorSampleConsensus::getInputTarget
PointCloudConstPtr const getInputTarget()
Get a pointer to the input point cloud dataset target.
Definition: correspondence_rejection_sample_consensus.h:120
pcl::registration::CorrespondenceRejectorSampleConsensus::save_inliers_
bool save_inliers_
Definition: correspondence_rejection_sample_consensus.h:238
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:90
pcl::registration::CorrespondenceRejectorSampleConsensus::setRefineModel
void setRefineModel(const bool refine)
Specify whether the model should be refined internally using the variance of the inliers.
Definition: correspondence_rejection_sample_consensus.h:186
pcl::registration::CorrespondenceRejectorSampleConsensus::setInputSource
virtual void setInputSource(const PointCloudConstPtr &cloud)
Provide a source point cloud dataset (must contain XYZ data!)
Definition: correspondence_rejection_sample_consensus.h:103
pcl::registration::CorrespondenceRejectorSampleConsensus::inlier_threshold_
double inlier_threshold_
Definition: correspondence_rejection_sample_consensus.h:226
pcl::registration::CorrespondenceRejector::input_correspondences_
CorrespondencesConstPtr input_correspondences_
The input correspondences.
Definition: correspondence_rejection.h:188
pcl::registration::CorrespondenceRejector::rejection_name_
std::string rejection_name_
The name of the rejection method.
Definition: correspondence_rejection.h:185
memory.h
Defines functions, macros and traits for allocating and using memory.
pcl::registration::CorrespondenceRejector
CorrespondenceRejector represents the base class for correspondence rejection methods
Definition: correspondence_rejection.h:59
pcl::fromPCLPointCloud2
void fromPCLPointCloud2(const pcl::PCLPointCloud2 &msg, pcl::PointCloud< PointT > &cloud, const MsgFieldMap &field_map)
Convert a PCLPointCloud2 binary data blob into a pcl::PointCloud<T> object using a field_map.
Definition: conversions.h:168