File: glArrayTypemap.i

package info (click to toggle)
mgltools-opengltk 1.5.7-1
  • links: PTS, VCS
  • area: non-free
  • in suites: stretch
  • size: 8,592 kB
  • ctags: 38,393
  • sloc: ansic: 98,617; python: 3,818; cpp: 1,943; sh: 1,332; tcl: 1,127; makefile: 65
file content (262 lines) | stat: -rw-r--r-- 6,686 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

%init %{
  import_array(); /* load the Numeric PyCObjects */
%}

%{
int checkArraySize = 1;
int checkArgumentsInCWrapper=1;

#include "numpy/arrayobject.h"

int NumericTypecode(char *type)
{
  /* maps GL types to Numeric types */
  if (strcmp(type,"GLbyte")==0) return NPY_BYTE;
  else if (strcmp(type,"GLdouble")==0) return NPY_DOUBLE;
  else if (strcmp(type,"GLfloat")==0) return NPY_FLOAT;
  else if (strcmp(type,"GLint")==0) return NPY_INT;
  else if (strcmp(type,"GLshort")==0) return NPY_SHORT;
  else if (strcmp(type,"GLubyte")==0) return NPY_UBYTE;
  else if (strcmp(type,"GLuint")==0) return NPY_INT;
  else if (strcmp(type,"GLushort")==0) return NPY_SHORT;
  else if (strcmp(type,"GLboolean")==0) return NPY_UBYTE;
}

/*********************************************
   1. Check if the incomming object is a buffer object.
   2. If buffer - check if it is a numeric array.
   3. If numeric array -  check if it is contiguous - 
        return 1.
   4. If buffer but not a numeric array - return 1.
   5. In all other cases return 0.
*******************************************/
int isContiguosBuffer(PyObject *incommingObject)
{
  int contig;
  /** Check if object is a buffer object **/
  /** if (PyBuffer_Check(incommingObject)) **/
  PyBufferProcs *pb = incommingObject->ob_type->tp_as_buffer;
  if (pb == NULL || pb->bf_getsegcount == NULL )
    contig = 0;
  else
  {
    /** printf ("object is a buffer\n"); **/
    if (PyArray_Check(incommingObject))
    {
      /** printf ("object is a Numeric array\n"); **/
      if (PyArray_ISCONTIGUOUS((PyArrayObject*)incommingObject))
	contig = 1;
      else contig = 0;	
    }
    else
      contig = 1; 
  }
  return contig;
}
/**********************************************************
This function takes a Python object and creates a Numeric Array 
after checking that the incomming object is of an acceptable
type(size).
***********************************************************/

void bufferWithCheck(PyObject *incommingObject, 
		     PyArrayObject **Narray,
                     char *type, int nbElem)
{
  char buf[255];
  int typecode, size, i;
  /**PyArrayObject *Narray; */
  /* make contiguous if needed */
  typecode = NumericTypecode(type);
  *Narray = (PyArrayObject *)PyArray_ContiguousFromObject(incommingObject,
							typecode, 0, 10 );
  if (*Narray == NULL) 
  {
    sprintf(buf,"Failed to make a contiguous array of type %d\n",
            typecode);
    PyErr_SetString(PyExc_ValueError, buf);
         *Narray =  NULL;
  }
   if (checkArraySize && nbElem)
   {
     /* check size */
     size = 1;
     for (i=0; i<((*Narray)->nd); i++)
     size = size * ((*Narray)->dimensions[i]);
     if (size!=nbElem) 
     {
       sprintf(buf, "%d values received when %d expected\n", size, nbElem);
       PyErr_SetString(PyExc_ValueError, buf);
       *Narray = NULL;
     }
   }
}
%}

extern int checkArgumentsInCWrapper=1;

/**************************************************************/
/* Macro for creating typemaps for:  const GL<type> xxx[ANY]  */
/**************************************************************/

%define IN_GLTYPE_ANY(T)
%typemap(in) const T xxx[ANY] (PyArrayObject *array)
{
  if (checkArgumentsInCWrapper) 
  {
    if (isContiguosBuffer((PyObject*)$input))
    { 
      Py_ssize_t buffer_len;
      array = NULL;
      if (PyObject_AsReadBuffer( $input, (const void**)&$1, &buffer_len))
        return NULL;
      if (! $1) return PyErr_Format( PyExc_ValueError,
	 			"NULL buffer not accepted");
    }
    else
    {
      bufferWithCheck($input, &array, "T", $1_dim0);
      if (! array) return NULL;
      $1 = ($1_ltype)array->data;
    }

  }
  else
  {
    Py_ssize_t buffer_len;
    array = NULL;
    if (PyObject_AsReadBuffer( $input, (const void**)&$1, &buffer_len))
      return NULL;
    if (! $1) return PyErr_Format( PyExc_ValueError,
				"NULL buffer not accepted");
  }
}

%typemap( freearg) const T xxx[ANY] 
{
  if (array$argnum)
  {
    Py_DECREF(array$argnum);
  }
}
%enddef

/**************************************************************/
/* Macro for creating typemaps for:  const GL<type> *         */
/**************************************************************/

%define IN_GLTYPE(T)
%typemap(in) const T *(PyArrayObject *array)
{
  if (checkArgumentsInCWrapper) 
  {
    if (isContiguosBuffer((PyObject*)$input))
    { 
      Py_ssize_t buffer_len;
      array = NULL;
      if (PyObject_AsReadBuffer( $input, (const void**)&$1, &buffer_len))
        return NULL;
      if (! $1) return PyErr_Format( PyExc_ValueError,
	 			"NULL buffer not accepted");
    }
    else
    {
      bufferWithCheck($input, &array, "T", 0);
      if (! array) return NULL;
      $1 = ($1_ltype)array->data;
    }
    /****
    if (array)
    printf ("Array refcnt 1: %d\n", (((PyObject *)array)->ob_refcnt);
    ****/
  }
  else
  {
    Py_ssize_t buffer_len;
    array = NULL;
    if (PyObject_AsReadBuffer( $input, (const void**)&$1, &buffer_len))
      return NULL;
    if (! $1) return PyErr_Format( PyExc_ValueError,
				"NULL buffer not accepted");
  }
}

%typemap( freearg) const T * 
{
  if (array$argnum)
  {
    Py_DECREF(array$argnum);
  }
}
%enddef

/** typemap for :
void gluPickMatrix( GLdouble x, GLdouble y, GLdouble delX, 
                    GLdouble delY, GLint xxx[4]);
***/

%typemap(in) GLint xxx[ANY] (PyArrayObject *array)
{
  if (checkArgumentsInCWrapper) 
  {
    if (isContiguosBuffer((PyObject*)$input))
    { 
      Py_ssize_t buffer_len;
      array = NULL;
      if (PyObject_AsReadBuffer( $input, (const void**)&$1, &buffer_len))
        return NULL;
      if (! $1) return PyErr_Format( PyExc_ValueError,
	 			"NULL buffer not accepted");
    }
    else
    {
      bufferWithCheck($input, &array, "GLint", $1_dim0);
      if (! array) return NULL;
      $1 = (GLint *)array->data;
    }
    /****
    if (array)
    printf ("Array refcnt 1: %d\n", (((PyObject *)array)->ob_refcnt);
    ****/
  }
  else
  {
    Py_ssize_t buffer_len;
    array = NULL;
    if (PyObject_AsReadBuffer( $input, (const void**)&$1, &buffer_len))
      return NULL;
    if (! $1) return PyErr_Format( PyExc_ValueError,
				"NULL buffer not accepted");
  }
}

%typemap( freearg) GLint xxx[ANY] 
{
  if (array$argnum)
  {
    Py_DECREF(array$argnum);
  }
}

/* Generate typemaps */

IN_GLTYPE_ANY(GLbyte)
IN_GLTYPE_ANY(GLdouble)
IN_GLTYPE_ANY(GLfloat)
IN_GLTYPE_ANY(GLint)
IN_GLTYPE_ANY(GLshort)
IN_GLTYPE_ANY(GLubyte)
IN_GLTYPE_ANY(GLuint)
IN_GLTYPE_ANY(GLushort)
IN_GLTYPE_ANY(GLboolean)

IN_GLTYPE(GLbyte)
IN_GLTYPE(GLdouble)
IN_GLTYPE(GLfloat)
IN_GLTYPE(GLint)
IN_GLTYPE(GLshort)
IN_GLTYPE(GLubyte)
IN_GLTYPE(GLuint)
IN_GLTYPE(GLushort)
IN_GLTYPE(GLboolean)