Point Cloud Library (PCL)  1.13.1-dev
trimmed_icp.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, 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 Willow Garage, 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 /*
41  * trimmed_icp.h
42  *
43  * Created on: Mar 10, 2013
44  * Author: papazov
45  */
46 
47 #pragma once
48 
49 #include <pcl/registration/transformation_estimation_svd.h>
50 #include <pcl/kdtree/kdtree_flann.h>
51 #include <pcl/correspondence.h>
52 #include <pcl/point_cloud.h>
53 #include <pcl/pcl_exports.h>
54 #include <limits>
55 #include <pcl/recognition/ransac_based/auxiliary.h>
56 
57 namespace pcl
58 {
59  namespace recognition
60  {
61  template<typename PointT, typename Scalar>
63  {
64  public:
67 
68  using Matrix4 = typename Eigen::Matrix<Scalar, 4, 4>;
69 
70  public:
72  : new_to_old_energy_ratio_ (0.99f)
73  {}
74 
75  ~TrimmedICP () override = default;
76 
77  /** \brief Call this method before calling align().
78  *
79  * \param[in] target is target point cloud. The method builds a kd-tree based on 'target' for performing fast closest point search.
80  * The source point cloud will be registered to 'target' (see align() method).
81  * */
82  inline void
83  init (const PointCloudConstPtr& target)
84  {
85  target_points_ = target;
86  kdtree_.setInputCloud (target);
87  }
88 
89  /** \brief The method performs trimmed ICP, i.e., it rigidly registers the source to the target (passed to the init() method).
90  *
91  * \param[in] source_points is the point cloud to be registered to the target.
92  * \param[in] num_source_points_to_use gives the number of closest source points taken into account for registration. By closest
93  * source points we mean the source points closest to the target. These points are computed anew at each iteration.
94  * \param[in,out] guess_and_result is the estimated rigid transform. IMPORTANT: this matrix is also taken as the initial guess
95  * for the alignment. If there is no guess, set the matrix to identity!
96  * */
97  inline void
98  align (const PointCloud& source_points, int num_source_points_to_use, Matrix4& guess_and_result) const
99  {
100  int num_trimmed_source_points = num_source_points_to_use, num_source_points = static_cast<int> (source_points.size ());
101 
102  if ( num_trimmed_source_points >= num_source_points )
103  {
104  printf ("WARNING in 'TrimmedICP::%s()': the user-defined number of source points of interest is greater or equal to "
105  "the total number of source points. Trimmed ICP will work correctly but won't be very efficient. Either set "
106  "the number of source points to use to a lower value or use standard ICP.\n", __func__);
107  num_trimmed_source_points = num_source_points;
108  }
109 
110  // These are vectors containing source to target correspondences
111  pcl::Correspondences full_src_to_tgt (num_source_points), trimmed_src_to_tgt (num_trimmed_source_points);
112 
113  // Some variables for the closest point search
114  pcl::PointXYZ transformed_source_point;
115  pcl::Indices target_index (1);
116  std::vector<float> sqr_dist_to_target (1);
117  float old_energy, energy = std::numeric_limits<float>::max ();
118 
119 // printf ("\nalign\n");
120 
121  do
122  {
123  // Update the correspondences
124  for ( int i = 0 ; i < num_source_points ; ++i )
125  {
126  // Transform the i-th source point based on the current transform matrix
127  aux::transform (guess_and_result, source_points[i], transformed_source_point);
128 
129  // Perform the closest point search
130  kdtree_.nearestKSearch (transformed_source_point, 1, target_index, sqr_dist_to_target);
131 
132  // Update the i-th correspondence
133  full_src_to_tgt[i].index_query = i;
134  full_src_to_tgt[i].index_match = target_index[0];
135  full_src_to_tgt[i].distance = sqr_dist_to_target[0];
136  }
137 
138  // Sort in ascending order according to the squared distance
139  std::sort (full_src_to_tgt.begin (), full_src_to_tgt.end (), TrimmedICP::compareCorrespondences);
140 
141  old_energy = energy;
142  energy = 0.0f;
143 
144  // Now, setup the trimmed correspondences used for the transform estimation
145  for ( int i = 0 ; i < num_trimmed_source_points ; ++i )
146  {
147  trimmed_src_to_tgt[i].index_query = full_src_to_tgt[i].index_query;
148  trimmed_src_to_tgt[i].index_match = full_src_to_tgt[i].index_match;
149  energy += full_src_to_tgt[i].distance;
150  }
151 
152  this->estimateRigidTransformation (source_points, *target_points_, trimmed_src_to_tgt, guess_and_result);
153 
154 // printf ("energy = %f, energy diff. = %f, ratio = %f\n", energy, old_energy - energy, energy/old_energy);
155  }
156  while ( energy/old_energy < new_to_old_energy_ratio_ ); // iterate if enough progress
157 
158 // printf ("\n");
159  }
160 
161  inline void
163  {
164  if ( ratio >= 1 )
165  new_to_old_energy_ratio_ = 0.99f;
166  else
167  new_to_old_energy_ratio_ = ratio;
168  }
169 
170  protected:
171  static inline bool
173  {
174  return a.distance < b.distance;
175  }
176 
177  protected:
181  };
182  } // namespace recognition
183 } // namespace pcl
KdTreeFLANN is a generic type of 3D spatial locator using kD-tree structures.
Definition: kdtree_flann.h:132
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
std::size_t size() const
Definition: point_cloud.h:443
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
~TrimmedICP() override=default
pcl::KdTreeFLANN< PointT > kdtree_
Definition: trimmed_icp.h:179
PointCloudConstPtr target_points_
Definition: trimmed_icp.h:178
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: trimmed_icp.h:66
void init(const PointCloudConstPtr &target)
Call this method before calling align().
Definition: trimmed_icp.h:83
static bool compareCorrespondences(const pcl::Correspondence &a, const pcl::Correspondence &b)
Definition: trimmed_icp.h:172
typename Eigen::Matrix< Scalar, 4, 4 > Matrix4
Definition: trimmed_icp.h:68
void setNewToOldEnergyRatio(float ratio)
Definition: trimmed_icp.h:162
void align(const PointCloud &source_points, int num_source_points_to_use, Matrix4 &guess_and_result) const
The method performs trimmed ICP, i.e., it rigidly registers the source to the target (passed to the i...
Definition: trimmed_icp.h:98
TransformationEstimationSVD implements SVD-based estimation of the transformation aligning the given ...
void transform(const T t[12], const T p[3], T out[3])
The first 9 elements of 't' are treated as a 3x3 matrix (row major order) and the last 3 as a transla...
Definition: auxiliary.h:304
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define PCL_EXPORTS
Definition: pcl_macros.h:323
Correspondence represents a match between two entities (e.g., points, descriptors,...
A point structure representing Euclidean xyz coordinates.