Verified Commit 5e7ae5b1 authored by Justin Carpentier's avatar Justin Carpentier
Browse files

core: add helper np_type_is_convertible_into_scalar

parent 4f6f7044
......@@ -86,34 +86,7 @@ namespace eigenpy
template<typename MatType>
struct EigenFromPy
{
static bool isScalarConvertible(const int np_type)
{
if(NumpyEquivalentType<typename MatType::Scalar>::type_code == np_type)
return true;
switch(np_type)
{
case NPY_INT:
return FromTypeToType<int,typename MatType::Scalar>::value;
case NPY_LONG:
return FromTypeToType<long,typename MatType::Scalar>::value;
case NPY_FLOAT:
return FromTypeToType<float,typename MatType::Scalar>::value;
case NPY_CFLOAT:
return FromTypeToType<std::complex<float>,typename MatType::Scalar>::value;
case NPY_DOUBLE:
return FromTypeToType<double,typename MatType::Scalar>::value;
case NPY_CDOUBLE:
return FromTypeToType<std::complex<double>,typename MatType::Scalar>::value;
case NPY_LONGDOUBLE:
return FromTypeToType<long double,typename MatType::Scalar>::value;
case NPY_CLONGDOUBLE:
return FromTypeToType<std::complex<long double>,typename MatType::Scalar>::value;
default:
return false;
}
}
typedef typename MatType::Scalar Scalar;
/// \brief Determine if pyObj can be converted into a MatType object
static void* convertible(PyArrayObject* pyArray)
......@@ -121,7 +94,7 @@ namespace eigenpy
if(!PyArray_Check(pyArray))
return 0;
if(!isScalarConvertible(EIGENPY_GET_PY_ARRAY_TYPE(pyArray)))
if(!np_type_is_convertible_into_scalar<Scalar>(EIGENPY_GET_PY_ARRAY_TYPE(pyArray)))
return 0;
if(MatType::IsVectorAtCompileTime)
......
......@@ -6,6 +6,7 @@
#define __eigenpy_numpy_type_hpp__
#include "eigenpy/fwd.hpp"
#include "eigenpy/scalar-conversion.hpp"
#include <patchlevel.h> // For PY_MAJOR_VERSION
......@@ -23,6 +24,35 @@ namespace eigenpy
template <> struct NumpyEquivalentType< std::complex<long double> > { enum { type_code = NPY_CLONGDOUBLE };};
template <> struct NumpyEquivalentType<int> { enum { type_code = NPY_INT };};
template <> struct NumpyEquivalentType<long> { enum { type_code = NPY_LONG };};
template<typename Scalar>
bool np_type_is_convertible_into_scalar(const int np_type)
{
if(NumpyEquivalentType<Scalar>::type_code == np_type)
return true;
switch(np_type)
{
case NPY_INT:
return FromTypeToType<int,Scalar>::value;
case NPY_LONG:
return FromTypeToType<long,Scalar>::value;
case NPY_FLOAT:
return FromTypeToType<float,Scalar>::value;
case NPY_CFLOAT:
return FromTypeToType<std::complex<float>,Scalar>::value;
case NPY_DOUBLE:
return FromTypeToType<double,Scalar>::value;
case NPY_CDOUBLE:
return FromTypeToType<std::complex<double>,Scalar>::value;
case NPY_LONGDOUBLE:
return FromTypeToType<long double,Scalar>::value;
case NPY_CLONGDOUBLE:
return FromTypeToType<std::complex<long double>,Scalar>::value;
default:
return false;
}
}
enum NP_TYPE
{
......
Supports Markdown
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