From 24659ef88c0686f394e9fe3f429ef4369fc6f900 Mon Sep 17 00:00:00 2001 From: David Vo Date: Wed, 20 Mar 2024 20:15:47 +1100 Subject: [PATCH] Make AprilTagDetection.Point unpackable --- .../robotpy-apriltag/gen/AprilTagDetection.yml | 13 ++++++++++++- .../robotpy-apriltag/tests/test_detection.py | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/subprojects/robotpy-apriltag/gen/AprilTagDetection.yml b/subprojects/robotpy-apriltag/gen/AprilTagDetection.yml index 54959053..857c794a 100644 --- a/subprojects/robotpy-apriltag/gen/AprilTagDetection.yml +++ b/subprojects/robotpy-apriltag/gen/AprilTagDetection.yml @@ -29,6 +29,17 @@ classes: AprilTagDetection::Point pt{x, y}; return std::make_unique(std::move(pt)); }), py::arg("x"), py::arg("y")) + .def("__len__", [](const AprilTagDetection::Point &self) { return 2; }) + .def("__getitem__", [](const AprilTagDetection::Point &self, int index) { + switch (index) { + case 0: + return self.x; + case 1: + return self.y; + default: + throw std::out_of_range("AprilTagDetection.Point index out of range"); + } + }) .def("__repr__", [](const AprilTagDetection::Point &self) { - return py::str("Point(x={}, y={})").format(self.x, self.y); + return py::str("AprilTagDetection.Point(x={}, y={})").format(self.x, self.y); }) diff --git a/subprojects/robotpy-apriltag/tests/test_detection.py b/subprojects/robotpy-apriltag/tests/test_detection.py index 433f0ec4..c323120a 100644 --- a/subprojects/robotpy-apriltag/tests/test_detection.py +++ b/subprojects/robotpy-apriltag/tests/test_detection.py @@ -7,6 +7,15 @@ import pytest +def test_point(): + point = robotpy_apriltag.AprilTagDetection.Point() + + x, y = point + + assert x == 0 + assert y == 0 + + def _load_grayscale_image(fname): full_path = pathlib.Path(__file__).parent / fname img = cv2.imread(str(full_path))