pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
constraint-cholesky.hpp
1//
2// Copyright (c) 2020-2025 INRIA
3//
4
5#pragma once
6
7#include <eigenpy/memory.hpp>
8#include <eigenpy/copyable.hpp>
9#include <eigenpy/deprecation-policy.hpp>
10
11#include "pinocchio/algorithm/constraint-cholesky.hpp"
12
13#include "pinocchio/algorithm/delassus-operator.hpp"
14
15#include "pinocchio/bindings/python/utils/macros.hpp"
16#include "pinocchio/bindings/python/utils/std-vector.hpp"
17#include "pinocchio/bindings/python/utils/comparable.hpp"
18#include "pinocchio/bindings/python/utils/model-checker.hpp"
19
20#include "pinocchio/bindings/python/algorithm/delassus-operator.hpp"
21
22namespace pinocchio
23{
24 namespace python
25 {
26 namespace bp = boost::python;
27
28 template<typename ConstraintCholeskyDecomposition>
30 : public boost::python::def_visitor<
31 ConstraintCholeskyDecompositionPythonVisitor<ConstraintCholeskyDecomposition>>
32 {
33 typedef ConstraintCholeskyDecomposition Self;
34 typedef typename Self::Scalar Scalar;
35 typedef context::RigidConstraintModel RigidConstraintModel;
36 typedef context::RigidConstraintData RigidConstraintData;
37 typedef typename Self::Matrix Matrix;
38 typedef typename Self::RowMatrix RowMatrix;
39 typedef typename Self::Vector Vector;
40
41 typedef context::ConstraintModel ConstraintModel;
42 typedef context::ConstraintData ConstraintData;
43
44 typedef Eigen::Ref<Matrix> RefMatrix;
45 typedef Eigen::Ref<RowMatrix> RefRowMatrix;
46 typedef Eigen::Ref<Vector> RefVector;
47
48 typedef typename std::vector<RigidConstraintModel> RigidConstraintModelVector;
49 typedef typename std::vector<RigidConstraintData> RigidConstraintDataVector;
50
51 typedef context::ConstraintModelVector ConstraintModelVector;
52 typedef context::ConstraintDataVector ConstraintDataVector;
53
54 typedef pinocchio::python::context::Model Model;
55 typedef pinocchio::python::context::Data Data;
56
57 template<class PyClass>
58 void visit(PyClass & cl) const
59 {
60 cl.def(bp::init<>(bp::arg("self"), "Default constructor."))
61 .def(
62 bp::init<const Model &, const Data &>(
63 bp::args("self", "model", "data"),
64 "Constructor from a model.")[mimic_not_supported_function<>(1)])
65 .def(
66 bp::init<
67 const Model &, const Data &, const RigidConstraintModelVector &,
68 const RigidConstraintDataVector>(
69 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("contact_models"),
70 bp::arg("contact_datas")),
71 "Constructor from a model and a collection of RigidConstraintModels.")
73 .def(
74 bp::init<
75 const Model &, const Data &, const ConstraintModelVector &,
76 const ConstraintDataVector>(
77 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("constraint_models"),
78 bp::arg("constraint_datas")),
79 "Constructor from a model and a collection of ConstraintModels.")
81
82 .add_property(
83 "U",
84 bp::make_function(
85 +[](const Self & self) -> RefRowMatrix { return RefRowMatrix(self.U); },
86 bp::with_custodian_and_ward_postcall<0, 1>()),
87 "")
88 .add_property(
89 "D",
90 bp::make_function(
91 +[](const Self & self) -> RefVector { return RefVector(self.D); },
92 bp::with_custodian_and_ward_postcall<0, 1>()),
93 "")
94 .add_property(
95 "Dinv",
96 bp::make_function(
97 +[](const Self & self) -> RefVector { return RefVector(self.Dinv); },
98 bp::with_custodian_and_ward_postcall<0, 1>()),
99 "")
100
101 .def("size", &Self::size, bp::arg("self"), "Size of the decomposition.")
102 .def(
103 "constraintDim", &Self::constraintDim, bp::arg("self"),
104 "Returns the total dimension of the constraints contained in the Cholesky "
105 "factorization.")
106 .def(
107 "matrix", (Matrix (Self::*)(void) const) & Self::matrix, bp::arg("self"),
108 "Returns the matrix resulting from the decomposition.")
109
110 .def(
111 "rebuild",
112 (void (*)(
113 Self & self, const Model &, const Data &, const RigidConstraintModelVector &,
114 const RigidConstraintDataVector &))&rebuild,
115 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("constraint_models"),
116 bp::arg("constraint_datas")),
117 "Rebuild the Cholesky decompostion internal memory according to the input constraint "
118 "models")
119
120 .def(
121 "rebuild",
122 (void (*)(
123 Self & self, const Model &, const Data &, const ConstraintModelVector &,
124 const ConstraintDataVector &))&rebuild,
125 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("constraint_models"),
126 bp::arg("constraint_datas")),
127 "Rebuild the Cholesky decompostion internal memory according to the input constraint "
128 "models")
129
130 .def(
131 "compute",
132 (void (*)(
133 Self & self, const Model &, Data &, const RigidConstraintModelVector &,
134 RigidConstraintDataVector &, const Scalar))&compute,
135 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("contact_models"),
136 bp::arg("contact_datas"), bp::arg("mu") = 0),
137 "Computes the Cholesky decompostion of the augmented matrix containing the KKT matrix\n"
138 "related to the system mass matrix and the Jacobians of the contact patches contained "
139 "in\n"
140 "the vector of RigidConstraintModel named contact_models. The decomposition is "
141 "regularized with a factor mu.",
143
144 .def(
145 "compute",
146 (void (*)(
147 Self & self, const Model &, Data &, const RigidConstraintModelVector &,
148 RigidConstraintDataVector &, const Vector &))&compute,
149 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("contact_models"),
150 bp::arg("contact_datas"), bp::arg("mus")),
151 "Computes the Cholesky decompostion of the augmented matrix containing the KKT matrix\n"
152 "related to the system mass matrix and the Jacobians of the contact patches contained "
153 "in\n"
154 "the vector of RigidConstraintModel named contact_models. The decomposition is "
155 "regularized with a factor mu.",
157
158 .def(
159 "compute",
160 (void (*)(
161 Self & self, const Model &, Data &, const ConstraintModelVector &,
162 ConstraintDataVector &, const Scalar))&compute,
163 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("constraint_models"),
164 bp::arg("constraint_datas"), bp::arg("mu") = 0),
165 "Computes the Cholesky decompostion of the augmented matrix containing the KKT matrix\n"
166 "related to the system mass matrix and the Jacobians of the contact patches contained "
167 "in\n"
168 "the vector of ConstraintModel named constraint_models. The decomposition is "
169 "regularized with a factor mu.")
170
171 .def(
172 "compute",
173 (void (*)(
174 Self & self, const Model &, Data &, const ConstraintModelVector &,
175 ConstraintDataVector &, const Vector &))&compute,
176 (bp::arg("self"), bp::arg("model"), bp::arg("data"), bp::arg("constraint_models"),
177 bp::arg("constraint_datas"), bp::arg("mus")),
178 "Computes the Cholesky decompostion of the augmented matrix containing the KKT matrix\n"
179 "related to the system mass matrix and the Jacobians of the contact patches contained "
180 "in\n"
181 "the vector of ConstraintModel named constraint_models. The decomposition is "
182 "regularized with a factor mu.")
183
184 .def(
185 "updateDamping", (void (Self::*)(const Scalar &))&Self::updateDamping,
186 bp::args("self", "mu"),
187 "Update the damping term on the upper left block part of the KKT matrix. The damping "
188 "term should be positive.")
189
190 .def(
191 "updateDamping", &Self::template updateDamping<Vector>, bp::args("self", "mus"),
192 "Update the damping terms on the upper left block part of the KKT matrix. The damping "
193 "terms should be all positives.")
194
195 .def(
196 "getDamping", +[](const Self & self) -> Matrix { return self.getDamping().matrix(); },
197 bp::arg("self"), "Returns the current damping as a dense matrix.")
198
199 .def(
200 "getInverseOperationalSpaceInertiaMatrix",
201 (Matrix (Self::*)(bool) const) & Self::getInverseOperationalSpaceInertiaMatrix,
202 (bp::arg("self"), bp::arg("enforce_symmetry") = false),
203 "Returns the Inverse of the Operational Space Inertia Matrix resulting from the "
204 "decomposition.",
205 bp::return_value_policy<bp::return_by_value>())
206
207 .def(
208 "getOperationalSpaceInertiaMatrix",
209 (Matrix (Self::*)(void) const) & Self::getOperationalSpaceInertiaMatrix,
210 bp::arg("self"),
211 "Returns the Operational Space Inertia Matrix resulting from the decomposition.",
212 bp::return_value_policy<bp::return_by_value>())
213
214 .def(
215 "getInverseMassMatrix", (Matrix (Self::*)(void) const) & Self::getInverseMassMatrix,
216 bp::arg("self"),
217 "Returns the inverse of the Joint Space Inertia Matrix or \"mass matrix\".",
218 bp::return_value_policy<bp::return_by_value>())
219
220 .def(
221 "solve", &solve<Matrix>, bp::args("self", "matrix"),
222 "Computes the solution of \f$ A x = b \f$ where self corresponds to the Cholesky "
223 "decomposition of A.",
224 bp::return_value_policy<bp::return_by_value>())
225
226 .def(
227 "inverse", (Matrix (Self::*)(void) const) & Self::inverse, bp::arg("self"),
228 "Returns the inverse matrix resulting from the decomposition.")
229
230 .def(
231 "getMassMatrixChoeslkyDecomposition",
232 &Self::template getMassMatrixChoeslkyDecomposition<
233 Scalar, 0, JointCollectionDefaultTpl>,
234 bp::arg("self"),
235 "Retrieves the Cholesky decomposition of the Mass Matrix contained in the current "
236 "decomposition.")
237
238 .def(
239 "getDelassusOperatorCholeskyExpression", &Self::getDelassusOperatorCholeskyExpression,
240 bp::arg("self"),
241 "Returns the Cholesky decomposition expression associated to the underlying Delassus "
242 "matrix.",
243 bp::with_custodian_and_ward_postcall<0, 1>())
244 .def(
245 "getDelassusCholeskyExpression", &Self::getDelassusOperatorCholeskyExpression,
246 bp::arg("self"), "Deprecated. Use getDelassusOperatorCholeskyExpression instead.",
247 ::eigenpy::deprecated_member<
248 eigenpy::DeprecationType::DEPRECATION, bp::with_custodian_and_ward_postcall<0, 1>>(
249 "Deprecated member. Use getDelassusOperatorCholeskyExpression instead."))
250
251 .def(ComparableVisitor<Self, pinocchio::is_floating_point<Scalar>::value>());
252 }
253
254 static void expose()
255 {
256 bp::class_<ConstraintCholeskyDecomposition>(
257 "ConstraintCholeskyDecomposition",
258 "Contact information container for contact dynamic algorithms.", bp::no_init)
260 .def(::eigenpy::CopyableVisitor<ConstraintCholeskyDecomposition>());
261
262 bp::scope().attr("ContactCholeskyDecomposition") =
263 bp::scope().attr("ConstraintCholeskyDecomposition"); // alias
264 {
265 typedef typename ConstraintCholeskyDecomposition::DelassusOperatorCholeskyExpression
266 DelassusOperatorCholeskyExpression;
267
268 bp::class_<DelassusOperatorCholeskyExpression>(
269 "DelassusOperatorCholeskyExpression",
270 "Delassus Cholesky expression associated to a given ConstraintCholeskyDecomposition "
271 "object.",
272 bp::no_init)
273 .def(
274 bp::init<ConstraintCholeskyDecomposition &>(
275 bp::args("self", "cholesky_decomposition"),
276 "Build from a given ConstraintCholeskyDecomposition object.")
277 [bp::with_custodian_and_ward<1, 2>()])
278 .def(
279 "cholesky",
280 +[](DelassusOperatorCholeskyExpression & self) -> ConstraintCholeskyDecomposition & {
281 return self.cholesky();
282 },
283 bp::arg("self"),
284 "Returns the Constraint Cholesky decomposition associated to this "
285 "DelassusOperatorCholeskyExpression.",
286 bp::return_internal_reference<>())
288
289 bp::scope().attr("DelassusCholeskyExpression") =
290 bp::scope().attr("DelassusOperatorCholeskyExpression"); // alias
291 }
292
293 {
294#ifndef PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
295 typedef DelassusOperatorDenseTpl<context::Scalar, context::Options> DelassusOperatorDense;
296 bp::class_<DelassusOperatorDense>(
297 "DelassusOperatorDense", "Delassus Cholesky dense operator from a dense matrix.",
298 bp::no_init)
299 .def(
300 bp::init<const context::RefConstMatrixXs>(
301 bp::args("self", "matrix"), "Build from a given dense matrix"))
302
304#endif // PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
305 }
306
307 {
308#ifndef PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
309 typedef DelassusOperatorSparseTpl<context::Scalar, context::Options>
310 DelassusOperatorSparse;
311 using SparseMatrix = typename DelassusOperatorSparse::SparseMatrix;
312 bp::class_<DelassusOperatorSparse, boost::noncopyable>(
313 "DelassusOperatorSparse", "Delassus Cholesky sparse operator from a sparse matrix.",
314 bp::no_init)
315 .def(
316 bp::init<const context::SparseMatrix &>(
317 bp::args("self", "matrix"), "Build from a given sparse matrix"))
319 .def(
320 "matrix",
321 static_cast<SparseMatrix (DelassusOperatorSparse::*)(bool) const>(
322 &DelassusOperatorSparse::sparseMatrix),
323 (bp::arg("self"), bp::arg("with_damping") = true),
324 "Returns the Delassus expression as a sparse matrix.");
325#endif // PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
326 }
327#ifdef PINOCCHIO_WITH_ACCELERATE_SUPPORT
328 {
329 typedef Eigen::AccelerateLLT<context::SparseMatrix> AccelerateCholeskyDecomposition;
330 typedef DelassusOperatorSparseTpl<
331 context::Scalar, context::Options, AccelerateCholeskyDecomposition>
332 DelassusOperatorSparseAccelerate;
333 bp::class_<DelassusOperatorSparseAccelerate, boost::noncopyable>(
334 "DelassusOperatorSparseAccelerate",
335 "Delassus Cholesky sparse operator leveraging the Accelerate framework on APPLE "
336 "systems.",
337 bp::no_init)
338 .def(
339 bp::init<const context::SparseMatrix &>(
340 bp::args("self", "matrix"), "Build from a given sparse matrix"))
341
343 }
344#endif
345 }
346
347 template<typename MatrixType>
348 static Matrix solve(const Self & self, const MatrixType & mat)
349 {
350 return self.solve(mat);
351 }
352
353 template<
354 typename ConstraintModel,
355 typename ConstraintModelAllocator,
356 typename ConstraintData,
357 typename ConstraintDataAllocator>
358 static void rebuild(
359 Self & self,
360 const Model & model,
361 const Data & data,
362 const std::vector<ConstraintModel, ConstraintModelAllocator> & contact_models,
363 const std::vector<ConstraintData, ConstraintDataAllocator> & contact_datas)
364 {
365 self.rebuild(model, data, contact_models, contact_datas);
366 }
367
368 template<
369 typename ConstraintModel,
370 typename ConstraintModelAllocator,
371 typename ConstraintData,
372 typename ConstraintDataAllocator>
373 static void compute(
374 Self & self,
375 const Model & model,
376 Data & data,
377 const std::vector<ConstraintModel, ConstraintModelAllocator> & contact_models,
378 std::vector<ConstraintData, ConstraintDataAllocator> & contact_datas,
379 const Scalar mu = static_cast<Scalar>(0))
380 {
381 self.compute(model, data, contact_models, contact_datas, mu);
382 }
383
384 template<
385 typename ConstraintModel,
386 typename ConstraintModelAllocator,
387 typename ConstraintData,
388 typename ConstraintDataAllocator>
389 static void compute(
390 Self & self,
391 const Model & model,
392 Data & data,
393 const std::vector<ConstraintModel, ConstraintModelAllocator> & contact_models,
394 std::vector<ConstraintData, ConstraintDataAllocator> & contact_datas,
395 const Vector & mus)
396 {
397 self.compute(model, data, contact_models, contact_datas, mus);
398 }
399 };
400
401 } // namespace python
402} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11
Add the Python method == and != to allow a comparison of this.