Main MRPT website > C++ reference
MRPT logo

Homogeneous.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-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
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_HOMOGENEOUS_H
00026 #define EIGEN_HOMOGENEOUS_H
00027 
00028 /** \geometry_module \ingroup Geometry_Module
00029   *
00030   * \class Homogeneous
00031   *
00032   * \brief Expression of one (or a set of) homogeneous vector(s)
00033   *
00034   * \param MatrixType the type of the object in which we are making homogeneous
00035   *
00036   * This class represents an expression of one (or a set of) homogeneous vector(s).
00037   * It is the return type of MatrixBase::homogeneous() and most of the time
00038   * this is the only way it is used.
00039   *
00040   * \sa MatrixBase::homogeneous()
00041   */
00042 
00043 namespace internal {
00044 
00045 template<typename MatrixType,int Direction>
00046 struct traits<Homogeneous<MatrixType,Direction> >
00047  : traits<MatrixType>
00048 {
00049   typedef typename traits<MatrixType>::StorageKind StorageKind;
00050   typedef typename nested<MatrixType>::type MatrixTypeNested;
00051   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00052   enum {
00053     RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ?
00054                   int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
00055     ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ?
00056                   int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
00057     RowsAtCompileTime = Direction==Vertical  ?  RowsPlusOne : MatrixType::RowsAtCompileTime,
00058     ColsAtCompileTime = Direction==Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
00059     MaxRowsAtCompileTime = RowsAtCompileTime,
00060     MaxColsAtCompileTime = ColsAtCompileTime,
00061     TmpFlags = _MatrixTypeNested::Flags & HereditaryBits,
00062     Flags = ColsAtCompileTime==1 ? (TmpFlags & ~RowMajorBit)
00063           : RowsAtCompileTime==1 ? (TmpFlags | RowMajorBit)
00064           : TmpFlags,
00065     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
00066   };
00067 };
00068 
00069 template<typename MatrixType,typename Lhs> struct homogeneous_left_product_impl;
00070 template<typename MatrixType,typename Rhs> struct homogeneous_right_product_impl;
00071 
00072 } // end namespace internal
00073 
00074 template<typename MatrixType,int _Direction> class Homogeneous
00075   : public MatrixBase<Homogeneous<MatrixType,_Direction> >
00076 {
00077   public:
00078 
00079     enum { Direction = _Direction };
00080 
00081     typedef MatrixBase<Homogeneous> Base;
00082     EIGEN_DENSE_PUBLIC_INTERFACE(Homogeneous)
00083 
00084     inline Homogeneous(const MatrixType& matrix)
00085       : m_matrix(matrix)
00086     {}
00087 
00088     inline Index rows() const { return m_matrix.rows() + (int(Direction)==Vertical   ? 1 : 0); }
00089     inline Index cols() const { return m_matrix.cols() + (int(Direction)==Horizontal ? 1 : 0); }
00090 
00091     inline Scalar coeff(Index row, Index col) const
00092     {
00093       if(  (int(Direction)==Vertical   && row==m_matrix.rows())
00094         || (int(Direction)==Horizontal && col==m_matrix.cols()))
00095         return 1;
00096       return m_matrix.coeff(row, col);
00097     }
00098 
00099     template<typename Rhs>
00100     inline const internal::homogeneous_right_product_impl<Homogeneous,Rhs>
00101     operator* (const MatrixBase<Rhs>& rhs) const
00102     {
00103       eigen_assert(int(Direction)==Horizontal);
00104       return internal::homogeneous_right_product_impl<Homogeneous,Rhs>(m_matrix,rhs.derived());
00105     }
00106 
00107     template<typename Lhs> friend
00108     inline const internal::homogeneous_left_product_impl<Homogeneous,Lhs>
00109     operator* (const MatrixBase<Lhs>& lhs, const Homogeneous& rhs)
00110     {
00111       eigen_assert(int(Direction)==Vertical);
00112       return internal::homogeneous_left_product_impl<Homogeneous,Lhs>(lhs.derived(),rhs.m_matrix);
00113     }
00114 
00115     template<typename Scalar, int Dim, int Mode> friend
00116     inline const internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode> >
00117     operator* (const Transform<Scalar,Dim,Mode>& lhs, const Homogeneous& rhs)
00118     {
00119       eigen_assert(int(Direction)==Vertical);
00120       return internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode> >(lhs,rhs.m_matrix);
00121     }
00122 
00123   protected:
00124     const typename MatrixType::Nested m_matrix;
00125 };
00126 
00127 /** \geometry_module
00128   *
00129   * \return an expression of the equivalent homogeneous vector
00130   *
00131   * \only_for_vectors
00132   *
00133   * Example: \include MatrixBase_homogeneous.cpp
00134   * Output: \verbinclude MatrixBase_homogeneous.out
00135   *
00136   * \sa class Homogeneous
00137   */
00138 template<typename Derived>
00139 inline typename MatrixBase<Derived>::HomogeneousReturnType
00140 MatrixBase<Derived>::homogeneous() const
00141 {
00142   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
00143   return derived();
00144 }
00145 
00146 /** \geometry_module
00147   *
00148   * \returns a matrix expression of homogeneous column (or row) vectors
00149   *
00150   * Example: \include VectorwiseOp_homogeneous.cpp
00151   * Output: \verbinclude VectorwiseOp_homogeneous.out
00152   *
00153   * \sa MatrixBase::homogeneous() */
00154 template<typename ExpressionType, int Direction>
00155 inline Homogeneous<ExpressionType,Direction>
00156 VectorwiseOp<ExpressionType,Direction>::homogeneous() const
00157 {
00158   return _expression();
00159 }
00160 
00161 /** \geometry_module
00162   *
00163   * \returns an expression of the homogeneous normalized vector of \c *this
00164   *
00165   * Example: \include MatrixBase_hnormalized.cpp
00166   * Output: \verbinclude MatrixBase_hnormalized.out
00167   *
00168   * \sa VectorwiseOp::hnormalized() */
00169 template<typename Derived>
00170 inline const typename MatrixBase<Derived>::HNormalizedReturnType
00171 MatrixBase<Derived>::hnormalized() const
00172 {
00173   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
00174   return ConstStartMinusOne(derived(),0,0,
00175     ColsAtCompileTime==1?size()-1:1,
00176     ColsAtCompileTime==1?1:size()-1) / coeff(size()-1);
00177 }
00178 
00179 /** \geometry_module
00180   *
00181   * \returns an expression of the homogeneous normalized vector of \c *this
00182   *
00183   * Example: \include DirectionWise_hnormalized.cpp
00184   * Output: \verbinclude DirectionWise_hnormalized.out
00185   *
00186   * \sa MatrixBase::hnormalized() */
00187 template<typename ExpressionType, int Direction>
00188 inline const typename VectorwiseOp<ExpressionType,Direction>::HNormalizedReturnType
00189 VectorwiseOp<ExpressionType,Direction>::hnormalized() const
00190 {
00191   return HNormalized_Block(_expression(),0,0,
00192       Direction==Vertical   ? _expression().rows()-1 : _expression().rows(),
00193       Direction==Horizontal ? _expression().cols()-1 : _expression().cols()).cwiseQuotient(
00194       Replicate<HNormalized_Factors,
00195                 Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
00196                 Direction==Horizontal ? HNormalized_SizeMinusOne : 1>
00197         (HNormalized_Factors(_expression(),
00198           Direction==Vertical    ? _expression().rows()-1:0,
00199           Direction==Horizontal  ? _expression().cols()-1:0,
00200           Direction==Vertical    ? 1 : _expression().rows(),
00201           Direction==Horizontal  ? 1 : _expression().cols()),
00202          Direction==Vertical   ? _expression().rows()-1 : 1,
00203          Direction==Horizontal ? _expression().cols()-1 : 1));
00204 }
00205 
00206 namespace internal {
00207 
00208 template<typename MatrixOrTransformType>
00209 struct take_matrix_for_product
00210 {
00211   typedef MatrixOrTransformType type;
00212   static const type& run(const type &x) { return x; }
00213 };
00214 
00215 template<typename Scalar, int Dim, int Mode>
00216 struct take_matrix_for_product<Transform<Scalar, Dim, Mode> >
00217 {
00218   typedef Transform<Scalar, Dim, Mode> TransformType;
00219   typedef typename TransformType::ConstAffinePart type;
00220   static const type run (const TransformType& x) { return x.affine(); }
00221 };
00222 
00223 template<typename Scalar, int Dim>
00224 struct take_matrix_for_product<Transform<Scalar, Dim, Projective> >
00225 {
00226   typedef Transform<Scalar, Dim, Projective> TransformType;
00227   typedef typename TransformType::MatrixType type;
00228   static const type& run (const TransformType& x) { return x.matrix(); }
00229 };
00230 
00231 template<typename MatrixType,typename Lhs>
00232 struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
00233 {
00234   typedef typename take_matrix_for_product<Lhs>::type LhsMatrixType;
00235   typedef typename make_proper_matrix_type<
00236                  typename traits<MatrixType>::Scalar,
00237                  LhsMatrixType::RowsAtCompileTime,
00238                  MatrixType::ColsAtCompileTime,
00239                  MatrixType::PlainObject::Options,
00240                  LhsMatrixType::MaxRowsAtCompileTime,
00241                  MatrixType::MaxColsAtCompileTime>::type ReturnType;
00242 };
00243 
00244 template<typename MatrixType,typename Lhs>
00245 struct homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
00246   : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
00247 {
00248   typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
00249   typedef typename remove_all<typename LhsMatrixType::Nested>::type LhsMatrixTypeNested;
00250   typedef typename MatrixType::Index Index;
00251   homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
00252     : m_lhs(take_matrix_for_product<Lhs>::run(lhs)),
00253       m_rhs(rhs)
00254   {}
00255 
00256   inline Index rows() const { return m_lhs.rows(); }
00257   inline Index cols() const { return m_rhs.cols(); }
00258 
00259   template<typename Dest> void evalTo(Dest& dst) const
00260   {
00261     // FIXME investigate how to allow lazy evaluation of this product when possible
00262     dst = Block<const LhsMatrixTypeNested,
00263               LhsMatrixTypeNested::RowsAtCompileTime,
00264               LhsMatrixTypeNested::ColsAtCompileTime==Dynamic?Dynamic:LhsMatrixTypeNested::ColsAtCompileTime-1>
00265             (m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
00266     dst += m_lhs.col(m_lhs.cols()-1).rowwise()
00267             .template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
00268   }
00269 
00270   const typename LhsMatrixType::Nested m_lhs;
00271   const typename MatrixType::Nested m_rhs;
00272 };
00273 
00274 template<typename MatrixType,typename Rhs>
00275 struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
00276 {
00277   typedef typename make_proper_matrix_type<typename traits<MatrixType>::Scalar,
00278                  MatrixType::RowsAtCompileTime,
00279                  Rhs::ColsAtCompileTime,
00280                  MatrixType::PlainObject::Options,
00281                  MatrixType::MaxRowsAtCompileTime,
00282                  Rhs::MaxColsAtCompileTime>::type ReturnType;
00283 };
00284 
00285 template<typename MatrixType,typename Rhs>
00286 struct homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
00287   : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
00288 {
00289   typedef typename remove_all<typename Rhs::Nested>::type RhsNested;
00290   typedef typename MatrixType::Index Index;
00291   homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
00292     : m_lhs(lhs), m_rhs(rhs)
00293   {}
00294 
00295   inline Index rows() const { return m_lhs.rows(); }
00296   inline Index cols() const { return m_rhs.cols(); }
00297 
00298   template<typename Dest> void evalTo(Dest& dst) const
00299   {
00300     // FIXME investigate how to allow lazy evaluation of this product when possible
00301     dst = m_lhs * Block<const RhsNested,
00302                         RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
00303                         RhsNested::ColsAtCompileTime>
00304             (m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
00305     dst += m_rhs.row(m_rhs.rows()-1).colwise()
00306             .template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
00307   }
00308 
00309   const typename MatrixType::Nested m_lhs;
00310   const typename Rhs::Nested m_rhs;
00311 };
00312 
00313 } // end namespace internal
00314 
00315 #endif // EIGEN_HOMOGENEOUS_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011