GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/multibody/impulse-base.cpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 30 32 93.8%
Branches: 70 140 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "python/crocoddyl/multibody/impulse-base.hpp"
11
12 #include "python/crocoddyl/multibody/multibody.hpp"
13 #include "python/crocoddyl/utils/deprecate.hpp"
14
15 namespace crocoddyl {
16 namespace python {
17
18 template <typename Model>
19 struct ImpulseModelAbstractVisitor
20 : public bp::def_visitor<ImpulseModelAbstractVisitor<Model>> {
21 typedef typename Model::StateMultibody State;
22 template <class PyClass>
23 40 void visit(PyClass& cl) const {
24
2/4
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
40 cl.def(bp::init<std::shared_ptr<State>, std::size_t>(
25 bp::args("self", "state", "nc"),
26 "Initialize the impulse model.\n\n"
27 ":param state: state of the multibody system\n"
28 ":param nc: dimension of impulse model"))
29
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
80 .def("calc", pure_virtual(&Model::calc), bp::args("self", "data", "x"),
30 "Compute the impulse Jacobian\n"
31 ":param data: impulse data\n"
32 ":param x: state point (dim. state.nx)")
33
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
80 .def("calcDiff", pure_virtual(&Model::calcDiff),
34 bp::args("self", "data", "x"),
35 "Compute the derivatives of impulse Jacobian\n"
36 "It assumes that calc has been run first.\n"
37 ":param data: impulse data\n"
38 ":param x: state point (dim. state.nx)")
39
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
80 .def("updateForce", pure_virtual(&Model::updateForce),
40 bp::args("self", "data", "force"),
41 "Convert the force into a stack of spatial forces.\n\n"
42 ":param data: impulse data\n"
43 ":param force: force vector (dimension nc)")
44
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
80 .def("updateForceDiff", &Model::updateForceDiff,
45 bp::args("self", "data", "df_dx"),
46 "Update the Jacobian of the impulse force.\n\n"
47 ":param data: impulse data\n"
48 ":param df_dx: Jacobian of the impulse force (dimension nc*ndx)")
49
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def("setZeroForce", &Model::setZeroForce, bp::args("self", "data"),
50 "Set zero the spatial force.\n\n"
51 ":param data: contact data")
52
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def("setZeroForceDiff", &Model::setZeroForceDiff,
53 bp::args("self", "data"),
54 "Set zero the derivatives of the spatial force.\n\n"
55 ":param data: contact data")
56
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def("createData", &Model::createData,
57 bp::with_custodian_and_ward_postcall<0, 2>(),
58 bp::args("self", "data"),
59 "Create the impulse data.\n\n"
60 "Each impulse model has its own data that needs to be allocated. "
61 "This function\n"
62 "returns the allocated data for a predefined impulse.\n"
63 ":param data: Pinocchio data\n"
64 ":return impulse data.")
65
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
40 .def("createData", &Model::default_createData,
66 bp::with_custodian_and_ward_postcall<0, 2>())
67
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
40 .add_property(
68 "state",
69 bp::make_function(&Model::get_state,
70 40 bp::return_value_policy<bp::return_by_value>()),
71 "state of the multibody system")
72
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
40 .add_property("ni",
73 bp::make_function(&Model::get_nc,
74
2/4
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 20 times.
✗ Branch 6 not taken.
80 deprecated<>("Deprecated. Use nc")),
75 "dimension of impulse")
76
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .add_property("nc", bp::make_function(&Model::get_nc),
77 "dimension of impulse")
78 80 .add_property("id", &Model::get_id, &Model::set_id,
79 "reference frame id")
80
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
40 .add_property("type", bp::make_function(&Model::get_type),
81 &Model::set_type, "type of impulse");
82 40 }
83 };
84
85 template <typename Data>
86 struct ImpulseDataAbstractVisitor
87 : public bp::def_visitor<ImpulseDataAbstractVisitor<Data>> {
88 template <class PyClass>
89 40 void visit(PyClass& cl) const {
90
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
40 cl.add_property(
91 40 "fXj", bp::make_getter(&Data::fXj, bp::return_internal_reference<>()),
92 bp::make_setter(&Data::fXj),
93 "action matrix from contact to local frames")
94
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
80 .add_property(
95 "dv0_dq",
96 40 bp::make_getter(&Data::dv0_dq, bp::return_internal_reference<>()),
97 bp::make_setter(&Data::dv0_dq),
98 "Jacobian of the previous impulse velocity")
99
3/6
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
80 .add_property(
100 "dtau_dq",
101 40 bp::make_getter(&Data::dtau_dq, bp::return_internal_reference<>()),
102 bp::make_setter(&Data::dtau_dq), "force contribution to dtau_dq");
103 40 }
104 };
105
106 #define CROCODDYL_IMPULSE_MODEL_ABSTRACT_PYTHON_BINDINGS(Scalar) \
107 typedef ImpulseModelAbstractTpl<Scalar> Model; \
108 typedef ImpulseModelAbstractTpl_wrap<Scalar> Model_wrap; \
109 typedef Model::StateMultibody State; \
110 bp::register_ptr_to_python<std::shared_ptr<Model>>(); \
111 bp::class_<Model_wrap, boost::noncopyable>( \
112 "ImpulseModelAbstract", \
113 "Abstract impulse model.\n\n" \
114 "It defines a template for impulse models.\n" \
115 "The calc and calcDiff functions compute the impulse Jacobian the " \
116 "derivatives respectively.", \
117 bp::init<std::shared_ptr<State>, pinocchio::ReferenceFrame, \
118 std::size_t>(bp::args("self", "state", "type", "nc"), \
119 "Initialize the impulse model.\n\n" \
120 ":param state: state of the multibody system\n" \
121 ":param type: type of impulse\n" \
122 ":param nc: dimension of impulse model")) \
123 .def(ImpulseModelAbstractVisitor<Model_wrap>()) \
124 .def(PrintableVisitor<Model_wrap>()) \
125 .def(CopyableVisitor<Model_wrap>());
126
127 #define CROCODDYL_IMPULSE_DATA_ABSTRACT_PYTHON_BINDINGS(Scalar) \
128 typedef ImpulseDataAbstractTpl<Scalar> Data; \
129 typedef ImpulseModelAbstractTpl<Scalar> Model; \
130 typedef ForceDataAbstractTpl<Scalar> ForceData; \
131 typedef pinocchio::DataTpl<Scalar> PinocchioData; \
132 bp::register_ptr_to_python<std::shared_ptr<Data>>(); \
133 bp::class_<Data, bp::bases<ForceData>>( \
134 "ImpulseDataAbstract", "Abstract class for impulse data.\n\n", \
135 bp::init<Model*, PinocchioData*>( \
136 bp::args("self", "model", "data"), \
137 "Create common data shared between impulse models.\n\n" \
138 ":param model: impulse model\n" \
139 ":param data: Pinocchio data")[bp::with_custodian_and_ward<1, 3>()]) \
140 .def(ImpulseDataAbstractVisitor<Data>()) \
141 .def(CopyableVisitor<Data>());
142
143 10 void exposeImpulseAbstract() {
144
15/30
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
20 CROCODDYL_PYTHON_SCALARS(CROCODDYL_IMPULSE_MODEL_ABSTRACT_PYTHON_BINDINGS)
145
15/30
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 10 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 10 times.
✗ Branch 16 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 32 taken 10 times.
✗ Branch 33 not taken.
✓ Branch 35 taken 10 times.
✗ Branch 36 not taken.
✓ Branch 38 taken 10 times.
✗ Branch 39 not taken.
✓ Branch 41 taken 10 times.
✗ Branch 42 not taken.
✓ Branch 44 taken 10 times.
✗ Branch 45 not taken.
✓ Branch 47 taken 10 times.
✗ Branch 48 not taken.
✓ Branch 50 taken 10 times.
✗ Branch 51 not taken.
20 CROCODDYL_PYTHON_SCALARS(CROCODDYL_IMPULSE_DATA_ABSTRACT_PYTHON_BINDINGS)
146 10 }
147
148 } // namespace python
149 } // namespace crocoddyl
150