-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
152 lines (109 loc) · 4.05 KB
/
matrix.py
File metadata and controls
152 lines (109 loc) · 4.05 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
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
# This file was copied verbatim from obiwac's Github.
# There was no explanation offered, so I do not have extensive knowledge on it.
# This is matrix math, meant to create and modify transform, scaling, and rotation matrices.
import copy
import math
def copy_matrix(matrix):
return copy.deepcopy(matrix) # we need to use deepcopy since we're dealing with 2D arrays
clean_matrix = [[0.0] * 4 for _ in range(4)]
identity_matrix = copy_matrix(clean_matrix)
identity_matrix[0][0] = 1.0
identity_matrix[1][1] = 1.0
identity_matrix[2][2] = 1.0
identity_matrix[3][3] = 1.0
def multiply_matrices(x_matrix, y_matrix):
result_matrix = copy_matrix(clean_matrix)
for i in range(4):
for j in range(4):
result_matrix[i][j] = (
(x_matrix[0][j] * y_matrix[i][0])
+ (x_matrix[1][j] * y_matrix[i][1])
+ (x_matrix[2][j] * y_matrix[i][2])
+ (x_matrix[3][j] * y_matrix[i][3])
)
return result_matrix
class Matrix:
def __init__(self, base=None):
if isinstance(base, Matrix):
self.data = copy_matrix(base.data)
elif isinstance(base, list):
self.data = copy_matrix(base)
else:
self.data = copy_matrix(clean_matrix)
def load_identity(self):
self.data = copy_matrix(identity_matrix)
def __mul__(self, matrix):
return Matrix(multiply_matrices(self.data, matrix.data))
def __imul__(self, matrix):
self.data = multiply_matrices(self.data, matrix.data)
def scale(self, x, y, z):
for i in range(4):
self.data[0][i] *= x
for i in range(4):
self.data[1][i] *= y
for i in range(4):
self.data[2][i] *= z
def translate(self, x, y, z):
for i in range(4):
self.data[3][i] = self.data[3][i] + (self.data[0][i] * x + self.data[1][i] * y + self.data[2][i] * z)
def rotate(self, angle, x, y, z):
magnitude = math.sqrt(x * x + y * y + z * z)
x /= -magnitude
y /= -magnitude
z /= -magnitude
sin_angle = math.sin(angle)
cos_angle = math.cos(angle)
one_minus_cos = 1.0 - cos_angle
xx = x * x
yy = y * y
zz = z * z
xy = x * y
yz = y * z
zx = z * x
xs = x * sin_angle
ys = y * sin_angle
zs = z * sin_angle
rotation_matrix = copy_matrix(clean_matrix)
rotation_matrix[0][0] = (one_minus_cos * xx) + cos_angle
rotation_matrix[0][1] = (one_minus_cos * xy) - zs
rotation_matrix[0][2] = (one_minus_cos * zx) + ys
rotation_matrix[1][0] = (one_minus_cos * xy) + zs
rotation_matrix[1][1] = (one_minus_cos * yy) + cos_angle
rotation_matrix[1][2] = (one_minus_cos * yz) - xs
rotation_matrix[2][0] = (one_minus_cos * zx) - ys
rotation_matrix[2][1] = (one_minus_cos * yz) + xs
rotation_matrix[2][2] = (one_minus_cos * zz) + cos_angle
rotation_matrix[3][3] = 1.0
self.data = multiply_matrices(self.data, rotation_matrix)
def rotate_2d(self, x, y):
self.rotate(x, 0, 1.0, 0)
self.rotate(-y, math.cos(x), 0, math.sin(x))
def frustum(self, left, right, bottom, top, near, far):
deltax = right - left
deltay = top - bottom
deltaz = far - near
frustum_matrix = copy_matrix(clean_matrix)
frustum_matrix[0][0] = 2 * near / deltax
frustum_matrix[1][1] = 2 * near / deltay
frustum_matrix[2][0] = (right + left) / deltax
frustum_matrix[2][1] = (top + bottom) / deltay
frustum_matrix[2][2] = -(near + far) / deltaz
frustum_matrix[2][3] = -1.0
frustum_matrix[3][2] = -2 * near * far / deltaz
self.data = multiply_matrices(self.data, frustum_matrix)
def perspective(self, fovy, aspect, near, far):
frustum_y = math.tan(math.radians(fovy) / 2)
frustum_x = frustum_y * aspect
self.frustum(-frustum_x * near, frustum_x * near, -frustum_y * near, frustum_y * near, near, far)
def orthographic(self, left, right, bottom, top, near, far):
deltax = right - left
deltay = top - bottom
deltaz = far - near
orthographic_matrix = copy_matrix(identity_matrix)
orthographic_matrix[0][0] = 2.0 / deltax
orthographic_matrix[3][0] = -(right + left) / deltax
orthographic_matrix[1][1] = 2.0 / deltay
orthographic_matrix[3][1] = -(top + bottom) / deltay
orthographic_matrix[2][2] = 2.0 / deltax
orthographic_matrix[3][2] = -(near + far) / deltaz
self.data = multiply_matrices(self.data, orthographic_matrix)