@@ -372,119 +372,87 @@ namespace eigenpy
372372 {
373373 if (!PyArray_Check (pyArray))
374374 return 0 ;
375+
376+ if (!isScalarConvertible (GET_PY_ARRAY_TYPE (pyArray)))
377+ return 0 ;
375378
376379 if (MatType::IsVectorAtCompileTime)
377380 {
378- // Special care of scalar matrix of dimension 1x1.
379- if (PyArray_DIMS (pyArray)[0 ] == 1 && PyArray_DIMS (pyArray)[1 ] == 1 )
380- return pyArray;
381-
382- if (PyArray_DIMS (pyArray)[0 ] > 1 && PyArray_DIMS (pyArray)[1 ] > 1 )
381+ switch (PyArray_NDIM (pyArray))
383382 {
383+ case 0 :
384+ return 0 ;
385+ case 1 :
386+ return pyArray;
387+ case 2 :
388+ {
389+ // Special care of scalar matrix of dimension 1x1.
390+ if (PyArray_DIMS (pyArray)[0 ] == 1 && PyArray_DIMS (pyArray)[1 ] == 1 )
391+ return pyArray;
392+
393+ if (PyArray_DIMS (pyArray)[0 ] > 1 && PyArray_DIMS (pyArray)[1 ] > 1 )
394+ {
384395#ifndef NDEBUG
385- std::cerr << " The number of dimension of the object does not correspond to a vector" << std::endl;
396+ std::cerr << " The number of dimension of the object does not correspond to a vector" << std::endl;
386397#endif
387- return 0 ;
388- }
389-
390- if (((PyArray_DIMS (pyArray)[0 ] == 1 ) && (MatType::ColsAtCompileTime == 1 ))
391- || ((PyArray_DIMS (pyArray)[1 ] == 1 ) && (MatType::RowsAtCompileTime == 1 )))
392- {
398+ return 0 ;
399+ }
400+
401+ if (((PyArray_DIMS (pyArray)[0 ] == 1 ) && (MatType::ColsAtCompileTime == 1 ))
402+ || ((PyArray_DIMS (pyArray)[1 ] == 1 ) && (MatType::RowsAtCompileTime == 1 )))
403+ {
393404#ifndef NDEBUG
394- if (MatType::ColsAtCompileTime == 1 )
395- std::cerr << " The object is not a column vector" << std::endl;
396- else
397- std::cerr << " The object is not a row vector" << std::endl;
405+ if (MatType::ColsAtCompileTime == 1 )
406+ std::cerr << " The object is not a column vector" << std::endl;
407+ else
408+ std::cerr << " The object is not a row vector" << std::endl;
398409#endif
399- return 0 ;
410+ return 0 ;
411+ }
412+ break ;
413+ }
414+ default :
415+ return 0 ;
400416 }
401417 }
402-
403- if (PyArray_NDIM (pyArray) != 2 )
418+ else // this is a matrix
404419 {
405- if ( ( PyArray_NDIM (pyArray) !=1 ) || (! MatType::IsVectorAtCompileTime) )
420+ if ( PyArray_NDIM (pyArray) != 2 )
406421 {
422+ if ( (PyArray_NDIM (pyArray) !=1 ) || (! MatType::IsVectorAtCompileTime) )
423+ {
407424#ifndef NDEBUG
408- std::cerr << " The number of dimension of the object is not correct." << std::endl;
425+ std::cerr << " The number of dimension of the object is not correct." << std::endl;
409426#endif
410- return 0 ;
427+ return 0 ;
428+ }
411429 }
412- }
413-
414- if (PyArray_NDIM (pyArray) == 2 )
415- {
416- const int R = (int )PyArray_DIMS (pyArray)[0 ];
417- const int C = (int )PyArray_DIMS (pyArray)[1 ];
418430
419- if ( (MatType::RowsAtCompileTime!=R)
420- && (MatType::RowsAtCompileTime!=Eigen::Dynamic) )
421- return 0 ;
422- if ( (MatType::ColsAtCompileTime!=C)
423- && (MatType::ColsAtCompileTime!=Eigen::Dynamic) )
424- return 0 ;
425- }
426-
427- // Check if the Scalar type of the obj_ptr is compatible with the Scalar type of MatType
428- if (GET_PY_ARRAY_TYPE (pyArray) == NPY_INT)
429- {
430- if (!FromTypeToType<int ,typename MatType::Scalar>::value)
431+ if (PyArray_NDIM (pyArray) == 2 )
431432 {
432- #ifndef NDEBUG
433- std::cerr << " The Python matrix scalar type (int) cannot be converted into the scalar type of the Eigen matrix. Loss of arithmetic precision" << std::endl;
434- #endif
435- return 0 ;
433+ const int R = (int )PyArray_DIMS (pyArray)[0 ];
434+ const int C = (int )PyArray_DIMS (pyArray)[1 ];
435+
436+ if ( (MatType::RowsAtCompileTime!=R)
437+ && (MatType::RowsAtCompileTime!=Eigen::Dynamic) )
438+ return 0 ;
439+ if ( (MatType::ColsAtCompileTime!=C)
440+ && (MatType::ColsAtCompileTime!=Eigen::Dynamic) )
441+ return 0 ;
436442 }
437443 }
438- else if (GET_PY_ARRAY_TYPE (pyArray) == NPY_LONG)
439- {
440- if (!FromTypeToType<long ,typename MatType::Scalar>::value)
441- {
442- #ifndef NDEBUG
443- std::cerr << " The Python matrix scalar type (long) cannot be converted into the scalar type of the Eigen matrix. Loss of arithmetic precision" << std::endl;
444- #endif
445- return 0 ;
446- }
447- }
448- else if (GET_PY_ARRAY_TYPE (pyArray) == NPY_FLOAT)
449- {
450- if (!FromTypeToType<float ,typename MatType::Scalar>::value)
451- {
452- #ifndef NDEBUG
453- std::cerr << " The Python matrix scalar type (float) cannot be converted into the scalar type of the Eigen matrix. Loss of arithmetic precision" << std::endl;
454- #endif
455- return 0 ;
456- }
457- }
458- else if (GET_PY_ARRAY_TYPE (pyArray) == NPY_DOUBLE)
459- {
460- if (!FromTypeToType<double ,typename MatType::Scalar>::value)
461- {
462- #ifndef NDEBUG
463- std::cerr << " The Python matrix scalar (double) type cannot be converted into the scalar type of the Eigen matrix. Loss of arithmetic precision." << std::endl;
464- #endif
465- return 0 ;
466- }
467- }
468- else if (GET_PY_ARRAY_TYPE (pyArray) != NumpyEquivalentType<typename MatType::Scalar>::type_code)
469- {
470- #ifndef NDEBUG
471- std::cerr << " The internal type as no Eigen equivalent." << std::endl;
472- #endif
473444
474- return 0 ;
475- }
476-
477445#ifdef NPY_1_8_API_VERSION
478446 if (!(PyArray_FLAGS (pyArray)))
479447#else
480448 if (!(PyArray_FLAGS (pyArray) & NPY_ALIGNED))
481449#endif
482- {
450+ {
483451#ifndef NDEBUG
484- std::cerr << " NPY non-aligned matrices are not implemented." << std::endl;
452+ std::cerr << " NPY non-aligned matrices are not implemented." << std::endl;
485453#endif
486- return 0 ;
487- }
454+ return 0 ;
455+ }
488456
489457 return pyArray;
490458 }
0 commit comments