diff --git a/tools/python/src/opaque_types.h b/tools/python/src/opaque_types.h index 1a31c08df..16c14f738 100644 --- a/tools/python/src/opaque_types.h +++ b/tools/python/src/opaque_types.h @@ -50,6 +50,7 @@ PYBIND11_MAKE_OPAQUE(sparse_ranking_pairs); PYBIND11_MAKE_OPAQUE(std::vector); +PYBIND11_MAKE_OPAQUE(std::vector); #endif // DLIB_PyTHON_OPAQUE_TYPES_H_ diff --git a/tools/python/src/vector.cpp b/tools/python/src/vector.cpp index a9f81c65e..edd1b2477 100644 --- a/tools/python/src/vector.cpp +++ b/tools/python/src/vector.cpp @@ -138,8 +138,24 @@ string point__str__(const point& p) return sout.str(); } +string dpoint__repr__ (const dpoint& p) +{ + std::ostringstream sout; + sout << "dpoint(" << p.x() << ", " << p.y() << ")"; + return sout.str(); +} + +string dpoint__str__(const dpoint& p) +{ + std::ostringstream sout; + sout << "(" << p.x() << ", " << p.y() << ")"; + return sout.str(); +} + long point_x(const point& p) { return p.x(); } long point_y(const point& p) { return p.y(); } +double dpoint_x(const dpoint& p) { return p.x(); } +double dpoint_y(const dpoint& p) { return p.y(); } // ---------------------------------------------------------------------------------------- void bind_vector(py::module& m) @@ -179,4 +195,23 @@ void bind_vector(py::module& m) .def("extend", extend_vector_with_python_list) .def(py::pickle(&getstate, &setstate)); } + + { + typedef dpoint type; + py::class_(m, "dpoint", "This object represents a single point of floating point coordinates that maps directly to a dlib::dpoint.") + .def(py::init(), py::arg("x"), py::arg("y")) + .def("__repr__", &dpoint__repr__) + .def("__str__", &dpoint__str__) + .def_property("x", &dpoint_x, [](dpoint& p, double x){p.x()=x;}, "The x-coordinate of the dpoint.") + .def_property("y", &dpoint_y, [](dpoint& p, double y){p.x()=y;}, "The y-coordinate of the dpoint.") + .def(py::pickle(&getstate, &setstate)); + } + { + typedef std::vector type; + py::bind_vector(m, "dpoints", "An array of dpoint objects.") + .def("clear", &type::clear) + .def("resize", resize) + .def("extend", extend_vector_with_python_list) + .def(py::pickle(&getstate, &setstate)); + } }