-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.py
152 lines (126 loc) · 4.41 KB
/
classes.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
import os
import platform
import numpy as np
import math
class Object3d:
def __init__(self, model, position, rotation):
self.position = np.array(position, dtype=float)
self.rotation = np.array(rotation, dtype=float)
self.model, self.edges = self.obj_to_model(model)
def obj_to_model(self, file):
f = open(file, "r")
model = []
edges = []
for line in f:
if line.startswith("v "):
line = line.replace("v ", "").strip().split(" ")
model.append(np.array([float(line[0]), float(line[1]), float(line[2].strip())]))
elif line.startswith("f "):
line = line.replace("f ", "").strip().split(" ")
edge = [int(x.split("/")[0]) - 1 for x in line]
edges.append(edge)
return model, edges
def points(self):
x_axis = [1, 0, 0]
mod = []
for point in self.model:
p = self.rotation_matrix(x_axis, self.rotation[0]).dot(point)
mod.append(p)
return mod + self.position
def rotation_matrix(self, axis, theta):
axis = np.asarray(axis)
axis = axis / math.sqrt(np.dot(axis, axis))
a = math.cos(theta / 2.0)
b, c, d = -axis * math.sin(theta / 2.0)
aa, bb, cc, dd = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
return np.array(
[
[aa + bb - cc - dd, 2 * (bc + ad), 2 * (bd - ac)],
[2 * (bc - ad), aa + cc - bb - dd, 2 * (cd + ab)],
[2 * (bd + ac), 2 * (cd - ab), aa + dd - bb - cc],
]
)
class Camera3d:
def __init__(self, alpha, beta, near, far):
self.alpha = alpha
self.beta = beta
self.near = near
self.far = far
def calculate_transform_matrix(self):
return np.array(
[
[math.tan(self.alpha), 0, 0, 0],
[0, math.tan(self.beta), 0, 0],
[
0,
0,
(self.far + self.near) / (self.far - self.near),
(2 * self.near * self.far) / (self.far - self.near),
],
[0, 0, -1, 0],
]
)
def calculate_x_y(self, position):
position = np.append(position, position[-1])
transformed_position = position.dot(self.calculate_transform_matrix())
transformed_position /= transformed_position[-1]
return transformed_position[0], transformed_position[1]
class RenderEngine:
def __init__(self, room_height, room_width):
self.room_height = room_height
self.room_width = room_width
self.room = []
self.fill_room()
def clear(self):
self.fill_room()
if platform.system() == "Windows":
os.system("cls")
elif platform.system() == "Linux":
os.system("clear")
def fill_room(self):
self.room.clear()
for _ in range(0, self.room_height):
r = []
for _ in range(0, self.room_width):
r.append(" ")
self.room.append(r)
def print_room(self):
for i in self.room:
toPrint = ""
for j in i:
toPrint += j
print(toPrint)
def normalize(self, i, j):
if i < -1 or j < -1:
return -1, -1
if i > 1 or j > 1:
return -1, -1
i = int(((i + 1) / 2) * self.room_height)
j = int(((j + 1) / 2) * self.room_width)
return i, j
def draw_point(self, i, j):
if i < 0:
return
if j < 0:
return
if i >= self.room_height:
return
if j >= self.room_width:
return
self.room[i][j] = "X"
def draw_line(self, i, j, x, y):
if i == -1 or j == -1 or x == -1 or y == -1:
return
lerp = lambda start, end, t: start + t * (end - start)
distance = lambda x, y: math.sqrt(x ** 2 + y ** 2)
iterations = int(distance(i - x, j - y))
for k in range(iterations):
t = k / iterations
ax = int(lerp(i, x, t))
ay = int(lerp(j, y, t))
self.draw_point(ax, ay)
def draw_rectangle(self, x, y, width, height):
for j in range(x, x + width):
for i in range(y, y + height):
self.draw_point(i, j)