Point Cloud Library (PCL)  1.14.1-dev
transformation_estimation_svd_scale.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_SCALE_HPP_
41 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_SCALE_HPP_
42 
43 namespace pcl {
44 
45 namespace registration {
46 
47 template <typename PointSource, typename PointTarget, typename Scalar>
48 void
51  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_src_demean,
52  const Eigen::Matrix<Scalar, 4, 1>& centroid_src,
53  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_tgt_demean,
54  const Eigen::Matrix<Scalar, 4, 1>& centroid_tgt,
55  Matrix4& transformation_matrix) const
56 {
57  transformation_matrix.setIdentity();
58 
59  // Assemble the correlation matrix H = source * target'
60  Eigen::Matrix<Scalar, 3, 3> H =
61  (cloud_src_demean * cloud_tgt_demean.transpose()).template topLeftCorner<3, 3>();
62 
63  // Compute the Singular Value Decomposition
64  Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3>> svd(
65  H, Eigen::ComputeFullU | Eigen::ComputeFullV);
66  Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU();
67  Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV();
68 
69  // Compute R = V * U'
70  if (u.determinant() * v.determinant() < 0) {
71  for (int x = 0; x < 3; ++x)
72  v(x, 2) *= -1;
73  }
74 
75  Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose();
76 
77  // rotated cloud
78  Eigen::Matrix<Scalar, 4, 4> R4;
79  R4.template topLeftCorner<3, 3>() = R;
80  R4(0, 3) = 0;
81  R4(1, 3) = 0;
82  R4(2, 3) = 0;
83  R4(3, 3) = 1;
84 
85  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> src_ = R4 * cloud_src_demean;
86 
87  double sum_ss = 0.0f, sum_tt = 0.0f;
88  for (unsigned corrIdx = 0; corrIdx < cloud_src_demean.cols(); ++corrIdx) {
89  sum_ss += cloud_src_demean(0, corrIdx) * cloud_src_demean(0, corrIdx);
90  sum_ss += cloud_src_demean(1, corrIdx) * cloud_src_demean(1, corrIdx);
91  sum_ss += cloud_src_demean(2, corrIdx) * cloud_src_demean(2, corrIdx);
92 
93  sum_tt += cloud_tgt_demean(0, corrIdx) * src_(0, corrIdx);
94  sum_tt += cloud_tgt_demean(1, corrIdx) * src_(1, corrIdx);
95  sum_tt += cloud_tgt_demean(2, corrIdx) * src_(2, corrIdx);
96  }
97 
98  float scale = sum_tt / sum_ss;
99  transformation_matrix.template topLeftCorner<3, 3>() = scale * R;
100  const Eigen::Matrix<Scalar, 3, 1> Rc(scale * R * centroid_src.template head<3>());
101  transformation_matrix.template block<3, 1>(0, 3) =
102  centroid_tgt.template head<3>() - Rc;
103 }
104 
105 } // namespace registration
106 } // namespace pcl
107 
108 //#define PCL_INSTANTIATE_TransformationEstimationSVD(T,U) template class PCL_EXPORTS
109 // pcl::registration::TransformationEstimationSVD<T,U>;
110 
111 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_SCALE_HPP_ */
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.
typename TransformationEstimationSVD< PointSource, PointTarget, Scalar >::Matrix4 Matrix4