Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2020-2025, University of Edinburgh, Heriot-Watt University |
5 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
6 |
|
|
// All rights reserved. |
7 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
8 |
|
|
|
9 |
|
|
#ifndef BINDINGS_PYTHON_CROCODDYL_CORE_CONSTRAINT_BASE_HPP_ |
10 |
|
|
#define BINDINGS_PYTHON_CROCODDYL_CORE_CONSTRAINT_BASE_HPP_ |
11 |
|
|
|
12 |
|
|
#include "crocoddyl/core/constraint-base.hpp" |
13 |
|
|
#include "python/crocoddyl/core/core.hpp" |
14 |
|
|
|
15 |
|
|
namespace crocoddyl { |
16 |
|
|
namespace python { |
17 |
|
|
|
18 |
|
|
template <typename _Scalar> |
19 |
|
|
class ConstraintModelAbstractTpl_wrap |
20 |
|
|
: public ConstraintModelAbstractTpl<_Scalar>, |
21 |
|
|
public bp::wrapper<ConstraintModelAbstractTpl<_Scalar>> { |
22 |
|
|
public: |
23 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
24 |
|
✗ |
CROCODDYL_DERIVED_CAST(ConstraintModelBase, ConstraintModelAbstractTpl_wrap) |
25 |
|
|
|
26 |
|
|
typedef _Scalar Scalar; |
27 |
|
|
typedef typename ScalarSelector<Scalar>::type ScalarType; |
28 |
|
|
typedef typename crocoddyl::ConstraintModelAbstractTpl<Scalar> |
29 |
|
|
ConstraintModel; |
30 |
|
|
typedef typename crocoddyl::ConstraintDataAbstractTpl<Scalar> ConstraintData; |
31 |
|
|
typedef typename ConstraintModel::StateAbstract State; |
32 |
|
|
typedef typename ConstraintModel::ResidualModelAbstract ResidualModel; |
33 |
|
|
typedef typename ConstraintModel::DataCollectorAbstract DataCollector; |
34 |
|
|
typedef typename ConstraintModel::VectorXs VectorXs; |
35 |
|
|
using ConstraintModel::ng_; |
36 |
|
|
using ConstraintModel::nh_; |
37 |
|
|
using ConstraintModel::nu_; |
38 |
|
|
using ConstraintModel::residual_; |
39 |
|
|
using ConstraintModel::state_; |
40 |
|
|
using ConstraintModel::T_constraint_; |
41 |
|
|
using ConstraintModel::unone_; |
42 |
|
|
|
43 |
|
✗ |
ConstraintModelAbstractTpl_wrap(std::shared_ptr<State> state, |
44 |
|
|
std::shared_ptr<ResidualModel> residual, |
45 |
|
|
const std::size_t ng, const std::size_t nh) |
46 |
|
|
: ConstraintModel(state, residual, ng, nh), |
47 |
|
✗ |
bp::wrapper<ConstraintModel>() { |
48 |
|
✗ |
unone_ = VectorXs::Constant(nu_, Scalar(NAN)); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
✗ |
ConstraintModelAbstractTpl_wrap(std::shared_ptr<State> state, |
52 |
|
|
const std::size_t nu, const std::size_t ng, |
53 |
|
|
const std::size_t nh, |
54 |
|
|
const bool T_const = true) |
55 |
|
|
: ConstraintModel(state, nu, ng, nh, T_const), |
56 |
|
✗ |
bp::wrapper<ConstraintModel>() { |
57 |
|
✗ |
unone_ = VectorXs::Constant(nu_, Scalar(NAN)); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
✗ |
ConstraintModelAbstractTpl_wrap(std::shared_ptr<State> state, |
61 |
|
|
const std::size_t ng, const std::size_t nh, |
62 |
|
|
const bool T_const = true) |
63 |
|
✗ |
: ConstraintModel(state, ng, nh, T_const) { |
64 |
|
✗ |
unone_ = VectorXs::Constant(nu_, Scalar(NAN)); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
✗ |
void calc(const std::shared_ptr<ConstraintData>& data, |
68 |
|
|
const Eigen::Ref<const VectorXs>& x, |
69 |
|
|
const Eigen::Ref<const VectorXs>& u) override { |
70 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
71 |
|
✗ |
throw_pretty( |
72 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
73 |
|
|
std::to_string(state_->get_nx()) + ")"); |
74 |
|
|
} |
75 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
76 |
|
✗ |
throw_pretty( |
77 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
78 |
|
|
std::to_string(nu_) + ")"); |
79 |
|
|
} |
80 |
|
✗ |
if (std::isnan( |
81 |
|
✗ |
scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
82 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, |
83 |
|
✗ |
(VectorXs)x); |
84 |
|
|
} else { |
85 |
|
✗ |
return bp::call<void>(this->get_override("calc").ptr(), data, (VectorXs)x, |
86 |
|
✗ |
(VectorXs)u); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
|
90 |
|
✗ |
void calcDiff(const std::shared_ptr<ConstraintData>& data, |
91 |
|
|
const Eigen::Ref<const VectorXs>& x, |
92 |
|
|
const Eigen::Ref<const VectorXs>& u) override { |
93 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != state_->get_nx()) { |
94 |
|
✗ |
throw_pretty( |
95 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
96 |
|
|
std::to_string(state_->get_nx()) + ")"); |
97 |
|
|
} |
98 |
|
✗ |
if (static_cast<std::size_t>(u.size()) != nu_) { |
99 |
|
✗ |
throw_pretty( |
100 |
|
|
"Invalid argument: " << "u has wrong dimension (it should be " + |
101 |
|
|
std::to_string(nu_) + ")"); |
102 |
|
|
} |
103 |
|
✗ |
if (std::isnan( |
104 |
|
✗ |
scalar_cast<ScalarType>(u.template lpNorm<Eigen::Infinity>()))) { |
105 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
106 |
|
✗ |
(VectorXs)x); |
107 |
|
|
} else { |
108 |
|
✗ |
return bp::call<void>(this->get_override("calcDiff").ptr(), data, |
109 |
|
✗ |
(VectorXs)x, (VectorXs)u); |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
✗ |
std::shared_ptr<ConstraintData> createData( |
114 |
|
|
DataCollector* const data) override { |
115 |
|
✗ |
if (boost::python::override createData = this->get_override("createData")) { |
116 |
|
|
return bp::call<std::shared_ptr<ConstraintData>>(createData.ptr(), |
117 |
|
✗ |
boost::ref(data)); |
118 |
|
|
} |
119 |
|
✗ |
return ConstraintModel::createData(data); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
✗ |
std::shared_ptr<ConstraintData> default_createData( |
123 |
|
|
DataCollector* const data) { |
124 |
|
✗ |
return this->ConstraintModel::createData(data); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
template <typename NewScalar> |
128 |
|
✗ |
ConstraintModelAbstractTpl_wrap<NewScalar> cast() const { |
129 |
|
|
typedef ConstraintModelAbstractTpl_wrap<NewScalar> ReturnType; |
130 |
|
✗ |
if (residual_) { |
131 |
|
✗ |
ReturnType ret(state_->template cast<NewScalar>(), |
132 |
|
✗ |
residual_->template cast<NewScalar>(), ng_, nh_); |
133 |
|
✗ |
return ret; |
134 |
|
✗ |
} else { |
135 |
|
✗ |
ReturnType ret(state_->template cast<NewScalar>(), nu_, ng_, nh_, |
136 |
|
✗ |
T_constraint_); |
137 |
|
✗ |
return ret; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
}; |
141 |
|
|
|
142 |
|
|
} // namespace python |
143 |
|
|
} // namespace crocoddyl |
144 |
|
|
|
145 |
|
|
#endif // BINDINGS_PYTHON_CROCODDYL_CORE_CONSTRAINT_BASE_HPP_ |
146 |
|
|
|