-
Notifications
You must be signed in to change notification settings - Fork 0
/
cylinder.py
398 lines (297 loc) · 17.4 KB
/
cylinder.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from scipy.spatial import distance
from util.util import straight_line_dialogue
import numpy as np
from scipy.spatial.transform import Rotation as R
import copy
class Cylinder_Tool(QObject):
def __init__(self, ctrl_wdg):
super().__init__(ctrl_wdg)
self.ctrl_wdg = ctrl_wdg
self.dist_thresh_select = 10.0
self.selected_cylinder_idx = -1
self.group_num = 0
self.order = []
self.data_val = []
self.vertices_cylinder = []
self.top_vertices = []
self.centers = []
self.top_centers = []
self.base_circles = []
self.sectorCount = 32
self.cylinder_count = []
self.scaling_factor = 1.3
self.colors = [(0,0,0)]
self.b_vecs = []
self.t_vecs = []
self.Ns = []
self.radii = []
self.heights = []
self.bool_cylinder_type = []
def reset(self, ctrl_wdg):
self.__init__(ctrl_wdg)
def select_feature(self, x, y):
v = self.ctrl_wdg.mv_panel.movie_caps[self.ctrl_wdg.mv_panel.selected_movie_idx]
t = self.ctrl_wdg.selected_thumbnail_index
feature_selected = False
if (len(v.features_regular) > 0 or len(v.features_network) > 0) and len(self.ctrl_wdg.gl_viewer.obj.all_ply_pts) > 0:
data = self.ctrl_wdg.gl_viewer.obj.all_ply_pts[-1] # 3D data from bundle adjustment
cnt = 0
if self.ctrl_wdg.kf_method == "Regular":
for i, fc in enumerate(v.features_regular[t]):
if not v.hide_regular[t][i]:
d = distance.euclidean((fc.x_loc, fc.y_loc), (x, y))
if d < self.dist_thresh_select:
if len(v.mapping_2d_3d_regular[t]) > cnt:
self.order.append(i)
self.data_val.append(data[v.mapping_2d_3d_regular[t][cnt],:])
v.cylinder_groups_regular[t][i] = self.group_num
feature_selected = True
if len(self.data_val) == 4:
if self.ctrl_wdg.ui.bnCylinder:
bases, tops, center, top_c, height, radius, b_vec, t_vec, N = self.make_new_cylinder(self.data_val[0], self.data_val[1], self.data_val[2], self.data_val[3])
if len(bases) > 0:
# self.ctrl_wdg.main_file.logfile.info("Make a BASE CYLINDER primitive ....")
self.bool_cylinder_type.append(False)
self.refresh_cylinder_data(bases, tops, center, top_c, height, radius, b_vec, t_vec, N)
else:
straight_line_dialogue()
del self.data_val[-1]
else:
# self.ctrl_wdg.main_file.logfile.info("Make a CENTER CYLINDER primitive ....")
bases, tops, center, top_c, height, radius, b_vec, t_vec, N = self.make_cylinder(self.data_val[0], self.data_val[1], self.data_val[2], self.data_val[3])
self.bool_cylinder_type.append(True)
self.refresh_cylinder_data(bases, tops, center, top_c, height, radius, b_vec, t_vec, N)
cnt += 1
elif self.ctrl_wdg.kf_method == "Network":
for i, fc in enumerate(v.features_network[t]):
if not v.hide_network[t][i]:
d = distance.euclidean((fc.x_loc, fc.y_loc), (x, y))
if d < self.dist_thresh_select:
if len(v.mapping_2d_3d_network[t]) > cnt:
self.data_val.append(data[v.mapping_2d_3d_network[t][cnt],:])
v.cylinder_groups_network[t][i] = self.group_num
self.order.append(i)
feature_selected = True
# self.ctrl_wdg.main_file.logfile.info("Clicked on a feature for CYLINDER primitive ....")
if len(self.data_val) == 4:
if self.ctrl_wdg.ui.bnCylinder:
bases, tops, center, top_c, height, radius, b_vec, t_vec, N = self.make_new_cylinder(self.data_val[0], self.data_val[1], self.data_val[2], self.data_val[3])
if len(bases) > 0:
# self.ctrl_wdg.main_file.logfile.info("Make a BASE CYLINDER primitive ....")
self.bool_cylinder_type.append(False)
self.refresh_cylinder_data(bases, tops, center, top_c, height, radius, b_vec, t_vec, N)
else:
straight_line_dialogue()
del self.data_val[-1]
else:
# self.ctrl_wdg.main_file.logfile.info("Make a CENTER CYLINDER primitive ....")
bases, tops, center, top_c, height, radius, b_vec, t_vec, N = self.make_cylinder(self.data_val[0], self.data_val[1], self.data_val[2], self.data_val[3])
self.bool_cylinder_type.append(True)
self.refresh_cylinder_data(bases, tops, center, top_c, height, radius, b_vec, t_vec, N)
cnt += 1
return feature_selected
def refresh_cylinder_data(self, bases, tops, center, top_c, height, radius, b_vec, t_vec, N):
v = self.ctrl_wdg.mv_panel.movie_caps[self.ctrl_wdg.mv_panel.selected_movie_idx]
t = self.ctrl_wdg.selected_thumbnail_index
if self.ctrl_wdg.kf_method == "Regular":
for order in self.order:
v.cylinder_groups_regular[t][order] = -1
elif self.ctrl_wdg.kf_method == "Network":
for order in self.order:
v.cylinder_groups_network[t][order] = -1
self.heights.append(height)
self.radii.append(radius)
self.b_vecs.append(b_vec)
self.t_vecs.append(t_vec)
self.Ns.append(N)
# print("Primitive count : "+str(self.ctrl_wdg.rect_obj.primitive_count))
self.cylinder_count.append(self.ctrl_wdg.rect_obj.primitive_count)
c = self.ctrl_wdg.rect_obj.getRGBfromI(self.ctrl_wdg.rect_obj.primitive_count)
self.colors.append(c)
self.centers.append(center)
self.top_centers.append(top_c)
self.vertices_cylinder.append(bases)
self.top_vertices.append(tops)
self.data_val = []
self.order = []
self.group_num += 1
self.ctrl_wdg.rect_obj.primitive_count += 1
# self.ctrl_wdg.main_file.logfile.info("A Cylinder number "+str(len(self.cylinder_count))+" has been made while total primitives are "+str(self.ctrl_wdg.rect_obj.primitive_count)+" ....")
def make_circle(self, center, p1, p2):
t_vec = p1 - center
b_vec_temp = p2 - center
radius = np.linalg.norm(b_vec_temp)
t_vec = t_vec/max(0.00005, np.linalg.norm(t_vec))
b_vec_temp = b_vec_temp/max(0.00005, radius)
N = np.cross(b_vec_temp, t_vec)
N = N/max(0.00005, np.linalg.norm(N))
b_vec = np.cross(t_vec, N) # t_vec, b_vec and N form our x,y,z coordinate system
sectorStep = 2*np.pi/self.sectorCount
base_points = []
for i in range(self.sectorCount+1):
sectorAngle = i * sectorStep # theta
base_points.append(center + radius*np.cos(sectorAngle)*t_vec + radius*np.sin(sectorAngle)*b_vec)
return base_points, center
def make_cylinder(self, center, p1, p2, p3): # p1 is anchor and p2 is used for radius
# print("Center : "+str(center))
t_vec = p1 - center
b_vec_temp = p2 - center
H_vec = p3 - center
radius = np.linalg.norm(b_vec_temp)
t_vec = t_vec/max(0.00005, np.linalg.norm(t_vec))
b_vec_temp = b_vec_temp/max(0.00005, radius)
N = np.cross(b_vec_temp, t_vec)
N = N/max(0.00005, np.linalg.norm(N))
height = np.dot(H_vec, N)
# if height < 0:
# # print("Center : "+str(center))
# return self.make_cylinder(center, p2, p1, p3)
b_vec = np.cross(t_vec, N) # t_vec, b_vec and N form our x,y,z coordinate system
sectorStep = 2*np.pi/self.sectorCount
base_points = []
top_points = []
for i in range(self.sectorCount+1):
sectorAngle = i * sectorStep # theta
base_points.append(center + radius*np.cos(sectorAngle)*t_vec + radius*np.sin(sectorAngle)*b_vec)
top_points.append(center + radius*np.cos(sectorAngle)*t_vec + radius*np.sin(sectorAngle)*b_vec + height*N)
return base_points, top_points, center, center + height*N, height, radius, b_vec, t_vec, N
def delete_cylinder(self, idx):
if idx != -1:
# self.ctrl_wdg.main_file.logfile.info("Delete the cylinder number "+str(idx)+" ....")
self.centers[idx] = np.array([-1, -1, -1])
self.selected_cylinder_idx = -1
def make_new_circle(self, p1, p2, p3):
t_vec = p2 - p1
b_vec_temp = p3 - p1
t_vec = t_vec/max(0.000005, np.linalg.norm(t_vec))
b_vec_temp = b_vec_temp/max(0.000005, np.linalg.norm(b_vec_temp))
N = np.cross(t_vec, b_vec_temp)
N = N/max(0.000005, np.linalg.norm(N))
b_vec = np.cross(t_vec, N) # t_vec, b_vec and N form our x,y,z coordinate system
new_P1 = (0, 0)
new_P2 = (np.dot(b_vec, p2-p1), np.dot(t_vec, p2-p1))
new_P3 = (np.dot(b_vec, p3-p1), np.dot(t_vec, p3-p1))
center_2d, radius = self.define_circle(new_P1, new_P2, new_P3)
if center_2d is None:
return [], 0
center = p1 + center_2d[0]*b_vec + center_2d[1]*t_vec
sectorStep = 2*np.pi/self.sectorCount
base_points = []
for i in range(self.sectorCount+1):
sectorAngle = i * sectorStep # theta
base_points.append(center + radius*np.cos(sectorAngle)*b_vec + radius*np.sin(sectorAngle)*t_vec)
return base_points, center
def make_new_cylinder(self, p1, p2, p3, p4):
t_vec = p2 - p1
b_vec_temp = p3 - p1
H_vec = p4 - p1
t_vec = t_vec/max(0.000005, np.linalg.norm(t_vec))
b_vec_temp = b_vec_temp/max(0.000005, np.linalg.norm(b_vec_temp))
N = np.cross(t_vec, b_vec_temp)
N = N/max(0.000005, np.linalg.norm(N))
height = np.dot(H_vec, N)
# print(height)
# if height > 0:
# return self.make_new_cylinder(p3, p2, p1, p4)
b_vec = np.cross(t_vec, N) # t_vec, b_vec and N form our x,y,z coordinate system
new_P1 = (0, 0)
new_P2 = (np.dot(b_vec, p2-p1), np.dot(t_vec, p2-p1))
new_P3 = (np.dot(b_vec, p3-p1), np.dot(t_vec, p3-p1))
center_2d, radius = self.define_circle(new_P1, new_P2, new_P3)
if center_2d is None:
return [], [], 0, 0, 0, 0, [], [], []
center = p1 + center_2d[0]*b_vec + center_2d[1]*t_vec
sectorStep = 2*np.pi/self.sectorCount
base_points = []
top_points = []
for i in range(self.sectorCount+1):
sectorAngle = i * sectorStep # theta
base_points.append(center + radius*np.cos(sectorAngle)*b_vec + radius*np.sin(sectorAngle)*t_vec)
top_points.append(center + radius*np.cos(sectorAngle)*b_vec + radius*np.sin(sectorAngle)*t_vec + height*N)
return base_points, top_points, center, center + height*N , height, radius, b_vec, t_vec, N
def define_circle(self, p1, p2, p3):
"""
Returns the center and radius of the circle passing the given 3 points.
In case the 3 points form a line, returns (None, infinity).
"""
temp = p2[0] * p2[0] + p2[1] * p2[1]
bc = (p1[0] * p1[0] + p1[1] * p1[1] - temp) / 2
cd = (temp - p3[0] * p3[0] - p3[1] * p3[1]) / 2
det = (p1[0] - p2[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p2[1])
if abs(det) < 1.0e-6:
return (None, np.inf)
# Center of circle
cx = (bc*(p2[1] - p3[1]) - cd*(p1[1] - p2[1])) / det
cy = ((p1[0] - p2[0]) * cd - (p2[0] - p3[0]) * bc) / det
radius = np.sqrt((cx - p1[0])**2 + (cy - p1[1])**2)
return (cx, cy), radius
def rotate(self, angle_degrees, rotation_axis):
if self.selected_cylinder_idx != -1:
angle_radians = np.radians(angle_degrees)
rotation_vector = angle_radians * rotation_axis
rotation = R.from_rotvec(rotation_vector)
base_center = self.centers[self.selected_cylinder_idx]
if -1 not in base_center:
self.t_vecs[self.selected_cylinder_idx] = rotation.apply(self.t_vecs[self.selected_cylinder_idx])
self.b_vecs[self.selected_cylinder_idx] = rotation.apply(self.b_vecs[self.selected_cylinder_idx])
self.Ns[self.selected_cylinder_idx] = rotation.apply(self.Ns[self.selected_cylinder_idx])
base_vertices = self.vertices_cylinder[self.selected_cylinder_idx]
top_center = self.top_centers[self.selected_cylinder_idx]
top_vertices = self.top_vertices[self.selected_cylinder_idx]
cyl_center = 0.5*(base_center + top_center)
self.centers[self.selected_cylinder_idx] = rotation.apply(base_center - cyl_center) + cyl_center
self.top_centers[self.selected_cylinder_idx] = rotation.apply(top_center - cyl_center) + cyl_center
for i, pt in enumerate(base_vertices):
self.vertices_cylinder[self.selected_cylinder_idx][i] = rotation.apply(pt - cyl_center) + cyl_center
for i, pt in enumerate(top_vertices):
self.top_vertices[self.selected_cylinder_idx][i] = rotation.apply(pt - cyl_center) + cyl_center
def translate(self, axis, idx):
if idx != -1:
base_center = self.centers[idx]
if -1 not in base_center:
base_vertices = self.vertices_cylinder[idx]
top_center = self.top_centers[idx]
top_vertices = self.top_vertices[idx]
self.centers[idx] = base_center + axis
self.top_centers[idx] = top_center + axis
for i, pt in enumerate(base_vertices):
self.vertices_cylinder[idx][i] = pt + axis
for i, pt in enumerate(top_vertices):
self.top_vertices[idx][i] = pt + axis
def scale(self, scale):
i = self.selected_cylinder_idx
# print("Scale : "+str(scale))
if i != -1:
center = self.centers[i]
vertices = self.vertices_cylinder[i]
top = self.top_centers[i]
top_vertices = self.top_vertices[i]
middle = 0.5*(center + top)
radius = self.radii[i] * scale
self.radii[i] = radius
if scale < 1:
axis1 = (1 - scale)*(middle - center)
axis2 = (1 - scale)*(middle - top)
else:
axis1 = (scale-1)*(center - middle)
axis2 = (scale-1)*(top - middle)
self.centers[i] = center + axis1
self.top_centers[i] = top + axis2
height = np.linalg.norm(self.top_centers[i] - self.centers[i])
# print("Height : "+str(height))
sectorStep = 2 * np.pi / self.sectorCount
t_vec, b_vec, N = self.t_vecs[i], self.b_vecs[i], self.Ns[i]
if self.bool_cylinder_type[i]:
for j in range(self.sectorCount + 1):
sectorAngle = j * sectorStep # theta
self.vertices_cylinder[i][j] = self.centers[i] + radius * np.cos(sectorAngle) * t_vec + radius * np.sin(sectorAngle) * b_vec
self.top_vertices[i][j] = self.centers[i] + radius*np.cos(sectorAngle)*t_vec + radius*np.sin(sectorAngle)*b_vec + height*N
else:
# print("else")
for j in range(self.sectorCount + 1):
sectorAngle = j * sectorStep # theta
self.vertices_cylinder[i][j] = self.centers[i] + radius * np.cos(sectorAngle) * b_vec + radius * np.sin(sectorAngle) * t_vec
self.top_vertices[i][j] = self.centers[i] + radius*np.cos(sectorAngle)*b_vec + radius*np.sin(sectorAngle)*t_vec - height*N