add efficient matrix product specializations for Homogeneous

This commit is contained in:
Gael Guennebaud 2009-03-05 16:40:56 +00:00
parent 31332fca0b
commit fa9f7708d4
4 changed files with 104 additions and 10 deletions

View File

@ -281,16 +281,16 @@ template<typename ExpressionType, int Direction> class PartialRedux
return Reverse<ExpressionType, Direction>( _expression() );
}
template<int Factor>
const Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
replicate(int factor = Factor) const;
/////////// Geometry module ///////////
const Homogeneous<ExpressionType,Direction> homogeneous() const;
const Replicate<ExpressionType,Direction==Vertical?Dynamic:1,Direction==Horizontal?Dynamic:1>
replicate(int factor) const;
template<int Factor>
const Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
replicate() const;
typedef typename ExpressionType::PlainMatrixType CrossReturnType;
template<typename OtherDerived>

View File

@ -151,9 +151,10 @@ PartialRedux<ExpressionType,Direction>::replicate(int factor) const
template<typename ExpressionType, int Direction>
template<int Factor>
const Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
PartialRedux<ExpressionType,Direction>::replicate() const
PartialRedux<ExpressionType,Direction>::replicate(int factor) const
{
return _expression();
return Replicate<ExpressionType,Direction==Vertical?Factor:1,Direction==Horizontal?Factor:1>
(_expression(),Direction==Vertical?factor:1,Direction==Horizontal?factor:1);
}
#endif // EIGEN_REPLICATE_H

View File

@ -59,6 +59,9 @@ struct ei_traits<Homogeneous<MatrixType,Direction> >
};
};
template<typename MatrixType,typename Lhs> struct ei_homogeneous_left_product_impl;
template<typename MatrixType,typename Rhs> struct ei_homogeneous_right_product_impl;
template<typename MatrixType,int Direction> class Homogeneous
: public MatrixBase<Homogeneous<MatrixType,Direction> >
{
@ -80,6 +83,22 @@ template<typename MatrixType,int Direction> class Homogeneous
return 1;
return m_matrix.coeff(row, col);
}
template<typename Rhs>
inline const ei_homogeneous_right_product_impl<Homogeneous,Rhs>
operator* (const MatrixBase<Rhs>& rhs) const
{
ei_assert(Direction==Horizontal);
return ei_homogeneous_right_product_impl<Homogeneous,Rhs>(m_matrix,rhs.derived());
}
template<typename Lhs> friend
inline const ei_homogeneous_left_product_impl<Homogeneous,Lhs>
operator* (const MatrixBase<Lhs>& lhs, const Homogeneous& rhs)
{
ei_assert(Direction==Vertical);
return ei_homogeneous_left_product_impl<Homogeneous,Lhs>(lhs.derived(),rhs.m_matrix);
}
protected:
const typename MatrixType::Nested m_matrix;
@ -165,4 +184,57 @@ PartialRedux<ExpressionType,Direction>::hnormalized() const
Direction==Horizontal ? _expression().cols()-1 : 1).nestByValue();
}
template<typename MatrixType,typename Lhs>
struct ei_homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
: public ReturnByValue<ei_homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>,
Matrix<typename ei_traits<MatrixType>::Scalar,
Lhs::RowsAtCompileTime,MatrixType::ColsAtCompileTime> >
{
typedef typename ei_cleantype<typename Lhs::Nested>::type LhsNested;
ei_homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
: m_lhs(lhs), m_rhs(rhs)
{}
template<typename Dest> void evalTo(Dest& dst) const
{
// FIXME investigate how to allow lazy evaluation of this product when possible
dst = Block<LhsNested,
LhsNested::RowsAtCompileTime,
LhsNested::ColsAtCompileTime==Dynamic?Dynamic:LhsNested::ColsAtCompileTime-1>
(m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
dst += m_lhs.col(m_lhs.cols()-1).rowwise()
.template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
}
const typename Lhs::Nested m_lhs;
const typename MatrixType::Nested m_rhs;
};
template<typename MatrixType,typename Rhs>
struct ei_homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
: public ReturnByValue<ei_homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>,
Matrix<typename ei_traits<MatrixType>::Scalar,
MatrixType::RowsAtCompileTime, Rhs::ColsAtCompileTime> >
{
typedef typename ei_cleantype<typename Rhs::Nested>::type RhsNested;
ei_homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
: m_lhs(lhs), m_rhs(rhs)
{}
template<typename Dest> void evalTo(Dest& dst) const
{
// FIXME investigate how to allow lazy evaluation of this product when possible
dst = m_lhs * Block<RhsNested,
RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
RhsNested::ColsAtCompileTime>
(m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
dst += m_rhs.row(m_rhs.rows()-1).colwise()
.template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
}
const typename MatrixType::Nested m_lhs;
const typename Rhs::Nested m_rhs;
};
#endif // EIGEN_HOMOGENEOUS_H

View File

@ -36,13 +36,15 @@ template<typename Scalar,int Size> void homogeneous(void)
typedef Matrix<Scalar,Size+1,Size> HMatrixType;
typedef Matrix<Scalar,Size+1,1> HVectorType;
typedef Matrix<Scalar,Size,Size+1> T1MatrixType;
typedef Matrix<Scalar,Size+1,Size+1> T2MatrixType;
typedef Matrix<Scalar,Size+1,Size> T3MatrixType;
Scalar largeEps = test_precision<Scalar>();
if (ei_is_same_type<Scalar,float>::ret)
largeEps = 1e-3f;
Scalar eps = ei_random<Scalar>() * 1e-2;
VectorType v0 = VectorType::Random(),
v1 = VectorType::Random(),
ones = VectorType::Ones();
@ -67,13 +69,32 @@ template<typename Scalar,int Size> void homogeneous(void)
for(int j=0; j<Size; ++j)
m0.col(j) = hm0.col(j).start(Size) / hm0(Size,j);
VERIFY_IS_APPROX(m0, hm0.colwise().hnormalized());
T1MatrixType t1 = T1MatrixType::Random();
VERIFY_IS_APPROX(t1 * (v0.homogeneous().eval()), t1 * v0.homogeneous());
VERIFY_IS_APPROX(t1 * (m0.colwise().homogeneous().eval()), t1 * m0.colwise().homogeneous());
T2MatrixType t2 = T2MatrixType::Random();
VERIFY_IS_APPROX(t2 * (v0.homogeneous().eval()), t2 * v0.homogeneous());
VERIFY_IS_APPROX(t2 * (m0.colwise().homogeneous().eval()), t2 * m0.colwise().homogeneous());
VERIFY_IS_APPROX((v0.transpose().rowwise().homogeneous().eval()) * t2,
v0.transpose().rowwise().homogeneous() * t2);
VERIFY_IS_APPROX((m0.transpose().rowwise().homogeneous().eval()) * t2,
m0.transpose().rowwise().homogeneous() * t2);
T3MatrixType t3 = T3MatrixType::Random();
VERIFY_IS_APPROX((v0.transpose().rowwise().homogeneous().eval()) * t3,
v0.transpose().rowwise().homogeneous() * t3);
VERIFY_IS_APPROX((m0.transpose().rowwise().homogeneous().eval()) * t3,
m0.transpose().rowwise().homogeneous() * t3);
}
void test_geo_homogeneous()
{
for(int i = 0; i < g_repeat; i++) {
CALL_SUBTEST(( homogeneous<float,1>() ));
// CALL_SUBTEST(( homogeneous<float,1>() ));
CALL_SUBTEST(( homogeneous<double,3>() ));
CALL_SUBTEST(( homogeneous<double,8>() ));
// CALL_SUBTEST(( homogeneous<double,8>() ));
}
}