77 */
88#define NO_IMPORT_ARRAY
99
10+ #include " ../mplutils.h"
1011#include " _tri.h"
1112
1213#include < algorithm>
1314#include < set>
1415
15- #define MOVETO 1
16- #define LINETO 2
17- #define CLOSEPOLY 79
18-
1916
2017TriEdge::TriEdge ()
2118 : tri(-1 ), edge(-1 )
@@ -629,14 +626,12 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
629626 npy_intp npoints = static_cast <npy_intp>(contour_line.size ());
630627
631628 npy_intp segs_dims[2 ] = {npoints, 2 };
632- PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew (
633- 2 , segs_dims, NPY_DOUBLE);
634- double * segs_ptr = (double *)PyArray_DATA (segs);
629+ numpy::array_view<double , 2 > segs (segs_dims);
630+ double * segs_ptr = segs.data ();
635631
636632 npy_intp codes_dims[1 ] = {npoints};
637- PyArrayObject* codes = (PyArrayObject*)PyArray_SimpleNew (
638- 1 , codes_dims, NPY_UBYTE);
639- 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 ();
640635
641636 for (ContourLine::const_iterator it = contour_line.begin ();
642637 it != contour_line.end (); ++it) {
@@ -650,24 +645,21 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
650645 contour_line.front () == contour_line.back ())
651646 *(codes_ptr-1 ) = CLOSEPOLY;
652647
653- if (PyList_SetItem (vertices_list, i, (PyObject*)segs) ||
654- PyList_SetItem (codes_list, i, (PyObject*)codes)) {
655- Py_XDECREF (segs);
656- Py_XDECREF (codes);
657- PyErr_SetString (PyExc_RuntimeError,
658- " Unable to set contour segments and kind codes" );
659- return NULL ;
660- }
648+ PyList_SET_ITEM (vertices_list, i, segs.pyobj ());
649+ PyList_SET_ITEM (codes_list, i, codes.pyobj ());
661650 }
662651
663652 PyObject* result = PyTuple_New (2 );
664- if (PyTuple_SetItem (result, 0 , (PyObject*)vertices_list) ||
665- PyTuple_SetItem (result, 1 , (PyObject*)codes_list)) {
666- Py_XDECREF (result);
667- PyErr_SetString (PyExc_RuntimeError,
668- " Unable to set contour segments and kind codes" );
669- return NULL ;
653+ if (result == 0 ) {
654+ Py_XDECREF (vertices_list);
655+ Py_XDECREF (codes_list);
656+ throw std::runtime_error (" Failed to create Python tuple" );
670657 }
658+
659+ // No error checking here as filling in a brand new pre-allocated result.
660+ PyTuple_SET_ITEM (result, 0 , vertices_list);
661+ PyTuple_SET_ITEM (result, 1 , codes_list);
662+
671663 return result;
672664}
673665
@@ -697,15 +689,13 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
697689
698690 // Create segs array for point coordinates.
699691 npy_intp segs_dims[2 ] = {n_points, 2 };
700- PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew (
701- 2 , segs_dims, NPY_DOUBLE);
702- double * segs_ptr = (double *)PyArray_DATA (segs);
692+ numpy::array_view<double , 2 > segs (segs_dims);
693+ double * segs_ptr = segs.data ();
703694
704695 // Create kinds array for code types.
705696 npy_intp codes_dims[1 ] = {n_points};
706- PyArrayObject* codes = (PyArrayObject*)PyArray_SimpleNew (
707- 1 , codes_dims, NPY_UBYTE);
708- 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 ();
709699
710700 for (line = contour.begin (); line != contour.end (); ++line) {
711701 for (point = line->begin (); point != line->end (); point++) {
@@ -725,21 +715,24 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
725715 throw std::runtime_error (" Failed to create Python list" );
726716 }
727717
728- if (PyList_Append (vertices_list, (PyObject*)segs ) ||
729- PyList_Append (codes_list, (PyObject*)codes )) {
718+ if (PyList_Append (vertices_list, segs. pyobj_steal () ) ||
719+ PyList_Append (codes_list, codes. pyobj_steal () )) {
730720 Py_XDECREF (vertices_list);
731721 Py_XDECREF (codes_list);
732722 throw std::runtime_error (" Unable to add contour to vertices and codes lists" );
733723 }
734724
735725 PyObject* result = PyTuple_New (2 );
736- if (PyTuple_SetItem (result, 0 , (PyObject*)vertices_list) ||
737- PyTuple_SetItem (result, 1 , (PyObject*)codes_list)) {
738- Py_XDECREF (result);
739- PyErr_SetString (PyExc_RuntimeError,
740- " Unable to set contour segments and kinds" );
741- return NULL ;
726+ if (result == 0 ) {
727+ Py_XDECREF (vertices_list);
728+ Py_XDECREF (codes_list);
729+ throw std::runtime_error (" Failed to create Python tuple" );
742730 }
731+
732+ // No error checking here as filling in a brand new pre-allocated tuple.
733+ PyTuple_SET_ITEM (result, 0 , vertices_list);
734+ PyTuple_SET_ITEM (result, 1 , codes_list);
735+
743736 return result;
744737}
745738
0 commit comments