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
|
#/*##########################################################################
# Copyright (C) 2004-2015 V.A. Sole, European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This file is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Please contact the ESRF industrial unit (industry@esrf.fr) if this license
# is a problem for you.
#
#############################################################################*/
#include <Python.h>
#include <stdlib.h>
#include <stdio.h>
#include <./numpy/arrayobject.h>
#include "libqhull.h"
#include "qset.h" /* for FOREACHneighbor_() */
#include "poly.h" /* for qh_vertexneighbors() */
#include "geom.h" /* for qh_facetcenter() */
struct module_state {
PyObject *error;
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
/* Doc strings */
#if (REALfloat == 1)
PyDoc_STRVAR(Object3DQhull__doc__,
"Object3DQhullf is just an interface module to the Qhull library.\n"
" For the time being only delaunay triangulation is implemented.\n"
" See http://www.qhull.org for Qhull details.\n"
"\n"
"Object3DQHullf.delaunay(nodes, \"qhull d Qbb QJ Qc Po\")\n"
" Nodes is a sequence of points (an nrows x 2 or an nrows x 3 array)\n"
" The second argument is optional.\n"
" The output is an array of indices for the facets.\n");
PyDoc_STRVAR(Object3DQhull_delaunay__doc__,
"delaunay(nodes, \"qhull d Qbb QJ Qc Po\")\n"
" Nodes is a sequence of points (an nrows x 2 or an nrows x 3 array)\n"
" The second argument is optional.\n"
" http://www.qhull.org for Qhull details.\n"
" The output is an array of indices for the facets.\n");
#else
PyDoc_STRVAR(Object3DQhull__doc__,
"Object3DQhull is just an interface module to the Qhull library.\n"
" For the time being only delaunay triangulation is implemented.\n"
" See http://www.qhull.org for Qhull details.\n"
"\n"
"Object3DQHull.delaunay(nodes, \"qhull d Qbb QJ Qc\")\n"
" Nodes is a sequence of points (an nrows x 2 or an nrows x 3 array)\n"
" The second argument is optional.\n"
" The output is an array of indices for the facets.\n");
PyDoc_STRVAR(Object3DQhull_delaunay__doc__,
"delaunay(nodes, \"qhull d Qbb QJ Qc\")\n"
" Nodes is a sequence of points (an nrows x 2 or an nrows x 3 array)\n"
" The second argument is optional.\n"
" http://www.qhull.org for Qhull details.\n"
" The output is an array of indices for the facets.\n");
#endif
/* Function declarations */
static PyObject *object3DDelaunay(PyObject *dummy, PyObject *args);
static PyObject *object3DVoronoi(PyObject *dummy, PyObject *args);
static void qhullResultFailure(PyObject * self, int qhull_exitcode);
static PyObject *getQhullVersion(PyObject *dummy, PyObject *args);
static PyObject *object3DDelaunay(PyObject *self, PyObject *args)
{
/* input parameters */
PyObject *input1, *input3=NULL;
const char *input2 = NULL;
/* local variables */
PyArrayObject *pointArray, *inner_pointArray=NULL;
PyArrayObject *result, *inner_result=NULL ;
coordT *points; /* Qhull */
int dimension; /* Qhull */
int nPoints; /* Qhull */
int inner_nPoints = 0; /* Qhull */
int qhullResult; /* Qhull exit code, 0 means no error */
boolT ismalloc = False; /* True if Qhull should free points in
qh_freeqhull() or reallocation */
//char cQhullDefaultFlags[] = "qhull d Qbb Qt"; /* Qhull flags (see doc)*/
#if (REALfloat == 1)
char cQhullDefaultFlags[] = "qhull d Qbb QJ Qc Po"; /* Qhull flags (see doc) Po is to ignore precision errors*/
#else
char cQhullDefaultFlags[] = "qhull d Qbb QJ Qc"; /* Qhull flags (see doc)*/
#endif
char *cQhullFlags;
int nFacets = 0;
npy_intp outDimensions[3];
facetT *facet; /* needed by FORALLfacets */
vertexT *vertex, **vertexp;
int j, i;
#if (REALfloat == 1)
float *p;
float bestdist;
float point[4];
#else
double *p;
double bestdist;
double point[4];
#endif
unsigned int *uintP;
boolT isoutside;
struct module_state *st = GETSTATE(self);
/* ------------- statements ---------------*/
if (!PyArg_ParseTuple(args, "O|zO", &input1, &input2, &input3 ))
{
PyErr_SetString(st->error, "Unable to parse arguments");
return NULL;
}
/* The array containing the points */
#if (REALfloat == 1)
pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input1, PyArray_FLOAT,2,2);
if(input3) {
inner_pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input3, PyArray_FLOAT,2,2);
if(!inner_pointArray) {
PyErr_SetString(st->error, "third argument if given must be a nrows x X array");
return NULL;
}
}
#else
pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input1, PyArray_DOUBLE,2,2);
if(input3) {
inner_pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input3, PyArray_DOUBLE,2,2);
if(!inner_pointArray) {
PyErr_SetString(st->error, "third argument if given must be a nrows x X array");
return NULL;
}
}
#endif
if (pointArray == NULL)
{
PyErr_SetString(st->error, "First argument is not a nrows x X array");
return NULL;
}
if (input2 == NULL)
{
cQhullFlags = &cQhullDefaultFlags[0];
}
else
{
cQhullFlags = (char *) input2;
}
/* printf("flags = %s\n", cQhullFlags); */
/* dimension to pass to Qhull */
dimension = (int) pointArray->dimensions[1];
/* number of points for Qhull */
nPoints = (int) pointArray->dimensions[0];
/* the points themselves for Qhull */
points = (coordT *) pointArray->data;
qhullResult = qh_new_qhull(dimension, nPoints, points,
ismalloc, cQhullFlags, NULL, stderr);
if (qhullResult)
{
/* Free the memory allocated by Qhull */
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
if(input3) {
Py_DECREF (inner_pointArray);
}
qhullResultFailure(self, qhullResult);
return NULL;
}
/* Get the number of facets */
/* Probably there is a better way to do it */
FORALLfacets {
if (facet->upperdelaunay)
continue;
nFacets ++;
}
/* printf("Number of facets = %d\n", nFacets); */
/* Allocate the memory for the output array */
if (0) // As triangles
{
/* It has the form: [nfacets, dimension, 3] */
outDimensions[0] = nFacets;
outDimensions[1] = 3;
outDimensions[2] = dimension;
result = (PyArrayObject *)
PyArray_SimpleNew(3, outDimensions, PyArray_FLOAT);
if (result == NULL)
{
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
if(input3) {
Py_DECREF (inner_pointArray);
}
PyErr_SetString(st->error, "Error allocating output memory");
return NULL;
}
#if (REALfloat == 1)
p = (float *) result->data;
#else
p = (double *) result->data;
#endif
FORALLfacets {
if (facet->upperdelaunay)
continue;
FOREACHvertex_(facet->vertices) {
for (j = 0; j < (qh hull_dim - 1); ++j) {
*p = vertex->point[j];
++p;
}
}
}
}
else // As indices
{
outDimensions[0] = nFacets;
outDimensions[1] = dimension+1 ;
result = (PyArrayObject *)
PyArray_SimpleNew(2, outDimensions, PyArray_UINT32);
if( input3 )
{
inner_nPoints = (int) inner_pointArray->dimensions[0];
outDimensions[0] = inner_nPoints;
outDimensions[1] = dimension+1;
inner_result = (PyArrayObject *)
PyArray_SimpleNew(2, outDimensions, PyArray_UINT32);
if (inner_result == NULL)
{
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
Py_DECREF (inner_pointArray);
PyErr_SetString(st->error, "Error allocating output memory for inner points facets");
return NULL;
}
}
if (result == NULL)
{
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
{
if(inner_pointArray)
{
Py_DECREF (inner_pointArray) ;
}
}
PyErr_SetString(st->error, "Error allocating output memory");
return NULL;
}
uintP = (unsigned int *) result->data;
FORALLfacets {
if (facet->upperdelaunay)
continue;
FOREACHvertex_(facet->vertices) {
*uintP = qh_pointid(vertex->point);
++uintP;
}
}
if(input3)
{
uintP = (unsigned int *) inner_result->data;
#if (REALfloat == 1)
p = (float *) inner_pointArray->data;
#else
p = (double *) inner_pointArray->data;
#endif
for (i=0; i< inner_nPoints; i++)
{
for(j=0; j<dimension; j++)
{
point[j] = *( p++);
}
qh_setdelaunay( dimension+1 , 1, point);
facet = qh_findbestfacet(point, qh_ALL, &bestdist, &isoutside);
if (facet && !facet->upperdelaunay && facet->simplicial)
{
FOREACHvertex_(facet->vertices) {
*uintP = qh_pointid(vertex->point);
++uintP;
}
}
else
{
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
if(pointArray)
{
Py_DECREF (inner_pointArray);
}
PyErr_SetString(st->error, "Error allocating output memory");
return NULL;
}
}
}
}
/* Free the memory allocated by Qhull */
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
if( input3==NULL)
{
return PyArray_Return(result);
}
else
{
return Py_BuildValue("NN", result, inner_result );
}
}
static PyObject *object3DVoronoi(PyObject *self, PyObject *args)
{
/* input parameters */
PyObject *input1, *input3=NULL;
const char *input2 = NULL;
/* local variables */
PyArrayObject *pointArray, *inner_pointArray=NULL;
PyArrayObject *result;
coordT *points; /* Qhull */
int dimension; /* Qhull */
int nPoints; /* Qhull */
int qhullResult; /* Qhull exit code, 0 means no error */
boolT ismalloc = False; /* True if Qhull should free points in
qh_freeqhull() or reallocation */
#if (REALfloat == 1)
char cQhullDefaultFlags[] = "qhull v p"; /* Qhull flags (see doc) Po is to ignore precision errors*/
#else
char cQhullDefaultFlags[] = "qhull v p"; /* Qhull flags (see doc)*/
#endif
char *cQhullFlags;
int nFacets = 0;
npy_intp outDimensions[2];
facetT *facet; /* needed by FORALLfacets */
pointT *center;
int j, i;
#if (REALfloat == 1)
float *p;
#else
double *p;
#endif
struct module_state *st = GETSTATE(self);
/* ------------- statements ---------------*/
if (!PyArg_ParseTuple(args, "O|zO", &input1, &input2, &input3 ))
{
PyErr_SetString(st->error, "Unable to parse arguments");
return NULL;
}
/* The array containing the points */
#if (REALfloat == 1)
pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input1, PyArray_FLOAT,2,2);
if(input3) {
inner_pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input3, PyArray_FLOAT,2,2);
if(!inner_pointArray) {
PyErr_SetString(st->error, "third argument if given must be a nrows x X array");
return NULL;
}
}
#else
pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input1, PyArray_DOUBLE,2,2);
if(input3) {
inner_pointArray = (PyArrayObject *)
PyArray_ContiguousFromAny(input3, PyArray_DOUBLE,2,2);
if(!inner_pointArray) {
PyErr_SetString(st->error, "third argument if given must be a nrows x X array");
return NULL;
}
}
#endif
if (pointArray == NULL)
{
PyErr_SetString(st->error, "First argument is not a nrows x X array");
return NULL;
}
if (input2 == NULL)
{
cQhullFlags = &cQhullDefaultFlags[0];
}
else
{
cQhullFlags = (char *) input2;
}
/* printf("flags = %s\n", cQhullFlags); */
/* dimension to pass to Qhull */
dimension = (int) pointArray->dimensions[1];
/* number of points for Qhull */
nPoints = (int) pointArray->dimensions[0];
/* the points themselves for Qhull */
points = (coordT *) pointArray->data;
qhullResult = qh_new_qhull(dimension, nPoints, points,
ismalloc, cQhullFlags, NULL, stderr);
if (qhullResult)
{
/* Free the memory allocated by Qhull */
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
if(input3) {
Py_DECREF (inner_pointArray);
}
qhullResultFailure(self, qhullResult);
return NULL;
}
/* Get the number of facets */
/* Probably there is a better way to do it */
i = 0;
FORALLfacets {
if (facet->upperdelaunay)
continue;
i += 1;
printf("Facet number %d\n", i);
nFacets ++;
}
printf("Number of facets = %d\n", nFacets);
/* Allocate the memory for the output array */
/* It has the form: [nfacets, dimension, 3] */
outDimensions[0] = nFacets;
outDimensions[1] = dimension;
//outDimensions[2] = dimension;
printf("output dimensions = %ld, %ld\n", outDimensions[0], outDimensions[1]);
result = (PyArrayObject *)
PyArray_SimpleNew(2, outDimensions, PyArray_DOUBLE);
if (result == NULL)
{
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
if(input3) {
Py_DECREF (inner_pointArray);
}
PyErr_SetString(st->error, "Error allocating output memory");
return NULL;
}
#if (REALfloat == 1)
printf("FLOAT\n");
p = (float *) result->data;
#else
p = (double *) result->data;
printf("DOUBLE\n");
#endif
printf("qh hull_dim = %d\n", qh hull_dim);
i = 0;
if (1)
{
FORALLfacets {
if (facet->upperdelaunay)
continue;
if (facet->visitid > 0)
{
i += 1;
center = qh_facetcenter(facet->vertices);
for (j = 0; j < (qh hull_dim - 1); ++j) {
printf("vertex[%d] = %f\n", j, center[j]);
//p = ((double *) result->data) + (facet->visitid-1) * dimension + j;
*p = center[j];
p++;
}
}
}
printf("Number of Voronoi vertices = %d\n", i);
}
printf("PASSED LOOP\n");
/* Free the memory allocated by Qhull */
qh_freeqhull(qh_ALL);
Py_DECREF (pointArray);
return PyArray_Return(result);
}
static void
qhullResultFailure(PyObject * self, int qhull_exitcode)
{
struct module_state *st = GETSTATE(self);
switch (qhull_exitcode) {
case qh_ERRinput:
PyErr_BadInternalCall ();
break;
case qh_ERRsingular:
PyErr_SetString(PyExc_ArithmeticError,
"qhull singular input data");
break;
case qh_ERRprec:
PyErr_SetString(PyExc_ArithmeticError,
"qhull precision error");
break;
case qh_ERRmem:
PyErr_NoMemory();
break;
case qh_ERRqhull:
PyErr_SetString(st->error,
"qhull internal error");
break;
}
}
static PyObject *getQhullVersion(PyObject *self, PyObject *args)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_DecodeASCII(qh_version, strlen(qh_version), NULL);
#else
return PyString_FromString(qh_version);
#endif
}
/* Module methods */
static PyMethodDef Object3DQhullMethods[] = {
{"delaunay", object3DDelaunay, METH_VARARGS, Object3DQhull_delaunay__doc__},
{"voronoi", object3DVoronoi, METH_VARARGS, Object3DQhull_delaunay__doc__},
{"version", getQhullVersion, METH_VARARGS},
{NULL, NULL, 0, NULL} /* sentinel */
};
#if (REALfloat == 1)
#define MOD_NAME "Object3DQhullf"
#else
#define MOD_NAME "Object3DQhull"
#endif
#if PY_MAJOR_VERSION >= 3
static int Object3DQhull_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int Object3DQhull_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
MOD_NAME,
Object3DQhull__doc__,
sizeof(struct module_state),
Object3DQhullMethods,
NULL,
Object3DQhull_traverse,
Object3DQhull_clear,
NULL
};
#define INITERROR return NULL
#if (REALfloat == 1)
PyObject *
PyInit_Object3DQhullf(void)
#else
PyObject *
PyInit_Object3DQhull(void)
#endif
#else
#define INITERROR return
#if (REALfloat == 1)
void
initObject3DQhullf(void)
#else
void
initObject3DQhull(void)
#endif
#endif
{
struct module_state *st;
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule3(MOD_NAME, Object3DQhullMethods, Object3DQhull__doc__);
#endif
if (module == NULL)
INITERROR;
st = GETSTATE(module);
st->error = PyErr_NewException("Object3DQhull.error", NULL, NULL);
if (st->error == NULL) {
Py_DECREF(module);
INITERROR;
}
import_array()
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}
|