5 #ifndef __pinocchio_python_context_casadi_hpp__
6 #define __pinocchio_python_context_casadi_hpp__
10 #define PINOCCHIO_PYTHON_SCALAR_TYPE ::casadi::SX
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
67 template<
typename CasadiScalar,
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
69 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>,
71 Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols, Options, MaxRows, MaxCols>>>
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>>
84 typedef Eigen::Matrix<::casadi::Matrix<CasadiScalar>, Rows, Cols,
Options, MaxRows, MaxCols>
91 static void construct(PyObject * pyObj, bp::converter::rvalue_from_python_stage1_data * memory);
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)); \
111 if (casadi_matrix_swig_obj == NULL)
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;
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)
185 assert(casadi_matrix_swig_obj != NULL);
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;
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(
216 bp::type_id<MatType>()
217 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
224 template<
typename MatType>
225 struct EigenToPy<MatType, ::casadi::Matrix<::casadi::SXElem>>
233 (
mat.rows() < INT_MAX) && (
mat.cols() < INT_MAX)
234 &&
"Matrix range larger than int ... should never happen.");
236 PyObject * casadi_matrix_py_ptr =
241 assert(casadi_matrix_swig_obj != NULL);
247 Py_DECREF(
reinterpret_cast<PyObject *
>(casadi_matrix_swig_obj));
248 return casadi_matrix_py_ptr;
253 return ::eigenpy::casadi::CasadiType::getSXType();
257 template<
typename TensorType,
typename _Scalar>
260 Eigen::TensorBase<TensorType>,
261 ::casadi::Matrix<_Scalar>>
268 template<
typename SparseType,
typename _Scalar>
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>>
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 =
294 assert(casadi_matrix_swig_obj != NULL);
300 Py_DECREF(
reinterpret_cast<PyObject *
>(casadi_matrix_swig_obj));
301 return casadi_matrix_py_ptr;
306 return ::eigenpy::casadi::CasadiType::getSXType();
315 void * ip,
void * array)
319 PyArrayObject * py_array =
static_cast<PyArrayObject *
>(array);
320 if (py_array == NULL || PyArray_ISBEHAVED_RO(py_array))
323 return (npy_bool)(
value.is_zero());
328 PyArray_DESCR(py_array)->f->copyswap(
329 &tmp_value, ip, PyArray_ISBYTESWAPPED(py_array), array);
330 return (npy_bool)(tmp_value.is_zero());
343 template<
typename CasadiMatrix>
347 static PyObject *
convert(CasadiMatrix
const & x)
349 PyObject * casadi_matrix_py_ptr = PyObject_CallObject(
353 assert(casadi_matrix_swig_obj != NULL);
355 CasadiMatrix * casadi_matrix_obj_ptr =
356 reinterpret_cast<CasadiMatrix *
>(casadi_matrix_swig_obj->
ptr);
357 *casadi_matrix_obj_ptr =
x;
359 Py_DECREF(
reinterpret_cast<PyObject *
>(casadi_matrix_swig_obj));
360 return casadi_matrix_py_ptr;
365 return ::eigenpy::casadi::CasadiType::getSXType();
370 boost::python::to_python_converter<CasadiMatrix, CasadiMatrixToPython, true>();
374 template<
typename CasadiMatrix>
381 throw std::runtime_error(
"Should never be called");
387 boost::python::converter::registry::insert(
389 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
403 return casadi_matrix_swig_obj->
ptr;
405 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
408 return ::eigenpy::casadi::CasadiType::getSXType();
453 bp::object scalar_type(bp::handle<>(bp::borrowed(pyObj)));
463 boost::python::implicitly_convertible<double, Scalar>();
464 boost::python::implicitly_convertible<float, Scalar>();
465 boost::python::implicitly_convertible<int, Scalar>();
466 boost::python::implicitly_convertible<long, Scalar>();
467 boost::python::implicitly_convertible<bool, Scalar>();
476 template<
typename Scalar>
483 #endif // #ifndef __pinocchio_python_context_casadi_hpp__