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 
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, int Options> friend
00116     inline const internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode,Options> >
00117     operator* (const Transform<Scalar,Dim,Mode,Options>& lhs, const Homogeneous& rhs)
00118     {
00119       eigen_assert(int(Direction)==Vertical);
00120       return internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode,Options> >(lhs,rhs.m_matrix);
00121     }
00122 
00123   protected:
00124     const typename MatrixType::Nested m_matrix;
00125 };
00126 
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 
00154 template<typename ExpressionType, int Direction>
00155 inline Homogeneous<ExpressionType,Direction>
00156 VectorwiseOp<ExpressionType,Direction>::homogeneous() const
00157 {
00158   return _expression();
00159 }
00160 
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 
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,int Options>
00216 struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> >
00217 {
00218   typedef Transform<Scalar, Dim, Mode, Options> 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, int Options>
00224 struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> >
00225 {
00226   typedef Transform<Scalar, Dim, Projective, Options> 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 remove_all<MatrixType>::type MatrixTypeCleaned;
00236   typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
00237   typedef typename make_proper_matrix_type<
00238                  typename traits<MatrixTypeCleaned>::Scalar,
00239                  LhsMatrixTypeCleaned::RowsAtCompileTime,
00240                  MatrixTypeCleaned::ColsAtCompileTime,
00241                  MatrixTypeCleaned::PlainObject::Options,
00242                  LhsMatrixTypeCleaned::MaxRowsAtCompileTime,
00243                  MatrixTypeCleaned::MaxColsAtCompileTime>::type ReturnType;
00244 };
00245 
00246 template<typename MatrixType,typename Lhs>
00247 struct homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
00248   : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
00249 {
00250   typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
00251   typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
00252   typedef typename remove_all<typename LhsMatrixTypeCleaned::Nested>::type LhsMatrixTypeNested;
00253   typedef typename MatrixType::Index Index;
00254   homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
00255     : m_lhs(take_matrix_for_product<Lhs>::run(lhs)),
00256       m_rhs(rhs)
00257   {}
00258 
00259   inline Index rows() const { return m_lhs.rows(); }
00260   inline Index cols() const { return m_rhs.cols(); }
00261 
00262   template<typename Dest> void evalTo(Dest& dst) const
00263   {
00264     // FIXME investigate how to allow lazy evaluation of this product when possible
00265     dst = Block<const LhsMatrixTypeNested,
00266               LhsMatrixTypeNested::RowsAtCompileTime,
00267               LhsMatrixTypeNested::ColsAtCompileTime==Dynamic?Dynamic:LhsMatrixTypeNested::ColsAtCompileTime-1>
00268             (m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
00269     dst += m_lhs.col(m_lhs.cols()-1).rowwise()
00270             .template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
00271   }
00272 
00273   const typename LhsMatrixTypeCleaned::Nested m_lhs;
00274   const typename MatrixType::Nested m_rhs;
00275 };
00276 
00277 template<typename MatrixType,typename Rhs>
00278 struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
00279 {
00280   typedef typename make_proper_matrix_type<typename traits<MatrixType>::Scalar,
00281                  MatrixType::RowsAtCompileTime,
00282                  Rhs::ColsAtCompileTime,
00283                  MatrixType::PlainObject::Options,
00284                  MatrixType::MaxRowsAtCompileTime,
00285                  Rhs::MaxColsAtCompileTime>::type ReturnType;
00286 };
00287 
00288 template<typename MatrixType,typename Rhs>
00289 struct homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
00290   : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
00291 {
00292   typedef typename remove_all<typename Rhs::Nested>::type RhsNested;
00293   typedef typename MatrixType::Index Index;
00294   homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
00295     : m_lhs(lhs), m_rhs(rhs)
00296   {}
00297 
00298   inline Index rows() const { return m_lhs.rows(); }
00299   inline Index cols() const { return m_rhs.cols(); }
00300 
00301   template<typename Dest> void evalTo(Dest& dst) const
00302   {
00303     // FIXME investigate how to allow lazy evaluation of this product when possible
00304     dst = m_lhs * Block<const RhsNested,
00305                         RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
00306                         RhsNested::ColsAtCompileTime>
00307             (m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
00308     dst += m_rhs.row(m_rhs.rows()-1).colwise()
00309             .template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
00310   }
00311 
00312   const typename MatrixType::Nested m_lhs;
00313   const typename Rhs::Nested m_rhs;
00314 };
00315 
00316 } // end namespace internal
00317 
00318 #endif // EIGEN_HOMOGENEOUS_H


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:46