GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/control-base.hpp Lines: 0 65 0.0 %
Date: 2024-02-13 11:12:33 Branches: 0 146 0.0 %

Line Branch Exec Source
1
2
///////////////////////////////////////////////////////////////////////////////
3
// BSD 3-Clause License
4
//
5
// Copyright (C) 2021-2022, LAAS-CNRS, University of Edinburgh, University of
6
// Trento Copyright note valid unless otherwise stated in individual files. All
7
// rights reserved.
8
///////////////////////////////////////////////////////////////////////////////
9
10
#ifndef BINDINGS_PYTHON_CROCODDYL_CORE_CONTROL_BASE_HPP_
11
#define BINDINGS_PYTHON_CROCODDYL_CORE_CONTROL_BASE_HPP_
12
13
#include "crocoddyl/core/control-base.hpp"
14
#include "crocoddyl/core/utils/exception.hpp"
15
#include "python/crocoddyl/core/core.hpp"
16
17
namespace crocoddyl {
18
namespace python {
19
20
class ControlParametrizationModelAbstract_wrap
21
    : public ControlParametrizationModelAbstract,
22
      public bp::wrapper<ControlParametrizationModelAbstract> {
23
 public:
24
  ControlParametrizationModelAbstract_wrap(std::size_t nw, std::size_t nu)
25
      : ControlParametrizationModelAbstract(nw, nu),
26
        bp::wrapper<ControlParametrizationModelAbstract>() {}
27
28
  void calc(const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
29
            double t, const Eigen::Ref<const Eigen::VectorXd>& u) const {
30
    if (static_cast<std::size_t>(u.size()) != nu_) {
31
      throw_pretty("Invalid argument: "
32
                   << "u has wrong dimension (it should be " +
33
                          std::to_string(nu_) + ")");
34
    }
35
    return bp::call<void>(this->get_override("calc").ptr(), data, t,
36
                          (Eigen::VectorXd)u);
37
  }
38
39
  void calcDiff(
40
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
41
      double t, const Eigen::Ref<const Eigen::VectorXd>& u) const {
42
    if (static_cast<std::size_t>(u.size()) != nu_) {
43
      throw_pretty("Invalid argument: "
44
                   << "u has wrong dimension (it should be " +
45
                          std::to_string(nu_) + ")");
46
    }
47
    return bp::call<void>(this->get_override("calcDiff").ptr(), data, t,
48
                          (Eigen::VectorXd)u);
49
  }
50
51
  void params(const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
52
              double t, const Eigen::Ref<const Eigen::VectorXd>& w) const {
53
    if (static_cast<std::size_t>(w.size()) != nw_) {
54
      throw_pretty("Invalid argument: "
55
                   << "w has wrong dimension (it should be " +
56
                          std::to_string(nw_) + ")");
57
    }
58
    return bp::call<void>(this->get_override("params").ptr(), data, t,
59
                          (Eigen::VectorXd)w);
60
  }
61
62
  boost::shared_ptr<ControlParametrizationDataAbstract> createData() {
63
    enableMultithreading() = false;
64
    if (boost::python::override createData = this->get_override("createData")) {
65
      return bp::call<boost::shared_ptr<ControlParametrizationDataAbstract> >(
66
          createData.ptr());
67
    }
68
    return ControlParametrizationModelAbstract::createData();
69
  }
70
71
  boost::shared_ptr<ControlParametrizationDataAbstract> default_createData() {
72
    return this->ControlParametrizationModelAbstract::createData();
73
  }
74
75
  void convertBounds(const Eigen::Ref<const Eigen::VectorXd>& w_lb,
76
                     const Eigen::Ref<const Eigen::VectorXd>& w_ub,
77
                     Eigen::Ref<Eigen::VectorXd> u_lb,
78
                     Eigen::Ref<Eigen::VectorXd> u_ub) const {
79
    bp::list res = convertBounds_wrap(w_lb, w_ub);
80
    u_lb.derived() = bp::extract<Eigen::VectorXd>(res[0])();
81
    u_ub.derived() = bp::extract<Eigen::VectorXd>(res[1])();
82
  }
83
84
  bp::list convertBounds_wrap(
85
      const Eigen::Ref<const Eigen::VectorXd>& w_lb,
86
      const Eigen::Ref<const Eigen::VectorXd>& w_ub) const {
87
    bp::list p_bounds =
88
        bp::call<bp::list>(this->get_override("convertBounds").ptr(),
89
                           (Eigen::VectorXd)w_lb, (Eigen::VectorXd)w_ub);
90
    return p_bounds;
91
  }
92
93
  void multiplyByJacobian(
94
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
95
      const Eigen::Ref<const Eigen::MatrixXd>& A,
96
      Eigen::Ref<Eigen::MatrixXd> out, const AssignmentOp op) const {
97
    switch (op) {
98
      case setto: {
99
        out = multiplyByJacobian_wrap(data, A);
100
        break;
101
      }
102
      case addto: {
103
        out += multiplyByJacobian_wrap(data, A);
104
        break;
105
      }
106
      case rmfrom: {
107
        out -= multiplyByJacobian_wrap(data, A);
108
        break;
109
      }
110
      default: {
111
        throw_pretty(
112
            "Invalid argument: allowed operators: setto, addto, rmfrom");
113
        break;
114
      }
115
    }
116
  }
117
118
  Eigen::MatrixXd multiplyByJacobian_wrap(
119
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
120
      const Eigen::Ref<const Eigen::MatrixXd>& A) const {
121
    return bp::call<Eigen::MatrixXd>(
122
        this->get_override("multiplyByJacobian").ptr(), data,
123
        (Eigen::MatrixXd)A);
124
  }
125
126
  void multiplyJacobianTransposeBy(
127
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
128
      const Eigen::Ref<const Eigen::MatrixXd>& A,
129
      Eigen::Ref<Eigen::MatrixXd> out, const AssignmentOp op) const {
130
    switch (op) {
131
      case setto: {
132
        out = multiplyJacobianTransposeBy_wrap(data, A);
133
        break;
134
      }
135
      case addto: {
136
        out += multiplyJacobianTransposeBy_wrap(data, A);
137
        break;
138
      }
139
      case rmfrom: {
140
        out -= multiplyJacobianTransposeBy_wrap(data, A);
141
        break;
142
      }
143
      default: {
144
        throw_pretty(
145
            "Invalid argument: allowed operators: setto, addto, rmfrom");
146
        break;
147
      }
148
    }
149
  }
150
151
  Eigen::MatrixXd multiplyJacobianTransposeBy_wrap(
152
      const boost::shared_ptr<ControlParametrizationDataAbstract>& data,
153
      const Eigen::Ref<const Eigen::MatrixXd>& A) const {
154
    return bp::call<Eigen::MatrixXd>(
155
        this->get_override("multiplyJacobianTransposeBy").ptr(), data,
156
        (Eigen::MatrixXd)A);
157
  }
158
};
159
160
}  // namespace python
161
}  // namespace crocoddyl
162
163
#endif  // BINDINGS_PYTHON_CROCODDYL_CORE_CONTROL_BASE_HPP_