Eigen  5.0.1-dev+master
 
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 <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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_HOMOGENEOUS_H
12#define EIGEN_HOMOGENEOUS_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
33
34namespace internal {
35
36template <typename MatrixType, int Direction>
37struct traits<Homogeneous<MatrixType, Direction> > : traits<MatrixType> {
38 using StorageKind = typename traits<MatrixType>::StorageKind;
39 using MatrixTypeNested = typename ref_selector<MatrixType>::type;
40 using MatrixTypeNested_ = std::remove_reference_t<MatrixTypeNested>;
41 enum {
42 RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ? int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
43 ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ? int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
44 RowsAtCompileTime = Direction == Vertical ? RowsPlusOne : MatrixType::RowsAtCompileTime,
45 ColsAtCompileTime = Direction == Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
46 MaxRowsAtCompileTime = RowsAtCompileTime,
47 MaxColsAtCompileTime = ColsAtCompileTime,
48 TmpFlags = MatrixTypeNested_::Flags & HereditaryBits,
49 Flags = ColsAtCompileTime == 1 ? (TmpFlags & ~RowMajorBit)
50 : RowsAtCompileTime == 1 ? (TmpFlags | RowMajorBit)
51 : TmpFlags
52 };
53};
54
55template <typename MatrixType, typename Lhs>
56struct homogeneous_left_product_impl;
57template <typename MatrixType, typename Rhs>
58struct homogeneous_right_product_impl;
59
60} // end namespace internal
61
62template <typename MatrixType, int Direction_>
63class Homogeneous : public MatrixBase<Homogeneous<MatrixType, Direction_> >, internal::no_assignment_operator {
64 public:
65 using NestedExpression = MatrixType;
66 enum { Direction = Direction_ };
67
68 using Base = MatrixBase<Homogeneous>;
69 EIGEN_DENSE_PUBLIC_INTERFACE(Homogeneous)
70
71 EIGEN_DEVICE_FUNC explicit inline Homogeneous(const MatrixType& matrix) : m_matrix(matrix) {}
72
73 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept {
74 return m_matrix.rows() + (int(Direction) == Vertical ? 1 : 0);
75 }
76 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept {
77 return m_matrix.cols() + (int(Direction) == Horizontal ? 1 : 0);
78 }
79
80 EIGEN_DEVICE_FUNC const NestedExpression& nestedExpression() const { return m_matrix; }
81
82 template <typename Rhs>
83 EIGEN_DEVICE_FUNC inline const Product<Homogeneous, Rhs> operator*(const MatrixBase<Rhs>& rhs) const {
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 return Product<Lhs, Homogeneous>(lhs.derived(), rhs);
91 }
92
93 template <typename Scalar, int Dim, int Mode, int Options>
94 friend EIGEN_DEVICE_FUNC inline const Product<Transform<Scalar, Dim, Mode, Options>, Homogeneous> operator*(
95 const Transform<Scalar, Dim, Mode, Options>& lhs, const Homogeneous& rhs) {
96 eigen_assert(int(Direction) == Vertical);
97 return Product<Transform<Scalar, Dim, Mode, Options>, Homogeneous>(lhs, rhs);
98 }
99
103 template <typename Func>
104 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::result_of<Func(Scalar, Scalar)>::type redux(
105 const Func& func) const {
106 return redux_impl(func, internal::bool_constant<MatrixType::MaxSizeAtCompileTime == 0>());
107 }
108
109 protected:
110 template <typename Func>
111 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::result_of<Func(Scalar, Scalar)>::type redux_impl(
112 const Func& func, std::true_type) const {
113 // Reduce the appended coefficients, not the empty underlying expression.
114 return Base::redux(func);
115 }
116
117 template <typename Func>
118 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename internal::result_of<Func(Scalar, Scalar)>::type redux_impl(
119 const Func& func, std::false_type) const {
120 EIGEN_IF_CONSTEXPR (MatrixType::SizeAtCompileTime == Dynamic) {
121 if (m_matrix.size() == 0) return Base::redux(func);
122 }
123 // One appended coefficient per column (Vertical) or per row (Horizontal) of m_matrix.
124 using Ones = std::conditional_t<int(Direction) == Vertical, typename internal::plain_row_type<MatrixType>::type,
125 typename internal::plain_col_type<MatrixType>::type>;
126 return func(m_matrix.redux(func),
127 Ones::Ones(int(Direction) == Vertical ? m_matrix.cols() : m_matrix.rows()).redux(func));
128 }
129
130 typename MatrixType::Nested m_matrix;
131};
132
147template <typename Derived>
148EIGEN_DEVICE_FUNC inline typename MatrixBase<Derived>::HomogeneousReturnType MatrixBase<Derived>::homogeneous() const {
149 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
150 return HomogeneousReturnType(derived());
151}
152
164template <typename ExpressionType, int Direction>
166 const {
167 return HomogeneousReturnType(_expression());
168}
169
187template <typename Derived>
188EIGEN_DEVICE_FUNC inline const typename MatrixBase<Derived>::HNormalizedReturnType MatrixBase<Derived>::hnormalized()
189 const {
190 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
191 return ConstStartMinusOne(derived(), 0, 0, ColsAtCompileTime == 1 ? size() - 1 : 1,
192 ColsAtCompileTime == 1 ? 1 : size() - 1) /
193 coeff(size() - 1);
194}
195
211template <typename ExpressionType, int Direction>
212EIGEN_DEVICE_FUNC inline const typename VectorwiseOp<ExpressionType, Direction>::HNormalizedReturnType
214 return HNormalized_Block(_expression(), 0, 0, Direction == Vertical ? _expression().rows() - 1 : _expression().rows(),
215 Direction == Horizontal ? _expression().cols() - 1 : _expression().cols())
216 .cwiseQuotient(Replicate < HNormalized_Factors, Direction == Vertical ? HNormalized_SizeMinusOne : 1,
217 Direction == Horizontal
218 ? HNormalized_SizeMinusOne
219 : 1 > (HNormalized_Factors(_expression(), Direction == Vertical ? _expression().rows() - 1 : 0,
220 Direction == Horizontal ? _expression().cols() - 1 : 0,
221 Direction == Vertical ? 1 : _expression().rows(),
222 Direction == Horizontal ? 1 : _expression().cols()),
223 Direction == Vertical ? _expression().rows() - 1 : 1,
224 Direction == Horizontal ? _expression().cols() - 1 : 1));
225}
226
227namespace internal {
228
229template <typename MatrixOrTransformType>
230struct take_matrix_for_product {
231 using type = MatrixOrTransformType;
232 EIGEN_DEVICE_FUNC static const type& run(const type& x) { return x; }
233};
234
235template <typename Scalar, int Dim, int Mode, int Options>
236struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> > {
237 using TransformType = Transform<Scalar, Dim, Mode, Options>;
238 using type = std::add_const_t<typename TransformType::ConstAffinePart>;
239 EIGEN_DEVICE_FUNC static type run(const TransformType& x) { return x.affine(); }
240};
241
242template <typename Scalar, int Dim, int Options>
243struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> > {
244 using TransformType = Transform<Scalar, Dim, Projective, Options>;
245 using type = typename TransformType::MatrixType;
246 EIGEN_DEVICE_FUNC static const type& run(const TransformType& x) { return x.matrix(); }
247};
248
249template <typename MatrixType, typename Lhs>
250struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType, Vertical>, Lhs> > {
251 using LhsMatrixType = typename take_matrix_for_product<Lhs>::type;
252 using MatrixTypeCleaned = remove_all_t<MatrixType>;
253 using LhsMatrixTypeCleaned = remove_all_t<LhsMatrixType>;
254 using ReturnType = typename make_proper_matrix_type<
255 typename traits<MatrixTypeCleaned>::Scalar, LhsMatrixTypeCleaned::RowsAtCompileTime,
256 MatrixTypeCleaned::ColsAtCompileTime, MatrixTypeCleaned::PlainObject::Options,
257 LhsMatrixTypeCleaned::MaxRowsAtCompileTime, MatrixTypeCleaned::MaxColsAtCompileTime>::type;
258};
259
260template <typename MatrixType, typename Lhs>
261struct homogeneous_left_product_impl<Homogeneous<MatrixType, Vertical>, Lhs>
262 : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType, Vertical>, Lhs> > {
263 using LhsMatrixType = typename traits<homogeneous_left_product_impl>::LhsMatrixType;
264 using LhsMatrixTypeCleaned = remove_all_t<LhsMatrixType>;
265 using LhsMatrixTypeNested = remove_all_t<typename LhsMatrixTypeCleaned::Nested>;
266 EIGEN_DEVICE_FUNC homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
267 : m_lhs(take_matrix_for_product<Lhs>::run(lhs)), m_rhs(rhs) {}
268
269 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_lhs.rows(); }
270 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
271
272 template <typename Dest>
273 EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const {
274 // FIXME: investigate how to allow lazy evaluation of this product when possible.
275 dst = Block < const LhsMatrixTypeNested, LhsMatrixTypeNested::RowsAtCompileTime,
276 LhsMatrixTypeNested::ColsAtCompileTime == Dynamic
277 ? Dynamic
278 : LhsMatrixTypeNested::ColsAtCompileTime - 1 > (m_lhs, 0, 0, m_lhs.rows(), m_lhs.cols() - 1) * m_rhs;
279 dst += m_lhs.col(m_lhs.cols() - 1).rowwise().template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
280 }
281
282 typename LhsMatrixTypeCleaned::Nested m_lhs;
283 typename MatrixType::Nested m_rhs;
284};
285
286template <typename MatrixType, typename Rhs>
287struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType, Horizontal>, Rhs> > {
288 using ReturnType =
289 typename make_proper_matrix_type<typename traits<MatrixType>::Scalar, MatrixType::RowsAtCompileTime,
290 Rhs::ColsAtCompileTime, MatrixType::PlainObject::Options,
291 MatrixType::MaxRowsAtCompileTime, Rhs::MaxColsAtCompileTime>::type;
292};
293
294template <typename MatrixType, typename Rhs>
295struct homogeneous_right_product_impl<Homogeneous<MatrixType, Horizontal>, Rhs>
296 : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType, Horizontal>, Rhs> > {
297 using RhsNested = remove_all_t<typename Rhs::Nested>;
298 EIGEN_DEVICE_FUNC homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs) {}
299
300 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_lhs.rows(); }
301 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_rhs.cols(); }
302
303 template <typename Dest>
304 EIGEN_DEVICE_FUNC void evalTo(Dest& dst) const {
305 // FIXME: investigate how to allow lazy evaluation of this product when possible.
306 dst = m_lhs * Block < const RhsNested,
307 RhsNested::RowsAtCompileTime == Dynamic ? Dynamic : RhsNested::RowsAtCompileTime - 1,
308 RhsNested::ColsAtCompileTime > (m_rhs, 0, 0, m_rhs.rows() - 1, m_rhs.cols());
309 dst += m_rhs.row(m_rhs.rows() - 1).colwise().template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
310 }
311
312 typename MatrixType::Nested m_lhs;
313 typename Rhs::Nested m_rhs;
314};
315
316template <typename ArgType, int Direction>
317struct evaluator_traits<Homogeneous<ArgType, Direction> > {
318 using Kind = typename storage_kind_to_evaluator_kind<typename ArgType::StorageKind>::Kind;
319 using Shape = HomogeneousShape;
320};
321
322template <>
323struct AssignmentKind<DenseShape, HomogeneousShape> {
324 using Kind = Dense2Dense;
325};
326
327template <typename ArgType, int Direction>
328struct unary_evaluator<Homogeneous<ArgType, Direction>, IndexBased>
329 : evaluator<typename Homogeneous<ArgType, Direction>::PlainObject> {
330 using XprType = Homogeneous<ArgType, Direction>;
331 using PlainObject = typename XprType::PlainObject;
332 using Base = evaluator<PlainObject>;
333
334 EIGEN_DEVICE_FUNC explicit unary_evaluator(const XprType& op) : Base(), m_temp(op) {
335 internal::construct_at<Base>(this, m_temp);
336 }
337
338 protected:
339 PlainObject m_temp;
340};
341
342// dense = homogeneous
343template <typename DstXprType, typename ArgType, typename Scalar>
344struct Assignment<DstXprType, Homogeneous<ArgType, Vertical>, internal::assign_op<Scalar, typename ArgType::Scalar>,
345 Dense2Dense> {
346 using SrcXprType = Homogeneous<ArgType, Vertical>;
347 EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src,
348 const internal::assign_op<Scalar, typename ArgType::Scalar>&) {
349 Index dstRows = src.rows();
350 Index dstCols = src.cols();
351 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
352
353 dst.template topRows<ArgType::RowsAtCompileTime>(src.nestedExpression().rows()) = src.nestedExpression();
354 dst.row(dst.rows() - 1).setOnes();
355 }
356};
357
358// dense = homogeneous
359template <typename DstXprType, typename ArgType, typename Scalar>
360struct Assignment<DstXprType, Homogeneous<ArgType, Horizontal>, internal::assign_op<Scalar, typename ArgType::Scalar>,
361 Dense2Dense> {
362 using SrcXprType = Homogeneous<ArgType, Horizontal>;
363 EIGEN_DEVICE_FUNC static void run(DstXprType& dst, const SrcXprType& src,
364 const internal::assign_op<Scalar, typename ArgType::Scalar>&) {
365 Index dstRows = src.rows();
366 Index dstCols = src.cols();
367 if ((dst.rows() != dstRows) || (dst.cols() != dstCols)) dst.resize(dstRows, dstCols);
368
369 dst.template leftCols<ArgType::ColsAtCompileTime>(src.nestedExpression().cols()) = src.nestedExpression();
370 dst.col(dst.cols() - 1).setOnes();
371 }
372};
373
374template <typename LhsArg, typename Rhs, int ProductTag>
375struct generic_product_impl<Homogeneous<LhsArg, Horizontal>, Rhs, HomogeneousShape, DenseShape, ProductTag> {
376 template <typename Dest>
377 EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Homogeneous<LhsArg, Horizontal>& lhs, const Rhs& rhs) {
378 homogeneous_right_product_impl<Homogeneous<LhsArg, Horizontal>, Rhs>(lhs.nestedExpression(), rhs).evalTo(dst);
379 }
380};
381
382template <typename Lhs, typename Rhs>
383struct homogeneous_right_product_refactoring_helper {
384 enum { Dim = Lhs::ColsAtCompileTime, Rows = Lhs::RowsAtCompileTime };
385 using LinearBlockConst = typename Rhs::template ConstNRowsBlockXpr<Dim>::Type;
386 using LinearBlock = std::remove_const_t<LinearBlockConst>;
387 using ConstantColumn = typename Rhs::ConstRowXpr;
388 using ConstantBlock = Replicate<const ConstantColumn, Rows, 1>;
389 using LinearProduct = Product<Lhs, LinearBlock, LazyProduct>;
390 using Xpr = CwiseBinaryOp<internal::scalar_sum_op<typename Lhs::Scalar, typename Rhs::Scalar>, const LinearProduct,
391 const ConstantBlock>;
392};
393
394template <typename Lhs, typename Rhs, int ProductTag>
395struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, HomogeneousShape, DenseShape>
396 : public evaluator<
397 typename homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression, Rhs>::Xpr> {
398 using XprType = Product<Lhs, Rhs, LazyProduct>;
399 using helper = homogeneous_right_product_refactoring_helper<typename Lhs::NestedExpression, Rhs>;
400 using ConstantBlock = typename helper::ConstantBlock;
401 using RefactoredXpr = typename helper::Xpr;
402 using Base = evaluator<RefactoredXpr>;
403
404 EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
405 : Base(xpr.lhs().nestedExpression().lazyProduct(
406 xpr.rhs().template topRows<helper::Dim>(xpr.lhs().nestedExpression().cols())) +
407 ConstantBlock(xpr.rhs().row(xpr.rhs().rows() - 1), xpr.lhs().rows(), 1)) {}
408};
409
410template <typename Lhs, typename RhsArg, int ProductTag>
411struct generic_product_impl<Lhs, Homogeneous<RhsArg, Vertical>, DenseShape, HomogeneousShape, ProductTag> {
412 template <typename Dest>
413 EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg, Vertical>& rhs) {
414 homogeneous_left_product_impl<Homogeneous<RhsArg, Vertical>, Lhs>(lhs, rhs.nestedExpression()).evalTo(dst);
415 }
416};
417
418template <typename Lhs, typename RhsArg, int ProductTag>
419struct generic_product_impl<Lhs, Homogeneous<RhsArg, Vertical>, TriangularShape, HomogeneousShape, ProductTag> {
420 template <typename Dest>
421 static void evalTo(Dest& dst, const Lhs& lhs, const Homogeneous<RhsArg, Vertical>& rhs) {
422 dst.noalias() = lhs * rhs.eval();
423 }
424};
425
426template <typename Lhs, typename Rhs>
427struct homogeneous_left_product_refactoring_helper {
428 enum { Dim = Rhs::RowsAtCompileTime, Cols = Rhs::ColsAtCompileTime };
429 using LinearBlockConst = typename Lhs::template ConstNColsBlockXpr<Dim>::Type;
430 using LinearBlock = std::remove_const_t<LinearBlockConst>;
431 using ConstantColumn = typename Lhs::ConstColXpr;
432 using ConstantBlock = Replicate<const ConstantColumn, 1, Cols>;
433 using LinearProduct = Product<LinearBlock, Rhs, LazyProduct>;
435 const ConstantBlock>;
436};
437
438template <typename Lhs, typename Rhs, int ProductTag>
439struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, DenseShape, HomogeneousShape>
440 : public evaluator<typename homogeneous_left_product_refactoring_helper<Lhs, typename Rhs::NestedExpression>::Xpr> {
441 using XprType = Product<Lhs, Rhs, LazyProduct>;
442 using helper = homogeneous_left_product_refactoring_helper<Lhs, typename Rhs::NestedExpression>;
443 using ConstantBlock = typename helper::ConstantBlock;
444 using RefactoredXpr = typename helper::Xpr;
445 using Base = evaluator<RefactoredXpr>;
446
447 EIGEN_DEVICE_FUNC explicit product_evaluator(const XprType& xpr)
448 : Base(xpr.lhs()
449 .template leftCols<helper::Dim>(xpr.rhs().nestedExpression().rows())
450 .lazyProduct(xpr.rhs().nestedExpression()) +
451 ConstantBlock(xpr.lhs().col(xpr.lhs().cols() - 1), 1, xpr.rhs().cols())) {}
452};
453
454template <typename Scalar, int Dim, int Mode, int Options, typename RhsArg, int ProductTag>
455struct generic_product_impl<Transform<Scalar, Dim, Mode, Options>, Homogeneous<RhsArg, Vertical>, DenseShape,
456 HomogeneousShape, ProductTag> {
457 using TransformType = Transform<Scalar, Dim, Mode, Options>;
458 template <typename Dest>
459 EIGEN_DEVICE_FUNC static void evalTo(Dest& dst, const TransformType& lhs, const Homogeneous<RhsArg, Vertical>& rhs) {
460 homogeneous_left_product_impl<Homogeneous<RhsArg, Vertical>, TransformType>(lhs, rhs.nestedExpression())
461 .evalTo(dst);
462 }
463};
464
465template <typename ExpressionType, int Side, bool Transposed>
466struct permutation_matrix_product<ExpressionType, Side, Transposed, HomogeneousShape>
467 : public permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape> {};
468
469} // end namespace internal
470
471} // end namespace Eigen
472
473#endif // EIGEN_HOMOGENEOUS_H
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:91
@ ColsAtCompileTime
Definition DenseBase.h:103
typename internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:63
Expression of one (or a set of) homogeneous vector(s)
Definition Homogeneous.h:63
internal::result_of< Func(Scalar, Scalar)>::type redux(const Func &func) const
Definition Homogeneous.h:104
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:211
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:222
const HNormalizedReturnType hnormalized() const
column or row-wise homogeneous normalization
Definition Homogeneous.h:213
const HNormalizedReturnType hnormalized() const
homogeneous normalization
Definition Homogeneous.h:188
HomogeneousReturnType homogeneous() const
Definition Homogeneous.h:148
HomogeneousReturnType homogeneous() const
Definition Homogeneous.h:165
@ Horizontal
Definition Constants.h:270
@ Vertical
Definition Constants.h:267
@ Projective
Definition Constants.h:479
constexpr unsigned int RowMajorBit
Definition Constants.h:71