[9e531f2] | 1 | /** |
---|
| 2 | SLD2I module to perform point and I calculations |
---|
| 3 | */ |
---|
| 4 | #include <Python.h> |
---|
| 5 | #include <stdio.h> |
---|
[3010f68] | 6 | #include <sld2i.h> |
---|
[9e531f2] | 7 | |
---|
[d04ac05] | 8 | #if PY_MAJOR_VERSION < 3 |
---|
| 9 | typedef void (*PyCapsule_Destructor)(PyObject *); |
---|
| 10 | typedef void (*PyCObject_Destructor)(void *); |
---|
| 11 | #define PyCapsule_New(pointer, name, destructor) (PyCObject_FromVoidPtr(pointer, (PyCObject_Destructor)destructor)) |
---|
| 12 | #define PyCapsule_GetPointer(capsule, name) (PyCObject_AsVoidPtr(capsule)) |
---|
| 13 | #endif |
---|
| 14 | |
---|
| 15 | |
---|
[9e531f2] | 16 | // Utilities |
---|
| 17 | #define INVECTOR(obj,buf,len) \ |
---|
| 18 | do { \ |
---|
| 19 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 20 | if (err < 0) return NULL; \ |
---|
| 21 | len /= sizeof(*buf); \ |
---|
| 22 | } while (0) |
---|
| 23 | |
---|
| 24 | #define OUTVECTOR(obj,buf,len) \ |
---|
| 25 | do { \ |
---|
| 26 | int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \ |
---|
| 27 | if (err < 0) return NULL; \ |
---|
| 28 | len /= sizeof(*buf); \ |
---|
| 29 | } while (0) |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Delete a GenI object |
---|
| 34 | */ |
---|
[d04ac05] | 35 | void |
---|
| 36 | del_sld2i(PyObject *obj){ |
---|
[3010f68] | 37 | GenI* sld2i = (GenI *)(PyCapsule_GetPointer(obj, "GenI")); |
---|
| 38 | PyMem_Free((void *)sld2i); |
---|
[9e531f2] | 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Create a GenI as a python object by supplying arrays |
---|
| 43 | */ |
---|
[3010f68] | 44 | PyObject * new_GenI(PyObject *self, PyObject *args) { |
---|
[9e531f2] | 45 | PyObject *x_val_obj; |
---|
| 46 | PyObject *y_val_obj; |
---|
| 47 | PyObject *z_val_obj; |
---|
| 48 | PyObject *sldn_val_obj; |
---|
| 49 | PyObject *mx_val_obj; |
---|
| 50 | PyObject *my_val_obj; |
---|
| 51 | PyObject *mz_val_obj; |
---|
| 52 | PyObject *vol_pix_obj; |
---|
| 53 | Py_ssize_t n_x; |
---|
| 54 | //PyObject rlimit_obj; |
---|
| 55 | //PyObject npoints_obj; |
---|
| 56 | //PyObject nrbins_obj; |
---|
| 57 | //PyObject nphibins_obj; |
---|
| 58 | int n_pix; |
---|
| 59 | double* x_val; |
---|
| 60 | double* y_val; |
---|
| 61 | double* z_val; |
---|
| 62 | double* sldn_val; |
---|
| 63 | double* mx_val; |
---|
| 64 | double* my_val; |
---|
| 65 | double* mz_val; |
---|
| 66 | double* vol_pix; |
---|
| 67 | double inspin; |
---|
| 68 | double outspin; |
---|
| 69 | double stheta; |
---|
| 70 | |
---|
| 71 | 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; |
---|
| 72 | OUTVECTOR(x_val_obj, x_val, n_x); |
---|
| 73 | OUTVECTOR(y_val_obj, y_val, n_x); |
---|
| 74 | OUTVECTOR(z_val_obj, z_val, n_x); |
---|
| 75 | OUTVECTOR(sldn_val_obj, sldn_val, n_x); |
---|
| 76 | OUTVECTOR(mx_val_obj, mx_val, n_x); |
---|
| 77 | OUTVECTOR(my_val_obj, my_val, n_x); |
---|
| 78 | OUTVECTOR(mz_val_obj, mz_val, n_x); |
---|
| 79 | OUTVECTOR(vol_pix_obj, vol_pix, n_x); |
---|
[3010f68] | 80 | GenI* sld2i = PyMem_Malloc(sizeof(GenI)); |
---|
| 81 | if (sld2i != NULL) { |
---|
| 82 | initGenI(sld2i, n_pix,x_val,y_val,z_val,sldn_val,mx_val,my_val,mz_val,vol_pix,inspin,outspin,stheta); |
---|
| 83 | } |
---|
[d04ac05] | 84 | return PyCapsule_New(sld2i, "GenI", del_sld2i); |
---|
[9e531f2] | 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * GenI the given input (2D) according to a given object |
---|
| 89 | */ |
---|
[3010f68] | 90 | PyObject * genicom_inputXY(PyObject *self, PyObject *args) { |
---|
[9e531f2] | 91 | int npoints; |
---|
| 92 | PyObject *qx_obj; |
---|
| 93 | double *qx; |
---|
| 94 | PyObject *qy_obj; |
---|
| 95 | double *qy; |
---|
| 96 | PyObject *I_out_obj; |
---|
| 97 | Py_ssize_t n_out; |
---|
| 98 | double *I_out; |
---|
| 99 | PyObject *gen_obj; |
---|
| 100 | |
---|
| 101 | if (!PyArg_ParseTuple(args, "OiOOO", &gen_obj, &npoints, &qx_obj, &qy_obj, &I_out_obj)) return NULL; |
---|
| 102 | OUTVECTOR(qx_obj, qx, n_out); |
---|
| 103 | OUTVECTOR(qy_obj, qy, n_out); |
---|
| 104 | OUTVECTOR(I_out_obj, I_out, n_out); |
---|
| 105 | |
---|
| 106 | // Sanity check |
---|
| 107 | //if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
| 108 | |
---|
| 109 | // Set the array pointers |
---|
[3010f68] | 110 | GenI* sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI"); |
---|
[9e531f2] | 111 | |
---|
[3010f68] | 112 | genicomXY(sld2i, npoints, qx, qy, I_out); |
---|
[9e531f2] | 113 | //return PyCObject_FromVoidPtr(s, del_genicom); |
---|
| 114 | return Py_BuildValue("i",1); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * GenI the given 1D input according to a given object |
---|
| 119 | */ |
---|
[3010f68] | 120 | PyObject * genicom_input(PyObject *self, PyObject *args) { |
---|
[9e531f2] | 121 | int npoints; |
---|
| 122 | PyObject *q_obj; |
---|
| 123 | double *q; |
---|
| 124 | PyObject *I_out_obj; |
---|
| 125 | Py_ssize_t n_out; |
---|
| 126 | double *I_out; |
---|
| 127 | PyObject *gen_obj; |
---|
| 128 | |
---|
| 129 | if (!PyArg_ParseTuple(args, "OiOO", &gen_obj, &npoints, &q_obj, &I_out_obj)) return NULL; |
---|
| 130 | OUTVECTOR(q_obj, q, n_out); |
---|
| 131 | OUTVECTOR(I_out_obj, I_out, n_out); |
---|
| 132 | |
---|
| 133 | // Sanity check |
---|
| 134 | //if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
| 135 | |
---|
| 136 | // Set the array pointers |
---|
[3010f68] | 137 | GenI *sld2i = (GenI *)PyCapsule_GetPointer(gen_obj, "GenI"); |
---|
[9e531f2] | 138 | |
---|
[3010f68] | 139 | genicom(sld2i, npoints, q, I_out); |
---|
[9e531f2] | 140 | //return PyCObject_FromVoidPtr(s, del_genicom); |
---|
| 141 | return Py_BuildValue("i",1); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * Define module methods |
---|
| 146 | */ |
---|
| 147 | static PyMethodDef module_methods[] = { |
---|
| 148 | {"new_GenI", (PyCFunction)new_GenI, METH_VARARGS, |
---|
| 149 | "Create a new GenI object"}, |
---|
| 150 | {"genicom",(PyCFunction)genicom_input, METH_VARARGS, |
---|
| 151 | "genicom the given 1d input arrays"}, |
---|
| 152 | {"genicomXY",(PyCFunction)genicom_inputXY, METH_VARARGS, |
---|
| 153 | "genicomXY the given 2d input arrays"}, |
---|
| 154 | {NULL} |
---|
| 155 | }; |
---|
| 156 | |
---|
[d04ac05] | 157 | #define MODULE_DOC "Sld2i C Library" |
---|
| 158 | #define MODULE_NAME "sld2i" |
---|
| 159 | #define MODULE_INIT2 initsld2i |
---|
[1d014cb] | 160 | #define MODULE_INIT3 PyInit_sld2i |
---|
[d04ac05] | 161 | #define MODULE_METHODS module_methods |
---|
| 162 | |
---|
| 163 | /* ==== boilerplate python 2/3 interface bootstrap ==== */ |
---|
[9e531f2] | 164 | |
---|
[d04ac05] | 165 | |
---|
| 166 | #if defined(WIN32) && !defined(__MINGW32__) |
---|
| 167 | #define DLL_EXPORT __declspec(dllexport) |
---|
| 168 | #else |
---|
| 169 | #define DLL_EXPORT |
---|
[9e531f2] | 170 | #endif |
---|
[d04ac05] | 171 | |
---|
| 172 | #if PY_MAJOR_VERSION >= 3 |
---|
| 173 | |
---|
| 174 | DLL_EXPORT PyMODINIT_FUNC MODULE_INIT3(void) |
---|
| 175 | { |
---|
| 176 | static struct PyModuleDef moduledef = { |
---|
| 177 | PyModuleDef_HEAD_INIT, |
---|
| 178 | MODULE_NAME, /* m_name */ |
---|
| 179 | MODULE_DOC, /* m_doc */ |
---|
| 180 | -1, /* m_size */ |
---|
| 181 | MODULE_METHODS, /* m_methods */ |
---|
| 182 | NULL, /* m_reload */ |
---|
| 183 | NULL, /* m_traverse */ |
---|
| 184 | NULL, /* m_clear */ |
---|
| 185 | NULL, /* m_free */ |
---|
| 186 | }; |
---|
| 187 | return PyModule_Create(&moduledef); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | #else /* !PY_MAJOR_VERSION >= 3 */ |
---|
| 191 | |
---|
| 192 | DLL_EXPORT PyMODINIT_FUNC MODULE_INIT2(void) |
---|
| 193 | { |
---|
| 194 | Py_InitModule4(MODULE_NAME, |
---|
| 195 | MODULE_METHODS, |
---|
| 196 | MODULE_DOC, |
---|
| 197 | 0, |
---|
| 198 | PYTHON_API_VERSION |
---|
| 199 | ); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | #endif /* !PY_MAJOR_VERSION >= 3 */ |
---|