$darkmode
Eigen  5.0.1-dev
Umeyama.h
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 // These helpers are required since it allows to use mixed types as parameters
25 // for the Umeyama. The problem with mixed parameters is that the return type
26 // cannot trivially be deduced when float and double types are mixed.
27 namespace internal {
28 
29 // Compile time return type deduction for different MatrixBase types.
30 // Different means here different alignment and parameters but the same underlying
31 // real scalar type.
32 template <typename MatrixType, typename OtherMatrixType>
33 struct umeyama_transform_matrix_type {
34  enum {
35  MinRowsAtCompileTime =
36  internal::min_size_prefer_dynamic(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
37 
38  // When possible we want to choose some small fixed size value since the result
39  // is likely to fit on the stack. So here, min_size_prefer_dynamic is not what we want.
40  HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime) + 1
41  };
42 
43  typedef Matrix<typename traits<MatrixType>::Scalar, HomogeneousDimension, HomogeneousDimension,
44  AutoAlign | (traits<MatrixType>::Flags & RowMajorBit ? RowMajor : ColMajor), HomogeneousDimension,
45  HomogeneousDimension>
46  type;
47 };
48 
49 } // namespace internal
50 
89 template <typename Derived, typename OtherDerived>
91  const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true) {
92  typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
93  typedef typename internal::traits<TransformationMatrixType>::Scalar Scalar;
94  typedef typename NumTraits<Scalar>::Real RealScalar;
95 
96  EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
97  EIGEN_STATIC_ASSERT(
98  (internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
99  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
100 
101  enum { Dimension = internal::min_size_prefer_dynamic(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
102 
103  typedef Matrix<Scalar, Dimension, 1> VectorType;
104  typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
105  typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
106 
107  const Index m = src.rows(); // dimension
108  const Index n = src.cols(); // number of measurements
109 
110  // required for demeaning ...
111  const RealScalar one_over_n = RealScalar(1) / static_cast<RealScalar>(n);
112 
113  // computation of mean
114  const VectorType src_mean = src.rowwise().sum() * one_over_n;
115  const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
116 
117  // demeaning of src and dst points
118  const RowMajorMatrixType src_demean = src.colwise() - src_mean;
119  const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
120 
121  // Eq. (38)
122  const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
123 
125 
126  // Initialize the resulting transformation with an identity matrix...
127  TransformationMatrixType Rt = TransformationMatrixType::Identity(m + 1, m + 1);
128 
129  // Eq. (39)
130  VectorType S = VectorType::Ones(m);
131 
132  if (svd.matrixU().determinant() * svd.matrixV().determinant() < 0) {
133  Index tmp = m - 1;
134  S(tmp) = -1;
135  }
136 
137  // Eq. (40) and (43)
138  Rt.block(0, 0, m, m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
139 
140  if (with_scaling) {
141  // Eq. (36)-(37)
142  const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
143 
144  // Eq. (42)
145  const Scalar c = Scalar(1) / src_var * svd.singularValues().dot(S);
146 
147  // Eq. (41)
148  Rt.col(m).head(m) = dst_mean;
149  Rt.col(m).head(m).noalias() -= c * Rt.topLeftCorner(m, m) * src_mean;
150  Rt.block(0, 0, m, m) *= c;
151  } else {
152  Rt.col(m).head(m) = dst_mean;
153  Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m, m) * src_mean;
154  }
155 
156  return Rt;
157 }
158 
159 } // end namespace Eigen
160 
161 #endif // EIGEN_UMEYAMA_H
Definition: Constants.h:318
const MatrixUType & matrixU() const
Definition: SVDBase.h:173
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:232
const unsigned int RowMajorBit
Definition: Constants.h:70
Definition: Constants.h:322
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
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:90
ConstColwiseReturnType colwise() const
Definition: DenseBase.h:518
ConstRowwiseReturnType rowwise() const
Definition: DenseBase.h:508
Definition: Constants.h:320
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition: ForwardDeclarations.h:437
const SingularValuesType & singularValues() const
Definition: SVDBase.h:200
const MatrixVType & matrixV() const
Definition: SVDBase.h:189
const int Dynamic
Definition: Constants.h:25
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:186
constexpr Index rows() const noexcept
Definition: EigenBase.h:59
constexpr Index cols() const noexcept
Definition: EigenBase.h:61
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52