Eigen  3.3.3
Umeyama.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_UMEYAMA_H
00011 #define EIGEN_UMEYAMA_H
00012 
00013 // This file requires the user to include 
00014 // * Eigen/Core
00015 // * Eigen/LU 
00016 // * Eigen/SVD
00017 // * Eigen/Array
00018 
00019 namespace Eigen { 
00020 
00021 #ifndef EIGEN_PARSED_BY_DOXYGEN
00022 
00023 // These helpers are required since it allows to use mixed types as parameters
00024 // for the Umeyama. The problem with mixed parameters is that the return type
00025 // cannot trivially be deduced when float and double types are mixed.
00026 namespace internal {
00027 
00028 // Compile time return type deduction for different MatrixBase types.
00029 // Different means here different alignment and parameters but the same underlying
00030 // real scalar type.
00031 template<typename MatrixType, typename OtherMatrixType>
00032 struct umeyama_transform_matrix_type
00033 {
00034   enum {
00035     MinRowsAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
00036 
00037     // When possible we want to choose some small fixed size value since the result
00038     // is likely to fit on the stack. So here, EIGEN_SIZE_MIN_PREFER_DYNAMIC is not what we want.
00039     HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime)+1
00040   };
00041 
00042   typedef Matrix<typename traits<MatrixType>::Scalar,
00043     HomogeneousDimension,
00044     HomogeneousDimension,
00045     AutoAlign | (traits<MatrixType>::Flags & RowMajorBit ? RowMajor : ColMajor),
00046     HomogeneousDimension,
00047     HomogeneousDimension
00048   > type;
00049 };
00050 
00051 }
00052 
00053 #endif
00054 
00093 template <typename Derived, typename OtherDerived>
00094 typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
00095 umeyama(const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true)
00096 {
00097   typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
00098   typedef typename internal::traits<TransformationMatrixType>::Scalar Scalar;
00099   typedef typename NumTraits<Scalar>::Real RealScalar;
00100 
00101   EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
00102   EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
00103     YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00104 
00105   enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
00106 
00107   typedef Matrix<Scalar, Dimension, 1> VectorType;
00108   typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
00109   typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
00110 
00111   const Index m = src.rows(); // dimension
00112   const Index n = src.cols(); // number of measurements
00113 
00114   // required for demeaning ...
00115   const RealScalar one_over_n = RealScalar(1) / static_cast<RealScalar>(n);
00116 
00117   // computation of mean
00118   const VectorType src_mean = src.rowwise().sum() * one_over_n;
00119   const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
00120 
00121   // demeaning of src and dst points
00122   const RowMajorMatrixType src_demean = src.colwise() - src_mean;
00123   const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
00124 
00125   // Eq. (36)-(37)
00126   const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
00127 
00128   // Eq. (38)
00129   const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
00130 
00131   JacobiSVD<MatrixType> svd(sigma, ComputeFullU | ComputeFullV);
00132 
00133   // Initialize the resulting transformation with an identity matrix...
00134   TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
00135 
00136   // Eq. (39)
00137   VectorType S = VectorType::Ones(m);
00138 
00139   if  ( svd.matrixU().determinant() * svd.matrixV().determinant() < 0 )
00140     S(m-1) = -1;
00141 
00142   // Eq. (40) and (43)
00143   Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
00144 
00145   if (with_scaling)
00146   {
00147     // Eq. (42)
00148     const Scalar c = Scalar(1)/src_var * svd.singularValues().dot(S);
00149 
00150     // Eq. (41)
00151     Rt.col(m).head(m) = dst_mean;
00152     Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
00153     Rt.block(0,0,m,m) *= c;
00154   }
00155   else
00156   {
00157     Rt.col(m).head(m) = dst_mean;
00158     Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m,m)*src_mean;
00159   }
00160 
00161   return Rt;
00162 }
00163 
00164 } // end namespace Eigen
00165 
00166 #endif // EIGEN_UMEYAMA_H
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends