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