Verified Commit ca5c9252 authored by Justin Carpentier's avatar Justin Carpentier
Browse files

core: allows template specialization of EigenFromPy

parent 8bbaa052
...@@ -275,13 +275,13 @@ namespace eigenpy ...@@ -275,13 +275,13 @@ namespace eigenpy
memory->convertible = storage->storage.bytes; memory->convertible = storage->storage.bytes;
} }
template<typename MatType> template<typename MatType, typename _Scalar>
struct EigenFromPy struct EigenFromPy
{ {
typedef typename MatType::Scalar Scalar; typedef typename MatType::Scalar Scalar;
/// \brief Determine if pyObj can be converted into a MatType object /// \brief Determine if pyObj can be converted into a MatType object
static void* convertible(PyArrayObject* pyArray); static void* convertible(PyObject* pyObj);
/// \brief Allocate memory and copy pyObj in the new storage /// \brief Allocate memory and copy pyObj in the new storage
static void construct(PyObject* pyObj, static void construct(PyObject* pyObj,
...@@ -290,12 +290,14 @@ namespace eigenpy ...@@ -290,12 +290,14 @@ namespace eigenpy
static void registration(); static void registration();
}; };
template<typename MatType> template<typename MatType, typename _Scalar>
void* EigenFromPy<MatType>::convertible(PyArrayObject* pyArray) void* EigenFromPy<MatType,_Scalar>::convertible(PyObject* pyObj)
{ {
if(!call_PyArray_Check(reinterpret_cast<PyObject*>(pyArray))) if(!call_PyArray_Check(reinterpret_cast<PyObject*>(pyObj)))
return 0; return 0;
PyArrayObject * pyArray = reinterpret_cast<PyArrayObject*>(pyObj);
if(!np_type_is_convertible_into_scalar<Scalar>(EIGENPY_GET_PY_ARRAY_TYPE(pyArray))) if(!np_type_is_convertible_into_scalar<Scalar>(EIGENPY_GET_PY_ARRAY_TYPE(pyArray)))
return 0; return 0;
...@@ -403,15 +405,15 @@ namespace eigenpy ...@@ -403,15 +405,15 @@ namespace eigenpy
return pyArray; return pyArray;
} }
template<typename MatType> template<typename MatType, typename _Scalar>
void EigenFromPy<MatType>::construct(PyObject* pyObj, void EigenFromPy<MatType,_Scalar>::construct(PyObject* pyObj,
bp::converter::rvalue_from_python_stage1_data* memory) bp::converter::rvalue_from_python_stage1_data* memory)
{ {
eigen_from_py_construct<MatType>(pyObj,memory); eigen_from_py_construct<MatType>(pyObj,memory);
} }
template<typename MatType> template<typename MatType, typename _Scalar>
void EigenFromPy<MatType>::registration() void EigenFromPy<MatType,_Scalar>::registration()
{ {
bp::converter::registry::push_back bp::converter::registry::push_back
(reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible), (reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible),
...@@ -431,7 +433,7 @@ namespace eigenpy ...@@ -431,7 +433,7 @@ namespace eigenpy
// Add conversion to Eigen::EigenBase<MatType> // Add conversion to Eigen::EigenBase<MatType>
typedef Eigen::EigenBase<MatType> EigenBase; typedef Eigen::EigenBase<MatType> EigenBase;
EigenFromPy<EigenBase>::registration(); EigenFromPy<EigenBase,typename MatType::Scalar>::registration();
// Add conversion to Eigen::PlainObjectBase<MatType> // Add conversion to Eigen::PlainObjectBase<MatType>
typedef Eigen::PlainObjectBase<MatType> PlainObjectBase; typedef Eigen::PlainObjectBase<MatType> PlainObjectBase;
...@@ -449,8 +451,8 @@ namespace eigenpy ...@@ -449,8 +451,8 @@ namespace eigenpy
} }
}; };
template<typename MatType> template<typename MatType, typename _Scalar>
struct EigenFromPy< Eigen::MatrixBase<MatType> > : EigenFromPy<MatType> struct EigenFromPy< Eigen::MatrixBase<MatType>, _Scalar > : EigenFromPy<MatType>
{ {
typedef EigenFromPy<MatType> EigenFromPyDerived; typedef EigenFromPy<MatType> EigenFromPyDerived;
typedef Eigen::MatrixBase<MatType> Base; typedef Eigen::MatrixBase<MatType> Base;
...@@ -463,8 +465,8 @@ namespace eigenpy ...@@ -463,8 +465,8 @@ namespace eigenpy
} }
}; };
template<typename MatType> template<typename MatType, typename _Scalar>
struct EigenFromPy< Eigen::EigenBase<MatType> > : EigenFromPy<MatType> struct EigenFromPy< Eigen::EigenBase<MatType>, _Scalar > : EigenFromPy<MatType>
{ {
typedef EigenFromPy<MatType> EigenFromPyDerived; typedef EigenFromPy<MatType> EigenFromPyDerived;
typedef Eigen::EigenBase<MatType> Base; typedef Eigen::EigenBase<MatType> Base;
...@@ -477,8 +479,8 @@ namespace eigenpy ...@@ -477,8 +479,8 @@ namespace eigenpy
} }
}; };
template<typename MatType> template<typename MatType, typename _Scalar>
struct EigenFromPy< Eigen::PlainObjectBase<MatType> > : EigenFromPy<MatType> struct EigenFromPy< Eigen::PlainObjectBase<MatType>, _Scalar > : EigenFromPy<MatType>
{ {
typedef EigenFromPy<MatType> EigenFromPyDerived; typedef EigenFromPy<MatType> EigenFromPyDerived;
typedef Eigen::PlainObjectBase<MatType> Base; typedef Eigen::PlainObjectBase<MatType> Base;
...@@ -493,20 +495,21 @@ namespace eigenpy ...@@ -493,20 +495,21 @@ namespace eigenpy
#if EIGEN_VERSION_AT_LEAST(3,2,0) #if EIGEN_VERSION_AT_LEAST(3,2,0)
template<typename MatType, int Options, typename Stride> template<typename MatType, int Options, typename Stride, typename _Scalar>
struct EigenFromPy<Eigen::Ref<MatType,Options,Stride> > struct EigenFromPy<Eigen::Ref<MatType,Options,Stride>,_Scalar>
{ {
typedef Eigen::Ref<MatType,Options,Stride> RefType; typedef Eigen::Ref<MatType,Options,Stride> RefType;
typedef typename MatType::Scalar Scalar; typedef typename MatType::Scalar Scalar;
/// \brief Determine if pyObj can be converted into a MatType object /// \brief Determine if pyObj can be converted into a MatType object
static void* convertible(PyArrayObject * pyArray) static void* convertible(PyObject * pyObj)
{ {
if(!call_PyArray_Check(reinterpret_cast<PyObject*>(pyArray))) if(!call_PyArray_Check(pyObj))
return 0; return 0;
PyArrayObject * pyArray = reinterpret_cast<PyArrayObject*>(pyObj);
if(!PyArray_ISWRITEABLE(pyArray)) if(!PyArray_ISWRITEABLE(pyArray))
return 0; return 0;
return EigenFromPy<MatType>::convertible(pyArray); return EigenFromPy<MatType>::convertible(pyObj);
} }
static void registration() static void registration()
...@@ -517,16 +520,16 @@ namespace eigenpy ...@@ -517,16 +520,16 @@ namespace eigenpy
} }
}; };
template<typename MatType, int Options, typename Stride> template<typename MatType, int Options, typename Stride, typename _Scalar>
struct EigenFromPy<const Eigen::Ref<const MatType,Options,Stride> > struct EigenFromPy<const Eigen::Ref<const MatType,Options,Stride>,_Scalar>
{ {
typedef const Eigen::Ref<const MatType,Options,Stride> ConstRefType; typedef const Eigen::Ref<const MatType,Options,Stride> ConstRefType;
typedef typename MatType::Scalar Scalar; typedef typename MatType::Scalar Scalar;
/// \brief Determine if pyObj can be converted into a MatType object /// \brief Determine if pyObj can be converted into a MatType object
static void* convertible(PyArrayObject * pyArray) static void* convertible(PyObject * pyObj)
{ {
return EigenFromPy<MatType>::convertible(pyArray); return EigenFromPy<MatType>::convertible(pyObj);
} }
static void registration() static void registration()
......
...@@ -28,8 +28,8 @@ ...@@ -28,8 +28,8 @@
namespace eigenpy namespace eigenpy
{ {
template<typename MatType> struct EigenFromPy;
template<typename MatType, typename Scalar = typename boost::remove_reference<MatType>::type::Scalar> struct EigenToPy; template<typename MatType, typename Scalar = typename boost::remove_reference<MatType>::type::Scalar> struct EigenToPy;
template<typename MatType, typename Scalar = typename boost::remove_reference<MatType>::type::Scalar> struct EigenFromPy;
} }
#endif // ifndef __eigenpy_fwd_hpp__ #endif // ifndef __eigenpy_fwd_hpp__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment