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 // IWYU pragma: private
20 #include "./InternalHeaderCheck.h"
21 
22 namespace Eigen {
23 
24 #ifndef EIGEN_PARSED_BY_DOXYGEN
25 
26 // These helpers are required since it allows to use mixed types as parameters
27 // for the Umeyama. The problem with mixed parameters is that the return type
28 // cannot trivially be deduced when float and double types are mixed.
29 namespace internal {
30 
31 // Compile time return type deduction for different MatrixBase types.
32 // Different means here different alignment and parameters but the same underlying
33 // real scalar type.
34 template <typename MatrixType, typename OtherMatrixType>
36  enum {
38  internal::min_size_prefer_dynamic(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
39 
40  // When possible we want to choose some small fixed size value since the result
41  // is likely to fit on the stack. So here, min_size_prefer_dynamic is not what we want.
43  };
44 
49 };
50 
51 } // namespace internal
52 
53 #endif
54 
93 template <typename Derived, typename OtherDerived>
95  const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true) {
96  typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
98  typedef typename NumTraits<Scalar>::Real RealScalar;
99 
100  EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
103  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
104 
105  enum { Dimension = internal::min_size_prefer_dynamic(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
106 
109  typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
110 
111  const Index m = src.rows(); // dimension
112  const Index n = src.cols(); // number of measurements
113 
114  // required for demeaning ...
115  const RealScalar one_over_n = RealScalar(1) / static_cast<RealScalar>(n);
116 
117  // computation of mean
118  const VectorType src_mean = src.rowwise().sum() * one_over_n;
119  const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
120 
121  // demeaning of src and dst points
122  const RowMajorMatrixType src_demean = src.colwise() - src_mean;
123  const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
124 
125  // Eq. (38)
126  const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
127 
129 
130  // Initialize the resulting transformation with an identity matrix...
131  TransformationMatrixType Rt = TransformationMatrixType::Identity(m + 1, m + 1);
132 
133  // Eq. (39)
134  VectorType S = VectorType::Ones(m);
135 
136  if (svd.matrixU().determinant() * svd.matrixV().determinant() < 0) {
137  Index tmp = m - 1;
138  S(tmp) = -1;
139  }
140 
141  // Eq. (40) and (43)
142  Rt.block(0, 0, m, m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
143 
144  if (with_scaling) {
145  // Eq. (36)-(37)
146  const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
147 
148  // Eq. (42)
149  const Scalar c = Scalar(1) / src_var * svd.singularValues().dot(S);
150 
151  // Eq. (41)
152  Rt.col(m).head(m) = dst_mean;
153  Rt.col(m).head(m).noalias() -= c * Rt.topLeftCorner(m, m) * src_mean;
154  Rt.block(0, 0, m, m) *= c;
155  } else {
156  Rt.col(m).head(m) = dst_mean;
157  Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m, m) * src_mean;
158  }
159 
160  return Rt;
161 }
162 
163 } // end namespace Eigen
164 
165 #endif // EIGEN_UMEYAMA_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf, ComputeThinU|ComputeThinV > svd(m)
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
EIGEN_DEVICE_FUNC ConstColwiseReturnType colwise() const
Definition: DenseBase.h:513
EIGEN_DEVICE_FUNC ConstRowwiseReturnType rowwise() const
Definition: DenseBase.h:503
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: JacobiSVD.h:500
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
EIGEN_DEVICE_FUNC const SumReturnType sum() const
Definition: VectorwiseOp.h:458
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:94
@ ColMajor
Definition: Constants.h:318
@ RowMajor
Definition: Constants.h:320
@ AutoAlign
Definition: Constants.h:322
const unsigned int RowMajorBit
Definition: Constants.h:70
return int(ret)+1
int * m
Definition: level2_cplx_impl.h:294
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
constexpr int min_size_prefer_dynamic(A a, B b)
Definition: Meta.h:668
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
int c
Definition: calibrate.py:100
int sigma
Definition: calibrate.py:179
Definition: Eigen_Colamd.h:49
@ S
Definition: quadtree.h:62
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: Meta.h:205
Definition: ForwardDeclarations.h:21
@ MinRowsAtCompileTime
Definition: Umeyama.h:37
@ HomogeneousDimension
Definition: Umeyama.h:42
Definition: fft_test_shared.h:66