source: sasview/src/sas/sascalc/calculator/c_extensions/sld2i_module.cpp @ b523c0e

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since b523c0e was b523c0e, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

remove compiler warnings

  • Property mode set to 100644
File size: 3.9 KB
Line 
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 */
27void 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 */
36PyObject * 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;
44        PyObject *vol_pix_obj;
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;
58        double* vol_pix;
59        double inspin;
60        double outspin;
61        double stheta;
62
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;
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);
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);
73        return PyCObject_FromVoidPtr(sld2i, del_sld2i);
74}
75
76/**
77 * GenI the given input (2D) according to a given object
78 */
79PyObject * genicom_inputXY(PyObject *, PyObject *args) {
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
102        s->genicomXY(npoints, qx, qy, I_out);
103        //return PyCObject_FromVoidPtr(s, del_genicom);
104        return Py_BuildValue("i",1);
105}
106
107/**
108 * GenI the given 1D input according to a given object
109 */
110PyObject * 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}
134
135/**
136 * Define module methods
137 */
138static PyMethodDef module_methods[] = {
139        {"new_GenI", (PyCFunction)new_GenI, METH_VARARGS,
140                  "Create a new GenI object"},
141        {"genicom",(PyCFunction)genicom_input, METH_VARARGS,
142                  "genicom the given 1d input arrays"},
143        {"genicomXY",(PyCFunction)genicom_inputXY, METH_VARARGS,
144                  "genicomXY the given 2d input arrays"},
145    {NULL}
146};
147
148
149#ifndef PyMODINIT_FUNC  /* declarations for DLL import/export */
150#define PyMODINIT_FUNC void
151#endif
152PyMODINIT_FUNC
153initsld2i(void)
154{
155    Py_InitModule3("sld2i", module_methods, "Sld2i module");
156}
Note: See TracBrowser for help on using the repository browser.