7
7
*/
8
8
#define NO_IMPORT_ARRAY
9
9
10
+ #include " ../mplutils.h"
10
11
#include " _tri.h"
11
12
12
13
#include < algorithm>
13
14
#include < set>
14
15
15
- #define MOVETO 1
16
- #define LINETO 2
17
- #define CLOSEPOLY 79
18
-
19
16
20
17
TriEdge::TriEdge ()
21
18
: tri(-1 ), edge(-1 )
@@ -629,14 +626,12 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
629
626
npy_intp npoints = static_cast <npy_intp>(contour_line.size ());
630
627
631
628
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 ();
635
631
636
632
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 ();
640
635
641
636
for (ContourLine::const_iterator it = contour_line.begin ();
642
637
it != contour_line.end (); ++it) {
@@ -650,24 +645,21 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
650
645
contour_line.front () == contour_line.back ())
651
646
*(codes_ptr-1 ) = CLOSEPOLY;
652
647
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 ());
661
650
}
662
651
663
652
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" );
670
657
}
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
+
671
663
return result;
672
664
}
673
665
@@ -697,15 +689,13 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
697
689
698
690
// Create segs array for point coordinates.
699
691
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 ();
703
694
704
695
// Create kinds array for code types.
705
696
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 ();
709
699
710
700
for (line = contour.begin (); line != contour.end (); ++line) {
711
701
for (point = line->begin (); point != line->end (); point++) {
@@ -725,21 +715,24 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
725
715
throw std::runtime_error (" Failed to create Python list" );
726
716
}
727
717
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 () )) {
730
720
Py_XDECREF (vertices_list);
731
721
Py_XDECREF (codes_list);
732
722
throw std::runtime_error (" Unable to add contour to vertices and codes lists" );
733
723
}
734
724
735
725
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" );
742
730
}
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
+
743
736
return result;
744
737
}
745
738
0 commit comments