From 63b0c1fb9d8bb4a8b43853e174d8de72891193fc Mon Sep 17 00:00:00 2001 From: Noah Johnson Date: Mon, 22 Jul 2019 10:15:09 -0400 Subject: [PATCH] Added pickling support for vgl_box_2d and vgl_box_3d, needed so that ProcessPoolExecutor can pass these objects around --- vgl/pyvgl.cxx | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/vgl/pyvgl.cxx b/vgl/pyvgl.cxx index f8425b2..4a51bc6 100644 --- a/vgl/pyvgl.cxx +++ b/vgl/pyvgl.cxx @@ -422,6 +422,22 @@ void wrap_vgl(py::module &m) .def(py::init(), py::arg("min_x"),py::arg("max_x"),py::arg("min_y"),py::arg("max_y")) .def("__repr__", streamToString >) + .def(py::pickle( + [](const vgl_box_2d &p) { // __getstate__ + /* Return a tuple, which is pickleable */ + return py::make_tuple(p.min_x(), p.max_x(), + p.min_y(), p.max_y()); + }, + [](py::tuple t) { // __setstate__ + if (t.size() != 4) + throw std::runtime_error("Can't unpickle vgl_box_2d: Needs 4 elements!"); + + /* Create a new C++ instance */ + vgl_box_2d box(t[0].cast(), t[1].cast(), + t[2].cast(), t[3].cast()); + + return box; + })) .def_property("min_x", &vgl_box_2d::min_x, &vgl_box_2d::set_min_x) .def_property("min_y", &vgl_box_2d::min_y, &vgl_box_2d::set_min_y) @@ -468,6 +484,22 @@ void wrap_vgl(py::module &m) py::arg("min_x"),py::arg("min_y"),py::arg("min_z"), py::arg("max_x"),py::arg("max_y"),py::arg("max_z")) .def("__repr__", streamToString >) + .def(py::pickle( + [](const vgl_box_3d &p) { // __getstate__ + /* Return a tuple, which is pickleable */ + return py::make_tuple(p.min_x(), p.min_y(), p.min_z(), + p.max_x(), p.max_y(), p.max_z()); + }, + [](py::tuple t) { // __setstate__ + if (t.size() != 6) + throw std::runtime_error("Can't unpickle vgl_box_3d: Needs 6 elements!"); + + /* Create a new C++ instance */ + vgl_box_3d box(t[0].cast(), t[1].cast(), t[2].cast(), + t[3].cast(), t[4].cast(), t[5].cast() ); + + return box; + })) .def_property("min_x", &vgl_box_3d::min_x, &vgl_box_3d::set_min_x) .def_property("min_y", &vgl_box_3d::min_y, &vgl_box_3d::set_min_y)