-
Notifications
You must be signed in to change notification settings - Fork 1
/
vecutils.py
302 lines (265 loc) · 7.5 KB
/
vecutils.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from __future__ import division
from math import sin, cos, degrees, radians, tan
import numpy
import numpy as np
from numpy import array, matrix, linalg
from numpy import float32
farray = float32
def fvec(*args):
return farray(args)
def sind(x):
return sin(radians(x))
def cosd(x):
return cos(radians(x))
def tand(x):
return tan(radians(x))
def polar(*args):
"""
polar(pi/6) -> (cos(pi/6), sin(pi/6)) = vec2(0.86602539, 0.5)
polar(5, pi/6) -> (5 * cos(pi/6), 5 * sin(pi/6)) = vec2(4.33012676, 2.5)
"""
if len(args) == 2:
r,t = args
elif len(args) == 1:
r,t = 1, args[0]
else:
raise TypeError('Accept 1 or 2 arguments')
return r * vec2(cos(t), sin(t))
def polard(*args):
"""
polard(30) -> (cosd(30), sind(30)) = vec2(0.86602539, 0.5)
polard(5, 30) -> (5 * cosd(30), 5 * sind(30)) = vec2(4.33012676, 2.5)
"""
if len(args) == 2:
r,t = args
elif len(args) == 1:
r,t = 1, args[0]
else:
raise TypeError('Accept 1 or 2 arguments')
return r * polar(radians(t))
NAME_TO_INT = {
'x': 0,
'y': 1,
'z': 2,
'w': 3,
'r': 0,
'g': 1,
'b': 2,
'a': 3,
}
def readvec(V, names):
"""
Returns numpy.array from values of vector V depending on arguments:
readvec((1,2,3), 'xy') -> farray((1,2))
readvec((1,2,3), 'rg') -> farray((1,2))
readvec((1,2,3), 'zx') -> farray((3,1))
readvec((1,2,3), 'zz') -> farray((3,3))
If the funcoperators lib is installed, I'd suggest defining functions like xy
(1,2,3)|xy -> farray((1,2))
Given:
xy = postfix(lambda vec: readvec(vec, 'xy'))
"""
return farray([
V[NAME_TO_INT[x]]
for i,x in enumerate(names)])
def vec_from_kwargs(size, **kwargs):
V = farray((0,) * size)
for x,v in kwargs.items():
if len(x) == 1:
V[NAME_TO_INT[x]] = v
else:
it = iter(v)
for c in x:
V[NAME_TO_INT[c]] = next(it)
return V
def vec_from_args(size, *args):
if len(args) == 0:
return farray((0,) * size)
if len(args) == 1 and not hasattr(args[0], '__iter__'):
return farray((args[0],) * size)
L = []
for a in args:
if hasattr(a, '__iter__'):
L.extend(a)
else:
L.append(a)
if not len(L) == size:
raise TypeError('Accept {} arguments'.format(size))
return farray(L)
def vec2(*args, **kwargs):
"""
vec2(1,2) -> farray((1.0, 2.0))
vec2(5) -> farray((5.0, 5.0))
vec2(y=6, x=1) -> farray((1.0, 6.0))
"""
if args and kwargs:
raise TypeError('vec2 accept either args or kwargs, not both')
elif kwargs:
return vec_from_kwargs(2, **kwargs)
else:
return vec_from_args(2, *args)
def vec3(*args, **kwargs):
"""
Returns numpy.array depending on arguments:
vec3() -> (0,0,0)
vec3(5) -> (5,5,5)
vec3(1,2,3) -> (1,2,3)
vec3((1,2,3)) -> (1,2,3)
vec3((1,2),3) -> (1,2,3)
vec3(1,(2,3)) -> (1,2,3)
vec3(x=1,y=2,z=3) -> (1,2,3)
vec3(r=1,g=2,b=3) -> (1,2,3)
vec3(xy=(1,2),z=3) -> (1,2,3)
vec3(x=1,yz=(2,3)) -> (1,2,3)
vec3(y=2,xz=(1,3)) -> (1,2,3)
vec3(xyz=(1,2,3)) -> (1,2,3)
"""
if args and kwargs:
raise TypeError('vec3 accept either args or kwargs, not both')
elif kwargs:
return vec_from_kwargs(3, **kwargs)
else:
return vec_from_args(3, *args)
def vec4(*args, **kwargs):
"""
vec4(5) -> (5,5,5,5)
vec4(1,2,3,4) -> (1,2,3,4)
vec4(xy=(1,2), z=3, w=4) -> (1,2,3,4)
vec4((1,2,3), 4) -> (1,2,3,4)
...
"""
if args and kwargs:
raise TypeError('vec3 accept either args or kwargs, not both')
elif kwargs:
return vec_from_kwargs(4, **kwargs)
else:
return vec_from_args(4, *args)
def normalized(v):
return v / linalg.norm(v)
def PerspectiveMatrix(fovy, aspect, zNear, zFar):
f = 1.0 / tan(radians(fovy) / 2.0)
return farray([
[f/aspect, 0, 0, 0],
[0, f, 0, 0],
[0, 0, 1.0 * (zFar + zNear) / (zNear - zFar), 2.0 * zFar * zNear / (zNear - zFar)],
[0,0,-1,0]
])
def TranslationMatrix(*args):
"""
TranslationMatrix(x, y, z)
TranslationMatrix((x, y, z))
TranslationMatrix(x, y) # z = 0
"""
if len(args) == 3:
tx,ty,tz = args
elif len(args) == 2:
(tx,ty),tz = args, 0
elif len(args) == 1:
tx,ty,tz = args[0]
else:
raise TypeError("Accept 1, 2 or 3 arguments")
return farray([
[1, 0, 0, tx],
[0, 1, 0, ty],
[0, 0, 1, tz],
[0, 0, 0, 1]
])
def LookAtMatrix(*args):
"""
LookAtMatrix((camera_x, camera_y, camera_z), (target_x, target_y, target_z), (up_x, up_y, up_z))
LookAtMatrix(camera_x, camera_y, camera_z, target_x, target_y, target_z, up_x, up_y, up_z)
"""
if len(args) == 3:
e,c,ur = args
elif len(args) == 9:
e,c,ur = args[:3], args[3:6], args[6:]
else:
raise TypeError("Accept 3 or 9 arguments")
e,c,ur = array(e), array(c), array(ur)
U = normalized(ur)
f = normalized(c - e)
s = numpy.cross(f, U)
u = numpy.cross(normalized(s), f)
return farray([
[ s[0], s[1], s[2], 0],
[ u[0], u[1], u[2], 0],
[-f[0], -f[1], -f[2], 0],
[ 0, 0, 0, 1],
]).dot(
TranslationMatrix(-e)
)
class Axis:
X = 0
Y = 1
Z = 2
def AxisRotationMatrix(angle, axis=Axis.Z):
if angle % 90 == 0:
x = angle % 360
c = 1 if x == 0 else -1 if x == 180 else 0
s = 1 if x == 90 else -1 if x == 270 else 0
else:
t = radians(angle)
c = cos(t)
s = sin(t)
return farray([
[c, s, 0, 0],
[-s, c, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
] if axis == 2 else [
[c, 0, s, 0],
[0, 1, 0, 0],
[-s, 0, c, 0],
[0, 0, 0, 1]
] if axis == 1 else [
[1, 0, 0, 0],
[0, c, -s, 0],
[0, s, c, 0],
[0, 0, 0, 1]
])
def RotationMatrix(angle, axe):
"""Rotation matrix for angle in degree around any axe
RotationMatrix(30, (0,0,1))
"""
x, y, z = normalized(axe)
if angle % 90 == 0:
a = angle % 360
c = 1 if a == 0 else -1 if a == 180 else 0
s = 1 if a == 90 else -1 if a == 270 else 0
else:
t = radians(angle)
c = cos(t)
s = sin(t)
k = 1 - c
# Rodriguez rotation formula
return farray([
[x * x * k + c, x * y * k - z * s, x * z * k + y * s, 0],
[y * x * k + z * s, y * y * k + c, y * z * k - x * s, 0],
[x * z * k - y * s, y * z * k + x * s, z * z * k + c, 0],
[0, 0, 0, 1]
])
def ScaleMatrix(kx, ky=None, kz=None):
"""ScaleMatrix
ScaleMatrix(2) = ScaleMatrix(2,2,2)
ScaleMatrix(1,2,3)
"""
if ky is None:
ky = kx
if kz is None:
kz = kx
return farray([
[kx, 0, 0, 0],
[0, ky, 0, 0],
[0, 0, kz, 0],
[0, 0, 0, 1]
])
def IdentityMatrix():
"""IdentityMatrix
IdentityMatrix()
"""
return farray([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])