1 //!/usr/bin/python 2 // 3 // C++ version copyright 2010 Erin Catto http://www.gphysics.com 4 // Python version copyright 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 %pythoncode %{ 22 _repr_attrs = {'b2AABB': ['center', 'extents', 'lowerBound', 'perimeter', 'upperBound', 23 'valid', ], 24 'b2Body': ['active', 'angle', 'angularDamping', 'angularVelocity', 'awake', 25 'bullet', 'contacts', 'fixedRotation', 'fixtures', 26 'inertia', 'joints', 'linearDamping', 'linearVelocity', 27 'localCenter', 'mass', 'massData', 'position', 28 'sleepingAllowed', 'transform', 'type', 'userData', 29 'worldCenter', ], 30 'b2BodyDef': ['active', 'allowSleep', 'angle', 'angularDamping', 'angularVelocity', 31 'awake', 'bullet', 'fixedRotation', 'fixtures', 32 'inertiaScale', 'linearDamping', 'linearVelocity', 'position', 33 'shapeFixture', 'shapes', 'type', 'userData', 34 ], 35 'b2BroadPhase': ['proxyCount', ], 36 'b2CircleShape': ['childCount', 'pos', 'radius', 'type', ], 37 'b2ClipVertex': ['id', 'v', ], 38 'b2Color': ['b', 'bytes', 'g', 'list', 'r', 39 ], 40 'b2Contact': ['childIndexA', 'childIndexB', 'enabled', 'fixtureA', 'fixtureB', 41 'manifold', 'touching', 'worldManifold', ], 42 'b2ContactEdge': ['contact', 'other', ], 43 'b2ContactFeature': ['indexA', 'indexB', 'typeA', 'typeB', ], 44 'b2ContactID': ['cf', 'key', ], 45 'b2ContactImpulse': ['normalImpulses', 'tangentImpulses', ], 46 'b2ContactManager': ['allocator', 'broadPhase', 'contactCount', 'contactFilter', 'contactList', 47 'contactListener', ], 48 'b2ContactPoint': ['fixtureA', 'fixtureB', 'normal', 'position', 'state', 49 ], 50 'b2DistanceInput': ['proxyA', 'proxyB', 'transformA', 'transformB', 'useRadii', 51 ], 52 'b2DistanceJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 53 'dampingRatio', 'frequency', 'length', 'type', 54 'userData', ], 55 'b2DistanceJointDef': ['anchorA', 'anchorB', 'bodyA', 'bodyB', 'collideConnected', 56 'dampingRatio', 'frequencyHz', 'length', 'localAnchorA', 57 'localAnchorB', 'type', 'userData', ], 58 'b2DistanceOutput': ['distance', 'iterations', 'pointA', 'pointB', ], 59 'b2DistanceProxy': ['m_buffer', 'shape', 'vertices', ], 60 'b2Draw': ['flags', ], 61 'b2DrawExtended': ['center', 'convertVertices', 'flags', 'flipX', 'flipY', 62 'offset', 'screenSize', 'zoom', ], 63 'b2EdgeShape': ['all_vertices', 'childCount', 'hasVertex0', 'hasVertex3', 'radius', 64 'type', 'vertex0', 'vertex1', 'vertex2', 65 'vertex3', 'vertexCount', 'vertices', ], 66 'b2Filter': ['categoryBits', 'groupIndex', 'maskBits', ], 67 'b2Fixture': ['body', 'density', 'filterData', 'friction', 'massData', 68 'restitution', 'sensor', 'shape', 'type', 69 'userData', ], 70 'b2FixtureDef': ['categoryBits', 'density', 'filter', 'friction', 'groupIndex', 71 'isSensor', 'maskBits', 'restitution', 'shape', 72 'userData', ], 73 'b2FixtureProxy': ['aabb', 'childIndex', 'fixture', 'proxyId', ], 74 'b2FrictionJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 75 'maxForce', 'maxTorque', 'type', 'userData', 76 ], 77 'b2FrictionJointDef': ['anchor', 'bodyA', 'bodyB', 'collideConnected', 'localAnchorA', 78 'localAnchorB', 'maxForce', 'maxTorque', 'type', 79 'userData', ], 80 'b2GearJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 81 'ratio', 'type', 'userData', ], 82 'b2GearJointDef': ['bodyA', 'bodyB', 'collideConnected', 'joint1', 'joint2', 83 'ratio', 'type', 'userData', ], 84 'b2Jacobian': ['angularA', 'angularB', 'linearA', 'linearB', ], 85 'b2Joint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 86 'type', 'userData', ], 87 'b2JointDef': ['bodyA', 'bodyB', 'collideConnected', 'type', 'userData', 88 ], 89 'b2JointEdge': ['joint', 'other', ], 90 'b2WheelJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 91 'maxMotorTorque', 'motorEnabled', 'motorSpeed', 'speed', 92 'springDampingRatio', 'springFrequencyHz', 'translation', 'type', 93 'userData', ], 94 'b2WheelJointDef': ['anchor', 'axis', 'bodyA', 'bodyB', 'collideConnected', 95 'dampingRatio', 'enableMotor', 'frequencyHz', 'localAnchorA', 96 'localAnchorB', 'localAxisA', 'maxMotorTorque', 'motorSpeed', 97 'type', 'userData', ], 98 'b2ChainShape': ['childCount', 'edges', 'radius', 'type', 'vertexCount', 99 'vertices', ], 100 'b2Manifold': ['localNormal', 'localPoint', 'pointCount', 'points', 'type_', 101 ], 102 'b2ManifoldPoint': ['id', 'isNew', 'localPoint', 'normalImpulse', 'tangentImpulse', 103 ], 104 'b2MassData': ['I', 'center', 'mass', ], 105 'b2Mat22': ['angle', 'col1', 'col2', 'inverse', ], 106 'b2Mat33': ['col1', 'col2', 'col3', ], 107 'b2MouseJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 108 'dampingRatio', 'frequency', 'maxForce', 'target', 109 'type', 'userData', ], 110 'b2MouseJointDef': ['bodyA', 'bodyB', 'collideConnected', 'dampingRatio', 'frequencyHz', 111 'maxForce', 'target', 'type', 'userData', 112 ], 113 'b2Pair': ['proxyIdA', 'proxyIdB', ], 114 'b2PolygonShape': ['box', 'centroid', 'childCount', 'normals', 'radius', 115 'type', 'valid', 'vertexCount', 'vertices', 116 ], 117 'b2PrismaticJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 118 'limitEnabled', 'limits', 'lowerLimit', 'maxMotorForce', 119 'motorEnabled', 'motorSpeed', 'speed', 'translation', 120 'type', 'upperLimit', 'userData', ], 121 'b2PrismaticJointDef': ['anchor', 'axis', 'bodyA', 'bodyB', 'collideConnected', 122 'enableLimit', 'enableMotor', 'localAnchorA', 'localAnchorB', 123 'localAxis1', 'lowerTranslation', 'maxMotorForce', 'motorSpeed', 124 'referenceAngle', 'type', 'upperTranslation', 'userData', 125 ], 126 'b2PulleyJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 127 'groundAnchorA', 'groundAnchorB', 'length1', 'length2', 128 'ratio', 'type', 'userData', ], 129 'b2PulleyJointDef': ['anchorA', 'anchorB', 'bodyA', 'bodyB', 'collideConnected', 130 'groundAnchorA', 'groundAnchorB', 'lengthA', 'lengthB', 131 'localAnchorA', 'localAnchorB', 'maxLengthA', 'maxLengthB', 132 'ratio', 'type', 'userData', ], 133 'b2RayCastInput': ['maxFraction', 'p1', 'p2', ], 134 'b2RayCastOutput': ['fraction', 'normal', ], 135 'b2RevoluteJoint': ['active', 'anchorA', 'anchorB', 'angle', 'bodyA', 136 'bodyB', 'limitEnabled', 'limits', 'lowerLimit', 137 'maxMotorTorque', 'motorEnabled', 'motorSpeed', 'speed', 138 'type', 'upperLimit', 'userData', ], 139 'b2RevoluteJointDef': ['anchor', 'bodyA', 'bodyB', 'collideConnected', 'enableLimit', 140 'enableMotor', 'localAnchorA', 'localAnchorB', 'lowerAngle', 141 'maxMotorTorque', 'motorSpeed', 'referenceAngle', 'type', 142 'upperAngle', 'userData', ], 143 'b2RopeJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 144 'limitState', 'maxLength', 'type', 'userData', 145 ], 146 'b2RopeJointDef': ['anchorA', 'anchorB', 'bodyA', 'bodyB', 'collideConnected', 147 'localAnchorA', 'localAnchorB', 'maxLength', 'type', 148 'userData', ], 149 'b2Shape': ['childCount', 'radius', 'type', ], 150 'b2Sweep': ['a', 'a0', 'alpha0', 'c', 'c0', 151 'localCenter', ], 152 'b2TOIInput': ['proxyA', 'proxyB', 'sweepA', 'sweepB', 'tMax', 153 ], 154 'b2TOIOutput': ['state', 't', ], 155 'b2Transform': ['R', 'angle', 'position', ], 156 'b2Vec2': ['length', 'lengthSquared', 'skew', 'tuple', 'valid', 157 'x', 'y', ], 158 'b2Vec3': ['length', 'lengthSquared', 'tuple', 'valid', 'x', 159 'y', 'z', ], 160 'b2Version': ['major', 'minor', 'revision', ], 161 'b2WeldJoint': ['active', 'anchorA', 'anchorB', 'bodyA', 'bodyB', 162 'type', 'userData', ], 163 'b2WeldJointDef': ['anchor', 'bodyA', 'bodyB', 'collideConnected', 'localAnchorA', 164 'localAnchorB', 'referenceAngle', 'type', 'userData', 165 ], 166 'b2World': ['autoClearForces', 'bodies', 'bodyCount', 'contactCount', 'contactFilter', 167 'contactListener', 'contactManager', 'contacts', 'continuousPhysics', 168 'destructionListener', 'gravity', 'jointCount', 'joints', 169 'locked', 'proxyCount', 'renderer', 'subStepping', 170 'warmStarting', ], 171 'b2WorldManifold': ['normal', 'points', ], 172 } 173 174 MAX_REPR_DEPTH = 4 175 MAX_REPR_STR_LEN = 250 176 MAX_REPR_SUB_LINES = 10 177 REPR_INDENT = 4 178 179 _repr_state = {} 180 def _format_repr(obj): 181 """ 182 Dynamically creates the object representation string for `obj`. 183 184 Attributes found in _repr_attrs[class_name] will be included. 185 """ 186 187 global _repr_state 188 if 'spaces' not in _repr_state: 189 _repr_state['spaces'] = 0 190 191 if 'depth' not in _repr_state: 192 _repr_state['depth'] = 1 193 else: 194 _repr_state['depth'] += 1 195 196 if _repr_state['depth'] > MAX_REPR_DEPTH: 197 _repr_state['depth'] -= 1 198 return '%s(max recursion depth hit)' % (' ' * _repr_state['spaces']) 199 200 class_line = '%s(' % (obj.__class__.__name__, ) 201 202 orig_spaces = _repr_state['spaces'] 203 204 ret = [] 205 206 props = _repr_attrs.get(obj.__class__.__name__, []) 207 208 try: 209 prop_spacing = _repr_state['spaces'] + len(class_line.lstrip()) 210 separator = '\n' + ' ' * prop_spacing 211 212 for prop in props: 213 _repr_state['spaces'] = len(prop) + 1 214 try: 215 s = repr(getattr(obj, prop)) 216 except Exception as ex: 217 s = '(repr: %s)' % ex 218 219 lines = s.split('\n') 220 if len(lines) > MAX_REPR_SUB_LINES: 221 length_ = 0 222 for i, line_ in enumerate(lines[:MAX_REPR_SUB_LINES]): 223 length_ += len(line_) 224 if length_ > MAX_REPR_STR_LEN: 225 ending_delim = [] 226 for j in s[::-1]: 227 if j in ')]}': 228 ending_delim.insert(0, j) 229 else: 230 break 231 232 ret[-1] = '%s... %s' % (ret[-1], ''.join(ending_delim)) 233 break 234 235 if i == 0: 236 ret.append('%s=%s' % (prop, line_)) 237 else: 238 ret.append(line_) 239 else: 240 ret.append('%s=%s' % (prop, lines[0].lstrip())) 241 if len(lines) > 1: 242 ret.extend(lines[1:]) 243 244 ret[-1] += ',' 245 246 finally: 247 _repr_state['depth'] -= 1 248 _repr_state['spaces'] = orig_spaces 249 250 if 1<= len(ret) <= 3: 251 # Closing parenthesis on same line 252 ret[-1] += ')' 253 return ''.join(ret) 254 255 else: 256 # Closing parenthesis on next line 257 ret.append(')') 258 return '%s%s' % (class_line, separator.join(ret)) 259 %} 260 261 %define REPREXTEND(classname) 262 %extend classname { 263 public: 264 long __hash__() { return (long)self; } 265 266 %pythoncode %{ 267 def __repr__(self): 268 return _format_repr(self) 269 %} 270 } 271 272 %enddef 273 274 REPREXTEND(b2AABB); 275 REPREXTEND(b2AssertException); 276 REPREXTEND(b2Body); 277 REPREXTEND(b2BodyDef); 278 REPREXTEND(b2BroadPhase); 279 REPREXTEND(b2ChainShape); 280 REPREXTEND(b2CircleShape); 281 REPREXTEND(b2ClipVertex); 282 REPREXTEND(b2Color); 283 REPREXTEND(b2Contact); 284 REPREXTEND(b2ContactEdge); 285 REPREXTEND(b2ContactFeature); 286 REPREXTEND(b2ContactFilter); 287 REPREXTEND(b2ContactID); 288 REPREXTEND(b2ContactImpulse); 289 REPREXTEND(b2ContactListener); 290 REPREXTEND(b2ContactManager); 291 REPREXTEND(b2ContactPoint); 292 REPREXTEND(b2DestructionListener); 293 REPREXTEND(b2DistanceInput); 294 REPREXTEND(b2DistanceJoint); 295 REPREXTEND(b2DistanceJointDef); 296 REPREXTEND(b2DistanceOutput); 297 REPREXTEND(b2DistanceProxy); 298 REPREXTEND(b2Draw); 299 REPREXTEND(b2DrawExtended); 300 REPREXTEND(b2EdgeShape); 301 REPREXTEND(b2Filter); 302 REPREXTEND(b2Fixture); 303 REPREXTEND(b2FixtureDef); 304 REPREXTEND(b2FixtureProxy); 305 REPREXTEND(b2FrictionJoint); 306 REPREXTEND(b2FrictionJointDef); 307 REPREXTEND(b2GearJoint); 308 REPREXTEND(b2GearJointDef); 309 REPREXTEND(b2Jacobian); 310 REPREXTEND(b2Joint); 311 REPREXTEND(b2JointDef); 312 REPREXTEND(b2JointEdge); 313 REPREXTEND(b2WheelJoint); 314 REPREXTEND(b2WheelJointDef); 315 REPREXTEND(b2Manifold); 316 REPREXTEND(b2ManifoldPoint); 317 REPREXTEND(b2MassData); 318 REPREXTEND(b2Mat22); 319 REPREXTEND(b2Mat33); 320 REPREXTEND(b2MouseJoint); 321 REPREXTEND(b2MouseJointDef); 322 REPREXTEND(b2Pair); 323 REPREXTEND(b2PolygonShape); 324 REPREXTEND(b2PrismaticJoint); 325 REPREXTEND(b2PrismaticJointDef); 326 REPREXTEND(b2PulleyJoint); 327 REPREXTEND(b2PulleyJointDef); 328 REPREXTEND(b2QueryCallback); 329 REPREXTEND(b2RayCastCallback); 330 REPREXTEND(b2RayCastInput); 331 REPREXTEND(b2RayCastOutput); 332 REPREXTEND(b2RevoluteJoint); 333 REPREXTEND(b2RevoluteJointDef); 334 REPREXTEND(b2RopeJoint); 335 REPREXTEND(b2RopeJointDef); 336 REPREXTEND(b2Shape); 337 REPREXTEND(b2Sweep); 338 REPREXTEND(b2TOIInput); 339 REPREXTEND(b2TOIOutput); 340 REPREXTEND(b2Transform); 341 REPREXTEND(b2Vec2); 342 REPREXTEND(b2Vec3); 343 REPREXTEND(b2Version); 344 REPREXTEND(b2WeldJoint); 345 REPREXTEND(b2WeldJointDef); 346 REPREXTEND(b2World); 347 REPREXTEND(b2WorldManifold); 348