Last active
August 29, 2015 14:19
-
-
Save lebek/ac5c03cfbdd8ea46e76a to your computer and use it in GitHub Desktop.
simplified transformations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import theano | |
import theano.tensor as T | |
class Point(): | |
def __init__(self, p): | |
self.p = p | |
class Vector(): | |
def __init__(self, v): | |
self.v = v | |
class Ray(): | |
def __init__(self, origin, direction): | |
self.o = origin | |
self.d = direction | |
class Transform(): | |
def __init__(self, m, mInv): | |
self.m = m | |
self.mInv = mInv | |
def inverse(self): | |
return Transform(self.mInv, self.m) | |
def __mul__(self, other): | |
m = T.dot(self.m, other.m) | |
mInv = T.dot(other.mInv, self.mInv) | |
return Transform(m, mInv) | |
def __call__(self, x): | |
if isinstance(x, Point): | |
return Point(T.dot(self.m, [x.p[0], x.p[1], x.p[2], 1])[:3]) | |
elif isinstance(x, Vector): | |
return Vector(T.dot(self.m, [x.v[0], x.v[1], x.v[2], 0])[:3]) | |
elif isinstance(x, Ray): | |
return Ray(self(x.o), self(x.d)) | |
class SceneObject(): | |
def __init__(self, w2o): | |
self.w2o = w2o | |
def distance(self, ray): | |
return self.w2o(ray) | |
def translate(x): | |
"""Returns a transform matrix to represent a translation""" | |
m = T.eye(4, 4) | |
m = T.set_subtensor(m[0,3], x[0]) | |
m = T.set_subtensor(m[1,3], x[1]) | |
m = T.set_subtensor(m[2,3], x[2]) | |
mInv = T.eye(4, 4) | |
mInv = T.set_subtensor(mInv[0,3], -x[0]) | |
mInv = T.set_subtensor(mInv[1,3], -x[1]) | |
mInv = T.set_subtensor(mInv[2,3], -x[2]) | |
return Transform(m, mInv) | |
def scale(x): | |
"""Creates a transform matrix to represent a translation""" | |
m = T.eye(4, 4) | |
m = T.set_subtensor(m[0,0], x[0]) | |
m = T.set_subtensor(m[1,1], x[1]) | |
m = T.set_subtensor(m[2,2], x[2]) | |
mInv = T.eye(4, 4) | |
mInv = T.set_subtensor(mInv[0,0], 1./x[0]) | |
mInv = T.set_subtensor(mInv[1,1], 1./x[1]) | |
mInv = T.set_subtensor(mInv[2,2], 1./x[2]) | |
return Transform(m, mInv) | |
center = theano.shared(value=np.array([3., 3., 3.]), name='center', | |
borrow=True) | |
size = theano.shared(value=np.array([2., 2., 2.]), name='size', | |
borrow=True) | |
# Test... | |
w2o = scale(size) * translate(center) | |
point = Point(T.as_tensor_variable([1, 1, 2])) | |
vector = Vector(T.as_tensor_variable([1, 1, 2])) | |
ray = Ray(point, vector) | |
s = SceneObject(w2o) | |
print theano.function([], s.distance(ray).o.p)() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment