-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenginemaths.py
More file actions
291 lines (241 loc) · 10.2 KB
/
enginemaths.py
File metadata and controls
291 lines (241 loc) · 10.2 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
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
import math
import numpy as np
class Vector4f:
def __init__(self, x, y, z, w=1.0):
self.x = float(x)
self.y = float(y)
self.z = float(z)
self.w = float(w)
def __str__(self):
return "Vector4f ({0}, {1}, {2}, {3})".format(self.x, self.y,
self.z, self.w)
def __repr__(self):
return "<Vector4f: x:{0}, y:{1}, z:{2}, w:{3}>".format(self.x, self.y,
self.z, self.w)
def length(self):
return math.sqrt(self.x*self.x + self.y*self.y + self.z*self.z +
self.w*self.w)
def normalized(self):
length = self.length()
return Vector4f(self.x/length, self.y/length,
self.z/length, self.w/length)
def dot_product(self, vec):
if isinstance(vec, Vector4f):
return self.x*vec.x + self.y*vec.y + self.z*vec.z + self.w*vec.w
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__,
type(vec)))
def cross_product(self, vec):
if isinstance(vec, Vector4f):
_x = self.y*vec.z - self.z*vec.y
_y = self.z*vec.x - self.x*vec.z
_z = self.x*vec.y - self.y*vec.x
return Vector4f(_x,_y,_z,0)
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__,
type(vec)))
def rotate(self, angle):
sinAngle = math.sin(-angle)
cosAngle = math.cos(-angle)
# return self.cross()
def equals(self, vec):
return self.x == vec.x and self.y == vec.y and \
self.z == vec.z and self.w == vec.w
def __add__(self, other):
if isinstance(other, (int, float)):
return Vector4f(self.x + other, self.y + other,
self.z + other, self.w + other)
elif isinstance(other, Vector4f):
return Vector4f(self.x + other.x, self.y + other.y,
self.z + other.z, self.w + other.w)
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__,
type(other)))
def __sub__(self, other):
if isinstance(other, (int, float)):
return Vector4f(self.x - other, self.y - other,
self.z - other, self.w - other)
elif isinstance(other, Vector4f):
return Vector4f(self.x - other.x, self.y - other.y,
self.z - other.z,self.w - other.w)
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__,
type(other)))
def __mul__(self, other):
if isinstance(other, (int, float)):
return Vector4f(self.x * other, self.y * other,
self.z * other, self.w * other)
elif isinstance(other, Vector4f):
return Vector4f(self.x * other.x, self.y * other.y,
self.z * other.z, self.w * other.w)
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__,
type(other)))
def __truediv__(self, other):
if isinstance(other, (int, float)):
return Vector4f(self.x / other, self.y / other,
self.z / other, self.w / other)
elif isinstance(other, Vector4f):
return Vector4f(self.x / other.x, self.y / other.y,
self.z / other.z, self.w / other.w)
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__,
type(other)))
def abso(self):
return Vector4f(math.fabs(self.x), math.fabs(self.y),
math.fabs(self.z), math.fabs(self.w))
class Matrix4f:
def __init__(self):
self.m = np.zeros((4,4))
def __str__(self):
return "Matrix4f\n {0}".format(self.m)
def __repr__(self):
return "<Matrix4f\n {0}>".format(self.m)
def get_matrix(self):
return self.m
def set_matrix(self, m):
self.m = m
def get_value_at(self, i, j):
return self.m[i][j]
def set_value_at(self, i, j, val):
self.m[i][j] = val
def init_screenspace_transform(self, halfWidth, halfheight):
self.m[0][0] = halfWidth
self.m[0][3] = halfWidth
self.m[1][1] = -halfheight
self.m[1][3] = halfheight
self.m[2][2] = 1
self.m[3][3] = 1
return self
def init_identity(self):
self.m[0][0] = 1
self.m[1][1] = 1
self.m[2][2] = 1
self.m[3][3] = 1
return self
def init_translation(self, x, y, z):
self.m[0][0] = 1
self.m[0][3] = x
self.m[1][1] = 1
self.m[1][3] = y
self.m[2][2] = 1
self.m[2][3] = z
self.m[3][3] = 1
return self
def init_rotation(self, x, y, z):
rx = Matrix4f()
ry = Matrix4f()
rz = Matrix4f()
rz.m[0][0] = math.cos(z)
rz.m[0][1] = -math.sin(z)
rz.m[1][0] = math.sin(z)
rz.m[1][1] = math.cos(z)
rz.m[2][2] = 1
rz.m[3][3] = 1
rx.m[0][0] = 1
rx.m[1][1] = math.cos(x)
rx.m[1][2] = -math.sin(x)
rx.m[2][1] = math.sin(x)
rx.m[2][2] = math.cos(x)
rx.m[3][3] = 1
ry.m[0][0] = math.cos(y)
ry.m[0][2] = -math.sin(z)
ry.m[1][1] = 1
ry.m[2][0] = math.sin(z)
ry.m[2][2] = math.cos(x)
ry.m[3][3] = 1
self.m = (rz * (ry * rx)).get_matrix()
return self
def init_perspective(self, fov, aspect_ratio, zNear, zFar):
tanHalfFOV = math.tan(fov / 2)
zRange = zNear - zFar
self.m[0][0] = 1.0 / (tanHalfFOV * aspect_ratio)
self.m[1][1] = 1.0 / tanHalfFOV
self.m[2][2] = (-zNear - zFar) / zRange
self.m[2][3] = 2 * zFar * zNear / zRange
self.m[3][2] = 1
return self
def __mul__(self, mat):
if isinstance(mat, Matrix4f):
res = Matrix4f()
for i in range(0,4):
for j in range(0,4):
val = (self.m[i][0] * mat.get_value_at(0, j))
val += (self.m[i][1] * mat.get_value_at(1, j))
val += (self.m[i][2] * mat.get_value_at(2, j))
val += (self.m[i][3] * mat.get_value_at(3, j))
res.set_value_at(i, j, val)
return res
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Matrix4f.__name__, type(mat)))
def transform(self, vec):
if isinstance(vec, Vector4f):
return Vector4f(self.m[0][0] * vec.x + self.m[0][1] * vec.y +
self.m[0][2] * vec.z + self.m[0][3] * vec.w,
self.m[1][0] * vec.x + self.m[1][1] * vec.y +
self.m[1][2] * vec.z + self.m[1][3] * vec.w,
self.m[2][0] * vec.x + self.m[2][1] * vec.y +
self.m[2][2] * vec.z + self.m[2][3] * vec.w,
self.m[3][0] * vec.x + self.m[3][1] * vec.y +
self.m[3][2] * vec.z + self.m[3][3] * vec.w)
else:
raise TypeError("The argument must be {0} type, found it to be "
"of type {1}".format(Vector4f.__name__, type(vec)))
class Quaternion4f:
def __init__(self, x, y, z, w):
self.x = float(x)
self.y = float(y)
self.z = float(z)
self.w = float(w)
def __str__(self):
return "Quaternion4f ({0}, {1}, {2}, {3})".format(self.x, self.y,
self.z, self.w)
def __repr__(self):
return "<Quaternion4f: " \
"x:{0}, y:{1}, z:{2}, w:{3}>".format(self.x, self.y,
self.z, self.w)
def length(self):
return math.sqrt(self.x * self.x + self.y * self.y +
self.z * self.z + self.w * self.w)
def normalized(self):
length = self.length()
return Quaternion4f(self.x/length, self.y/length,
self.z/length, self.w/length)
def normalize_this(self):
length = self.length()
self.x /= length
self.y /= length
self.z /= length
self.w /= length
return self
def conjugate(self):
return Quaternion4f(-self.x, -self.y, -self.z, -self.w)
def __mul__(self, other):
if other is Quaternion4f:
_w = self.w*other.w - self.x*other.x - \
self.y*other.y - self.z*other.z
_x = self.x*other.w + self.w*other.x + \
self.y*other.z - self.z*other.y
_y = self.y*other.w + self.w*other.y + \
self.z*other.x - self.x*other.z
_z = self.z*other.w + self.w*other.z + \
self.x*other.y - self.y*other.x
return Quaternion4f(_x, _y, _z, _w)
if other is Vector4f:
_w = -self.x * other.x - self.y * other.y - self.z * other.z
_x = self.w * other.x + self.y * other.z - self.z * other.y
_y = self.w * other.y + self.z * other.x - self.x * other.z
_z = self.w * other.z + self.x * other.y - self.y * other.x
return Quaternion4f(_x, _y, _z, _w)
else:
raise TypeError("The argument must be {0} or {1} type, found it "
"to be of type {2}".format(Quaternion4f.__name__,
Vector4f.__name__,
type(other)))