-
Notifications
You must be signed in to change notification settings - Fork 1
/
transforms.py
148 lines (126 loc) · 3.88 KB
/
transforms.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Very simple transformation library that is needed for some examples.
"""
import math
import numpy
import numpy as np
def translate(M, x, y=None, z=None):
"""
translate produces a translation by (x, y, z) .
Parameters
----------
x, y, z
Specify the x, y, and z coordinates of a translation vector.
"""
if y is None: y = x
if z is None: z = x
T = [[ 1, 0, 0, x],
[ 0, 1, 0, y],
[ 0, 0, 1, z],
[ 0, 0, 0, 1]]
T = np.array(T, dtype=np.float32).T
M[...] = np.dot(M,T)
def scale(M, x, y=None, z=None):
"""
scale produces a non uniform scaling along the x, y, and z axes. The three
parameters indicate the desired scale factor along each of the three axes.
Parameters
----------
x, y, z
Specify scale factors along the x, y, and z axes, respectively.
"""
if y is None: y = x
if z is None: z = x
S = [[ x, 0, 0, 0],
[ 0, y, 0, 0],
[ 0, 0, z, 0],
[ 0, 0, 0, 1]]
S = np.array(S,dtype=np.float32).T
M[...] = np.dot(M,S)
def xrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
[[ 1.0, 0.0, 0.0, 0.0 ],
[ 0.0, cosT,-sinT, 0.0 ],
[ 0.0, sinT, cosT, 0.0 ],
[ 0.0, 0.0, 0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)
def yrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
[[ cosT, 0.0, sinT, 0.0 ],
[ 0.0, 1.0, 0.0, 0.0 ],
[-sinT, 0.0, cosT, 0.0 ],
[ 0.0, 0.0, 0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)
def zrotate(M,theta):
t = math.pi*theta/180
cosT = math.cos( t )
sinT = math.sin( t )
R = numpy.array(
[[ cosT,-sinT, 0.0, 0.0 ],
[ sinT, cosT, 0.0, 0.0 ],
[ 0.0, 0.0, 1.0, 0.0 ],
[ 0.0, 0.0, 0.0, 1.0 ]], dtype=np.float32)
M[...] = np.dot(M,R)
def rotate(M, angle, x, y, z, point=None):
"""
rotate produces a rotation of angle degrees around the vector (x, y, z).
Parameters
----------
M
Current transformation as a numpy array
angle
Specifies the angle of rotation, in degrees.
x, y, z
Specify the x, y, and z coordinates of a vector, respectively.
"""
angle = math.pi*angle/180
c,s = math.cos(angle), math.sin(angle)
n = math.sqrt(x*x+y*y+z*z)
x /= n
y /= n
z /= n
cx,cy,cz = (1-c)*x, (1-c)*y, (1-c)*z
R = numpy.array([[ cx*x + c , cy*x - z*s, cz*x + y*s, 0],
[ cx*y + z*s, cy*y + c , cz*y - x*s, 0],
[ cx*z - y*s, cy*z + x*s, cz*z + c, 0],
[ 0, 0, 0, 1]]).T
M[...] = np.dot(M,R)
def ortho( left, right, bottom, top, znear, zfar ):
assert( right != left )
assert( bottom != top )
assert( znear != zfar )
M = np.zeros((4,4), dtype=np.float32)
M[0,0] = +2.0/(right-left)
M[3,0] = -(right+left)/float(right-left)
M[1,1] = +2.0/(top-bottom)
M[3,1] = -(top+bottom)/float(top-bottom)
M[2,2] = -2.0/(zfar-znear)
M[3,2] = -(zfar+znear)/float(zfar-znear)
M[3,3] = 1.0
return M
def frustum( left, right, bottom, top, znear, zfar ):
assert( right != left )
assert( bottom != top )
assert( znear != zfar )
M = np.zeros((4,4), dtype=np.float32)
M[0,0] = +2.0*znear/(right-left)
M[2,0] = (right+left)/(right-left)
M[1,1] = +2.0*znear/(top-bottom)
M[3,1] = (top+bottom)/(top-bottom)
M[2,2] = -(zfar+znear)/(zfar-znear)
M[3,2] = -2.0*znear*zfar/(zfar-znear)
M[2,3] = -1.0
return M
def perspective(fovy, aspect, znear, zfar):
assert( znear != zfar )
h = np.tan(fovy / 360.0 * np.pi) * znear
w = h * aspect
return frustum( -w, w, -h, h, znear, zfar )