Skip to content

Commit 5ebd67d

Browse files
committed
Add support for ulab.numpy float vectors in Vertex2f()
Using numpy greatly increases graphics drawing for complex geometry.
1 parent d8b9f64 commit 5ebd67d

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

shared-bindings/_eve/__init__.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "py/runtime.h"
1212
#include "py/binary.h"
1313

14+
#include "extmod/ulab/code/ulab.h"
15+
#include "extmod/ulab/code/ndarray.h"
16+
1417
#include "shared-module/_eve/__init__.h"
1518
#include "shared-bindings/_eve/__init__.h"
1619

@@ -844,6 +847,17 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vertex2ii_obj, 3, 5, _vertex2ii);
844847

845848
// }
846849

850+
static bool is_vector(mp_obj_t a) {
851+
if(!mp_obj_is_type(a, &ulab_ndarray_type)) {
852+
return false;
853+
}
854+
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(a);
855+
if (!ndarray_is_dense(ndarray)) {
856+
mp_raise_TypeError(MP_ERROR_TEXT("input must be an ndarray"));
857+
}
858+
return true;
859+
}
860+
847861
// Hand-written functions {
848862

849863
//| def Vertex2f(self, b: float) -> None:
@@ -853,9 +867,19 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vertex2ii_obj, 3, 5, _vertex2ii);
853867
//| :param float y: pixel y-coordinate"""
854868
//| ...
855869
static mp_obj_t _vertex2f(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
856-
mp_float_t x = mp_obj_get_float(a0);
857-
mp_float_t y = mp_obj_get_float(a1);
858-
common_hal__eve_Vertex2f(EVEHAL(self), x, y);
870+
if(is_vector(a0) && is_vector(a1)) {
871+
ndarray_obj_t *v0 = MP_OBJ_TO_PTR(a0);
872+
ndarray_obj_t *v1 = MP_OBJ_TO_PTR(a1);
873+
mp_float_t *p0 = (mp_float_t *)v0->array;
874+
mp_float_t *p1 = (mp_float_t *)v1->array;
875+
for(size_t i=0; i < v0->len; i++, p0++, p1++) {
876+
common_hal__eve_Vertex2f(EVEHAL(self), *p0, *p1);
877+
}
878+
} else {
879+
mp_float_t x = mp_obj_get_float(a0);
880+
mp_float_t y = mp_obj_get_float(a1);
881+
common_hal__eve_Vertex2f(EVEHAL(self), x, y);
882+
}
859883
return mp_const_none;
860884
}
861885
static MP_DEFINE_CONST_FUN_OBJ_3(vertex2f_obj, _vertex2f);

0 commit comments

Comments
 (0)