11#ifndef EIGEN_HOMOGENEOUS_H
12#define EIGEN_HOMOGENEOUS_H
15#include "./InternalHeaderCheck.h"
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>;
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)
55template <
typename MatrixType,
typename Lhs>
56struct homogeneous_left_product_impl;
57template <
typename MatrixType,
typename Rhs>
58struct homogeneous_right_product_impl;
62template <
typename MatrixType,
int Direction_>
63class Homogeneous :
public MatrixBase<Homogeneous<MatrixType, Direction_> >, internal::no_assignment_operator {
65 using NestedExpression = MatrixType;
66 enum { Direction = Direction_ };
68 using Base = MatrixBase<Homogeneous>;
69 EIGEN_DENSE_PUBLIC_INTERFACE(Homogeneous)
71 EIGEN_DEVICE_FUNC
explicit inline Homogeneous(
const MatrixType& matrix) : m_matrix(matrix) {}
73 EIGEN_DEVICE_FUNC
constexpr Index rows()
const noexcept {
74 return m_matrix.rows() + (int(Direction) ==
Vertical ? 1 : 0);
76 EIGEN_DEVICE_FUNC
constexpr Index cols()
const noexcept {
77 return m_matrix.cols() + (int(Direction) ==
Horizontal ? 1 : 0);
80 EIGEN_DEVICE_FUNC
const NestedExpression& nestedExpression()
const {
return m_matrix; }
82 template <
typename Rhs>
87 template <
typename Lhs>
89 const Homogeneous& rhs) {
93 template <
typename Scalar,
int Dim,
int Mode,
int Options>
96 eigen_assert(
int(Direction) ==
Vertical);
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>());
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 {
114 return Base::redux(func);
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);
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));
130 typename MatrixType::Nested m_matrix;
147template <
typename Derived>
149 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
150 return HomogeneousReturnType(derived());
164template <
typename ExpressionType,
int Direction>
167 return HomogeneousReturnType(_expression());
187template <
typename Derived>
190 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
191 return ConstStartMinusOne(derived(), 0, 0,
ColsAtCompileTime == 1 ? size() - 1 : 1,
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,
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));
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; }
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(); }
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(); }
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;
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) {}
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(); }
272 template <
typename Dest>
273 EIGEN_DEVICE_FUNC
void evalTo(Dest& dst)
const {
275 dst = Block <
const LhsMatrixTypeNested, LhsMatrixTypeNested::RowsAtCompileTime,
276 LhsMatrixTypeNested::ColsAtCompileTime == 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());
282 typename LhsMatrixTypeCleaned::Nested m_lhs;
283 typename MatrixType::Nested m_rhs;
286template <
typename MatrixType,
typename Rhs>
287struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,
Horizontal>, Rhs> > {
289 typename make_proper_matrix_type<typename traits<MatrixType>::Scalar, MatrixType::RowsAtCompileTime,
290 Rhs::ColsAtCompileTime, MatrixType::PlainObject::Options,
291 MatrixType::MaxRowsAtCompileTime, Rhs::MaxColsAtCompileTime>::type;
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) {}
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(); }
303 template <
typename Dest>
304 EIGEN_DEVICE_FUNC
void evalTo(Dest& dst)
const {
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());
312 typename MatrixType::Nested m_lhs;
313 typename Rhs::Nested m_rhs;
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;
323struct AssignmentKind<DenseShape, HomogeneousShape> {
324 using Kind = Dense2Dense;
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>;
334 EIGEN_DEVICE_FUNC
explicit unary_evaluator(
const XprType& op) : Base(), m_temp(op) {
335 internal::construct_at<Base>(
this, m_temp);
343template <
typename DstXprType,
typename ArgType,
typename Scalar>
344struct Assignment<DstXprType, Homogeneous<ArgType,
Vertical>, internal::assign_op<Scalar, typename ArgType::Scalar>,
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);
353 dst.template topRows<ArgType::RowsAtCompileTime>(src.nestedExpression().rows()) = src.nestedExpression();
354 dst.row(dst.rows() - 1).setOnes();
359template <
typename DstXprType,
typename ArgType,
typename Scalar>
360struct Assignment<DstXprType, Homogeneous<ArgType,
Horizontal>, internal::assign_op<Scalar, typename ArgType::Scalar>,
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);
369 dst.template leftCols<ArgType::ColsAtCompileTime>(src.nestedExpression().cols()) = src.nestedExpression();
370 dst.col(dst.cols() - 1).setOnes();
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);
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>;
394template <
typename Lhs,
typename Rhs,
int ProductTag>
395struct product_evaluator<Product<Lhs, Rhs, LazyProduct>, ProductTag, HomogeneousShape, DenseShape>
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>;
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)) {}
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);
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();
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;
435 const ConstantBlock>;
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> {
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>;
447 EIGEN_DEVICE_FUNC
explicit product_evaluator(
const XprType& xpr)
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())) {}
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())
465template <
typename ExpressionType,
int S
ide,
bool Transposed>
466struct permutation_matrix_product<ExpressionType, Side, Transposed, HomogeneousShape>
467 :
public permutation_matrix_product<ExpressionType, Side, Transposed, DenseShape> {};
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
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