-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatutils.py
More file actions
106 lines (90 loc) · 2.52 KB
/
matutils.py
File metadata and controls
106 lines (90 loc) · 2.52 KB
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
import numpy as np
def scaleMatrix(s):
s.append(1)
return np.diag(s)
def translationMatrix(t):
n = len(t)
T = np.identity(n+1,dtype='f')
T[:n,-1] = t
return T
def rotationMatrixZ(angle):
c = np.cos(angle)
s = np.sin(angle)
R = np.identity(4)
R[0,0] = c
R[0,1] = s
R[1,0] = -s
R[1,1] = c
return R
def rotationMatrixX(angle):
c = np.cos(angle)
s = np.sin(angle)
R = np.identity(4)
R[1,1] = c
R[1,2] = s
R[2,1] = -s
R[2,2] = c
return R
def rotationMatrixY(angle):
c = np.cos(angle)
s = np.sin(angle)
R = np.identity(4)
R[0,0] = c
R[0,2] = s
R[2,0] = -s
R[2,2] = c
return R
def poseMatrix(position=[0,0,0], orientation=0, scale=1):
'''
Returns a combined TRS matrix for the pose of a model.
:param position: the position of the model
:param orientation: the model orientation (for now assuming a rotation around the Y axis)
:param scale: the model scale, either a scalar for isotropic scaling, or vector of scale factors
:return: the 4x4 TRS matrix
'''
# apply the position and orientation of the object
R = rotationMatrixY(orientation)
T = translationMatrix(position)
# ... and the scale factor
if np.isscalar(scale):
scale = [scale, scale, scale]
S = scaleMatrix(scale)
return np.matmul(np.matmul(T,R),S)
def orthoMatrix(l,r,t,b,n,f):
'''
Returns an orthographic projection matrix
:param l: left clip plane
:param r: right clip plane
:param t: top clip plane
:param b: bottom clip plane
:param n: near clip plane
:param f: far clip plane
:return: A 4x4 orthographic projection matrix
'''
return np.array(
[
[2./(r-l), 0., 0., (r+l)/(r-l) ],
[0., -2./(t-b), 0., (t+b)/(t-b) ],
[0., 0., 2./(f-n), (f+n)/(f-n) ],
[0., 0., 0., 1. ]
]
)
def frustumMatrix(l,r,t,b,n,f):
return np.array(
[
[ 2*n/(r-l), 0, (r+l)/(r-l), 0 ],
[ 0, -2*n/(t-b), (t+b)/(t-b), 0 ],
[ 0, 0, -(f+n)/(f-n), -2*f*n/(f-n) ],
[ 0, 0, -1, 0 ]
]
)
# Homogeneous coordinates helpers
def homog(v):
return np.hstack([v,1])
def unhomog(vh):
return vh[:-1]/vh[-1]
def matmul(L):
R = L[0]
for M in L[1:]:
R = np.matmul(R,M)
return R