-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformations2.py
185 lines (146 loc) · 3.85 KB
/
transformations2.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
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
# coding=utf-8
"""
Daniel Calderon, CC3501, 2019-1
Transformation matrices for computer graphics
v2.0
"""
import numpy as np
def identity():
return np.identity(4, dtype=np.float32)
def uniformScale(s):
return np.array([
[s, 0, 0, 0],
[0, s, 0, 0],
[0, 0, s, 0],
[0, 0, 0, 1]], dtype=np.float32)
def scale(sx, sy, sz):
return np.array([
[sx, 0, 0, 0],
[0, sy, 0, 0],
[0, 0, sz, 0],
[0, 0, 0, 1]], dtype=np.float32)
def rotationX(theta):
sin_theta = np.sin(theta)
cos_theta = np.cos(theta)
return np.array([
[1, 0, 0, 0],
[0, cos_theta, -sin_theta, 0],
[0, sin_theta, cos_theta, 0],
[0, 0, 0, 1]], dtype=np.float32)
def rotationY(theta):
sin_theta = np.sin(theta)
cos_theta = np.cos(theta)
return np.array([
[cos_theta, 0, sin_theta, 0],
[0, 1, 0, 0],
[-sin_theta, 0, cos_theta, 0],
[0, 0, 0, 1]], dtype=np.float32)
def rotationZ(theta):
sin_theta = np.sin(theta)
cos_theta = np.cos(theta)
return np.array([
[cos_theta, -sin_theta, 0, 0],
[sin_theta, cos_theta, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], dtype=np.float32)
def rotationA(theta, axis):
s = np.sin(theta)
c = np.cos(theta)
assert axis.shape == (3,)
x = axis[0]
y = axis[1]
z = axis[2]
return np.array([
# First row
[c + (1 - c) * x * x,
(1 - c) * x * y - s * z,
(1 - c) * x * z + s * y,
0],
# Second row
[(1 - c) * x * y + s * z,
c + (1 - c) * y * y,
(1 - c) * y * z - s * x,
0],
# Third row
[(1 - c) * x * z - s * y,
(1 - c) * y * z + s * x,
c + (1 - c) * z * z,
0],
# Fourth row
[0, 0, 0, 1]], dtype=np.float32)
def translate(tx, ty, tz):
return np.array([
[1, 0, 0, tx],
[0, 1, 0, ty],
[0, 0, 1, tz],
[0, 0, 0, 1]], dtype=np.float32)
def shearing(xy, yx, xz, zx, yz, zy):
return np.array([
[1, xy, xz, 0],
[yx, 1, yz, 0],
[zx, zy, 1, 0],
[0, 0, 0, 1]], dtype=np.float32)
def matmul(mats):
out = mats[0]
for i in range(1, len(mats)):
out = np.matmul(out, mats[i])
return out
def frustum(left, right, bottom, top, near, far):
r_l = right - left
t_b = top - bottom
f_n = far - near
return np.array([
[2 * near / r_l,
0,
(right + left) / r_l,
0],
[0,
2 * near / t_b,
(top + bottom) / t_b,
0],
[0,
0,
-(far + near) / f_n,
-2 * near * far / f_n],
[0,
0,
-1,
0]], dtype=np.float32)
def perspective(fovy, aspect, near, far):
halfHeight = np.tan(np.pi * fovy / 360) * near
halfWidth = halfHeight * aspect
return frustum(-halfWidth, halfWidth, -halfHeight, halfHeight, near, far)
def ortho(left, right, bottom, top, near, far):
r_l = right - left
t_b = top - bottom
f_n = far - near
return np.array([
[2 / r_l,
0,
0,
-(right + left) / r_l],
[0,
2 / t_b,
0,
-(top + bottom) / t_b],
[0,
0,
-2 / f_n,
-(far + near) / f_n],
[0,
0,
0,
1]], dtype=np.float32)
def lookAt(eye, at, up):
forward = (at - eye)
forward = forward / np.linalg.norm(forward)
side = np.cross(forward, up)
side = side / np.linalg.norm(side)
newUp = np.cross(side, forward)
newUp = newUp / np.linalg.norm(newUp)
return np.array([
[side[0], side[1], side[2], -np.dot(side, eye)],
[newUp[0], newUp[1], newUp[2], -np.dot(newUp, eye)],
[-forward[0], -forward[1], -forward[2], np.dot(forward, eye)],
[0, 0, 0, 1]
], dtype=np.float32)