From 34b8b4d81f8341493f08cb48055ebd76b4c7d9bf Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 26 Nov 2024 11:12:37 +0000 Subject: [PATCH] PicoVector: MPY bindings for line and arc. --- micropython/modules/picovector/picovector.c | 4 ++ micropython/modules/picovector/picovector.cpp | 66 +++++++++++++++++++ micropython/modules/picovector/picovector.h | 2 + 3 files changed, 72 insertions(+) diff --git a/micropython/modules/picovector/picovector.c b/micropython/modules/picovector/picovector.c index 5e025c657..1caaeeb11 100644 --- a/micropython/modules/picovector/picovector.c +++ b/micropython/modules/picovector/picovector.c @@ -19,6 +19,8 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR(POLYGON_path_obj, 4, POLYGON_path); static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_regular_obj, 5, POLYGON_regular); static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_circle_obj, 4, POLYGON_circle); static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_arc_obj, 6, POLYGON_arc); +static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_star_obj, 6, POLYGON_star); +static MP_DEFINE_CONST_FUN_OBJ_KW(POLYGON_line_obj, 5, POLYGON_line); static const mp_rom_map_elem_t POLYGON_locals_dict_table[] = { @@ -39,6 +41,8 @@ static const mp_rom_map_elem_t POLYGON_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_path), MP_ROM_PTR(&POLYGON_path_obj) }, { MP_ROM_QSTR(MP_QSTR_circle), MP_ROM_PTR(&POLYGON_circle_obj) }, { MP_ROM_QSTR(MP_QSTR_arc), MP_ROM_PTR(&POLYGON_arc_obj) }, + { MP_ROM_QSTR(MP_QSTR_star), MP_ROM_PTR(&POLYGON_star_obj) }, + { MP_ROM_QSTR(MP_QSTR_line), MP_ROM_PTR(&POLYGON_line_obj) }, }; static MP_DEFINE_CONST_DICT(POLYGON_locals_dict, POLYGON_locals_dict_table); diff --git a/micropython/modules/picovector/picovector.cpp b/micropython/modules/picovector/picovector.cpp index 4f637f700..6709c3d3f 100644 --- a/micropython/modules/picovector/picovector.cpp +++ b/micropython/modules/picovector/picovector.cpp @@ -351,6 +351,72 @@ mp_obj_t POLYGON_arc(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) return self; } +mp_obj_t POLYGON_star(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_self, ARG_x, ARG_y, ARG_points, ARG_inner_radius, ARG_outer_radius, ARG_stroke }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_inner_radius, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_outer_radius, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_stroke, MP_ARG_OBJ, { .u_obj = mp_const_none }}, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + _POLY_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _POLY_obj_t); + + picovector_point_type x = mp_picovector_get_point_type(args[ARG_x].u_obj); + picovector_point_type y = mp_picovector_get_point_type(args[ARG_y].u_obj); + int p = args[ARG_points].u_int; + picovector_point_type r1 = mp_picovector_get_point_type(args[ARG_inner_radius].u_obj); + picovector_point_type r2 = mp_picovector_get_point_type(args[ARG_outer_radius].u_obj); + picovector_point_type s = args[ARG_stroke].u_obj == mp_const_none ? 0 : mp_picovector_get_point_type(args[ARG_stroke].u_obj); + + pp_poly_merge(self->poly, ppp_star({ + x, y, + p, + r1, + r2, + s + })); + + return self; +} + +mp_obj_t POLYGON_line(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_self, ARG_x, ARG_y, ARG_x2, ARG_y2, ARG_thickness }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_x, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_y, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_x2, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_y2, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_thickness, MP_ARG_OBJ, { .u_obj = mp_const_none }}, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + _POLY_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _POLY_obj_t); + + picovector_point_type x = mp_picovector_get_point_type(args[ARG_x].u_obj); + picovector_point_type y = mp_picovector_get_point_type(args[ARG_y].u_obj); + picovector_point_type x2 = mp_picovector_get_point_type(args[ARG_x2].u_obj); + picovector_point_type y2 = mp_picovector_get_point_type(args[ARG_y2].u_obj); + picovector_point_type t = args[ARG_thickness].u_obj == mp_const_none ? 0 : mp_picovector_get_point_type(args[ARG_thickness].u_obj); + + pp_poly_merge(self->poly, ppp_line({ + x, y, + x2, y2, + t + })); + + return self; +} + // Utility functions mp_obj_t POLYGON_centroid(mp_obj_t self_in) { diff --git a/micropython/modules/picovector/picovector.h b/micropython/modules/picovector/picovector.h index cff667612..71ffa7d35 100644 --- a/micropython/modules/picovector/picovector.h +++ b/micropython/modules/picovector/picovector.h @@ -13,6 +13,8 @@ extern mp_obj_t POLYGON_regular(size_t n_args, const mp_obj_t *pos_args, mp_map_ extern mp_obj_t POLYGON_rectangle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t POLYGON_circle(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t POLYGON_arc(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +extern mp_obj_t POLYGON_star(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +extern mp_obj_t POLYGON_line(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern void POLYGON_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); extern mp_obj_t POLYGON_centroid(mp_obj_t self_in);