Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add & speed up PVector operations, clean up namespace #44

Merged
merged 4 commits into from Apr 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 43 additions & 5 deletions runtime/src/jycessing/core.py
Expand Up @@ -106,11 +106,11 @@ def sub(cls, a, b, dest=None):

@classmethod
def mult(cls, a, b, dest=None):
return __pvector__.mult(a, b, dest)
return __pvector__.mult(a, float(b), dest)

@classmethod
def div(cls, a, b, dest=None):
return __pvector__.div(a, b, dest)
return __pvector__.div(a, float(b), dest)

@classmethod
def cross(cls, a, b, dest=None):
Expand Down Expand Up @@ -138,17 +138,55 @@ def decorator(func):

@monkeypatch_method(__pvector__)
def __sub__(a, b):
return PVector(a.x - b.x, a.y - b.y, a.z - b.z)
return __pvector__.sub(a, b, None)

@monkeypatch_method(__pvector__)
def __isub__(a, b):
a.sub(b)
return a

@monkeypatch_method(__pvector__)
def __add__(a, b):
return PVector(a.x + b.x, a.y + b.y, a.z + b.z)
return __pvector__.add(a, b, None)

@monkeypatch_method(__pvector__)
def __iadd__(a, b):
a.add(b)
return a

@monkeypatch_method(__pvector__)
def __mul__(a, b):
if isinstance(b, __pvector__):
raise TypeError("The * operator can only be used to multiply a PVector by a scalar")
return PVector(a.x * b, a.y * b, a.z * b)
return __pvector__.mult(a, float(b), None)

@monkeypatch_method(__pvector__)
def __rmul__(a, b):
if isinstance(b, __pvector__):
raise TypeError("The * operator can only be used to multiply a PVector by a scalar")
return __pvector__.mult(a, float(b), None)

@monkeypatch_method(__pvector__)
def __imul__(a, b):
if isinstance(b, __pvector__):
raise TypeError("The *= operator can only be used to multiply a PVector by a scalar")
a.mult(float(b))
return a

@monkeypatch_method(__pvector__)
def __div__(a, b):
if isinstance(b, __pvector__):
raise TypeError("The / operator can only be used to divide a PVector by a scalar")
return __pvector__.div(a, float(b), None)

@monkeypatch_method(__pvector__)
def __idiv__(a, b):
if isinstance(b, __pvector__):
raise TypeError("The /= operator can only be used to divide a PVector by a scalar")
a.div(float(b))
return a

del __sub__, __isub__, __add__, __iadd__, __mul__, __rmul__, __imul__, __div__, __idiv__

# Now expose the funky PVector class as a builtin.
__builtin__.PVector = PVector
Expand Down
35 changes: 35 additions & 0 deletions testing/resources/test_pvector.py
Expand Up @@ -31,6 +31,40 @@
assert a.cross(b) == PVector(-260.0, 280.0, -60.0)
assert PVector.dot(a, b) == 24110.0

d = a.get()
d += b
assert d == a + b
d = a.get()
d -= c
assert d == a - c
d = a.get()
d *= 5.0
assert d == a * 5.0
d = a.get()
d /= 5.0
assert d == a / 5.0

assert b * 5 == b * 5.0
assert b / 5 == b / 5.0
d = b.get()
d *= 391
assert d == b * 391.0
d = b.get()
d /= 10203
assert d == b / 10203.0

d = a.get()
d += a + a
assert d == a + a + a

assert a * 57.0 == 57.0 * a

assert (a / 5.0) == (1.0 / 5.0) * a

m, n = b, c
a += b * 5 - c / 2 + PVector(0, 1, 2)
assert (m, n) == (b, c)

import copy
x = [a, b]
y = copy.deepcopy(x)
Expand All @@ -39,6 +73,7 @@
x[0].sub(PVector(100, 100, 100))
assert x != y


print 'OK'

exit()