diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 55d6b569944d46878c5c08da0a81a0c00b89f6bf..507f1456368a7bfa108e54a3fdddcac7e0054561 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -33,6 +33,7 @@ if(NOT ${EIGEN3_VERSION} VERSION_LESS "3.2.0") add_lib_unit_test(eigen_ref) endif() add_lib_unit_test(user_type) +add_lib_unit_test(vector) add_python_unit_test("py-matrix" "unittest/python/test_matrix.py" "unittest") add_python_unit_test("py-geometry" "unittest/python/test_geometry.py" @@ -78,3 +79,7 @@ if(NOT WIN32) "python;unittest") set_tests_properties("py-MINRES" PROPERTIES DEPENDS ${PYWRAP}) endif(NOT WIN32) + +add_python_unit_test("py-std-vector" "unittest/python/test_std_vector.py" + "python;unittest") +set_tests_properties("py-std-vector" PROPERTIES DEPENDS ${PYWRAP}) diff --git a/unittest/python/test_std_vector.py b/unittest/python/test_std_vector.py index 3d679cd6dac38c2ca9806e9a035fa9c0e5fe0880..ddc900f874f3a6a57db86bf6061a260d8b72b335 100644 --- a/unittest/python/test_std_vector.py +++ b/unittest/python/test_std_vector.py @@ -1,9 +1,14 @@ import numpy as np import eigenpy +import inspect +import vector +from vector import printVectorOfMatrix, printVectorOf3x3, copyStdVector -x0 = np.random.randn(3) -l1 = [x0, x0, x0] -l2 = eigenpy.StdVec_VectorXd(3, x0) +np.random.seed(0) + +l1 = [np.random.randn(3), np.random.randn(2)] +l2 = eigenpy.StdVec_VectorXd(l1) +l3 = [np.random.randn(2, 2), np.random.randn(1, 2), np.random.randn(2, 6)] def checkAllValues(li1, li2): @@ -14,3 +19,25 @@ def checkAllValues(li1, li2): checkAllValues(l1, l2) +checkAllValues(l1, copyStdVector(l1)) + + +printVectorOfMatrix(l1) +print() +printVectorOfMatrix(l2) +print() +printVectorOfMatrix(l3) +print() + + +l4 = [np.random.randn(3, 3) for _ in range(4)] +assert "StdVec_Mat3d" in printVectorOf3x3.__doc__ +printVectorOf3x3(l4) + +l4_copy = copyStdVector(l4) +assert isinstance(l4_copy, eigenpy.StdVec_MatrixXd) +print(l4_copy) + +l4_copy2 = vector.copyStdVec_3x3(l4) +assert isinstance(l4_copy2, vector.StdVec_Mat3d) +print(l4_copy2) diff --git a/unittest/vector.cpp b/unittest/vector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f18fa5f20c752ab5efa2f808097a59408379fbf7 --- /dev/null +++ b/unittest/vector.cpp @@ -0,0 +1,38 @@ +#include "eigenpy/eigenpy.hpp" +#include "eigenpy/std-vector.hpp" + + +#include <ostream> +#include <type_traits> + +template<typename MatType> +void printVectorOfMatrix(const std::vector<MatType> &Ms) { + const std::size_t n = Ms.size(); + for (std::size_t i = 0; i < n; i++) { + std::cout << "el[" << i << "] =\n" << Ms[i] << '\n'; + } +} + +template<typename MatType> +std::vector<MatType> copy(const std::vector<MatType> &Ms) { + std::vector<MatType> out = Ms; + return out; +} + + +BOOST_PYTHON_MODULE(vector) { + namespace bp = boost::python; + + eigenpy::enableEigenPy(); + + bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::VectorXd>); + bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::MatrixXd>); + + bp::def("copyStdVector", copy<Eigen::MatrixXd>); + bp::def("copyStdVector", copy<Eigen::VectorXd>); + + eigenpy::StdVectorPythonVisitor<std::vector<Eigen::Matrix3d>>::expose("StdVec_Mat3d", "3D matrices."); + bp::def("printVectorOf3x3", printVectorOfMatrix<Eigen::Matrix3d>); + bp::def("copyStdVec_3x3", copy<Eigen::Matrix3d>, bp::args("mats")); + +}