Umeyama.h
Go to the documentation of this file.
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 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_UMEYAMA_H
00026 #define EIGEN_UMEYAMA_H
00027 
00028 // This file requires the user to include 
00029 // * Eigen/Core
00030 // * Eigen/LU 
00031 // * Eigen/SVD
00032 // * Eigen/Array
00033 
00034 #ifndef EIGEN_PARSED_BY_DOXYGEN
00035 
00036 // These helpers are required since it allows to use mixed types as parameters
00037 // for the Umeyama. The problem with mixed parameters is that the return type
00038 // cannot trivially be deduced when float and double types are mixed.
00039 namespace internal {
00040 
00041 // Compile time return type deduction for different MatrixBase types.
00042 // Different means here different alignment and parameters but the same underlying
00043 // real scalar type.
00044 template<typename MatrixType, typename OtherMatrixType>
00045 struct umeyama_transform_matrix_type
00046 {
00047   enum {
00048     MinRowsAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
00049 
00050     // When possible we want to choose some small fixed size value since the result
00051     // is likely to fit on the stack. So here, EIGEN_SIZE_MIN_PREFER_DYNAMIC is not what we want.
00052     HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime)+1
00053   };
00054 
00055   typedef Matrix<typename traits<MatrixType>::Scalar,
00056     HomogeneousDimension,
00057     HomogeneousDimension,
00058     AutoAlign | (traits<MatrixType>::Flags & RowMajorBit ? RowMajor : ColMajor),
00059     HomogeneousDimension,
00060     HomogeneousDimension
00061   > type;
00062 };
00063 
00064 }
00065 
00066 #endif
00067 
00106 template <typename Derived, typename OtherDerived>
00107 typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
00108 umeyama(const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true)
00109 {
00110   typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
00111   typedef typename internal::traits<TransformationMatrixType>::Scalar Scalar;
00112   typedef typename NumTraits<Scalar>::Real RealScalar;
00113   typedef typename Derived::Index Index;
00114 
00115   EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
00116   EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
00117     YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00118 
00119   enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
00120 
00121   typedef Matrix<Scalar, Dimension, 1> VectorType;
00122   typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
00123   typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
00124 
00125   const Index m = src.rows(); // dimension
00126   const Index n = src.cols(); // number of measurements
00127 
00128   // required for demeaning ...
00129   const RealScalar one_over_n = 1 / static_cast<RealScalar>(n);
00130 
00131   // computation of mean
00132   const VectorType src_mean = src.rowwise().sum() * one_over_n;
00133   const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
00134 
00135   // demeaning of src and dst points
00136   const RowMajorMatrixType src_demean = src.colwise() - src_mean;
00137   const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
00138 
00139   // Eq. (36)-(37)
00140   const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
00141 
00142   // Eq. (38)
00143   const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
00144 
00145   JacobiSVD<MatrixType> svd(sigma, ComputeFullU | ComputeFullV);
00146 
00147   // Initialize the resulting transformation with an identity matrix...
00148   TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
00149 
00150   // Eq. (39)
00151   VectorType S = VectorType::Ones(m);
00152   if (sigma.determinant()<0) S(m-1) = -1;
00153 
00154   // Eq. (40) and (43)
00155   const VectorType& d = svd.singularValues();
00156   Index rank = 0; for (Index i=0; i<m; ++i) if (!internal::isMuchSmallerThan(d.coeff(i),d.coeff(0))) ++rank;
00157   if (rank == m-1) {
00158     if ( svd.matrixU().determinant() * svd.matrixV().determinant() > 0 ) {
00159       Rt.block(0,0,m,m).noalias() = svd.matrixU()*svd.matrixV().transpose();
00160     } else {
00161       const Scalar s = S(m-1); S(m-1) = -1;
00162       Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
00163       S(m-1) = s;
00164     }
00165   } else {
00166     Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
00167   }
00168 
00169   // Eq. (42)
00170   const Scalar c = 1/src_var * svd.singularValues().dot(S);
00171 
00172   // Eq. (41)
00173   // Note that we first assign dst_mean to the destination so that there no need
00174   // for a temporary.
00175   Rt.col(m).head(m) = dst_mean;
00176   Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
00177 
00178   if (with_scaling) Rt.block(0,0,m,m) *= c;
00179 
00180   return Rt;
00181 }
00182 
00183 #endif // EIGEN_UMEYAMA_H


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:29