Skip to content
Snippets Groups Projects
Commit cdbe8e7b authored by Wilson Jallet's avatar Wilson Jallet :clapper:
Browse files

Add unit tests for exposing std::vector

* tests we can expose std::vector<Matrix3d>
* test input and returning std::vector<Matrix> objects
* test specific functions for specific dims
parent 87cdcf99
No related merge requests found
......@@ -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})
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)
#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"));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment