5#ifndef __pinocchio_python_context_casadi_hpp__
6#define __pinocchio_python_context_casadi_hpp__
8#include "pinocchio/autodiff/casadi.hpp"
10#define PINOCCHIO_PYTHON_SCALAR_TYPE ::casadi::SX
11#include "pinocchio/bindings/python/context/generic.hpp"
12#undef PINOCCHIO_PYTHON_SCALAR_TYPE
14#define PINOCCHIO_PYTHON_SKIP_COMPARISON_OPERATIONS
15#define PINOCCHIO_PYTHON_NO_SERIALIZATION
16#define PINOCCHIO_PYTHON_SKIP_REACHABLE_WORKSPACE
17#define PINOCCHIO_PYTHON_SKIP_ALGORITHM_CONSTRAINED_DYNAMICS
19#define PINOCCHIO_PYTHON_SKIP_CASADI_UNSUPPORTED
21#include <eigenpy/eigenpy.hpp>
22#include <eigenpy/user-type.hpp>
23#include <eigenpy/ufunc.hpp>
24#include <eigenpy/swig.hpp>
29 namespace bp = boost::python;
36 static PyTypeObject * getSXType()
38 return reinterpret_cast<PyTypeObject *
>(getInstance().casadi_SX_type.ptr());
50 casadi_module = bp::import(
"casadi");
51 casadi_SX_type = casadi_module.attr(
"SX");
52 Py_INCREF(casadi_module.ptr());
57 casadi_SX_type.~object();
61 bp::object casadi_module;
62 bp::object casadi_SX_type;
67 template<
typename CasadiScalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
68 struct expected_pytype_for_arg<
69 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>,
71 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>>>
73 static PyTypeObject
const * get_pytype()
75 return ::eigenpy::casadi::CasadiType::getSXType();
79 template<
typename CasadiScalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
81 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>>
83 typedef ::casadi::Matrix<CasadiScalar> CasadiMatrix;
84 typedef Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>
88 static void * convertible(PyObject * pyObj);
91 static void construct(PyObject * pyObj, bp::converter::rvalue_from_python_stage1_data * memory);
93 static void registration();
96 template<
typename CasadiScalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
98 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>>::
99 convertible(PyObject * pyObj)
101 if (std::strcmp(pyObj->ob_type->tp_name, CasadiMatrix::type_name().c_str()) != 0)
104#define RETURN_VALUE(value) \
106 Py_DECREF(reinterpret_cast<PyObject *>(casadi_matrix_swig_obj)); \
110 eigenpy::PySwigObject * casadi_matrix_swig_obj = eigenpy::get_PySwigObject(pyObj);
111 if (casadi_matrix_swig_obj == NULL)
114 CasadiMatrix * casadi_matrix_ptr =
115 reinterpret_cast<CasadiMatrix *
>(casadi_matrix_swig_obj->ptr);
116 const CasadiMatrix & casadi_matrix = *casadi_matrix_ptr;
118 const casadi_int R = casadi_matrix.rows(), C = casadi_matrix.columns(),
119 size = casadi_matrix.numel();
121 const int ndim = (R == 0 || C == 0) ? 0 : (R == 1 || C == 1) ? 1 : 2;
123 if (MatType::IsVectorAtCompileTime)
125 const Eigen::DenseIndex size_at_compile_time =
126 MatType::IsRowMajor ? MatType::ColsAtCompileTime : MatType::RowsAtCompileTime;
133 if (size_at_compile_time != Eigen::Dynamic)
136 if (size == size_at_compile_time)
138 if (MatType::ColsAtCompileTime != C || MatType::RowsAtCompileTime != R)
154 assert(R > 1 && C > 1);
168 if ((MatType::RowsAtCompileTime != R) && (MatType::RowsAtCompileTime != Eigen::Dynamic))
170 if ((MatType::ColsAtCompileTime != C) && (MatType::ColsAtCompileTime != Eigen::Dynamic))
179 template<
typename CasadiScalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
181 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>>::
182 construct(PyObject * pyObj, bp::converter::rvalue_from_python_stage1_data * memory)
184 eigenpy::PySwigObject * casadi_matrix_swig_obj = eigenpy::get_PySwigObject(pyObj);
185 assert(casadi_matrix_swig_obj != NULL);
187 CasadiMatrix * casadi_matrix_ptr =
188 reinterpret_cast<CasadiMatrix *
>(casadi_matrix_swig_obj->ptr);
189 const CasadiMatrix & casadi_matrix = *casadi_matrix_ptr;
191 const casadi_int R = casadi_matrix.rows(), C = casadi_matrix.columns();
193 bp::converter::rvalue_from_python_storage<MatType> * storage =
194 reinterpret_cast<bp::converter::rvalue_from_python_storage<MatType> *
>(
195 reinterpret_cast<void *
>(memory));
198 void * storage_ptr = storage->storage.bytes;
199 MatType * eigen_matrix_ptr =
200 ::eigenpy::details::init_matrix_or_array<MatType>::run(R, C, storage_ptr);
203 pinocchio::casadi::copy(casadi_matrix, *eigen_matrix_ptr);
205 memory->convertible = storage->storage.bytes;
206 Py_DECREF(
reinterpret_cast<PyObject *
>(casadi_matrix_swig_obj));
209 template<
typename CasadiScalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
211 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>>::
214 bp::converter::registry::push_back(
215 reinterpret_cast<void * (*)(_object *)
>(&EigenFromPy::convertible), &EigenFromPy::construct,
216 bp::type_id<MatType>()
217#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
219 &eigenpy::expected_pytype_for_arg<MatType>::get_pytype
224 template<
typename MatType>
225 struct EigenToPy<MatType, ::casadi::Matrix<::casadi::SXElem>>
227 typedef ::casadi::Matrix<::casadi::SXElem> CasadiMatrix;
230 convert(
typename boost::add_reference<
typename boost::add_const<MatType>::type>::type mat)
233 (mat.rows() < INT_MAX) && (mat.cols() < INT_MAX)
234 &&
"Matrix range larger than int ... should never happen.");
236 PyObject * casadi_matrix_py_ptr =
237 PyObject_CallObject(
reinterpret_cast<PyObject *
>(casadi::CasadiType::getSXType()), NULL);
239 eigenpy::PySwigObject * casadi_matrix_swig_obj =
240 eigenpy::get_PySwigObject(casadi_matrix_py_ptr);
241 assert(casadi_matrix_swig_obj != NULL);
243 CasadiMatrix * casadi_matrix_obj_ptr =
244 reinterpret_cast<CasadiMatrix *
>(casadi_matrix_swig_obj->ptr);
245 pinocchio::casadi::copy(mat, *casadi_matrix_obj_ptr);
247 Py_DECREF(
reinterpret_cast<PyObject *
>(casadi_matrix_swig_obj));
248 return casadi_matrix_py_ptr;
251 static PyTypeObject
const * get_pytype()
253 return ::eigenpy::casadi::CasadiType::getSXType();
257 template<
typename TensorType,
typename _Scalar>
258 struct expose_eigen_type_impl<
260 Eigen::TensorBase<TensorType>,
261 ::casadi::Matrix<_Scalar>>
268 template<
typename SparseType,
typename _Scalar>
269 struct expose_eigen_type_impl<
271 Eigen::SparseMatrixBase<SparseType>,
272 ::casadi::Matrix<_Scalar>>
279 template<
typename MatType,
int Options,
typename Str
ide>
280 struct EigenToPy<Eigen::Ref<MatType, Options, Stride>, ::casadi::Matrix<::casadi::SXElem>>
282 typedef ::casadi::Matrix<::casadi::SXElem> CasadiMatrix;
284 static PyObject * convert(
const Eigen::Ref<MatType, Options, Stride> & mat)
287 (mat.rows() < INT_MAX) && (mat.cols() < INT_MAX)
288 &&
"Matrix range larger than int ... should never happen.");
289 PyObject * casadi_matrix_py_ptr =
290 PyObject_CallObject(
reinterpret_cast<PyObject *
>(casadi::CasadiType::getSXType()), NULL);
292 eigenpy::PySwigObject * casadi_matrix_swig_obj =
293 eigenpy::get_PySwigObject(casadi_matrix_py_ptr);
294 assert(casadi_matrix_swig_obj != NULL);
296 CasadiMatrix * casadi_matrix_obj_ptr =
297 reinterpret_cast<CasadiMatrix *
>(casadi_matrix_swig_obj->ptr);
298 pinocchio::casadi::copy(mat.derived(), *casadi_matrix_obj_ptr);
300 Py_DECREF(
reinterpret_cast<PyObject *
>(casadi_matrix_swig_obj));
301 return casadi_matrix_py_ptr;
304 static PyTypeObject
const * get_pytype()
306 return ::eigenpy::casadi::CasadiType::getSXType();
314 inline npy_bool SpecialMethods<pinocchio::python::context::Scalar, NPY_USERDEF>::nonzero(
315 void * ip,
void * array)
318 typedef pinocchio::python::context::Scalar Scalar;
319 PyArrayObject * py_array =
static_cast<PyArrayObject *
>(array);
320 if (py_array == NULL || PyArray_ISBEHAVED_RO(py_array))
322 const Scalar & value = *
static_cast<Scalar *
>(ip);
323 return (npy_bool)(value.is_zero());
328 PyArray_Descr * descr = PyArray_DESCR(py_array);
329 PyArray_ArrFuncs * f = PyDataType_GetArrFuncs(descr);
330 f->copyswap(&tmp_value, ip, PyArray_ISBYTESWAPPED(py_array), array);
331 return (npy_bool)(tmp_value.is_zero());
344 template<
typename CasadiMatrix>
348 static PyObject * convert(CasadiMatrix
const & x)
351 reinterpret_cast<PyObject *
>(eigenpy::casadi::CasadiType::getSXType()),
NULL);
366 return ::eigenpy::casadi::CasadiType::getSXType();
369 static void registration()
371 boost::python::to_python_converter<CasadiMatrix, CasadiMatrixToPython, true>();
375 template<
typename CasadiMatrix>
380 static CasadiMatrix & execute(
PyObject * )
382 throw std::runtime_error(
"Should never be called");
386 static void registration()
388 boost::python::converter::registry::insert(
389 &extract, boost::python::detail::extractor_type_id(&Extractor::execute)
406#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
409 return ::eigenpy::casadi::CasadiType::getSXType();
449 inline boost::python::object getScalarType()
451 namespace bp = boost::python;
453 PyObject * pyObj =
reinterpret_cast<PyObject *
>(::eigenpy::casadi::CasadiType::getSXType());
454 bp::object scalar_type(bp::handle<>(bp::borrowed(pyObj)));
459 inline void exposeSpecificTypeFeatures()
461 typedef pinocchio::python::context::Scalar Scalar;
462 CasadiMatrixToPython<Scalar>::registration();
463 CasadiMatrixFromPython<Scalar>::registration();
464 boost::python::implicitly_convertible<double, Scalar>();
465 boost::python::implicitly_convertible<float, Scalar>();
466 boost::python::implicitly_convertible<int, Scalar>();
467 boost::python::implicitly_convertible<long, Scalar>();
468 boost::python::implicitly_convertible<bool, Scalar>();
477 template<
typename Scalar>
478 struct has_operator_equal<::casadi::Matrix<Scalar>> : boost::false_type
Main pinocchio namespace.