Point Cloud Library (PCL)  1.14.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:
71  TrimmedICP () = default;
72 
73  ~TrimmedICP () override = default;
74 
75  /** \brief Call this method before calling align().
76  *
77  * \param[in] target is target point cloud. The method builds a kd-tree based on 'target' for performing fast closest point search.
78  * The source point cloud will be registered to 'target' (see align() method).
79  * */
80  inline void
81  init (const PointCloudConstPtr& target)
82  {
83  target_points_ = target;
84  kdtree_.setInputCloud (target);
85  }
86 
87  /** \brief The method performs trimmed ICP, i.e., it rigidly registers the source to the target (passed to the init() method).
88  *
89  * \param[in] source_points is the point cloud to be registered to the target.
90  * \param[in] num_source_points_to_use gives the number of closest source points taken into account for registration. By closest
91  * source points we mean the source points closest to the target. These points are computed anew at each iteration.
92  * \param[in,out] guess_and_result is the estimated rigid transform. IMPORTANT: this matrix is also taken as the initial guess
93  * for the alignment. If there is no guess, set the matrix to identity!
94  * */
95  inline void
96  align (const PointCloud& source_points, int num_source_points_to_use, Matrix4& guess_and_result) const
97  {
98  int num_trimmed_source_points = num_source_points_to_use, num_source_points = static_cast<int> (source_points.size ());
99 
100  if ( num_trimmed_source_points >= num_source_points )
101  {
102  printf ("WARNING in 'TrimmedICP::%s()': the user-defined number of source points of interest is greater or equal to "
103  "the total number of source points. Trimmed ICP will work correctly but won't be very efficient. Either set "
104  "the number of source points to use to a lower value or use standard ICP.\n", __func__);
105  num_trimmed_source_points = num_source_points;
106  }
107 
108  // These are vectors containing source to target correspondences
109  pcl::Correspondences full_src_to_tgt (num_source_points), trimmed_src_to_tgt (num_trimmed_source_points);
110 
111  // Some variables for the closest point search
112  pcl::PointXYZ transformed_source_point;
113  pcl::Indices target_index (1);
114  std::vector<float> sqr_dist_to_target (1);
115  float old_energy, energy = std::numeric_limits<float>::max ();
116 
117 // printf ("\nalign\n");
118 
119  do
120  {
121  // Update the correspondences
122  for ( int i = 0 ; i < num_source_points ; ++i )
123  {
124  // Transform the i-th source point based on the current transform matrix
125  aux::transform (guess_and_result, source_points[i], transformed_source_point);
126 
127  // Perform the closest point search
128  kdtree_.nearestKSearch (transformed_source_point, 1, target_index, sqr_dist_to_target);
129 
130  // Update the i-th correspondence
131  full_src_to_tgt[i].index_query = i;
132  full_src_to_tgt[i].index_match = target_index[0];
133  full_src_to_tgt[i].distance = sqr_dist_to_target[0];
134  }
135 
136  // Sort in ascending order according to the squared distance
137  std::sort (full_src_to_tgt.begin (), full_src_to_tgt.end (), TrimmedICP::compareCorrespondences);
138 
139  old_energy = energy;
140  energy = 0.0f;
141 
142  // Now, setup the trimmed correspondences used for the transform estimation
143  for ( int i = 0 ; i < num_trimmed_source_points ; ++i )
144  {
145  trimmed_src_to_tgt[i].index_query = full_src_to_tgt[i].index_query;
146  trimmed_src_to_tgt[i].index_match = full_src_to_tgt[i].index_match;
147  energy += full_src_to_tgt[i].distance;
148  }
149 
150  this->estimateRigidTransformation (source_points, *target_points_, trimmed_src_to_tgt, guess_and_result);
151 
152 // printf ("energy = %f, energy diff. = %f, ratio = %f\n", energy, old_energy - energy, energy/old_energy);
153  }
154  while ( energy/old_energy < new_to_old_energy_ratio_ ); // iterate if enough progress
155 
156 // printf ("\n");
157  }
158 
159  inline void
161  {
162  if ( ratio >= 1 )
163  new_to_old_energy_ratio_ = 0.99f;
164  else
165  new_to_old_energy_ratio_ = ratio;
166  }
167 
168  protected:
169  static inline bool
171  {
172  return a.distance < b.distance;
173  }
174 
175  protected:
178  float new_to_old_energy_ratio_{0.99f};
179  };
180  } // namespace recognition
181 } // 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:177
PointCloudConstPtr target_points_
Definition: trimmed_icp.h:176
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: trimmed_icp.h:66
void init(const PointCloudConstPtr &target)
Call this method before calling align().
Definition: trimmed_icp.h:81
static bool compareCorrespondences(const pcl::Correspondence &a, const pcl::Correspondence &b)
Definition: trimmed_icp.h:170
typename Eigen::Matrix< Scalar, 4, 4 > Matrix4
Definition: trimmed_icp.h:68
void setNewToOldEnergyRatio(float ratio)
Definition: trimmed_icp.h:160
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:96
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:325
Correspondence represents a match between two entities (e.g., points, descriptors,...
A point structure representing Euclidean xyz coordinates.