Skip to content

Commit

Permalink
PicoVector: Add arbitrary matrix transform.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Dec 11, 2024
1 parent a75aea8 commit 12ed706
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions micropython/modules/picovector/picovector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
};

Expand Down
25 changes: 25 additions & 0 deletions micropython/modules/picovector/picovector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions micropython/modules/picovector/picovector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 12ed706

Please sign in to comment.