-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrix.py
61 lines (54 loc) · 1.42 KB
/
matrix.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
import numpy as np
class Matrix3D:
def translate(x: float, y: float, z: float):
return np.array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
x, y, z, 1,
]).reshape(4, 4)
def scale(x: float, y: float, z: float):
return np.array([
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1,
]).reshape(4, 4)
def rotateX(rad: float):
c = np.cos(rad)
s = np.sin(rad)
return np.array([
1, 0, 0, 0,
0, c, -s, 0,
0, s, c, 0,
0, 0, 0, 1,
]).reshape(4, 4)
def rotateY(rad: float):
c = np.cos(rad)
s = np.sin(rad)
return np.array([
c, 0, s, 0,
0, 1, 0, 0,
-s, 0, c, 0,
0, 0, 0, 1,
]).reshape(4, 4)
def rotateZ(rad: float):
c = np.cos(rad)
s = np.sin(rad)
return np.array([
c, -s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
]).reshape(4, 4)
def rotate(radX: float, radY: float, radZ: float):
return Matrix3D.compose([
Matrix3D.rotateX(radX),
Matrix3D.rotateY(radY),
Matrix3D.rotateZ(radZ)
])
def compose(m: list[np.ndarray]):
r = np.identity(4)
for _m in m:
r = np.matmul(_m, r)
return r