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 namespace Eigen { 
00029 
00045 namespace internal {
00046 
00047 template<typename MatrixType,int Direction>
00048 struct traits<Homogeneous<MatrixType,Direction> >
00049  : traits<MatrixType>
00050 {
00051   typedef typename traits<MatrixType>::StorageKind StorageKind;
00052   typedef typename nested<MatrixType>::type MatrixTypeNested;
00053   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
00054   enum {
00055     RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ?
00056                   int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
00057     ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ?
00058                   int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
00059     RowsAtCompileTime = Direction==Vertical  ?  RowsPlusOne : MatrixType::RowsAtCompileTime,
00060     ColsAtCompileTime = Direction==Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
00061     MaxRowsAtCompileTime = RowsAtCompileTime,
00062     MaxColsAtCompileTime = ColsAtCompileTime,
00063     TmpFlags = _MatrixTypeNested::Flags & HereditaryBits,
00064     Flags = ColsAtCompileTime==1 ? (TmpFlags & ~RowMajorBit)
00065           : RowsAtCompileTime==1 ? (TmpFlags | RowMajorBit)
00066           : TmpFlags,
00067     CoeffReadCost = _MatrixTypeNested::CoeffReadCost
00068   };
00069 };
00070 
00071 template<typename MatrixType,typename Lhs> struct homogeneous_left_product_impl;
00072 template<typename MatrixType,typename Rhs> struct homogeneous_right_product_impl;
00073 
00074 } // end namespace internal
00075 
00076 template<typename MatrixType,int _Direction> class Homogeneous
00077   : public MatrixBase<Homogeneous<MatrixType,_Direction> >
00078 {
00079   public:
00080 
00081     enum { Direction = _Direction };
00082 
00083     typedef MatrixBase<Homogeneous> Base;
00084     EIGEN_DENSE_PUBLIC_INTERFACE(Homogeneous)
00085 
00086     inline Homogeneous(const MatrixType& matrix)
00087       : m_matrix(matrix)
00088     {}
00089 
00090     inline Index rows() const { return m_matrix.rows() + (int(Direction)==Vertical   ? 1 : 0); }
00091     inline Index cols() const { return m_matrix.cols() + (int(Direction)==Horizontal ? 1 : 0); }
00092 
00093     inline Scalar coeff(Index row, Index col) const
00094     {
00095       if(  (int(Direction)==Vertical   && row==m_matrix.rows())
00096         || (int(Direction)==Horizontal && col==m_matrix.cols()))
00097         return 1;
00098       return m_matrix.coeff(row, col);
00099     }
00100 
00101     template<typename Rhs>
00102     inline const internal::homogeneous_right_product_impl<Homogeneous,Rhs>
00103     operator* (const MatrixBase<Rhs>& rhs) const
00104     {
00105       eigen_assert(int(Direction)==Horizontal);
00106       return internal::homogeneous_right_product_impl<Homogeneous,Rhs>(m_matrix,rhs.derived());
00107     }
00108 
00109     template<typename Lhs> friend
00110     inline const internal::homogeneous_left_product_impl<Homogeneous,Lhs>
00111     operator* (const MatrixBase<Lhs>& lhs, const Homogeneous& rhs)
00112     {
00113       eigen_assert(int(Direction)==Vertical);
00114       return internal::homogeneous_left_product_impl<Homogeneous,Lhs>(lhs.derived(),rhs.m_matrix);
00115     }
00116 
00117     template<typename Scalar, int Dim, int Mode, int Options> friend
00118     inline const internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode,Options> >
00119     operator* (const Transform<Scalar,Dim,Mode,Options>& lhs, const Homogeneous& rhs)
00120     {
00121       eigen_assert(int(Direction)==Vertical);
00122       return internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode,Options> >(lhs,rhs.m_matrix);
00123     }
00124 
00125   protected:
00126     typename MatrixType::Nested m_matrix;
00127 };
00128 
00140 template<typename Derived>
00141 inline typename MatrixBase<Derived>::HomogeneousReturnType
00142 MatrixBase<Derived>::homogeneous() const
00143 {
00144   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
00145   return derived();
00146 }
00147 
00156 template<typename ExpressionType, int Direction>
00157 inline Homogeneous<ExpressionType,Direction>
00158 VectorwiseOp<ExpressionType,Direction>::homogeneous() const
00159 {
00160   return _expression();
00161 }
00162 
00171 template<typename Derived>
00172 inline const typename MatrixBase<Derived>::HNormalizedReturnType
00173 MatrixBase<Derived>::hnormalized() const
00174 {
00175   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
00176   return ConstStartMinusOne(derived(),0,0,
00177     ColsAtCompileTime==1?size()-1:1,
00178     ColsAtCompileTime==1?1:size()-1) / coeff(size()-1);
00179 }
00180 
00189 template<typename ExpressionType, int Direction>
00190 inline const typename VectorwiseOp<ExpressionType,Direction>::HNormalizedReturnType
00191 VectorwiseOp<ExpressionType,Direction>::hnormalized() const
00192 {
00193   return HNormalized_Block(_expression(),0,0,
00194       Direction==Vertical   ? _expression().rows()-1 : _expression().rows(),
00195       Direction==Horizontal ? _expression().cols()-1 : _expression().cols()).cwiseQuotient(
00196       Replicate<HNormalized_Factors,
00197                 Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
00198                 Direction==Horizontal ? HNormalized_SizeMinusOne : 1>
00199         (HNormalized_Factors(_expression(),
00200           Direction==Vertical    ? _expression().rows()-1:0,
00201           Direction==Horizontal  ? _expression().cols()-1:0,
00202           Direction==Vertical    ? 1 : _expression().rows(),
00203           Direction==Horizontal  ? 1 : _expression().cols()),
00204          Direction==Vertical   ? _expression().rows()-1 : 1,
00205          Direction==Horizontal ? _expression().cols()-1 : 1));
00206 }
00207 
00208 namespace internal {
00209 
00210 template<typename MatrixOrTransformType>
00211 struct take_matrix_for_product
00212 {
00213   typedef MatrixOrTransformType type;
00214   static const type& run(const type &x) { return x; }
00215 };
00216 
00217 template<typename Scalar, int Dim, int Mode,int Options>
00218 struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> >
00219 {
00220   typedef Transform<Scalar, Dim, Mode, Options> TransformType;
00221   typedef typename internal::add_const<typename TransformType::ConstAffinePart>::type type;
00222   static type run (const TransformType& x) { return x.affine(); }
00223 };
00224 
00225 template<typename Scalar, int Dim, int Options>
00226 struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> >
00227 {
00228   typedef Transform<Scalar, Dim, Projective, Options> TransformType;
00229   typedef typename TransformType::MatrixType type;
00230   static const type& run (const TransformType& x) { return x.matrix(); }
00231 };
00232 
00233 template<typename MatrixType,typename Lhs>
00234 struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
00235 {
00236   typedef typename take_matrix_for_product<Lhs>::type LhsMatrixType;
00237   typedef typename remove_all<MatrixType>::type MatrixTypeCleaned;
00238   typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
00239   typedef typename make_proper_matrix_type<
00240                  typename traits<MatrixTypeCleaned>::Scalar,
00241                  LhsMatrixTypeCleaned::RowsAtCompileTime,
00242                  MatrixTypeCleaned::ColsAtCompileTime,
00243                  MatrixTypeCleaned::PlainObject::Options,
00244                  LhsMatrixTypeCleaned::MaxRowsAtCompileTime,
00245                  MatrixTypeCleaned::MaxColsAtCompileTime>::type ReturnType;
00246 };
00247 
00248 template<typename MatrixType,typename Lhs>
00249 struct homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
00250   : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
00251 {
00252   typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
00253   typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
00254   typedef typename remove_all<typename LhsMatrixTypeCleaned::Nested>::type LhsMatrixTypeNested;
00255   typedef typename MatrixType::Index Index;
00256   homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
00257     : m_lhs(take_matrix_for_product<Lhs>::run(lhs)),
00258       m_rhs(rhs)
00259   {}
00260 
00261   inline Index rows() const { return m_lhs.rows(); }
00262   inline Index cols() const { return m_rhs.cols(); }
00263 
00264   template<typename Dest> void evalTo(Dest& dst) const
00265   {
00266     // FIXME investigate how to allow lazy evaluation of this product when possible
00267     dst = Block<const LhsMatrixTypeNested,
00268               LhsMatrixTypeNested::RowsAtCompileTime,
00269               LhsMatrixTypeNested::ColsAtCompileTime==Dynamic?Dynamic:LhsMatrixTypeNested::ColsAtCompileTime-1>
00270             (m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
00271     dst += m_lhs.col(m_lhs.cols()-1).rowwise()
00272             .template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
00273   }
00274 
00275   typename LhsMatrixTypeCleaned::Nested m_lhs;
00276   typename MatrixType::Nested m_rhs;
00277 };
00278 
00279 template<typename MatrixType,typename Rhs>
00280 struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
00281 {
00282   typedef typename make_proper_matrix_type<typename traits<MatrixType>::Scalar,
00283                  MatrixType::RowsAtCompileTime,
00284                  Rhs::ColsAtCompileTime,
00285                  MatrixType::PlainObject::Options,
00286                  MatrixType::MaxRowsAtCompileTime,
00287                  Rhs::MaxColsAtCompileTime>::type ReturnType;
00288 };
00289 
00290 template<typename MatrixType,typename Rhs>
00291 struct homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
00292   : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
00293 {
00294   typedef typename remove_all<typename Rhs::Nested>::type RhsNested;
00295   typedef typename MatrixType::Index Index;
00296   homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
00297     : m_lhs(lhs), m_rhs(rhs)
00298   {}
00299 
00300   inline Index rows() const { return m_lhs.rows(); }
00301   inline Index cols() const { return m_rhs.cols(); }
00302 
00303   template<typename Dest> void evalTo(Dest& dst) const
00304   {
00305     // FIXME investigate how to allow lazy evaluation of this product when possible
00306     dst = m_lhs * Block<const RhsNested,
00307                         RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
00308                         RhsNested::ColsAtCompileTime>
00309             (m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
00310     dst += m_rhs.row(m_rhs.rows()-1).colwise()
00311             .template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
00312   }
00313 
00314   typename MatrixType::Nested m_lhs;
00315   typename Rhs::Nested m_rhs;
00316 };
00317 
00318 } // end namespace internal
00319 
00320 } // end namespace Eigen
00321 
00322 #endif // EIGEN_HOMOGENEOUS_H