Line |
Branch |
Exec |
Source |
1 |
|
|
|
2 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
3 |
|
|
// BSD 3-Clause License |
4 |
|
|
// |
5 |
|
|
// Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh, |
6 |
|
|
// Heriot-Watt University |
7 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
8 |
|
|
// All rights reserved. |
9 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
10 |
|
|
|
11 |
|
|
#ifndef BINDINGS_PYTHON_CROCODDYL_CORE_STATE_BASE_HPP_ |
12 |
|
|
#define BINDINGS_PYTHON_CROCODDYL_CORE_STATE_BASE_HPP_ |
13 |
|
|
|
14 |
|
|
#include <string> |
15 |
|
|
|
16 |
|
|
#include "crocoddyl/core/state-base.hpp" |
17 |
|
|
#include "python/crocoddyl/core/core.hpp" |
18 |
|
|
|
19 |
|
|
namespace crocoddyl { |
20 |
|
|
namespace python { |
21 |
|
|
|
22 |
|
|
template <typename Scalar> |
23 |
|
|
class StateAbstractTpl_wrap : public StateAbstractTpl<Scalar>, |
24 |
|
|
public bp::wrapper<StateAbstractTpl<Scalar>> { |
25 |
|
|
public: |
26 |
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
27 |
|
✗ |
CROCODDYL_DERIVED_CAST(StateBase, StateAbstractTpl_wrap) |
28 |
|
|
|
29 |
|
|
typedef typename crocoddyl::StateAbstractTpl<Scalar> State; |
30 |
|
|
typedef typename State::VectorXs VectorXs; |
31 |
|
|
typedef typename State::MatrixXs MatrixXs; |
32 |
|
|
using State::lb_; |
33 |
|
|
using State::ndx_; |
34 |
|
|
using State::nq_; |
35 |
|
|
using State::nv_; |
36 |
|
|
using State::nx_; |
37 |
|
|
using State::ub_; |
38 |
|
|
|
39 |
|
8 |
StateAbstractTpl_wrap(std::size_t nx, std::size_t ndx) |
40 |
|
8 |
: State(nx, ndx), bp::wrapper<State>() { |
41 |
|
8 |
enableMultithreading() = false; |
42 |
|
|
} |
43 |
|
✗ |
explicit StateAbstractTpl_wrap() {} |
44 |
|
|
|
45 |
|
✗ |
VectorXs zero() const override { |
46 |
|
✗ |
return bp::call<VectorXs>(this->get_override("zero").ptr()); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
✗ |
VectorXs rand() const override { |
50 |
|
✗ |
return bp::call<VectorXs>(this->get_override("rand").ptr()); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
✗ |
VectorXs diff_wrap(const Eigen::Ref<const VectorXs>& x0, |
54 |
|
|
const Eigen::Ref<const VectorXs>& x1) const { |
55 |
|
✗ |
if (static_cast<std::size_t>(x0.size()) != nx_) { |
56 |
|
✗ |
throw_pretty( |
57 |
|
|
"Invalid argument: " << "x0 has wrong dimension (it should be " + |
58 |
|
|
std::to_string(nx_) + ")"); |
59 |
|
|
} |
60 |
|
✗ |
if (static_cast<std::size_t>(x1.size()) != nx_) { |
61 |
|
✗ |
throw_pretty( |
62 |
|
|
"Invalid argument: " << "x1 has wrong dimension (it should be " + |
63 |
|
|
std::to_string(nx_) + ")"); |
64 |
|
|
} |
65 |
|
✗ |
return bp::call<VectorXs>(this->get_override("diff").ptr(), (VectorXs)x0, |
66 |
|
✗ |
(VectorXs)x1); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
✗ |
void diff(const Eigen::Ref<const VectorXs>& x0, |
70 |
|
|
const Eigen::Ref<const VectorXs>& x1, |
71 |
|
|
Eigen::Ref<VectorXs> dxout) const override { |
72 |
|
✗ |
dxout = diff_wrap(x0, x1); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
✗ |
VectorXs integrate_wrap(const Eigen::Ref<const VectorXs>& x, |
76 |
|
|
const Eigen::Ref<const VectorXs>& dx) const { |
77 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != nx_) { |
78 |
|
✗ |
throw_pretty( |
79 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
80 |
|
|
std::to_string(nx_) + ")"); |
81 |
|
|
} |
82 |
|
✗ |
if (static_cast<std::size_t>(dx.size()) != ndx_) { |
83 |
|
✗ |
throw_pretty( |
84 |
|
|
"Invalid argument: " << "dx has wrong dimension (it should be " + |
85 |
|
|
std::to_string(ndx_) + ")"); |
86 |
|
|
} |
87 |
|
|
return bp::call<VectorXs>(this->get_override("integrate").ptr(), |
88 |
|
✗ |
(VectorXs)x, (VectorXs)dx); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
✗ |
void integrate(const Eigen::Ref<const VectorXs>& x, |
92 |
|
|
const Eigen::Ref<const VectorXs>& dx, |
93 |
|
|
Eigen::Ref<VectorXs> x1out) const override { |
94 |
|
✗ |
x1out = integrate_wrap(x, dx); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
✗ |
void Jdiff(const Eigen::Ref<const VectorXs>& x0, |
98 |
|
|
const Eigen::Ref<const VectorXs>& x1, Eigen::Ref<MatrixXs> Jfirst, |
99 |
|
|
Eigen::Ref<MatrixXs> Jsecond, |
100 |
|
|
const Jcomponent firstsecond) const override { |
101 |
|
✗ |
bp::list res = Jdiff_wrap(x0, x1, firstsecond); |
102 |
|
✗ |
switch (firstsecond) { |
103 |
|
✗ |
case first: { |
104 |
|
✗ |
Jfirst.derived() = bp::extract<MatrixXs>(res[0])(); |
105 |
|
✗ |
break; |
106 |
|
|
} |
107 |
|
✗ |
case second: { |
108 |
|
✗ |
Jsecond.derived() = bp::extract<MatrixXs>(res[0])(); |
109 |
|
✗ |
break; |
110 |
|
|
} |
111 |
|
✗ |
case both: { |
112 |
|
✗ |
Jfirst.derived() = bp::extract<MatrixXs>(res[0])(); |
113 |
|
✗ |
Jsecond.derived() = bp::extract<MatrixXs>(res[1])(); |
114 |
|
✗ |
break; |
115 |
|
|
} |
116 |
|
✗ |
default: { |
117 |
|
✗ |
Jfirst.derived() = bp::extract<MatrixXs>(res[0])(); |
118 |
|
✗ |
Jsecond.derived() = bp::extract<MatrixXs>(res[1])(); |
119 |
|
✗ |
break; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
|
124 |
|
✗ |
bp::list Jdiff_wrap(const Eigen::Ref<const VectorXs>& x0, |
125 |
|
|
const Eigen::Ref<const VectorXs>& x1, |
126 |
|
|
const Jcomponent firstsecond) const { |
127 |
|
✗ |
assert_pretty( |
128 |
|
|
is_a_Jcomponent(firstsecond), |
129 |
|
|
("firstsecond must be one of the Jcomponent {both, first, second}")); |
130 |
|
✗ |
if (static_cast<std::size_t>(x0.size()) != nx_) { |
131 |
|
✗ |
throw_pretty( |
132 |
|
|
"Invalid argument: " << "x0 has wrong dimension (it should be " + |
133 |
|
|
std::to_string(nx_) + ")"); |
134 |
|
|
} |
135 |
|
✗ |
if (static_cast<std::size_t>(x1.size()) != nx_) { |
136 |
|
✗ |
throw_pretty( |
137 |
|
|
"Invalid argument: " << "x1 has wrong dimension (it should be " + |
138 |
|
|
std::to_string(nx_) + ")"); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
✗ |
bp::list Jacs; |
142 |
|
✗ |
switch (firstsecond) { |
143 |
|
✗ |
case first: { |
144 |
|
✗ |
MatrixXs J = |
145 |
|
✗ |
bp::call<MatrixXs>(this->get_override("Jdiff").ptr(), (VectorXs)x0, |
146 |
|
✗ |
(VectorXs)x1, firstsecond); |
147 |
|
✗ |
Jacs.append(J); |
148 |
|
✗ |
break; |
149 |
|
|
} |
150 |
|
✗ |
case second: { |
151 |
|
✗ |
MatrixXs J = |
152 |
|
✗ |
bp::call<MatrixXs>(this->get_override("Jdiff").ptr(), (VectorXs)x0, |
153 |
|
✗ |
(VectorXs)x1, firstsecond); |
154 |
|
✗ |
Jacs.append(J); |
155 |
|
✗ |
break; |
156 |
|
|
} |
157 |
|
✗ |
case both: { |
158 |
|
✗ |
Jacs = bp::call<bp::list>(this->get_override("Jdiff").ptr(), |
159 |
|
✗ |
(VectorXs)x0, (VectorXs)x1, firstsecond); |
160 |
|
✗ |
break; |
161 |
|
|
} |
162 |
|
✗ |
default: { |
163 |
|
✗ |
Jacs = bp::call<bp::list>(this->get_override("Jdiff").ptr(), |
164 |
|
✗ |
(VectorXs)x0, (VectorXs)x1, firstsecond); |
165 |
|
✗ |
break; |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
✗ |
return Jacs; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
✗ |
void Jintegrate(const Eigen::Ref<const VectorXs>& x, |
172 |
|
|
const Eigen::Ref<const VectorXs>& dx, |
173 |
|
|
Eigen::Ref<MatrixXs> Jfirst, Eigen::Ref<MatrixXs> Jsecond, |
174 |
|
|
const Jcomponent firstsecond, |
175 |
|
|
const AssignmentOp op) const override { |
176 |
|
✗ |
bp::list res = Jintegrate_wrap(x, dx, firstsecond); |
177 |
|
✗ |
if (firstsecond == first || firstsecond == both) { |
178 |
|
✗ |
if (static_cast<std::size_t>(Jfirst.rows()) != ndx_ || |
179 |
|
✗ |
static_cast<std::size_t>(Jfirst.cols()) != ndx_) { |
180 |
|
✗ |
throw_pretty("Invalid argument: " |
181 |
|
|
<< "Jfirst has wrong dimension (it should be " + |
182 |
|
|
std::to_string(ndx_) + "," + std::to_string(ndx_) + |
183 |
|
|
")"); |
184 |
|
|
} |
185 |
|
✗ |
switch (op) { |
186 |
|
✗ |
case setto: { |
187 |
|
✗ |
Jfirst.derived() = bp::extract<MatrixXs>(res[0])(); |
188 |
|
✗ |
break; |
189 |
|
|
} |
190 |
|
✗ |
case addto: { |
191 |
|
✗ |
Jfirst.derived() += bp::extract<MatrixXs>(res[0])(); |
192 |
|
✗ |
break; |
193 |
|
|
} |
194 |
|
✗ |
case rmfrom: { |
195 |
|
✗ |
Jfirst.derived() -= bp::extract<MatrixXs>(res[0])(); |
196 |
|
✗ |
break; |
197 |
|
|
} |
198 |
|
✗ |
default: { |
199 |
|
✗ |
throw_pretty( |
200 |
|
|
"Invalid argument: allowed operators: setto, addto, rmfrom"); |
201 |
|
|
break; |
202 |
|
|
} |
203 |
|
|
} |
204 |
|
|
} |
205 |
|
✗ |
if (firstsecond == second || firstsecond == both) { |
206 |
|
✗ |
if (static_cast<std::size_t>(Jsecond.rows()) != ndx_ || |
207 |
|
✗ |
static_cast<std::size_t>(Jsecond.cols()) != ndx_) { |
208 |
|
✗ |
throw_pretty("Invalid argument: " |
209 |
|
|
<< "Jsecond has wrong dimension (it should be " + |
210 |
|
|
std::to_string(ndx_) + "," + std::to_string(ndx_) + |
211 |
|
|
")"); |
212 |
|
|
} |
213 |
|
✗ |
switch (op) { |
214 |
|
✗ |
case setto: { |
215 |
|
✗ |
Jsecond.derived() = bp::extract<MatrixXs>(res[0])(); |
216 |
|
✗ |
break; |
217 |
|
|
} |
218 |
|
✗ |
case addto: { |
219 |
|
✗ |
Jsecond.derived() += bp::extract<MatrixXs>(res[0])(); |
220 |
|
✗ |
break; |
221 |
|
|
} |
222 |
|
✗ |
case rmfrom: { |
223 |
|
✗ |
Jsecond.derived() -= bp::extract<MatrixXs>(res[0])(); |
224 |
|
✗ |
break; |
225 |
|
|
} |
226 |
|
✗ |
default: { |
227 |
|
✗ |
throw_pretty( |
228 |
|
|
"Invalid argument: allowed operators: setto, addto, rmfrom"); |
229 |
|
|
break; |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
} |
234 |
|
|
|
235 |
|
✗ |
bp::list Jintegrate_wrap(const Eigen::Ref<const VectorXs>& x, |
236 |
|
|
const Eigen::Ref<const VectorXs>& dx, |
237 |
|
|
const Jcomponent firstsecond) const { |
238 |
|
✗ |
assert_pretty( |
239 |
|
|
is_a_Jcomponent(firstsecond), |
240 |
|
|
("firstsecond must be one of the Jcomponent {both, first, second}")); |
241 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != nx_) { |
242 |
|
✗ |
throw_pretty( |
243 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
244 |
|
|
std::to_string(nx_) + ")"); |
245 |
|
|
} |
246 |
|
✗ |
if (static_cast<std::size_t>(dx.size()) != ndx_) { |
247 |
|
✗ |
throw_pretty( |
248 |
|
|
"Invalid argument: " << "dx has wrong dimension (it should be " + |
249 |
|
|
std::to_string(ndx_) + ")"); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
✗ |
bp::list Jacs; |
253 |
|
✗ |
switch (firstsecond) { |
254 |
|
✗ |
case first: { |
255 |
|
✗ |
MatrixXs J = bp::call<MatrixXs>(this->get_override("Jintegrate").ptr(), |
256 |
|
✗ |
(VectorXs)x, (VectorXs)dx, firstsecond); |
257 |
|
✗ |
Jacs.append(J); |
258 |
|
✗ |
break; |
259 |
|
|
} |
260 |
|
✗ |
case second: { |
261 |
|
✗ |
MatrixXs J = bp::call<MatrixXs>(this->get_override("Jintegrate").ptr(), |
262 |
|
✗ |
(VectorXs)x, (VectorXs)dx, firstsecond); |
263 |
|
✗ |
Jacs.append(J); |
264 |
|
✗ |
break; |
265 |
|
|
} |
266 |
|
✗ |
case both: { |
267 |
|
✗ |
Jacs = bp::call<bp::list>(this->get_override("Jintegrate").ptr(), |
268 |
|
✗ |
(VectorXs)x, (VectorXs)dx, firstsecond); |
269 |
|
✗ |
break; |
270 |
|
|
} |
271 |
|
✗ |
default: { |
272 |
|
✗ |
Jacs = bp::call<bp::list>(this->get_override("Jintegrate").ptr(), |
273 |
|
✗ |
(VectorXs)x, (VectorXs)dx, firstsecond); |
274 |
|
✗ |
break; |
275 |
|
|
} |
276 |
|
|
} |
277 |
|
✗ |
return Jacs; |
278 |
|
|
} |
279 |
|
|
|
280 |
|
✗ |
void JintegrateTransport(const Eigen::Ref<const VectorXs>& x, |
281 |
|
|
const Eigen::Ref<const VectorXs>& dx, |
282 |
|
|
Eigen::Ref<MatrixXs> Jin, |
283 |
|
|
const Jcomponent firstsecond) const override { |
284 |
|
✗ |
Jin = JintegrateTransport_wrap(x, dx, Jin, firstsecond); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
✗ |
MatrixXs JintegrateTransport_wrap(const Eigen::Ref<const VectorXs>& x, |
288 |
|
|
const Eigen::Ref<const VectorXs>& dx, |
289 |
|
|
Eigen::Ref<MatrixXs> Jin, |
290 |
|
|
const Jcomponent firstsecond) const { |
291 |
|
✗ |
assert_pretty( |
292 |
|
|
is_a_Jcomponent(firstsecond), |
293 |
|
|
("firstsecond must be one of the Jcomponent {both, first, second}")); |
294 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != nx_) { |
295 |
|
✗ |
throw_pretty( |
296 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
297 |
|
|
std::to_string(nx_) + ")"); |
298 |
|
|
} |
299 |
|
✗ |
if (static_cast<std::size_t>(dx.size()) != ndx_) { |
300 |
|
✗ |
throw_pretty( |
301 |
|
|
"Invalid argument: " << "dx has wrong dimension (it should be " + |
302 |
|
|
std::to_string(ndx_) + ")"); |
303 |
|
|
} |
304 |
|
|
return bp::call<MatrixXs>(this->get_override("JintegrateTransport").ptr(), |
305 |
|
✗ |
(VectorXs)x, (VectorXs)dx, (MatrixXs)Jin, |
306 |
|
✗ |
firstsecond); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
template <typename NewScalar> |
310 |
|
✗ |
StateAbstractTpl_wrap<NewScalar> cast() const { |
311 |
|
|
typedef StateAbstractTpl_wrap<NewScalar> ReturnType; |
312 |
|
✗ |
ReturnType ret(nx_, ndx_); |
313 |
|
✗ |
return ret; |
314 |
|
|
} |
315 |
|
|
}; |
316 |
|
|
|
317 |
|
20 |
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Jdiffs, StateAbstract::Jdiff_Js, 2, 3) |
318 |
|
10 |
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Jintegrates, |
319 |
|
|
StateAbstract::Jintegrate_Js, 2, 3) |
320 |
|
|
|
321 |
|
|
} // namespace python |
322 |
|
|
} // namespace crocoddyl |
323 |
|
|
|
324 |
|
|
#endif // BINDINGS_PYTHON_CROCODDYL_CORE_STATE_BASE_HPP_ |
325 |
|
|
|