pinocchio  3.2.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
delassus-operator-dense.hpp
1 //
2 // Copyright (c) 2024 INRIA
3 //
4 
5 #ifndef __pinocchio_algorithm_delassus_operator_dense_hpp__
6 #define __pinocchio_algorithm_delassus_operator_dense_hpp__
7 
8 #include "pinocchio/algorithm/fwd.hpp"
9 #include "pinocchio/algorithm/delassus-operator-base.hpp"
10 
11 namespace pinocchio
12 {
13 
14  template<typename _Scalar, int _Options>
15  struct traits<DelassusOperatorDenseTpl<_Scalar, _Options>>
16  {
17  typedef _Scalar Scalar;
18  enum
19  {
20  Options = _Options,
21  RowsAtCompileTime = Eigen::Dynamic
22  };
23 
24  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options> Matrix;
25  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> Vector;
26  };
27 
28  template<typename _Scalar, int _Options>
30  : DelassusOperatorBase<DelassusOperatorDenseTpl<_Scalar, _Options>>
31  {
32  typedef _Scalar Scalar;
34  enum
35  {
36  Options = _Options,
38  };
39 
40  typedef typename traits<Self>::Matrix Matrix;
41  typedef typename traits<Self>::Vector Vector;
42  typedef Eigen::LLT<Matrix> CholeskyDecomposition;
44 
45  template<typename MatrixDerived>
46  explicit DelassusOperatorDenseTpl(const Eigen::MatrixBase<MatrixDerived> & mat)
47  : Base(mat.rows())
48  , delassus_matrix(mat)
49  , mat_tmp(mat.rows(), mat.cols())
50  , llt(mat)
51  , damping(Vector::Zero(mat.rows()))
52  {
53  PINOCCHIO_CHECK_ARGUMENT_SIZE(mat.rows(), mat.cols());
54  }
55 
56  template<typename VectorLike>
57  void updateDamping(const Eigen::MatrixBase<VectorLike> & vec)
58  {
59  damping = vec;
60  mat_tmp = delassus_matrix;
61  mat_tmp += vec.asDiagonal();
62  llt.compute(mat_tmp);
63  }
64 
65  void updateDamping(const Scalar & mu)
66  {
67  updateDamping(Vector::Constant(size(), mu));
68  }
69 
70  template<typename MatrixLike>
71  void solveInPlace(const Eigen::MatrixBase<MatrixLike> & mat) const
72  {
73  llt.solveInPlace(mat.const_cast_derived());
74  }
75 
76  template<typename MatrixLike>
77  typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixLike)
78  solve(const Eigen::MatrixBase<MatrixLike> & mat) const
79  {
80  typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixLike) res(mat);
81  solveInPlace(res);
82  return res;
83  }
84 
85  template<typename MatrixDerivedIn, typename MatrixDerivedOut>
86  void solve(
87  const Eigen::MatrixBase<MatrixDerivedIn> & x,
88  const Eigen::MatrixBase<MatrixDerivedOut> & res) const
89  {
90  res.const_cast_derived() = x;
91  solveInPlace(res.const_cast_derived());
92  }
93 
94  template<typename MatrixIn, typename MatrixOut>
95  void applyOnTheRight(
96  const Eigen::MatrixBase<MatrixIn> & x, const Eigen::MatrixBase<MatrixOut> & res_) const
97  {
98  MatrixOut & res = res_.const_cast_derived();
99  res.noalias() = delassus_matrix * x;
100  res.array() += damping.array() * x.array();
101  }
102 
103  template<typename MatrixDerived>
104  typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixDerived)
105  operator*(const Eigen::MatrixBase<MatrixDerived> & x) const
106  {
107  typedef typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixDerived) ReturnType;
108 
109  PINOCCHIO_CHECK_ARGUMENT_SIZE(x.rows(), size());
110  ReturnType res(x.rows(), x.cols());
111  applyOnTheRight(x, res);
112  return res;
113  }
114 
115  Eigen::DenseIndex size() const
116  {
117  return delassus_matrix.rows();
118  }
119  Eigen::DenseIndex rows() const
120  {
121  return delassus_matrix.rows();
122  }
123  Eigen::DenseIndex cols() const
124  {
125  return delassus_matrix.cols();
126  }
127 
128  Matrix matrix() const
129  {
130  mat_tmp = delassus_matrix;
131  mat_tmp += damping.asDiagonal();
132  return mat_tmp;
133  }
134 
135  Matrix inverse() const
136  {
137  Matrix res = Matrix::Identity(size(), size());
138  llt.solveInPlace(res);
139  return res;
140  }
141 
142  protected:
143  Matrix delassus_matrix;
144  mutable Matrix mat_tmp;
145  CholeskyDecomposition llt;
146  Vector damping;
147 
148  }; // struct DelassusOperatorDenseTpl
149 
150 } // namespace pinocchio
151 
152 #endif // ifndef __pinocchio_algorithm_delassus_operator_dense_hpp__
Main pinocchio namespace.
Definition: treeview.dox:11
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:72