[318b5bbb] | 1 | /** |
---|
| 2 | SLD2I module to perform point and I calculations |
---|
| 3 | */ |
---|
| 4 | #include <Python.h> |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <sld2i.hh> |
---|
| 7 | |
---|
| 8 | // Utilities |
---|
| 9 | #define INVECTOR(obj,buf,len) \ |
---|
| 10 | do { \ |
---|
| 11 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 12 | if (err < 0) return NULL; \ |
---|
| 13 | len /= sizeof(*buf); \ |
---|
| 14 | } while (0) |
---|
| 15 | |
---|
| 16 | #define OUTVECTOR(obj,buf,len) \ |
---|
| 17 | do { \ |
---|
| 18 | int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \ |
---|
| 19 | if (err < 0) return NULL; \ |
---|
| 20 | len /= sizeof(*buf); \ |
---|
| 21 | } while (0) |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * Delete a GenI object |
---|
| 26 | */ |
---|
| 27 | void del_sld2i(void *ptr){ |
---|
| 28 | GenI* sld2i = static_cast<GenI *>(ptr); |
---|
| 29 | delete sld2i; |
---|
| 30 | return; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Create a GenI as a python object by supplying arrays |
---|
| 35 | */ |
---|
| 36 | PyObject * new_GenI(PyObject *, PyObject *args) { |
---|
| 37 | PyObject *x_val_obj; |
---|
| 38 | PyObject *y_val_obj; |
---|
| 39 | PyObject *z_val_obj; |
---|
| 40 | PyObject *sldn_val_obj; |
---|
| 41 | PyObject *mx_val_obj; |
---|
| 42 | PyObject *my_val_obj; |
---|
| 43 | PyObject *mz_val_obj; |
---|
[b1174ec] | 44 | PyObject *vol_pix_obj; |
---|
[318b5bbb] | 45 | Py_ssize_t n_x; |
---|
| 46 | //PyObject rlimit_obj; |
---|
| 47 | //PyObject npoints_obj; |
---|
| 48 | //PyObject nrbins_obj; |
---|
| 49 | //PyObject nphibins_obj; |
---|
| 50 | int n_pix; |
---|
| 51 | double* x_val; |
---|
| 52 | double* y_val; |
---|
| 53 | double* z_val; |
---|
| 54 | double* sldn_val; |
---|
| 55 | double* mx_val; |
---|
| 56 | double* my_val; |
---|
| 57 | double* mz_val; |
---|
[b1174ec] | 58 | double* vol_pix; |
---|
[318b5bbb] | 59 | double inspin; |
---|
| 60 | double outspin; |
---|
| 61 | double stheta; |
---|
| 62 | |
---|
[b1174ec] | 63 | if (!PyArg_ParseTuple(args, "iOOOOOOOOddd", &n_pix, &x_val_obj, &y_val_obj, &z_val_obj, &sldn_val_obj, &mx_val_obj, &my_val_obj, &mz_val_obj, &vol_pix_obj, &inspin, &outspin, &stheta)) return NULL; |
---|
[318b5bbb] | 64 | OUTVECTOR(x_val_obj, x_val, n_x); |
---|
| 65 | OUTVECTOR(y_val_obj, y_val, n_x); |
---|
| 66 | OUTVECTOR(z_val_obj, z_val, n_x); |
---|
| 67 | OUTVECTOR(sldn_val_obj, sldn_val, n_x); |
---|
| 68 | OUTVECTOR(mx_val_obj, mx_val, n_x); |
---|
| 69 | OUTVECTOR(my_val_obj, my_val, n_x); |
---|
| 70 | OUTVECTOR(mz_val_obj, mz_val, n_x); |
---|
[b1174ec] | 71 | OUTVECTOR(vol_pix_obj, vol_pix, n_x); |
---|
| 72 | GenI* sld2i = new GenI(n_pix,x_val,y_val,z_val,sldn_val,mx_val,my_val,mz_val,vol_pix,inspin,outspin,stheta); |
---|
[318b5bbb] | 73 | return PyCObject_FromVoidPtr(sld2i, del_sld2i); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
[9624cda] | 77 | * GenI the given input (2D) according to a given object |
---|
[318b5bbb] | 78 | */ |
---|
[9624cda] | 79 | PyObject * genicom_inputXY(PyObject *, PyObject *args) { |
---|
[318b5bbb] | 80 | int npoints; |
---|
| 81 | PyObject *qx_obj; |
---|
| 82 | double *qx; |
---|
| 83 | PyObject *qy_obj; |
---|
| 84 | double *qy; |
---|
| 85 | PyObject *I_out_obj; |
---|
| 86 | Py_ssize_t n_out; |
---|
| 87 | double *I_out; |
---|
| 88 | PyObject *gen_obj; |
---|
| 89 | |
---|
| 90 | if (!PyArg_ParseTuple(args, "OiOOO", &gen_obj, &npoints, &qx_obj, &qy_obj, &I_out_obj)) return NULL; |
---|
| 91 | OUTVECTOR(qx_obj, qx, n_out); |
---|
| 92 | OUTVECTOR(qy_obj, qy, n_out); |
---|
| 93 | OUTVECTOR(I_out_obj, I_out, n_out); |
---|
| 94 | |
---|
| 95 | // Sanity check |
---|
| 96 | //if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
| 97 | |
---|
| 98 | // Set the array pointers |
---|
| 99 | void *temp = PyCObject_AsVoidPtr(gen_obj); |
---|
| 100 | GenI* s = static_cast<GenI *>(temp); |
---|
| 101 | |
---|
[9624cda] | 102 | s->genicomXY(npoints, qx, qy, I_out); |
---|
[318b5bbb] | 103 | //return PyCObject_FromVoidPtr(s, del_genicom); |
---|
| 104 | return Py_BuildValue("i",1); |
---|
| 105 | } |
---|
| 106 | |
---|
[9624cda] | 107 | /** |
---|
| 108 | * GenI the given 1D input according to a given object |
---|
| 109 | */ |
---|
| 110 | PyObject * genicom_input(PyObject *, PyObject *args) { |
---|
| 111 | int npoints; |
---|
| 112 | PyObject *q_obj; |
---|
| 113 | double *q; |
---|
| 114 | PyObject *I_out_obj; |
---|
| 115 | Py_ssize_t n_out; |
---|
| 116 | double *I_out; |
---|
| 117 | PyObject *gen_obj; |
---|
| 118 | |
---|
| 119 | if (!PyArg_ParseTuple(args, "OiOO", &gen_obj, &npoints, &q_obj, &I_out_obj)) return NULL; |
---|
| 120 | OUTVECTOR(q_obj, q, n_out); |
---|
| 121 | OUTVECTOR(I_out_obj, I_out, n_out); |
---|
| 122 | |
---|
| 123 | // Sanity check |
---|
| 124 | //if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
| 125 | |
---|
| 126 | // Set the array pointers |
---|
| 127 | void *temp = PyCObject_AsVoidPtr(gen_obj); |
---|
| 128 | GenI* s = static_cast<GenI *>(temp); |
---|
| 129 | |
---|
| 130 | s->genicom(npoints, q, I_out); |
---|
| 131 | //return PyCObject_FromVoidPtr(s, del_genicom); |
---|
| 132 | return Py_BuildValue("i",1); |
---|
| 133 | } |
---|
[318b5bbb] | 134 | |
---|
| 135 | /** |
---|
| 136 | * Define module methods |
---|
| 137 | */ |
---|
| 138 | static PyMethodDef module_methods[] = { |
---|
| 139 | {"new_GenI", (PyCFunction)new_GenI, METH_VARARGS, |
---|
| 140 | "Create a new GenI object"}, |
---|
| 141 | {"genicom",(PyCFunction)genicom_input, METH_VARARGS, |
---|
[9624cda] | 142 | "genicom the given 1d input arrays"}, |
---|
| 143 | {"genicomXY",(PyCFunction)genicom_inputXY, METH_VARARGS, |
---|
| 144 | "genicomXY the given 2d input arrays"}, |
---|
[318b5bbb] | 145 | {NULL} |
---|
| 146 | }; |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
| 150 | #define PyMODINIT_FUNC void |
---|
| 151 | #endif |
---|
| 152 | PyMODINIT_FUNC |
---|
| 153 | initsld2i(void) |
---|
| 154 | { |
---|
| 155 | PyObject* m; |
---|
| 156 | |
---|
| 157 | m = Py_InitModule3("sld2i", module_methods, |
---|
| 158 | "Sld2i module"); |
---|
| 159 | } |
---|