Skip to content

Commit ace6442

Browse files
committed
Use array_view in TriContour.
This makes NumPy array creation match the QuadContour case.
1 parent e618218 commit ace6442

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

src/tri/_tri.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,12 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
626626
npy_intp npoints = static_cast<npy_intp>(contour_line.size());
627627

628628
npy_intp segs_dims[2] = {npoints, 2};
629-
PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew(
630-
2, segs_dims, NPY_DOUBLE);
631-
double* segs_ptr = (double*)PyArray_DATA(segs);
629+
numpy::array_view<double, 2> segs(segs_dims);
630+
double* segs_ptr = segs.data();
632631

633632
npy_intp codes_dims[1] = {npoints};
634-
PyArrayObject* codes = (PyArrayObject*)PyArray_SimpleNew(
635-
1, codes_dims, NPY_UBYTE);
636-
unsigned char* codes_ptr = (unsigned char*)PyArray_DATA(codes);
633+
numpy::array_view<unsigned char, 1> codes(codes_dims);
634+
unsigned char* codes_ptr = codes.data();
637635

638636
for (ContourLine::const_iterator it = contour_line.begin();
639637
it != contour_line.end(); ++it) {
@@ -647,13 +645,8 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
647645
contour_line.front() == contour_line.back())
648646
*(codes_ptr-1) = CLOSEPOLY;
649647

650-
if (PyList_SetItem(vertices_list, i, (PyObject*)segs) ||
651-
PyList_SetItem(codes_list, i, (PyObject*)codes)) {
652-
Py_XDECREF(segs);
653-
Py_XDECREF(codes);
654-
throw std::runtime_error(
655-
"Unable to set contour segments and kind codes");
656-
}
648+
PyList_SET_ITEM(vertices_list, i, segs.pyobj());
649+
PyList_SET_ITEM(codes_list, i, codes.pyobj());
657650
}
658651

659652
PyObject* result = PyTuple_New(2);
@@ -696,15 +689,13 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
696689

697690
// Create segs array for point coordinates.
698691
npy_intp segs_dims[2] = {n_points, 2};
699-
PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew(
700-
2, segs_dims, NPY_DOUBLE);
701-
double* segs_ptr = (double*)PyArray_DATA(segs);
692+
numpy::array_view<double, 2> segs(segs_dims);
693+
double* segs_ptr = segs.data();
702694

703695
// Create kinds array for code types.
704696
npy_intp codes_dims[1] = {n_points};
705-
PyArrayObject* codes = (PyArrayObject*)PyArray_SimpleNew(
706-
1, codes_dims, NPY_UBYTE);
707-
unsigned char* codes_ptr = (unsigned char*)PyArray_DATA(codes);
697+
numpy::array_view<unsigned char, 1> codes(codes_dims);
698+
unsigned char* codes_ptr = codes.data();
708699

709700
for (line = contour.begin(); line != contour.end(); ++line) {
710701
for (point = line->begin(); point != line->end(); point++) {
@@ -724,8 +715,8 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
724715
throw std::runtime_error("Failed to create Python list");
725716
}
726717

727-
if (PyList_Append(vertices_list, (PyObject*)segs) ||
728-
PyList_Append(codes_list, (PyObject*)codes)) {
718+
if (PyList_Append(vertices_list, segs.pyobj_steal()) ||
719+
PyList_Append(codes_list, codes.pyobj_steal())) {
729720
Py_XDECREF(vertices_list);
730721
Py_XDECREF(codes_list);
731722
throw std::runtime_error("Unable to add contour to vertices and codes lists");

0 commit comments

Comments
 (0)