Eigne Matrix Class Library

Dependents:   MPC_current_control HydraulicControlBoard_SW AHRS Test_ekf ... more

Committer:
jsoh91
Date:
Tue Sep 24 00:18:23 2019 +0000
Revision:
1:3b8049da21b8
Parent:
0:13a5d365ba16
ignore and revise some of error parts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
ykuroda 0:13a5d365ba16 5 //
ykuroda 0:13a5d365ba16 6 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 7 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 9
ykuroda 0:13a5d365ba16 10 #ifndef EIGEN_UMEYAMA_H
ykuroda 0:13a5d365ba16 11 #define EIGEN_UMEYAMA_H
ykuroda 0:13a5d365ba16 12
ykuroda 0:13a5d365ba16 13 // This file requires the user to include
ykuroda 0:13a5d365ba16 14 // * Eigen/Core
ykuroda 0:13a5d365ba16 15 // * Eigen/LU
ykuroda 0:13a5d365ba16 16 // * Eigen/SVD
ykuroda 0:13a5d365ba16 17 // * Eigen/Array
ykuroda 0:13a5d365ba16 18
ykuroda 0:13a5d365ba16 19 namespace Eigen {
ykuroda 0:13a5d365ba16 20
ykuroda 0:13a5d365ba16 21 #ifndef EIGEN_PARSED_BY_DOXYGEN
ykuroda 0:13a5d365ba16 22
ykuroda 0:13a5d365ba16 23 // These helpers are required since it allows to use mixed types as parameters
ykuroda 0:13a5d365ba16 24 // for the Umeyama. The problem with mixed parameters is that the return type
ykuroda 0:13a5d365ba16 25 // cannot trivially be deduced when float and double types are mixed.
ykuroda 0:13a5d365ba16 26 namespace internal {
ykuroda 0:13a5d365ba16 27
ykuroda 0:13a5d365ba16 28 // Compile time return type deduction for different MatrixBase types.
ykuroda 0:13a5d365ba16 29 // Different means here different alignment and parameters but the same underlying
ykuroda 0:13a5d365ba16 30 // real scalar type.
ykuroda 0:13a5d365ba16 31 template<typename MatrixType, typename OtherMatrixType>
ykuroda 0:13a5d365ba16 32 struct umeyama_transform_matrix_type
ykuroda 0:13a5d365ba16 33 {
ykuroda 0:13a5d365ba16 34 enum {
ykuroda 0:13a5d365ba16 35 MinRowsAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
ykuroda 0:13a5d365ba16 36
ykuroda 0:13a5d365ba16 37 // When possible we want to choose some small fixed size value since the result
ykuroda 0:13a5d365ba16 38 // is likely to fit on the stack. So here, EIGEN_SIZE_MIN_PREFER_DYNAMIC is not what we want.
ykuroda 0:13a5d365ba16 39 HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime)+1
ykuroda 0:13a5d365ba16 40 };
ykuroda 0:13a5d365ba16 41
ykuroda 0:13a5d365ba16 42 typedef Matrix<typename traits<MatrixType>::Scalar,
ykuroda 0:13a5d365ba16 43 HomogeneousDimension,
ykuroda 0:13a5d365ba16 44 HomogeneousDimension,
ykuroda 0:13a5d365ba16 45 AutoAlign | (traits<MatrixType>::Flags & RowMajorBit ? RowMajor : ColMajor),
ykuroda 0:13a5d365ba16 46 HomogeneousDimension,
ykuroda 0:13a5d365ba16 47 HomogeneousDimension
ykuroda 0:13a5d365ba16 48 > type;
ykuroda 0:13a5d365ba16 49 };
ykuroda 0:13a5d365ba16 50
ykuroda 0:13a5d365ba16 51 }
ykuroda 0:13a5d365ba16 52
ykuroda 0:13a5d365ba16 53 #endif
ykuroda 0:13a5d365ba16 54
ykuroda 0:13a5d365ba16 55 /**
ykuroda 0:13a5d365ba16 56 * \geometry_module \ingroup Geometry_Module
ykuroda 0:13a5d365ba16 57 *
ykuroda 0:13a5d365ba16 58 * \brief Returns the transformation between two point sets.
ykuroda 0:13a5d365ba16 59 *
ykuroda 0:13a5d365ba16 60 * The algorithm is based on:
ykuroda 0:13a5d365ba16 61 * "Least-squares estimation of transformation parameters between two point patterns",
ykuroda 0:13a5d365ba16 62 * Shinji Umeyama, PAMI 1991, DOI: 10.1109/34.88573
ykuroda 0:13a5d365ba16 63 *
ykuroda 0:13a5d365ba16 64 * It estimates parameters \f$ c, \mathbf{R}, \f$ and \f$ \mathbf{t} \f$ such that
ykuroda 0:13a5d365ba16 65 * \f{align*}
ykuroda 0:13a5d365ba16 66 * \frac{1}{n} \sum_{i=1}^n \vert\vert y_i - (c\mathbf{R}x_i + \mathbf{t}) \vert\vert_2^2
ykuroda 0:13a5d365ba16 67 * \f}
ykuroda 0:13a5d365ba16 68 * is minimized.
ykuroda 0:13a5d365ba16 69 *
ykuroda 0:13a5d365ba16 70 * The algorithm is based on the analysis of the covariance matrix
ykuroda 0:13a5d365ba16 71 * \f$ \Sigma_{\mathbf{x}\mathbf{y}} \in \mathbb{R}^{d \times d} \f$
ykuroda 0:13a5d365ba16 72 * of the input point sets \f$ \mathbf{x} \f$ and \f$ \mathbf{y} \f$ where
ykuroda 0:13a5d365ba16 73 * \f$d\f$ is corresponding to the dimension (which is typically small).
ykuroda 0:13a5d365ba16 74 * The analysis is involving the SVD having a complexity of \f$O(d^3)\f$
ykuroda 0:13a5d365ba16 75 * though the actual computational effort lies in the covariance
ykuroda 0:13a5d365ba16 76 * matrix computation which has an asymptotic lower bound of \f$O(dm)\f$ when
ykuroda 0:13a5d365ba16 77 * the input point sets have dimension \f$d \times m\f$.
ykuroda 0:13a5d365ba16 78 *
ykuroda 0:13a5d365ba16 79 * Currently the method is working only for floating point matrices.
ykuroda 0:13a5d365ba16 80 *
ykuroda 0:13a5d365ba16 81 * \todo Should the return type of umeyama() become a Transform?
ykuroda 0:13a5d365ba16 82 *
ykuroda 0:13a5d365ba16 83 * \param src Source points \f$ \mathbf{x} = \left( x_1, \hdots, x_n \right) \f$.
ykuroda 0:13a5d365ba16 84 * \param dst Destination points \f$ \mathbf{y} = \left( y_1, \hdots, y_n \right) \f$.
ykuroda 0:13a5d365ba16 85 * \param with_scaling Sets \f$ c=1 \f$ when <code>false</code> is passed.
ykuroda 0:13a5d365ba16 86 * \return The homogeneous transformation
ykuroda 0:13a5d365ba16 87 * \f{align*}
ykuroda 0:13a5d365ba16 88 * T = \begin{bmatrix} c\mathbf{R} & \mathbf{t} \\ \mathbf{0} & 1 \end{bmatrix}
ykuroda 0:13a5d365ba16 89 * \f}
ykuroda 0:13a5d365ba16 90 * minimizing the resudiual above. This transformation is always returned as an
ykuroda 0:13a5d365ba16 91 * Eigen::Matrix.
ykuroda 0:13a5d365ba16 92 */
ykuroda 0:13a5d365ba16 93 template <typename Derived, typename OtherDerived>
ykuroda 0:13a5d365ba16 94 typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
ykuroda 0:13a5d365ba16 95 umeyama(const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true)
ykuroda 0:13a5d365ba16 96 {
ykuroda 0:13a5d365ba16 97 typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
ykuroda 0:13a5d365ba16 98 typedef typename internal::traits<TransformationMatrixType>::Scalar Scalar;
ykuroda 0:13a5d365ba16 99 typedef typename NumTraits<Scalar>::Real RealScalar;
ykuroda 0:13a5d365ba16 100 typedef typename Derived::Index Index;
ykuroda 0:13a5d365ba16 101
ykuroda 0:13a5d365ba16 102 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
ykuroda 0:13a5d365ba16 103 EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
ykuroda 0:13a5d365ba16 104 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
ykuroda 0:13a5d365ba16 105
ykuroda 0:13a5d365ba16 106 enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
ykuroda 0:13a5d365ba16 107
ykuroda 0:13a5d365ba16 108 typedef Matrix<Scalar, Dimension, 1> VectorType;
ykuroda 0:13a5d365ba16 109 typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
ykuroda 0:13a5d365ba16 110 typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
ykuroda 0:13a5d365ba16 111
ykuroda 0:13a5d365ba16 112 const Index m = src.rows(); // dimension
ykuroda 0:13a5d365ba16 113 const Index n = src.cols(); // number of measurements
ykuroda 0:13a5d365ba16 114
ykuroda 0:13a5d365ba16 115 // required for demeaning ...
ykuroda 0:13a5d365ba16 116 const RealScalar one_over_n = RealScalar(1) / static_cast<RealScalar>(n);
ykuroda 0:13a5d365ba16 117
ykuroda 0:13a5d365ba16 118 // computation of mean
ykuroda 0:13a5d365ba16 119 const VectorType src_mean = src.rowwise().sum() * one_over_n;
ykuroda 0:13a5d365ba16 120 const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
ykuroda 0:13a5d365ba16 121
ykuroda 0:13a5d365ba16 122 // demeaning of src and dst points
ykuroda 0:13a5d365ba16 123 const RowMajorMatrixType src_demean = src.colwise() - src_mean;
ykuroda 0:13a5d365ba16 124 const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
ykuroda 0:13a5d365ba16 125
ykuroda 0:13a5d365ba16 126 // Eq. (36)-(37)
ykuroda 0:13a5d365ba16 127 const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
ykuroda 0:13a5d365ba16 128
ykuroda 0:13a5d365ba16 129 // Eq. (38)
ykuroda 0:13a5d365ba16 130 const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
ykuroda 0:13a5d365ba16 131
ykuroda 0:13a5d365ba16 132 JacobiSVD<MatrixType> svd(sigma, ComputeFullU | ComputeFullV);
ykuroda 0:13a5d365ba16 133
ykuroda 0:13a5d365ba16 134 // Initialize the resulting transformation with an identity matrix...
ykuroda 0:13a5d365ba16 135 TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
ykuroda 0:13a5d365ba16 136
ykuroda 0:13a5d365ba16 137 // Eq. (39)
ykuroda 0:13a5d365ba16 138 VectorType S = VectorType::Ones(m);
ykuroda 0:13a5d365ba16 139 if (sigma.determinant()<Scalar(0)) S(m-1) = Scalar(-1);
ykuroda 0:13a5d365ba16 140
ykuroda 0:13a5d365ba16 141 // Eq. (40) and (43)
ykuroda 0:13a5d365ba16 142 const VectorType& d = svd.singularValues();
ykuroda 0:13a5d365ba16 143 Index rank = 0; for (Index i=0; i<m; ++i) if (!internal::isMuchSmallerThan(d.coeff(i),d.coeff(0))) ++rank;
ykuroda 0:13a5d365ba16 144 if (rank == m-1) {
ykuroda 0:13a5d365ba16 145 if ( svd.matrixU().determinant() * svd.matrixV().determinant() > Scalar(0) ) {
ykuroda 0:13a5d365ba16 146 Rt.block(0,0,m,m).noalias() = svd.matrixU()*svd.matrixV().transpose();
ykuroda 0:13a5d365ba16 147 } else {
ykuroda 0:13a5d365ba16 148 const Scalar s = S(m-1); S(m-1) = Scalar(-1);
ykuroda 0:13a5d365ba16 149 Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
ykuroda 0:13a5d365ba16 150 S(m-1) = s;
ykuroda 0:13a5d365ba16 151 }
ykuroda 0:13a5d365ba16 152 } else {
ykuroda 0:13a5d365ba16 153 Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
ykuroda 0:13a5d365ba16 154 }
ykuroda 0:13a5d365ba16 155
ykuroda 0:13a5d365ba16 156 if (with_scaling)
ykuroda 0:13a5d365ba16 157 {
ykuroda 0:13a5d365ba16 158 // Eq. (42)
ykuroda 0:13a5d365ba16 159 const Scalar c = Scalar(1)/src_var * svd.singularValues().dot(S);
ykuroda 0:13a5d365ba16 160
ykuroda 0:13a5d365ba16 161 // Eq. (41)
ykuroda 0:13a5d365ba16 162 Rt.col(m).head(m) = dst_mean;
ykuroda 0:13a5d365ba16 163 Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
ykuroda 0:13a5d365ba16 164 Rt.block(0,0,m,m) *= c;
ykuroda 0:13a5d365ba16 165 }
ykuroda 0:13a5d365ba16 166 else
ykuroda 0:13a5d365ba16 167 {
ykuroda 0:13a5d365ba16 168 Rt.col(m).head(m) = dst_mean;
ykuroda 0:13a5d365ba16 169 Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m,m)*src_mean;
ykuroda 0:13a5d365ba16 170 }
ykuroda 0:13a5d365ba16 171
ykuroda 0:13a5d365ba16 172 return Rt;
ykuroda 0:13a5d365ba16 173 }
ykuroda 0:13a5d365ba16 174
ykuroda 0:13a5d365ba16 175 } // end namespace Eigen
ykuroda 0:13a5d365ba16 176
ykuroda 0:13a5d365ba16 177 #endif // EIGEN_UMEYAMA_H