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