From 2b4077c37c3358ba633155b4c3d465c50cf37c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Bergstr=C3=B6m?= <davbe125@student.liu.se> Date: Wed, 27 Jun 2018 10:19:41 +0200 Subject: [PATCH] Add 2 classes for points --- python-api-src/lib_point.cpp | 42 ++++++++++++++++++++++++++++++++++++ python-api-src/library.cpp | 1 + python-api-src/library.h | 3 ++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 python-api-src/lib_point.cpp diff --git a/python-api-src/lib_point.cpp b/python-api-src/lib_point.cpp new file mode 100644 index 0000000..66cb4b5 --- /dev/null +++ b/python-api-src/lib_point.cpp @@ -0,0 +1,42 @@ +#include "library.h" + +namespace py = pybind11; + +void define_point(py::module & m) +{ + // Note that Point2D has dynamic attributes, allowing students to add additional methods to it + + py::class_<sc2::Point2D>(m, "Point2D", py::dynamic_attr()) + .def(py::init<float, float>()) + .def(py::init()) + .def_readwrite("x", &sc2::Point2D::x) + .def_readwrite("y", &sc2::Point2D::y) + .def(py::self += py::self) + .def(py::self -= py::self) + .def(py::self *= float()) + .def(py::self /= float()) + // TODO: These does not compile + //.def(py::self == py::self) + //.def(py::self != py::self) + .def(py::self + py::self) + .def(py::self - py::self) + .def(py::self * float()) + .def(float() * py::self) + .def(py::self / float()) + .def(float() / py::self) + .def("__repr__", + [](const sc2::Point2D & p) { + return "<Point2D: (" + std::to_string(p.x) + ", " + std::to_string(p.y) + ")>"; + }) + .def("__str__", + [](const sc2::Point2D & p) { + return "(" + std::to_string(p.x) + ", " + std::to_string(p.y) + ")"; + }); + + py::class_<sc2::Point2DI>(m, "Point2DI") + .def(py::init<int, int>()) + .def_readwrite("x", &sc2::Point2DI::x) + .def_readwrite("y", &sc2::Point2DI::y) + .def(py::self == py::self) + .def(py::self != py::self); +} \ No newline at end of file diff --git a/python-api-src/library.cpp b/python-api-src/library.cpp index 08add75..50c2a4e 100644 --- a/python-api-src/library.cpp +++ b/python-api-src/library.cpp @@ -10,6 +10,7 @@ PYBIND11_MODULE(library, m) define_unit(m); define_unittype(m); define_util(m); + define_point(m); py::class_<Coordinator>(m, "Coordinator") .def(py::init()) diff --git a/python-api-src/library.h b/python-api-src/library.h index 14223b3..b1da21a 100644 --- a/python-api-src/library.h +++ b/python-api-src/library.h @@ -53,4 +53,5 @@ public: void define_typeenums(pybind11::module & m); void define_unit(pybind11::module & m); void define_unittype(pybind11::module &m); -void define_util(pybind11::module &m); \ No newline at end of file +void define_util(pybind11::module &m); +void define_point(pybind11::module &m); \ No newline at end of file -- GitLab