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 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_UMEYAMA_H
11 #define EIGEN_UMEYAMA_H
12 
13 // This file requires the user to include
14 // * Eigen/Core
15 // * Eigen/LU
16 // * Eigen/SVD
17 // * Eigen/Array
18 
19 namespace Eigen {
20 
21 #ifndef EIGEN_PARSED_BY_DOXYGEN
22 
23 // These helpers are required since it allows to use mixed types as parameters
24 // for the Umeyama. The problem with mixed parameters is that the return type
25 // cannot trivially be deduced when float and double types are mixed.
26 namespace internal {
27 
28 // Compile time return type deduction for different MatrixBase types.
29 // Different means here different alignment and parameters but the same underlying
30 // real scalar type.
31 template<typename MatrixType, typename OtherMatrixType>
33 {
34  enum {
35  MinRowsAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
36 
37  // When possible we want to choose some small fixed size value since the result
38  // is likely to fit on the stack. So here, EIGEN_SIZE_MIN_PREFER_DYNAMIC is not what we want.
40  };
41 
46  HomogeneousDimension,
47  HomogeneousDimension
48  > type;
49 };
50 
51 }
52 
53 #endif
54 
93 template <typename Derived, typename OtherDerived>
95 umeyama(const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true)
96 {
97  typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
99  typedef typename NumTraits<Scalar>::Real RealScalar;
100  typedef typename Derived::Index Index;
101 
102  EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
104  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
105 
106  enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
107 
108  typedef Matrix<Scalar, Dimension, 1> VectorType;
109  typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
110  typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
111 
112  const Index m = src.rows(); // dimension
113  const Index n = src.cols(); // number of measurements
114 
115  // required for demeaning ...
116  const RealScalar one_over_n = 1 / static_cast<RealScalar>(n);
117 
118  // computation of mean
119  const VectorType src_mean = src.rowwise().sum() * one_over_n;
120  const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
121 
122  // demeaning of src and dst points
123  const RowMajorMatrixType src_demean = src.colwise() - src_mean;
124  const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
125 
126  // Eq. (36)-(37)
127  const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
128 
129  // Eq. (38)
130  const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
131 
133 
134  // Initialize the resulting transformation with an identity matrix...
135  TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
136 
137  // Eq. (39)
138  VectorType S = VectorType::Ones(m);
139  if (sigma.determinant()<0) S(m-1) = -1;
140 
141  // Eq. (40) and (43)
142  const VectorType& d = svd.singularValues();
143  Index rank = 0; for (Index i=0; i<m; ++i) if (!internal::isMuchSmallerThan(d.coeff(i),d.coeff(0))) ++rank;
144  if (rank == m-1) {
145  if ( svd.matrixU().determinant() * svd.matrixV().determinant() > 0 ) {
146  Rt.block(0,0,m,m).noalias() = svd.matrixU()*svd.matrixV().transpose();
147  } else {
148  const Scalar s = S(m-1); S(m-1) = -1;
149  Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
150  S(m-1) = s;
151  }
152  } else {
153  Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
154  }
155 
156  if (with_scaling)
157  {
158  // Eq. (42)
159  const Scalar c = 1/src_var * svd.singularValues().dot(S);
160 
161  // Eq. (41)
162  Rt.col(m).head(m) = dst_mean;
163  Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
164  Rt.block(0,0,m,m) *= c;
165  }
166  else
167  {
168  Rt.col(m).head(m) = dst_mean;
169  Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m,m)*src_mean;
170  }
171 
172  return Rt;
173 }
174 
175 } // end namespace Eigen
176 
177 #endif // EIGEN_UMEYAMA_H
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:111
const SingularValuesType & singularValues() const
Definition: JacobiSVD.h:630
bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, typename NumTraits< Scalar >::Real precision=NumTraits< Scalar >::dummy_precision())
const unsigned int RowMajorBit
ConstColwiseReturnType colwise() const
Definition: VectorwiseOp.h:598
internal::umeyama_transform_matrix_type< Derived, OtherDerived >::type umeyama(const MatrixBase< Derived > &src, const MatrixBase< OtherDerived > &dst, bool with_scaling=true)
Returns the transformation between two point sets.
Definition: Umeyama.h:95
#define EIGEN_SIZE_MIN_PREFER_DYNAMIC(a, b)
Two-sided Jacobi SVD decomposition of a rectangular matrix.
const MatrixUType & matrixU() const
Definition: JacobiSVD.h:602
ConstRowwiseReturnType rowwise() const
Definition: VectorwiseOp.h:623
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const MatrixVType & matrixV() const
Definition: JacobiSVD.h:618


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:15