00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
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 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
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
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 }
00314
00315 #endif // EIGEN_HOMOGENEOUS_H