diff --git a/unittest/python/test_std_vector.py b/unittest/python/test_std_vector.py index ddc900f874f3a6a57db86bf6061a260d8b72b335..13c0edc8e6cd680f1c6d3220dc456261e4ad9042 100644 --- a/unittest/python/test_std_vector.py +++ b/unittest/python/test_std_vector.py @@ -8,7 +8,7 @@ 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)] +l3 = [np.random.randn(2, 2), np.random.randn(3, 1), np.random.randn(4, 2)] def checkAllValues(li1, li2): @@ -33,11 +33,31 @@ print() l4 = [np.random.randn(3, 3) for _ in range(4)] assert "StdVec_Mat3d" in printVectorOf3x3.__doc__ printVectorOf3x3(l4) +print() 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) + + +def checkZero(l): + for x in l: + assert np.allclose(x, 0.), "x = {}".format(x) + + +print("l1:") +vector.setZero(l1) +print(l1) +checkZero(l1) + +print("l2:") +l2_py = l2.tolist() +vector.setZero(l2_py) +print(l2_py) +checkZero(l2_py) + +print("l3:") +vector.setZero(l3) +checkZero(l3) diff --git a/unittest/vector.cpp b/unittest/vector.cpp index 6941b3843a1b201459e4ef577b6b4f42b94a3bbf..97201c682509081cbaf298a45c538cfad95c740a 100644 --- a/unittest/vector.cpp +++ b/unittest/vector.cpp @@ -18,10 +18,19 @@ std::vector<MatType> copy(const std::vector<MatType> &Ms) { return out; } +template<typename MatType> +void setZero(std::vector<MatType> Ms) { + for (std::size_t i = 0; i < Ms.size(); i++) { + Ms[i].setZero(); + } +} + + BOOST_PYTHON_MODULE(vector) { namespace bp = boost::python; + using namespace eigenpy; - eigenpy::enableEigenPy(); + enableEigenPy(); bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::VectorXd>); bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::MatrixXd>); @@ -29,8 +38,12 @@ BOOST_PYTHON_MODULE(vector) { bp::def("copyStdVector", copy<Eigen::MatrixXd>); bp::def("copyStdVector", copy<Eigen::VectorXd>); - eigenpy::StdVectorPythonVisitor<std::vector<Eigen::Matrix3d>>::expose( + 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")); + + typedef Eigen::Ref<Eigen::MatrixXd> MatRef; + StdVectorPythonVisitor<std::vector<MatRef>, true>::expose("StdVec_MatRef"); + bp::def("setZero", setZero<MatRef>, "Sets the coeff in [0,0] to 0."); }