File: Box2D.i

package info (click to toggle)
python-elements 0.13%2Bsvn20090823.230%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 1,208 kB
  • ctags: 445
  • sloc: python: 2,816; makefile: 27; sh: 6
file content (515 lines) | stat: -rw-r--r-- 17,651 bytes parent folder | download | duplicates (2)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* Python SWIG interface file for Box2D (www.box2d.org)
*
* Copyright (c) 2008 kne / sirkne at gmail dot com
* 
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#todo: use enumerate
#implement the checkDef() code here
%module(directors="1") Box2D2
%{
    #include "../Include/Box2D.h"
    
    //Define these functions so that SWIG does not fail
    void b2BroadPhase::ValidatePairs() { }

%}

#ifdef TARGET_FLOAT32_IS_FIXED
    //figure out what to do here :)
#endif
#ifdef SWIGPYTHON
    
    //Autodoc puts the basic docstrings for each function
    %feature("autodoc", "1");

    //Add callback support for the following classes:
    %feature("director") b2ContactListener;
    %feature("director") b2BoundaryListener;
    %feature("director") b2DestructionListener;
    %feature("director") b2DebugDraw;

    //These operators do not work unless explicitly defined like this 
    %rename(b2add) operator  + (const b2Vec2& a, const b2Vec2& b);
    %rename(b2add) operator  + (const b2Mat22& A, const b2Mat22& B);
    %rename(b2sub) operator  - (const b2Vec2& a, const b2Vec2& b);
    %rename(b2mul) operator  * (float32 s, const b2Vec2& a);
    %rename(b2equ) operator == (const b2Vec2& a, const b2Vec2& b);
    
    //Since Python (apparently) requires __imul__ to return self,
    //these void operators will not do. So, rename them, then call them
    //with Python code, and return self. (see further down in b2Vec2)
    %rename(add_vector) b2Vec2::operator += (const b2Vec2& v);
    %rename(sub_vector) b2Vec2::operator -= (const b2Vec2& v);
    %rename(mul_float ) b2Vec2::operator *= (float32 a);

    /*%ignore b2Shape::GetUserData;
    %ignore b2Body::GetUserData;
    %ignore b2Joint::GetUserData;*/

    //Allow access to (m_)userData
    %typemap(in) void* userData, void* m_userData {
        //In
        if ($input == Py_None) {
            $1 = NULL;
        } else {
            $1 = (void*)( $input );
            Py_INCREF($input);
        }
    }

    %typemap(out) void* userData, void* m_userData {
        //Out
        if ($1 == NULL) {
            $result = Py_None;
        } else {
            $result = (PyObject*)( $1 );
        }
        Py_INCREF($result);
    }

    %extend b2World {
    public:
        PyObject* Query(const b2AABB& aabb, uint32 maxCount) {
            PyObject* ret=Py_None;
            b2Shape** shapes=(b2Shape**)malloc(maxCount * sizeof(b2Shape*));

            if (!shapes) {
                PyErr_SetString(PyExc_MemoryError, "Insufficient memory");
                return ret;
            }

            int32 num=$self->Query(aabb, shapes, maxCount);
            if (num < 0)
                num = 0;

            ret = PyTuple_New(2);
            
            PyObject* shapeList=PyTuple_New(num);
            PyObject* shape;

            for (int i=0; i < num; i++) {
                shape=SWIG_NewPointerObj(SWIG_as_voidptr(shapes[i]), SWIGTYPE_p_b2Shape, 0 );
                PyTuple_SetItem(shapeList, i, shape);
            }

            PyTuple_SetItem(ret, 0, SWIG_From_int(num));
            PyTuple_SetItem(ret, 1, shapeList);

            free(shapes);
            return ret;
        }
    }

    /*
    //hmm
    %feature("director:except") {
        if ($error != NULL) {
            throw Swig::DirectorMethodException();
        }
    }*/
        
    %typemap(directorin) b2Vec2* vertices {
        $input = PyTuple_New(vertexCount);
        PyObject* vertex;
        for (int i=0; i < vertexCount; i++) {
            vertex = PyTuple_New(2);
            PyTuple_SetItem(vertex, 0, PyFloat_FromDouble((float32)vertices[i].x));
            PyTuple_SetItem(vertex, 1, PyFloat_FromDouble((float32)vertices[i].y));

            PyTuple_SetItem($input, i, vertex);
        }
    }

    %feature("shadow") GetUserData {
        def GetUserData(self): # override the C++ version as it does not work. 
            """Get the specified userData (m_userData)"""
            return self.pyGetUserData()
    }

    //Typecast the shape as necessary so Python can use them properly (2.0)
    %include "Collision/Shapes/b2Shape.h"
    %extend b2Shape {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2Shape(from Body %s )" % (self.GetBody())
        def typeName(self):
            types = {  e_unknownShape   : "Unknown",
                        e_circleShape   : "Circle",
                        e_polygonShape  : "Polygon",
                        e_shapeTypeCount: "ShapeType" }
            return types[self.GetType()]
        def getAsType(self):
            """Return a typecasted version of the shape"""
            return (getattr(self, "as%s" % self.typeName())) ()
        %}
        b2CircleShape* asCircle() {
            if ($self->GetType()==e_circleShape)
                return (b2CircleShape*)$self;
            return NULL;
        }
        b2PolygonShape* asPolygon() {
            if ($self->GetType()==e_polygonShape)
                return (b2PolygonShape*)$self;
            return NULL;
        }
        PyObject* pyGetUserData() {
            PyObject* ret=(PyObject*)self->GetUserData();
            Py_INCREF(ret);
            return ret;
        }
    }
   
    //Generic joint information
    %pythoncode %{
        def b2JointInfo(self):
            """Return a rather verbose string representation of a joint"""
            props = dir(self)
            ignoreList = ('this', 'thisown', 'next', 'prev', 'm_next', 'm_prev')
            info  = []
            for prop in dir(self):
                if prop[:2]=='__' or prop in ignoreList: continue
                if not callable(getattr(self, prop)): 
                    info.append(prop + ":")
                    info.append(str(getattr(self, prop)))
            return "%s(%s)" % (self.__class__.__name__, " ".join(info))
    %}

    %extend b2MouseJoint {
    public:
        %pythoncode %{
        def __repr__(self):
            return b2JointInfo(self)
        %}
    }

    %extend b2GearJoint {
    public:
        %pythoncode %{
        def __repr__(self):
            return b2JointInfo(self)
        %}
    }

    %extend b2DistanceJoint {
    public:
        %pythoncode %{
        def __repr__(self):
            return b2JointInfo(self)
        %}
    }

    %extend b2PrismaticJoint {
    public:
        %pythoncode %{
        def __repr__(self):
            return b2JointInfo(self)
        %}
    }
 
   %extend b2PulleyJoint {
    public:
        %pythoncode %{
        def __repr__(self):
            return b2JointInfo(self)
        %}
    }

   %extend b2RevoluteJoint {
    public:
        %pythoncode %{
        def __repr__(self):
            return b2JointInfo(self)
        %}
    }

    %include "Dynamics/Joints/b2Joint.h"

    %extend b2JointDef {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2JointDef(body1: %s body2: %s)" % (self.body1, self.body2)
        def typeName(self):
            types = { e_unknownJoint  : "Unknown",
                      e_mouseJoint    : "Mouse", 
                      e_gearJoint     : "Gear",
                      e_distanceJoint : "Distance",
                      e_prismaticJoint: "Prismatic",
                      e_pulleyJoint   : "Pulley",
                      e_revoluteJoint : "Revolute" }
            return types[self.GetType()]
        %}
    }

    %extend b2Joint {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2Joint(m_body1: %s m_body2: %s getAsType(): %s)" % (self.m_body1, self.m_body2, self.getAsType())
        def typeName(self):
            types = { e_unknownJoint  : "Unknown",
                      e_mouseJoint    : "Mouse", 
                      e_gearJoint     : "Gear",
                      e_distanceJoint : "Distance",
                      e_prismaticJoint: "Prismatic",
                      e_pulleyJoint   : "Pulley",
                      e_revoluteJoint : "Revolute" }
            return types[self.GetType()]
        def getAsType(self):
            """Return a typecasted version of the joint"""
            return (getattr(self, "as%sJoint" % self.typeName())) ()
        %}
        PyObject* pyGetUserData() {
            PyObject* ret=(PyObject*)self->GetUserData();
            Py_INCREF(ret);
            return ret;
        }

        b2MouseJoint* asMouseJoint() {
            if ($self->GetType()==e_mouseJoint)
                return (b2MouseJoint*)$self;
            return NULL;
        }

        b2GearJoint* asGearJoint() {
            if ($self->GetType()==e_gearJoint)
                return (b2GearJoint*)$self;
            return NULL;
        }

        b2DistanceJoint* asDistanceJoint() {
            if ($self->GetType()==e_distanceJoint)
                return (b2DistanceJoint*)$self;
            return NULL;
        }

        b2PrismaticJoint* asPrismaticJoint() {
            if ($self->GetType()==e_prismaticJoint)
                return (b2PrismaticJoint*)$self;
            return NULL;
        }

        b2PulleyJoint* asPulleyJoint() {
            if ($self->GetType()==e_pulleyJoint)
                return (b2PulleyJoint*)$self;
            return NULL;
        }

        b2RevoluteJoint* asRevoluteJoint() {
            if ($self->GetType()==e_revoluteJoint)
                return (b2RevoluteJoint*)$self;
            return NULL;
        }
    }

    %ignore b2PolygonShape::GetVertices; //Inaccessible 

    //Let python access all the vertices in the b2PolygonDef/Shape
    %extend b2PolygonShape {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2PolygonShape(vertices: %s count: %d)" % (self.getVertices_tuple(), self.vertexCount)
        def getVertices_tuple(self):
            """Returns all of the vertices as a list of tuples [ (x1,y1), (x2,y2) ... (xN,yN) ]"""
            vertices = []
            for i in range(0, self.vertexCount):
                vertices.append( (self.getVertex(i).x, self.getVertex(i).y ) )
            return vertices
        def getVertices_b2Vec2(self):
            """Returns all of the vertices as a list of b2Vec2's [ (x1,y1), (x2,y2) ... (xN,yN) ]"""
            vertices = []
            for i in range(0, self.GetVertexCount()):
                vertices.append(self.getVertex(i))
            return vertices
        %}
        const b2Vec2* getVertex(uint16 vnum) {
            if (vnum > b2_maxPolygonVertices || vnum > self->GetVertexCount()) return NULL;
            return &( $self->GetVertices() [vnum] );
        }
    }
    
    %extend b2PolygonDef{
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2PolygonDef(vertices: %s count: %d)" % (self.getVertices_tuple(), self.vertexCount)
        def getVertices_tuple(self):
            """Returns all of the vertices as a list of tuples [ (x1,y1), (x2,y2) ... (xN,yN) ]"""
            vertices = []
            for i in range(0, self.vertexCount):
                vertices.append( (self.getVertex(i).x, self.getVertex(i).y ) )
            return vertices
        def getVertices_b2Vec2(self):
            """Returns all of the vertices as a list of b2Vec2's [ (x1,y1), (x2,y2) ... (xN,yN) ]"""
            vertices = []
            for i in range(0, self.vertexCount):
                vertices.append(self.getVertex(i))
            return vertices
        def setVertices_tuple(self, vertices):
            """Sets all of the vertices (up to b2_maxPolygonVertices) given a tuple 
                in the format ( (x1,y1), (x2,y2) ... (xN,yN) )"""
            if len(vertices) > b2_maxPolygonVertices:
                raise ValueError
            self.vertexCount = len(vertices)
            for i in range(0, self.vertexCount):
                self.setVertex(i, vertices[i][0], vertices[i][1])
        def setVertices_b2Vec2(self, vertices):
            """Sets all of the vertices (up to b2_maxPolygonVertices) given a tuple 
                in the format ( (x1,y1), (x2,y2) ... (xN,yN) ) where each vertex
                is a b2Vec2"""
            if len(vertices) > b2_maxPolygonVertices:
                raise ValueError
            self.vertexCount = len(vertices)
            for i in range(0, self.vertexCount):
                self.setVertex(i, vertices[i])
        %}
        b2Vec2* getVertex(uint16 vnum) {
            if (vnum > b2_maxPolygonVertices || vnum > self->vertexCount) return NULL;
            return &( $self->vertices[vnum] );
        }
        void setVertex(uint16 vnum, b2Vec2& value) {
            if (vnum > b2_maxPolygonVertices) return;
            $self->vertices[vnum].Set(value.x, value.y);
        }
        void setVertex(uint16 vnum, float32 x, float32 y) {
            if (vnum > b2_maxPolygonVertices) return;
            $self->vertices[vnum].Set(x, y);
        }
    }

    //Extend the vector class to support Python print statements
    //Also, add vector addition and scalar multiplication
    %extend b2Vec2 {
        %pythoncode %{
        def __repr__(self):
            return "b2Vec2(%g,%g)" % (self.x, self.y)
        def tuple(self):
            return (self.x, self.y)
        def fromTuple(self, tuple):
            self.x, self.y = tuple
            return self
        def copy(self):
            return b2Vec2(self.x, self.y)
        def __iadd__(self, other):
            self.add_vector(other)
            return self
        def __isub__(self, other):
            self.sub_vector(other)
            return self
        def __imul__(self, a):
            self.mul_float(a)
            return self
        def __idiv__(self, a):
            self.div_float(a)
            return self

        %}
        b2Vec2 __div__(float32 a) { //convenience function
            return b2Vec2($self->x / a, $self->y / a);
        }
        b2Vec2 __mul__(float32 a) {
            return b2Vec2($self->x * a, $self->y * a);
        }
        b2Vec2 __add__(b2Vec2* other) {
            return b2Vec2($self->x + other->x, $self->y + other->y);
        }
        b2Vec2 __sub__(b2Vec2* other) {
            return b2Vec2($self->x - other->x, $self->y - other->y);
        }

        b2Vec2 __rmul__(float32 a) {
            return b2Vec2($self->x * a, $self->y * a);
        }
        b2Vec2 __rdiv__(float32 a) { //perhaps not _correct_, but convenient
            return b2Vec2($self->x / a, $self->y / a);
        }
        void div_float(float32 a) {
            self->x /= a;
            self->y /= a;
        }
    }

    //Pretty printing section
    %extend b2Body {
        %pythoncode %{
        def __repr__(self):
            return "b2Body(Position: %s)" % (self.GetPosition())
        %}
        PyObject* pyGetUserData() {
            PyObject* ret=(PyObject*)self->GetUserData();
            Py_INCREF(ret);
            return ret;
        }
    }

    %extend b2ContactID_features {
        %pythoncode %{
        def __repr__(self):
            return "b2ContactID::Features(\n\treferenceFace: %d incidentEdge: %d incidentVertex: %d flip: %d)" % \
                (self.referenceFace, self.incidentEdge, self.incidentVertex, self.flip)
        %}
    }
    %extend b2ContactID {
        %pythoncode %{
        def __repr__(self):
            return "b2ContactID(key: %d Features: \n\t%s)" % \
                (self.key, self.features)
        %}
    }

    %extend b2ContactPoint {
        %pythoncode %{
        def __repr__(self):
            return "b2ContactPoint(\n\tid: %s\n\tshape1: %s\n\tshape2: %s\n\tposition: %s\n\tnormal: %s\n\tseparation: %f normalForce: %f tangentForce: %f)" % \
                (self.id, self.shape1, self.shape2, self.position, self.normal, self.separation, self.normalForce, self.tangentForce)
        %}
    }

    %extend b2JointEdge {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2JointEdge(other: %s)" % (self.other)
        %}
    }
    
    %extend b2Jacobian {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2Jacobian(linear1: %s: linear2: %s angular1: %s angular2: %s)" %\
                (self.linear1, self.linear2, self.angular1, self.angular2)
        %}
    }

    %extend b2Mat22 {
    public:
        %pythoncode %{
        def __repr__(self):
            return "b2Mat22(col1: %s col2: %s)" % (self.col1, self.col2)
        %}
    }


#endif

%include "../Include/Box2D.h"