Point Cloud Library (PCL)  1.9.1-dev
correspondence_rejection_var_trimmed.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Open Perception, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #pragma once
41 
42 #include <pcl/registration/correspondence_rejection.h>
43 #include <pcl/point_cloud.h>
44 
45 #include <vector>
46 
47 namespace pcl
48 {
49  namespace registration
50  {
51  /**
52  * @b CorrespondenceRejectoVarTrimmed implements a simple correspondence
53  * rejection method by considering as inliers a certain percentage of correspondences
54  * with the least distances. The percentage of inliers is computed internally as mentioned
55  * in the paper 'Outlier Robust ICP for minimizing Fractional RMSD, J. M. Philips et al'
56  *
57  * \note If \ref setInputCloud and \ref setInputTarget are given, then the
58  * distances between correspondences will be estimated using the given XYZ
59  * data, and not read from the set of input correspondences.
60  *
61  * \author Aravindhan K Krishnan. This code is ported from libpointmatcher (https://github.com/ethz-asl/libpointmatcher)
62  * \ingroup registration
63  */
65  {
69 
70  public:
71  using Ptr = boost::shared_ptr<CorrespondenceRejectorVarTrimmed>;
72  using ConstPtr = boost::shared_ptr<const CorrespondenceRejectorVarTrimmed>;
73 
74  /** \brief Empty constructor. */
76  trimmed_distance_ (0),
77  factor_ (),
78  min_ratio_ (0.05),
79  max_ratio_ (0.95),
80  lambda_ (0.95)
81  {
82  rejection_name_ = "CorrespondenceRejectorVarTrimmed";
83  }
84 
85  /** \brief Get a list of valid correspondences after rejection from the original set of correspondences.
86  * \param[in] original_correspondences the set of initial correspondences given
87  * \param[out] remaining_correspondences the resultant filtered set of remaining correspondences
88  */
89  void
90  getRemainingCorrespondences (const pcl::Correspondences& original_correspondences,
91  pcl::Correspondences& remaining_correspondences) override;
92 
93  /** \brief Get the trimmed distance used for thresholding in correspondence rejection. */
94  inline double
95  getTrimmedDistance () const { return trimmed_distance_; };
96 
97  /** \brief Provide a source point cloud dataset (must contain XYZ
98  * data!), used to compute the correspondence distance.
99  * \param[in] cloud a cloud containing XYZ data
100  */
101  template <typename PointT> inline void
103  {
104  if (!data_container_)
105  data_container_.reset (new DataContainer<PointT>);
106  boost::static_pointer_cast<DataContainer<PointT> > (data_container_)->setInputSource (cloud);
107  }
108 
109  /** \brief Provide a source point cloud dataset (must contain XYZ
110  * data!), used to compute the correspondence distance.
111  * \param[in] cloud a cloud containing XYZ data
112  */
113  template <typename PointT> inline void
115  {
116  PCL_WARN ("[pcl::registration::%s::setInputCloud] setInputCloud is deprecated. Please use setInputSource instead.\n", getClassName ().c_str ());
117  if (!data_container_)
118  data_container_.reset (new DataContainer<PointT>);
119  boost::static_pointer_cast<DataContainer<PointT> > (data_container_)->setInputSource (cloud);
120  }
121 
122  /** \brief Provide a target point cloud dataset (must contain XYZ
123  * data!), used to compute the correspondence distance.
124  * \param[in] target a cloud containing XYZ data
125  */
126  template <typename PointT> inline void
128  {
129  if (!data_container_)
130  data_container_.reset (new DataContainer<PointT>);
131  boost::static_pointer_cast<DataContainer<PointT> > (data_container_)->setInputTarget (target);
132  }
133 
134 
135 
136  /** \brief See if this rejector requires source points */
137  bool
138  requiresSourcePoints () const override
139  { return (true); }
140 
141  /** \brief Blob method for setting the source cloud */
142  void
144  {
146  fromPCLPointCloud2 (*cloud2, *cloud);
147  setInputSource<PointXYZ> (cloud);
148  }
149 
150  /** \brief See if this rejector requires a target cloud */
151  bool
152  requiresTargetPoints () const override
153  { return (true); }
154 
155  /** \brief Method for setting the target cloud */
156  void
158  {
160  fromPCLPointCloud2 (*cloud2, *cloud);
161  setInputTarget<PointXYZ> (cloud);
162  }
163 
164  /** \brief Provide a pointer to the search object used to find correspondences in
165  * the target cloud.
166  * \param[in] tree a pointer to the spatial search object.
167  * \param[in] force_no_recompute If set to true, this tree will NEVER be
168  * recomputed, regardless of calls to setInputTarget. Only use if you are
169  * confident that the tree will be set correctly.
170  */
171  template <typename PointT> inline void
173  bool force_no_recompute = false)
174  {
175  boost::static_pointer_cast< DataContainer<PointT> >
176  (data_container_)->setSearchMethodTarget (tree, force_no_recompute );
177  }
178 
179  /** \brief Get the computed inlier ratio used for thresholding in correspondence rejection. */
180  inline double
181  getTrimFactor () const { return factor_; }
182 
183  /** brief set the minimum overlap ratio
184  * \param[in] ratio the overlap ratio [0..1]
185  */
186  inline void
187  setMinRatio (double ratio) { min_ratio_ = ratio; }
188 
189  /** brief get the minimum overlap ratio
190  */
191  inline double
192  getMinRatio () const { return min_ratio_; }
193 
194  /** brief set the maximum overlap ratio
195  * \param[in] ratio the overlap ratio [0..1]
196  */
197  inline void
198  setMaxRatio (double ratio) { max_ratio_ = ratio; }
199 
200  /** brief get the maximum overlap ratio
201  */
202  inline double
203  getMaxRatio () const { return max_ratio_; }
204 
205  protected:
206 
207  /** \brief Apply the rejection algorithm.
208  * \param[out] correspondences the set of resultant correspondences.
209  */
210  inline void
211  applyRejection (pcl::Correspondences &correspondences) override
212  {
213  getRemainingCorrespondences (*input_correspondences_, correspondences);
214  }
215 
216  /** \brief The inlier distance threshold (based on the computed trim factor) between two correspondent points in source <-> target.
217  */
219 
220  /** \brief The factor for correspondence rejection. Only factor times the total points sorted based on
221  * the correspondence distances will be considered as inliers. Remaining points are rejected. This factor is
222  * computed internally
223  */
224  double factor_;
225 
226  /** \brief The minimum overlap ratio between the input and target clouds
227  */
228  double min_ratio_;
229 
230  /** \brief The maximum overlap ratio between the input and target clouds
231  */
232  double max_ratio_;
233 
234  /** \brief part of the term that balances the root mean square difference. This is an internal parameter
235  */
236  double lambda_;
237 
239 
240  /** \brief A pointer to the DataContainer object containing the input and target point clouds */
242 
243  private:
244 
245  /** \brief finds the optimal inlier ratio. This is based on the paper 'Outlier Robust ICP for minimizing Fractional RMSD, J. M. Philips et al'
246  */
247  inline float optimizeInlierRatio (std::vector <double> &dists);
248  };
249  }
250 }
251 
252 #include <pcl/registration/impl/correspondence_rejection_var_trimmed.hpp>
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
DataContainer is a container for the input and target point clouds and implements the interface to co...
CorrespondenceRejectoVarTrimmed implements a simple correspondence rejection method by considering as...
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
CorrespondenceRejector represents the base class for correspondence rejection methods ...
double trimmed_distance_
The inlier distance threshold (based on the computed trim factor) between two correspondent points in...
void setTargetPoints(pcl::PCLPointCloud2::ConstPtr cloud2) override
Method for setting the target cloud.
double getMinRatio() const
brief get the minimum overlap ratio
void setInputTarget(const typename pcl::PointCloud< PointT >::ConstPtr &target)
Provide a target point cloud dataset (must contain XYZ data!), used to compute the correspondence dis...
const std::string & getClassName() const
Get a string representation of the name of this class.
DataContainerPtr data_container_
A pointer to the DataContainer object containing the input and target point clouds.
double lambda_
part of the term that balances the root mean square difference.
boost::shared_ptr< DataContainerInterface > Ptr
void setSourcePoints(pcl::PCLPointCloud2::ConstPtr cloud2) override
Blob method for setting the source cloud.
void setMinRatio(double ratio)
brief set the minimum overlap ratio
void setMaxRatio(double ratio)
brief set the maximum overlap ratio
boost::shared_ptr< KdTree< PointT, Tree > > Ptr
Definition: kdtree.h:78
bool requiresTargetPoints() const override
See if this rejector requires a target cloud.
void setInputSource(const typename pcl::PointCloud< PointT >::ConstPtr &cloud)
Provide a source point cloud dataset (must contain XYZ data!), used to compute the correspondence dis...
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:412
PointCloud represents the base class in PCL for storing collections of 3D points. ...
double min_ratio_
The minimum overlap ratio between the input and target clouds.
bool requiresSourcePoints() const override
See if this rejector requires source points.
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:413
void applyRejection(pcl::Correspondences &correspondences) override
Apply the rejection algorithm.
boost::shared_ptr< CorrespondenceRejector > Ptr
void setInputCloud(const typename pcl::PointCloud< PointT >::ConstPtr &cloud)
Provide a source point cloud dataset (must contain XYZ data!), used to compute the correspondence dis...
double getMaxRatio() const
brief get the maximum overlap ratio
CorrespondencesConstPtr input_correspondences_
The input correspondences.
std::string rejection_name_
The name of the rejection method.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
double getTrimmedDistance() const
Get the trimmed distance used for thresholding in correspondence rejection.
void setSearchMethodTarget(const typename pcl::search::KdTree< PointT >::Ptr &tree, bool force_no_recompute=false)
Provide a pointer to the search object used to find correspondences in the target cloud...
double getTrimFactor() const
Get the computed inlier ratio used for thresholding in correspondence rejection.
boost::shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
#define PCL_EXPORTS
Definition: pcl_macros.h:241
boost::shared_ptr< const CorrespondenceRejector > ConstPtr
double max_ratio_
The maximum overlap ratio between the input and target clouds.