Eigen  5.0.1
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_UMEYAMA_H
12#define EIGEN_UMEYAMA_H
13
14// This file requires the user to include
15// * Eigen/Core
16// * Eigen/LU
17// * Eigen/SVD
18// * Eigen/Array
19
20// IWYU pragma: private
21#include "./InternalHeaderCheck.h"
22
23namespace Eigen {
24
25// These helpers are required since they allow the use of mixed types as parameters
26// for the Umeyama. The problem with mixed parameters is that the return type
27// cannot trivially be deduced when float and double types are mixed.
28namespace internal {
29
30// Compile time return type deduction for different MatrixBase types.
31// Different means here different alignment and parameters but the same underlying
32// real scalar type.
33template <typename MatrixType, typename OtherMatrixType>
34struct umeyama_transform_matrix_type {
35 enum {
36 MinRowsAtCompileTime =
37 internal::min_size_prefer_dynamic(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
38
39 // When possible we want to choose some small fixed size value since the result
40 // is likely to fit on the stack. So here, min_size_prefer_dynamic is not what we want.
41 HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime) + 1
42 };
43
44 using type = Matrix<typename traits<MatrixType>::Scalar, HomogeneousDimension, HomogeneousDimension,
45 AutoAlign | (traits<MatrixType>::Flags & RowMajorBit ? RowMajor : ColMajor), HomogeneousDimension,
46 HomogeneousDimension>;
47};
48
49} // namespace internal
50
89template <typename Derived, typename OtherDerived>
90typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type umeyama(
91 const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true) {
92 using TransformationMatrixType = typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type;
93 using Scalar = typename internal::traits<TransformationMatrixType>::Scalar;
94 using RealScalar = typename NumTraits<Scalar>::Real;
95
96 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
97 EIGEN_STATIC_ASSERT(
98 (std::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 using VectorType = Matrix<Scalar, Dimension, 1>;
104 using MatrixType = Matrix<Scalar, Dimension, Dimension>;
105 using RowMajorMatrixType = typename internal::plain_matrix_type_row_major<Derived>::type;
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 if (src_var <= Scalar(0)) {
145 // Degenerate: source points have zero variance (all nearly identical).
146 // Scaling is undefined; return the best-fit pure translation.
147 Rt.col(m).head(m) = dst_mean - src_mean;
148 return Rt;
149 }
150
151 // Eq. (42)
152 const Scalar c = Scalar(1) / src_var * svd.singularValues().dot(S);
153
154 // Eq. (41)
155 Rt.col(m).head(m) = dst_mean;
156 Rt.col(m).head(m).noalias() -= c * Rt.topLeftCorner(m, m) * src_mean;
157 Rt.block(0, 0, m, m) *= c;
158 } else {
159 Rt.col(m).head(m) = dst_mean;
160 Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m, m) * src_mean;
161 }
162
163 return Rt;
164}
165
166} // end namespace Eigen
167
168#endif // EIGEN_UMEYAMA_H
ConstColwiseReturnType colwise() const
Definition DenseBase.h:482
ConstRowwiseReturnType rowwise() const
Definition DenseBase.h:472
Two-sided Jacobi SVD decomposition of a rectangular matrix.
Definition JacobiSVD.h:613
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
const SingularValuesType & singularValues() const
Definition SVDBase.h:203
const MatrixUType & matrixU() const
Definition SVDBase.h:176
const MatrixVType & matrixV() const
Definition SVDBase.h:192
const SumReturnType sum() const
Definition VectorwiseOp.h:511
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
@ ColMajor
Definition Constants.h:319
@ RowMajor
Definition Constants.h:321
@ AutoAlign
Definition Constants.h:323
constexpr unsigned int RowMajorBit
Definition Constants.h:71