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