GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/residuals/control.cpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 14 15 93.3%
Branches: 34 68 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021-2025, University of Edinburgh, Heriot-Watt University
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
7 ///////////////////////////////////////////////////////////////////////////////
8
9 #include "crocoddyl/core/residuals/control.hpp"
10
11 #include "python/crocoddyl/core/core.hpp"
12
13 namespace crocoddyl {
14 namespace python {
15
16 template <typename Model>
17 struct ResidualModelControlVisitor
18 : public bp::def_visitor<ResidualModelControlVisitor<Model>> {
19 typedef typename Model::ResidualDataAbstract Data;
20 typedef typename Model::Base ModelBase;
21 typedef typename Model::StateAbstract State;
22 typedef typename Model::VectorXs VectorXs;
23 template <class PyClass>
24 40 void visit(PyClass& cl) const {
25
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>(
26 bp::args("self", "state", "nu"),
27 "Initialize the control residual model.\n\n"
28 "The default reference control is obtained from np.zero(nu).\n"
29 ":param state: state description\n"
30 ":param nu: dimension of the control vector"))
31
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(bp::init<std::shared_ptr<State>>(
32 bp::args("self", "state"),
33 "Initialize the control residual model.\n\n"
34 "The default reference control is obtained from np.zero(nu).\n"
35 ":param state: state description"))
36
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
80 .def(
37 "calc",
38 static_cast<void (Model::*)(
39 const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&,
40 const Eigen::Ref<const VectorXs>&)>(&Model::calc),
41 bp::args("self", "data", "x", "u"),
42 "Compute the control residual.\n\n"
43 ":param data: residual data\n"
44 ":param x: state point (dim. state.nx)\n"
45 ":param u: control input (dim. nu)")
46
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def("calc",
47 static_cast<void (Model::*)(const std::shared_ptr<Data>&,
48 const Eigen::Ref<const VectorXs>&)>(
49 &Model::calc),
50 bp::args("self", "data", "x"))
51
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
80 .def(
52 "calcDiff",
53 static_cast<void (Model::*)(
54 const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&,
55 const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff),
56 bp::args("self", "data", "x", "u"),
57 "Compute the Jacobians of the control residual.\n\n"
58 "It assumes that calc has been run first.\n"
59 ":param data: action data\n"
60 ":param x: state point (dim. state.nx)\n"
61 ":param u: control input (dim. nu)")
62
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(
63 "calcDiff",
64 static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&,
65 const Eigen::Ref<const VectorXs>&)>(
66 &ModelBase::calcDiff),
67 bp::args("self", "data", "x"))
68
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
80 .def("createData", &Model::createData,
69 bp::with_custodian_and_ward_postcall<0, 2>(),
70 bp::args("self", "data"),
71 "Create the control residual data.\n\n"
72 "Each residual model has its own data that needs to be allocated. "
73 "This function returns the allocated data for the control "
74 "residual.\n"
75 ":param data: shared data\n"
76 ":return residual data.")
77
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("reference",
78 bp::make_function(&Model::get_reference,
79 40 bp::return_internal_reference<>()),
80 &Model::set_reference, "reference control vector");
81 40 }
82 };
83
84 #define CROCODDYL_RESIDUAL_MODEL_CONTROL_PYTHON_BINDINGS(Scalar) \
85 typedef ResidualModelControlTpl<Scalar> Model; \
86 typedef ResidualModelAbstractTpl<Scalar> ModelBase; \
87 typedef typename ModelBase::StateAbstract State; \
88 typedef typename ModelBase::VectorXs VectorXs; \
89 bp::register_ptr_to_python<std::shared_ptr<Model>>(); \
90 bp::class_<Model, bp::bases<ModelBase>>( \
91 "ResidualModelControl", \
92 "This residual function defines a residual vector as r = u - uref, " \
93 "with u and uref as the current and reference control, respectively.", \
94 bp::init<std::shared_ptr<State>, VectorXs>( \
95 bp::args("self", "state", "uref"), \
96 "Initialize the control residual model.\n\n" \
97 ":param state: state description\n" \
98 ":param uref: reference control")) \
99 .def(ResidualModelControlVisitor<Model>()) \
100 .def(CastVisitor<Model>()) \
101 .def(PrintableVisitor<Model>()) \
102 .def(CopyableVisitor<Model>());
103
104 10 void exposeResidualControl() {
105
17/34
✓ 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 18 taken 10 times.
✗ Branch 19 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 10 times.
✗ Branch 28 not taken.
✓ Branch 30 taken 10 times.
✗ Branch 31 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.
✓ Branch 53 taken 10 times.
✗ Branch 54 not taken.
✓ Branch 56 taken 10 times.
✗ Branch 57 not taken.
20 CROCODDYL_PYTHON_SCALARS(CROCODDYL_RESIDUAL_MODEL_CONTROL_PYTHON_BINDINGS)
106 10 }
107
108 } // namespace python
109 } // namespace crocoddyl
110