| 1 | /*\file FetchData.cpp:
|
|---|
| 2 | * \brief: general I/O interface to fetch data in python
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | #ifdef HAVE_CONFIG_H
|
|---|
| 6 | #include <config.h>
|
|---|
| 7 | #else
|
|---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
|---|
| 9 | #endif
|
|---|
| 10 |
|
|---|
| 11 | #define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
|
|---|
| 12 | #define NO_IMPORT
|
|---|
| 13 |
|
|---|
| 14 | #include "./pythonio.h"
|
|---|
| 15 | #include "../../c/shared/shared.h"
|
|---|
| 16 |
|
|---|
| 17 | /*Primitive data types*/
|
|---|
| 18 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
|---|
| 19 | void FetchData(double* pscalar,PyObject* py_float){
|
|---|
| 20 |
|
|---|
| 21 | double dscalar;
|
|---|
| 22 |
|
|---|
| 23 | /*return internal value: */
|
|---|
| 24 | #if _PYTHON_MAJOR_ == 3
|
|---|
| 25 | if (PyFloat_Check(py_float))
|
|---|
| 26 | dscalar=PyFloat_AsDouble(py_float);
|
|---|
| 27 | else if (PyLong_Check(py_float))
|
|---|
| 28 | dscalar=(double)PyLong_AsLong(py_float);
|
|---|
| 29 | else if (PyBool_Check(py_float))
|
|---|
| 30 | dscalar=(double)PyLong_AsLong(py_float);
|
|---|
| 31 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
|---|
| 32 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 33 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
|---|
| 34 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 35 | else
|
|---|
| 36 | _error_("unrecognized float type in input!");
|
|---|
| 37 |
|
|---|
| 38 | #else
|
|---|
| 39 | if (PyFloat_Check(py_float))
|
|---|
| 40 | dscalar=PyFloat_AsDouble(py_float);
|
|---|
| 41 | else if (PyLong_Check(py_float))
|
|---|
| 42 | dscalar=PyLong_AsDouble(py_float);
|
|---|
| 43 | else if (PyInt_Check(py_float))
|
|---|
| 44 | dscalar=(double)PyInt_AsLong(py_float);
|
|---|
| 45 | else if (PyBool_Check(py_float))
|
|---|
| 46 | dscalar=(double)PyLong_AsLong(py_float);
|
|---|
| 47 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
|---|
| 48 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 49 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
|---|
| 50 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 51 | else
|
|---|
| 52 | _error_("unrecognized float type in input!");
|
|---|
| 53 | #endif
|
|---|
| 54 | /*output: */
|
|---|
| 55 | *pscalar=dscalar;
|
|---|
| 56 | }
|
|---|
| 57 | /*}}}*/
|
|---|
| 58 | /*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
|
|---|
| 59 | void FetchData(float* pscalar,PyObject* py_float){
|
|---|
| 60 |
|
|---|
| 61 | float fscalar;
|
|---|
| 62 |
|
|---|
| 63 | /*return internal value: */
|
|---|
| 64 | #if _PYTHON_MAJOR_ == 3
|
|---|
| 65 | if (PyFloat_Check(py_float))
|
|---|
| 66 | fscalar=PyFloat_AsDouble(py_float);
|
|---|
| 67 | else if (PyLong_Check(py_float))
|
|---|
| 68 | fscalar=(float)PyLong_AsLong(py_float);
|
|---|
| 69 | else if (PyBool_Check(py_float))
|
|---|
| 70 | fscalar=(float)PyLong_AsLong(py_float);
|
|---|
| 71 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
|---|
| 72 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 73 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
|---|
| 74 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 75 | else
|
|---|
| 76 | _error_("unrecognized float type in input!");
|
|---|
| 77 | #else
|
|---|
| 78 | if (PyFloat_Check(py_float))
|
|---|
| 79 | fscalar=PyFloat_AsDouble(py_float);
|
|---|
| 80 | else if (PyLong_Check(py_float))
|
|---|
| 81 | fscalar=(float)PyLong_AsDouble(py_float);
|
|---|
| 82 | else if (PyInt_Check(py_float))
|
|---|
| 83 | fscalar=(float)PyInt_AsLong(py_float);
|
|---|
| 84 | else if (PyBool_Check(py_float))
|
|---|
| 85 | fscalar=(float)PyLong_AsLong(py_float);
|
|---|
| 86 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
|---|
| 87 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 88 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
|---|
| 89 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
|---|
| 90 | else
|
|---|
| 91 | _error_("unrecognized float type in input!");
|
|---|
| 92 | #endif
|
|---|
| 93 | /*output: */
|
|---|
| 94 | *pscalar=fscalar;
|
|---|
| 95 | }
|
|---|
| 96 | /*}}}*/
|
|---|
| 97 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
|---|
| 98 | void FetchData(int* pscalar, PyObject* py_long){
|
|---|
| 99 |
|
|---|
| 100 | int iscalar;
|
|---|
| 101 |
|
|---|
| 102 | /*return internal value: */
|
|---|
| 103 | #if _PYTHON_MAJOR_ == 3
|
|---|
| 104 | if (PyLong_Check(py_long))
|
|---|
| 105 | iscalar=(int)PyLong_AsLong(py_long);
|
|---|
| 106 | else if (PyFloat_Check(py_long))
|
|---|
| 107 | iscalar=(int)PyFloat_AsDouble(py_long);
|
|---|
| 108 | else if (PyBool_Check(py_long))
|
|---|
| 109 | iscalar=(int)PyLong_AsLong(py_long);
|
|---|
| 110 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
|---|
| 111 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
|---|
| 112 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
|---|
| 113 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
|---|
| 114 | else
|
|---|
| 115 | _error_("unrecognized long type in input!");
|
|---|
| 116 |
|
|---|
| 117 | #else
|
|---|
| 118 | if (PyLong_Check(py_long))
|
|---|
| 119 | iscalar=(int)PyLong_AsLong(py_long);
|
|---|
| 120 | else if (PyInt_Check(py_long))
|
|---|
| 121 | iscalar=(int)PyInt_AsLong(py_long);
|
|---|
| 122 | else if (PyFloat_Check(py_long))
|
|---|
| 123 | iscalar=(int)PyFloat_AsDouble(py_long);
|
|---|
| 124 | else if (PyBool_Check(py_long))
|
|---|
| 125 | iscalar=(int)PyLong_AsLong(py_long);
|
|---|
| 126 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
|---|
| 127 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
|---|
| 128 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
|---|
| 129 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
|---|
| 130 | else
|
|---|
| 131 | _error_("unrecognized long type in input!");
|
|---|
| 132 | #endif
|
|---|
| 133 | /*output: */
|
|---|
| 134 | *pscalar=iscalar;
|
|---|
| 135 | }
|
|---|
| 136 | /*}}}*/
|
|---|
| 137 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
|---|
| 138 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
|---|
| 139 |
|
|---|
| 140 | bool bscalar;
|
|---|
| 141 |
|
|---|
| 142 | /*return internal value: */
|
|---|
| 143 | #if _PYTHON_MAJOR_ == 3
|
|---|
| 144 | if (PyBool_Check(py_boolean))
|
|---|
| 145 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 146 | else if (PyLong_Check(py_boolean))
|
|---|
| 147 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 148 | else if (PyLong_Check(py_boolean))
|
|---|
| 149 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 150 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
|---|
| 151 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
|---|
| 152 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
|---|
| 153 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
|---|
| 154 | else
|
|---|
| 155 | _error_("unrecognized boolean type in input!");
|
|---|
| 156 |
|
|---|
| 157 | #else
|
|---|
| 158 | if (PyBool_Check(py_boolean))
|
|---|
| 159 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 160 | else if (PyLong_Check(py_boolean))
|
|---|
| 161 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 162 | else if (PyLong_Check(py_boolean))
|
|---|
| 163 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
|---|
| 164 | else if (PyInt_Check(py_boolean))
|
|---|
| 165 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
|---|
| 166 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
|---|
| 167 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
|---|
| 168 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
|---|
| 169 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
|---|
| 170 | else
|
|---|
| 171 | _error_("unrecognized boolean type in input!");
|
|---|
| 172 | #endif
|
|---|
| 173 | /*output: */
|
|---|
| 174 | *pscalar=bscalar;
|
|---|
| 175 | }
|
|---|
| 176 | /*}}}*/
|
|---|
| 177 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
|---|
| 178 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
|---|
| 179 |
|
|---|
| 180 | /*output: */
|
|---|
| 181 | double* dmatrix=NULL;
|
|---|
| 182 | double* matrix=NULL;
|
|---|
| 183 | int M=0;
|
|---|
| 184 | int N=0;
|
|---|
| 185 | int ndim;
|
|---|
| 186 | npy_intp* dims=NULL;
|
|---|
| 187 |
|
|---|
| 188 | /*intermediary:*/
|
|---|
| 189 | long* lmatrix=NULL;
|
|---|
| 190 | bool* bmatrix=NULL;
|
|---|
| 191 | int i;
|
|---|
| 192 | PyObject* py_matrix2=NULL;
|
|---|
| 193 |
|
|---|
| 194 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
|---|
| 195 | /*retrieve dimensions: */
|
|---|
| 196 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
|---|
| 197 | if (ndim==2) {
|
|---|
| 198 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 199 | M=dims[0]; N=dims[1];
|
|---|
| 200 | }
|
|---|
| 201 | else if (ndim==1) {
|
|---|
| 202 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 203 | M=dims[0]; N=1;
|
|---|
| 204 | }
|
|---|
| 205 | else
|
|---|
| 206 | _error_("expecting an MxN matrix or M vector in input!");
|
|---|
| 207 |
|
|---|
| 208 | if (M && N) {
|
|---|
| 209 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
|---|
| 210 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
|
|---|
| 211 | py_matrix=py_matrix2;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
|---|
| 215 | /*retrieve internal value: */
|
|---|
| 216 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 217 |
|
|---|
| 218 | /*copy matrix: */
|
|---|
| 219 | matrix=xNew<double>(M*N);
|
|---|
| 220 | // if (PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
|---|
| 221 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
|---|
| 222 | // }
|
|---|
| 223 |
|
|---|
| 224 | // else {
|
|---|
| 225 | // int j,k,ipt=0;
|
|---|
| 226 | // int mstride,nstride;
|
|---|
| 227 | // mstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,0)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
|
|---|
| 228 | // if (ndim > 1)
|
|---|
| 229 | // nstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,1)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
|
|---|
| 230 | // else
|
|---|
| 231 | // nstride=1;
|
|---|
| 232 | // for (i=0; i<M; i++) {
|
|---|
| 233 | // k=i*mstride;
|
|---|
| 234 | // for (j=0; j<N; j++) {
|
|---|
| 235 | // matrix[ipt++]=dmatrix[k];
|
|---|
| 236 | // k+=nstride;
|
|---|
| 237 | // }
|
|---|
| 238 | // }
|
|---|
| 239 | // }
|
|---|
| 240 |
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
|---|
| 244 | /*retrieve internal value: */
|
|---|
| 245 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 246 |
|
|---|
| 247 | /*transform into double matrix: */
|
|---|
| 248 | matrix=xNew<double>(M*N);
|
|---|
| 249 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
|---|
| 253 | /*retrieve internal value: */
|
|---|
| 254 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 255 |
|
|---|
| 256 | /*transform into double matrix: */
|
|---|
| 257 | matrix=xNew<double>(M*N);
|
|---|
| 258 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | else
|
|---|
| 262 | _error_("unrecognized double pyarray type in input!");
|
|---|
| 263 |
|
|---|
| 264 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
|---|
| 265 | }
|
|---|
| 266 | else
|
|---|
| 267 | matrix=NULL;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | else {
|
|---|
| 271 | M=1;
|
|---|
| 272 | N=1;
|
|---|
| 273 | matrix=xNew<double>(M*N);
|
|---|
| 274 | FetchData(&(matrix[0]),py_matrix);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /*output: */
|
|---|
| 278 | if(pM)*pM=M;
|
|---|
| 279 | if(pN)*pN=N;
|
|---|
| 280 | if(pmatrix)*pmatrix=matrix;
|
|---|
| 281 | }
|
|---|
| 282 | /*}}}*/
|
|---|
| 283 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
|---|
| 284 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
|---|
| 285 |
|
|---|
| 286 | /*output: */
|
|---|
| 287 | int* matrix=NULL;
|
|---|
| 288 | int M=0;
|
|---|
| 289 | int N=0;
|
|---|
| 290 | int ndim;
|
|---|
| 291 | npy_intp* dims=NULL;
|
|---|
| 292 |
|
|---|
| 293 | /*intermediary:*/
|
|---|
| 294 | double* dmatrix=NULL;
|
|---|
| 295 | long* lmatrix=NULL;
|
|---|
| 296 | bool* bmatrix=NULL;
|
|---|
| 297 | int i;
|
|---|
| 298 | PyObject* py_matrix2=NULL;
|
|---|
| 299 |
|
|---|
| 300 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
|---|
| 301 | /*retrieve dimensions: */
|
|---|
| 302 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
|---|
| 303 | if (ndim==2) {
|
|---|
| 304 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 305 | M=dims[0]; N=dims[1];
|
|---|
| 306 | }
|
|---|
| 307 | else if (ndim==1) {
|
|---|
| 308 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 309 | M=dims[0]; N=1;
|
|---|
| 310 | }
|
|---|
| 311 | else
|
|---|
| 312 | _error_("expecting an MxN matrix or M vector in input!");
|
|---|
| 313 |
|
|---|
| 314 | if (M && N) {
|
|---|
| 315 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
|---|
| 316 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
|
|---|
| 317 | py_matrix=py_matrix2;
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
|---|
| 321 | /*retrieve internal value: */
|
|---|
| 322 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 323 |
|
|---|
| 324 | /*transform into integer matrix: */
|
|---|
| 325 | matrix=xNew<int>(M*N);
|
|---|
| 326 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
|---|
| 330 | /*retrieve internal value: */
|
|---|
| 331 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 332 |
|
|---|
| 333 | /*transform into integer matrix: */
|
|---|
| 334 | matrix=xNew<int>(M*N);
|
|---|
| 335 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
|---|
| 339 | /*retrieve internal value: */
|
|---|
| 340 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 341 |
|
|---|
| 342 | /*transform into integer matrix: */
|
|---|
| 343 | matrix=xNew<int>(M*N);
|
|---|
| 344 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | else
|
|---|
| 348 | _error_("unrecognized int pyarray type in input!");
|
|---|
| 349 |
|
|---|
| 350 | /* These lines are causing a segfault
|
|---|
| 351 | if (py_matrix2)
|
|---|
| 352 | delete(py_matrix2);
|
|---|
| 353 | */
|
|---|
| 354 | }
|
|---|
| 355 | else
|
|---|
| 356 | matrix=NULL;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | else {
|
|---|
| 360 | M=1;
|
|---|
| 361 | N=1;
|
|---|
| 362 | matrix=xNew<int>(M*N);
|
|---|
| 363 | FetchData(&(matrix[0]),py_matrix);
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | /*output: */
|
|---|
| 367 | if(pM)*pM=M;
|
|---|
| 368 | if(pN)*pN=N;
|
|---|
| 369 | if(pmatrix)*pmatrix=matrix;
|
|---|
| 370 | }
|
|---|
| 371 | /*}}}*/
|
|---|
| 372 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
|---|
| 373 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
|---|
| 374 |
|
|---|
| 375 | /*output: */
|
|---|
| 376 | bool* bmatrix=NULL;
|
|---|
| 377 | bool* matrix=NULL;
|
|---|
| 378 | int M=0;
|
|---|
| 379 | int N=0;
|
|---|
| 380 | int ndim;
|
|---|
| 381 | npy_intp* dims=NULL;
|
|---|
| 382 |
|
|---|
| 383 | /*intermediary:*/
|
|---|
| 384 | double* dmatrix=NULL;
|
|---|
| 385 | long* lmatrix=NULL;
|
|---|
| 386 | int i;
|
|---|
| 387 | PyObject* py_matrix2=NULL;
|
|---|
| 388 |
|
|---|
| 389 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
|---|
| 390 | /*retrieve dimensions: */
|
|---|
| 391 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
|---|
| 392 | if (ndim==2) {
|
|---|
| 393 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 394 | M=dims[0]; N=dims[1];
|
|---|
| 395 | }
|
|---|
| 396 | else if (ndim==1) {
|
|---|
| 397 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
|---|
| 398 | M=dims[0]; N=1;
|
|---|
| 399 | }
|
|---|
| 400 | else
|
|---|
| 401 | _error_("expecting an MxN matrix or M vector in input!");
|
|---|
| 402 |
|
|---|
| 403 | if (M && N) {
|
|---|
| 404 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
|---|
| 405 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
|
|---|
| 406 | py_matrix=py_matrix2;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
|---|
| 410 | /*retrieve internal value: */
|
|---|
| 411 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 412 |
|
|---|
| 413 | /*transform into bool matrix: */
|
|---|
| 414 | matrix=xNew<bool>(M*N);
|
|---|
| 415 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
|---|
| 419 | /*retrieve internal value: */
|
|---|
| 420 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 421 |
|
|---|
| 422 | /*transform into bool matrix: */
|
|---|
| 423 | matrix=xNew<bool>(M*N);
|
|---|
| 424 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
|---|
| 428 | /*retrieve internal value: */
|
|---|
| 429 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
|---|
| 430 |
|
|---|
| 431 | /*copy matrix: */
|
|---|
| 432 | matrix=xNew<bool>(M*N);
|
|---|
| 433 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | else
|
|---|
| 437 | _error_("unrecognized bool pyarray type in input!");
|
|---|
| 438 |
|
|---|
| 439 | if (py_matrix2)
|
|---|
| 440 | delete(py_matrix2);
|
|---|
| 441 | }
|
|---|
| 442 | else
|
|---|
| 443 | matrix=NULL;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | else {
|
|---|
| 447 | M=1;
|
|---|
| 448 | N=1;
|
|---|
| 449 | matrix=xNew<bool>(M*N);
|
|---|
| 450 | FetchData(&(matrix[0]),py_matrix);
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | /*output: */
|
|---|
| 454 | if(pM)*pM=M;
|
|---|
| 455 | if(pN)*pN=N;
|
|---|
| 456 | if(pmatrix)*pmatrix=matrix;
|
|---|
| 457 | }
|
|---|
| 458 | /*}}}*/
|
|---|
| 459 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 460 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
|---|
| 461 |
|
|---|
| 462 | /*output: */
|
|---|
| 463 | double* dvector=NULL;
|
|---|
| 464 | double* vector=NULL;
|
|---|
| 465 | int M=0;
|
|---|
| 466 | int ndim;
|
|---|
| 467 | npy_intp* dims=NULL;
|
|---|
| 468 |
|
|---|
| 469 | /*intermediary:*/
|
|---|
| 470 | long* lvector=NULL;
|
|---|
| 471 | bool* bvector=NULL;
|
|---|
| 472 | int i;
|
|---|
| 473 | PyObject* py_vector2=NULL;
|
|---|
| 474 |
|
|---|
| 475 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 476 | /*retrieve dimensions: */
|
|---|
| 477 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 478 | if (ndim==1) {
|
|---|
| 479 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 480 | M=dims[0];
|
|---|
| 481 | }
|
|---|
| 482 | else if (ndim==2) {
|
|---|
| 483 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 484 | if (dims[1]==1)
|
|---|
| 485 | M=dims[0];
|
|---|
| 486 | else
|
|---|
| 487 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 488 | }
|
|---|
| 489 | else
|
|---|
| 490 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 491 |
|
|---|
| 492 | if (M) {
|
|---|
| 493 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
|---|
| 494 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
|
|---|
| 495 | py_vector=py_vector2;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 499 | /*retrieve internal value: */
|
|---|
| 500 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 501 |
|
|---|
| 502 | /*copy vector: */
|
|---|
| 503 | vector=xNew<double>(M);
|
|---|
| 504 | memcpy(vector,dvector,(M)*sizeof(double));
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
|---|
| 508 | /*retrieve internal value: */
|
|---|
| 509 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 510 |
|
|---|
| 511 | /*transform into double vector: */
|
|---|
| 512 | vector=xNew<double>(M);
|
|---|
| 513 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 517 | /*retrieve internal value: */
|
|---|
| 518 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 519 |
|
|---|
| 520 | /*transform into double vector: */
|
|---|
| 521 | vector=xNew<double>(M);
|
|---|
| 522 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | else
|
|---|
| 526 | _error_("unrecognized double pyarray type in input!");
|
|---|
| 527 |
|
|---|
| 528 | /* Causing a seg fault.
|
|---|
| 529 | if (py_vector2)
|
|---|
| 530 | delete(py_vector2);
|
|---|
| 531 | */
|
|---|
| 532 | }
|
|---|
| 533 | else
|
|---|
| 534 | vector=NULL;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | else {
|
|---|
| 538 | M=1;
|
|---|
| 539 | vector=xNew<double>(M);
|
|---|
| 540 | FetchData(&(vector[0]),py_vector);
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /*output: */
|
|---|
| 544 | if(pM)*pM=M;
|
|---|
| 545 | if(pvector)*pvector=vector;
|
|---|
| 546 | }
|
|---|
| 547 | /*}}}*/
|
|---|
| 548 | /*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 549 | void FetchData(float** pvector,int* pM,PyObject* py_vector){
|
|---|
| 550 |
|
|---|
| 551 | /*output: */
|
|---|
| 552 | float* vector=NULL;
|
|---|
| 553 | int M=0;
|
|---|
| 554 | int ndim;
|
|---|
| 555 | npy_intp* dims=NULL;
|
|---|
| 556 |
|
|---|
| 557 | /*intermediary:*/
|
|---|
| 558 | long* lvector=NULL;
|
|---|
| 559 | bool* bvector=NULL;
|
|---|
| 560 | double* dvector=NULL;
|
|---|
| 561 | int i;
|
|---|
| 562 | PyObject* py_vector2=NULL;
|
|---|
| 563 |
|
|---|
| 564 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 565 | /*retrieve dimensions: */
|
|---|
| 566 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 567 | if (ndim==1) {
|
|---|
| 568 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 569 | M=dims[0];
|
|---|
| 570 | }
|
|---|
| 571 | else if (ndim==2) {
|
|---|
| 572 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 573 | if (dims[1]==1)
|
|---|
| 574 | M=dims[0];
|
|---|
| 575 | else
|
|---|
| 576 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 577 | }
|
|---|
| 578 | else
|
|---|
| 579 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 580 |
|
|---|
| 581 | if (M) {
|
|---|
| 582 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
|---|
| 583 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
|---|
| 584 | py_vector=py_vector2;
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 588 | /*retrieve internal value: */
|
|---|
| 589 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 590 |
|
|---|
| 591 | /*transform into int vector: */
|
|---|
| 592 | vector=xNew<float>(M);
|
|---|
| 593 | for(i=0;i<M;i++)vector[i]=(float)dvector[i];
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
|---|
| 597 | /*retrieve internal value: */
|
|---|
| 598 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 599 |
|
|---|
| 600 | /*transform into int vector: */
|
|---|
| 601 | vector=xNew<float>(M);
|
|---|
| 602 | for(i=0;i<M;i++)vector[i]=(float)lvector[i];
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 606 | /*retrieve internal value: */
|
|---|
| 607 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 608 |
|
|---|
| 609 | /*transform into int vector: */
|
|---|
| 610 | vector=xNew<float>(M);
|
|---|
| 611 | for(i=0;i<M;i++)vector[i]=(float)bvector[i];
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 | else
|
|---|
| 615 | _error_("unrecognized int pyarray type in input!");
|
|---|
| 616 |
|
|---|
| 617 | if(py_vector2) delete(py_vector2);
|
|---|
| 618 | }
|
|---|
| 619 | else
|
|---|
| 620 | vector=NULL;
|
|---|
| 621 | }
|
|---|
| 622 | else{
|
|---|
| 623 | M=1;
|
|---|
| 624 | vector=xNew<float>(M);
|
|---|
| 625 | FetchData(&(vector[0]),py_vector);
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | /*output: */
|
|---|
| 629 | if(pM)*pM=M;
|
|---|
| 630 | if(pvector)*pvector=vector;
|
|---|
| 631 | }
|
|---|
| 632 | /*}}}*/
|
|---|
| 633 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 634 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
|---|
| 635 |
|
|---|
| 636 | /*output: */
|
|---|
| 637 | int* vector=NULL;
|
|---|
| 638 | int M=0;
|
|---|
| 639 | int ndim;
|
|---|
| 640 | npy_intp* dims=NULL;
|
|---|
| 641 |
|
|---|
| 642 | /*intermediary:*/
|
|---|
| 643 | long* lvector=NULL;
|
|---|
| 644 | bool* bvector=NULL;
|
|---|
| 645 | double* dvector=NULL;
|
|---|
| 646 | int i;
|
|---|
| 647 | PyObject* py_vector2=NULL;
|
|---|
| 648 |
|
|---|
| 649 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 650 | /*retrieve dimensions: */
|
|---|
| 651 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 652 | if (ndim==1) {
|
|---|
| 653 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 654 | M=dims[0];
|
|---|
| 655 | }
|
|---|
| 656 | else if (ndim==2) {
|
|---|
| 657 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 658 | if (dims[1]==1)
|
|---|
| 659 | M=dims[0];
|
|---|
| 660 | else
|
|---|
| 661 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 662 | }
|
|---|
| 663 | else
|
|---|
| 664 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 665 |
|
|---|
| 666 | if (M) {
|
|---|
| 667 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
|---|
| 668 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
|---|
| 669 | py_vector=py_vector2;
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 673 | /*retrieve internal value: */
|
|---|
| 674 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 675 |
|
|---|
| 676 | /*transform into int vector: */
|
|---|
| 677 | vector=xNew<int>(M);
|
|---|
| 678 | for(i=0;i<M;i++)vector[i]=(int)dvector[i];
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
|---|
| 682 | /*retrieve internal value: */
|
|---|
| 683 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 684 |
|
|---|
| 685 | /*transform into int vector: */
|
|---|
| 686 | vector=xNew<int>(M);
|
|---|
| 687 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
|---|
| 688 | }
|
|---|
| 689 |
|
|---|
| 690 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 691 | /*retrieve internal value: */
|
|---|
| 692 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 693 |
|
|---|
| 694 | /*transform into int vector: */
|
|---|
| 695 | vector=xNew<int>(M);
|
|---|
| 696 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | else
|
|---|
| 700 | _error_("unrecognized int pyarray type in input!");
|
|---|
| 701 |
|
|---|
| 702 | if (py_vector2)
|
|---|
| 703 | delete(py_vector2);
|
|---|
| 704 | }
|
|---|
| 705 | else
|
|---|
| 706 | vector=NULL;
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | else {
|
|---|
| 710 | M=1;
|
|---|
| 711 | vector=xNew<int>(M);
|
|---|
| 712 | FetchData(&(vector[0]),py_vector);
|
|---|
| 713 | }
|
|---|
| 714 |
|
|---|
| 715 | /*output: */
|
|---|
| 716 | if(pM)*pM=M;
|
|---|
| 717 | if(pvector)*pvector=vector;
|
|---|
| 718 | }
|
|---|
| 719 | /*}}}*/
|
|---|
| 720 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
|---|
| 721 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
|---|
| 722 |
|
|---|
| 723 | /*output: */
|
|---|
| 724 | bool* bvector=NULL;
|
|---|
| 725 | bool* vector=NULL;
|
|---|
| 726 | int M=0;
|
|---|
| 727 | int ndim;
|
|---|
| 728 | npy_intp* dims=NULL;
|
|---|
| 729 |
|
|---|
| 730 | /*intermediary:*/
|
|---|
| 731 | double* dvector=NULL;
|
|---|
| 732 | long* lvector=NULL;
|
|---|
| 733 | int i;
|
|---|
| 734 | PyObject* py_vector2=NULL;
|
|---|
| 735 |
|
|---|
| 736 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
|---|
| 737 | /*retrieve dimensions: */
|
|---|
| 738 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
|---|
| 739 | if (ndim==1) {
|
|---|
| 740 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 741 | M=dims[0];
|
|---|
| 742 | }
|
|---|
| 743 | else if (ndim==2) {
|
|---|
| 744 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
|---|
| 745 | if (dims[1]==1)
|
|---|
| 746 | M=dims[0];
|
|---|
| 747 | else
|
|---|
| 748 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 749 | }
|
|---|
| 750 | else
|
|---|
| 751 | _error_("expecting an Mx1 matrix or M vector in input!");
|
|---|
| 752 |
|
|---|
| 753 | if (M) {
|
|---|
| 754 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
|---|
| 755 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
|
|---|
| 756 | py_vector=py_vector2;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
|---|
| 760 | /*retrieve internal value: */
|
|---|
| 761 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 762 |
|
|---|
| 763 | /*transform into bool vector: */
|
|---|
| 764 | vector=xNew<bool>(M);
|
|---|
| 765 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
|---|
| 769 | /*retrieve internal value: */
|
|---|
| 770 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 771 |
|
|---|
| 772 | /*transform into bool vector: */
|
|---|
| 773 | vector=xNew<bool>(M);
|
|---|
| 774 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
|---|
| 775 | }
|
|---|
| 776 |
|
|---|
| 777 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
|---|
| 778 | /*retrieve internal value: */
|
|---|
| 779 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
|---|
| 780 |
|
|---|
| 781 | /*copy vector: */
|
|---|
| 782 | vector=xNew<bool>(M);
|
|---|
| 783 | memcpy(vector,bvector,(M)*sizeof(bool));
|
|---|
| 784 | }
|
|---|
| 785 |
|
|---|
| 786 | else
|
|---|
| 787 | _error_("unrecognized bool pyarray type in input!");
|
|---|
| 788 |
|
|---|
| 789 | if (py_vector2)
|
|---|
| 790 | delete(py_vector2);
|
|---|
| 791 | }
|
|---|
| 792 | else
|
|---|
| 793 | vector=NULL;
|
|---|
| 794 | }
|
|---|
| 795 |
|
|---|
| 796 | else {
|
|---|
| 797 | M=1;
|
|---|
| 798 | vector=xNew<bool>(M);
|
|---|
| 799 | FetchData(&(vector[0]),py_vector);
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | /*output: */
|
|---|
| 803 | if(pM)*pM=M;
|
|---|
| 804 | if(pvector)*pvector=vector;
|
|---|
| 805 | }
|
|---|
| 806 | /*}}}*/
|
|---|
| 807 |
|
|---|
| 808 | /*ISSM objects*/
|
|---|
| 809 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
|---|
| 810 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
|---|
| 811 |
|
|---|
| 812 | /*Initialize output*/
|
|---|
| 813 | BamgGeom* bamggeom=new BamgGeom();
|
|---|
| 814 |
|
|---|
| 815 | /*Fetch all fields*/
|
|---|
| 816 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
|---|
| 817 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
|---|
| 818 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
|---|
| 819 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
|---|
| 820 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
|---|
| 821 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
|---|
| 822 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
|---|
| 823 |
|
|---|
| 824 | /*Assign output pointers:*/
|
|---|
| 825 | *pbamggeom=bamggeom;
|
|---|
| 826 | }
|
|---|
| 827 | /*}}}*/
|
|---|
| 828 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
|---|
| 829 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
|---|
| 830 |
|
|---|
| 831 | /*Initialize output*/
|
|---|
| 832 | BamgMesh* bamgmesh=new BamgMesh();
|
|---|
| 833 |
|
|---|
| 834 | /*Fetch all fields*/
|
|---|
| 835 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
|---|
| 836 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
|---|
| 837 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
|---|
| 838 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
|---|
| 839 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
|---|
| 840 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
|---|
| 841 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
|---|
| 842 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
|---|
| 843 |
|
|---|
| 844 | /*Assign output pointers:*/
|
|---|
| 845 | *pbamgmesh=bamgmesh;
|
|---|
| 846 | }
|
|---|
| 847 | /*}}}*/
|
|---|
| 848 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
|---|
| 849 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
|---|
| 850 |
|
|---|
| 851 | /*Initialize output*/
|
|---|
| 852 | BamgOpts* bamgopts=new BamgOpts();
|
|---|
| 853 |
|
|---|
| 854 | /*Fetch all fields*/
|
|---|
| 855 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
|---|
| 856 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
|---|
| 857 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
|---|
| 858 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
|---|
| 859 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
|---|
| 860 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
|---|
| 861 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
|---|
| 862 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
|---|
| 863 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
|---|
| 864 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
|---|
| 865 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
|---|
| 866 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
|---|
| 867 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
|---|
| 868 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
|---|
| 869 |
|
|---|
| 870 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
|---|
| 871 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
|---|
| 872 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
|---|
| 873 |
|
|---|
| 874 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
|---|
| 875 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
|---|
| 876 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
|---|
| 877 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
|---|
| 878 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
|
|---|
| 879 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
|---|
| 880 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
|---|
| 881 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
|---|
| 882 |
|
|---|
| 883 | /*Additional checks*/
|
|---|
| 884 | bamgopts->Check();
|
|---|
| 885 |
|
|---|
| 886 | /*Assign output pointers:*/
|
|---|
| 887 | *pbamgopts=bamgopts;
|
|---|
| 888 | }
|
|---|
| 889 | /*}}}*/
|
|---|
| 890 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
|---|
| 891 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
|---|
| 892 |
|
|---|
| 893 | char *name = NULL;
|
|---|
| 894 | Option *option = NULL;
|
|---|
| 895 |
|
|---|
| 896 | /*Initialize output*/
|
|---|
| 897 | Options* options=new Options();
|
|---|
| 898 |
|
|---|
| 899 | /*Fetch all options*/
|
|---|
| 900 | for (int i=istart; i<nrhs; i=i+2){
|
|---|
| 901 | #if _PYTHON_MAJOR_ >= 3
|
|---|
| 902 | if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
|---|
| 903 | #else
|
|---|
| 904 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
|---|
| 905 | #endif
|
|---|
| 906 |
|
|---|
| 907 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
|---|
| 908 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
|---|
| 909 |
|
|---|
| 910 | _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
|
|---|
| 911 |
|
|---|
| 912 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
|---|
| 913 | // options->AddOption(option);
|
|---|
| 914 | // option=NULL;
|
|---|
| 915 | }
|
|---|
| 916 |
|
|---|
| 917 | /*Assign output pointers:*/
|
|---|
| 918 | *poptions=options;
|
|---|
| 919 | }
|
|---|
| 920 | /*}}}*/
|
|---|
| 921 | /*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
|
|---|
| 922 | void FetchData(Contours** pcontours,PyObject* py_list){
|
|---|
| 923 |
|
|---|
| 924 | int numcontours,test1,test2;
|
|---|
| 925 | char *contourname = NULL;
|
|---|
| 926 | Contours *contours = NULL;
|
|---|
| 927 | Contour<double> *contouri = NULL;
|
|---|
| 928 | PyObject *py_dicti = NULL;
|
|---|
| 929 | PyObject *py_item = NULL;
|
|---|
| 930 |
|
|---|
| 931 | #if _PYTHON_MAJOR_ >= 3
|
|---|
| 932 | if (PyUnicode_Check(py_list)){
|
|---|
| 933 | FetchData(&contourname,py_list);
|
|---|
| 934 | contours=ExpRead<double>(contourname);
|
|---|
| 935 | }
|
|---|
| 936 | #else
|
|---|
| 937 | if (PyString_Check(py_list)){
|
|---|
| 938 | FetchData(&contourname,py_list);
|
|---|
| 939 | contours=ExpRead<double>(contourname);
|
|---|
| 940 | }
|
|---|
| 941 | #endif
|
|---|
| 942 | else if(PyList_Check(py_list)){
|
|---|
| 943 |
|
|---|
| 944 | contours=new Contours();
|
|---|
| 945 | numcontours=(int)PyList_Size(py_list);
|
|---|
| 946 |
|
|---|
| 947 | for(int i=0;i<numcontours;i++){
|
|---|
| 948 |
|
|---|
| 949 | contouri=new Contour<double>();
|
|---|
| 950 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
|---|
| 951 |
|
|---|
| 952 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
|---|
| 953 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
|---|
| 954 | FetchData(&contouri->nods,py_item);
|
|---|
| 955 |
|
|---|
| 956 | py_item = PyDict_GetItemString(py_dicti,"x");
|
|---|
| 957 | if(!py_item) _error_("input structure does not have a 'x' field");
|
|---|
| 958 | FetchData(&contouri->x,&test1,&test2,py_item);
|
|---|
| 959 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
|---|
| 960 |
|
|---|
| 961 | py_item = PyDict_GetItemString(py_dicti,"y");
|
|---|
| 962 | if(!py_item) _error_("input structure does not have a 'y' field");
|
|---|
| 963 | FetchData(&contouri->y,&test1,&test2,py_item);
|
|---|
| 964 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
|---|
| 965 |
|
|---|
| 966 | contours->AddObject(contouri);
|
|---|
| 967 | }
|
|---|
| 968 | }
|
|---|
| 969 | else{
|
|---|
| 970 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | /*clean-up and assign output pointer*/
|
|---|
| 974 | xDelete<char>(contourname);
|
|---|
| 975 | *pcontours=contours;
|
|---|
| 976 | }
|
|---|
| 977 |
|
|---|
| 978 | void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
|
|---|
| 979 |
|
|---|
| 980 | double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
|
|---|
| 981 | int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
|
|---|
| 982 | int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
|
|---|
| 983 |
|
|---|
| 984 | /*Fetch adjacency matrix:*/
|
|---|
| 985 | int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
|
|---|
| 986 |
|
|---|
| 987 | int* mwstart = xNewZeroInit<int>(nvtxs+1);
|
|---|
| 988 | pyGetJc(a,nvtxs,mwstart);
|
|---|
| 989 |
|
|---|
| 990 | int* mwadjacency = xNewZeroInit<int>(nzmax);
|
|---|
| 991 | pyGetIr(a,nvtxs,nzmax,mwadjacency);
|
|---|
| 992 |
|
|---|
| 993 | int* start = xNew<int>(nvtxs+1);
|
|---|
| 994 | for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
|
|---|
| 995 | int* adjacency = xNew<int>(nzmax);
|
|---|
| 996 | for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
|
|---|
| 997 |
|
|---|
| 998 | /*Get edges weights*/
|
|---|
| 999 | int nedges = start[nvtxs];
|
|---|
| 1000 | float* ewgts = NULL;
|
|---|
| 1001 | int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
|
|---|
| 1002 | //size may be 1 and still be empty;
|
|---|
| 1003 | //presumably size of edge_weights input will never be exactly 1
|
|---|
| 1004 | if(size != 1 && size != 0){
|
|---|
| 1005 | ewgts = xNewZeroInit<float>(nedges);
|
|---|
| 1006 | for(int i = 0; i<nedges;i++){
|
|---|
| 1007 | ewgts[i] = (float)a[i];
|
|---|
| 1008 | }
|
|---|
| 1009 | }
|
|---|
| 1010 |
|
|---|
| 1011 | /*Assign output pointers*/
|
|---|
| 1012 | *pnvtxs = nvtxs;
|
|---|
| 1013 | *padjacency = adjacency;
|
|---|
| 1014 | *pstart = start;
|
|---|
| 1015 | *pewgts = ewgts;
|
|---|
| 1016 | }
|
|---|
| 1017 | /*}}}*/
|
|---|
| 1018 |
|
|---|
| 1019 | void pyGetJc(double* a, int nvtxs, int* Jc){
|
|---|
| 1020 | /*{{{*/
|
|---|
| 1021 | //get the number of non-zero elements in each row, adding to the prior number;
|
|---|
| 1022 | //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
|
|---|
| 1023 | // eg: 0, 1, 3, 6, 8, 13, ...
|
|---|
| 1024 | // for: 1 in the first row, 2 in the second, 3 in the third, etc.
|
|---|
| 1025 | int c = 0;
|
|---|
| 1026 | Jc[c++] = 0;
|
|---|
| 1027 |
|
|---|
| 1028 | for(int i=0;i<nvtxs;i++){
|
|---|
| 1029 | for(int j=0;j<nvtxs;j++){
|
|---|
| 1030 | if ((int)a[i+(j*nvtxs)] != 0){
|
|---|
| 1031 | Jc[c] += 1;
|
|---|
| 1032 | }
|
|---|
| 1033 | }
|
|---|
| 1034 | c++;
|
|---|
| 1035 | Jc[c] = Jc[c-1];
|
|---|
| 1036 | }
|
|---|
| 1037 | return;
|
|---|
| 1038 | }
|
|---|
| 1039 | /*}}}*/
|
|---|
| 1040 |
|
|---|
| 1041 | void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
|
|---|
| 1042 | /*{{{*/
|
|---|
| 1043 | //get the row-wise position of each non-zero element;
|
|---|
| 1044 | //has length: number_of_non_zero_elements_in(a) == nzmax
|
|---|
| 1045 | // eg: 10, 24, 25, 4, ...
|
|---|
| 1046 | // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
|
|---|
| 1047 | // using pyGetJc to determine which indices correspond to which row
|
|---|
| 1048 | int r = 0;
|
|---|
| 1049 |
|
|---|
| 1050 | for(int i=0;i<nvtxs;i++){
|
|---|
| 1051 | for(int j=0;j<nvtxs;j++){
|
|---|
| 1052 | if ((int)a[i+(j*nvtxs)] != 0){
|
|---|
| 1053 | Ir[r] = j;
|
|---|
| 1054 | r++;
|
|---|
| 1055 | }
|
|---|
| 1056 | }
|
|---|
| 1057 | }
|
|---|
| 1058 | return;
|
|---|
| 1059 | }
|
|---|
| 1060 | /*}}}*/
|
|---|
| 1061 |
|
|---|
| 1062 | /*Python version dependent: */
|
|---|
| 1063 | /* #if _PYTHON_MAJOR_ >= 3 */
|
|---|
| 1064 | /* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
|
|---|
| 1065 | /* void FetchData(char** pstring,PyObject* py_unicode){ */
|
|---|
| 1066 |
|
|---|
| 1067 | /* PyObject* py_bytes; */
|
|---|
| 1068 | /* char* string=NULL; */
|
|---|
| 1069 |
|
|---|
| 1070 | /* /\*convert to bytes format: *\/ */
|
|---|
| 1071 | /* PyUnicode_FSConverter(py_unicode,&py_bytes); */
|
|---|
| 1072 |
|
|---|
| 1073 | /* /\*convert from bytes to string: *\/ */
|
|---|
| 1074 | /* string=PyBytes_AsUTF8(py_bytes); */
|
|---|
| 1075 |
|
|---|
| 1076 | /* /\*copy string (note strlen does not include trailing NULL): *\/ */
|
|---|
| 1077 | /* *pstring=xNew<char>(strlen(string)+1); */
|
|---|
| 1078 | /* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
|
|---|
| 1079 | /* } */
|
|---|
| 1080 | /* /\*}}}*\/ */
|
|---|
| 1081 | /* #else */
|
|---|
| 1082 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
|---|
| 1083 | void FetchData(char** pstring,PyObject* py_string){
|
|---|
| 1084 |
|
|---|
| 1085 | char* string=NULL;
|
|---|
| 1086 |
|
|---|
| 1087 | /*extract internal string: */
|
|---|
| 1088 | #if _PYTHON_MAJOR_ == 3
|
|---|
| 1089 | string=PyUnicode_AsUTF8(py_string);
|
|---|
| 1090 | #else
|
|---|
| 1091 | string=PyString_AsString(py_string);
|
|---|
| 1092 | #endif
|
|---|
| 1093 | /*copy string (note strlen does not include trailing NULL): */
|
|---|
| 1094 | *pstring=xNew<char>(strlen(string)+1);
|
|---|
| 1095 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
|---|
| 1096 | }
|
|---|
| 1097 | /*}}}*/
|
|---|
| 1098 | //#endif
|
|---|