GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bindings/python/crocoddyl/core/action-base.hpp Lines: 26 45 57.8 %
Date: 2024-02-13 11:12:33 Branches: 26 148 17.6 %

Line Branch Exec Source
1
///////////////////////////////////////////////////////////////////////////////
2
// BSD 3-Clause License
3
//
4
// Copyright (C) 2019-2024, 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
#ifndef BINDINGS_PYTHON_CROCODDYL_CORE_ACTION_BASE_HPP_
11
#define BINDINGS_PYTHON_CROCODDYL_CORE_ACTION_BASE_HPP_
12
13
#include "crocoddyl/core/action-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 ActionModelAbstract_wrap : public ActionModelAbstract,
21
                                 public bp::wrapper<ActionModelAbstract> {
22
 public:
23
  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
24
  using ActionModelAbstract::ng_;
25
  using ActionModelAbstract::nh_;
26
  using ActionModelAbstract::nu_;
27
  using ActionModelAbstract::unone_;
28
29
7
  ActionModelAbstract_wrap(boost::shared_ptr<StateAbstract> state,
30
                           const std::size_t nu, const std::size_t nr = 1,
31
                           const std::size_t ng = 0, const std::size_t nh = 0)
32
7
      : ActionModelAbstract(state, nu, nr, ng, nh),
33
7
        bp::wrapper<ActionModelAbstract>() {
34

7
    unone_ = NAN * MathBase::VectorXs::Ones(nu);
35
7
  }
36
37
230
  void calc(const boost::shared_ptr<ActionDataAbstract>& data,
38
            const Eigen::Ref<const Eigen::VectorXd>& x,
39
            const Eigen::Ref<const Eigen::VectorXd>& u) {
40
230
    if (static_cast<std::size_t>(x.size()) != state_->get_nx()) {
41
      throw_pretty("Invalid argument: "
42
                   << "x has wrong dimension (it should be " +
43
                          std::to_string(state_->get_nx()) + ")");
44
    }
45
230
    if (static_cast<std::size_t>(u.size()) != nu_) {
46
      throw_pretty("Invalid argument: "
47
                   << "u has wrong dimension (it should be " +
48
                          std::to_string(nu_) + ")");
49
    }
50
230
    if (std::isnan(u.lpNorm<Eigen::Infinity>())) {
51

3
      return bp::call<void>(this->get_override("calc").ptr(), data,
52
6
                            (Eigen::VectorXd)x);
53
    } else {
54

227
      return bp::call<void>(this->get_override("calc").ptr(), data,
55
454
                            (Eigen::VectorXd)x, (Eigen::VectorXd)u);
56
    }
57
  }
58
59
54
  void calcDiff(const boost::shared_ptr<ActionDataAbstract>& data,
60
                const Eigen::Ref<const Eigen::VectorXd>& x,
61
                const Eigen::Ref<const Eigen::VectorXd>& u) {
62
54
    if (static_cast<std::size_t>(x.size()) != state_->get_nx()) {
63
      throw_pretty("Invalid argument: "
64
                   << "x has wrong dimension (it should be " +
65
                          std::to_string(state_->get_nx()) + ")");
66
    }
67
54
    if (static_cast<std::size_t>(u.size()) != nu_) {
68
      throw_pretty("Invalid argument: "
69
                   << "u has wrong dimension (it should be " +
70
                          std::to_string(nu_) + ")");
71
    }
72
54
    if (std::isnan(u.lpNorm<Eigen::Infinity>())) {
73

1
      return bp::call<void>(this->get_override("calcDiff").ptr(), data,
74
2
                            (Eigen::VectorXd)x);
75
    } else {
76

53
      return bp::call<void>(this->get_override("calcDiff").ptr(), data,
77
106
                            (Eigen::VectorXd)x, (Eigen::VectorXd)u);
78
    }
79
  }
80
81
309
  boost::shared_ptr<ActionDataAbstract> createData() {
82
309
    enableMultithreading() = false;
83

309
    if (boost::python::override createData = this->get_override("createData")) {
84
309
      return bp::call<boost::shared_ptr<ActionDataAbstract> >(createData.ptr());
85
    }
86
    return ActionModelAbstract::createData();
87
  }
88
89
  boost::shared_ptr<ActionDataAbstract> default_createData() {
90
    return this->ActionModelAbstract::createData();
91
  }
92
93
  void quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
94
                   Eigen::Ref<Eigen::VectorXd> u,
95
                   const Eigen::Ref<const Eigen::VectorXd>& x,
96
                   const std::size_t maxiter, const double tol) {
97
    if (boost::python::override quasiStatic =
98
            this->get_override("quasiStatic")) {
99
      u = bp::call<Eigen::VectorXd>(quasiStatic.ptr(), data, (Eigen::VectorXd)x,
100
                                    maxiter, tol);
101
      if (static_cast<std::size_t>(u.size()) != nu_) {
102
        throw_pretty("Invalid argument: "
103
                     << "u has wrong dimension (it should be " +
104
                            std::to_string(nu_) + ")");
105
      }
106
      return;
107
    }
108
    return ActionModelAbstract::quasiStatic(data, u, x, maxiter, tol);
109
  }
110
111
  void default_quasiStatic(const boost::shared_ptr<ActionDataAbstract>& data,
112
                           Eigen::Ref<Eigen::VectorXd> u,
113
                           const Eigen::Ref<const Eigen::VectorXd>& x,
114
                           const std::size_t maxiter, const double tol) {
115
    return this->ActionModelAbstract::quasiStatic(data, u, x, maxiter, tol);
116
  }
117
};
118
119
20
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(ActionModel_quasiStatic_wraps,
120
                                       ActionModelAbstract::quasiStatic_x, 2, 4)
121
122
}  // namespace python
123
}  // namespace crocoddyl
124
125
#endif  // BINDINGS_PYTHON_CROCODDYL_CORE_ACTION_BASE_HPP_