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