Medial Code Documentation
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
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
19namespace 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.
26namespace 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.
31template<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.
39 HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime)+1
40 };
41
43 HomogeneousDimension,
44 HomogeneousDimension,
46 HomogeneousDimension,
47 HomogeneousDimension
48 > type;
49};
50
51}
52
53#endif
54
93template <typename Derived, typename OtherDerived>
96{
99 typedef typename NumTraits<Scalar>::Real RealScalar;
100
101 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
102 EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
103 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
104
105 enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
106
107 typedef Matrix<Scalar, Dimension, 1> VectorType;
108 typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
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. (36)-(37)
126 const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
127
128 // Eq. (38)
129 const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
130
132
133 // Initialize the resulting transformation with an identity matrix...
134 TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
135
136 // Eq. (39)
137 VectorType S = VectorType::Ones(m);
138 if (sigma.determinant()<Scalar(0)) S(m-1) = Scalar(-1);
139
140 // Eq. (40) and (43)
141 const VectorType& d = svd.singularValues();
142 Index rank = 0; for (Index i=0; i<m; ++i) if (!internal::isMuchSmallerThan(d.coeff(i),d.coeff(0))) ++rank;
143 if (rank == m-1) {
144 if ( svd.matrixU().determinant() * svd.matrixV().determinant() > Scalar(0) ) {
145 Rt.block(0,0,m,m).noalias() = svd.matrixU()*svd.matrixV().transpose();
146 } else {
147 const Scalar s = S(m-1); S(m-1) = Scalar(-1);
148 Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
149 S(m-1) = s;
150 }
151 } else {
152 Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
153 }
154
155 if (with_scaling)
156 {
157 // Eq. (42)
158 const Scalar c = Scalar(1)/src_var * svd.singularValues().dot(S);
159
160 // Eq. (41)
161 Rt.col(m).head(m) = dst_mean;
162 Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
163 Rt.block(0,0,m,m) *= c;
164 }
165 else
166 {
167 Rt.col(m).head(m) = dst_mean;
168 Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m,m)*src_mean;
169 }
170
171 return Rt;
172}
173
174} // end namespace Eigen
175
176#endif // EIGEN_UMEYAMA_H
Pseudo expression representing a solving operation.
Definition Solve.h:63
internal::umeyama_transform_matrix_type< Derived, OtherDerived >::type umeyama(const MatrixBase< Derived > &src, const MatrixBase< OtherDerived > &dst, bool with_scaling=true)
\geometry_module
Definition Umeyama.h:95
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition Constants.h:320
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition Constants.h:322
@ AutoAlign
Align the matrix itself if it is vectorizable fixed-size.
Definition Constants.h:324
@ ComputeFullV
Used in JacobiSVD to indicate that the square matrix V is to be computed.
Definition Constants.h:387
@ ComputeFullU
Used in JacobiSVD to indicate that the square matrix U is to be computed.
Definition Constants.h:383
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Holds information about the various numeric (i.e.
Definition NumTraits.h:108
Definition Meta.h:39
Definition ForwardDeclarations.h:17