@@ -138,13 +138,7 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
138138 int exitcode; /* Value returned from qh_new_qhull(). */
139139 std::vector<int > tri_indices; /* Maps qhull facet id to triangle index. */
140140 int indices[3 ];
141- PyObject* tuple; /* Return tuple (triangles, neighbors). */
142141 const int ndim = 2 ;
143- npy_intp dims[2 ];
144- PyArrayObject* triangles = NULL ;
145- PyArrayObject* neighbors = NULL ;
146- int * triangles_ptr;
147- int * neighbors_ptr;
148142 double x_mean = 0.0 ;
149143 double y_mean = 0.0 ;
150144
@@ -213,24 +207,12 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
213207 tri_indices.resize (max_facet_id+1 );
214208
215209 /* Allocate Python arrays to return. */
216- dims[0 ] = ntri;
217- dims[1 ] = 3 ;
218- triangles = (PyArrayObject*)PyArray_SimpleNew (ndim, dims, NPY_INT);
219- if (triangles == NULL ) {
220- PyErr_SetString (PyExc_MemoryError,
221- " Could not allocate triangles array in qhull.delaunay" );
222- goto error;
223- }
224-
225- neighbors = (PyArrayObject*)PyArray_SimpleNew (ndim, dims, NPY_INT);
226- if (neighbors == NULL ) {
227- PyErr_SetString (PyExc_MemoryError,
228- " Could not allocate neighbors array in qhull.delaunay" );
229- goto error;
230- }
210+ npy_intp dims[2 ] = {ntri, 3 };
211+ numpy::array_view<int , ndim> triangles (dims);
212+ int * triangles_ptr = triangles.data ();
231213
232- triangles_ptr = ( int *) PyArray_DATA (triangles );
233- neighbors_ptr = ( int *) PyArray_DATA ( neighbors);
214+ numpy::array_view< int , ndim> neighbors (dims );
215+ int * neighbors_ptr = neighbors. data ( );
234216
235217 /* Determine triangles array and set tri_indices array. */
236218 i = 0 ;
@@ -257,17 +239,14 @@ delaunay_impl(npy_intp npoints, const double* x, const double* y,
257239 }
258240 }
259241
260- tuple = PyTuple_New (2 );
261- PyTuple_SetItem (tuple, 0 , (PyObject*)triangles);
262- PyTuple_SetItem (tuple, 1 , (PyObject*)neighbors);
263- return tuple;
264-
265- error:
266- /* Clean up. */
267- Py_XDECREF (triangles);
268- Py_XDECREF (neighbors);
242+ PyObject* tuple = PyTuple_New (2 );
243+ if (tuple == 0 ) {
244+ throw std::runtime_error (" Failed to create Python tuple" );
245+ }
269246
270- return NULL ;
247+ PyTuple_SET_ITEM (tuple, 0 , triangles.pyobj ());
248+ PyTuple_SET_ITEM (tuple, 1 , neighbors.pyobj ());
249+ return tuple;
271250}
272251
273252/* Process Python arguments and call Delaunay implementation method. */
0 commit comments