Point Cloud Library (PCL)  1.9.1-dev
transformation_estimation_svd.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, 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 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
41 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
42 
43 #include <pcl/common/eigen.h>
44 
45 ///////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointSource, typename PointTarget, typename Scalar> inline void
48  const pcl::PointCloud<PointSource> &cloud_src,
49  const pcl::PointCloud<PointTarget> &cloud_tgt,
50  Matrix4 &transformation_matrix) const
51 {
52  size_t nr_points = cloud_src.points.size ();
53  if (cloud_tgt.points.size () != nr_points)
54  {
55  PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ());
56  return;
57  }
58 
59  ConstCloudIterator<PointSource> source_it (cloud_src);
60  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
61  estimateRigidTransformation (source_it, target_it, transformation_matrix);
62 }
63 
64 ///////////////////////////////////////////////////////////////////////////////////////////
65 template <typename PointSource, typename PointTarget, typename Scalar> void
67  const pcl::PointCloud<PointSource> &cloud_src,
68  const std::vector<int> &indices_src,
69  const pcl::PointCloud<PointTarget> &cloud_tgt,
70  Matrix4 &transformation_matrix) const
71 {
72  if (indices_src.size () != cloud_tgt.points.size ())
73  {
74  PCL_ERROR ("[pcl::TransformationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), cloud_tgt.points.size ());
75  return;
76  }
77 
78  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
79  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
80  estimateRigidTransformation (source_it, target_it, transformation_matrix);
81 }
82 
83 ///////////////////////////////////////////////////////////////////////////////////////////
84 template <typename PointSource, typename PointTarget, typename Scalar> inline void
86  const pcl::PointCloud<PointSource> &cloud_src,
87  const std::vector<int> &indices_src,
88  const pcl::PointCloud<PointTarget> &cloud_tgt,
89  const std::vector<int> &indices_tgt,
90  Matrix4 &transformation_matrix) const
91 {
92  if (indices_src.size () != indices_tgt.size ())
93  {
94  PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ());
95  return;
96  }
97 
98  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
99  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
100  estimateRigidTransformation (source_it, target_it, transformation_matrix);
101 }
102 
103 ///////////////////////////////////////////////////////////////////////////////////////////
104 template <typename PointSource, typename PointTarget, typename Scalar> void
106  const pcl::PointCloud<PointSource> &cloud_src,
107  const pcl::PointCloud<PointTarget> &cloud_tgt,
108  const pcl::Correspondences &correspondences,
109  Matrix4 &transformation_matrix) const
110 {
111  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
112  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
113  estimateRigidTransformation (source_it, target_it, transformation_matrix);
114 }
115 
116 ///////////////////////////////////////////////////////////////////////////////////////////
117 template <typename PointSource, typename PointTarget, typename Scalar> inline void
121  Matrix4 &transformation_matrix) const
122 {
123  // Convert to Eigen format
124  const int npts = static_cast <int> (source_it.size ());
125 
126 
127 
128  if (use_umeyama_)
129  {
130  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src (3, npts);
131  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt (3, npts);
132 
133  for (int i = 0; i < npts; ++i)
134  {
135  cloud_src (0, i) = source_it->x;
136  cloud_src (1, i) = source_it->y;
137  cloud_src (2, i) = source_it->z;
138  ++source_it;
139 
140  cloud_tgt (0, i) = target_it->x;
141  cloud_tgt (1, i) = target_it->y;
142  cloud_tgt (2, i) = target_it->z;
143  ++target_it;
144  }
145 
146  // Call Umeyama directly from Eigen (PCL patched version until Eigen is released)
147  transformation_matrix = pcl::umeyama (cloud_src, cloud_tgt, false);
148  }
149  else
150  {
151  source_it.reset (); target_it.reset ();
152  // <cloud_src,cloud_src> is the source dataset
153  transformation_matrix.setIdentity ();
154 
155  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
156  // Estimate the centroids of source, target
157  compute3DCentroid (source_it, centroid_src);
158  compute3DCentroid (target_it, centroid_tgt);
159  source_it.reset (); target_it.reset ();
160 
161  // Subtract the centroids from source, target
162  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean;
163  demeanPointCloud (source_it, centroid_src, cloud_src_demean);
164  demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean);
165 
166  getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix);
167  }
168 }
169 
170 ///////////////////////////////////////////////////////////////////////////////////////////
171 template <typename PointSource, typename PointTarget, typename Scalar> void
173  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean,
174  const Eigen::Matrix<Scalar, 4, 1> &centroid_src,
175  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean,
176  const Eigen::Matrix<Scalar, 4, 1> &centroid_tgt,
177  Matrix4 &transformation_matrix) const
178 {
179  transformation_matrix.setIdentity ();
180 
181  // Assemble the correlation matrix H = source * target'
182  Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3);
183 
184  // Compute the Singular Value Decomposition
185  Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3> > svd (H, Eigen::ComputeFullU | Eigen::ComputeFullV);
186  Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU ();
187  Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV ();
188 
189  // Compute R = V * U'
190  if (u.determinant () * v.determinant () < 0)
191  {
192  for (int x = 0; x < 3; ++x)
193  v (x, 2) *= -1;
194  }
195 
196  Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose ();
197 
198  // Return the correct transformation
199  transformation_matrix.topLeftCorner (3, 3) = R;
200  const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3));
201  transformation_matrix.block (0, 3, 3, 1) = centroid_tgt.head (3) - Rc;
202 }
203 
204 //#define PCL_INSTANTIATE_TransformationEstimationSVD(T,U) template class PCL_EXPORTS pcl::registration::TransformationEstimationSVD<T,U>;
205 
206 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_ */
Iterator class for point clouds with or without given indices.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:423
void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const pcl::PointCloud< PointTarget > &cloud_tgt, Matrix4 &transformation_matrix) const override
Estimate a rigid rotation transformation between a source and a target point cloud using SVD...
Eigen::internal::umeyama_transform_matrix_type< Derived, OtherDerived >::type umeyama(const Eigen::MatrixBase< Derived > &src, const Eigen::MatrixBase< OtherDerived > &dst, bool with_scaling=false)
Returns the transformation between two point sets.
Definition: eigen.hpp:739
size_t size() const
Size of the range the iterator is going through.
void demeanPointCloud(ConstCloudIterator< PointT > &cloud_iterator, const Eigen::Matrix< Scalar, 4, 1 > &centroid, pcl::PointCloud< PointT > &cloud_out, int npts=0)
Subtract a centroid from a point cloud and return the de-meaned representation.
Definition: centroid.hpp:625
virtual void getTransformationFromCorrelation(const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &cloud_src_demean, const Eigen::Matrix< Scalar, 4, 1 > &centroid_src, const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &cloud_tgt_demean, const Eigen::Matrix< Scalar, 4, 1 > &centroid_tgt, Matrix4 &transformation_matrix) const
Obtain a 4x4 rigid transformation matrix from a correlation matrix H = src * tgt&#39;.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
Definition: centroid.hpp:50