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
|
/*
* pybox2d -- http://pybox2d.googlecode.com
*
* Copyright (c) 2010 Ken Lauer / 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.
*/
//These operators do not work unless explicitly defined like this
%ignore operator + (const b2Vec2& a, const b2Vec2& b);
%ignore operator + (const b2Mat22& A, const b2Mat22& B);
%ignore operator - (const b2Vec2& a, const b2Vec2& b);
%ignore operator * (float32 s, const b2Vec2& a);
%ignore operator == (const b2Vec2& a, const b2Vec2& b);
%ignore operator * (float32 s, const b2Vec3& a);
%ignore operator + (const b2Vec3& a, const b2Vec3& b);
%ignore operator - (const b2Vec3& a, const b2Vec3& 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);
%rename(__add_vector) b2Vec3::operator += (const b2Vec3& v);
%rename(__sub_vector) b2Vec3::operator -= (const b2Vec3& v);
%rename(__mul_float ) b2Vec3::operator *= (float32 a);
/**** Vector classes ****/
%extend b2Vec2 {
public:
b2Vec2() {
return new b2Vec2(0.0f, 0.0f);
}
b2Vec2(b2Vec2& other) {
return new b2Vec2(other.x, other.y);
}
%pythoncode %{
__iter__ = lambda self: iter( (self.x, self.y) )
__eq__ = lambda self, other: self.__equ(other)
__ne__ = lambda self,other: not self.__equ(other)
def __repr__(self):
return "b2Vec2(%g,%g)" % (self.x, self.y)
def __len__(self):
return 2
def __neg__(self):
return b2Vec2(-self.x, -self.y)
def copy(self):
"""
Return a copy of the vector.
Remember that the following:
a = b2Vec2()
b = a
Does not copy the vector itself, but b now refers to a.
"""
return b2Vec2(self.x, self.y)
__copy__ = copy
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 __itruediv__(self, a):
self.__div_float(a)
return self
def __idiv__(self, a):
self.__div_float(a)
return self
def __set(self, x, y):
self.x = x
self.y = y
def __nonzero__(self):
return self.x!=0.0 or self.y!=0.0
tuple = property(lambda self: (self.x, self.y), lambda self, value: self.__set(*value))
length = property(__Length, None)
lengthSquared = property(__LengthSquared, None)
valid = property(__IsValid, None)
skew = property(__Skew, None)
%}
float32 cross(b2Vec2& other) {
return $self->x * other.y - $self->y * other.x;
}
b2Vec2 cross(float32 s) {
return b2Vec2(s * $self->y, -s * $self->x);
}
float32 __getitem__(int i) {
if (i==0)
return $self->x;
else if (i==1)
return $self->y;
PyErr_SetString(PyExc_IndexError, "Index must be in (0,1)");
return 0.0f;
}
void __setitem__(int i, float32 value) {
if (i==0)
$self->x=value;
else if (i==1)
$self->y=value;
else
PyErr_SetString(PyExc_IndexError, "Index must be in (0,1)");
}
bool __equ(b2Vec2& other) {
return ($self->x == other.x && $self->y == other.y);
}
float32 dot(b2Vec2& other) {
return $self->x * other.x + $self->y * other.y;
}
b2Vec2 __truediv__(float32 a) { //python 3k
return b2Vec2($self->x / a, $self->y / a);
}
b2Vec2 __div__(float32 a) {
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) {
return b2Vec2($self->x / a, $self->y / a);
}
void __div_float(float32 a) {
$self->x /= a;
$self->y /= a;
}
}
%rename (__Length) b2Vec2::Length;
%rename (__LengthSquared) b2Vec2::LengthSquared;
%rename (__IsValid) b2Vec2::IsValid;
%rename (__Skew) b2Vec2::Skew;
%extend b2Vec3 {
public:
b2Vec3() {
return new b2Vec3(0.0f, 0.0f, 0.0f);
}
b2Vec3(b2Vec3& other) {
return new b2Vec3(other.x, other.y, other.z);
}
b2Vec3(b2Vec2& other) {
return new b2Vec3(other.x, other.y, 0.0f);
}
%pythoncode %{
__iter__ = lambda self: iter( (self.x, self.y, self.z) )
__eq__ = lambda self, other: (self.x == other.x and self.y == other.y and self.z == other.z)
__ne__ = lambda self, other: (self.x != other.x or self.y != other.y or self.z != other.z)
def __repr__(self):
return "b2Vec3(%g,%g,%g)" % (self.x, self.y, self.z)
def __len__(self):
return 3
def __neg__(self):
return b2Vec3(-self.x, -self.y, -self.z)
def copy(self):
"""
Return a copy of the vector.
Remember that the following:
a = b2Vec3()
b = a
Does not copy the vector itself, but b now refers to a.
"""
return b2Vec3(self.x, self.y, self.z)
__copy__ = copy
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 __itruediv__(self, a):
self.__div_float(a)
return self
def __idiv__(self, a):
self.__div_float(a)
return self
def dot(self, v):
"""
Dot product with v (list/tuple or b2Vec3)
"""
if isinstance(v, (list, tuple)):
return self.x*v[0] + self.y*v[1] + self.z*v[2]
else:
return self.x*v.x + self.y*v.y + self.z*v.z
def __set(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __nonzero__(self):
return self.x!=0.0 or self.y!=0.0 or self.z!=0.0
tuple = property(lambda self: (self.x, self.y, self.z), lambda self, value: self.__set(*value))
length = property(_Box2D.b2Vec3___Length, None)
lengthSquared = property(_Box2D.b2Vec3___LengthSquared, None)
valid = property(_Box2D.b2Vec3___IsValid, None)
%}
b2Vec3 cross(b2Vec3& b) {
return b2Vec3($self->y * b.z - $self->z * b.y, $self->z * b.x - $self->x * b.z, $self->x * b.y - $self->y * b.x);
}
float32 __getitem__(int i) {
if (i==0)
return $self->x;
else if (i==1)
return $self->y;
else if (i==2)
return $self->z;
PyErr_SetString(PyExc_IndexError, "Index must be in (0,1,2)");
return 0.0f;
}
void __setitem__(int i, float32 value) {
if (i==0)
$self->x=value;
else if (i==1)
$self->y=value;
else if (i==2)
$self->z=value;
else
PyErr_SetString(PyExc_IndexError, "Index must be in (0,1,2)");
}
bool __IsValid() {
return b2IsValid($self->x) && b2IsValid($self->y) && b2IsValid($self->z);
}
float32 __Length() {
return b2Sqrt($self->x * $self->x + $self->y * $self->y + $self->z * $self->z);
}
float32 __LengthSquared() {
return ($self->x * $self->x + $self->y * $self->y + $self->z * $self->z);
}
b2Vec3 __truediv__(float32 a) {
return b2Vec3($self->x / a, $self->y / a, $self->z / a);
}
b2Vec3 __div__(float32 a) {
return b2Vec3($self->x / a, $self->y / a, $self->z / a);
}
b2Vec3 __mul__(float32 a) {
return b2Vec3($self->x * a, $self->y * a, $self->z * a);
}
b2Vec3 __add__(b2Vec3* other) {
return b2Vec3($self->x + other->x, $self->y + other->y, $self->z + other->z);
}
b2Vec3 __sub__(b2Vec3* other) {
return b2Vec3($self->x - other->x, $self->y - other->y, $self->z - other->z);
}
b2Vec3 __rmul__(float32 a) {
return b2Vec3($self->x * a, $self->y * a, $self->z * a);
}
b2Vec3 __rdiv__(float32 a) {
return b2Vec3($self->x / a, $self->y / a, self->z / a);
}
void __div_float(float32 a) {
$self->x /= a;
$self->y /= a;
$self->z /= a;
}
}
/**** Mat22 ****/
%extend b2Mat22 {
public:
b2Mat22() {
return new b2Mat22(b2Vec2(1.0f, 0.0f), b2Vec2(0.0f, 1.0f));
}
// backward-compatibility
float32 __GetAngle() const
{
return b2Atan2($self->ex.y, $self->ex.x);
}
void __SetAngle(float32 angle)
{
float32 c = cosf(angle), s = sinf(angle);
$self->ex.x = c; $self->ey.x = -s;
$self->ex.y = s; $self->ey.y = c;
}
%pythoncode %{
# Read-only
inverse = property(__GetInverse, None)
angle = property(__GetAngle, __SetAngle)
ex = property(lambda self: self.col1,
lambda self, v: setattr(self, 'col1', v))
ey = property(lambda self: self.col2,
lambda self, v: setattr(self, 'col2', v))
set = __SetAngle
%}
b2Vec2 __mul__(b2Vec2* v) {
return b2Vec2($self->ex.x * v->x + $self->ey.x * v->y,
$self->ex.y * v->x + $self->ey.y * v->y);
}
b2Mat22 __mul__(b2Mat22* m) {
return b2Mat22(b2Mul(*($self), m->ex), b2Mul(*($self), m->ey));
}
b2Mat22 __add__(b2Mat22* m) {
return b2Mat22($self->ex + m->ex, $self->ey + m->ey);
}
b2Mat22 __sub__(b2Mat22* m) {
return b2Mat22($self->ex - m->ex, $self->ey - m->ey);
}
void __iadd(b2Mat22* m) {
$self->ex += m->ex;
$self->ey += m->ey;
}
void __isub(b2Mat22* m) {
$self->ex -= m->ex;
$self->ey -= m->ey;
}
}
%rename(__SetAngle) b2Mat22::Set;
%rename(__GetInverse) b2Mat22::GetInverse;
%rename(col1) b2Mat22::ex;
%rename(col2) b2Mat22::ey;
%feature("shadow") b2Mat22::__iadd__ {
def __iadd__(self, other):
self.__iadd(other)
return self
}
%feature("shadow") b2Mat22::__isub__ {
def __iadd__(self, other):
self.__iadd(other)
return self
}
/**** Mat33 ****/
%extend b2Mat33 {
public:
b2Mat33() {
return new b2Mat33(b2Vec3(1.0f, 0.0f, 0.0f),
b2Vec3(0.0f, 1.0f, 0.0f),
b2Vec3(0.0f, 0.0f, 1.0f));
}
%pythoncode %{
ex = property(lambda self: self.col1, lambda self, v: setattr(self, 'col1', v))
ey = property(lambda self: self.col2, lambda self, v: setattr(self, 'col2', v))
ez = property(lambda self: self.col3, lambda self, v: setattr(self, 'col3', v))
%}
b2Vec3 __mul__(b2Vec3& v) {
return v.x * $self->ex + v.y * $self->ey + v.z * $self->ez;
}
b2Mat33 __add__(b2Mat33* other) {
return b2Mat33($self->ex + other->ex, $self->ey + other->ey, $self->ez + other->ez);
}
b2Mat33 __sub__(b2Mat33* other) {
return b2Mat33($self->ex - other->ex, $self->ey - other->ey, $self->ez - other->ez);
}
void __iadd(b2Mat33* other) {
$self->ex += other->ex;
$self->ey += other->ey;
$self->ez += other->ez;
}
void __isub(b2Mat33* other) {
$self->ex -= other->ex;
$self->ey -= other->ey;
$self->ez -= other->ez;
}
}
%feature("shadow") b2Mat33::__iadd__ {
def __iadd__(self, other):
self.__iadd(other)
return self
}
%feature("shadow") b2Mat33::__isub__ {
def __isub__(self, other):
self.__isub(other)
return self
}
%rename(set) b2Mat33::Set;
%rename(col1) b2Mat33::ex;
%rename(col2) b2Mat33::ey;
%rename(col3) b2Mat33::ez;
/**** Transform ****/
%extend b2Transform {
public:
b2Rot __get_rotation_matrix() {
return $self->q;
}
%pythoncode %{
def __get_angle(self):
return self.q.angle
def __set_angle(self, angle):
self.q.angle = angle
def __set_rotation_matrix(self, rot_matrix):
self.q.angle = rot_matrix.angle
angle = property(__get_angle, __set_angle)
R = property(__get_rotation_matrix, __set_rotation_matrix)
%}
b2Vec2 __mul__(b2Vec2& v) {
float32 x = ($self->q.c * v.x - $self->q.s * v.y) + $self->p.x;
float32 y = ($self->q.s * v.x + $self->q.c * v.y) + $self->p.y;
return b2Vec2(x, y);
}
}
%rename(position) b2Transform::p;
/**** Rot ****/
%extend b2Rot {
public:
%pythoncode %{
angle = property(__GetAngle, __SetAngle)
x_axis = property(GetXAxis, None)
y_axis = property(GetYAxis, None)
%}
b2Vec2 __mul__(b2Vec2& v) {
return b2Mul(*($self), v);
}
}
%rename(__SetAngle) b2Rot::Set;
%rename(__GetAngle) b2Rot::GetAngle;
/**** AABB ****/
%rename(__contains__) b2AABB::Contains;
%rename(__IsValid) b2AABB::IsValid;
%rename(__GetExtents) b2AABB::GetExtents;
%rename(__GetCenter) b2AABB::GetCenter;
%rename(__GetPerimeter) b2AABB::GetPerimeter;
%include "Box2D/Collision/b2Collision.h"
%extend b2AABB {
public:
%pythoncode %{
# Read-only
valid = property(__IsValid, None)
extents = property(__GetExtents, None)
center = property(__GetCenter, None)
perimeter = property(__GetPerimeter, None)
%}
bool __contains__(const b2Vec2& point) {
//If point is in aabb (including a small buffer around it), return true.
if (point.x < ($self->upperBound.x + b2_epsilon) &&
point.x > ($self->lowerBound.x - b2_epsilon) &&
point.y < ($self->upperBound.y + b2_epsilon) &&
point.y > ($self->lowerBound.y - b2_epsilon))
return true;
return false;
}
bool overlaps(const b2AABB& aabb2) {
//If aabb and aabb2 overlap, return true. (modified from b2BroadPhase::InRange)
b2Vec2 d = b2Max($self->lowerBound - aabb2.upperBound, aabb2.lowerBound - $self->upperBound);
return b2Max(d.x, d.y) < 0.0f;
}
}
|