Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/Eigen/src/Geometry/Umeyama.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2009 Hauke Heibel <hauke.heibel@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_UMEYAMA_H
0011 #define EIGEN_UMEYAMA_H
0012 
0013 // This file requires the user to include 
0014 // * Eigen/Core
0015 // * Eigen/LU 
0016 // * Eigen/SVD
0017 // * Eigen/Array
0018 
0019 namespace Eigen { 
0020 
0021 #ifndef EIGEN_PARSED_BY_DOXYGEN
0022 
0023 // These helpers are required since it allows to use mixed types as parameters
0024 // for the Umeyama. The problem with mixed parameters is that the return type
0025 // cannot trivially be deduced when float and double types are mixed.
0026 namespace internal {
0027 
0028 // Compile time return type deduction for different MatrixBase types.
0029 // Different means here different alignment and parameters but the same underlying
0030 // real scalar type.
0031 template<typename MatrixType, typename OtherMatrixType>
0032 struct umeyama_transform_matrix_type
0033 {
0034   enum {
0035     MinRowsAtCompileTime = EIGEN_SIZE_MIN_PREFER_DYNAMIC(MatrixType::RowsAtCompileTime, OtherMatrixType::RowsAtCompileTime),
0036 
0037     // When possible we want to choose some small fixed size value since the result
0038     // is likely to fit on the stack. So here, EIGEN_SIZE_MIN_PREFER_DYNAMIC is not what we want.
0039     HomogeneousDimension = int(MinRowsAtCompileTime) == Dynamic ? Dynamic : int(MinRowsAtCompileTime)+1
0040   };
0041 
0042   typedef Matrix<typename traits<MatrixType>::Scalar,
0043     HomogeneousDimension,
0044     HomogeneousDimension,
0045     AutoAlign | (traits<MatrixType>::Flags & RowMajorBit ? RowMajor : ColMajor),
0046     HomogeneousDimension,
0047     HomogeneousDimension
0048   > type;
0049 };
0050 
0051 }
0052 
0053 #endif
0054 
0055 /**
0056 * \geometry_module \ingroup Geometry_Module
0057 *
0058 * \brief Returns the transformation between two point sets.
0059 *
0060 * The algorithm is based on:
0061 * "Least-squares estimation of transformation parameters between two point patterns",
0062 * Shinji Umeyama, PAMI 1991, DOI: 10.1109/34.88573
0063 *
0064 * It estimates parameters \f$ c, \mathbf{R}, \f$ and \f$ \mathbf{t} \f$ such that
0065 * \f{align*}
0066 *   \frac{1}{n} \sum_{i=1}^n \vert\vert y_i - (c\mathbf{R}x_i + \mathbf{t}) \vert\vert_2^2
0067 * \f}
0068 * is minimized.
0069 *
0070 * The algorithm is based on the analysis of the covariance matrix
0071 * \f$ \Sigma_{\mathbf{x}\mathbf{y}} \in \mathbb{R}^{d \times d} \f$
0072 * of the input point sets \f$ \mathbf{x} \f$ and \f$ \mathbf{y} \f$ where 
0073 * \f$d\f$ is corresponding to the dimension (which is typically small).
0074 * The analysis is involving the SVD having a complexity of \f$O(d^3)\f$
0075 * though the actual computational effort lies in the covariance
0076 * matrix computation which has an asymptotic lower bound of \f$O(dm)\f$ when 
0077 * the input point sets have dimension \f$d \times m\f$.
0078 *
0079 * Currently the method is working only for floating point matrices.
0080 *
0081 * \todo Should the return type of umeyama() become a Transform?
0082 *
0083 * \param src Source points \f$ \mathbf{x} = \left( x_1, \hdots, x_n \right) \f$.
0084 * \param dst Destination points \f$ \mathbf{y} = \left( y_1, \hdots, y_n \right) \f$.
0085 * \param with_scaling Sets \f$ c=1 \f$ when <code>false</code> is passed.
0086 * \return The homogeneous transformation 
0087 * \f{align*}
0088 *   T = \begin{bmatrix} c\mathbf{R} & \mathbf{t} \\ \mathbf{0} & 1 \end{bmatrix}
0089 * \f}
0090 * minimizing the residual above. This transformation is always returned as an 
0091 * Eigen::Matrix.
0092 */
0093 template <typename Derived, typename OtherDerived>
0094 typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
0095 umeyama(const MatrixBase<Derived>& src, const MatrixBase<OtherDerived>& dst, bool with_scaling = true)
0096 {
0097   typedef typename internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
0098   typedef typename internal::traits<TransformationMatrixType>::Scalar Scalar;
0099   typedef typename NumTraits<Scalar>::Real RealScalar;
0100 
0101   EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
0102   EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename internal::traits<OtherDerived>::Scalar>::value),
0103     YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
0104 
0105   enum { Dimension = EIGEN_SIZE_MIN_PREFER_DYNAMIC(Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
0106 
0107   typedef Matrix<Scalar, Dimension, 1> VectorType;
0108   typedef Matrix<Scalar, Dimension, Dimension> MatrixType;
0109   typedef typename internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
0110 
0111   const Index m = src.rows(); // dimension
0112   const Index n = src.cols(); // number of measurements
0113 
0114   // required for demeaning ...
0115   const RealScalar one_over_n = RealScalar(1) / static_cast<RealScalar>(n);
0116 
0117   // computation of mean
0118   const VectorType src_mean = src.rowwise().sum() * one_over_n;
0119   const VectorType dst_mean = dst.rowwise().sum() * one_over_n;
0120 
0121   // demeaning of src and dst points
0122   const RowMajorMatrixType src_demean = src.colwise() - src_mean;
0123   const RowMajorMatrixType dst_demean = dst.colwise() - dst_mean;
0124 
0125   // Eq. (36)-(37)
0126   const Scalar src_var = src_demean.rowwise().squaredNorm().sum() * one_over_n;
0127 
0128   // Eq. (38)
0129   const MatrixType sigma = one_over_n * dst_demean * src_demean.transpose();
0130 
0131   JacobiSVD<MatrixType> svd(sigma, ComputeFullU | ComputeFullV);
0132 
0133   // Initialize the resulting transformation with an identity matrix...
0134   TransformationMatrixType Rt = TransformationMatrixType::Identity(m+1,m+1);
0135 
0136   // Eq. (39)
0137   VectorType S = VectorType::Ones(m);
0138 
0139   if  ( svd.matrixU().determinant() * svd.matrixV().determinant() < 0 )
0140     S(m-1) = -1;
0141 
0142   // Eq. (40) and (43)
0143   Rt.block(0,0,m,m).noalias() = svd.matrixU() * S.asDiagonal() * svd.matrixV().transpose();
0144 
0145   if (with_scaling)
0146   {
0147     // Eq. (42)
0148     const Scalar c = Scalar(1)/src_var * svd.singularValues().dot(S);
0149 
0150     // Eq. (41)
0151     Rt.col(m).head(m) = dst_mean;
0152     Rt.col(m).head(m).noalias() -= c*Rt.topLeftCorner(m,m)*src_mean;
0153     Rt.block(0,0,m,m) *= c;
0154   }
0155   else
0156   {
0157     Rt.col(m).head(m) = dst_mean;
0158     Rt.col(m).head(m).noalias() -= Rt.topLeftCorner(m,m)*src_mean;
0159   }
0160 
0161   return Rt;
0162 }
0163 
0164 } // end namespace Eigen
0165 
0166 #endif // EIGEN_UMEYAMA_H