-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.py
More file actions
267 lines (160 loc) · 6.09 KB
/
object.py
File metadata and controls
267 lines (160 loc) · 6.09 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
from graphics import *
import math
import numpy as np
from copy import deepcopy
class Object():
def __init__(self, window, num_vertices = None, vertices = None):
self.window = window
if (num_vertices):
vertices = self.generate_random_object(num_vertices)
elif(vertices == None):
vertices = self.user_create_object_vertices(window)
self.initial_vertices = vertices
self.center_vertices()
self.vertices = deepcopy(vertices)
self.scaled_vertices = None
self.projected_image = None
self.image_lines = None
self.init_diameter()
def update_image(self):
#Update scaled vertices
self.scale_vertices()
#Get orthographic projected of image
self.update_projected_image()
#Update image lines
self.update_image_lines()
return
def update_image_lines(self):
#Get image lines
self.image_lines = []
for i in range(len(self.projected_image)):
for j in range(i + 1, len(self.projected_image)):
p1 = Point(self.projected_image[i][0], self.projected_image[i][1])
p2 = Point(self.projected_image[j][0], self.projected_image[j][1])
self.image_lines.append(Line(p1, p2))
return
def draw(self):
self.update_image()
for line in self.image_lines:
line.draw(self.window)
return
def redraw(self):
#Undraw lines
for line in self.image_lines:
line.undraw()
#Update image
self.update_image()
#Draw lines
for line in self.image_lines:
line.draw(self.window)
def scale_vertices(self):
#If object has no diameter, return
if (len(self.vertices) < 2):
self.scaled_vertices = self.vertices
return
SCALE = 0.7 * (self.window.width / 2) / self.diameter
self.scaled_vertices = deepcopy(self.vertices)
#Scale all dimensions that are not 0
for i in range(len(self.scaled_vertices)):
p = self.scaled_vertices[i]
for d in range(len(p)):
value = p[d]
if (value != 0):
self.scaled_vertices[i][d] = value * SCALE
return
def rotate(self, rotation):
ROTATION_RATE = 10
x_axis_rotation = rotation[1] * ROTATION_RATE
y_axis_rotation = rotation[0] * ROTATION_RATE
if (abs(x_axis_rotation) > abs(y_axis_rotation)):
y_axis_rotation = 0
else:
x_axis_rotation = 0
prev_vertices = deepcopy(self.vertices)
for i, v in enumerate(self.vertices):
prev_v = prev_vertices[i]
#rotate x axis
v[1] = prev_v[1]* math.cos(x_axis_rotation) - prev_v[2] * math.sin(x_axis_rotation)
v[2] = prev_v[1] * math.sin(x_axis_rotation) + prev_v[2] * math.cos(x_axis_rotation)
prev_v = deepcopy(v)
#rotate y axis
v[0] = prev_v[0] * math.cos(y_axis_rotation) + prev_v[2] * math.sin(y_axis_rotation)
v[2] = prev_v[2] * math.cos(y_axis_rotation) - prev_v[0] * math.sin(y_axis_rotation)
return
def translate(self, translation):
for v in self.vertices:
v[0] += translation[0]
v[1] -= translation[1]
return
def update_projected_image(self):
self.projected_image = []
for v in self.scaled_vertices:
self.projected_image.append([v[0], v[1]])
return
def init_diameter(self):
diameter = 0
for i in range(len(self.vertices)):
for j in range(i + 1, len(self.vertices)):
v = self.vertices[i]
w = self.vertices[j]
distance = ((v[0] - w[0])**2 + (v[1] - w[1])**2 + (v[2] - w[2])**2)**0.5
diameter = max([diameter, distance])
self.diameter = diameter
def generate_object(self, n):
vertices = []
count = 0
alpha = 4 * math.pi / n
d = alpha ** 0.5
M_theta = round(math.pi / d)
d_theta = math.pi / M_theta
d_psi = alpha / d_theta
for m in range(M_theta):
theta = math.pi * (m + 0.5) / M_theta
M_psi = round(2 * math.pi * math.sin(theta) / d_psi)
for n in range(M_psi):
psi = 2 * math.pi * n / M_psi
x = math.sin(theta) * math.cos(psi)
y = math.sin(theta) * math.sin(psi)
z = math.cos(theta)
vertices.append([x, y, z])
count += 1
return vertices
def generate_random_object(self, n):
vertices = []
for i in range(n):
theta = np.random.uniform(0, math.pi)
psi = np.random.uniform(0, 2 * math.pi)
x = math.sin(theta) * math.cos(psi)
y = math.sin(theta) * math.sin(psi)
z = math.cos(theta)
vertices.append([x, y, z])
return vertices
def user_create_object_vertices(self, window):
limit = window.height
vertices = []
print('How many vertices would you like your object to have?')
n = int(input())
for i in range(n):
print("Click vertex location for x and y value")
mouse = window.getMouse()
x = mouse.x
y = mouse.y
x = float(x) / limit
y = float(y) / limit
print("Enter a corresponding z value in the range [{}, {}]".format(-limit, limit))
z = int(input()) / limit
vertices.append([x, y, z])
return vertices
def center_vertices(self):
x_avg = 0
y_avg = 0
z_avg = 0
n = len(self.initial_vertices)
for v in self.initial_vertices:
x_avg += v[0] / n
y_avg += v[1] / n
z_avg += v[2] / n
for v in self.initial_vertices:
v[0] -= x_avg
v[1] -= y_avg
v[2] -= z_avg