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 | #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 | |
---|
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 | */ |
---|
35 | void |
---|
36 | del_sld2i(PyObject *obj){ |
---|
37 | GenI* sld2i = static_cast<GenI *>(PyCapsule_GetPointer(obj, "GenI")); |
---|
38 | delete sld2i; |
---|
39 | return; |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Create a GenI as a python object by supplying arrays |
---|
44 | */ |
---|
45 | PyObject * new_GenI(PyObject *, PyObject *args) { |
---|
46 | PyObject *x_val_obj; |
---|
47 | PyObject *y_val_obj; |
---|
48 | PyObject *z_val_obj; |
---|
49 | PyObject *sldn_val_obj; |
---|
50 | PyObject *mx_val_obj; |
---|
51 | PyObject *my_val_obj; |
---|
52 | PyObject *mz_val_obj; |
---|
53 | PyObject *vol_pix_obj; |
---|
54 | Py_ssize_t n_x; |
---|
55 | //PyObject rlimit_obj; |
---|
56 | //PyObject npoints_obj; |
---|
57 | //PyObject nrbins_obj; |
---|
58 | //PyObject nphibins_obj; |
---|
59 | int n_pix; |
---|
60 | double* x_val; |
---|
61 | double* y_val; |
---|
62 | double* z_val; |
---|
63 | double* sldn_val; |
---|
64 | double* mx_val; |
---|
65 | double* my_val; |
---|
66 | double* mz_val; |
---|
67 | double* vol_pix; |
---|
68 | double inspin; |
---|
69 | double outspin; |
---|
70 | double stheta; |
---|
71 | |
---|
72 | 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; |
---|
73 | OUTVECTOR(x_val_obj, x_val, n_x); |
---|
74 | OUTVECTOR(y_val_obj, y_val, n_x); |
---|
75 | OUTVECTOR(z_val_obj, z_val, n_x); |
---|
76 | OUTVECTOR(sldn_val_obj, sldn_val, n_x); |
---|
77 | OUTVECTOR(mx_val_obj, mx_val, n_x); |
---|
78 | OUTVECTOR(my_val_obj, my_val, n_x); |
---|
79 | OUTVECTOR(mz_val_obj, mz_val, n_x); |
---|
80 | OUTVECTOR(vol_pix_obj, vol_pix, n_x); |
---|
81 | 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); |
---|
82 | return PyCapsule_New(sld2i, "GenI", del_sld2i); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * GenI the given input (2D) according to a given object |
---|
87 | */ |
---|
88 | PyObject * genicom_inputXY(PyObject *, PyObject *args) { |
---|
89 | int npoints; |
---|
90 | PyObject *qx_obj; |
---|
91 | double *qx; |
---|
92 | PyObject *qy_obj; |
---|
93 | double *qy; |
---|
94 | PyObject *I_out_obj; |
---|
95 | Py_ssize_t n_out; |
---|
96 | double *I_out; |
---|
97 | PyObject *gen_obj; |
---|
98 | |
---|
99 | if (!PyArg_ParseTuple(args, "OiOOO", &gen_obj, &npoints, &qx_obj, &qy_obj, &I_out_obj)) return NULL; |
---|
100 | OUTVECTOR(qx_obj, qx, n_out); |
---|
101 | OUTVECTOR(qy_obj, qy, n_out); |
---|
102 | OUTVECTOR(I_out_obj, I_out, n_out); |
---|
103 | |
---|
104 | // Sanity check |
---|
105 | //if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
106 | |
---|
107 | // Set the array pointers |
---|
108 | void *temp = PyCapsule_GetPointer(gen_obj, "GenI"); |
---|
109 | GenI* s = static_cast<GenI *>(temp); |
---|
110 | |
---|
111 | s->genicomXY(npoints, qx, qy, I_out); |
---|
112 | //return PyCObject_FromVoidPtr(s, del_genicom); |
---|
113 | return Py_BuildValue("i",1); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * GenI the given 1D input according to a given object |
---|
118 | */ |
---|
119 | PyObject * genicom_input(PyObject *, PyObject *args) { |
---|
120 | int npoints; |
---|
121 | PyObject *q_obj; |
---|
122 | double *q; |
---|
123 | PyObject *I_out_obj; |
---|
124 | Py_ssize_t n_out; |
---|
125 | double *I_out; |
---|
126 | PyObject *gen_obj; |
---|
127 | |
---|
128 | if (!PyArg_ParseTuple(args, "OiOO", &gen_obj, &npoints, &q_obj, &I_out_obj)) return NULL; |
---|
129 | OUTVECTOR(q_obj, q, n_out); |
---|
130 | OUTVECTOR(I_out_obj, I_out, n_out); |
---|
131 | |
---|
132 | // Sanity check |
---|
133 | //if(n_in!=n_out) return Py_BuildValue("i",-1); |
---|
134 | |
---|
135 | // Set the array pointers |
---|
136 | void *temp = PyCapsule_GetPointer(gen_obj, "GenI"); |
---|
137 | GenI* s = static_cast<GenI *>(temp); |
---|
138 | |
---|
139 | s->genicom(npoints, q, I_out); |
---|
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 | |
---|
157 | #define MODULE_DOC "Sld2i C Library" |
---|
158 | #define MODULE_NAME "sld2i" |
---|
159 | #define MODULE_INIT2 initsld2i |
---|
160 | #define MODULE_INIT3 PyInit_sld2i |
---|
161 | #define MODULE_METHODS module_methods |
---|
162 | |
---|
163 | /* ==== boilerplate python 2/3 interface bootstrap ==== */ |
---|
164 | |
---|
165 | |
---|
166 | #if defined(WIN32) && !defined(__MINGW32__) |
---|
167 | #define DLL_EXPORT __declspec(dllexport) |
---|
168 | #else |
---|
169 | #define DLL_EXPORT |
---|
170 | #endif |
---|
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 */ |
---|