From 12ed7069615684b4dc8f8b9331c4a44aba52daa4 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 11 Dec 2024 11:24:42 +0000 Subject: [PATCH] PicoVector: Add arbitrary matrix transform. --- micropython/modules/picovector/picovector.c | 2 ++ micropython/modules/picovector/picovector.cpp | 25 +++++++++++++++++++ micropython/modules/picovector/picovector.h | 1 + 3 files changed, 28 insertions(+) diff --git a/micropython/modules/picovector/picovector.c b/micropython/modules/picovector/picovector.c index 1caaeeb11..7c6b1e482 100644 --- a/micropython/modules/picovector/picovector.c +++ b/micropython/modules/picovector/picovector.c @@ -62,12 +62,14 @@ MP_DEFINE_CONST_OBJ_TYPE( static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_rotate_obj, TRANSFORM_rotate); static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_translate_obj, TRANSFORM_translate); static MP_DEFINE_CONST_FUN_OBJ_3(TRANSFORM_scale_obj, TRANSFORM_scale); +static MP_DEFINE_CONST_FUN_OBJ_2(TRANSFORM_custom_obj, TRANSFORM_custom); static MP_DEFINE_CONST_FUN_OBJ_1(TRANSFORM_reset_obj, TRANSFORM_reset); static const mp_rom_map_elem_t TRANSFORM_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_rotate), MP_ROM_PTR(&TRANSFORM_rotate_obj) }, { MP_ROM_QSTR(MP_QSTR_translate), MP_ROM_PTR(&TRANSFORM_translate_obj) }, { MP_ROM_QSTR(MP_QSTR_scale), MP_ROM_PTR(&TRANSFORM_scale_obj) }, + { MP_ROM_QSTR(MP_QSTR_matrix), MP_ROM_PTR(&TRANSFORM_custom_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&TRANSFORM_reset_obj) }, }; diff --git a/micropython/modules/picovector/picovector.cpp b/micropython/modules/picovector/picovector.cpp index a920f6ad5..c6f603b41 100644 --- a/micropython/modules/picovector/picovector.cpp +++ b/micropython/modules/picovector/picovector.cpp @@ -544,6 +544,31 @@ mp_obj_t TRANSFORM_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_k return self; } +mp_obj_t TRANSFORM_custom(mp_obj_t self_in, mp_obj_t custom_in) { + _TRANSFORM_obj_t *transform = MP_OBJ_TO_PTR2(self_in, _TRANSFORM_obj_t); + + if(!mp_obj_is_type(custom_in, &mp_type_list)) mp_raise_ValueError("custom: transform must be a list!"); + mp_obj_list_t *list = MP_OBJ_TO_PTR2(custom_in, mp_obj_list_t); + + if(list->len != 9) mp_raise_ValueError("custom: expected 9 items!"); + + pp_mat3_t t = pp_mat3_identity(); + + t.v00 = mp_obj_get_float(list->items[0]); + t.v10 = mp_obj_get_float(list->items[1]); + t.v20 = mp_obj_get_float(list->items[2]); + t.v01 = mp_obj_get_float(list->items[3]); + t.v11 = mp_obj_get_float(list->items[4]); + t.v21 = mp_obj_get_float(list->items[5]); + t.v02 = mp_obj_get_float(list->items[6]); + t.v12 = mp_obj_get_float(list->items[7]); + t.v22 = mp_obj_get_float(list->items[8]); + + pp_mat3_mul(&transform->transform, &t); + + return mp_const_none; +} + mp_obj_t TRANSFORM_rotate(mp_obj_t self_in, mp_obj_t angle_in, mp_obj_t origin_in) { _TRANSFORM_obj_t *transform = MP_OBJ_TO_PTR2(self_in, _TRANSFORM_obj_t); diff --git a/micropython/modules/picovector/picovector.h b/micropython/modules/picovector/picovector.h index 71ffa7d35..f02c44dbc 100644 --- a/micropython/modules/picovector/picovector.h +++ b/micropython/modules/picovector/picovector.h @@ -29,6 +29,7 @@ extern mp_obj_t TRANSFORM_make_new(const mp_obj_type_t *type, size_t n_args, siz extern mp_obj_t TRANSFORM_rotate(mp_obj_t self_in, mp_obj_t angle_in, mp_obj_t origin_in); extern mp_obj_t TRANSFORM_translate(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in); extern mp_obj_t TRANSFORM_scale(mp_obj_t self_in, mp_obj_t x_in, mp_obj_t y_in); +extern mp_obj_t TRANSFORM_custom(mp_obj_t self_in, mp_obj_t custom_in); extern mp_obj_t TRANSFORM_reset(mp_obj_t self_in); /* Vector */