Homogeneous.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_HOMOGENEOUS_H
11 #define EIGEN_HOMOGENEOUS_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
32 namespace internal {
33 
34 template<typename MatrixType,int Direction>
35 struct traits<Homogeneous<MatrixType,Direction> >
36  : traits<MatrixType>
37 {
38  typedef typename traits<MatrixType>::StorageKind StorageKind;
39  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
40  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
41  enum {
42  RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ?
43  int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
44  ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ?
45  int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
46  RowsAtCompileTime = Direction==Vertical ? RowsPlusOne : MatrixType::RowsAtCompileTime,
47  ColsAtCompileTime = Direction==Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
48  MaxRowsAtCompileTime = RowsAtCompileTime,
49  MaxColsAtCompileTime = ColsAtCompileTime,
50  TmpFlags = MatrixTypeNested_::Flags & HereditaryBits,
51  Flags = ColsAtCompileTime==1 ? (TmpFlags & ~RowMajorBit)
52  : RowsAtCompileTime==1 ? (TmpFlags | RowMajorBit)
53  : TmpFlags
54  };
55 };
56 
57 template<typename MatrixType,typename Lhs> struct homogeneous_left_product_impl;
58 template<typename MatrixType,typename Rhs> struct homogeneous_right_product_impl;
59 
60 } // end namespace internal
61 
62 template<typename MatrixType,int Direction_> class Homogeneous
63  : public MatrixBase<Homogeneous<MatrixType,Direction_> >, internal::no_assignment_operator
64 {
65  public:
66 
68  enum { Direction = Direction_ };
69 
72 
74  : m_matrix(matrix)
75  {}
76 
78  inline Index rows() const EIGEN_NOEXCEPT { return m_matrix.rows() + (int(Direction)==Vertical ? 1 : 0); }
80  inline Index cols() const EIGEN_NOEXCEPT { return m_matrix.cols() + (int(Direction)==Horizontal ? 1 : 0); }
81 
83 
84  template<typename Rhs>
86  operator* (const MatrixBase<Rhs>& rhs) const
87  {
89  return Product<Homogeneous,Rhs>(*this,rhs.derived());
90  }
91 
92  template<typename Lhs> friend
94  operator* (const MatrixBase<Lhs>& lhs, const Homogeneous& rhs)
95  {
97  return Product<Lhs,Homogeneous>(lhs.derived(),rhs);
98  }
99 
100  template<typename Scalar, int Dim, int Mode, int Options> friend
103  {
106  }
107 
108  template<typename Func>
109  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::result_of<Func(Scalar,Scalar)>::type
110  redux(const Func& func) const
111  {
112  return func(m_matrix.redux(func), Scalar(1));
113  }
114 
115  protected:
116  typename MatrixType::Nested m_matrix;
117 };
118 
132 template<typename Derived>
135 {
137  return HomogeneousReturnType(derived());
138 }
139 
150 template<typename ExpressionType, int Direction>
153 {
154  return HomogeneousReturnType(_expression());
155 }
156 
174 template<typename Derived>
177 {
179  return ConstStartMinusOne(derived(),0,0,
180  ColsAtCompileTime==1?size()-1:1,
181  ColsAtCompileTime==1?1:size()-1) / coeff(size()-1);
182 }
183 
198 template<typename ExpressionType, int Direction>
201 {
202  return HNormalized_Block(_expression(),0,0,
203  Direction==Vertical ? _expression().rows()-1 : _expression().rows(),
204  Direction==Horizontal ? _expression().cols()-1 : _expression().cols()).cwiseQuotient(
206  Direction==Vertical ? HNormalized_SizeMinusOne : 1,
207  Direction==Horizontal ? HNormalized_SizeMinusOne : 1>
208  (HNormalized_Factors(_expression(),
209  Direction==Vertical ? _expression().rows()-1:0,
210  Direction==Horizontal ? _expression().cols()-1:0,
211  Direction==Vertical ? 1 : _expression().rows(),
212  Direction==Horizontal ? 1 : _expression().cols()),
213  Direction==Vertical ? _expression().rows()-1 : 1,
214  Direction==Horizontal ? _expression().cols()-1 : 1));
215 }
216 
217 namespace internal {
218 
219 template<typename MatrixOrTransformType>
220 struct take_matrix_for_product
221 {
222  typedef MatrixOrTransformType type;
223  EIGEN_DEVICE_FUNC static const type& run(const type &x) { return x; }
224 };
225 
226 template<typename Scalar, int Dim, int Mode,int Options>
227 struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> >
228 {
229  typedef Transform<Scalar, Dim, Mode, Options> TransformType;
230  typedef std::add_const_t<typename TransformType::ConstAffinePart> type;
231  EIGEN_DEVICE_FUNC static type run (const TransformType& x) { return x.affine(); }
232 };
233 
234 template<typename Scalar, int Dim, int Options>
235 struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> >
236 {
237  typedef Transform<Scalar, Dim, Projective, Options> TransformType;
238  typedef typename TransformType::MatrixType type;
239  EIGEN_DEVICE_FUNC static const type& run (const TransformType& x) { return x.matrix(); }
240 };
241 
242 template<typename MatrixType,typename Lhs>
243 struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
244 {
245  typedef typename take_matrix_for_product<Lhs>::type LhsMatrixType;
246  typedef remove_all_t<MatrixType> MatrixTypeCleaned;
247  typedef remove_all_t<LhsMatrixType> LhsMatrixTypeCleaned;
248  typedef typename make_proper_matrix_type<
249  typename traits<MatrixTypeCleaned>::Scalar,
250  LhsMatrixTypeCleaned::RowsAtCompileTime,
251  MatrixTypeCleaned::ColsAtCompileTime,
252  MatrixTypeCleaned::PlainObject::Options,
253  LhsMatrixTypeCleaned::MaxRowsAtCompileTime,
254  MatrixTypeCleaned::MaxColsAtCompileTime>::type ReturnType;
255 };
256 
257 template<typename MatrixType,typename Lhs>
258 struct homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
259  : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
260 {
261  typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
262  typedef remove_all_t<LhsMatrixType> LhsMatrixTypeCleaned;
263  typedef remove_all_t<typename LhsMatrixTypeCleaned::Nested> LhsMatrixTypeNested;
264  EIGEN_DEVICE_FUNC homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
265  : m_lhs(take_matrix_for_product<Lhs>::run(lhs)),
266  m_rhs(rhs)
267  {}
268 
270  inline Index rows() const EIGEN_NOEXCEPT { return m_lhs.rows(); }
272  inline Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
273 
274  template<typename Dest> EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const
275  {
276  // FIXME investigate how to allow lazy evaluation of this product when possible
277  dst = Block<const LhsMatrixTypeNested,
278  LhsMatrixTypeNested::RowsAtCompileTime,
279  LhsMatrixTypeNested::ColsAtCompileTime==Dynamic?Dynamic:LhsMatrixTypeNested::ColsAtCompileTime-1>
280  (m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
281  dst += m_lhs.col(m_lhs.cols()-1).rowwise()
282  .template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
283  }
284 
285  typename LhsMatrixTypeCleaned::Nested m_lhs;
286  typename MatrixType::Nested m_rhs;
287 };
288 
289 template<typename MatrixType,typename Rhs>
290 struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
291 {
292  typedef typename make_proper_matrix_type<typename traits<MatrixType>::Scalar,
293  MatrixType::RowsAtCompileTime,
294  Rhs::ColsAtCompileTime,
295  MatrixType::PlainObject::Options,
296  MatrixType::MaxRowsAtCompileTime,
297  Rhs::MaxColsAtCompileTime>::type ReturnType;
298 };
299 
300 template<typename MatrixType,typename Rhs>
301 struct homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
302  : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
303 {
304  typedef remove_all_t<typename Rhs::Nested> RhsNested;
305  EIGEN_DEVICE_FUNC homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
306  : m_lhs(lhs), m_rhs(rhs)
307  {}
308 
309  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index rows() const EIGEN_NOEXCEPT { return m_lhs.rows(); }
310  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR inline Index cols() const EIGEN_NOEXCEPT { return m_rhs.cols(); }
311 
312  template<typename Dest> EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const
313  {
314  // FIXME investigate how to allow lazy evaluation of this product when possible
315  dst = m_lhs * Block<const RhsNested,
316  RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
317  RhsNested::ColsAtCompileTime>
318  (m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
319  dst += m_rhs.row(m_rhs.rows()-1).colwise()
320  .template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
321  }
322 
323  typename MatrixType::Nested m_lhs;
324  typename Rhs::Nested m_rhs;
325 };
326 
327 template<typename ArgType,int Direction>
328 struct evaluator_traits<Homogeneous<ArgType,Direction> >
329 {
330  typedef typename storage_kind_to_evaluator_kind<typename ArgType::StorageKind>::Kind Kind;
331  typedef HomogeneousShape Shape;
332 };
333 
334 template<> struct AssignmentKind<DenseShape,HomogeneousShape> { typedef Dense2Dense Kind; };
335 
336 
337 template<typename ArgType,int Direction>
338 struct unary_evaluator<Homogeneous<ArgType,Direction>, IndexBased>
339  : evaluator<typename Homogeneous<ArgType,Direction>::PlainObject >
340 {
341  typedef Homogeneous<ArgType,Direction> XprType;
342  typedef typename XprType::PlainObject PlainObject;
343  typedef evaluator<PlainObject> Base;
344 
345  EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& op)
346  : Base(), m_temp(op)
347  {
348  internal::construct_at<Base>(this, m_temp);
349  }
350 
351 protected:
352  PlainObject m_temp;
353 };
354 
355 // dense = homogeneous
356 template< typename DstXprType, typename ArgType, typename Scalar>
357 struct Assignment<DstXprType, Homogeneous<ArgType,Vertical>, internal::assign_op<Scalar,typename ArgType::Scalar>, Dense2Dense>
358 {
359  typedef Homogeneous<ArgType,Vertical> SrcXprType;
360  EIGEN_DEVICE_FUNC static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,typename ArgType::Scalar> &)
361  {
362  Index dstRows = src.rows();
363  Index dstCols = src.cols();
364  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
365  dst.resize(dstRows, dstCols);
366 
367  dst.template topRows<ArgType::RowsAtCompileTime>(src.nestedExpression().rows()) = src.nestedExpression();
368  dst.row(dst.rows()-1).setOnes();
369  }
370 };
371 
372 // dense = homogeneous
373 template< typename DstXprType, typename ArgType, typename Scalar>
374 struct Assignment<DstXprType, Homogeneous<ArgType,Horizontal>, internal::assign_op<Scalar,typename ArgType::Scalar>, Dense2Dense>
375 {
376  typedef Homogeneous<ArgType,Horizontal> SrcXprType;
377  EIGEN_DEVICE_FUNC static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op<Scalar,typename ArgType::Scalar> &)
378  {
379  Index dstRows = src.rows();
380  Index dstCols = src.cols();
381  if((dst.rows()!=dstRows) || (dst.cols()!=dstCols))
382  dst.resize(dstRows, dstCols);
383 
384  dst.template leftCols<ArgType::ColsAtCompileTime>(src.nestedExpression().cols()) = src.nestedExpression();
385  dst.col(dst.cols()-1).setOnes();
386  }
387 };
388 
389 template<typename LhsArg, typename Rhs, int ProductTag>
390 struct generic_product_impl<Homogeneous<LhsArg,Horizontal>, Rhs, HomogeneousShape, DenseShape, ProductTag>
391 {
392  template<typename Dest>
393  EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Homogeneous<LhsArg,Horizontal>& lhs, const Rhs& rhs)
394  {
395  homogeneous_right_product_impl<Homogeneous<LhsArg,Horizontal>, Rhs>(lhs.nestedExpression(), rhs).evalTo(dst);
396  }
397 };
398 
399 template<typename Lhs,typename Rhs>
400 struct homogeneous_right_product_refactoring_helper
401 {
402  enum {
403  Dim = Lhs::ColsAtCompileTime,
404  Rows = Lhs::RowsAtCompileTime
405  };
406  typedef typename Rhs::template ConstNRowsBlockXpr<Dim>::Type LinearBlockConst;
407  typedef std::remove_const_t<LinearBlockConst> LinearBlock;
408  typedef typename Rhs::ConstRowXpr ConstantColumn;
409  typedef Replicate<const ConstantColumn,Rows,1> ConstantBlock;
410  typedef Product<Lhs,LinearBlock,LazyProduct> LinearProduct;
411  typedef CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar,typename Rhs::Scalar>, const LinearProduct, const ConstantBlock> Xpr;
412 };
413 
414 template<typename Lhs, typename Rhs, int ProductTag>
415 struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, HomogeneousShape, DenseShape>
416  : public evaluator<typename homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression,Rhs>::Xpr>
417 {
418  typedef Product<Lhs, Rhs, LazyProduct> XprType;
419  typedef homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression,Rhs> helper;
420  typedef typename helper::ConstantBlock ConstantBlock;
421  typedef typename helper::Xpr RefactoredXpr;
422  typedef evaluator<RefactoredXpr> Base;
423 
424  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
425  : Base( xpr.lhs().nestedExpression() .lazyProduct( xpr.rhs().template topRows<helper::Dim>(xpr.lhs().nestedExpression().cols()) )
426  + ConstantBlock(xpr.rhs().row(xpr.rhs().rows()-1),xpr.lhs().rows(), 1) )
427  {}
428 };
429 
430 template<typename Lhs, typename RhsArg, int ProductTag>
431 struct generic_product_impl<Lhs, Homogeneous<RhsArg,Vertical>, DenseShape, HomogeneousShape, ProductTag>
432 {
433  template<typename Dest>
434  EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg,Vertical>& rhs)
435  {
436  homogeneous_left_product_impl<Homogeneous<RhsArg,Vertical>, Lhs>(lhs, rhs.nestedExpression()).evalTo(dst);
437  }
438 };
439 
440 // TODO: the following specialization is to address a regression from 3.2 to 3.3
441 // In the future, this path should be optimized.
442 template<typename Lhs, typename RhsArg, int ProductTag>
443 struct generic_product_impl<Lhs, Homogeneous<RhsArg,Vertical>, TriangularShape, HomogeneousShape, ProductTag>
444 {
445  template<typename Dest>
446  static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg,Vertical>& rhs)
447  {
448  dst.noalias() = lhs * rhs.eval();
449  }
450 };
451 
452 template<typename Lhs,typename Rhs>
453 struct homogeneous_left_product_refactoring_helper
454 {
455  enum {
456  Dim = Rhs::RowsAtCompileTime,
457  Cols = Rhs::ColsAtCompileTime
458  };
459  typedef typename Lhs::template ConstNColsBlockXpr<Dim>::Type LinearBlockConst;
460  typedef std::remove_const_t<LinearBlockConst> LinearBlock;
461  typedef typename Lhs::ConstColXpr ConstantColumn;
462  typedef Replicate<const ConstantColumn,1,Cols> ConstantBlock;
463  typedef Product<LinearBlock,Rhs,LazyProduct> LinearProduct;
464  typedef CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar,typename Rhs::Scalar>, const LinearProduct, const ConstantBlock> Xpr;
465 };
466 
467 template<typename Lhs, typename Rhs, int ProductTag>
468 struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, HomogeneousShape>
469  : public evaluator<typename homogeneous_left_product_refactoring_helper<Lhs,typename Rhs::NestedExpression>::Xpr>
470 {
471  typedef Product<Lhs, Rhs, LazyProduct> XprType;
472  typedef homogeneous_left_product_refactoring_helper<Lhs,typename Rhs::NestedExpression> helper;
473  typedef typename helper::ConstantBlock ConstantBlock;
474  typedef typename helper::Xpr RefactoredXpr;
475  typedef evaluator<RefactoredXpr> Base;
476 
477  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
478  : Base( xpr.lhs().template leftCols<helper::Dim>(xpr.rhs().nestedExpression().rows()) .lazyProduct( xpr.rhs().nestedExpression() )
479  + ConstantBlock(xpr.lhs().col(xpr.lhs().cols()-1),1,xpr.rhs().cols()) )
480  {}
481 };
482 
483 template<typename Scalar, int Dim, int Mode,int Options, typename RhsArg, int ProductTag>
484 struct generic_product_impl<Transform<Scalar,Dim,Mode,Options>, Homogeneous<RhsArg,Vertical>, DenseShape, HomogeneousShape, ProductTag>
485 {
486  typedef Transform<Scalar,Dim,Mode,Options> TransformType;
487  template<typename Dest>
488  EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const TransformType& lhs, const Homogeneous<RhsArg,Vertical>& rhs)
489  {
490  homogeneous_left_product_impl<Homogeneous<RhsArg,Vertical>, TransformType>(lhs, rhs.nestedExpression()).evalTo(dst);
491  }
492 };
493 
494 template<typename ExpressionType, int Side, bool Transposed>
495 struct permutation_matrix_product<ExpressionType, Side, Transposed, HomogeneousShape>
496  : public permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape>
497 {};
498 
499 } // end namespace internal
500 
501 } // end namespace Eigen
502 
503 #endif // EIGEN_HOMOGENEOUS_H
NRowsBlockXpr<... >::Type topRows(NRowsType n)
Definition: BlockMethods.h:570
RowXpr row(Index i)
This is the const version of row(). *‍/.
NColsBlockXpr<... >::Type leftCols(NColsType n)
Definition: BlockMethods.h:797
ColXpr col(Index i)
This is the const version of col().
#define EIGEN_NOEXCEPT
Definition: Macros.h:1260
#define EIGEN_CONSTEXPR
Definition: Macros.h:747
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
#define EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
Definition: Macros.h:1168
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
Matrix< float, 1, Dynamic > MatrixType
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:107
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:86
internal::traits< Homogeneous< MatrixType, Direction_ > >::Scalar Scalar
Definition: DenseBase.h:61
Expression of one (or a set of) homogeneous vector(s)
Definition: Homogeneous.h:64
internal::result_of< Func(Scalar, Scalar)>::type redux(const Func &func) const
Definition: Homogeneous.h:110
const Product< Homogeneous, Rhs > operator*(const MatrixBase< Rhs > &rhs) const
Definition: Homogeneous.h:86
const NestedExpression & nestedExpression() const
Definition: Homogeneous.h:82
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: Homogeneous.h:78
MatrixBase< Homogeneous > Base
Definition: Homogeneous.h:70
MatrixType NestedExpression
Definition: Homogeneous.h:67
MatrixType::Nested m_matrix
Definition: Homogeneous.h:116
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: Homogeneous.h:80
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
MatrixBase< Homogeneous< MatrixType, Direction_ > > & matrix()
Definition: MatrixBase.h:319
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
Expression of the multiple replication of a matrix or vector.
Definition: Replicate.h:65
Represents an homogeneous transformation in a N dimensional space.
Definition: Transform.h:207
const HNormalizedReturnType hnormalized() const
column or row-wise homogeneous normalization
Definition: Homogeneous.h:200
const HNormalizedReturnType hnormalized() const
homogeneous normalization
Definition: Homogeneous.h:176
HomogeneousReturnType homogeneous() const
Definition: Homogeneous.h:134
HomogeneousReturnType homogeneous() const
Definition: Homogeneous.h:152
@ Horizontal
Definition: Constants.h:269
@ Vertical
Definition: Constants.h:266
@ Projective
Definition: Constants.h:468
const unsigned int RowMajorBit
Definition: Constants.h:68
: InteropHeaders
Definition: Core:139
@ LazyProduct
Definition: Constants.h:504
const unsigned int HereditaryBits
Definition: Constants.h:197
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24