xref: /aosp_15_r20/external/eigen/Eigen/src/Geometry/Homogeneous.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2009-2010 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li 
10*bf2c3715SXin Li #ifndef EIGEN_HOMOGENEOUS_H
11*bf2c3715SXin Li #define EIGEN_HOMOGENEOUS_H
12*bf2c3715SXin Li 
13*bf2c3715SXin Li namespace Eigen {
14*bf2c3715SXin Li 
15*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
16*bf2c3715SXin Li   *
17*bf2c3715SXin Li   * \class Homogeneous
18*bf2c3715SXin Li   *
19*bf2c3715SXin Li   * \brief Expression of one (or a set of) homogeneous vector(s)
20*bf2c3715SXin Li   *
21*bf2c3715SXin Li   * \param MatrixType the type of the object in which we are making homogeneous
22*bf2c3715SXin Li   *
23*bf2c3715SXin Li   * This class represents an expression of one (or a set of) homogeneous vector(s).
24*bf2c3715SXin Li   * It is the return type of MatrixBase::homogeneous() and most of the time
25*bf2c3715SXin Li   * this is the only way it is used.
26*bf2c3715SXin Li   *
27*bf2c3715SXin Li   * \sa MatrixBase::homogeneous()
28*bf2c3715SXin Li   */
29*bf2c3715SXin Li 
30*bf2c3715SXin Li namespace internal {
31*bf2c3715SXin Li 
32*bf2c3715SXin Li template<typename MatrixType,int Direction>
33*bf2c3715SXin Li struct traits<Homogeneous<MatrixType,Direction> >
34*bf2c3715SXin Li  : traits<MatrixType>
35*bf2c3715SXin Li {
36*bf2c3715SXin Li   typedef typename traits<MatrixType>::StorageKind StorageKind;
37*bf2c3715SXin Li   typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
38*bf2c3715SXin Li   typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
39*bf2c3715SXin Li   enum {
40*bf2c3715SXin Li     RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ?
41*bf2c3715SXin Li                   int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
42*bf2c3715SXin Li     ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ?
43*bf2c3715SXin Li                   int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
44*bf2c3715SXin Li     RowsAtCompileTime = Direction==Vertical  ?  RowsPlusOne : MatrixType::RowsAtCompileTime,
45*bf2c3715SXin Li     ColsAtCompileTime = Direction==Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
46*bf2c3715SXin Li     MaxRowsAtCompileTime = RowsAtCompileTime,
47*bf2c3715SXin Li     MaxColsAtCompileTime = ColsAtCompileTime,
48*bf2c3715SXin Li     TmpFlags = _MatrixTypeNested::Flags & HereditaryBits,
49*bf2c3715SXin Li     Flags = ColsAtCompileTime==1 ? (TmpFlags & ~RowMajorBit)
50*bf2c3715SXin Li           : RowsAtCompileTime==1 ? (TmpFlags | RowMajorBit)
51*bf2c3715SXin Li           : TmpFlags
52*bf2c3715SXin Li   };
53*bf2c3715SXin Li };
54*bf2c3715SXin Li 
55*bf2c3715SXin Li template<typename MatrixType,typename Lhs> struct homogeneous_left_product_impl;
56*bf2c3715SXin Li template<typename MatrixType,typename Rhs> struct homogeneous_right_product_impl;
57*bf2c3715SXin Li 
58*bf2c3715SXin Li } // end namespace internal
59*bf2c3715SXin Li 
60*bf2c3715SXin Li template<typename MatrixType,int _Direction> class Homogeneous
61*bf2c3715SXin Li   : public MatrixBase<Homogeneous<MatrixType,_Direction> >, internal::no_assignment_operator
62*bf2c3715SXin Li {
63*bf2c3715SXin Li   public:
64*bf2c3715SXin Li 
65*bf2c3715SXin Li     typedef MatrixType NestedExpression;
66*bf2c3715SXin Li     enum { Direction = _Direction };
67*bf2c3715SXin Li 
68*bf2c3715SXin Li     typedef MatrixBase<Homogeneous> Base;
69*bf2c3715SXin Li     EIGEN_DENSE_PUBLIC_INTERFACE(Homogeneous)
70*bf2c3715SXin Li 
71*bf2c3715SXin Li     EIGEN_DEVICE_FUNC explicit inline Homogeneous(const MatrixType& matrix)
72*bf2c3715SXin Li       : m_matrix(matrix)
73*bf2c3715SXin Li     {}
74*bf2c3715SXin Li 
75*bf2c3715SXin Li     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
76*bf2c3715SXin Li     inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows() + (int(Direction)==Vertical   ? 1 : 0); }
77*bf2c3715SXin Li     EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
78*bf2c3715SXin Li     inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols() + (int(Direction)==Horizontal ? 1 : 0); }
79*bf2c3715SXin Li 
80*bf2c3715SXin Li     EIGEN_DEVICE_FUNC const NestedExpression& nestedExpression() const { return m_matrix; }
81*bf2c3715SXin Li 
82*bf2c3715SXin Li     template<typename Rhs>
83*bf2c3715SXin Li     EIGEN_DEVICE_FUNC inline const Product<Homogeneous,Rhs>
84*bf2c3715SXin Li     operator* (const MatrixBase<Rhs>& rhs) const
85*bf2c3715SXin Li     {
86*bf2c3715SXin Li       eigen_assert(int(Direction)==Horizontal);
87*bf2c3715SXin Li       return Product<Homogeneous,Rhs>(*this,rhs.derived());
88*bf2c3715SXin Li     }
89*bf2c3715SXin Li 
90*bf2c3715SXin Li     template<typename Lhs> friend
91*bf2c3715SXin Li     EIGEN_DEVICE_FUNC inline const Product<Lhs,Homogeneous>
92*bf2c3715SXin Li     operator* (const MatrixBase<Lhs>& lhs, const Homogeneous& rhs)
93*bf2c3715SXin Li     {
94*bf2c3715SXin Li       eigen_assert(int(Direction)==Vertical);
95*bf2c3715SXin Li       return Product<Lhs,Homogeneous>(lhs.derived(),rhs);
96*bf2c3715SXin Li     }
97*bf2c3715SXin Li 
98*bf2c3715SXin Li     template<typename Scalar, int Dim, int Mode, int Options> friend
99*bf2c3715SXin Li     EIGEN_DEVICE_FUNC inline const Product<Transform<Scalar,Dim,Mode,Options>, Homogeneous >
100*bf2c3715SXin Li     operator* (const Transform<Scalar,Dim,Mode,Options>& lhs, const Homogeneous& rhs)
101*bf2c3715SXin Li     {
102*bf2c3715SXin Li       eigen_assert(int(Direction)==Vertical);
103*bf2c3715SXin Li       return Product<Transform<Scalar,Dim,Mode,Options>, Homogeneous>(lhs,rhs);
104*bf2c3715SXin Li     }
105*bf2c3715SXin Li 
106*bf2c3715SXin Li     template<typename Func>
107*bf2c3715SXin Li     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::result_of<Func(Scalar,Scalar)>::type
108*bf2c3715SXin Li     redux(const Func& func) const
109*bf2c3715SXin Li     {
110*bf2c3715SXin Li       return func(m_matrix.redux(func), Scalar(1));
111*bf2c3715SXin Li     }
112*bf2c3715SXin Li 
113*bf2c3715SXin Li   protected:
114*bf2c3715SXin Li     typename MatrixType::Nested m_matrix;
115*bf2c3715SXin Li };
116*bf2c3715SXin Li 
117*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
118*bf2c3715SXin Li   *
119*bf2c3715SXin Li   * \returns a vector expression that is one longer than the vector argument, with the value 1 symbolically appended as the last coefficient.
120*bf2c3715SXin Li   *
121*bf2c3715SXin Li   * This can be used to convert affine coordinates to homogeneous coordinates.
122*bf2c3715SXin Li   *
123*bf2c3715SXin Li   * \only_for_vectors
124*bf2c3715SXin Li   *
125*bf2c3715SXin Li   * Example: \include MatrixBase_homogeneous.cpp
126*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_homogeneous.out
127*bf2c3715SXin Li   *
128*bf2c3715SXin Li   * \sa VectorwiseOp::homogeneous(), class Homogeneous
129*bf2c3715SXin Li   */
130*bf2c3715SXin Li template<typename Derived>
131*bf2c3715SXin Li EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::HomogeneousReturnType
132*bf2c3715SXin Li MatrixBase<Derived>::homogeneous() const
133*bf2c3715SXin Li {
134*bf2c3715SXin Li   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
135*bf2c3715SXin Li   return HomogeneousReturnType(derived());
136*bf2c3715SXin Li }
137*bf2c3715SXin Li 
138*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
139*bf2c3715SXin Li   *
140*bf2c3715SXin Li   * \returns an expression where the value 1 is symbolically appended as the final coefficient to each column (or row) of the matrix.
141*bf2c3715SXin Li   *
142*bf2c3715SXin Li   * This can be used to convert affine coordinates to homogeneous coordinates.
143*bf2c3715SXin Li   *
144*bf2c3715SXin Li   * Example: \include VectorwiseOp_homogeneous.cpp
145*bf2c3715SXin Li   * Output: \verbinclude VectorwiseOp_homogeneous.out
146*bf2c3715SXin Li   *
147*bf2c3715SXin Li   * \sa MatrixBase::homogeneous(), class Homogeneous */
148*bf2c3715SXin Li template<typename ExpressionType, int Direction>
149*bf2c3715SXin Li EIGEN_DEVICE_FUNC inline Homogeneous<ExpressionType,Direction>
150*bf2c3715SXin Li VectorwiseOp<ExpressionType,Direction>::homogeneous() const
151*bf2c3715SXin Li {
152*bf2c3715SXin Li   return HomogeneousReturnType(_expression());
153*bf2c3715SXin Li }
154*bf2c3715SXin Li 
155*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
156*bf2c3715SXin Li   *
157*bf2c3715SXin Li   * \brief homogeneous normalization
158*bf2c3715SXin Li   *
159*bf2c3715SXin Li   * \returns a vector expression of the N-1 first coefficients of \c *this divided by that last coefficient.
160*bf2c3715SXin Li   *
161*bf2c3715SXin Li   * This can be used to convert homogeneous coordinates to affine coordinates.
162*bf2c3715SXin Li   *
163*bf2c3715SXin Li   * It is essentially a shortcut for:
164*bf2c3715SXin Li   * \code
165*bf2c3715SXin Li     this->head(this->size()-1)/this->coeff(this->size()-1);
166*bf2c3715SXin Li     \endcode
167*bf2c3715SXin Li   *
168*bf2c3715SXin Li   * Example: \include MatrixBase_hnormalized.cpp
169*bf2c3715SXin Li   * Output: \verbinclude MatrixBase_hnormalized.out
170*bf2c3715SXin Li   *
171*bf2c3715SXin Li   * \sa VectorwiseOp::hnormalized() */
172*bf2c3715SXin Li template<typename Derived>
173*bf2c3715SXin Li EIGEN_DEVICE_FUNC inline const typename MatrixBase<Derived>::HNormalizedReturnType
174*bf2c3715SXin Li MatrixBase<Derived>::hnormalized() const
175*bf2c3715SXin Li {
176*bf2c3715SXin Li   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
177*bf2c3715SXin Li   return ConstStartMinusOne(derived(),0,0,
178*bf2c3715SXin Li     ColsAtCompileTime==1?size()-1:1,
179*bf2c3715SXin Li     ColsAtCompileTime==1?1:size()-1) / coeff(size()-1);
180*bf2c3715SXin Li }
181*bf2c3715SXin Li 
182*bf2c3715SXin Li /** \geometry_module \ingroup Geometry_Module
183*bf2c3715SXin Li   *
184*bf2c3715SXin Li   * \brief column or row-wise homogeneous normalization
185*bf2c3715SXin Li   *
186*bf2c3715SXin Li   * \returns an expression of the first N-1 coefficients of each column (or row) of \c *this divided by the last coefficient of each column (or row).
187*bf2c3715SXin Li   *
188*bf2c3715SXin Li   * This can be used to convert homogeneous coordinates to affine coordinates.
189*bf2c3715SXin Li   *
190*bf2c3715SXin Li   * It is conceptually equivalent to calling MatrixBase::hnormalized() to each column (or row) of \c *this.
191*bf2c3715SXin Li   *
192*bf2c3715SXin Li   * Example: \include DirectionWise_hnormalized.cpp
193*bf2c3715SXin Li   * Output: \verbinclude DirectionWise_hnormalized.out
194*bf2c3715SXin Li   *
195*bf2c3715SXin Li   * \sa MatrixBase::hnormalized() */
196*bf2c3715SXin Li template<typename ExpressionType, int Direction>
197*bf2c3715SXin Li EIGEN_DEVICE_FUNC inline const typename VectorwiseOp<ExpressionType,Direction>::HNormalizedReturnType
198*bf2c3715SXin Li VectorwiseOp<ExpressionType,Direction>::hnormalized() const
199*bf2c3715SXin Li {
200*bf2c3715SXin Li   return HNormalized_Block(_expression(),0,0,
201*bf2c3715SXin Li       Direction==Vertical   ? _expression().rows()-1 : _expression().rows(),
202*bf2c3715SXin Li       Direction==Horizontal ? _expression().cols()-1 : _expression().cols()).cwiseQuotient(
203*bf2c3715SXin Li       Replicate<HNormalized_Factors,
204*bf2c3715SXin Li                 Direction==Vertical   ? HNormalized_SizeMinusOne : 1,
205*bf2c3715SXin Li                 Direction==Horizontal ? HNormalized_SizeMinusOne : 1>
206*bf2c3715SXin Li         (HNormalized_Factors(_expression(),
207*bf2c3715SXin Li           Direction==Vertical    ? _expression().rows()-1:0,
208*bf2c3715SXin Li           Direction==Horizontal  ? _expression().cols()-1:0,
209*bf2c3715SXin Li           Direction==Vertical    ? 1 : _expression().rows(),
210*bf2c3715SXin Li           Direction==Horizontal  ? 1 : _expression().cols()),
211*bf2c3715SXin Li          Direction==Vertical   ? _expression().rows()-1 : 1,
212*bf2c3715SXin Li          Direction==Horizontal ? _expression().cols()-1 : 1));
213*bf2c3715SXin Li }
214*bf2c3715SXin Li 
215*bf2c3715SXin Li namespace internal {
216*bf2c3715SXin Li 
217*bf2c3715SXin Li template<typename MatrixOrTransformType>
218*bf2c3715SXin Li struct take_matrix_for_product
219*bf2c3715SXin Li {
220*bf2c3715SXin Li   typedef MatrixOrTransformType type;
221*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static const type& run(const type &x) { return x; }
222*bf2c3715SXin Li };
223*bf2c3715SXin Li 
224*bf2c3715SXin Li template<typename Scalar, int Dim, int Mode,int Options>
225*bf2c3715SXin Li struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> >
226*bf2c3715SXin Li {
227*bf2c3715SXin Li   typedef Transform<Scalar, Dim, Mode, Options> TransformType;
228*bf2c3715SXin Li   typedef typename internal::add_const<typename TransformType::ConstAffinePart>::type type;
229*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static type run (const TransformType& x) { return x.affine(); }
230*bf2c3715SXin Li };
231*bf2c3715SXin Li 
232*bf2c3715SXin Li template<typename Scalar, int Dim, int Options>
233*bf2c3715SXin Li struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> >
234*bf2c3715SXin Li {
235*bf2c3715SXin Li   typedef Transform<Scalar, Dim, Projective, Options> TransformType;
236*bf2c3715SXin Li   typedef typename TransformType::MatrixType type;
237*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static const type& run (const TransformType& x) { return x.matrix(); }
238*bf2c3715SXin Li };
239*bf2c3715SXin Li 
240*bf2c3715SXin Li template<typename MatrixType,typename Lhs>
241*bf2c3715SXin Li struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
242*bf2c3715SXin Li {
243*bf2c3715SXin Li   typedef typename take_matrix_for_product<Lhs>::type LhsMatrixType;
244*bf2c3715SXin Li   typedef typename remove_all<MatrixType>::type MatrixTypeCleaned;
245*bf2c3715SXin Li   typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
246*bf2c3715SXin Li   typedef typename make_proper_matrix_type<
247*bf2c3715SXin Li                  typename traits<MatrixTypeCleaned>::Scalar,
248*bf2c3715SXin Li                  LhsMatrixTypeCleaned::RowsAtCompileTime,
249*bf2c3715SXin Li                  MatrixTypeCleaned::ColsAtCompileTime,
250*bf2c3715SXin Li                  MatrixTypeCleaned::PlainObject::Options,
251*bf2c3715SXin Li                  LhsMatrixTypeCleaned::MaxRowsAtCompileTime,
252*bf2c3715SXin Li                  MatrixTypeCleaned::MaxColsAtCompileTime>::type ReturnType;
253*bf2c3715SXin Li };
254*bf2c3715SXin Li 
255*bf2c3715SXin Li template<typename MatrixType,typename Lhs>
256*bf2c3715SXin Li struct homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
257*bf2c3715SXin Li   : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
258*bf2c3715SXin Li {
259*bf2c3715SXin Li   typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
260*bf2c3715SXin Li   typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
261*bf2c3715SXin Li   typedef typename remove_all<typename LhsMatrixTypeCleaned::Nested>::type LhsMatrixTypeNested;
262*bf2c3715SXin Li   EIGEN_DEVICE_FUNC homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
263*bf2c3715SXin Li     : m_lhs(take_matrix_for_product<Lhs>::run(lhs)),
264*bf2c3715SXin Li       m_rhs(rhs)
265*bf2c3715SXin Li   {}
266*bf2c3715SXin Li 
267*bf2c3715SXin Li   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
268*bf2c3715SXin Li   inline Index rows() const EIGEN_NOEXCEPT { return m_lhs.rows(); }
269*bf2c3715SXin Li   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
270*bf2c3715SXin Li   inline Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
271*bf2c3715SXin Li 
272*bf2c3715SXin Li   template<typename Dest> EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const
273*bf2c3715SXin Li   {
274*bf2c3715SXin Li     // FIXME investigate how to allow lazy evaluation of this product when possible
275*bf2c3715SXin Li     dst = Block<const LhsMatrixTypeNested,
276*bf2c3715SXin Li               LhsMatrixTypeNested::RowsAtCompileTime,
277*bf2c3715SXin Li               LhsMatrixTypeNested::ColsAtCompileTime==Dynamic?Dynamic:LhsMatrixTypeNested::ColsAtCompileTime-1>
278*bf2c3715SXin Li             (m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
279*bf2c3715SXin Li     dst += m_lhs.col(m_lhs.cols()-1).rowwise()
280*bf2c3715SXin Li             .template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
281*bf2c3715SXin Li   }
282*bf2c3715SXin Li 
283*bf2c3715SXin Li   typename LhsMatrixTypeCleaned::Nested m_lhs;
284*bf2c3715SXin Li   typename MatrixType::Nested m_rhs;
285*bf2c3715SXin Li };
286*bf2c3715SXin Li 
287*bf2c3715SXin Li template<typename MatrixType,typename Rhs>
288*bf2c3715SXin Li struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
289*bf2c3715SXin Li {
290*bf2c3715SXin Li   typedef typename make_proper_matrix_type<typename traits<MatrixType>::Scalar,
291*bf2c3715SXin Li                  MatrixType::RowsAtCompileTime,
292*bf2c3715SXin Li                  Rhs::ColsAtCompileTime,
293*bf2c3715SXin Li                  MatrixType::PlainObject::Options,
294*bf2c3715SXin Li                  MatrixType::MaxRowsAtCompileTime,
295*bf2c3715SXin Li                  Rhs::MaxColsAtCompileTime>::type ReturnType;
296*bf2c3715SXin Li };
297*bf2c3715SXin Li 
298*bf2c3715SXin Li template<typename MatrixType,typename Rhs>
299*bf2c3715SXin Li struct homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
300*bf2c3715SXin Li   : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
301*bf2c3715SXin Li {
302*bf2c3715SXin Li   typedef typename remove_all<typename Rhs::Nested>::type RhsNested;
303*bf2c3715SXin Li   EIGEN_DEVICE_FUNC homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
304*bf2c3715SXin Li     : m_lhs(lhs), m_rhs(rhs)
305*bf2c3715SXin Li   {}
306*bf2c3715SXin Li 
307*bf2c3715SXin Li   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rows() const EIGEN_NOEXCEPT { return m_lhs.rows(); }
308*bf2c3715SXin Li   EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
309*bf2c3715SXin Li 
310*bf2c3715SXin Li   template<typename Dest> EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const
311*bf2c3715SXin Li   {
312*bf2c3715SXin Li     // FIXME investigate how to allow lazy evaluation of this product when possible
313*bf2c3715SXin Li     dst = m_lhs * Block<const RhsNested,
314*bf2c3715SXin Li                         RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
315*bf2c3715SXin Li                         RhsNested::ColsAtCompileTime>
316*bf2c3715SXin Li             (m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
317*bf2c3715SXin Li     dst += m_rhs.row(m_rhs.rows()-1).colwise()
318*bf2c3715SXin Li             .template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
319*bf2c3715SXin Li   }
320*bf2c3715SXin Li 
321*bf2c3715SXin Li   typename MatrixType::Nested m_lhs;
322*bf2c3715SXin Li   typename Rhs::Nested m_rhs;
323*bf2c3715SXin Li };
324*bf2c3715SXin Li 
325*bf2c3715SXin Li template<typename ArgType,int Direction>
326*bf2c3715SXin Li struct evaluator_traits<Homogeneous<ArgType,Direction> >
327*bf2c3715SXin Li {
328*bf2c3715SXin Li   typedef typename storage_kind_to_evaluator_kind<typename ArgType::StorageKind>::Kind Kind;
329*bf2c3715SXin Li   typedef HomogeneousShape Shape;
330*bf2c3715SXin Li };
331*bf2c3715SXin Li 
332*bf2c3715SXin Li template<> struct AssignmentKind<DenseShape,HomogeneousShape> { typedef Dense2Dense Kind; };
333*bf2c3715SXin Li 
334*bf2c3715SXin Li 
335*bf2c3715SXin Li template<typename ArgType,int Direction>
336*bf2c3715SXin Li struct unary_evaluator<Homogeneous<ArgType,Direction>, IndexBased>
337*bf2c3715SXin Li   : evaluator<typename Homogeneous<ArgType,Direction>::PlainObject >
338*bf2c3715SXin Li {
339*bf2c3715SXin Li   typedef Homogeneous<ArgType,Direction> XprType;
340*bf2c3715SXin Li   typedef typename XprType::PlainObject PlainObject;
341*bf2c3715SXin Li   typedef evaluator<PlainObject> Base;
342*bf2c3715SXin Li 
343*bf2c3715SXin Li   EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& op)
344*bf2c3715SXin Li     : Base(), m_temp(op)
345*bf2c3715SXin Li   {
346*bf2c3715SXin Li     ::new (static_cast<Base*>(this)) Base(m_temp);
347*bf2c3715SXin Li   }
348*bf2c3715SXin Li 
349*bf2c3715SXin Li protected:
350*bf2c3715SXin Li   PlainObject m_temp;
351*bf2c3715SXin Li };
352*bf2c3715SXin Li 
353*bf2c3715SXin Li // dense = homogeneous
354*bf2c3715SXin Li template< typename DstXprType, typename ArgType, typename Scalar>
355*bf2c3715SXin Li struct Assignment<DstXprType, Homogeneous<ArgType,Vertical>, internal::assign_op<Scalar,typename ArgType::Scalar>, Dense2Dense>
356*bf2c3715SXin Li {
357*bf2c3715SXin Li   typedef Homogeneous<ArgType,Vertical> SrcXprType;
358*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,typename ArgType::Scalar> &)
359*bf2c3715SXin Li   {
360*bf2c3715SXin Li     Index dstRows = src.rows();
361*bf2c3715SXin Li     Index dstCols = src.cols();
362*bf2c3715SXin Li     if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
363*bf2c3715SXin Li       dst.resize(dstRows, dstCols);
364*bf2c3715SXin Li 
365*bf2c3715SXin Li     dst.template topRows<ArgType::RowsAtCompileTime>(src.nestedExpression().rows()) = src.nestedExpression();
366*bf2c3715SXin Li     dst.row(dst.rows()-1).setOnes();
367*bf2c3715SXin Li   }
368*bf2c3715SXin Li };
369*bf2c3715SXin Li 
370*bf2c3715SXin Li // dense = homogeneous
371*bf2c3715SXin Li template< typename DstXprType, typename ArgType, typename Scalar>
372*bf2c3715SXin Li struct Assignment<DstXprType, Homogeneous<ArgType,Horizontal>, internal::assign_op<Scalar,typename ArgType::Scalar>, Dense2Dense>
373*bf2c3715SXin Li {
374*bf2c3715SXin Li   typedef Homogeneous<ArgType,Horizontal> SrcXprType;
375*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,typename ArgType::Scalar> &)
376*bf2c3715SXin Li   {
377*bf2c3715SXin Li     Index dstRows = src.rows();
378*bf2c3715SXin Li     Index dstCols = src.cols();
379*bf2c3715SXin Li     if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
380*bf2c3715SXin Li       dst.resize(dstRows, dstCols);
381*bf2c3715SXin Li 
382*bf2c3715SXin Li     dst.template leftCols<ArgType::ColsAtCompileTime>(src.nestedExpression().cols()) = src.nestedExpression();
383*bf2c3715SXin Li     dst.col(dst.cols()-1).setOnes();
384*bf2c3715SXin Li   }
385*bf2c3715SXin Li };
386*bf2c3715SXin Li 
387*bf2c3715SXin Li template<typename LhsArg, typename Rhs, int ProductTag>
388*bf2c3715SXin Li struct generic_product_impl<Homogeneous<LhsArg,Horizontal>, Rhs, HomogeneousShape, DenseShape, ProductTag>
389*bf2c3715SXin Li {
390*bf2c3715SXin Li   template<typename Dest>
391*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Homogeneous<LhsArg,Horizontal>& lhs, const Rhs& rhs)
392*bf2c3715SXin Li   {
393*bf2c3715SXin Li     homogeneous_right_product_impl<Homogeneous<LhsArg,Horizontal>, Rhs>(lhs.nestedExpression(), rhs).evalTo(dst);
394*bf2c3715SXin Li   }
395*bf2c3715SXin Li };
396*bf2c3715SXin Li 
397*bf2c3715SXin Li template<typename Lhs,typename Rhs>
398*bf2c3715SXin Li struct homogeneous_right_product_refactoring_helper
399*bf2c3715SXin Li {
400*bf2c3715SXin Li   enum {
401*bf2c3715SXin Li     Dim  = Lhs::ColsAtCompileTime,
402*bf2c3715SXin Li     Rows = Lhs::RowsAtCompileTime
403*bf2c3715SXin Li   };
404*bf2c3715SXin Li   typedef typename Rhs::template ConstNRowsBlockXpr<Dim>::Type          LinearBlockConst;
405*bf2c3715SXin Li   typedef typename remove_const<LinearBlockConst>::type                 LinearBlock;
406*bf2c3715SXin Li   typedef typename Rhs::ConstRowXpr                                     ConstantColumn;
407*bf2c3715SXin Li   typedef Replicate<const ConstantColumn,Rows,1>                        ConstantBlock;
408*bf2c3715SXin Li   typedef Product<Lhs,LinearBlock,LazyProduct>                          LinearProduct;
409*bf2c3715SXin Li   typedef CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar,typename Rhs::Scalar>, const LinearProduct, const ConstantBlock> Xpr;
410*bf2c3715SXin Li };
411*bf2c3715SXin Li 
412*bf2c3715SXin Li template<typename Lhs, typename Rhs, int ProductTag>
413*bf2c3715SXin Li struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, HomogeneousShape, DenseShape>
414*bf2c3715SXin Li  : public evaluator<typename homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression,Rhs>::Xpr>
415*bf2c3715SXin Li {
416*bf2c3715SXin Li   typedef Product<Lhs, Rhs, LazyProduct> XprType;
417*bf2c3715SXin Li   typedef homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression,Rhs> helper;
418*bf2c3715SXin Li   typedef typename helper::ConstantBlock ConstantBlock;
419*bf2c3715SXin Li   typedef typename helper::Xpr RefactoredXpr;
420*bf2c3715SXin Li   typedef evaluator<RefactoredXpr> Base;
421*bf2c3715SXin Li 
422*bf2c3715SXin Li   EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
423*bf2c3715SXin Li     : Base(  xpr.lhs().nestedExpression() .lazyProduct(  xpr.rhs().template topRows<helper::Dim>(xpr.lhs().nestedExpression().cols()) )
424*bf2c3715SXin Li             + ConstantBlock(xpr.rhs().row(xpr.rhs().rows()-1),xpr.lhs().rows(), 1) )
425*bf2c3715SXin Li   {}
426*bf2c3715SXin Li };
427*bf2c3715SXin Li 
428*bf2c3715SXin Li template<typename Lhs, typename RhsArg, int ProductTag>
429*bf2c3715SXin Li struct generic_product_impl<Lhs, Homogeneous<RhsArg,Vertical>, DenseShape, HomogeneousShape, ProductTag>
430*bf2c3715SXin Li {
431*bf2c3715SXin Li   template<typename Dest>
432*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg,Vertical>& rhs)
433*bf2c3715SXin Li   {
434*bf2c3715SXin Li     homogeneous_left_product_impl<Homogeneous<RhsArg,Vertical>, Lhs>(lhs, rhs.nestedExpression()).evalTo(dst);
435*bf2c3715SXin Li   }
436*bf2c3715SXin Li };
437*bf2c3715SXin Li 
438*bf2c3715SXin Li // TODO: the following specialization is to address a regression from 3.2 to 3.3
439*bf2c3715SXin Li // In the future, this path should be optimized.
440*bf2c3715SXin Li template<typename Lhs, typename RhsArg, int ProductTag>
441*bf2c3715SXin Li struct generic_product_impl<Lhs, Homogeneous<RhsArg,Vertical>, TriangularShape, HomogeneousShape, ProductTag>
442*bf2c3715SXin Li {
443*bf2c3715SXin Li   template<typename Dest>
444*bf2c3715SXin Li   static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg,Vertical>& rhs)
445*bf2c3715SXin Li   {
446*bf2c3715SXin Li     dst.noalias() = lhs * rhs.eval();
447*bf2c3715SXin Li   }
448*bf2c3715SXin Li };
449*bf2c3715SXin Li 
450*bf2c3715SXin Li template<typename Lhs,typename Rhs>
451*bf2c3715SXin Li struct homogeneous_left_product_refactoring_helper
452*bf2c3715SXin Li {
453*bf2c3715SXin Li   enum {
454*bf2c3715SXin Li     Dim = Rhs::RowsAtCompileTime,
455*bf2c3715SXin Li     Cols = Rhs::ColsAtCompileTime
456*bf2c3715SXin Li   };
457*bf2c3715SXin Li   typedef typename Lhs::template ConstNColsBlockXpr<Dim>::Type          LinearBlockConst;
458*bf2c3715SXin Li   typedef typename remove_const<LinearBlockConst>::type                 LinearBlock;
459*bf2c3715SXin Li   typedef typename Lhs::ConstColXpr                                     ConstantColumn;
460*bf2c3715SXin Li   typedef Replicate<const ConstantColumn,1,Cols>                        ConstantBlock;
461*bf2c3715SXin Li   typedef Product<LinearBlock,Rhs,LazyProduct>                          LinearProduct;
462*bf2c3715SXin Li   typedef CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar,typename Rhs::Scalar>, const LinearProduct, const ConstantBlock> Xpr;
463*bf2c3715SXin Li };
464*bf2c3715SXin Li 
465*bf2c3715SXin Li template<typename Lhs, typename Rhs, int ProductTag>
466*bf2c3715SXin Li struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, HomogeneousShape>
467*bf2c3715SXin Li  : public evaluator<typename homogeneous_left_product_refactoring_helper<Lhs,typename Rhs::NestedExpression>::Xpr>
468*bf2c3715SXin Li {
469*bf2c3715SXin Li   typedef Product<Lhs, Rhs, LazyProduct> XprType;
470*bf2c3715SXin Li   typedef homogeneous_left_product_refactoring_helper<Lhs,typename Rhs::NestedExpression> helper;
471*bf2c3715SXin Li   typedef typename helper::ConstantBlock ConstantBlock;
472*bf2c3715SXin Li   typedef typename helper::Xpr RefactoredXpr;
473*bf2c3715SXin Li   typedef evaluator<RefactoredXpr> Base;
474*bf2c3715SXin Li 
475*bf2c3715SXin Li   EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
476*bf2c3715SXin Li     : Base(   xpr.lhs().template leftCols<helper::Dim>(xpr.rhs().nestedExpression().rows()) .lazyProduct( xpr.rhs().nestedExpression() )
477*bf2c3715SXin Li             + ConstantBlock(xpr.lhs().col(xpr.lhs().cols()-1),1,xpr.rhs().cols()) )
478*bf2c3715SXin Li   {}
479*bf2c3715SXin Li };
480*bf2c3715SXin Li 
481*bf2c3715SXin Li template<typename Scalar, int Dim, int Mode,int Options, typename RhsArg, int ProductTag>
482*bf2c3715SXin Li struct generic_product_impl<Transform<Scalar,Dim,Mode,Options>, Homogeneous<RhsArg,Vertical>, DenseShape, HomogeneousShape, ProductTag>
483*bf2c3715SXin Li {
484*bf2c3715SXin Li   typedef Transform<Scalar,Dim,Mode,Options> TransformType;
485*bf2c3715SXin Li   template<typename Dest>
486*bf2c3715SXin Li   EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const TransformType& lhs, const Homogeneous<RhsArg,Vertical>& rhs)
487*bf2c3715SXin Li   {
488*bf2c3715SXin Li     homogeneous_left_product_impl<Homogeneous<RhsArg,Vertical>, TransformType>(lhs, rhs.nestedExpression()).evalTo(dst);
489*bf2c3715SXin Li   }
490*bf2c3715SXin Li };
491*bf2c3715SXin Li 
492*bf2c3715SXin Li template<typename ExpressionType, int Side, bool Transposed>
493*bf2c3715SXin Li struct permutation_matrix_product<ExpressionType, Side, Transposed, HomogeneousShape>
494*bf2c3715SXin Li   : public permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape>
495*bf2c3715SXin Li {};
496*bf2c3715SXin Li 
497*bf2c3715SXin Li } // end namespace internal
498*bf2c3715SXin Li 
499*bf2c3715SXin Li } // end namespace Eigen
500*bf2c3715SXin Li 
501*bf2c3715SXin Li #endif // EIGEN_HOMOGENEOUS_H
502