GCC Code Coverage Report


Directory: ./
File: bindings/python/crocoddyl/core/cost-base.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 10 47 21.3%
Branches: 8 154 5.2%

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_COST_BASE_HPP_
11 #define BINDINGS_PYTHON_CROCODDYL_CORE_COST_BASE_HPP_
12
13 #include "crocoddyl/core/cost-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 CostModelAbstract_wrap : public CostModelAbstract,
21 public bp::wrapper<CostModelAbstract> {
22 public:
23 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
24 using CostModelAbstract::nu_;
25 using CostModelAbstract::unone_;
26
27 CostModelAbstract_wrap(std::shared_ptr<StateAbstract> state,
28 std::shared_ptr<ActivationModelAbstract> activation,
29 std::shared_ptr<ResidualModelAbstract> residual)
30 : CostModelAbstract(state, activation, residual) {
31 unone_ = NAN * MathBase::VectorXs::Ones(nu_);
32 }
33
34 1 CostModelAbstract_wrap(std::shared_ptr<StateAbstract> state,
35 std::shared_ptr<ActivationModelAbstract> activation,
36 const std::size_t nu)
37
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 : CostModelAbstract(state, activation, nu) {
38
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 unone_ = NAN * MathBase::VectorXs::Ones(nu);
39 1 }
40
41 6 CostModelAbstract_wrap(std::shared_ptr<StateAbstract> state,
42 std::shared_ptr<ActivationModelAbstract> activation)
43
1/2
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
6 : CostModelAbstract(state, activation) {
44
3/6
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
6 unone_ = NAN * MathBase::VectorXs::Ones(nu_);
45 6 }
46
47 CostModelAbstract_wrap(std::shared_ptr<StateAbstract> state,
48 std::shared_ptr<ResidualModelAbstract> residual)
49 : CostModelAbstract(state, residual) {
50 unone_ = NAN * MathBase::VectorXs::Ones(nu_);
51 }
52
53 CostModelAbstract_wrap(std::shared_ptr<StateAbstract> state,
54 const std::size_t nr, const std::size_t nu)
55 : CostModelAbstract(state, nr, nu), bp::wrapper<CostModelAbstract>() {
56 unone_ = NAN * MathBase::VectorXs::Ones(nu);
57 }
58
59 CostModelAbstract_wrap(std::shared_ptr<StateAbstract> state,
60 const std::size_t nr)
61 : CostModelAbstract(state, nr), bp::wrapper<CostModelAbstract>() {
62 unone_ = NAN * MathBase::VectorXs::Ones(nu_);
63 }
64
65 void calc(const std::shared_ptr<CostDataAbstract>& data,
66 const Eigen::Ref<const Eigen::VectorXd>& x,
67 const Eigen::Ref<const Eigen::VectorXd>& u) {
68 if (static_cast<std::size_t>(x.size()) != state_->get_nx()) {
69 throw_pretty(
70 "Invalid argument: " << "x has wrong dimension (it should be " +
71 std::to_string(state_->get_nx()) + ")");
72 }
73 if (static_cast<std::size_t>(u.size()) != nu_) {
74 throw_pretty(
75 "Invalid argument: " << "u has wrong dimension (it should be " +
76 std::to_string(nu_) + ")");
77 }
78 if (std::isnan(u.lpNorm<Eigen::Infinity>())) {
79 return bp::call<void>(this->get_override("calc").ptr(), data,
80 (Eigen::VectorXd)x);
81 } else {
82 return bp::call<void>(this->get_override("calc").ptr(), data,
83 (Eigen::VectorXd)x, (Eigen::VectorXd)u);
84 }
85 }
86
87 void calcDiff(const std::shared_ptr<CostDataAbstract>& data,
88 const Eigen::Ref<const Eigen::VectorXd>& x,
89 const Eigen::Ref<const Eigen::VectorXd>& u) {
90 if (static_cast<std::size_t>(x.size()) != state_->get_nx()) {
91 throw_pretty(
92 "Invalid argument: " << "x has wrong dimension (it should be " +
93 std::to_string(state_->get_nx()) + ")");
94 }
95 if (static_cast<std::size_t>(u.size()) != nu_) {
96 throw_pretty(
97 "Invalid argument: " << "u has wrong dimension (it should be " +
98 std::to_string(nu_) + ")");
99 }
100 if (std::isnan(u.lpNorm<Eigen::Infinity>())) {
101 return bp::call<void>(this->get_override("calcDiff").ptr(), data,
102 (Eigen::VectorXd)x);
103 } else {
104 return bp::call<void>(this->get_override("calcDiff").ptr(), data,
105 (Eigen::VectorXd)x, (Eigen::VectorXd)u);
106 }
107 }
108
109 std::shared_ptr<CostDataAbstract> createData(
110 DataCollectorAbstract* const data) {
111 enableMultithreading() = false;
112 if (boost::python::override createData = this->get_override("createData")) {
113 return bp::call<std::shared_ptr<CostDataAbstract> >(createData.ptr(),
114 boost::ref(data));
115 }
116 return CostModelAbstract::createData(data);
117 }
118
119 15 std::shared_ptr<CostDataAbstract> default_createData(
120 DataCollectorAbstract* const data) {
121 15 return this->CostModelAbstract::createData(data);
122 }
123 };
124
125 } // namespace python
126 } // namespace crocoddyl
127
128 #endif // BINDINGS_PYTHON_CROCODDYL_MULTIBODY_COST_BASE_HPP_
129