8000 Merge pull request #9589 from jamesbowman/main · jamesbowman/circuitpython@5f66400 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f66400

Browse files
authored
Merge pull request adafruit#9589 from jamesbowman/main
Add support for ulab.numpy float vectors in Vertex2f()
2 parents c302c23 + 6b82e5c commit 5f66400

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

shared-bindings/_eve/__init__.c

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

14+
#if CIRCUITPY_ULAB
15+
#include "extmod/ulab/code/ulab.h"
16+
#include "extmod/ulab/code/ndarray.h"
17+
#endif
18+
1419
#include "shared-module/_eve/__init__.h"
1520
#include "shared-bindings/_eve/__init__.h"
1621

@@ -844,6 +849,19 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vertex2ii_obj, 3, 5, _vertex2ii);
844849

845850
// }
846851

852+
#if CIRCUITPY_ULAB
853+
static bool is_vector(mp_obj_t a) {
854+
if (!mp_obj_is_type(a, &ulab_ndarray_type)) {
855+
return false;
856+
}
857+
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(a);
858+
if (!ndarray_is_dense(ndarray)) {
859+
mp_raise_TypeError(MP_ERROR_TEXT("input must be an ndarray"));
860+
}
861+
return true;
862+
}
863+
#endif
864+
847865
// Hand-written functions {
848866

849867
//| def Vertex2f(self, b: float) -> None:
@@ -853,6 +871,18 @@ static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(vertex2ii_obj, 3, 5, _vertex2ii);
853871
//| :param float y: pixel y-coordinate"""
854872
//| ...
855873
static mp_obj_t _vertex2f(mp_obj_t self, mp_obj_t a0, mp_obj_t a1) {
874+
#if CIRCUITPY_ULAB
875+
if (is_vector(a0) && is_vector(a1)) {
876+
ndarray_obj_t *v0 = MP_OBJ_TO_PTR(a0);
877+
ndarray_obj_t *v1 = MP_OBJ_TO_PTR(a1);
878+
mp_float_t *p0 = (mp_float_t *)v0->array;
879+
mp_float_t *p1 = (mp_float_t *)v1->array;
880+
for (size_t i = 0; i < v0->len; i++, p0++, p1++) {
881+
common_hal__eve_Vertex2f(EVEHAL(self), *p0, *p1);
882+
}
883+
return mp_const_none;
884+
}
885+
#endif
856886
mp_float_t x = mp_obj_get_float(a0);
857887
mp_float_t y = mp_obj_get_float(a1);
858888
common_hal__eve_Vertex2f(EVEHAL(self), x, y);

0 commit comments

Comments
 (0)
0