1 /*
2 * pybox2d -- http://pybox2d.googlecode.com
3 *
4 * Copyright (c) 2010 Ken Lauer / sirkne at gmail dot com
5 *
6 * This software is provided 'as-is', without any express or implied
7 * warranty.  In no event will the authors be held liable for any damages
8 * arising from the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 */
20 
21 %inline %{
22 #define pybox2d_float_from_sequence(_sequence, _num, _dest, _err_msg) \
23     {                                                                 \
24         PyObject* item=PySequence_GetItem(_sequence, _num);           \
25         int res=SWIG_AsVal_float(item, _dest);                        \
26         Py_XDECREF(item);                                             \
27         if (!SWIG_IsOK(res)) {                                        \
28             PyErr_SetString(PyExc_TypeError,_err_msg);                \
29             SWIG_fail;                                                \
30         }                                                             \
31     }
32 
33 %}
34 
35 
36 //input - $input -> ($1_type) $1 $1_descriptor
37 %typemap(in) b2Vec2* self {
38     int res1 = SWIG_ConvertPtr($input, (void**)&$1, $descriptor(b2Vec2*), 0);
39     if (!SWIG_IsOK(res1)) {
40         SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
41     }
42 }
43 %typemap(in) b2Vec3* self {
44     int res1 = SWIG_ConvertPtr($input, (void**)&$1, $descriptor(b2Vec3*), 0);
45     if (!SWIG_IsOK(res1)) {
46         SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
47     }
48 }
49 %typemap(in) b2Color* self {
50     int res1 = SWIG_ConvertPtr($input, (void**)&$1, $descriptor(b2Color*), 0);
51     if (!SWIG_IsOK(res1)) {
52         SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
53     }
54 }
55 
56 //Resolve ambiguities in overloaded functions when you pass a tuple or list when
57 //SWIG expects a b2Vec2 (b2Vec3, b2Color)
58 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) b2Vec2*,b2Vec2& {
59    $1 = (PySequence_Check($input) ||
60          SWIG_CheckState(SWIG_ConvertPtr($input, 0, $descriptor(b2Vec2*), 0))
61         ) ? 1 : 0;
62 }
63 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) b2Vec3*,b2Vec3& {
64    $1 = (PySequence_Check($input) ||
65          SWIG_CheckState(SWIG_ConvertPtr($input, 0, $descriptor(b2Vec3*), 0))
66         ) ? 1 : 0;
67 }
68 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) b2Color*,b2Color& {
69    $1 = (PySequence_Check($input) ||
70          SWIG_CheckState(SWIG_ConvertPtr($input, 0, $descriptor(b2Color*), 0))
71         ) ? 1 : 0;
72 }
73 
74 // Allow b2Vec2* arguments be passed in as tuples or lists
75 %typemap(in) b2Vec2* (b2Vec2 temp) {
76     if (PySequence_Check($input)) {
77         if (PySequence_Size($input) != 2) {
78             PyErr_Format(PyExc_TypeError, "Expected tuple or list of length 2, got length %ld", (long) PySequence_Size($input));
79             SWIG_fail;
80         }
81         pybox2d_float_from_sequence($input, 0, &temp.x, "Converting from sequence to b2Vec2, expected int/float arguments index 0");
82         pybox2d_float_from_sequence($input, 1, &temp.y, "Converting from sequence to b2Vec2, expected int/float arguments index 1");
83     } else if ($input==Py_None) {
84         temp.Set(0.0f,0.0f);
85     } else {
86         int res1 = SWIG_ConvertPtr($input, (void**)&$1, $1_descriptor, 0);
87         if (!SWIG_IsOK(res1)) {
88             SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
89             SWIG_fail;
90         }
91         temp =(b2Vec2&) *$1;
92     }
93     $1 = &temp;
94 }
95 
96 // Allow b2Color* arguments be passed in as tuples or lists
97 %typemap(in) b2Color* (b2Color temp) {
98     //input - $input -> ($1_type) $1 $1_descriptor
99     if (PySequence_Check($input)) {
100         if (PySequence_Size($input) != 3) {
101             PyErr_Format(PyExc_TypeError, "Expected tuple or list of length 3, got length %ld", (long) PySequence_Size($input));
102             SWIG_fail;
103         }
104         pybox2d_float_from_sequence($input, 0, &temp.r, "Converting from sequence to b2Color, expected int/float arguments index 0");
105         pybox2d_float_from_sequence($input, 1, &temp.g, "Converting from sequence to b2Color, expected int/float arguments index 1");
106         pybox2d_float_from_sequence($input, 2, &temp.b, "Converting from sequence to b2Color, expected int/float arguments index 2");
107     } else if ($input==Py_None) {
108         temp.Set(0.0f,0.0f,0.0f);
109     } else {
110         int res1 = SWIG_ConvertPtr($input, (void**)&$1, $1_descriptor, 0);
111         if (!SWIG_IsOK(res1)) {
112             SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
113             SWIG_fail;
114         }
115         temp =(b2Color&) *$1;
116     }
117     $1 = &temp;
118 }
119 
120 // Allow b2Vec3* arguments be passed in as tuples or lists
121 %typemap(in) b2Vec3* (b2Vec3 temp) {
122     //input - $input -> ($1_type) $1 $1_descriptor
123     if (PySequence_Check($input)) {
124         if (PySequence_Size($input) != 3) {
125             PyErr_Format(PyExc_TypeError, "Expected tuple or list of length 3, got length %ld", (long) PySequence_Size($input));
126             SWIG_fail;
127         }
128         pybox2d_float_from_sequence($input, 0, &temp.x, "Converting from sequence to b2Vec3, expected int/float arguments index 0");
129         pybox2d_float_from_sequence($input, 1, &temp.y, "Converting from sequence to b2Vec3, expected int/float arguments index 1");
130         pybox2d_float_from_sequence($input, 2, &temp.z, "Converting from sequence to b2Vec3, expected int/float arguments index 2");
131     } else if ($input==Py_None) {
132         temp.Set(0.0f,0.0f,0.0f);
133     } else {
134         int res1 = SWIG_ConvertPtr($input, (void**)&$1, $1_descriptor, 0);
135         if (!SWIG_IsOK(res1)) {
136             SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
137             SWIG_fail;
138         }
139         temp =(b2Vec3&) *$1;
140     }
141     $1 = &temp;
142 }
143 
144 // Allow b2Vec2& arguments be passed in as tuples or lists
145 %typemap(in) b2Vec2& (b2Vec2 temp) {
146     //input - $input -> ($1_type) $1 $1_descriptor
147     if (PySequence_Check($input)) {
148         if (PySequence_Size($input) != 2) {
149             PyErr_Format(PyExc_TypeError, "Expected tuple or list of length 2, got length %ld", (long) PySequence_Size($input));
150             SWIG_fail;
151         }
152         pybox2d_float_from_sequence($input, 0, &temp.x, "Converting from sequence to b2Vec2, expected int/float arguments index 0");
153         pybox2d_float_from_sequence($input, 1, &temp.y, "Converting from sequence to b2Vec2, expected int/float arguments index 1");
154     } else if ($input == Py_None) {
155         temp.Set(0.0f,0.0f);
156     } else {
157         int res1 = SWIG_ConvertPtr($input, (void**)&$1, $1_descriptor, 0);
158         if (!SWIG_IsOK(res1)) {
159             SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
160         }
161         temp =(b2Vec2&) *$1;
162     }
163     $1 = &temp;
164 }
165 
166 // Allow b2Color& arguments be passed in as tuples or lists
167 %typemap(in) b2Color& (b2Color temp) {
168     //input - $input -> ($1_type) $1 $1_descriptor
169     if (PySequence_Check($input)) {
170         if (PySequence_Size($input) != 3) {
171             PyErr_Format(PyExc_TypeError, "Expected tuple or list of length 3, got length %ld", (long) PySequence_Size($input));
172             SWIG_fail;
173         }
174         pybox2d_float_from_sequence($input, 0, &temp.r, "Converting from sequence to b2Color, expected int/float arguments index 0");
175         pybox2d_float_from_sequence($input, 1, &temp.g, "Converting from sequence to b2Color, expected int/float arguments index 1");
176         pybox2d_float_from_sequence($input, 2, &temp.b, "Converting from sequence to b2Color, expected int/float arguments index 2");
177     } else if ($input==Py_None) {
178         temp.Set(0.0f,0.0f,0.0f);
179     } else {
180         int res1 = SWIG_ConvertPtr($input, (void**)&$1, $1_descriptor, 0);
181         if (!SWIG_IsOK(res1)) {
182             SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
183             SWIG_fail;
184         }
185         temp =(b2Color&) *$1;
186     }
187     $1 = &temp;
188 }
189 
190 // Allow b2Vec3& arguments be passed in as tuples or lists
191 %typemap(in) b2Vec3& (b2Vec3 temp) {
192     //input - $input -> ($1_type) $1 $1_descriptor
193     if (PySequence_Check($input)) {
194         if (PySequence_Size($input) != 3) {
195             PyErr_Format(PyExc_TypeError, "Expected tuple or list of length 3, got length %ld", (long) PySequence_Size($input));
196             SWIG_fail;
197         }
198         pybox2d_float_from_sequence($input, 0, &temp.x, "Converting from sequence to b2Vec3, expected int/float arguments index 0");
199         pybox2d_float_from_sequence($input, 1, &temp.y, "Converting from sequence to b2Vec3, expected int/float arguments index 1");
200         pybox2d_float_from_sequence($input, 2, &temp.z, "Converting from sequence to b2Vec3, expected int/float arguments index 2");
201     } else if ($input==Py_None) {
202         temp.Set(0.0f,0.0f,0.0f);
203     } else {
204         int res1 = SWIG_ConvertPtr($input, (void**)&$1, $1_descriptor, 0);
205         if (!SWIG_IsOK(res1)) {
206             SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
207             SWIG_fail;
208         }
209         temp =(b2Vec3&) *$1;
210     }
211     $1 = &temp;
212 }
213 
214 //Allow access to void* types
215 %typemap(in) void* {
216     $1 = $input;
217     Py_INCREF((PyObject*)$1);
218 }
219 %typemap(out) void* {
220     if (!$1)
221         $result=Py_None;
222     else
223         $result=(PyObject*)$1;
224 
225     Py_INCREF($result);
226 }
227 
228 %typemap(in) b2Manifold* oldManifold{
229     void* argp=NULL;
230     int res3 = SWIG_ConvertPtr($input, &argp, $descriptor(b2Manifold *), 0);
231     Swig::Director *director = SWIG_DIRECTOR_CAST(arg1);
232 #ifdef _SWIG_KWARGS
233     bool upcall_ = (director && (director->swig_get_self()==obj0));
234 #else
235     bool upcall_ = (director && (director->swig_get_self()==swig_obj[0]));
236 #endif
237     if (upcall_) {
238         /* This conversion fails on py3k when attempting to call the
239            b2_defaultListener.PreSolve() and I cannot quite figure out why.
240            I think it has something to do with the fact that the default implementation
241            is in C++, the director method gets called, then it attempts to call a nonexistent
242            Python function, ... or something like that.
243 
244            In any case, the base b2ContactListener does nothing, so whatever args
245            we pass it, that's fine.
246            TODO
247            */
248     } else if (!SWIG_IsOK(res3)) {
249         SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "$symname" "', argument " "$1_name"" of type '" "$1_type""'");
250     }
251     $1 = reinterpret_cast<b2Manifold*>(argp);
252 }
253 
254 %typemap(directorin) (const b2Vec2* vertices, int32 vertexCount) {
255     $input = PyTuple_New(vertexCount);
256     PyObject* vertex;
257     for (int i=0; i < vertexCount; i++) {
258         vertex = PyTuple_New(2);
259         PyTuple_SetItem(vertex, 0, SWIG_From_double((float32)vertices[i].x));
260         PyTuple_SetItem(vertex, 1, SWIG_From_double((float32)vertices[i].y));
261 
262         PyTuple_SetItem($input, i, vertex);
263     }
264 }
265 
266 %typemap(directorin) b2Vec2& {
267     $input = PyTuple_New(2);
268     PyTuple_SetItem( $input, 0, SWIG_From_double((float32)$1_name.x));
269     PyTuple_SetItem( $input, 1, SWIG_From_double((float32)$1_name.y));
270 }
271 
272 /* Properly downcast joints for all return values using b2Joint */
273 %typemap(out) b2Joint* {
274 
275     if ($1) {
276         switch (($1)->GetType())
277         {
278         case e_revoluteJoint:
279             $result=SWIG_NewPointerObj($1, $descriptor(b2RevoluteJoint*), 0); break;
280         case e_prismaticJoint:
281             $result=SWIG_NewPointerObj($1, $descriptor(b2PrismaticJoint*), 0); break;
282         case e_distanceJoint:
283             $result=SWIG_NewPointerObj($1, $descriptor(b2DistanceJoint*), 0); break;
284         case e_pulleyJoint:
285             $result=SWIG_NewPointerObj($1, $descriptor(b2PulleyJoint*), 0); break;
286         case e_mouseJoint:
287             $result=SWIG_NewPointerObj($1, $descriptor(b2MouseJoint*), 0); break;
288         case e_gearJoint:
289             $result=SWIG_NewPointerObj($1, $descriptor(b2GearJoint*), 0); break;
290         case e_wheelJoint:
291             $result=SWIG_NewPointerObj($1, $descriptor(b2WheelJoint*), 0); break;
292         case e_weldJoint:
293             $result=SWIG_NewPointerObj($1, $descriptor(b2WeldJoint*), 0); break;
294         case e_frictionJoint:
295             $result=SWIG_NewPointerObj($1, $descriptor(b2FrictionJoint*), 0); break;
296         case e_ropeJoint:
297             $result=SWIG_NewPointerObj($1, $descriptor(b2RopeJoint*), 0); break;
298         case e_motorJoint:
299             $result=SWIG_NewPointerObj($1, $descriptor(b2MotorJoint*), 0); break;
300         case e_unknownJoint:
301         default:
302             $result=SWIG_NewPointerObj($1, $descriptor(b2Joint*), 0); break;
303             break;
304         }
305     } else {
306         $result=Py_None;
307         Py_INCREF($result);
308     }
309 }
310 
311 /* Properly downcast shapes for all return values using b2Shape */
312 %typemap(out) b2Shape* {
313     if ($1) {
314         switch (($1)->GetType())
315         {
316         case b2Shape::e_circle:
317             $result=SWIG_NewPointerObj($1, $descriptor(b2CircleShape*), 0); break;
318         case b2Shape::e_polygon:
319             $result=SWIG_NewPointerObj($1, $descriptor(b2PolygonShape*), 0); break;
320         case b2Shape::e_edge:
321             $result=SWIG_NewPointerObj($1, $descriptor(b2EdgeShape*), 0); break;
322         case b2Shape::e_chain:
323             $result=SWIG_NewPointerObj($1, $descriptor(b2ChainShape*), 0); break;
324         default:
325             $result=SWIG_NewPointerObj($1, $descriptor(b2Shape*), 0); break;
326         }
327     } else {
328         $result=Py_None;
329         Py_INCREF($result);
330     }
331 }
332 
333