-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransform.py
122 lines (87 loc) · 3.33 KB
/
transform.py
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
import numpy as np
import theano
import theano.tensor as T
class Point():
def __init__(self, p):
self.p = p
class PointField():
def __init__(self, pf):
self.pf = pf
class VectorField():
def __init__(self, vf):
self.vf = vf
class RayField():
def __init__(self, origin, directions):
self.origin = T.as_tensor_variable(origin)
self.rays = T.as_tensor_variable(directions)
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, RayField):
o = x.origin
r = x.rays
origin = T.dot(self.m, [o[0], o[1], o[2], 1])[:3]
rays = T.concatenate([r, T.zeros_like(r)[:, :, :1]], axis=2)
rays = T.tensordot(self.m, rays, [1, 2]).T[:, :, :3]
return RayField(origin, rays)
#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))
def identity():
"""Returns the identity transform"""
return Transform(np.asarray(np.eye(4, 4),dtype=theano.config.floatX) , np.asarray(np.eye(4, 4), dtype=theano.config.floatX))
def translate(x):
"""Returns a transform to represent a translation"""
x = T.as_tensor_variable(x)
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):
"""Returns a transform to represent a scaling"""
x = T.as_tensor_variable(x)
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)
def rotate(angle, axis):
"""Returns a transform to represent a rotation"""
angle = T.as_tensor_variable(angle)
axis = T.as_tensor_variable(axis)
a = axis
radians = angle*np.pi/180.0
s = T.sin(radians)
c = T.cos(radians)
m = T.alloc(0., 4, 4)
m = T.set_subtensor(m[0,0], a[0] * a[0] + (1. - a[0] * a[0]) * c)
m = T.set_subtensor(m[0,1], a[0] * a[1] * (1. - c) - a[2] * s)
m = T.set_subtensor(m[0,2], a[0] * a[2] * (1. - c) + a[1] * s)
m = T.set_subtensor(m[1,0], a[0] * a[1] * (1. - c) + a[2] * s)
m = T.set_subtensor(m[1,1], a[1] * a[1] + (1. - a[1] * a[1]) * c)
m = T.set_subtensor(m[1,2], a[1] * a[2] * (1. - c) - a[0] * s)
m = T.set_subtensor(m[2,0], a[0] * a[2] * (1. - c) - a[1] * s)
m = T.set_subtensor(m[2,1], a[1] * a[2] * (1. - c) + a[0] * s)
m = T.set_subtensor(m[2,2], a[2] * a[2] + (1. - a[2] * a[2]) * c)
m = T.set_subtensor(m[3,3], 1)
return Transform(m, m.T)