Commit a151a4c2 authored by Wilson Jallet's avatar Wilson Jallet 🎬
Browse files

test/eigen_ref: test using Eigen::Ref as data member

* show correct return_value_policy to use
parent 9958e358
Pipeline #18509 passed with stage
in 1 minute and 58 seconds
...@@ -86,6 +86,13 @@ struct modify_wrap : modify_block, bp::wrapper<modify_block> { ...@@ -86,6 +86,13 @@ struct modify_wrap : modify_block, bp::wrapper<modify_block> {
void call(Eigen::Ref<MatrixXd> mat) { this->get_override("call")(mat); } void call(Eigen::Ref<MatrixXd> mat) { this->get_override("call")(mat); }
}; };
struct has_ref_member
{
MatrixXd J;
Eigen::Ref<MatrixXd> Jref;
has_ref_member() : J(4, 4), Jref(J.topRightCorner(3, 3)) { J.setZero(); }
};
BOOST_PYTHON_MODULE(eigen_ref) { BOOST_PYTHON_MODULE(eigen_ref) {
namespace bp = boost::python; namespace bp = boost::python;
eigenpy::enableEigenPy(); eigenpy::enableEigenPy();
...@@ -119,4 +126,11 @@ BOOST_PYTHON_MODULE(eigen_ref) { ...@@ -119,4 +126,11 @@ BOOST_PYTHON_MODULE(eigen_ref) {
.def_readonly("J", &modify_block::J) .def_readonly("J", &modify_block::J)
.def("modify", &modify_block::modify) .def("modify", &modify_block::modify)
.def("call", bp::pure_virtual(&modify_wrap::call)); .def("call", bp::pure_virtual(&modify_wrap::call));
bp::class_<has_ref_member, boost::noncopyable>("has_ref_member", bp::init<>())
.def_readonly("J", &has_ref_member::J)
.add_property("Jref", bp::make_getter(&has_ref_member::Jref,
bp::return_value_policy<bp::return_by_value>()));
// can't return Eigen::Ref by reference but by value
// (def_readonly creates a by-reference getter)
} }
...@@ -62,6 +62,14 @@ def test(mat): ...@@ -62,6 +62,14 @@ def test(mat):
assert np.array_equal(Jref, modify.J) assert np.array_equal(Jref, modify.J)
hasref = has_ref_member()
A = np.ones((3, 3)) / 2
hasref.Jref[:, :] = A
J_true = np.zeros((4, 4))
J_true[:3, 1:] = A
assert np.array_equal(hasref.J, J_true)
rows = 10 rows = 10
cols = 30 cols = 30
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment