1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
|
#include <Python.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <numpy/arrayobject.h>
#include "KDTree.h"
/* Must define Py_TYPE for Python 2.5 or older */
#ifndef Py_TYPE
# define Py_TYPE(o) ((o)->ob_type)
#endif
/* Must define PyVarObject_HEAD_INIT for Python 2.5 or older */
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) \
PyObject_HEAD_INIT(type) size,
#endif
typedef struct {
PyObject_HEAD
struct Neighbor neighbor;
} PyNeighbor;
static int
PyNeighbor_init(PyNeighbor *self, PyObject *args, PyObject *kwds)
{
long int index1, index2;
float radius = 0.0;
static char *kwlist[] = {"index1", "index2", "radius", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "ii|d", kwlist,
&index1, &index2, &radius))
return -1;
self->neighbor.index1 = index1;
self->neighbor.index2 = index2;
self->neighbor.radius = radius;
return 0;
}
static PyObject*
PyNeighbor_repr(PyNeighbor* self)
{
char string[64];
sprintf(string, "(%ld, %ld): %g",
self->neighbor.index1, self->neighbor.index2, self->neighbor.radius);
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString(string);
#else
return PyString_FromString(string);
#endif
}
static char PyNeighbor_index1__doc__[] =
"index of the first neighbor";
static PyObject*
PyNeighbor_getindex1(PyNeighbor* self, void* closure)
{
#if PY_MAJOR_VERSION >= 3
return PyLong_FromLong(self->neighbor.index1);
#else
return PyInt_FromLong(self->neighbor.index1);
#endif
}
static int
PyNeighbor_setindex1(PyNeighbor* self, PyObject* value, void* closure)
{
#if PY_MAJOR_VERSION >= 3
long index1 = PyLong_AsLong(value);
#else
long index1 = PyInt_AsLong(value);
#endif
if (PyErr_Occurred()) return -1;
self->neighbor.index1 = index1;
return 0;
}
static char PyNeighbor_index2__doc__[] =
"index of the second neighbor";
static PyObject*
PyNeighbor_getindex2(PyNeighbor* self, void* closure)
{
#if PY_MAJOR_VERSION >= 3
return PyLong_FromLong(self->neighbor.index2);
#else
return PyInt_FromLong(self->neighbor.index2);
#endif
}
static int
PyNeighbor_setindex2(PyNeighbor* self, PyObject* value, void* closure)
{
#if PY_MAJOR_VERSION >= 3
long index2 = PyLong_AsLong(value);
#else
long index2 = PyInt_AsLong(value);
#endif
if (PyErr_Occurred()) return -1;
self->neighbor.index2 = index2;
return 0;
}
static PyObject*
PyNeighbor_getradius(PyNeighbor* self, void* closure)
{
float value = self->neighbor.radius;
return PyFloat_FromDouble((double)value);
}
static int
PyNeighbor_setradius(PyNeighbor* self, PyObject* value, void* closure)
{ const double radius = PyFloat_AsDouble(value);
if (PyErr_Occurred()) return -1;
self->neighbor.radius = (float)radius;
return 0;
}
static char PyNeighbor_radius__doc__[] =
"the radius\n";
static PyGetSetDef PyNeighbor_getset[] = {
{"index1", (getter)PyNeighbor_getindex1, (setter)PyNeighbor_setindex1, PyNeighbor_index1__doc__, NULL},
{"index2", (getter)PyNeighbor_getindex2, (setter)PyNeighbor_setindex2, PyNeighbor_index2__doc__, NULL},
{"radius", (getter)PyNeighbor_getradius, (setter)PyNeighbor_setradius, PyNeighbor_radius__doc__, NULL},
{NULL} /* Sentinel */
};
static char PyNeighbor_doc[] =
"A neighbor pair; members are index1, index2, and radius.\n";
static PyTypeObject PyNeighborType = {
PyVarObject_HEAD_INIT(NULL, 0)
"KDTree.Neighbor", /* tp_name*/
sizeof(PyNeighbor), /* tp_basicsize*/
0, /* tp_itemsize*/
0, /* tp_dealloc*/
0, /* tp_print*/
0, /* tp_getattr*/
0, /* tp_setattr*/
0, /* tp_compare*/
(reprfunc)PyNeighbor_repr, /* tp_repr*/
0, /* tp_as_number*/
0, /* tp_as_sequence*/
0, /* tp_as_mapping*/
0, /* tp_hash */
0, /* tp_call*/
0, /* tp_str*/
0, /* tp_getattro*/
0, /* tp_setattro*/
0, /* tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /* tp_flags*/
PyNeighbor_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
PyNeighbor_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)PyNeighbor_init, /* tp_init */
};
typedef struct {
PyObject_HEAD
struct KDTree* tree;
} PyTree;
static void
PyTree_dealloc(PyTree* self)
{
KDTree_destroy(self->tree);
Py_TYPE(self)->tp_free((PyObject*)self);
}
static int
PyTree_init(PyTree* self, PyObject* args, PyObject* kwds)
{
int dim;
int bucket_size;
struct KDTree* tree;
if(!PyArg_ParseTuple(args, "ii:KDTree_init" ,&dim, &bucket_size)) return -1;
if (dim <= 0 || bucket_size <= 0)
{
PyErr_SetString(PyExc_ValueError, "Both arguments should be positive");
return -1;
}
tree = KDTree_init(dim, bucket_size);
if (tree==NULL)
{
PyErr_SetString(PyExc_MemoryError, "Insufficient memory for tree");
return -1;
}
self->tree = tree;
return 0;
}
static PyObject*
PyTree_get_count(PyTree* self)
{
long count;
struct KDTree* tree = self->tree;
PyObject* result;
count = KDTree_get_count(tree);
#if PY_MAJOR_VERSION >= 3
result = PyLong_FromLong(count);
#else
result = PyInt_FromLong(count);
#endif
if (!result)
{
PyErr_SetString (PyExc_MemoryError, "Failed to allocate memory for object.");
return NULL;
}
return result;
}
static PyObject*
PyTree_neighbor_get_count(PyTree* self)
{
long count;
struct KDTree* tree = self->tree;
PyObject* result;
count = KDTree_neighbor_get_count(tree);
#if PY_MAJOR_VERSION >= 3
result = PyLong_FromLong(count);
#else
result = PyInt_FromLong(count);
#endif
if (!result)
{
PyErr_SetString (PyExc_MemoryError, "Failed to allocate memory for object.");
return NULL;
}
return result;
}
static PyObject*
PyTree_set_data(PyTree* self, PyObject* args)
{
float* coords;
long int n, m, i;
PyObject *obj;
PyArrayObject *array;
struct KDTree* tree = self->tree;
int ok;
npy_intp rowstride, colstride;
const char* p;
if(!PyArg_ParseTuple(args, "O:KDTree_set_data",&obj)) return NULL;
/* Check if it is an array */
if (!PyArray_Check(obj))
{
PyErr_SetString(PyExc_TypeError, "First argument must be an array.");
return NULL;
}
array=(PyArrayObject *) obj;
if(PyArray_NDIM(array)!=2)
{
PyErr_SetString(PyExc_ValueError, "Array must be two dimensional.");
return NULL;
}
if (PyArray_TYPE(array) == NPY_DOUBLE)
{
Py_INCREF(obj);
}
else
{
/* Cast to type double */
obj = PyArray_Cast(array, NPY_DOUBLE);
if (!obj)
{
PyErr_SetString(PyExc_ValueError,
"coordinates cannot be cast to needed type.");
return NULL;
}
array = (PyArrayObject*) obj;
}
n = (long int) PyArray_DIM(array, 0);
m = (long int) PyArray_DIM(array, 1);
/* coord_data is deleted by the KDTree object */
coords= malloc(m*n*sizeof(float));
if (!coords)
{
Py_DECREF(obj);
PyErr_SetString (PyExc_MemoryError, "Failed to allocate memory for coordinates.");
return NULL;
}
rowstride = PyArray_STRIDE(array, 0);
colstride = PyArray_STRIDE(array, 1);
p = PyArray_BYTES(array);
for (i=0; i<n; i++)
{
int j;
for (j=0; j<m; j++)
{
coords[i*m+j]=*(double *) (p+i*rowstride+j*colstride);
}
}
Py_DECREF(obj);
ok = KDTree_set_data(tree, coords, n);
if (!ok)
{
PyErr_SetString (PyExc_MemoryError, "Failed to allocate memory for nodes.");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
PyTree_search_center_radius(PyTree* self, PyObject* args)
{
PyObject *obj;
double radius;
PyArrayObject *array;
long int n, i;
float *coords;
struct KDTree* tree = self->tree;
int ok;
npy_intp stride;
const char* p;
if(!PyArg_ParseTuple(args, "Od:KDTree_search_center_radius", &obj ,&radius))
return NULL;
if(radius <= 0)
{
PyErr_SetString(PyExc_ValueError, "Radius must be positive.");
return NULL;
}
/* Check if it is an array */
if (!PyArray_Check(obj))
{
PyErr_SetString(PyExc_TypeError, "First argument must be an array.");
return NULL;
}
array=(PyArrayObject *) obj;
if(PyArray_NDIM(array)!=1)
{
PyErr_SetString(PyExc_ValueError, "Array must be one dimensional.");
return NULL;
}
if (PyArray_TYPE(array) == NPY_DOUBLE)
{
Py_INCREF(obj);
}
else
{
/* Cast to type double */
obj = PyArray_Cast(array, NPY_DOUBLE);
if (!obj)
{
PyErr_SetString(PyExc_ValueError,
"coordinates cannot be cast to needed type.");
return NULL;
}
array = (PyArrayObject*) obj;
}
n = (long int) PyArray_DIM(array, 0);
/* coord_data is deleted by the KDTree object */
coords= malloc(n*sizeof(float));
if (!coords)
{
Py_DECREF(obj);
PyErr_SetString (PyExc_MemoryError, "Failed to allocate memory for coordinates.");
return NULL;
}
stride = PyArray_STRIDE(array, 0);
p = PyArray_BYTES(array);
for (i=0; i<n; i++)
{
coords[i]=*(double *) (p+i*stride);
}
Py_DECREF(obj);
ok = KDTree_search_center_radius(tree, coords, radius);
if (!ok)
{
PyErr_SetString (PyExc_MemoryError, "Insufficient memory for calculation.");
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
PyTree_neighbor_search(PyTree* self, PyObject* args)
{
int ok;
double radius;
struct KDTree* tree = self->tree;
struct Neighbor* neighbors;
struct Neighbor* pp, *qq;
PyObject* list;
Py_ssize_t i, n;
if(!PyArg_ParseTuple(args, "d:KDTree_neighbor_search", &radius))
return NULL;
if(radius <= 0)
{
PyErr_SetString(PyExc_ValueError, "Radius must be positive.");
return NULL;
}
ok = KDTree_neighbor_search(tree, radius, &neighbors);
if (!ok)
{
PyErr_SetString(PyExc_MemoryError,
"calculation failed due to lack of memory");
return NULL;
}
pp = neighbors;
n = 0;
while (pp)
{
n+=1;
pp = pp->next;
}
list = PyList_New(n);
if (list)
{
PyNeighbor* p;
pp = neighbors;
for (i = 0; i < n; i++)
{
p = (PyNeighbor*) PyNeighborType.tp_alloc(&PyNeighborType, 0);
if(!p)
{
PyErr_SetString(PyExc_MemoryError,
"could not create node for return value");
Py_DECREF(list);
return NULL;
}
p->neighbor = *pp;
PyList_SET_ITEM(list, i, (PyObject*)p);
qq = pp->next;
free(pp);
pp = qq;
}
}
return list;
}
static PyObject*
PyTree_neighbor_simple_search(PyTree* self, PyObject* args)
{
int ok;
double radius;
struct KDTree* tree = self->tree;
struct Neighbor* neighbors;
struct Neighbor* pp, *qq;
PyObject* list;
Py_ssize_t i, n;
if(!PyArg_ParseTuple(args, "d:KDTree_neighbor_simple_search", &radius))
return NULL;
if(radius <= 0)
{
PyErr_SetString(PyExc_ValueError, "Radius must be positive.");
return NULL;
}
ok = KDTree_neighbor_simple_search(tree, radius, &neighbors);
if (!ok)
{
PyErr_SetString(PyExc_MemoryError,
"calculation failed due to lack of memory");
return NULL;
}
pp = neighbors;
n = 0;
while (pp)
{
n+=1;
pp = pp->next;
}
list = PyList_New(n);
if (list)
{
PyNeighbor* p;
pp = neighbors;
for (i = 0; i < n; i++)
{
p = (PyNeighbor*) PyNeighborType.tp_alloc(&PyNeighborType, 0);
if(!p)
{
PyErr_SetString(PyExc_MemoryError,
"could not create node for return value");
Py_DECREF(list);
return NULL;
}
p->neighbor = *pp;
PyList_SET_ITEM(list, i, (PyObject*)p);
qq = pp->next;
free(pp);
pp = qq;
}
}
return list;
}
static char PyTree_get_indices__doc__[] =
"returns indices of coordinates within radius as a Numpy array\n";
static PyObject *PyTree_get_indices(PyTree *self)
{
npy_intp length;
PyArrayObject *array;
struct KDTree* tree = self->tree;
length=KDTree_get_count(tree);
if (length==0)
{
Py_INCREF(Py_None);
return Py_None;
}
array=(PyArrayObject *) PyArray_SimpleNew(1, &length, NPY_LONG);
if (!array)
{
PyErr_SetString(PyExc_MemoryError,
"Insufficient memory for array");
return NULL;
}
/* copy the data into the Numpy data pointer */
KDTree_copy_indices(tree, (long int *) PyArray_BYTES(array));
return PyArray_Return(array);
}
static char PyTree_get_radii__doc__[] =
"returns distances of coordinates within radius as a Numpy array.\n";
static PyObject *PyTree_get_radii(PyTree *self)
{
npy_intp length;
PyArrayObject *array;
struct KDTree* tree = self->tree;
length=KDTree_get_count(tree);
if (length==0)
{
Py_INCREF(Py_None);
return Py_None;
}
array=(PyArrayObject *) PyArray_SimpleNew(1, &length, NPY_FLOAT32);
if (!array)
{
PyErr_SetString(PyExc_MemoryError,
"Insufficient memory for array");
return NULL;
}
/* copy the data into the Numpy data pointer */
KDTree_copy_radii(tree, (float *) PyArray_BYTES(array));
return PyArray_Return(array);
}
static PyMethodDef PyTree_methods[] = {
{"get_count", (PyCFunction)PyTree_get_count, METH_NOARGS, NULL},
{"set_data", (PyCFunction)PyTree_set_data, METH_VARARGS, NULL},
{"search_center_radius", (PyCFunction)PyTree_search_center_radius, METH_VARARGS, NULL},
{"neighbor_get_count", (PyCFunction)PyTree_neighbor_get_count, METH_NOARGS, NULL},
{"neighbor_search", (PyCFunction)PyTree_neighbor_search, METH_VARARGS, NULL},
{"neighbor_simple_search", (PyCFunction)PyTree_neighbor_simple_search, METH_VARARGS, NULL},
{"get_indices", (PyCFunction)PyTree_get_indices, METH_NOARGS, PyTree_get_indices__doc__},
{"get_radii", (PyCFunction)PyTree_get_radii, METH_NOARGS, PyTree_get_radii__doc__},
{NULL} /* Sentinel */
};
static char PyTree_doc[] = "C KDTree.\n";
static PyTypeObject PyTreeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"C KDTree", /*tp_name*/
sizeof(PyTree), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)PyTree_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
PyTree_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PyTree_methods, /* tp_methods */
NULL, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)PyTree_init, /* tp_init */
};
/* ========================================================================== */
/* -- Initialization -------------------------------------------------------- */
/* ========================================================================== */
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_CKDTree",
NULL,
-1,
NULL,
NULL,
NULL,
NULL,
NULL
};
PyObject *
PyInit__CKDTree(void)
#else
void
init_CKDTree(void)
#endif
{
PyObject *module;
import_array();
PyTreeType.tp_new = PyType_GenericNew;
PyNeighborType.tp_new = PyType_GenericNew;
if (PyType_Ready(&PyTreeType) < 0)
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
if (PyType_Ready(&PyNeighborType) < 0)
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
#if PY_MAJOR_VERSION >= 3
module = PyModule_Create(&moduledef);
if (module==NULL) return NULL;
#else
module = Py_InitModule("_CKDTree", NULL);
if (module==NULL) return;
#endif
Py_INCREF(&PyTreeType);
Py_INCREF(&PyNeighborType);
PyModule_AddObject(module, "KDTree", (PyObject*) &PyTreeType);
PyModule_AddObject(module, "Neighbor", (PyObject*) &PyNeighborType);
if (PyErr_Occurred()) Py_FatalError("can't initialize module _CKDTree");
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
|