Skip to content

Commit 62925ed

Browse files
committed
qhull: Use array_view in delaunay.
1 parent b9696fe commit 62925ed

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/qhull_wrap.cpp

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
#define PY_SSIZE_T_CLEAN
99
#include "Python.h"
10-
#include "numpy/ndarrayobject.h"
10+
#include "numpy_cpp.h"
1111
#ifdef _MSC_VER
1212
/* The Qhull header does not declare this as extern "C", but only MSVC seems to
1313
* do name mangling on global variables. We thus need to declare this before
@@ -63,7 +63,7 @@ get_facet_neighbours(const facetT* facet, const int* tri_indices,
6363
/* Return true if the specified points arrays contain at least 3 unique points,
6464
* or false otherwise. */
6565
static bool
66-
at_least_3_unique_points(int npoints, const double* x, const double* y)
66+
at_least_3_unique_points(npy_intp npoints, const double* x, const double* y)
6767
{
6868
int i;
6969
const int unique1 = 0; /* First unique point has index 0. */
@@ -98,7 +98,7 @@ at_least_3_unique_points(int npoints, const double* x, const double* y)
9898
* If hide_qhull_errors is true then qhull error messages are discarded;
9999
* if it is false then they are written to stderr. */
100100
static PyObject*
101-
delaunay_impl(int npoints, const double* x, const double* y,
101+
delaunay_impl(npy_intp npoints, const double* x, const double* y,
102102
bool hide_qhull_errors)
103103
{
104104
qhT qh_qh; /* qh variable type and name must be like */
@@ -281,58 +281,44 @@ delaunay_impl(int npoints, const double* x, const double* y,
281281
static PyObject*
282282
delaunay(PyObject *self, PyObject *args)
283283
{
284-
PyObject* xarg;
285-
PyObject* yarg;
286-
PyArrayObject* xarray;
287-
PyArrayObject* yarray;
284+
numpy::array_view<double, 1> xarray;
285+
numpy::array_view<double, 1> yarray;
288286
PyObject* ret;
289-
int npoints;
287+
npy_intp npoints;
290288
const double* x;
291289
const double* y;
292290

293-
if (!PyArg_ParseTuple(args, "OO", &xarg, &yarg)) {
294-
PyErr_SetString(PyExc_ValueError, "expecting x and y arrays");
291+
if (!PyArg_ParseTuple(args, "O&O&",
292+
&xarray.converter_contiguous, &xarray,
293+
&yarray.converter_contiguous, &yarray)) {
295294
return NULL;
296295
}
297296

298-
xarray = (PyArrayObject*)PyArray_ContiguousFromObject(xarg, NPY_DOUBLE,
299-
1, 1);
300-
yarray = (PyArrayObject*)PyArray_ContiguousFromObject(yarg, NPY_DOUBLE,
301-
1, 1);
302-
if (xarray == 0 || yarray == 0 ||
303-
PyArray_DIM(xarray,0) != PyArray_DIM(yarray, 0)) {
304-
Py_XDECREF(xarray);
305-
Py_XDECREF(yarray);
297+
npoints = xarray.dim(0);
298+
if (npoints != yarray.dim(0)) {
306299
PyErr_SetString(PyExc_ValueError,
307300
"x and y must be 1D arrays of the same length");
308301
return NULL;
309302
}
310303

311-
npoints = PyArray_DIM(xarray, 0);
312-
313304
if (npoints < 3) {
314-
Py_XDECREF(xarray);
315-
Py_XDECREF(yarray);
316305
PyErr_SetString(PyExc_ValueError,
317306
"x and y arrays must have a length of at least 3");
318307
return NULL;
319308
}
320309

321-
x = (const double*)PyArray_DATA(xarray);
322-
y = (const double*)PyArray_DATA(yarray);
310+
x = xarray.data();
311+
y = yarray.data();
323312

324313
if (!at_least_3_unique_points(npoints, x, y)) {
325-
Py_XDECREF(xarray);
326-
Py_XDECREF(yarray);
327314
PyErr_SetString(PyExc_ValueError,
328315
"x and y arrays must consist of at least 3 unique points");
329316
return NULL;
330317
}
331318

332-
ret = delaunay_impl(npoints, x, y, Py_VerboseFlag == 0);
319+
CALL_CPP("qhull.delaunay",
320+
(ret = delaunay_impl(npoints, x, y, Py_VerboseFlag == 0)));
333321

334-
Py_XDECREF(xarray);
335-
Py_XDECREF(yarray);
336322
return ret;
337323
}
338324

0 commit comments

Comments
 (0)