$darkmode
Eigen  5.0.1-dev
Homogeneous.h
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 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
33 namespace internal {
34 
35 template <typename MatrixType, int Direction>
36 struct traits<Homogeneous<MatrixType, Direction> > : traits<MatrixType> {
37  typedef typename traits<MatrixType>::StorageKind StorageKind;
38  typedef typename ref_selector<MatrixType>::type MatrixTypeNested;
39  typedef std::remove_reference_t<MatrixTypeNested> MatrixTypeNested_;
40  enum {
41  RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ? int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
42  ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ? int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
43  RowsAtCompileTime = Direction == Vertical ? RowsPlusOne : MatrixType::RowsAtCompileTime,
44  ColsAtCompileTime = Direction == Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
45  MaxRowsAtCompileTime = RowsAtCompileTime,
46  MaxColsAtCompileTime = ColsAtCompileTime,
47  TmpFlags = MatrixTypeNested_::Flags & HereditaryBits,
48  Flags = ColsAtCompileTime == 1 ? (TmpFlags & ~RowMajorBit)
49  : RowsAtCompileTime == 1 ? (TmpFlags | RowMajorBit)
50  : TmpFlags
51  };
52 };
53 
54 template <typename MatrixType, typename Lhs>
55 struct homogeneous_left_product_impl;
56 template <typename MatrixType, typename Rhs>
57 struct homogeneous_right_product_impl;
58 
59 } // end namespace internal
60 
61 template <typename MatrixType, int Direction_>
62 class Homogeneous : public MatrixBase<Homogeneous<MatrixType, Direction_> >, internal::no_assignment_operator {
63  public:
64  typedef MatrixType NestedExpression;
65  enum { Direction = Direction_ };
66 
67  typedef MatrixBase<Homogeneous> Base;
68  EIGEN_DENSE_PUBLIC_INTERFACE(Homogeneous)
69 
70  EIGEN_DEVICE_FUNC explicit inline Homogeneous(const MatrixType& matrix) : m_matrix(matrix) {}
71 
72  EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept {
73  return m_matrix.rows() + (int(Direction) == Vertical ? 1 : 0);
74  }
75  EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept {
76  return m_matrix.cols() + (int(Direction) == Horizontal ? 1 : 0);
77  }
78 
79  EIGEN_DEVICE_FUNC const NestedExpression& nestedExpression() const { return m_matrix; }
80 
81  template <typename Rhs>
82  EIGEN_DEVICE_FUNC inline const Product<Homogeneous, Rhs> operator*(const MatrixBase<Rhs>& rhs) const {
83  return Product<Homogeneous, Rhs>(*this, rhs.derived());
84  }
85 
86  template <typename Lhs>
87  friend EIGEN_DEVICE_FUNC inline const Product<Lhs, Homogeneous> operator*(const MatrixBase<Lhs>& lhs,
88  const Homogeneous& rhs) {
89  return Product<Lhs, Homogeneous>(lhs.derived(), rhs);
90  }
91 
92  template <typename Scalar, int Dim, int Mode, int Options>
93  friend EIGEN_DEVICE_FUNC inline const Product<Transform<Scalar, Dim, Mode, Options>, Homogeneous> operator*(
94  const Transform<Scalar, Dim, Mode, Options>& lhs, const Homogeneous& rhs) {
95  eigen_assert(int(Direction) == Vertical);
96  return Product<Transform<Scalar, Dim, Mode, Options>, Homogeneous>(lhs, rhs);
97  }
98 
99  template <typename Func>
100  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::result_of<Func(Scalar, Scalar)>::type redux(
101  const Func& func) const {
102  return func(m_matrix.redux(func), Scalar(1));
103  }
104 
105  protected:
106  typename MatrixType::Nested m_matrix;
107 };
108 
123 template <typename Derived>
125  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
126  return HomogeneousReturnType(derived());
127 }
128 
140 template <typename ExpressionType, int Direction>
142  const {
143  return HomogeneousReturnType(_expression());
144 }
145 
163 template <typename Derived>
165  const {
166  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
167  return ConstStartMinusOne(derived(), 0, 0, ColsAtCompileTime == 1 ? size() - 1 : 1,
168  ColsAtCompileTime == 1 ? 1 : size() - 1) /
169  coeff(size() - 1);
170 }
171 
187 template <typename ExpressionType, int Direction>
188 EIGEN_DEVICE_FUNC inline const typename VectorwiseOp<ExpressionType, Direction>::HNormalizedReturnType
190  return HNormalized_Block(_expression(), 0, 0, Direction == Vertical ? _expression().rows() - 1 : _expression().rows(),
191  Direction == Horizontal ? _expression().cols() - 1 : _expression().cols())
192  .cwiseQuotient(Replicate < HNormalized_Factors, Direction == Vertical ? HNormalized_SizeMinusOne : 1,
193  Direction == Horizontal
194  ? HNormalized_SizeMinusOne
195  : 1 > (HNormalized_Factors(_expression(), Direction == Vertical ? _expression().rows() - 1 : 0,
196  Direction == Horizontal ? _expression().cols() - 1 : 0,
197  Direction == Vertical ? 1 : _expression().rows(),
198  Direction == Horizontal ? 1 : _expression().cols()),
199  Direction == Vertical ? _expression().rows() - 1 : 1,
200  Direction == Horizontal ? _expression().cols() - 1 : 1));
201 }
202 
203 namespace internal {
204 
205 template <typename MatrixOrTransformType>
206 struct take_matrix_for_product {
207  typedef MatrixOrTransformType type;
208  EIGEN_DEVICE_FUNC static const type& run(const type& x) { return x; }
209 };
210 
211 template <typename Scalar, int Dim, int Mode, int Options>
212 struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> > {
213  typedef Transform<Scalar, Dim, Mode, Options> TransformType;
214  typedef std::add_const_t<typename TransformType::ConstAffinePart> type;
215  EIGEN_DEVICE_FUNC static type run(const TransformType& x) { return x.affine(); }
216 };
217 
218 template <typename Scalar, int Dim, int Options>
219 struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> > {
220  typedef Transform<Scalar, Dim, Projective, Options> TransformType;
221  typedef typename TransformType::MatrixType type;
222  EIGEN_DEVICE_FUNC static const type& run(const TransformType& x) { return x.matrix(); }
223 };
224 
225 template <typename MatrixType, typename Lhs>
226 struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType, Vertical>, Lhs> > {
227  typedef typename take_matrix_for_product<Lhs>::type LhsMatrixType;
228  typedef remove_all_t<MatrixType> MatrixTypeCleaned;
229  typedef remove_all_t<LhsMatrixType> LhsMatrixTypeCleaned;
230  typedef typename make_proper_matrix_type<
231  typename traits<MatrixTypeCleaned>::Scalar, LhsMatrixTypeCleaned::RowsAtCompileTime,
232  MatrixTypeCleaned::ColsAtCompileTime, MatrixTypeCleaned::PlainObject::Options,
233  LhsMatrixTypeCleaned::MaxRowsAtCompileTime, MatrixTypeCleaned::MaxColsAtCompileTime>::type ReturnType;
234 };
235 
236 template <typename MatrixType, typename Lhs>
237 struct homogeneous_left_product_impl<Homogeneous<MatrixType, Vertical>, Lhs>
238  : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType, Vertical>, Lhs> > {
239  typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
240  typedef remove_all_t<LhsMatrixType> LhsMatrixTypeCleaned;
241  typedef remove_all_t<typename LhsMatrixTypeCleaned::Nested> LhsMatrixTypeNested;
242  EIGEN_DEVICE_FUNC homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
243  : m_lhs(take_matrix_for_product<Lhs>::run(lhs)), m_rhs(rhs) {}
244 
245  EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_lhs.rows(); }
246  EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
247 
248  template <typename Dest>
249  EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const {
250  // FIXME investigate how to allow lazy evaluation of this product when possible
251  dst = Block < const LhsMatrixTypeNested, LhsMatrixTypeNested::RowsAtCompileTime,
252  LhsMatrixTypeNested::ColsAtCompileTime == Dynamic
253  ? Dynamic
254  : LhsMatrixTypeNested::ColsAtCompileTime - 1 > (m_lhs, 0, 0, m_lhs.rows(), m_lhs.cols() - 1) * m_rhs;
255  dst += m_lhs.col(m_lhs.cols() - 1).rowwise().template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
256  }
257 
258  typename LhsMatrixTypeCleaned::Nested m_lhs;
259  typename MatrixType::Nested m_rhs;
260 };
261 
262 template <typename MatrixType, typename Rhs>
263 struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType, Horizontal>, Rhs> > {
264  typedef
265  typename make_proper_matrix_type<typename traits<MatrixType>::Scalar, MatrixType::RowsAtCompileTime,
266  Rhs::ColsAtCompileTime, MatrixType::PlainObject::Options,
267  MatrixType::MaxRowsAtCompileTime, Rhs::MaxColsAtCompileTime>::type ReturnType;
268 };
269 
270 template <typename MatrixType, typename Rhs>
271 struct homogeneous_right_product_impl<Homogeneous<MatrixType, Horizontal>, Rhs>
272  : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType, Horizontal>, Rhs> > {
273  typedef remove_all_t<typename Rhs::Nested> RhsNested;
274  EIGEN_DEVICE_FUNC homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs) {}
275 
276  EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_lhs.rows(); }
277  EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
278 
279  template <typename Dest>
280  EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const {
281  // FIXME investigate how to allow lazy evaluation of this product when possible
282  dst = m_lhs * Block < const RhsNested,
283  RhsNested::RowsAtCompileTime == Dynamic ? Dynamic : RhsNested::RowsAtCompileTime - 1,
284  RhsNested::ColsAtCompileTime > (m_rhs, 0, 0, m_rhs.rows() - 1, m_rhs.cols());
285  dst += m_rhs.row(m_rhs.rows() - 1).colwise().template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
286  }
287 
288  typename MatrixType::Nested m_lhs;
289  typename Rhs::Nested m_rhs;
290 };
291 
292 template <typename ArgType, int Direction>
293 struct evaluator_traits<Homogeneous<ArgType, Direction> > {
294  typedef typename storage_kind_to_evaluator_kind<typename ArgType::StorageKind>::Kind Kind;
295  typedef HomogeneousShape Shape;
296 };
297 
298 template <>
299 struct AssignmentKind<DenseShape, HomogeneousShape> {
300  typedef Dense2Dense Kind;
301 };
302 
303 template <typename ArgType, int Direction>
304 struct unary_evaluator<Homogeneous<ArgType, Direction>, IndexBased>
305  : evaluator<typename Homogeneous<ArgType, Direction>::PlainObject> {
306  typedef Homogeneous<ArgType, Direction> XprType;
307  typedef typename XprType::PlainObject PlainObject;
308  typedef evaluator<PlainObject> Base;
309 
310  EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& op) : Base(), m_temp(op) {
311  internal::construct_at<Base>(this, m_temp);
312  }
313 
314  protected:
315  PlainObject m_temp;
316 };
317 
318 // dense = homogeneous
319 template <typename DstXprType, typename ArgType, typename Scalar>
320 struct Assignment<DstXprType, Homogeneous<ArgType, Vertical>, internal::assign_op<Scalar, typename ArgType::Scalar>,
321  Dense2Dense> {
322  typedef Homogeneous<ArgType, Vertical> SrcXprType;
323  EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src,
324  const internal::assign_op<Scalar, typename ArgType::Scalar>&) {
325  Index dstRows = src.rows();
326  Index dstCols = src.cols();
327  if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
328 
329  dst.template topRows<ArgType::RowsAtCompileTime>(src.nestedExpression().rows()) = src.nestedExpression();
330  dst.row(dst.rows() - 1).setOnes();
331  }
332 };
333 
334 // dense = homogeneous
335 template <typename DstXprType, typename ArgType, typename Scalar>
336 struct Assignment<DstXprType, Homogeneous<ArgType, Horizontal>, internal::assign_op<Scalar, typename ArgType::Scalar>,
337  Dense2Dense> {
338  typedef Homogeneous<ArgType, Horizontal> SrcXprType;
339  EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src,
340  const internal::assign_op<Scalar, typename ArgType::Scalar>&) {
341  Index dstRows = src.rows();
342  Index dstCols = src.cols();
343  if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
344 
345  dst.template leftCols<ArgType::ColsAtCompileTime>(src.nestedExpression().cols()) = src.nestedExpression();
346  dst.col(dst.cols() - 1).setOnes();
347  }
348 };
349 
350 template <typename LhsArg, typename Rhs, int ProductTag>
351 struct generic_product_impl<Homogeneous<LhsArg, Horizontal>, Rhs, HomogeneousShape, DenseShape, ProductTag> {
352  template <typename Dest>
353  EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Homogeneous<LhsArg, Horizontal>& lhs, const Rhs& rhs) {
354  homogeneous_right_product_impl<Homogeneous<LhsArg, Horizontal>, Rhs>(lhs.nestedExpression(), rhs).evalTo(dst);
355  }
356 };
357 
358 template <typename Lhs, typename Rhs>
359 struct homogeneous_right_product_refactoring_helper {
360  enum { Dim = Lhs::ColsAtCompileTime, Rows = Lhs::RowsAtCompileTime };
361  typedef typename Rhs::template ConstNRowsBlockXpr<Dim>::Type LinearBlockConst;
362  typedef std::remove_const_t<LinearBlockConst> LinearBlock;
363  typedef typename Rhs::ConstRowXpr ConstantColumn;
364  typedef Replicate<const ConstantColumn, Rows, 1> ConstantBlock;
365  typedef Product<Lhs, LinearBlock, LazyProduct> LinearProduct;
366  typedef CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar, typename Rhs::Scalar>, const LinearProduct,
367  const ConstantBlock>
368  Xpr;
369 };
370 
371 template <typename Lhs, typename Rhs, int ProductTag>
372 struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, HomogeneousShape, DenseShape>
373  : public evaluator<
374  typename homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression, Rhs>::Xpr> {
375  typedef Product<Lhs, Rhs, LazyProduct> XprType;
376  typedef homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression, Rhs> helper;
377  typedef typename helper::ConstantBlock ConstantBlock;
378  typedef typename helper::Xpr RefactoredXpr;
379  typedef evaluator<RefactoredXpr> Base;
380 
381  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
382  : Base(xpr.lhs().nestedExpression().lazyProduct(
383  xpr.rhs().template topRows<helper::Dim>(xpr.lhs().nestedExpression().cols())) +
384  ConstantBlock(xpr.rhs().row(xpr.rhs().rows() - 1), xpr.lhs().rows(), 1)) {}
385 };
386 
387 template <typename Lhs, typename RhsArg, int ProductTag>
388 struct generic_product_impl<Lhs, Homogeneous<RhsArg, Vertical>, DenseShape, HomogeneousShape, ProductTag> {
389  template <typename Dest>
390  EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg, Vertical>& rhs) {
391  homogeneous_left_product_impl<Homogeneous<RhsArg, Vertical>, Lhs>(lhs, rhs.nestedExpression()).evalTo(dst);
392  }
393 };
394 
395 // TODO: the following specialization is to address a regression from 3.2 to 3.3
396 // In the future, this path should be optimized.
397 template <typename Lhs, typename RhsArg, int ProductTag>
398 struct generic_product_impl<Lhs, Homogeneous<RhsArg, Vertical>, TriangularShape, HomogeneousShape, ProductTag> {
399  template <typename Dest>
400  static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg, Vertical>& rhs) {
401  dst.noalias() = lhs * rhs.eval();
402  }
403 };
404 
405 template <typename Lhs, typename Rhs>
406 struct homogeneous_left_product_refactoring_helper {
407  enum { Dim = Rhs::RowsAtCompileTime, Cols = Rhs::ColsAtCompileTime };
408  typedef typename Lhs::template ConstNColsBlockXpr<Dim>::Type LinearBlockConst;
409  typedef std::remove_const_t<LinearBlockConst> LinearBlock;
410  typedef typename Lhs::ConstColXpr ConstantColumn;
411  typedef Replicate<const ConstantColumn, 1, Cols> ConstantBlock;
412  typedef Product<LinearBlock, Rhs, LazyProduct> LinearProduct;
413  typedef CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar, typename Rhs::Scalar>, const LinearProduct,
414  const ConstantBlock>
415  Xpr;
416 };
417 
418 template <typename Lhs, typename Rhs, int ProductTag>
419 struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, HomogeneousShape>
420  : public evaluator<typename homogeneous_left_product_refactoring_helper<Lhs, typename Rhs::NestedExpression>::Xpr> {
421  typedef Product<Lhs, Rhs, LazyProduct> XprType;
422  typedef homogeneous_left_product_refactoring_helper<Lhs, typename Rhs::NestedExpression> helper;
423  typedef typename helper::ConstantBlock ConstantBlock;
424  typedef typename helper::Xpr RefactoredXpr;
425  typedef evaluator<RefactoredXpr> Base;
426 
427  EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
428  : Base(xpr.lhs()
429  .template leftCols<helper::Dim>(xpr.rhs().nestedExpression().rows())
430  .lazyProduct(xpr.rhs().nestedExpression()) +
431  ConstantBlock(xpr.lhs().col(xpr.lhs().cols() - 1), 1, xpr.rhs().cols())) {}
432 };
433 
434 template <typename Scalar, int Dim, int Mode, int Options, typename RhsArg, int ProductTag>
435 struct generic_product_impl<Transform<Scalar, Dim, Mode, Options>, Homogeneous<RhsArg, Vertical>, DenseShape,
436  HomogeneousShape, ProductTag> {
437  typedef Transform<Scalar, Dim, Mode, Options> TransformType;
438  template <typename Dest>
439  EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const TransformType& lhs, const Homogeneous<RhsArg, Vertical>& rhs) {
440  homogeneous_left_product_impl<Homogeneous<RhsArg, Vertical>, TransformType>(lhs, rhs.nestedExpression())
441  .evalTo(dst);
442  }
443 };
444 
445 template <typename ExpressionType, int Side, bool Transposed>
446 struct permutation_matrix_product<ExpressionType, Side, Transposed, HomogeneousShape>
447  : public permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape> {};
448 
449 } // end namespace internal
450 
451 } // end namespace Eigen
452 
453 #endif // EIGEN_HOMOGENEOUS_H
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:62
Definition: Constants.h:266
HomogeneousReturnType homogeneous() const
Definition: Homogeneous.h:141
const HNormalizedReturnType hnormalized() const
column or row-wise homogeneous normalization
Definition: Homogeneous.h:189
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
const unsigned int RowMajorBit
Definition: Constants.h:70
Definition: Constants.h:462
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition: CwiseBinaryOp.h:75
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
Expression of the multiple replication of a matrix or vector.
Definition: Replicate.h:64
Definition: Constants.h:269
const HNormalizedReturnType hnormalized() const
homogeneous normalization
Definition: Homogeneous.h:164
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:109
const int Dynamic
Definition: Constants.h:25
HomogeneousReturnType homogeneous() const
Definition: Homogeneous.h:124
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Expression of one (or a set of) homogeneous vector(s)
Definition: ForwardDeclarations.h:477