From 291929c933cb33a4261e61c2f6d4052eb64d015b Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 28 Jun 2019 13:05:53 +0200 Subject: [PATCH 1/2] core: make np_type static thhrough a Singleton --- include/eigenpy/details.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/include/eigenpy/details.hpp b/include/eigenpy/details.hpp index e6dfc50a9..f0d32adee 100644 --- a/include/eigenpy/details.hpp +++ b/include/eigenpy/details.hpp @@ -89,18 +89,19 @@ namespace eigenpy static void switchToNumpyArray() { getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; - getInstance().np_type = ARRAY_TYPE; + getType() = ARRAY_TYPE; } static void switchToNumpyMatrix() { getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; - getInstance().np_type = MATRIX_TYPE; + getType() = MATRIX_TYPE; } - static NP_TYPE getType() + static NP_TYPE & getType() { - return getInstance().np_type; + static NP_TYPE np_type; + return np_type; } protected: @@ -121,6 +122,7 @@ namespace eigenpy //NumpyAsMatrixType = reinterpret_cast(NumpyAsMatrixObject.ptr()); CurrentNumpyType = NumpyMatrixObject; // default conversion + getType() = MATRIX_TYPE; } bp::object CurrentNumpyType; @@ -131,11 +133,8 @@ namespace eigenpy //bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType; bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType; - static NP_TYPE np_type; }; - NP_TYPE NumpyType::np_type = MATRIX_TYPE; - template struct EigenObjectAllocator { From 070d223d546f96aafab9411e36dd93204a95091b Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Fri, 28 Jun 2019 13:16:27 +0200 Subject: [PATCH 2/2] test: add test for the dimensions checking --- unittest/CMakeLists.txt | 1 + unittest/python/test_dimensions.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 unittest/python/test_dimensions.py diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 723247bf7..84ce752b8 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -36,3 +36,4 @@ ENDIF() ADD_PYTHON_UNIT_TEST("py-matrix" "unittest/python/test_matrix.py" "unittest") ADD_PYTHON_UNIT_TEST("py-geometry" "unittest/python/test_geometry.py" "unittest") ADD_PYTHON_UNIT_TEST("py-switch" "unittest/python/test_switch.py" "python/eigenpy") +ADD_PYTHON_UNIT_TEST("py-dimensions" "unittest/python/test_dimensions.py" "python/eigenpy") diff --git a/unittest/python/test_dimensions.py b/unittest/python/test_dimensions.py new file mode 100644 index 000000000..3ccc354b7 --- /dev/null +++ b/unittest/python/test_dimensions.py @@ -0,0 +1,14 @@ +from __future__ import print_function + +import eigenpy +import numpy as np + +quat = eigenpy.Quaternion() +# By default, we convert as numpy.matrix +coeffs_vector = quat.coeffs() +assert len(coeffs_vector.shape) == 2 + +# Switch to numpy.array +eigenpy.switchToNumpyArray() +coeffs_vector = quat.coeffs() +assert len(coeffs_vector.shape) == 1