| Directory: | ./ |
|---|---|
| File: | bindings/python/crocoddyl/multibody/residuals/frame-translation.cpp |
| Date: | 2025-03-26 19:23:43 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 21 | 22 | 95.5% |
| Branches: | 51 | 102 | 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/multibody/residuals/frame-translation.hpp" | ||
| 10 | |||
| 11 | #include "python/crocoddyl/multibody/multibody.hpp" | ||
| 12 | |||
| 13 | namespace crocoddyl { | ||
| 14 | namespace python { | ||
| 15 | |||
| 16 | template <typename Model> | ||
| 17 | struct ResidualModelFrameTranslationVisitor | ||
| 18 | : public bp::def_visitor<ResidualModelFrameTranslationVisitor<Model>> { | ||
| 19 | typedef typename Model::ResidualDataAbstract Data; | ||
| 20 | typedef typename Model::Base ModelBase; | ||
| 21 | typedef typename Model::StateMultibody State; | ||
| 22 | typedef typename Model::Vector3s Vector3s; | ||
| 23 | typedef typename Model::VectorXs VectorXs; | ||
| 24 | template <class PyClass> | ||
| 25 | 40 | void visit(PyClass& cl) const { | |
| 26 |
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>, pinocchio::FrameIndex, Vector3s>( |
| 27 | bp::args("self", "state", "id", "xref"), | ||
| 28 | "Initialize the frame translation residual model.\n\n" | ||
| 29 | "The default nu is obtained from state.nv.\n" | ||
| 30 | ":param state: state of the multibody system\n" | ||
| 31 | ":param id: reference frame id\n" | ||
| 32 | ":param xref: reference frame translation")) | ||
| 33 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
| 34 | "calc", | ||
| 35 | static_cast<void (Model::*)( | ||
| 36 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 37 | const Eigen::Ref<const VectorXs>&)>(&Model::calc), | ||
| 38 | bp::args("self", "data", "x", "u"), | ||
| 39 | "Compute the frame translation residual.\n\n" | ||
| 40 | ":param data: residual data\n" | ||
| 41 | ":param x: state point (dim. state.nx)\n" | ||
| 42 | ":param u: control input (dim. nu)") | ||
| 43 |
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( |
| 44 | "calc", | ||
| 45 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
| 46 | const Eigen::Ref<const VectorXs>&)>( | ||
| 47 | &ModelBase::calc), | ||
| 48 | bp::args("self", "data", "x")) | ||
| 49 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def( |
| 50 | "calcDiff", | ||
| 51 | static_cast<void (Model::*)( | ||
| 52 | const std::shared_ptr<Data>&, const Eigen::Ref<const VectorXs>&, | ||
| 53 | const Eigen::Ref<const VectorXs>&)>(&Model::calcDiff), | ||
| 54 | bp::args("self", "data", "x", "u"), | ||
| 55 | "Compute the derivatives of the frame translation residual.\n\n" | ||
| 56 | "It assumes that calc has been run first.\n" | ||
| 57 | ":param data: action data\n" | ||
| 58 | ":param x: state point (dim. state.nx)\n" | ||
| 59 | ":param u: control input (dim. nu)") | ||
| 60 |
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( |
| 61 | "calcDiff", | ||
| 62 | static_cast<void (ModelBase::*)(const std::shared_ptr<Data>&, | ||
| 63 | const Eigen::Ref<const VectorXs>&)>( | ||
| 64 | &ModelBase::calcDiff), | ||
| 65 | bp::args("self", "data", "x")) | ||
| 66 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
80 | .def("createData", &Model::createData, |
| 67 | ✗ | bp::with_custodian_and_ward_postcall<0, 2>(), | |
| 68 | bp::args("self", "data"), | ||
| 69 | "Create the frame translation residual data.\n\n" | ||
| 70 | "Each residual model has its own data that needs to be allocated. " | ||
| 71 | "This function\n" | ||
| 72 | "returns the allocated data for the frame translation residual.\n" | ||
| 73 | ":param data: shared data\n" | ||
| 74 | ":return residual data.") | ||
| 75 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
40 | .add_property("id", &Model::get_id, &Model::set_id, |
| 76 | "reference frame id") | ||
| 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 frame translation"); | ||
| 81 | 40 | } | |
| 82 | }; | ||
| 83 | |||
| 84 | template <typename Data> | ||
| 85 | struct ResidualDataFrameTranslationVisitor | ||
| 86 | : public bp::def_visitor<ResidualDataFrameTranslationVisitor<Data>> { | ||
| 87 | template <class PyClass> | ||
| 88 | 40 | void visit(PyClass& cl) const { | |
| 89 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | cl.add_property( |
| 90 | "pinocchio", | ||
| 91 | 40 | bp::make_getter(&Data::pinocchio, bp::return_internal_reference<>()), | |
| 92 | "pinocchio data") | ||
| 93 |
2/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
|
40 | .add_property( |
| 94 | "fJf", | ||
| 95 | 40 | bp::make_getter(&Data::fJf, bp::return_internal_reference<>()), | |
| 96 | "local Jacobian of the frame"); | ||
| 97 | 40 | } | |
| 98 | }; | ||
| 99 | |||
| 100 | #define CROCODDYL_RESIDUAL_MODEL_FRAME_TRANSLATION_PYTHON_BINDINGS(Scalar) \ | ||
| 101 | typedef ResidualModelFrameTranslationTpl<Scalar> Model; \ | ||
| 102 | typedef ResidualModelAbstractTpl<Scalar> ModelBase; \ | ||
| 103 | typedef typename Model::StateMultibody State; \ | ||
| 104 | typedef typename Model::Vector3s Vector3s; \ | ||
| 105 | bp::register_ptr_to_python<std::shared_ptr<Model>>(); \ | ||
| 106 | bp::class_<Model, bp::bases<ModelBase>>( \ | ||
| 107 | "ResidualModelFrameTranslation", \ | ||
| 108 | "This residual function defines the the frame translation tracking as " \ | ||
| 109 | "as r = t - tref, with t and tref as the current and reference frame " \ | ||
| 110 | "translations, respectively.", \ | ||
| 111 | bp::init<std::shared_ptr<State>, pinocchio::FrameIndex, Vector3s, \ | ||
| 112 | std::size_t>( \ | ||
| 113 | bp::args("self", "state", "id", "xref", "nu"), \ | ||
| 114 | "Initialize the frame translation residual model.\n\n" \ | ||
| 115 | ":param state: state of the multibody system\n" \ | ||
| 116 | ":param id: reference frame id\n" \ | ||
| 117 | ":param xref: reference frame translation\n" \ | ||
| 118 | ":param nu: dimension of control vector")) \ | ||
| 119 | .def(ResidualModelFrameTranslationVisitor<Model>()) \ | ||
| 120 | .def(CastVisitor<Model>()) \ | ||
| 121 | .def(PrintableVisitor<Model>()) \ | ||
| 122 | .def(CopyableVisitor<Model>()); | ||
| 123 | |||
| 124 | #define CROCODDYL_RESIDUAL_DATA_FRAME_TRANSLATION_PYTHON_BINDINGS(Scalar) \ | ||
| 125 | typedef ResidualDataFrameTranslationTpl<Scalar> Data; \ | ||
| 126 | typedef ResidualDataAbstractTpl<Scalar> DataBase; \ | ||
| 127 | typedef ResidualModelFrameTranslationTpl<Scalar> Model; \ | ||
| 128 | typedef Model::DataCollectorAbstract DataCollector; \ | ||
| 129 | bp::register_ptr_to_python<std::shared_ptr<Data>>(); \ | ||
| 130 | bp::class_<Data, bp::bases<DataBase>>( \ | ||
| 131 | "ResidualDataFrameTranslation", \ | ||
| 132 | "Data for frame translation residual.\n\n", \ | ||
| 133 | bp::init<Model*, DataCollector*>( \ | ||
| 134 | bp::args("self", "model", "data"), \ | ||
| 135 | "Create frame translation residual data.\n\n" \ | ||
| 136 | ":param model: frame translation residual model\n" \ | ||
| 137 | ":param data: shared data")[bp::with_custodian_and_ward< \ | ||
| 138 | 1, 2, bp::with_custodian_and_ward<1, 3>>()]) \ | ||
| 139 | .def(ResidualDataFrameTranslationVisitor<Data>()) \ | ||
| 140 | .def(CopyableVisitor<Data>()); | ||
| 141 | |||
| 142 | 10 | void exposeResidualFrameTranslation() { | |
| 143 |
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( |
| 144 | CROCODDYL_RESIDUAL_MODEL_FRAME_TRANSLATION_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( |
| 146 | CROCODDYL_RESIDUAL_DATA_FRAME_TRANSLATION_PYTHON_BINDINGS) | ||
| 147 | 10 | } | |
| 148 | |||
| 149 | } // namespace python | ||
| 150 | } // namespace crocoddyl | ||
| 151 |