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