PyAPI: drop Vector requirement for tessellate_polygon

Use mathutils_array_parse which converts any sequence of numbers.
This commit is contained in:
Campbell Barton 2019-10-09 17:26:39 +11:00
parent 6ff60d9982
commit d3d6cd2e09

@ -1201,8 +1201,8 @@ static PyObject *M_Geometry_interpolate_bezier(PyObject *UNUSED(self), PyObject
PyDoc_STRVAR(M_Geometry_tessellate_polygon_doc,
".. function:: tessellate_polygon(veclist_list)\n"
"\n"
" Takes a list of polylines (each point a vector) and returns the point indices "
"for a polyline filled with triangles.\n"
" Takes a list of polylines (each point a pair or triplet of numbers) and returns "
"the point indices for a polyline filled with triangles.\n"
"\n"
" :arg veclist_list: list of polylines\n"
" :rtype: list\n");
@ -1211,14 +1211,15 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject
{
PyObject *tri_list; /*return this list of tri's */
PyObject *polyLine, *polyVec;
int i, len_polylines, len_polypoints, ls_error = 0;
int i, len_polylines, len_polypoints;
bool list_parse_error = false;
bool is_2d = true;
/* Display #ListBase. */
ListBase dispbase = {NULL, NULL};
DispList *dl;
float *fp; /*pointer to the array of malloced dl->verts to set the points from the vectors */
int index, *dl_face, totpoints = 0;
int totpoints = 0;
if (!PySequence_Check(polyLineSeq)) {
PyErr_SetString(PyExc_TypeError, "expected a sequence of poly lines");
@ -1239,15 +1240,6 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject
len_polypoints = PySequence_Size(polyLine);
if (len_polypoints > 0) { /* don't bother adding edges as polylines */
# if 0
if (EXPP_check_sequence_consistency(polyLine, &vector_Type) != 1) {
freedisplist(&dispbase);
Py_DECREF(polyLine);
PyErr_SetString(PyExc_TypeError,
"A point in one of the polylines is not a mathutils.Vector type");
return NULL;
}
# endif
dl = MEM_callocN(sizeof(DispList), "poly disp");
BLI_addtail(&dispbase, dl);
dl->type = DL_INDEX3;
@ -1255,44 +1247,33 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject
dl->type = DL_POLY;
dl->parts = 1; /* no faces, 1 edge loop */
dl->col = 0; /* no material */
dl->verts = fp = MEM_callocN(sizeof(float) * 3 * len_polypoints, "dl verts");
dl->index = MEM_callocN(sizeof(int) * 3 * len_polypoints, "dl index");
dl->verts = fp = MEM_mallocN(sizeof(float[3]) * len_polypoints, "dl verts");
dl->index = MEM_callocN(sizeof(int[3]) * len_polypoints, "dl index");
for (index = 0; index < len_polypoints; index++, fp += 3) {
for (int index = 0; index < len_polypoints; index++, fp += 3) {
polyVec = PySequence_GetItem(polyLine, index);
if (VectorObject_Check(polyVec)) {
const int polyVec_len = mathutils_array_parse(
fp, 2, 3 | MU_ARRAY_SPILL, polyVec, "tessellate_polygon: parse coord");
Py_DECREF(polyVec);
if (BaseMath_ReadCallback((VectorObject *)polyVec) == -1) {
ls_error = 1;
}
fp[0] = ((VectorObject *)polyVec)->vec[0];
fp[1] = ((VectorObject *)polyVec)->vec[1];
if (((VectorObject *)polyVec)->size > 2) {
fp[2] = ((VectorObject *)polyVec)->vec[2];
is_2d = false;
}
else {
/* if its a 2d vector then set the z to be zero */
fp[2] = 0.0f;
}
if (UNLIKELY(polyVec_len == -1)) {
list_parse_error = true;
}
else {
ls_error = 1;
else if (polyVec_len == 2) {
fp[2] = 0.0f;
}
else if (polyVec_len == 3) {
is_2d = false;
}
totpoints++;
Py_DECREF(polyVec);
}
}
Py_DECREF(polyLine);
}
if (ls_error) {
if (list_parse_error) {
BKE_displist_free(&dispbase); /* possible some dl was allocated */
PyErr_SetString(PyExc_TypeError,
"A point in one of the polylines "
"is not a mathutils.Vector type");
return NULL;
}
else if (totpoints) {
@ -1310,12 +1291,10 @@ static PyObject *M_Geometry_tessellate_polygon(PyObject *UNUSED(self), PyObject
return NULL;
}
index = 0;
dl_face = dl->index;
while (index < dl->parts) {
int *dl_face = dl->index;
for (int index = 0; index < dl->parts; index++) {
PyList_SET_ITEM(tri_list, index, PyC_Tuple_Pack_I32(dl_face[0], dl_face[1], dl_face[2]));
dl_face += 3;
index++;
}
BKE_displist_free(&dispbase);
}