Point Cloud Library (PCL)  1.9.1-dev
correspondence_estimation.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 #include <string>
44 
45 #include <pcl/pcl_base.h>
46 #include <pcl/common/transforms.h>
47 #include <pcl/search/kdtree.h>
48 #include <pcl/pcl_macros.h>
49 
50 #include <pcl/registration/correspondence_types.h>
51 
52 namespace pcl
53 {
54  namespace registration
55  {
56  /** \brief Abstract @b CorrespondenceEstimationBase class.
57  * All correspondence estimation methods should inherit from this.
58  * \author Radu B. Rusu
59  * \ingroup registration
60  */
61  template <typename PointSource, typename PointTarget, typename Scalar = float>
62  class CorrespondenceEstimationBase: public PCLBase<PointSource>
63  {
64  public:
65  typedef boost::shared_ptr<CorrespondenceEstimationBase<PointSource, PointTarget, Scalar> > Ptr;
66  typedef boost::shared_ptr<const CorrespondenceEstimationBase<PointSource, PointTarget, Scalar> > ConstPtr;
67 
68  // using PCLBase<PointSource>::initCompute;
73 
75  typedef typename KdTree::Ptr KdTreePtr;
76 
78  typedef typename KdTree::Ptr KdTreeReciprocalPtr;
79 
83 
87 
89 
90  /** \brief Empty constructor. */
92  : corr_name_ ("CorrespondenceEstimationBase")
93  , tree_ (new pcl::search::KdTree<PointTarget>)
94  , tree_reciprocal_ (new pcl::search::KdTree<PointSource>)
95  , target_ ()
96  , target_indices_ ()
99  , input_fields_ ()
100  , target_cloud_updated_ (true)
101  , source_cloud_updated_ (true)
102  , force_no_recompute_ (false)
104  {
105  }
106 
107  /** \brief Empty destructor */
109 
110  /** \brief Provide a pointer to the input source
111  * (e.g., the point cloud that we want to align to the target)
112  *
113  * \param[in] cloud the input point cloud source
114  */
115  inline void
116  setInputSource (const PointCloudSourceConstPtr &cloud)
117  {
118  source_cloud_updated_ = true;
120  pcl::getFields (*cloud, input_fields_);
121  }
122 
123  /** \brief Get a pointer to the input point cloud dataset target. */
124  inline PointCloudSourceConstPtr const
126  {
127  return (input_ );
128  }
129 
130  /** \brief Provide a pointer to the input target
131  * (e.g., the point cloud that we want to align the input source to)
132  * \param[in] cloud the input point cloud target
133  */
134  inline void
135  setInputTarget (const PointCloudTargetConstPtr &cloud);
136 
137  /** \brief Get a pointer to the input point cloud dataset target. */
138  inline PointCloudTargetConstPtr const
139  getInputTarget () { return (target_ ); }
140 
141 
142  /** \brief See if this rejector requires source normals */
143  virtual bool
145  { return (false); }
146 
147  /** \brief Abstract method for setting the source normals */
148  virtual void
150  {
151  PCL_WARN ("[pcl::registration::%s::setSourceNormals] This class does not require input source normals", getClassName ().c_str ());
152  }
153 
154  /** \brief See if this rejector requires target normals */
155  virtual bool
157  { return (false); }
158 
159  /** \brief Abstract method for setting the target normals */
160  virtual void
162  {
163  PCL_WARN ("[pcl::registration::%s::setTargetNormals] This class does not require input target normals", getClassName ().c_str ());
164  }
165 
166  /** \brief Provide a pointer to the vector of indices that represent the
167  * input source point cloud.
168  * \param[in] indices a pointer to the vector of indices
169  */
170  inline void
171  setIndicesSource (const IndicesPtr &indices)
172  {
173  setIndices (indices);
174  }
175 
176  /** \brief Get a pointer to the vector of indices used for the source dataset. */
177  inline IndicesPtr const
178  getIndicesSource () { return (indices_); }
179 
180  /** \brief Provide a pointer to the vector of indices that represent the input target point cloud.
181  * \param[in] indices a pointer to the vector of indices
182  */
183  inline void
184  setIndicesTarget (const IndicesPtr &indices)
185  {
186  target_cloud_updated_ = true;
187  target_indices_ = indices;
188  }
189 
190  /** \brief Get a pointer to the vector of indices used for the target dataset. */
191  inline IndicesPtr const
193 
194  /** \brief Provide a pointer to the search object used to find correspondences in
195  * the target cloud.
196  * \param[in] tree a pointer to the spatial search object.
197  * \param[in] force_no_recompute If set to true, this tree will NEVER be
198  * recomputed, regardless of calls to setInputTarget. Only use if you are
199  * confident that the tree will be set correctly.
200  */
201  inline void
202  setSearchMethodTarget (const KdTreePtr &tree,
203  bool force_no_recompute = false)
204  {
205  tree_ = tree;
206  if (force_no_recompute)
207  {
208  force_no_recompute_ = true;
209  }
210  // Since we just set a new tree, we need to check for updates
211  target_cloud_updated_ = true;
212  }
213 
214  /** \brief Get a pointer to the search method used to find correspondences in the
215  * target cloud. */
216  inline KdTreePtr
218  {
219  return (tree_);
220  }
221 
222  /** \brief Provide a pointer to the search object used to find correspondences in
223  * the source cloud (usually used by reciprocal correspondence finding).
224  * \param[in] tree a pointer to the spatial search object.
225  * \param[in] force_no_recompute If set to true, this tree will NEVER be
226  * recomputed, regardless of calls to setInputSource. Only use if you are
227  * extremely confident that the tree will be set correctly.
228  */
229  inline void
230  setSearchMethodSource (const KdTreeReciprocalPtr &tree,
231  bool force_no_recompute = false)
232  {
233  tree_reciprocal_ = tree;
234  if ( force_no_recompute )
235  {
237  }
238  // Since we just set a new tree, we need to check for updates
239  source_cloud_updated_ = true;
240  }
241 
242  /** \brief Get a pointer to the search method used to find correspondences in the
243  * source cloud. */
244  inline KdTreeReciprocalPtr
246  {
247  return (tree_reciprocal_);
248  }
249 
250  /** \brief Determine the correspondences between input and target cloud.
251  * \param[out] correspondences the found correspondences (index of query point, index of target point, distance)
252  * \param[in] max_distance maximum allowed distance between correspondences
253  */
254  virtual void
256  double max_distance = std::numeric_limits<double>::max ()) = 0;
257 
258  /** \brief Determine the reciprocal correspondences between input and target cloud.
259  * A correspondence is considered reciprocal if both Src_i has Tgt_i as a
260  * correspondence, and Tgt_i has Src_i as one.
261  *
262  * \param[out] correspondences the found correspondences (index of query and target point, distance)
263  * \param[in] max_distance maximum allowed distance between correspondences
264  */
265  virtual void
267  double max_distance = std::numeric_limits<double>::max ()) = 0;
268 
269  /** \brief Provide a boost shared pointer to the PointRepresentation to be used
270  * when searching for nearest neighbors.
271  *
272  * \param[in] point_representation the PointRepresentation to be used by the
273  * k-D tree for nearest neighbor search
274  */
275  inline void
276  setPointRepresentation (const PointRepresentationConstPtr &point_representation)
277  {
278  point_representation_ = point_representation;
279  }
280 
281  /** \brief Clone and cast to CorrespondenceEstimationBase */
283 
284  protected:
285  /** \brief The correspondence estimation method name. */
286  std::string corr_name_;
287 
288  /** \brief A pointer to the spatial search object used for the target dataset. */
289  KdTreePtr tree_;
290 
291  /** \brief A pointer to the spatial search object used for the source dataset. */
292  KdTreeReciprocalPtr tree_reciprocal_;
293 
294 
295 
296  /** \brief The input point cloud dataset target. */
297  PointCloudTargetConstPtr target_;
298 
299  /** \brief The target point cloud dataset indices. */
301 
302  /** \brief The point representation used (internal). */
303  PointRepresentationConstPtr point_representation_;
304 
305  /** \brief The transformed input source point cloud dataset. */
306  PointCloudTargetPtr input_transformed_;
307 
308  /** \brief The types of input point fields available. */
309  std::vector<pcl::PCLPointField> input_fields_;
310 
311  /** \brief Abstract class get name method. */
312  inline const std::string&
313  getClassName () const { return (corr_name_); }
314 
315  /** \brief Internal computation initialization. */
316  bool
317  initCompute ();
318 
319  /** \brief Internal computation initialization for reciprocal correspondences. */
320  bool
322 
323  /** \brief Variable that stores whether we have a new target cloud, meaning we need to pre-process it again.
324  * This way, we avoid rebuilding the kd-tree for the target cloud every time the determineCorrespondences () method
325  * is called. */
327  /** \brief Variable that stores whether we have a new source cloud, meaning we need to pre-process it again.
328  * This way, we avoid rebuilding the reciprocal kd-tree for the source cloud every time the determineCorrespondences () method
329  * is called. */
331  /** \brief A flag which, if set, means the tree operating on the target cloud
332  * will never be recomputed*/
334 
335  /** \brief A flag which, if set, means the tree operating on the source cloud
336  * will never be recomputed*/
338 
339  };
340 
341  /** \brief @b CorrespondenceEstimation represents the base class for
342  * determining correspondences between target and query point
343  * sets/features.
344  *
345  * Code example:
346  *
347  * \code
348  * pcl::PointCloud<pcl::PointXYZRGBA>::Ptr source, target;
349  * // ... read or fill in source and target
350  * pcl::CorrespondenceEstimation<pcl::PointXYZ, pcl::PointXYZ> est;
351  * est.setInputSource (source);
352  * est.setInputTarget (target);
353  *
354  * pcl::Correspondences all_correspondences;
355  * // Determine all reciprocal correspondences
356  * est.determineReciprocalCorrespondences (all_correspondences);
357  * \endcode
358  *
359  * \author Radu B. Rusu, Michael Dixon, Dirk Holz
360  * \ingroup registration
361  */
362  template <typename PointSource, typename PointTarget, typename Scalar = float>
363  class CorrespondenceEstimation : public CorrespondenceEstimationBase<PointSource, PointTarget, Scalar>
364  {
365  public:
366  typedef boost::shared_ptr<CorrespondenceEstimation<PointSource, PointTarget, Scalar> > Ptr;
367  typedef boost::shared_ptr<const CorrespondenceEstimation<PointSource, PointTarget, Scalar> > ConstPtr;
368 
383 
386 
390 
394 
396 
397  /** \brief Empty constructor. */
399  {
400  corr_name_ = "CorrespondenceEstimation";
401  }
402 
403  /** \brief Empty destructor */
405 
406  /** \brief Determine the correspondences between input and target cloud.
407  * \param[out] correspondences the found correspondences (index of query point, index of target point, distance)
408  * \param[in] max_distance maximum allowed distance between correspondences
409  */
410  void
412  double max_distance = std::numeric_limits<double>::max ()) override;
413 
414  /** \brief Determine the reciprocal correspondences between input and target cloud.
415  * A correspondence is considered reciprocal if both Src_i has Tgt_i as a
416  * correspondence, and Tgt_i has Src_i as one.
417  *
418  * \param[out] correspondences the found correspondences (index of query and target point, distance)
419  * \param[in] max_distance maximum allowed distance between correspondences
420  */
421  void
423  double max_distance = std::numeric_limits<double>::max ()) override;
424 
425 
426  /** \brief Clone and cast to CorrespondenceEstimationBase */
428  clone () const override
429  {
431  return (copy);
432  }
433  };
434  }
435 }
436 
437 #include <pcl/registration/impl/correspondence_estimation.hpp>
CorrespondenceEstimationBase< PointSource, PointTarget, Scalar >::Ptr clone() const override
Clone and cast to CorrespondenceEstimationBase.
KdTree::PointRepresentationConstPtr PointRepresentationConstPtr
const std::string & getClassName() const
Abstract class get name method.
virtual void setSourceNormals(pcl::PCLPointCloud2::ConstPtr)
Abstract method for setting the source normals.
bool force_no_recompute_
A flag which, if set, means the tree operating on the target cloud will never be recomputed.
boost::shared_ptr< CorrespondenceEstimation< PointSource, PointTarget, Scalar > > Ptr
void setSearchMethodSource(const KdTreeReciprocalPtr &tree, bool force_no_recompute=false)
Provide a pointer to the search object used to find correspondences in the source cloud (usually used...
IndicesPtr const getIndicesTarget()
Get a pointer to the vector of indices used for the target dataset.
boost::shared_ptr< std::vector< int > > IndicesPtr
Definition: pcl_base.h:60
bool source_cloud_updated_
Variable that stores whether we have a new source cloud, meaning we need to pre-process it again...
PointCloudTargetConstPtr target_
The input point cloud dataset target.
std::string corr_name_
The correspondence estimation method name.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
pcl::search::KdTree< PointTarget >::Ptr KdTreePtr
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition: pcl_base.h:153
PointCloudSourceConstPtr const getInputSource()
Get a pointer to the input point cloud dataset target.
PointRepresentationConstPtr point_representation_
The point representation used (internal).
virtual bool requiresTargetNormals() const
See if this rejector requires target normals.
boost::shared_ptr< KdTree< PointT > > Ptr
Definition: kdtree.h:70
KdTreePtr getSearchMethodTarget() const
Get a pointer to the search method used to find correspondences in the target cloud.
IndicesPtr const getIndicesSource()
Get a pointer to the vector of indices used for the source dataset.
std::vector< pcl::PCLPointField > input_fields_
The types of input point fields available.
virtual bool requiresSourceNormals() const
See if this rejector requires source normals.
void setPointRepresentation(const PointRepresentationConstPtr &point_representation)
Provide a boost shared pointer to the PointRepresentation to be used when searching for nearest neigh...
bool target_cloud_updated_
Variable that stores whether we have a new target cloud, meaning we need to pre-process it again...
boost::shared_ptr< PointCloud< PointSource > > Ptr
Definition: point_cloud.h:427
IndicesPtr target_indices_
The target point cloud dataset indices.
bool initCompute()
Internal computation initialization.
boost::shared_ptr< KdTree< PointT, Tree > > Ptr
Definition: kdtree.h:78
virtual void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max())=0
Determine the correspondences between input and target cloud.
void setInputSource(const PointCloudSourceConstPtr &cloud)
Provide a pointer to the input source (e.g., the point cloud that we want to align to the target) ...
PointCloudTargetPtr input_transformed_
The transformed input source point cloud dataset.
boost::shared_ptr< const CorrespondenceEstimation< PointSource, PointTarget, Scalar > > ConstPtr
PointCloudTargetConstPtr const getInputTarget()
Get a pointer to the input point cloud dataset target.
KdTreePtr tree_
A pointer to the spatial search object used for the target dataset.
PCL base class.
Definition: pcl_base.h:68
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
boost::shared_ptr< const PointCloud< PointSource > > ConstPtr
Definition: point_cloud.h:428
boost::shared_ptr< ::pcl::PCLPointCloud2 const > ConstPtr
virtual CorrespondenceEstimationBase< PointSource, PointTarget, Scalar >::Ptr clone() const =0
Clone and cast to CorrespondenceEstimationBase.
bool initComputeReciprocal()
Internal computation initialization for reciprocal correspondences.
virtual void setIndices(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represents the input data.
virtual void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max())=0
Determine the reciprocal correspondences between input and target cloud.
void setIndicesTarget(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represent the input target point cloud.
boost::shared_ptr< const PointRepresentation > PointRepresentationConstPtr
Definition: kdtree.h:67
bool force_no_recompute_reciprocal_
A flag which, if set, means the tree operating on the source cloud will never be recomputed.
void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
virtual void setInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: pcl_base.hpp:66
virtual void setTargetNormals(pcl::PCLPointCloud2::ConstPtr)
Abstract method for setting the target normals.
PointCloudConstPtr input_
The input point cloud dataset.
Definition: pcl_base.h:150
void setSearchMethodTarget(const KdTreePtr &tree, bool force_no_recompute=false)
Provide a pointer to the search object used to find correspondences in the target cloud...
CorrespondenceEstimation represents the base class for determining correspondences between target and...
KdTreeReciprocalPtr getSearchMethodSource() const
Get a pointer to the search method used to find correspondences in the source cloud.
KdTree::PointRepresentationConstPtr PointRepresentationConstPtr
void setIndicesSource(const IndicesPtr &indices)
Provide a pointer to the vector of indices that represent the input source point cloud.
boost::shared_ptr< const CorrespondenceEstimationBase< PointSource, PointTarget, Scalar > > ConstPtr
void getFields(const pcl::PointCloud< PointT > &cloud, std::vector< pcl::PCLPointField > &fields)
Get the list of available fields (i.e., dimension/channel)
Definition: io.hpp:79
boost::shared_ptr< const PointRepresentation< PointTarget > > PointRepresentationConstPtr
Definition: kdtree.h:83
boost::shared_ptr< CorrespondenceEstimationBase< PointSource, PointTarget, Scalar > > Ptr
KdTreeReciprocalPtr tree_reciprocal_
A pointer to the spatial search object used for the source dataset.
Abstract CorrespondenceEstimationBase class.