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