-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
521 lines (436 loc) · 19.7 KB
/
utils.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import copy
import math
import numpy as np
import pyclipper
import torch
from PIL import Image
import GeomBase
import Polyline
import GeoError
import RNP
import resample
import CSS
from GenPath import GenDpPath
from VtkAdaptor import VtkAdaptor
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
def get_polygon(points):
P = Polyline.Polyline()
for point in points:
if point[0] != 1348083461:
P.points.append(GeomBase.Point3D(point[0], point[1], point[2]))
else:
yield P
P = Polyline.Polyline()
def PolylineToTuple(Poly: Polyline, coefficient):
temp = []
for point in Poly.points:
var_tuple = (round(point.x * coefficient), round(point.y * coefficient), round(point.z * coefficient))
temp.append(var_tuple)
tuple_P = tuple(temp)
return tuple_P
def dic_slice(_dic, start, end):
keys = list(_dic.keys())
_dic_slice = {}
for key in keys[start: end + 1]:
_dic_slice[key] = _dic[key]
return _dic_slice
def load_2d_contours(inner_path, out_path, slicer_start, coefficient, slice_end):
inner_points = np.loadtxt(inner_path)
out_points = np.loadtxt(out_path)
a = get_polygon(out_points)
layers = {}
try:
while True:
out_poly = next(a)
z = out_poly.points[-1].z
if z not in layers.keys():
layers[z] = []
layers[z].append([out_poly])
except StopIteration:
print('StopIteration')
b = get_polygon(inner_points)
try:
while True:
inner_poly = next(b)
z = inner_poly.points[0].z
if z in layers.keys():
for polygons in layers[z]:
tuple_Point = (
round(inner_poly.points[0].x * coefficient), round(inner_poly.points[0].y * coefficient),
round(inner_poly.points[0].z * coefficient))
tuple_Poly = PolylineToTuple(polygons[0], coefficient)
if pyclipper.PointInPolygon(tuple_Point, tuple_Poly) == 1:
polygons.append(inner_poly)
except StopIteration:
print('StopIteration')
layers = dic_slice(layers, slicer_start, slice_end)
return layers
def direnction(contour: Polyline):
points = contour.points
d = 0
for i in range(len(points) - 1):
d += -0.5 * (points[i + 1].y + points[i].y) * (points[i + 1].x - points[i].x)
if d > 0:
return 0
else:
return 1
def resmaple_about_interval(interval, polyline, isOpen):
length = resample.pl_arcLength(polyline.points, isOpen)
if length < interval:
raise GeoError.lengtherror("Too short can not resample")
N = int(length / interval)
pathrp = resample.ResampleCurve(polyline, N)
return pathrp
def Show_loss(x, y):
plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 创建一个带有上下标的标题和标签的折线图
plt.plot(x, y, label=":Loss")
# 设置标题,带有上下标
plt.title(r'迭代次数与Loss值', fontsize=14)
# 设置X轴和Y轴标签,带有上下标
plt.xlabel(r'迭代次数', fontsize=12)
plt.ylabel(r'Loss', fontsize=12)
# 显示图例
plt.legend()
# 显示网格
plt.grid(True)
# 显示图形
plt.show()
def Filter(polygon, interval):
result = []
for poly in polygon:
# 判断当前多边形是否为内轮廓
if direnction(poly) == 1:
try:
poly = resmaple_about_interval(0.4, poly, 1)
except GeoError.lengtherror:
print("Can not resmaple this short inner_contour")
# remove the inner_poly
continue
else:
poly = resmaple_about_interval(0.4, poly, 1)
#if len(poly.points) >= 31:
# c = CSS.polygon_To_Array(poly)
# kappa, smooth = CSS.compute_curve_css(c, 3)
# poly = CSS.Array_To_polygon(smooth, poly.points[0].z)
#else:
# print("Too few points to CSS")
# DNP处理开口轮廓
a = RNP.DNP(poly, 0.4, 5)
temp = a.dnp()
temp.points.append(temp.points[0])
poly = temp
result.append(poly)
# path_show(polygon)
return result
def Adjust(polygon, interval):
result = []
for poly in polygon:
# 判断当前多边形是否为内轮廓
if direnction(poly) == 1:
try:
poly = resmaple_about_interval(0.4, poly, 1)
except GeoError.lengtherror:
print("Can not resmaple this short inner_contour")
# remove the inner_poly
continue
else:
poly = resmaple_about_interval(0.4, poly, 1)
# DNP处理开口轮廓
result.append(poly)
# path_show(polygon)
return result
def Get_path_area(path):
pass
def Get_fill_area(fill_area):
pass
def path_show(paths):
if len(paths) == 0:
return
va = VtkAdaptor()
for i in range(len(paths)):
va.drawPolyline(paths[i]).GetProperty().SetColor(0, 0, 1)
va.display()
def path_conformal_show(paths, polys):
va = VtkAdaptor()
for i in range(len(paths)):
va.drawPolyline(paths[i]).GetProperty().SetColor(0, 0, 1)
for i in range(len(polys)):
va.drawPolyline(polys[i]).GetProperty().SetColor(1, 0, 0)
va.display()
def PolyToTensor(Poly: Polyline.Polyline):
Tensor = torch.zeros(Poly.count(), 3)
for i in range(len(Poly.points)):
Tensor[i][0] = Poly.points[i].x
Tensor[i][1] = Poly.points[i].y
Tensor[i][2] = Poly.points[i].z
return Tensor
def show_path_and_polygon(paths, polygon, write_flag, name):
va = VtkAdaptor()
# for i in range(path.shape[0]):
for p in paths:
c = va.showPolyline(p)
c.GetProperty().SetColor(1, 0, 0)
c.GetProperty().SetLineWidth(2)
for i in range(len(polygon)):
c1 = va.showPolyline(polygon[i])
c1.GetProperty().SetColor(0, 0, 1)
c1.GetProperty().SetLineWidth(2)
if write_flag:
va.write_image(name, va.window)
else:
va.display()
def Calculate_distance(Tensor1, Tensor2):
x = Tensor1.unsqueeze(1).expand(-1, Tensor2.shape[0], -1)
y = Tensor2.unsqueeze(0).expand(Tensor1.shape[0],-1, -1)
distance = torch.norm(x - y, dim=-1)
return distance
def Calculate_SI(a, b, c, d):
" a b"
" c d"
ac = c - a
ab = b - a
ad = d - a
if torch.cross(ac, ad)[-1] * torch.cross(ab, ad)[-1]<0:
return True
else:
return False
def Show_offset_and_org(offset, original):
va = VtkAdaptor()
for polygon in offset:
for i in range(len(polygon)):
c = va.drawPolyline(polygon[i])
c.GetProperty().SetColor(0, 0, 1)
c.GetProperty().SetLineWidth(2)
for polygon in original:
for i in range(len(polygon)):
c = va.drawPolyline(polygon[i])
c.GetProperty().SetColor(1, 0, 0)
c.GetProperty().SetLineWidth(2)
va.display()
def Pre_check(polygon, interval, coefficient):
# remove repeat points
polygon = [reduce_repeated_points(poly) for poly in polygon]
isOpen = True
length = resample.pl_arcLength(polygon[0].points, isOpen)
area = pyclipper.Area(PolylineToTuple(polygon[0], coefficient))
area = area / (coefficient * coefficient)
if area < pow(interval, 2) or length < interval * 2:
raise GeoError.outpoly_too_small("too small out contour")
def reduce_repeated_points(poly):
temp = []
for point in poly.points:
if point not in temp:
temp.append(point)
poly.points = temp
return poly
def contour_offset(polygon: list, interval, coefficient, flag=0):
"offset the polygon, generate filling area O = [o_1, o_2...o_n]"
length_inner = 0
length_out = 0
for poly in polygon:
if direnction(poly) == 1:
length_inner = length_inner + 1
else:
length_out = length_out + 1
if length_out == 0:
length_out = length_inner
length_inner = 0
pco = pyclipper.PyclipperOffset()
for poly in polygon:
contour_tuple = []
for point in poly.points:
var_tuple = (round(point.x * coefficient), round(point.y * coefficient))
contour_tuple.append(var_tuple)
contour_tuple = tuple(contour_tuple)
pco.AddPath(contour_tuple, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
solution = pco.Execute(-interval * coefficient)
poly_out_list = []
poly_inner_list = []
total_polygons = []
for i in range(len(solution)):
solution[i] = np.array(solution[i]) / coefficient
solution[i] = solution[i].tolist()
if len(solution) == 0:
raise GeoError.Small("The R is too small")
if flag == 0:
for i in range(len(solution)):
polys1 = Polyline.Polyline()
for j in range(len(solution[i])):
point = GeomBase.Point3D(solution[i][j][0], solution[i][j][1], polygon[0].points[0].z)
polys1.points.append(point)
polys1.points.append(polys1.points[0])
if direnction(polys1) == 1:
poly_inner_list.append(polys1)
else:
poly_out_list.append(polys1)
if length_inner != len(poly_inner_list) or len(
poly_out_list) != length_out:
for out_poly in poly_out_list:
polygons = [out_poly]
for inner_poly in poly_inner_list:
tuple_Point = (
round(inner_poly.points[0].x * coefficient), round(inner_poly.points[0].y * coefficient),
round(inner_poly.points[0].z * coefficient))
tuple_Poly = PolylineToTuple(out_poly, coefficient)
if pyclipper.PointInPolygon(tuple_Point, tuple_Poly) == 1:
polygons.append(inner_poly)
total_polygons.append(polygons)
raise GeoError.topology(msg="topology will be change after one time offset", polygons=total_polygons,
len_inner=len(poly_inner_list))
else:
poly_out_list.extend(poly_inner_list)
total_polygons.append(poly_out_list)
return total_polygons
else:
return None
def To_Intersect_ys(polygon, interval): # Get the scan_lines of the polygon
ys, yMin, yMax = [], float('inf'), float('-inf')
for poly in polygon:
for pt in poly.points:
yMin, yMax = min(yMin, pt.y), max(yMax, pt.y)
y = yMin + interval
while yMax - y >= 1e-10:
ys.append(y)
y += interval
ys.sort(reverse=True)
return ys
def get_ys(poly, ys): # extract some scan_lines of the poly from total scan_lines
yMin, yMax = float('inf'), float('-inf')
for pt in poly.points:
yMin, yMax = min(yMin, pt.y), max(yMax, pt.y)
front = -1
behind = -1
for i in range(len(ys)):
if yMax - ys[i] >= 1e-10:
front = i
break
if front != -1:
for j in range(len(ys) - 1, front, -1):
if ys[j] - yMin >= 1e-10:
behind = j
break
if behind == -1:
local_ys = ys[front]
return local_ys
else:
local_ys = []
return local_ys
local_ys = ys[front:behind + 1]
return local_ys
def Generate_local_Z(child_regions, interval, ys):
return GenDpPath(child_regions, interval, 0, ys).generate2()
def replaceL(index, A):
return A[index:] + A[:index]
def findindex(point: GeomBase.Point3D, endpath: Polyline):
for i in range(len(endpath.points)):
if abs(endpath.points[i].x - point.x) <= 1e-8 and abs(endpath.points[i].y - point.y) <= 1e-8:
break
return i
def insert(fatherpath, childpath, interval, freroute, creroute):
if ((fatherpath.points[freroute[1]].x <= childpath.points[creroute[1]].x) and (
fatherpath.points[freroute[0]].x <= childpath.points[creroute[0]].x)):
insertpointx = (fatherpath.points[freroute[0]].x + childpath.points[creroute[1]].x) * 0.5
if (
(fatherpath.points[freroute[1]].x >= childpath.points[creroute[1]].x) and (
fatherpath.points[freroute[0]].x >= childpath.points[creroute[0]].x)):
insertpointx = (fatherpath.points[freroute[1]].x + childpath.points[creroute[0]].x) * 0.5
if ((fatherpath.points[freroute[0]].x <= childpath.points[creroute[0]].x) and (
fatherpath.points[freroute[1]].x >= childpath.points[creroute[1]].x)):
insertpointx = (fatherpath.points[freroute[0]].x + fatherpath.points[freroute[1]].x) * 0.5
if ((fatherpath.points[freroute[0]].x >= childpath.points[creroute[0]].x) and (
fatherpath.points[freroute[1]].x <= childpath.points[creroute[1]].x)):
insertpointx = (childpath.points[creroute[0]].x + childpath.points[creroute[1]].x) * 0.5
if (freroute[0] < freroute[1]):
for i in range(freroute[0], freroute[1] + 1):
if (i != len(fatherpath.points) - 1):
if insertpointx < fatherpath.points[i].x and insertpointx > fatherpath.points[i + 1].x:
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
else:
if insertpointx < fatherpath.points[i].x and insertpointx > fatherpath.points[0].x:
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
else:
for i in range(freroute[1], freroute[0] + 1):
if (i != len(fatherpath.points) - 1):
if insertpointx > fatherpath.points[i].x and insertpointx < fatherpath.points[i + 1].x:
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
else:
if insertpointx > fatherpath.points[i].x and insertpointx < fatherpath.points[0].x:
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
fatherpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, fatherpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
if (creroute[0] < creroute[1]):
for i in range(creroute[0], creroute[1] + 1):
if (i != len(childpath.points) - 1):
if insertpointx < childpath.points[i].x and insertpointx > childpath.points[i + 1].x:
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
else:
if insertpointx < childpath.points[i].x and insertpointx > childpath.points[0].x:
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
else:
for i in range(creroute[1], creroute[0] + 1):
if (i != len(childpath.points) - 1):
if insertpointx > childpath.points[i].x and insertpointx < childpath.points[i + 1].x:
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
else:
if insertpointx > childpath.points[i].x and insertpointx < childpath.points[0].x:
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
childpath.points.insert(i + 1,
GeomBase.Point3D(insertpointx - interval, childpath.points[i].y,
fatherpath.points[freroute[0]].z))
break
fatherxL = GeomBase.Point3D(insertpointx - interval, fatherpath.points[freroute[0]].y, fatherpath.points[freroute[0]].z)
fatherxR = GeomBase.Point3D(insertpointx, fatherpath.points[freroute[0]].y, fatherpath.points[freroute[0]].z)
childxL = GeomBase.Point3D(insertpointx - interval, childpath.points[creroute[0]].y, fatherpath.points[freroute[0]].z)
childxR = GeomBase.Point3D(insertpointx, childpath.points[creroute[0]].y, fatherpath.points[freroute[0]].z)
return fatherxL, fatherxR, childxL, childxR
def contourconnect(pathhigher: Polyline, path: Polyline):
pathhighervar= Polyline.Polyline()
pathhighervar.points =copy.deepcopy(pathhigher.points)
pathhighervar.points.extend(path.points)
pathhighervar.points.append(pathhighervar.points[0])
return pathhighervar