Umeyama.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_UMEYAMA_H
26 #define EIGEN_UMEYAMA_H
27 
28 // This file requires the user to include
29 // * Eigen/Core
30 // * Eigen/LU
31 // * Eigen/SVD
32 // * Eigen/Array
33 
34 namespace Eigen {
35 
36 #ifndef EIGEN_PARSED_BY_DOXYGEN
37 
38 // These helpers are required since it allows to use mixed types as parameters
39 // for the Umeyama. The problem with mixed parameters is that the return type
40 // cannot trivially be deduced when float and double types are mixed.
41 namespace internal {
42 
43 // Compile time return type deduction for different MatrixBase types.
44 // Different means here different alignment and parameters but the same underlying
45 // real scalar type.
46 template<typename MatrixType, typename OtherMatrixType>
47 struct umeyama_transform_matrix_type
48 {
49  enum {
50  MinRowsAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
51 
52  // When possible we want to choose some small fixed size value since the result
53  // is likely to fit on the stack. So here, EIGEN_SIZE_MIN_PREFER_DYNAMIC is not what we want.
54  HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime)+1
55  };
56 
57  typedef Matrix<typename traits<MatrixType>::Scalar,
58  HomogeneousDimension,
59  HomogeneousDimension,
61  HomogeneousDimension,
62  HomogeneousDimension
63  > type;
64 };
65 
66 }
67 
68 #endif
69 
108 template <typename Derived, typename OtherDerived>
109 typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
110 umeyama(const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true)
111 {
112  typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
113  typedef typename internal::traits<TransformationMatrixType>::Scalar Scalar;
114  typedef typename NumTraits<Scalar>::Real RealScalar;
115  typedef typename Derived::Index Index;
116 
117  EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
118  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
119  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
120 
121  enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
122 
123  typedef Matrix<Scalar, Dimension, 1> VectorType;
124  typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
125  typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
126 
127  const Index m = src.rows(); // dimension
128  const Index n = src.cols(); // number of measurements
129 
130  // required for demeaning ...
131  const RealScalar one_over_n = 1 / static_cast<RealScalar>(n);
132 
133  // computation of mean
134  const VectorType src_mean = src.rowwise().sum() * one_over_n;
135  const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
136 
137  // demeaning of src and dst points
138  const RowMajorMatrixType src_demean = src.colwise() - src_mean;
139  const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
140 
141  // Eq. (36)-(37)
142  const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
143 
144  // Eq. (38)
145  const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
146 
148 
149  // Initialize the resulting transformation with an identity matrix...
150  TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
151 
152  // Eq. (39)
153  VectorType S = VectorType::Ones(m);
154  if (sigma.determinant()<0) S(m-1) = -1;
155 
156  // Eq. (40) and (43)
157  const VectorType& d = svd.singularValues();
158  Index rank = 0; for (Index i=0; i<m; ++i) if (!internal::isMuchSmallerThan(d.coeff(i),d.coeff(0))) ++rank;
159  if (rank == m-1) {
160  if ( svd.matrixU().determinant() * svd.matrixV().determinant() > 0 ) {
161  Rt.block(0,0,m,m).noalias() = svd.matrixU()*svd.matrixV().transpose();
162  } else {
163  const Scalar s = S(m-1); S(m-1) = -1;
164  Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
165  S(m-1) = s;
166  }
167  } else {
168  Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
169  }
170 
171  // Eq. (42)
172  const Scalar c = 1/src_var * svd.singularValues().dot(S);
173 
174  // Eq. (41)
175  // Note that we first assign dst_mean to the destination so that there no need
176  // for a temporary.
177  Rt.col(m).head(m) = dst_mean;
178  Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
179 
180  if (with_scaling) Rt.block(0,0,m,m) *= c;
181 
182  return Rt;
183 }
184 
185 } // end namespace Eigen
186 
187 #endif // EIGEN_UMEYAMA_H