hpp-constraints  4.9.1
Definition of basic geometric constraints for motion planning
matrix-view.hh
Go to the documentation of this file.
1 // Copyright (c) 2017, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of hpp-constraints.
5 // hpp-constraints is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-constraints is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // hpp-constraints. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HPP_CONSTRAINTS_MATRIX_VIEW_HH
18 #define HPP_CONSTRAINTS_MATRIX_VIEW_HH
19 
20 #include <Eigen/Core>
21 #include <vector>
22 #include <iostream>
23 #include <hpp/util/indent.hh>
24 #include <hpp/pinocchio/util.hh>
25 #include <hpp/constraints/fwd.hh>
26 
27 # define HPP_EIGEN_USE_EVALUATOR EIGEN_VERSION_AT_LEAST(3,2,92)
28 
29 namespace Eigen {
30 
33 
37  struct BlockIndex {
41  typedef std::pair<size_type, size_type> segment_t;
43  typedef std::vector<segment_t> segments_t;
44 
47  static size_type cardinal (const segments_t& a);
48 
53  template <typename Derived>
54  static segments_t fromLogicalExpression
55  (const Eigen::ArrayBase<Derived>& array);
56 
59  static void sort (segments_t& a);
60 
64  static void shrink (segments_t& a);
65 
67  static bool overlap (const segment_t& a, const segment_t& b);
68 
70  static segments_t sum (const segment_t& a, const segment_t& b);
71 
73  static void add (segments_t& a, const segment_t& b);
74 
76  static void add (segments_t& a, const segments_t& b);
77 
79  static segments_t difference (const segment_t& a, const segment_t& b);
80 
83  static segments_t difference (const segments_t& a, const segment_t& b);
84 
87  static segments_t difference (const segment_t& a, const segments_t& b);
88 
91  static segments_t difference (const segments_t& a, const segments_t& b);
92 
99  static segments_t split (segments_t& segments, const size_type& cardinal);
100 
107  static segments_t extract (const segments_t& segments, size_type start,
108  size_type cardinal);
109  }; // struct BlockIndex
110 
111  template <typename ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols> class MatrixBlockView;
112 
123  template <bool _allRows = false, bool _allCols = false> class MatrixBlocks;
124 
126 
127  template <bool _allRows = false, bool _allCols = false> class MatrixBlocksRef;
128 
129  namespace internal {
130  template <bool row> struct return_first {
131  template <typename First, typename Second>
132  static inline First& run (First& f, Second&) { return f; }
133  };
134  template <> struct return_first <false> {
135  template <typename First, typename Second>
136  static inline Second& run (First&, Second& s) { return s; }
137  };
138 
139  struct empty_struct {
140  typedef MatrixXd::Index Index;
142  template <typename In_t> empty_struct (In_t) {}
143  template <typename In0_t, typename In1_t> empty_struct (In0_t, In1_t) {}
144  static inline Index size() { return 1; }
145  inline const Index& operator[](const Index& i) const { return i; }
146  };
147  template <bool If> struct get_if { template <typename T1, typename T2> static EIGEN_STRONG_INLINE T1 run (T1 then, T2 Else) { (void)Else; return then; } };
148  template <> struct get_if<false> { template <typename T1, typename T2> static EIGEN_STRONG_INLINE T2 run (T1 then, T2 Else) { (void)then; return Else; } };
149 
150  template <bool _allRows, bool _allCols>
151  struct traits< MatrixBlocks <_allRows, _allCols> >
152  {
153  enum {
154  AllRows = _allRows,
155  AllCols = _allCols
156  };
157  typedef typename internal::conditional<_allRows, internal::empty_struct, BlockIndex::segments_t>::type RowIndices_t;
158  typedef typename internal::conditional<_allCols, internal::empty_struct, BlockIndex::segments_t>::type ColIndices_t;
159  };
160 
161  template <bool _allRows, bool _allCols>
162  struct traits< MatrixBlocksRef <_allRows, _allCols> >
163  {
164  enum {
165  AllRows = _allRows,
166  AllCols = _allCols
167  };
168  typedef typename internal::conditional<_allRows, internal::empty_struct, const BlockIndex::segments_t&>::type RowIndices_t;
169  typedef typename internal::conditional<_allCols, internal::empty_struct, const BlockIndex::segments_t&>::type ColIndices_t;
170  };
171 
172  template <typename ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols>
173  struct traits< MatrixBlockView <ArgType, _Rows, _Cols, _allRows, _allCols> >
174  {
175 # if HPP_EIGEN_USE_EVALUATOR
176  typedef typename ArgType::StorageIndex StorageIndex;
177 # else // HPP_EIGEN_USE_EVALUATOR
178  typedef typename ArgType::Index Index;
179 # endif // HPP_EIGEN_USE_EVALUATOR
180  typedef typename traits<ArgType>::StorageKind StorageKind;
181  typedef typename traits<ArgType>::XprKind XprKind;
182  typedef typename ArgType::Scalar Scalar;
183  enum {
184 # if !HPP_EIGEN_USE_EVALUATOR
185  CoeffReadCost = ArgType::CoeffReadCost,
186 # endif // !HPP_EIGEN_USE_EVALUATOR
187  Flags = ~PacketAccessBit & ~DirectAccessBit & ~ActualPacketAccessBit & ~LinearAccessBit & ArgType::Flags,
188  RowsAtCompileTime = (_allRows ? ArgType::RowsAtCompileTime : _Rows),
189  ColsAtCompileTime = (_allCols ? ArgType::ColsAtCompileTime : _Cols),
190  MaxRowsAtCompileTime = ArgType::MaxRowsAtCompileTime,
191  MaxColsAtCompileTime = ArgType::MaxColsAtCompileTime
192  };
193  };
194 
195 # if HPP_EIGEN_USE_EVALUATOR
196  template<typename Derived, typename ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols, typename Functor, typename Scalar>
197  struct Assignment<Derived, MatrixBlockView <ArgType, _Rows, _Cols, _allRows, _allCols>, Functor, Dense2Dense, Scalar> {
199  static EIGEN_STRONG_INLINE void run(Derived& dst, const OtherDerived& src, const Functor& func) {
200  dst.resize(src.rows(), src.cols());
201  typedef Block<Derived> BlockDerived;
202  typedef Assignment<BlockDerived, typename OtherDerived::BlockConstXprType, Functor> AssignmentType;
203  for (typename OtherDerived::block_iterator b (src); b.valid(); ++b) {
204  BlockDerived bdst (dst.block(b.ro(), b.co(), b.rs(), b.cs()));
205  AssignmentType::run(bdst, src._block(b), func);
206  }
207  }
208  };
209 # else // HPP_EIGEN_USE_EVALUATOR
210  template<typename Derived, typename ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols>
211  struct assign_selector<Derived, MatrixBlockView <ArgType, _Rows, _Cols, _allRows, _allCols>,false,false> {
213  static EIGEN_STRONG_INLINE Derived& run(Derived& dst, const OtherDerived& other) { other.writeTo(dst); return dst; }
214  template<typename ActualDerived, typename ActualOtherDerived>
215  static EIGEN_STRONG_INLINE Derived& evalTo(ActualDerived& dst, const ActualOtherDerived& other) { other.evalTo(dst); return dst; }
216  };
217  template<typename Derived, typename ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols>
218  struct assign_selector<Derived, MatrixBlockView <ArgType, _Rows, _Cols, _allRows, _allCols>,false,true> {
220  static EIGEN_STRONG_INLINE Derived& run(Derived& dst, const OtherDerived& other) { other.writeTo(dst.transpose()); return dst; }
221  template<typename ActualDerived, typename ActualOtherDerived>
222  static EIGEN_STRONG_INLINE Derived& evalTo(ActualDerived& dst, const ActualOtherDerived& other) { Transpose<ActualDerived> dstTrans(dst); other.evalTo(dstTrans); return dst; }
223  };
224 # endif // HPP_EIGEN_USE_EVALUATOR
225 
226  template <typename Src, typename Dst> struct eval_matrix_block_view_to {};
227  template <typename Src, typename _ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols>
228  struct eval_matrix_block_view_to <Src, MatrixBlockView<_ArgType, _Rows, _Cols, _allRows, _allCols> > {
229  // MatrixBlockView <- matrix
231  static void run (const Src& src, Dst& dst) {
232  for (typename Dst::block_iterator b (dst); b.valid(); ++b)
233  dst._block(b) = src.block(b.ro(), b.co(), b.rs(), b.cs());
234  }
235  };
236  template <typename _ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols, typename Dst>
237  struct eval_matrix_block_view_to <MatrixBlockView<_ArgType, _Rows, _Cols, _allRows, _allCols>, Dst > {
238  // matrix <- MatrixBlockView
240  static void run (const Src& src, Dst& dst) {
241  for (typename Src::block_iterator b (src); b.valid(); ++b)
242  dst.block(b.ro(), b.co(), b.rs(), b.cs()) = src._block(b);
243  }
244  };
245  template <typename _ArgType , int _Rows , int _Cols , bool _allRows , bool _allCols ,
246  typename _ArgType2, int _Rows2, int _Cols2, bool _allRows2, bool _allCols2>
248  MatrixBlockView<_ArgType , _Rows , _Cols , _allRows , _allCols >,
249  MatrixBlockView<_ArgType2, _Rows2, _Cols2, _allRows2, _allCols2> > {
250  // MatrixBlockView <- MatrixBlockView
253  static void run (const Src& src, Dst& dst) {
254  typename Dst::block_iterator db (dst);
255  for (typename Src::block_iterator sb (src); sb.valid(); ++sb) {
256  assert (db.valid());
257  dst._block(db) = src._block(sb);
258  ++db;
259  }
260  assert (!db.valid());
261  }
262  };
263 
264  template <typename ReturnType, typename View, bool AllRows = View::AllRows, bool AllCols = View::AllCols>
266  {
267  typedef typename View::size_type size_type;
268  template <typename Derived>
269  static ReturnType run (Derived& d, size_type r, size_type c, size_type rs, size_type cs)
270  {
271  return ReturnType (d, r, c, rs, cs);
272  }
273  };
274  template <typename ReturnType, typename View>
275  struct access_block_from_matrix_block_view <ReturnType, View, false, true>
276  {
277  typedef typename View::size_type size_type;
278  template <typename Derived>
279  static ReturnType run (Derived& d, size_type r, size_type, size_type rs, size_type)
280  {
281  return d.middleRows (r, rs);
282  }
283  };
284  template <typename ReturnType, typename View>
285  struct access_block_from_matrix_block_view <ReturnType, View, true, false>
286  {
287  typedef typename View::size_type size_type;
288  template <typename Derived>
289  static ReturnType run (Derived& d, size_type, size_type c, size_type, size_type cs)
290  {
291  return d.middleCols (c, cs);
292  }
293  };
294 
295  struct dont_print_indices { template <typename BlockIndexType> static void run (std::ostream&, const BlockIndexType&) {} };
296  struct print_indices {
297  template <typename BlockIndexType>
298  static void run (std::ostream& os, const BlockIndexType& bi) {
299  for (std::size_t i = 0; i < bi.size(); ++i)
300  os << "[ " << bi[i].first << ", " << bi[i].second << "], ";
301  }
302  };
303 
304 # if HPP_EIGEN_USE_EVALUATOR
305  template <typename ArgType, int _Rows, int _Cols, bool _allRows, bool _allCols>
306  struct unary_evaluator <MatrixBlockView <ArgType, _Rows, _Cols, _allRows, _allCols> >
307  : evaluator_base <MatrixBlockView <ArgType, _Rows, _Cols, _allRows, _allCols> >
308  {
310 
311  enum {
312  CoeffReadCost = evaluator<ArgType>::CoeffReadCost,
313  Flags = ~PacketAccessBit & ~DirectAccessBit & ~ActualPacketAccessBit & ~LinearAccessBit & ArgType::Flags,
314  Alignment = 0
315  };
316  EIGEN_DEVICE_FUNC explicit unary_evaluator (const XprType& view)
317  : m_view (view)
318  {}
319 
320  const XprType& m_view;
321  };
322 # endif // HPP_EIGEN_USE_EVALUATOR
323  } // namespace internal
324 
325 #define EIGEN_MATRIX_BLOCKS_PUBLIC_INTERFACE(Derived) \
326  enum { \
327  AllRows = _allRows, \
328  AllCols = _allCols \
329  }; \
330  typedef MatrixBlocksBase<Derived> Base; \
331  typedef typename Base::size_type size_type; \
332  typedef typename Base::segments_t segments_t; \
333  typedef typename Base::segment_t segment_t; \
334  typedef typename Base::RowIndices_t RowIndices_t; \
335  typedef typename Base::ColIndices_t ColIndices_t;
336 
339 
340  template <typename Derived>
342  {
343  public:
344  enum {
345  AllRows = internal::traits<Derived>::AllRows,
346  AllCols = internal::traits<Derived>::AllCols,
347  OneDimension = bool(AllRows) || bool(AllCols)
348  };
355  typedef typename internal::traits<Derived>::RowIndices_t RowIndices_t;
356  typedef typename internal::traits<Derived>::ColIndices_t ColIndices_t;
357 
359  template <typename MatrixType, int _Rows = MatrixType::RowsAtCompileTime, int _Cols = MatrixType::ColsAtCompileTime> struct View {
361  }; // struct View
362 
363  Derived const& derived () const { return static_cast<Derived const&> (*this); }
364  Derived & derived () { return static_cast<Derived &> (*this); }
365 
370  template <typename MatrixType>
371  EIGEN_STRONG_INLINE typename View<MatrixType>::type lview(const MatrixBase<MatrixType>& other) const {
372  MatrixType& o = const_cast<MatrixBase<MatrixType>&>(other).derived();
373  if (Derived::OneDimension)
374  return typename View<MatrixType>::type (o, nbIndices(), indices());
375  else
376  return typename View<MatrixType>::type (o, nbRows(), rows(), nbCols(), cols());
377  }
378 
383  template <typename MatrixType>
384  EIGEN_STRONG_INLINE typename View<const MatrixType>::type rview(const MatrixBase<MatrixType>& other) const {
385  if (Derived::OneDimension)
386  return typename View<const MatrixType>::type (other.derived(), nbIndices(), indices());
387  else
388  return typename View<const MatrixType>::type (other.derived(), nbRows(), rows(), nbCols(), cols());
389  }
390 
392  {
393  return MatrixBlocksRef<AllCols, AllRows> (nbCols(), cols(), nbRows(), rows());
394  }
395 
397  {
398  assert (!AllRows);
399  return MatrixBlocksRef<AllRows, true> (nbRows(), rows());
400  }
401 
403  {
404  assert (!AllCols);
405  return MatrixBlocksRef<true, AllCols> (nbCols(), cols());
406  }
407 
413  inline const segments_t& indices() const
414  {
415  return internal::return_first<AllRows>::run(cols(), rows());
416  }
417 
420  inline const RowIndices_t& rows() const
421  {
422  return derived().rows();
423  }
424 
427  inline const ColIndices_t& cols() const
428  {
429  return derived().cols();
430  }
431 
437  inline const size_type& nbIndices() const
438  {
439  return internal::return_first<AllRows>::run(nbCols(), nbRows());
440  }
441 
444  inline const size_type& nbRows() const
445  {
446  return derived().nbRows();
447  }
448 
451  inline const size_type& nbCols() const
452  {
453  return derived().nbCols();
454  }
455 
460  (size_type i, size_type j, size_type ni, size_type nj) const
461  {
462  return MatrixBlocks <AllRows, AllCols> (BlockIndex::extract (rows (), i, ni),
463  BlockIndex::extract (cols (), j, nj));
464  }
465 
470  (size_type i, size_type ni) const
471  {
472  return MatrixBlocks <AllRows, AllCols> (BlockIndex::extract (rows (), i, ni),
473  cols ());
474  }
475 
480  (size_type j, size_type nj) const
481  {
482  return MatrixBlocks <AllRows, AllCols> (rows (),
483  BlockIndex::extract (cols (), j, nj));
484  }
485 
486  protected:
489 
492  }; // class MatrixBlocks
493 
494  template <bool _allRows, bool _allCols>
495  class MatrixBlocks : public MatrixBlocksBase < MatrixBlocks <_allRows, _allCols> >
496  {
497  public:
499 
500 
501  MatrixBlocks () : m_nbRows(0), m_nbCols(0), m_rows(), m_cols() {}
502 
507  MatrixBlocks (const segments_t& rows,
508  const segments_t& cols) :
509  m_nbRows(BlockIndex::cardinal(rows)),
510  m_nbCols(BlockIndex::cardinal(cols)), m_rows(rows), m_cols(cols)
511  {
512 # ifndef NDEBUG
513  // test that input is sorted
514  segments_t r (rows); BlockIndex::sort (r);
515  assert (r == rows);
516  segments_t c (cols); BlockIndex::sort (c);
517  assert (c == cols);
518 #endif
519  }
520 
527  MatrixBlocks (const size_type& nbRows, const RowIndices_t& rows,
528  const size_type& nbCols, const ColIndices_t& cols) :
529  m_nbRows(nbRows), m_nbCols(nbCols), m_rows(rows), m_cols(cols)
530  {
531 # ifndef NDEBUG
532  // test that input is sorted
533  segments_t r (rows); BlockIndex::sort (r);
534  assert (r == rows);
535  segments_t c (cols); BlockIndex::sort (c);
536  assert (c == cols);
537 #endif
538  }
539 
545  MatrixBlocks (size_type start, size_type size)
546  : m_nbRows(_allRows ? 0 : size)
547  , m_nbCols(_allCols ? 0 : size)
548  , m_rows(1, BlockIndex::segment_t (start, size))
549  , m_cols(1, BlockIndex::segment_t (start, size))
550  {}
551 
557  MatrixBlocks (const segments_t& idx)
558  : m_nbRows(_allRows ? 0 : BlockIndex::cardinal(idx))
559  , m_nbCols(_allCols ? 0 : BlockIndex::cardinal(idx))
560  , m_rows(idx), m_cols(idx)
561  {}
562 
567  MatrixBlocks (const segment_t& idx)
568  : m_nbRows(_allRows ? 0 : idx.second)
569  , m_nbCols(_allCols ? 0 : idx.second)
570  , m_rows(segments_t(1,idx)), m_cols(segments_t(1,idx))
571  {}
572 
574  template <typename MBDerived>
576  : m_nbRows(other.nbRows())
577  , m_nbCols(other.nbCols())
578  , m_rows(other.rows()), m_cols(other.cols())
579  {
580  EIGEN_STATIC_ASSERT(
581  (bool(AllRows) == bool(MBDerived::AllRows)) && (bool(AllCols) == bool(MBDerived::AllCols)),
582  YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES);
583  }
584 
586  inline void clearRows ()
587  {
588  m_rows.clear();
589  m_nbRows = 0;
590  }
591 
593  inline void clearCols ()
594  {
595  m_cols.clear();
596  m_nbCols = 0;
597  }
598 
602  inline void addRow (const size_type& row, const size_type size)
603  {
604  m_rows.push_back(segment_t (row, size));
605  m_nbRows += size;
606  }
607 
611  inline void addCol (const size_type& col, const size_type size)
612  {
613  m_cols.push_back(segment_t (col, size));
614  m_nbCols += size;
615  }
616 
621  template<bool Sort, bool Shrink, bool Cardinal>
622  inline void updateRows() {
623  update<Sort, Shrink, Cardinal> (m_rows, m_nbRows);
624  }
625 
630  template<bool Sort, bool Shrink, bool Cardinal>
631  inline void updateCols() {
632  update<Sort, Shrink, Cardinal> (m_cols, m_nbCols);
633  }
634 
637  inline const RowIndices_t& rows() const
638  {
639  return m_rows;
640  }
641 
644  inline const ColIndices_t& cols() const
645  {
646  return m_cols;
647  }
648 
651  inline const size_type& nbRows() const
652  {
653  return m_nbRows;
654  }
655 
658  inline const size_type& nbCols() const
659  {
660  return m_nbCols;
661  }
662 
663  template<bool Sort, bool Shrink, bool Cardinal>
664  inline void updateIndices() {
665  update<Sort, Shrink, Cardinal> (
666  internal::return_first<_allRows>::run(m_cols , m_rows ),
667  internal::return_first<_allRows>::run(m_nbCols, m_nbRows));
668  }
669 
670  size_type m_nbRows, m_nbCols;
671  RowIndices_t m_rows;
672  ColIndices_t m_cols;
673 
674  private:
675  template<bool Sort, bool Shrink, bool Cardinal>
676  static inline void update(segments_t& b, size_type& idx) {
677  if (Sort) BlockIndex::sort(b);
678  if (Shrink) BlockIndex::shrink(b);
679  if (Cardinal) idx = BlockIndex::cardinal(b);
680  }
681  }; // class MatrixBlocks
682 
684  template <bool _allRows, bool _allCols>
685  class MatrixBlocksRef : public MatrixBlocksBase < MatrixBlocksRef <_allRows, _allCols> >
686  {
687  public:
689 
690 
691  MatrixBlocksRef (const size_type& nbRows, const RowIndices_t& rows,
695  const size_type& nbCols, const ColIndices_t& cols) :
696  m_nbRows(nbRows), m_nbCols(nbCols),
697  m_rows (rows ), m_cols (cols )
698  {}
699 
701  template <typename Derived1, typename Derived2>
703  const MatrixBlocksBase<Derived2>& cols) :
704  m_nbRows(rows.nbIndices()), m_nbCols(cols.nbIndices()),
705  m_rows (rows. indices()), m_cols (cols. indices())
706  {
707  EIGEN_STATIC_ASSERT( bool(Derived1::OneDimension) && bool (Derived2::OneDimension),
708  YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES);
709  }
710 
716  MatrixBlocksRef (const size_type& nidx, const segments_t& idx)
717  : m_nbRows(_allRows ? 0 : nidx)
718  , m_nbCols(_allCols ? 0 : nidx)
719  , m_rows(idx), m_cols(idx)
720  {}
721 
723  MatrixBlocksRef (const MatrixBlocksRef& other)
724  : Base (other)
725  , m_nbRows(other.nbRows())
726  , m_nbCols(other.nbCols())
727  , m_rows(other.rows())
728  , m_cols(other.cols())
729  {}
730 
733  inline const RowIndices_t& rows() const
734  {
735  return m_rows;
736  }
737 
740  inline const ColIndices_t& cols() const
741  {
742  return m_cols;
743  }
744 
747  inline const size_type& nbRows() const
748  {
749  return m_nbRows;
750  }
751 
754  inline const size_type& nbCols() const
755  {
756  return m_nbCols;
757  }
758 
759  const size_type m_nbRows, m_nbCols;
760  RowIndices_t m_rows;
761  ColIndices_t m_cols;
762  }; // class MatrixBlocksRef
764 
765  template <typename Derived>
766  std::ostream& operator<< (std::ostream& os, const MatrixBlocksBase<Derived>& mbi)
767  {
768  typedef typename internal::conditional<Derived::AllRows, internal::dont_print_indices, internal::print_indices>::type row_printer;
769  typedef typename internal::conditional<Derived::AllCols, internal::dont_print_indices, internal::print_indices>::type col_printer;
770  if (!Derived::AllRows) {
771  os << "Rows: ";
772  row_printer::run (os, mbi.rows());
773  if (!Derived::AllCols) os << hpp::iendl;
774  }
775  if (!Derived::AllCols) {
776  os << "Cols: ";
777  col_printer::run (os, mbi.cols());
778  }
779  return os;
780  }
781 
784 
811  template <typename _ArgType, int _Rows = _ArgType::RowsAtCompileTime, int _Cols = _ArgType::ColsAtCompileTime, bool _allRows = false, bool _allCols = false>
812  class MatrixBlockView : public MatrixBase< MatrixBlockView<_ArgType, _Rows, _Cols, _allRows, _allCols> >
813  {
814  public:
816  enum {
817  Rows = _Rows,
818  Cols = _Cols,
819  AllRows = _allRows,
820  AllCols = _allCols
821  };
822  struct block_iterator {
824  size_type row, col;
825  internal::variable_if_dynamic<size_type, (_allRows ? 0 : Dynamic) > _ro;
826  internal::variable_if_dynamic<size_type, (_allCols ? 0 : Dynamic) > _co;
827  block_iterator (const MatrixBlockView& v) : view(v), row (0), col (0), _ro(0), _co(0) {}
829  size_type ro() const { return _ro.value(); }
831  size_type co() const { return _co.value(); }
833  size_type ri() const { return internal::get_if<AllRows>::run(std::make_pair(0,view.m_nbRows), view.m_rows[row]).first; }
835  size_type ci() const { return internal::get_if<AllCols>::run(std::make_pair(0,view.m_nbCols), view.m_cols[col]).first; }
837  size_type rs() const { return internal::get_if<AllRows>::run(std::make_pair(0,view.m_nbRows), view.m_rows[row]).second; }
839  size_type cs() const { return internal::get_if<AllCols>::run(std::make_pair(0,view.m_nbCols), view.m_cols[col]).second; }
840  // ++it
842  {
843  _ro.setValue(_ro.value() + rs());
844  ++row;
845  if (row == (size_type)view.m_rows.size()) {
846  row = 0;
847  _ro.setValue(0);
848  _co.setValue(_co.value() + cs());
849  ++col;
850  // if (col < (size_type)view.m_cols.size()) _co.setValue(0);
851  }
852  return *this;
853  };
854  // it++
855  block_iterator operator++(int) { block_iterator copy(*this); operator++(); return copy; };
856  bool valid () const {
857  return (internal::get_if<AllRows>::run(true, view.m_rows.size()>0))
858  && (col < (size_type)internal::get_if<AllCols>::run(1, view.m_cols.size()));
859  }
860  };
861  typedef MatrixBase< MatrixBlockView<_ArgType, _Rows, _Cols, _allRows, _allCols> > Base;
862  EIGEN_GENERIC_PUBLIC_INTERFACE(MatrixBlockView)
863 
864  typedef Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime> PlainObject;
865  // typedef typename internal::ref_selector<MatrixBlockView>::type Nested;
866  typedef _ArgType ArgType;
867  typedef typename internal::ref_selector<ArgType>::type ArgTypeNested;
868  typedef typename internal::remove_all<ArgType>::type NestedExpression;
869  // typedef typename Base::CoeffReturnType CoeffReturnType;
870  // typedef typename Base::Scalar Scalar;
871 
872  template <typename Derived>
873  struct block_t {
874  typedef Block<Derived,
875  (AllRows ? Derived::RowsAtCompileTime : Eigen::Dynamic),
876  (AllCols ? Derived::ColsAtCompileTime : Eigen::Dynamic),
877  (AllCols ? (bool)Derived::IsRowMajor
878  : (AllRows ? (bool)!Derived::IsRowMajor : false)
879  )> type ;
880  };
883 
886  typedef typename internal::conditional<_allRows, const internal::empty_struct, const Indices_t& >::type RowIndices_t;
887  typedef typename internal::conditional<_allCols, const internal::empty_struct, const Indices_t& >::type ColIndices_t;
888 
889  // using Base::operator=;
890 
891  MatrixBlockView (ArgType& arg, const size_type& nbRows,
892  const RowIndices_t rows, const size_type& nbCols,
893  const ColIndices_t cols) :
894  m_arg (arg), m_nbRows(nbRows), m_rows(rows), m_nbCols(nbCols),
895  m_cols(cols)
896  {
897  }
898 
900  MatrixBlockView (ArgType& arg, const size_type& nbIndices,
901  const Indices_t& indices) :
902  m_arg (arg), m_nbRows(_allRows ? arg.rows() : nbIndices),
903  m_rows(indices), m_nbCols(_allCols ? arg.cols() : nbIndices),
904  m_cols(indices)
905  {}
906 
907  EIGEN_STRONG_INLINE size_type rows() const { return m_nbRows; }
908  EIGEN_STRONG_INLINE size_type cols() const { return m_nbCols; }
909 
910  EIGEN_STRONG_INLINE CoeffReturnType coeff (size_type index) const
911  {
912  assert(false && "It is not possible to access the coefficients of "
913  "MatrixBlockView this way.");
914  }
915  EIGEN_STRONG_INLINE CoeffReturnType coeff (size_type row, size_type col)
916  const
917  {
918  assert(false && "It is not possible to access the coefficients of "
919  "MatrixBlockView this way.");
920  }
921  EIGEN_STRONG_INLINE Scalar& coeffRef (size_type index)
922  {
923  assert(false && "It is not possible to access the coefficients of "
924  "MatrixBlockView this way.");
925  }
926  EIGEN_STRONG_INLINE Scalar& coeffRef (size_type row, const size_type& col)
927  {
928  assert(false && "It is not possible to access the coefficients of "
929  "MatrixBlockView this way.");
930  }
931  template <typename Dest>
932  EIGEN_STRONG_INLINE void evalTo (Dest& dst) const {
934  }
935 
936  template <typename Dest>
937  EIGEN_STRONG_INLINE void writeTo (Dest& dst) const {
938  dst.resize(rows(), cols());
939  evalTo(dst.derived());
940  }
941 
942  EIGEN_STRONG_INLINE PlainObject eval () const {
943  PlainObject dst;
944  writeTo(dst);
945  return dst;
946  }
947 
948  template <typename OtherDerived>
949  EIGEN_STRONG_INLINE MatrixBlockView& operator= (const EigenBase<OtherDerived>& other) {
950  EIGEN_STATIC_ASSERT_LVALUE(ArgType);
952  return *this;
953  }
954 
955  EIGEN_STRONG_INLINE size_type _blocks() const { return m_rows.size() * m_cols.size(); }
956  EIGEN_STRONG_INLINE BlockXprType _block(const block_iterator& b)
957  {
959  ::template run <ArgType> (m_arg, b.ri(), b.ci(), b.rs(), b.cs());
960  }
961  EIGEN_STRONG_INLINE const BlockConstXprType _block(const block_iterator& b) const
962  {
964  ::template run <const ArgType> (m_arg, b.ri(), b.ci(), b.rs(), b.cs());
965  }
966  EIGEN_STRONG_INLINE block_iterator _block_iterator() const { return block_iterator(*this); }
967 
968  EIGEN_STRONG_INLINE bool isZero (const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
969  {
970  for (block_iterator block (*this); block.valid(); ++block)
971  if (!m_arg.block(
972  block.ri(), block.ci(),
973  block.rs(), block.cs())
974  .isZero(prec))
975  return false;
976  return true;
977  }
978 
980  size_type m_nbRows;
981  RowIndices_t m_rows;
982  size_type m_nbCols;
983  ColIndices_t m_cols;
984  }; // MatrixBlockView
985 
987 
988 } // namespace Eigen
989 
992 
993 # undef HPP_EIGEN_USE_EVALUATOR
994 
995 namespace hpp {
996  template <int Option>
997  struct prettyPrint<constraints::segment_t, Option> {
998  static std::ostream& run (std::ostream& os, const constraints::segment_t& s) {
999  return os << "[ " << s.first << ", " << s.first + s.second << " ]";
1000  }
1001  };
1002 }
1003 
1004 #endif // HPP_CONSTRAINTS_MATRIX_VIEW_HH
static segments_t split(segments_t &segments, const size_type &cardinal)
void addCol(const size_type &col, const size_type size)
Definition: matrix-view.hh:611
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > PlainObject
Definition: matrix-view.hh:864
MatrixBlocksRef< AllCols, AllRows > transpose() const
Definition: matrix-view.hh:391
MatrixBlockView< ArgType, _Rows, _Cols, _allRows, _allCols > OtherDerived
Definition: matrix-view.hh:219
internal::variable_if_dynamic< size_type,(_allRows ? 0 :Dynamic) > _ro
Definition: matrix-view.hh:825
Definition: matrix-view.hh:127
MatrixIndices_t::segments_t Indices_t
Definition: matrix-view.hh:885
MatrixXd::Index Index
Definition: matrix-view.hh:140
const RowIndices_t & rows() const
Definition: matrix-view.hh:420
static ReturnType run(Derived &d, size_type r, size_type, size_type rs, size_type)
Definition: matrix-view.hh:279
const MatrixBlockView & view
Definition: matrix-view.hh:823
Definition: matrix-view.hh:123
MatrixBlocks(size_type start, size_type size)
Definition: matrix-view.hh:545
size_type cs() const
number of ColS
Definition: matrix-view.hh:839
MatrixBlocksRef< true, AllCols > keepCols() const
Definition: matrix-view.hh:402
const size_type & nbCols() const
Definition: matrix-view.hh:658
internal::variable_if_dynamic< size_type,(_allCols ? 0 :Dynamic) > _co
Definition: matrix-view.hh:826
MatrixBlockView(ArgType &arg, const size_type &nbRows, const RowIndices_t rows, const size_type &nbCols, const ColIndices_t cols)
Definition: matrix-view.hh:891
static size_type cardinal(const segments_t &a)
Definition: matrix-view.hh:37
empty_struct(In0_t, In1_t)
Definition: matrix-view.hh:143
Definition: matrix-view.hh:130
internal::conditional< _allRows, internal::empty_struct, const BlockIndex::segments_t & >::type RowIndices_t
Definition: matrix-view.hh:168
size_type ro() const
Row in the Output matrix
Definition: matrix-view.hh:829
Vec3f b
const ColIndices_t & cols() const
Definition: matrix-view.hh:427
const Derived & d
Definition: matrix-view-operation.hh:126
Block< Derived,(AllRows ? Derived::RowsAtCompileTime :Eigen::Dynamic),(AllCols ? Derived::ColsAtCompileTime :Eigen::Dynamic),(AllCols ?(bool) Derived::IsRowMajor :(AllRows ?(bool)!Derived::IsRowMajor :false))> type
Definition: matrix-view.hh:879
traits< ArgType >::XprKind XprKind
Definition: matrix-view.hh:181
HPP_UTIL_DLLAPI std::ostream & iendl(std::ostream &o)
block_iterator(const MatrixBlockView &v)
Definition: matrix-view.hh:827
EIGEN_STRONG_INLINE BlockXprType _block(const block_iterator &b)
Definition: matrix-view.hh:956
Definition: matrix-view.hh:296
MatrixBase< MatrixBlockView< _ArgType, _Rows, _Cols, _allRows, _allCols > > Base
Definition: matrix-view.hh:861
Derived & derived()
Definition: matrix-view.hh:364
EIGEN_STRONG_INLINE void evalTo(Dest &dst) const
Definition: matrix-view.hh:932
void updateIndices()
Definition: matrix-view.hh:664
block_t< ArgType >::type BlockXprType
Definition: matrix-view.hh:881
MatrixBlockView< _ArgType, _Rows, _Cols, _allRows, _allCols > Dst
Definition: matrix-view.hh:230
EIGEN_STRONG_INLINE block_iterator _block_iterator() const
Definition: matrix-view.hh:966
size_type m_nbRows
Definition: matrix-view.hh:670
Definition: matrix-view.hh:147
MatrixBlockView< MatrixType, _Rows, _Cols, AllRows, AllCols > type
Definition: matrix-view.hh:360
MatrixBlocks(const segments_t &idx)
Definition: matrix-view.hh:557
static First & run(First &f, Second &)
Definition: matrix-view.hh:132
MatrixBlocks(const size_type &nbRows, const RowIndices_t &rows, const size_type &nbCols, const ColIndices_t &cols)
Definition: matrix-view.hh:527
Definition: fwd.hh:26
internal::remove_all< ArgType >::type NestedExpression
Definition: matrix-view.hh:868
static EIGEN_STRONG_INLINE Derived & evalTo(ActualDerived &dst, const ActualOtherDerived &other)
Definition: matrix-view.hh:215
std::vector< segment_t > segments_t
vector of segments
Definition: matrix-view.hh:43
static segments_t difference(const segment_t &a, const segment_t &b)
Compute the set difference between two segments.
size_type co() const
Col in the Output matrix
Definition: matrix-view.hh:831
#define EIGEN_MATRIX_BLOCKS_PUBLIC_INTERFACE(Derived)
Definition: matrix-view.hh:325
FCL_REAL r
void clearRows()
Clear rows.
Definition: matrix-view.hh:586
hpp::constraints::size_type size_type
Definition: matrix-view.hh:815
std::pair< size_type, size_type > segment_t
Definition: fwd.hh:68
static EIGEN_STRONG_INLINE Derived & evalTo(ActualDerived &dst, const ActualOtherDerived &other)
Definition: matrix-view.hh:222
MatrixBlockView< ArgType, _Rows, _Cols, _allRows, _allCols > OtherDerived
Definition: matrix-view.hh:212
ColIndices_t m_cols
Definition: matrix-view.hh:672
static ReturnType run(Derived &d, size_type r, size_type c, size_type rs, size_type cs)
Definition: matrix-view.hh:269
EIGEN_STRONG_INLINE size_type rows() const
Definition: matrix-view.hh:907
void addRow(const size_type &row, const size_type size)
Definition: matrix-view.hh:602
EIGEN_STRONG_INLINE size_type _blocks() const
Definition: matrix-view.hh:955
const size_type & nbCols() const
Definition: matrix-view.hh:451
Vec3f c
MatrixBlocksBase()
Empty constructor.
Definition: matrix-view.hh:488
static void run(std::ostream &os, const BlockIndexType &bi)
Definition: matrix-view.hh:298
EIGEN_STRONG_INLINE Scalar & coeffRef(size_type index)
Definition: matrix-view.hh:921
EIGEN_STRONG_INLINE void writeTo(Dest &dst) const
Definition: matrix-view.hh:937
MatrixBlocks(const segment_t &idx)
Definition: matrix-view.hh:567
block_iterator operator++(int)
Definition: matrix-view.hh:855
bool valid() const
Definition: matrix-view.hh:856
block_iterator & operator++()
Definition: matrix-view.hh:841
Definition: matrix-view.hh:139
EIGEN_STRONG_INLINE const BlockConstXprType _block(const block_iterator &b) const
Definition: matrix-view.hh:961
MatrixBlocks< _allRows, _allCols > MatrixIndices_t
Definition: matrix-view.hh:884
hpp::constraints::size_type size_type
Index of vector or matrix.
Definition: matrix-view.hh:39
Eigen::MatrixBlocks< false, true > RowBlockIndices
Definition: matrix-view.hh:782
static void shrink(segments_t &a)
assert(d.lhs()._blocks()==d.rhs()._blocks())
static void add(segments_t &a, const segment_t &b)
In place addition of a segment_t to segments_t.
Definition: matrix-view.hh:341
Definition: matrix-view.hh:111
static segments_t fromLogicalExpression(const Eigen::ArrayBase< Derived > &array)
Definition: matrix-view.hh:20
const size_type & nbIndices() const
Definition: matrix-view.hh:437
size_type ri() const
Row in the Input matrix
Definition: matrix-view.hh:833
empty_struct()
Definition: matrix-view.hh:141
BlockIndex::segments_t segments_t
vector of segments
Definition: matrix-view.hh:354
size_type ci() const
Col in the Input matrix
Definition: matrix-view.hh:835
Definition: matrix-view.hh:295
RowIndices_t m_rows
Definition: matrix-view.hh:671
EIGEN_STRONG_INLINE Scalar & coeffRef(size_type row, const size_type &col)
Definition: matrix-view.hh:926
internal::conditional< _allRows, const internal::empty_struct, const Indices_t &>::type RowIndices_t
Definition: matrix-view.hh:886
block_t< const ArgType >::type BlockConstXprType
Definition: matrix-view.hh:882
static EIGEN_STRONG_INLINE T1 run(T1 then, T2 Else)
Definition: matrix-view.hh:147
View::size_type size_type
Definition: matrix-view.hh:267
const size_type & nbRows() const
Definition: matrix-view.hh:651
static void sort(segments_t &a)
const segments_t & indices() const
Definition: matrix-view.hh:413
EIGEN_STRONG_INLINE View< const MatrixType >::type rview(const MatrixBase< MatrixType > &other) const
Definition: matrix-view.hh:384
static Index size()
Definition: matrix-view.hh:144
Vec3f a
ColIndices_t m_cols
Definition: matrix-view.hh:983
_ArgType ArgType
Definition: matrix-view.hh:866
MatrixBlockView< _ArgType, _Rows, _Cols, _allRows, _allCols > Src
Definition: matrix-view.hh:239
ArgType & m_arg
Definition: matrix-view.hh:979
Definition: matrix-view.hh:822
static EIGEN_STRONG_INLINE Derived & run(Derived &dst, const OtherDerived &other)
Definition: matrix-view.hh:220
const Index & operator[](const Index &i) const
Definition: matrix-view.hh:145
MatrixBlocks(const MatrixBlocksBase< MBDerived > &other)
Copy constructor.
Definition: matrix-view.hh:575
RowIndices_t m_rows
Definition: matrix-view.hh:981
EIGEN_STRONG_INLINE CoeffReturnType coeff(size_type index) const
Definition: matrix-view.hh:910
size_type m_nbRows
Definition: matrix-view.hh:980
EIGEN_STRONG_INLINE PlainObject eval() const
Definition: matrix-view.hh:942
static EIGEN_STRONG_INLINE T2 run(T1 then, T2 Else)
Definition: matrix-view.hh:148
internal::conditional< _allCols, internal::empty_struct, const BlockIndex::segments_t & >::type ColIndices_t
Definition: matrix-view.hh:169
std::pair< size_type, size_type > segment_t
Interval of indices [first, first + second - 1].
Definition: matrix-view.hh:41
EIGEN_STRONG_INLINE View< MatrixType >::type lview(const MatrixBase< MatrixType > &other) const
Definition: matrix-view.hh:371
static bool overlap(const segment_t &a, const segment_t &b)
Whether two segments overlap.
static void run(std::ostream &, const BlockIndexType &)
Definition: matrix-view.hh:295
size_type m_nbCols
Definition: matrix-view.hh:982
MatrixBlocksRef< AllRows, true > keepRows() const
Definition: matrix-view.hh:396
const ColIndices_t & cols() const
Definition: matrix-view.hh:644
Eigen::MatrixBlocks< true, false > ColBlockIndices
Definition: matrix-view.hh:783
internal::traits< Derived >::ColIndices_t ColIndices_t
Definition: matrix-view.hh:356
static segments_t sum(const segment_t &a, const segment_t &b)
Compute the union of tws segments.
void clearCols()
Clear cols.
Definition: matrix-view.hh:593
EIGEN_STRONG_INLINE size_type cols() const
Definition: matrix-view.hh:908
pinocchio::size_type size_type
Definition: fwd.hh:35
Smaller matrix composed by concatenation of the blocks.
Definition: matrix-view.hh:359
static Second & run(First &, Second &s)
Definition: matrix-view.hh:136
EIGEN_STRONG_INLINE CoeffReturnType coeff(size_type row, size_type col) const
Definition: matrix-view.hh:915
internal::traits< Derived >::RowIndices_t RowIndices_t
Definition: matrix-view.hh:355
Derived const & derived() const
Definition: matrix-view.hh:363
MatrixBlockView(ArgType &arg, const size_type &nbIndices, const Indices_t &indices)
Valid only when _allRows or _allCols is true.
Definition: matrix-view.hh:900
const size_type & nbRows() const
Definition: matrix-view.hh:444
FCL_REAL size() const
empty_struct(In_t)
Definition: matrix-view.hh:142
const RowIndices_t & rows() const
Definition: matrix-view.hh:637
MatrixBlocksBase(const MatrixBlocksBase &)
Copy constructor.
Definition: matrix-view.hh:491
void updateCols()
Definition: matrix-view.hh:631
MatrixBlocks(const segments_t &rows, const segments_t &cols)
Definition: matrix-view.hh:507
static EIGEN_STRONG_INLINE Derived & run(Derived &dst, const OtherDerived &other)
Definition: matrix-view.hh:213
static std::ostream & run(std::ostream &os, const constraints::segment_t &s)
Definition: matrix-view.hh:998
internal::ref_selector< ArgType >::type ArgTypeNested
Definition: matrix-view.hh:867
hpp::constraints::size_type size_type
Index of vector or matrix.
Definition: matrix-view.hh:350
EIGEN_STRONG_INLINE bool isZero(const RealScalar &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: matrix-view.hh:968
internal::conditional< _allRows, internal::empty_struct, BlockIndex::segments_t >::type RowIndices_t
Definition: matrix-view.hh:157
traits< ArgType >::StorageKind StorageKind
Definition: matrix-view.hh:180
size_type row
Definition: matrix-view.hh:824
static segments_t extract(const segments_t &segments, size_type start, size_type cardinal)
BlockIndex::segment_t segment_t
Interval of indices [first, first + second - 1].
Definition: matrix-view.hh:352
internal::conditional< _allCols, const internal::empty_struct, const Indices_t &>::type ColIndices_t
Definition: matrix-view.hh:887
internal::conditional< _allCols, internal::empty_struct, BlockIndex::segments_t >::type ColIndices_t
Definition: matrix-view.hh:158
static ReturnType run(Derived &d, size_type, size_type c, size_type, size_type cs)
Definition: matrix-view.hh:289
Definition: matrix-view.hh:226
void updateRows()
Definition: matrix-view.hh:622
size_type rs() const
number of RowS
Definition: matrix-view.hh:837
Vec3f o
Definition: matrix-view.hh:873