-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_pin_rout.py
1853 lines (1600 loc) · 71.9 KB
/
two_pin_rout.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
3D模式布线说明:
1. 起点引脚区域和终点引脚区域不在同一层:
- 使用矩形区域到矩形区域的 3D L 型和 Z 型布线模式。
2. 起点引脚区域和终点引脚区域在同一层:
- 使用矩形区域到矩形区域的 3D L 型和双 L 型布线模式。
"""
import math
import random
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TKagg')
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import plotly.graph_objects as go
import time
import re
from pattern_routing.Judge_the_violation import create_obstacle_hash
from pattern_routing.Optimize_line_segment import remove_overlaps_from_paths
from pattern_routing.Path_check import check_and_return_path_x,check_and_return_path_y, check_same_layer_x,\
check_same_layer_x_via, check_same_layer_y_via, check_path_all, check_path_samey_all, check_path_samex_all,\
check_node_via
# ------------------------- 参数定义 -------------------------
# 定义布线和通孔的各种参数,包括线宽、线距、通孔尺寸等
VIA_LENGTH = 9 # 通孔长度
VIA_WIDTH = 3.8 # 通孔宽度
rect_center = 2.6 # 矩形中心
VIA_LENGTH1 = 7 # 通孔长度
VIA_WIDTH1 = 3 # 通孔宽度
rect_center1 = 2 # 矩形中心
# 起点层线宽与线距
LINE_WIDTH_S = 2
LINE_SPACING_S = 2
# 终点层线宽与线距
LINE_WIDTH_T = 2
LINE_SPACING_T = 2
# 过滤距离计算
FILTER_DIS_S = LINE_WIDTH_S / 2 + LINE_SPACING_S
FILTER_DIS_T = LINE_WIDTH_T / 2 + LINE_SPACING_T
# 版图缩放因子
SCALE_FACTOR = 100
# ------------------------- 障碍物生成函数 -------------------------
def generate_random_rectangles(num_rectangles, min_width, max_width, min_height, max_height, layers, obstacles_to_avoid):
"""
生成指定数量的不重叠矩形,随机分布到指定层,并避免与给定的矩形框重叠。
参数:
- num_rectangles: 需要生成的矩形数量
- min_width: 矩形最小宽度
- max_width: 矩形最大宽度
- min_height: 矩形最小高度
- max_height: 矩形最大高度
- layers: 层列表
- obstacles_to_avoid: 要避免重叠的矩形框列表
返回:
- 字典,键为层名称,值为该层的矩形列表
"""
rectangles = {layer: [] for layer in layers}
attempts = 0
max_attempts = num_rectangles * 100 # 防止无限循环
while sum(len(v) for v in rectangles.values()) < num_rectangles and attempts < max_attempts:
attempts += 1
width = random.randint(min_width, max_width)
height = random.randint(min_height, max_height)
x1 = random.randint(50, 450 - width) # 限制在范围内
y1 = random.randint(50, 450 - height) # 限制在范围内
x2 = x1 + width
y2 = y1 + height
new_rect = ((x1, y1), (x2, y2))
# 检查新矩形是否与要避免的矩形框重叠
overlap = False
for obstacle1 in obstacles_to_avoid:
obstacle = (obstacle1[0],obstacle1[1])
if rectangles_overlap(new_rect, obstacle):
overlap = True
break
if not overlap:
# 随机选择一个层
layer = random.choice(layers)
# 检查新矩形是否与该层中已有的矩形重叠
for rect in rectangles[layer]:
if rectangles_overlap(new_rect, rect):
overlap = True
break
if not overlap:
rectangles[layer].append(new_rect)
if attempts == max_attempts:
print("达到最大尝试次数,可能无法生成所有不重叠的矩形。")
return rectangles
def rectangles_overlap(rect1, rect2):
"""
判断两个矩形是否重叠。
参数:
- rect1: 第一个矩形,格式为 ((x1_min, y1_min), (x1_max, y1_max))
- rect2: 第二个矩形,格式为 ((x2_min, y2_min), (x2_max, y2_max))
返回:
- 重叠返回True,否则False
"""
(x1_min, y1_min), (x1_max, y1_max) = rect1
(x2_min, y2_min), (x2_max, y2_max) = rect2
# 检查是否没有重叠
if x1_max <= x2_min or x1_min >= x2_max or y1_max <= y2_min or y1_min >= y2_max:
return False
return True
# ------------------------- 路径规划函数 -------------------------
def Iteration_step_size(start_rect, end_rect, factor):
"""
确定迭代的步长基于起始和终止矩形的尺寸。
该方法根据起始矩形和终止矩形在x和y方向上的最大尺寸,
计算适合的迭代步长。步长通过找到最大尺寸的位数,然后
设定步长为该位数减二次方的结果(即10^(位数-2))。
这有助于在路径搜索过程中进行适当的采样和搜索步进。
:return: 一个包含起始步长和终止步长的元组 (start_step, end_step)
"""
# 计算起始矩形在x和y方向上的最大尺寸
max_start = max(
abs(start_rect[0][0] - start_rect[1][0]),
abs(start_rect[0][1] - start_rect[1][1])
)
# 计算终止矩形在x和y方向上的最大尺寸
max_end = max(
abs(end_rect[0][0] - end_rect[1][0]),
abs(end_rect[0][1] - end_rect[1][1])
)
# Initialize counters to determine the number of digits
start_step = max(map_number_custom(max_start),math.ceil(LINE_WIDTH_S)) * factor
end_step = max(map_number_custom(max_end),math.ceil(LINE_WIDTH_T)) * factor
return start_step, end_step
def map_number_custom(x):
"""
根据自定义的阈值和输出列表进行映射。
该函数根据输入数值 `x` 的大小,将其映射到一个预先定义的输出值列表 `outputs` 中。
映射的依据是数值范围的阈值列表 `thresholds`,即输入值 `x` 落在哪个阈值范围,
就对应返回该范围的输出值。
参数:
x (float or int): 输入的数值,表示需要映射的值。
thresholds (list): 定义数值范围的阈值列表,用于划分输入值的范围。
在函数中,固定为 [10, 100, 1000, 10000, 100000]。
outputs (list): 对应阈值范围的输出值列表。
在函数中,固定为 [1, number, 10, 100, 1000, 5000]。
返回:
(int): 对应的输出值。如果输入值 `x` 超过所有阈值,则返回 `outputs` 列表中的最后一个值。
"""
# 计算动态值 `number`:
# - 计算 `(LINE_SPACING_T + LINE_WIDTH_T)` 和 `(LINE_SPACING_S + LINE_WIDTH_S)` 的和,
# 然后取两者中的最小值。
# - 使用 `math.ceil` 对最小值向上取整,得到 `number`。
number = math.ceil(min((LINE_SPACING_T + LINE_WIDTH_T), (LINE_SPACING_S + LINE_WIDTH_S)))
# 定义阈值列表,用于划分输入值范围。
thresholds = [10, 100, 1000, 10000, 100000]
# 定义输出值列表,对应每个阈值范围的输出值。
# `number` 是动态计算的值,用于第二个范围(10 <= x < 100)。
outputs = [1, number, number * 2, 100, 1000, 5000]
# 遍历阈值列表,找到输入值 `x` 所属的范围,并返回对应的输出值。
for i, threshold in enumerate(thresholds):
if x <= threshold:
# 如果 x 小于当前阈值,返回对应的输出值。
return outputs[i]
# 如果输入值 `x` 超过所有阈值,返回输出列表中的最后一个值。
return outputs[-1]
def get_rectangle_access_points(rects, step):
"""
从多个矩形中提取接入点,接入点是矩形边界上均匀间隔采样的点。
参数:
rects (list of tuples): 矩形列表,每个矩形格式为 ((x1, y1), (x2, y2), z),
其中 (x1, y1) 和 (x2, y2) 是矩形的两个对角点,
z 是矩形所在的 z 坐标。
step (int): 采样步长,定义在矩形边界上的采样间隔。
返回:
list of tuples: 接入点列表,每个点格式为 (x, y, z),并确保没有重复的点。
"""
points = [] # 存储接入点的列表
for rect in rects:
(x1, y1), (x2, y2), z = rect # 提取矩形的对角点和 z 坐标
# 确保 x1 <= x2 和 y1 <= y2,以便遍历时能正确采样
x_min, x_max = sorted([x1, x2])
y_min, y_max = sorted([y1, y2])
# 采样下边界和上边界的接入点
for x in range(math.ceil(x_min), int(x_max + 1), step):
points.append((x, y_min, z)) # 添加下边界的点
points.append((x, y_max, z)) # 添加上边界的点
# 采样左边界和右边界的接入点
for y in range(math.ceil(y_min), int(y_max + 1), step):
points.append((x_min, y, z)) # 添加左边界的点
points.append((x_max, y, z)) # 添加右边界的点
# 去重并返回接入点列表
return list(set(points)) # 使用 set 去重,确保返回的点都是唯一的
def manhattan_distance_point_to_rect(point, rects):
"""
计算三维点到多个矩形边界的最短曼哈顿距离。
参数:
point (tuple): 三维点的坐标,格式为 (x, y, z)。
rects (list of tuples): 矩形列表,每个矩形由三个元素组成:
((x1, y1), (x2, y2), z_rect)。
- (x1, y1) 和 (x2, y2) 是矩形对角线的两个顶点坐标。
- z_rect 是矩形所在的 z 坐标。
返回:
int or float: 点到所有矩形边界的最短曼哈顿距离。
如果点位于某个矩形的边界上,则距离为 0。
"""
min_total_dis = float('inf') # 初始化最短距离为正无穷大
x, y, z = point # 分别提取点的 x, y, z 坐标
for rect in rects:
(x1, y1), (x2, y2), z_rect = rect # 分别提取矩形的两个顶点坐标和 z 坐标
# 如果点的 z 坐标与矩形的 z 坐标不同,跳过该矩形
# # 因为曼哈顿距离需要在同一平面上计算
# if z != z_rect:
# continue
# 确保 x1 <= x2 和 y1 <= y2
x_min, x_max = sorted([x1, x2])
y_min, y_max = sorted([y1, y2])
# 计算点在 x-y 平面上的曼哈顿距离到矩形边界
if x_min <= x <= x_max and y_min <= y <= y_max:
# 点位于矩形内部,距离为到最近边的距离
distance_2d = min(x - x_min, x_max - x, y - y_min, y_max - y)
elif x_min <= x <= x_max:
# 点在 y 方向外部
if y < y_min:
distance_2d = y_min - y
else: # y > y_max
distance_2d = y - y_max
elif y_min <= y <= y_max:
# 点在 x 方向外部
if x < x_min:
distance_2d = x_min - x
else: # x > x_max
distance_2d = x - x_max
else:
# 点在 x 和 y 两个方向都在外部
if x < x_min and y < y_min:
distance_2d = (x_min - x) + (y_min - y)
elif x < x_min and y > y_max:
distance_2d = (x_min - x) + (y - y_max)
elif x > x_max and y < y_min:
distance_2d = (x - x_max) + (y_min - y)
else: # x > x_max 和 y > y_max
distance_2d = (x - x_max) + (y - y_max)
total_distance = distance_2d # 曼哈顿距离仅考虑 x 和 y 方向
# 更新最短距离和对应的点对
if total_distance < min_total_dis:
min_total_dis = total_distance
return min_total_dis # 返回点到所有矩形边界的最短曼哈顿距离
def compute_manhattan_distance(rect1, rect2):
"""
计算两个矩形边缘之间的最短曼哈顿距离。
参数:
rect1: ((x1, y1), (x2, y2)) - 第一个矩形的两个对角线坐标
rect2: ((x3, y3), (x4, y4)) - 第二个矩形的两个对角线坐标
返回:
最短的曼哈顿距离 (int)
"""
# 提取第一个矩形的坐标
x1_min, y1_min = min(rect1[0][0], rect1[1][0]), min(rect1[0][1], rect1[1][1])
x1_max, y1_max = max(rect1[0][0], rect1[1][0]), max(rect1[0][1], rect1[1][1])
# 提取第二个矩形的坐标
x2_min, y2_min = min(rect2[0][0], rect2[1][0]), min(rect2[0][1], rect2[1][1])
x2_max, y2_max = max(rect2[0][0], rect2[1][0]), max(rect2[0][1], rect2[1][1])
# 计算x轴方向的最短距离
if x1_max < x2_min:
distance_x = x2_min - x1_max
elif x2_max < x1_min:
distance_x = x1_min - x2_max
else:
distance_x = 0 # x轴方向有重叠
# 计算y轴方向的最短距离
if y1_max < y2_min:
distance_y = y2_min - y1_max
elif y2_max < y1_min:
distance_y = y1_min - y2_max
else:
distance_y = 0 # y轴方向有重叠
# 曼哈顿距离是x和y方向距离的最大值
manhattan_distance = max(distance_x , distance_y)
return manhattan_distance
def compute_manhattan_distance_with_all_points(rect1, rect2):
"""
计算两个矩形边缘之间的最短曼哈顿距离,并返回所有对应的点对。
如果只有一对点满足最短曼哈顿距离,则返回这对点。
参数:
rect1: ((x1, y1), (x2, y2)) - 第一个矩形的两个对角线坐标
rect2: ((x3, y3), (x4, y4)) - 第二个矩形的两个对角线坐标
返回:
(最短的曼哈顿距离 (int), 点对列表 (list of tuples))
"""
# 提取第一个矩形的坐标
x1_min, y1_min = math.ceil(min(rect1[0][0], rect1[1][0])), math.ceil(min(rect1[0][1], rect1[1][1]))
x1_max, y1_max = int(max(rect1[0][0], rect1[1][0])), int(max(rect1[0][1], rect1[1][1]))
# 提取第二个矩形的坐标
x2_min, y2_min = math.ceil(min(rect2[0][0], rect2[1][0])), math.ceil(min(rect2[0][1], rect2[1][1]))
x2_max, y2_max = int(max(rect2[0][0], rect2[1][0])), int(max(rect2[0][1], rect2[1][1]))
point_pairs = []
min_distance = float('inf')
# 遍历第一个矩形的所有边缘点
rect1_points = []
for x in range(x1_min, x1_max + 1, math.ceil(LINE_WIDTH_T + LINE_SPACING_T)): # 步长改为 4
rect1_points.append((x, y1_min)) # 下边
rect1_points.append((x, y1_max)) # 上边
for y in range(y1_min + 1, y1_max, math.ceil(LINE_WIDTH_T + LINE_SPACING_T)): # 步长改为 4,避免重复上下边
rect1_points.append((x1_min, y)) # 左边
rect1_points.append((x1_max, y)) # 右边
# 遍历第二个矩形的所有边缘点
rect2_points = []
for x in range(x2_min, x2_max + 1, math.ceil(LINE_WIDTH_T + LINE_SPACING_T)): # 步长改为 4
rect2_points.append((x, y2_min)) # 下边
rect2_points.append((x, y2_max)) # 上边
for y in range(y2_min + 1, y2_max, math.ceil(LINE_WIDTH_T + LINE_SPACING_T)): # 步长改为 4,避免重复上下边
rect2_points.append((x2_min, y)) # 左边
rect2_points.append((x2_max, y)) # 右边
# 计算所有点对的曼哈顿距离
for p1 in rect1_points:
for p2 in rect2_points:
distance = abs(p1[0] - p2[0]) + abs(p1[1] - p2[1]) # 曼哈顿距离公式
if distance < min_distance:
min_distance = distance
point_pairs = [(p1, p2)] # 更新最近点对
elif distance == min_distance:
point_pairs.append((p1, p2)) # 添加到最近点对列表
return min_distance, point_pairs
def all_rects_point_pairs(s, t):
"""
对于两个矩形列表 s 和 t,计算所有矩形对之间的最短曼哈顿距离,
并返回达到该最短距离的所有点对列表。
参数:
s: list of rectangles - 每个矩形由两个对角线坐标表示 ((x1, y1), (x2, y2))
t: list of rectangles - 同上
返回:
min_dis_point_pairs: 达到最短曼哈顿距离的所有点对列表,格式为 [((x1, y1), (x2, y2)), ...]
"""
min_dis = float('inf')
min_dis_point_pairs = [] # 初始化最短距离对应的点对列表为空
for rect in s:
for rect1 in t:
# 计算两个矩形之间的最短曼哈顿距离和对应的点对列表
dis, point_pairs = compute_manhattan_distance_with_all_points(rect, rect1)
if dis < min_dis:
# if len(point_pairs) > 1:
# min_dis = dis
# min_dis_point_pairs = point_pairs + min_dis_point_pairs
# else:
min_dis = dis
# min_dis_point_pairs = point_pairs + min_dis_point_pairs
min_dis_point_pairs = point_pairs
return min_dis_point_pairs # 返回所有达到最短距离的点对列表
def Dynamic_Step_Size(s, t):
"""
动态确定路径规划中的步长。
该函数基于起点和终点之间的最小曼哈顿距离来决定迭代步长,
以优化路径搜索过程的效率和精度。
参数:
- s: 起始矩形,格式为 ((x1, y1), (x2, y2), z)
表示一个三维矩形,其中 (x1, y1) 和 (x2, y2) 是矩形的两个对角点,z 表示高度。
- t: 终止矩形,格式为 ((x1, y1), (x2, y2), z)
格式与参数 s 相同。
返回:
- 一个整数,表示动态确定的步长大小。
较大的步长适合远距离搜索(提高效率),较小的步长适合短距离搜索(提高精度)。
"""
factor_1000 = 1
# 计算起点和终点之间的最小曼哈顿距离:
min_manhattan = compute_manhattan_distance(s, t)
# 如果最小曼哈顿距离大于 1000,则认为点之间距离较远,可以使用较大的步长。
if min_manhattan > 1000:
return int(VIA_LENGTH + min(LINE_WIDTH_S, LINE_WIDTH_T)) * factor_1000
# 取两者中的较大值,并向上取整作为步长。
return int(LINE_WIDTH_S + LINE_SPACING_S)
def same_layer_pattern_routing(s, t, rects):
"""
在同一层上进行模式布线,尝试3D L型和Z型模式路径连接。
参数:
s (tuple): 起始矩形,格式为 ((x1, y1), (x2, y2), z)
t (tuple): 终止矩形,格式为 ((x1, y1), (x2, y2), z)
rects (dict): 障碍物字典,格式为 {"layer_key": [((a, b), (c, d)), ...]}
返回:
tuple: 起点路径、终点路径、通孔列表
"""
# 根据起点和终点确定迭代步长
step = Dynamic_Step_Size(s[0], t[0])
# 分类障碍物,仅考虑起点层的障碍物
rect = [] # 存储起点层和终点层的障碍物
rect1 = [] # 存储上一层的障碍物
for key, value in rects.items():
layer_num = int(re.search(r'\d+', key).group()) # 从层名称中提取层号
if layer_num == 0:
rect.extend(value) # 将属于起点层的障碍物添加到 rect
elif layer_num == 1 and key in rects: # 检查键是否存在
rect1.extend(value) # 将起点层的上一层的障碍物添加到 rect1
# 将分类后的障碍物列表转换为哈希表,以便快速查找和重叠检测
rect = create_obstacle_hash(rect) # 创建起点层障碍物的哈希表
rect1 = create_obstacle_hash(rect1) # 创建起点层障碍物的哈希表
list_node = all_rects_point_pairs(s, t)
# 确定模式布线步长
s_step, t_step = Iteration_step_size(s[0], t[0], factor=2)
# 获取起点和终点的接入点列表
list_point_start = get_rectangle_access_points(s, s_step)
list_point_end = get_rectangle_access_points(t, t_step)
# 根据曼哈顿距离对起点和终点的接入点进行排序,以优化路径搜索顺序
list_point_start_sorted = sorted(
list_point_start,
key=lambda point: manhattan_distance_point_to_rect(point, t)
)
list_point_end_sorted = sorted(
list_point_end,
key=lambda point: manhattan_distance_point_to_rect(point, s)
)
# 记录开始时间
start_time = time.time()
TIME_LIMIT = 5 # 时间限制(秒)
# ------------------------- 多源多汇模式布线 ------------------------
# 阶段1:尝试所有3D L型模式路径连接
for (start_x, start_y, z_start) in list_point_start_sorted:
for (end_x, end_y, z_end) in list_point_end_sorted:
# 尝试沿X方向优先的L型路径布线
s_path, t_path, vias = check_same_layer_x(start_x, start_y, start_x, end_x, end_y, rect)
if s_path is not None:
return s_path, [], []
# 尝试另一种X方向优先的L型路径布线
s_path, t_path, vias = check_same_layer_x(
start_x, start_y, end_x, end_x, end_y, rect)
if s_path is not None:
return s_path, [], []
# 阶段2:根据匹配点得到双孔的模式布线
dict_node={} #避免重复搜索
for i in range(0, len(list_node), math.ceil(LINE_WIDTH_S + LINE_SPACING_S)):
(nodes, nodet) = list_node[i]
start_x, start_y = nodes
end_x, end_y = nodet
if start_y == end_y:
for x in range(math.ceil(min(start_x , end_x)) , max(start_x,end_x), step):
for x1 in range(math.ceil(x + math.ceil(VIA_LENGTH + LINE_SPACING_S)), int(max(start_x,end_x) + step), step):
s_path, t_path, vias = check_path_samey_all(start_x, start_y, x, x1, end_x, end_y, rect, rect1)
if s_path is not None:
return s_path, t_path, vias
elif start_x == end_x:
for y in range(math.ceil(min(start_y, end_y)) , max(start_y, end_y), step):
for y1 in range(math.ceil(y + math.ceil(VIA_WIDTH + LINE_SPACING_S)) , int(max(start_y, end_y) + step), step):
s_path, t_path, vias = check_path_samex_all(start_x, start_y, y, y1, end_x, end_y, rect, rect1)
if s_path is not None:
return s_path, t_path, vias
else:
# 阶段2:如果阶段1未找到路径,进行基于迭代步长的3D 双L模式路径搜索
# 迭代步长搜索路径
y_range = range(math.ceil(start_y) , int(end_y) + step, step) if math.ceil(start_y) + step + 1 < end_y else range(math.ceil(end_y) ,
int(start_y) + step, step)
x_range = range(math.ceil(start_x) , int(end_x) + step, step) if math.ceil(start_x) + step + 1 < end_x else range(math.ceil(end_x) ,
int(start_x) + step, step)
for x in x_range:
for y in y_range:
s_path, t_path, vias, dict_node = check_path_all(start_x, start_y, x, y, end_x, end_y, rect, rect1, dict_node)
if s_path is not None:
return s_path, t_path, vias
# 阶段3:如果阶段2未找到路径,进行基于迭代步长的3D Z型模式路径搜索
for (start_x, start_y, z_start) in list_point_start_sorted:
for (end_x, end_y, z_end) in list_point_end_sorted:
if ((start_x, start_y),(end_x, end_y)) in list_node:
continue
current_time = time.time()
if current_time - start_time > TIME_LIMIT:
# print("遍历时间超过十秒,返回空结果。")
return [], [], []
x_range = range(math.ceil(start_x) , int(end_x) + step, step) if math.ceil(start_x) + step + 1 < end_x else range(math.ceil(end_x) ,
int(start_x) + step, step)
# 尝试沿X轴迭代步长搜索路径
for x in x_range:
s_path, t_path, vias = check_same_layer_x_via(
start_x, start_y, x, end_x, end_y, rect, rect1
)
if s_path is not None:
return s_path, t_path, vias
# 尝试沿Y轴迭代步长搜索路径
y_range = range(math.ceil(start_y) , int(end_y) + step, int(step)) if math.ceil(start_y + step) + 1 < end_y else range(math.ceil(end_y ) , int(start_y) + step, int(step))
for y in y_range:
s_path, t_path, vias = check_same_layer_y_via(
start_x, start_y, y, end_x, end_y, rect, rect1
)
if s_path is not None:
return s_path, t_path, vias
# 若未找到可行路径,返回空结果
return [], [], []
def manhattan_distance(point1, point2):
"""
计算两个点之间的曼哈顿距离。
参数:
point1 (tuple): 第一个点,格式为 (x1, y1) 或 (x1, y1, z1) 等。
point2 (tuple): 第二个点,格式为 (x2, y2) 或 (x2, y2, z2) 等。
返回:
float: 两个点之间的曼哈顿距离。
"""
# 确保两个点的维度相同
if len(point1) != len(point2):
raise ValueError("两个点的维度必须相同")
# 计算曼哈顿距离
distance = sum(abs(a - b) for a, b in zip(point1, point2))
return distance
def has_valid_distance(vias, vias1):
"""
检查 vias 和 vias1 中的点之间是否存在符合条件的曼哈顿距离。
参数:
vias (list): 第一个点的列表,格式为 [((x1, y1),), ((x2, y2),), ...]
vias1 (list): 第二个点的列表,格式为 [((x1, y1),), ((x2, y2),), ...]
via_length (float): 定义的 VIA_LENGTH
line_spacing_s (float): 定义的 LINE_SPACING_S
返回:
bool: 如果存在符合条件的距离,则返回 True,否则返回 False。
"""
for via in vias:
for via1 in vias1:
if 0 < manhattan_distance(via1[0], via[0]) < VIA_LENGTH + LINE_SPACING_S:
return True # 找到符合条件的距离
return False # 没有找到符合条件的距离
def same_layer_pattern_routing_three_layers(s, t, rects):
"""
在同一层上进行模式布线,尝试3D L型和Z型模式路径连接。
参数:
s (tuple): 起始矩形,格式为 ((x1, y1), (x2, y2), z)
t (tuple): 终止矩形,格式为 ((x1, y1), (x2, y2), z)
rects (dict): 障碍物字典,格式为 {"layer_key": [((a, b), (c, d)), ...]}
返回:
tuple: 起点路径、终点路径、通孔列表
"""
# 根据起点和终点确定迭代步长
step = Dynamic_Step_Size(s[0], t[0])
# 分类障碍物,仅考虑起点层的障碍物
rect = [] # 存储起点层和终点层的障碍物
rect1 = [] # 存储上一层的障碍物
rect2 = [] # 存储上上一层的障碍物
for key, value in rects.items():
layer_num = int(re.search(r'\d+', key).group()) # 从层名称中提取层号
if layer_num == 0:
rect.extend(value) # 将属于起点层的障碍物添加到 rect
elif layer_num == 1 and key in rects: # 检查键是否存在
rect1.extend(value) # 将起点层的上一层的障碍物添加到 rect1
elif layer_num == 2 and key in rects: # 检查键是否存在
rect2.extend(value) # 将起点层的上一层的障碍物添加到 rect1
# 将分类后的障碍物列表转换为哈希表,以便快速查找和重叠检测
rect = create_obstacle_hash(rect) # 创建起点层障碍物的哈希表
rect1 = create_obstacle_hash(rect1) # 创建起点层障碍物的哈希表
rect2 = create_obstacle_hash(rect2) # 创建起点层障碍物的哈希表
list_node = all_rects_point_pairs(s, t)
# 确定模式布线步长
s_step, t_step = Iteration_step_size(s[0], t[0], factor=1)
# 获取起点和终点的接入点列表
list_point_start = get_rectangle_access_points(s, s_step)
list_point_end = get_rectangle_access_points(t, t_step)
# 根据曼哈顿距离对起点和终点的接入点进行排序,以优化路径搜索顺序
list_point_start_sorted = sorted(
list_point_start,
key=lambda point: manhattan_distance_point_to_rect(point, t)
)
list_point_end_sorted = sorted(
list_point_end,
key=lambda point: manhattan_distance_point_to_rect(point, s)
)
# 记录开始时间
start_time = time.time()
TIME_LIMIT = 5 # 时间限制(秒)
# ------------------------- 多源多汇模式布线 ------------------------
# 阶段1:尝试所有3D L型模式路径连接
for (start_x, start_y, z_start) in list_point_start_sorted:
for (end_x, end_y, z_end) in list_point_end_sorted:
vias1 = check_node_via((start_x, start_y), (end_x, end_y), rect,rect1)
if vias1 is not None:
# 尝试沿X方向优先的L型路径布线
s_path, t_path, vias = check_same_layer_x(start_x, start_y, start_x, end_x, end_y, rect1)
if s_path is not None:
return s_path, [], [] , vias1
# 尝试另一种X方向优先的L型路径布线
s_path, t_path, vias = check_same_layer_x(
start_x, start_y, end_x, end_x, end_y, rect1)
if s_path is not None:
return s_path, [], [] ,vias1
# 阶段2:根据匹配点得到双孔的模式布线
dict_node={} #避免重复搜索
for i in range(0, len(list_node), math.ceil(LINE_WIDTH_S + LINE_SPACING_S)):
(nodes, nodet) = list_node[i]
start_x, start_y = nodes
end_x, end_y = nodet
vias1 = check_node_via((start_x, start_y), (end_x, end_y), rect,rect1)
if vias1 is not None:
if start_y == end_y:
for x in range(math.ceil(min(start_x , end_x)) , max(start_x,end_x), step):
if 0 < abs(x - start_x) < VIA_LENGTH + LINE_SPACING_S or 0 < abs(x - end_x) < VIA_LENGTH + LINE_SPACING_S:
continue
for x1 in range(math.ceil(x + math.ceil(VIA_LENGTH + LINE_SPACING_S)) , int(max(start_x,end_x) + step), step):
if 0< abs(x1 - start_x) < VIA_LENGTH + LINE_SPACING_S or \
0 < abs(x1 - end_x) < VIA_LENGTH + LINE_SPACING_S:
continue
s_path, t_path, vias = check_path_samey_all(start_x, start_y, x, x1, end_x, end_y, rect1, rect2)
if s_path is not None:
return s_path, t_path, vias, vias1
elif start_x == end_x:
for y in range(math.ceil(min(start_y, end_y)) , max(start_y, end_y), step):
for y1 in range(math.ceil(y + math.ceil(VIA_WIDTH + LINE_SPACING_S)) , int(max(start_y, end_y) + step), step):
if 0 < abs(y - start_y) < VIA_LENGTH + LINE_SPACING_S or 0 < abs(
y1 - start_y) < VIA_LENGTH + LINE_SPACING_S or \
0 < abs(y - end_y) < VIA_LENGTH + LINE_SPACING_S or 0 < abs(
y1 - end_y) < VIA_LENGTH + LINE_SPACING_S:
continue
s_path, t_path, vias = check_path_samex_all(start_x, start_y, y, y1, end_x, end_y, rect1, rect2)
if s_path is not None:
return s_path, t_path, vias, vias1
else:
# 阶段2:如果阶段1未找到路径,进行基于迭代步长的3D 双L模式路径搜索
# 迭代步长搜索路径
y_range = range(math.ceil(start_y) , int(end_y) + step, step) if math.ceil(start_y) + step + 1 < end_y else range(math.ceil(end_y),
int(start_y) + step, step)
x_range = range(math.ceil(start_x) , int(end_x) + step, step) if math.ceil(start_x) + step + 1 < end_x else range(math.ceil(end_x),
int(start_x) + step, step)
for x in x_range:
for y in y_range:
s_path, t_path, vias, dict_node = check_path_all(start_x, start_y, x, y, end_x, end_y, rect1, rect2, dict_node)
if s_path is not None:
if not has_valid_distance(vias, vias1):
return s_path, t_path, vias, vias1
# 阶段3:如果阶段2未找到路径,进行基于迭代步长的3D Z型模式路径搜索
for (start_x, start_y, z_start) in list_point_start_sorted:
for (end_x, end_y, z_end) in list_point_end_sorted:
if ((start_x, start_y),(end_x, end_y)) in list_node:
continue
vias1 = check_node_via((start_x, start_y), (end_x, end_y), rect,rect1)
if vias1 is not None:
current_time = time.time()
if current_time - start_time > TIME_LIMIT:
# print("遍历时间超过十秒,返回空结果。")
return [], [], [], []
x_range = range(math.ceil(start_x) , int(end_x) + step, step) if math.ceil(start_x) + step + 1 < end_x else range(math.ceil(end_x),
int(start_x) + step, step)
# 尝试沿X轴迭代步长搜索路径
for x in x_range:
s_path, t_path, vias = check_same_layer_x_via(
start_x, start_y, x, end_x, end_y, rect1, rect2
)
if s_path is not None:
if not has_valid_distance(vias, vias1):
return s_path, t_path, vias, vias1
# 尝试沿Y轴迭代步长搜索路径
y_range = range(math.ceil(start_y), int(end_y) + step, int(step)) if math.ceil(start_y + step) + 1 < end_y else range(math.ceil(end_y ), int(start_y) + step, int(step))
for y in y_range:
s_path, t_path, vias = check_same_layer_y_via(
start_x, start_y, y, end_x, end_y, rect1, rect2
)
if s_path is not None:
if not has_valid_distance(vias, vias1):
return s_path, t_path, vias, vias1
# 若未找到可行路径,返回空结果
return [], [], [], []
# ------------------------- 不同层路径规划函数 -------------------------
def different_layers_pattern_routing_(s, t, rects):
"""
利用3D Z型模式进行布线。
参数:
- s: 起始矩形,格式为 ((x1, y1), (x2, y2), z)
- t: 目标矩形,格式为 ((x1, y1), (x2, y2), z)
- rects: 障碍物字典,格式为 {"layer_key": [((a, b), (c, d)), ...]}
返回:
- 起点路径, 终点路径, 通孔列表
"""
# 迭代步长由距离确定
step = Dynamic_Step_Size(s[0], t[0])
# # 计算起始和目标矩形的中心点
# s_center = ((s[0][0] + s[1][0]) // 2, (s[0][1] + s[1][1]) // 2)
# t_center = ((t[0][0] + t[1][0]) // 2, (t[0][1] + t[1][1]) // 2)
#
# # 确保起点在左侧
# if s_center[0] > t_center[0]:
# s, t = t, s
# 分类障碍物
rect_s = [] # 起点层障碍物
rect_t = [] # 终点层障碍物
rect_via = [] # 所有层通孔障碍物
for key, value in rects.items():
layer_num = int(re.search(r'\d+', key).group()) # 从层名称中提取层号
if layer_num == s[0][2]:
rect_s.extend(value) # 将属于起点层的障碍物添加到 rect_s
rect_via.extend(value) # 通孔障碍物包含起点层的障碍物
elif layer_num == t[0][2]:
rect_t.extend(value) # 将属于终点层的障碍物添加到 rect_t
rect_via.extend(value) # 通孔障碍物包含终点层的障碍物
elif min(s[0][2],t[0][2]) < layer_num and layer_num < max(s[0][2],t[0][2]):
rect_via.extend(value) # 将其他层的障碍物添加到 rect_via
# 将分类后的障碍物列表转换为哈希表(字典),以便快速查找和重叠检测
rect_s = create_obstacle_hash(rect_s) # 创建起点层障碍物的哈希表
rect_t = create_obstacle_hash(rect_t) # 创建终点层障碍物的哈希表
rect_via = create_obstacle_hash(rect_via) # 创建所有层通孔障碍物的哈希表
# 确定模式布线步长
s_step, t_step = Iteration_step_size(s[0], t[0], factor=2)
# 获取起点和终点的接入点列表
list_point_start = get_rectangle_access_points(s, s_step)
list_point_end = get_rectangle_access_points(t, t_step)
# 根据曼哈顿距离对起点和终点的接入点进行排序,以优化路径搜索顺序
list_point_start_sorted = sorted(
list_point_start,
key=lambda point: manhattan_distance_point_to_rect(point, t)
)
list_point_end_sorted = sorted(
list_point_end,
key=lambda point: manhattan_distance_point_to_rect(point, s)
)
# 记录开始时间
start_time = time.time()
TIME_LIMIT = 5 # 时间限制(秒)
# 阶段1:尝试所有 3D L型模式路径连接
for (start_x, start_y, z_start) in list_point_start_sorted:
for (end_x, end_y, z_end) in list_point_end_sorted:
# 尝试x方向L型模式布线优先路径
s_path, t_path, vias = check_and_return_path_x(
start_x, start_y, start_x, end_x, end_y, rect_s, rect_t, rect_via
)
if s_path is not None:
return s_path, t_path, vias
s_path, t_path, vias = check_and_return_path_x(
start_x, start_y, end_x, end_x, end_y, rect_s, rect_t, rect_via
)
if s_path is not None:
return s_path, t_path, vias
# 尝试y方向L型模式布线优先路径
s_path, t_path, vias = check_and_return_path_y(
start_x, start_y, start_y, end_x, end_y, rect_s, rect_t, rect_via
)
if s_path is not None:
return s_path, t_path, vias
s_path, t_path, vias = check_and_return_path_y(
start_x, start_y, end_y, end_x, end_y, rect_s, rect_t, rect_via
)
if s_path is not None:
return s_path, t_path, vias
# 阶段2:如果阶段1未找到路径,进行基于迭代步长的 3D Z型模式路径搜索
for (start_x, start_y, z_start) in list_point_start_sorted:
for (end_x, end_y, z_end) in list_point_end_sorted:
current_time = time.time()
if current_time - start_time > TIME_LIMIT:
# print("遍历时间超过十秒,返回空结果。")
return [], [], []
# 尝试沿 x 轴迭代步长搜索路径
for x in range(math.ceil(start_x + step), int(end_x), step):
current_time = time.time()
if current_time - start_time > TIME_LIMIT:
# print("遍历时间超过十秒,返回空结果。")
return [], [], []
s_path, t_path, vias = check_and_return_path_x(
start_x, start_y, x, end_x, end_y, rect_s, rect_t, rect_via
)
if s_path is not None:
return s_path, t_path, vias
# 尝试沿 y 轴迭代步长搜索路径
y_range = range(math.ceil(start_y + step), int(end_y), step) if start_y + step + 1 < end_y else range(math.ceil(end_y + step), int(start_y), step)
for y in y_range:
current_time = time.time()
if current_time - start_time > TIME_LIMIT:
# print("遍历时间超过十秒,返回空结果。")
return [], [], []
s_path, t_path, vias = check_and_return_path_y(
start_x, start_y, y, end_x, end_y, rect_s, rect_t, rect_via
)
if s_path is not None:
return s_path, t_path, vias
# 若未找到可行路径,返回空
return [], [], []
# ------------------------- 路径规划核心函数 -------------------------
def find_path(s, t, rects):
"""
根据起点和终点是否在同一层,调用相应的布线函数进行路径规划。
"""
if s[0][2] == t[0][2]:
# 如果起点和终点在同一层,调用同层布线函数
s_path, t_path, vias = same_layer_pattern_routing(s, t, rects)
else:
# 如果起点和终点不在同一层,调用不同层布线函数
s_path, t_path, vias = different_layers_pattern_routing_(s, t, rects)
for s1 in s:
for t1 in t:
# 移除路径中与起点和终点重叠的部分
s_path, t_path = remove_overlaps_from_paths(s_path, t_path, s1, t1)
return s_path, t_path, vias
def Pin_grouping(s, t):
"""
将输入的两个列表 s 和 t 中的元素进行分组,并移除具有特定标记的元素。
参数:
s (list): 包含多个元素的列表,每个元素是一个元组或列表,要求其包含至少 3 个元素。
t (list): 另一个包含多个元素的列表,每个元素也是一个元组或列表,要求其包含至少 3 个元素。
返回:
tuple: 返回两个列表,分别是 s 和 t 中所有第三个值为 1 的元素 s_1 和 t_1。
"""
s_1 = [] # 初始化列表,保存从 s 中筛选出来的元素
t_1 = [] # 初始化列表,保存从 t 中筛选出来的元素
# 遍历列表 s,筛选出第三个元素值为 1 的元素并添加到 s_1 列表
for s1 in s:
if s1[2] == 1: # 检查元素的第三个值
s_1.append(s1) # 将符合条件的元素添加到 s_1 列表
# 遍历列表 t,筛选出第三个元素值为 1 的元素并添加到 t_1 列表
for t1 in t:
if t1[2] == 1: # 检查元素的第三个值
t_1.append(t1) # 将符合条件的元素添加到 t_1 列表
# 从原列表中移除已经添加到 s_1 的元素
remove_elements_in_place(s, s_1) # 在列表 s 中移除所有出现在 s_1 中的元素
# 从原列表中移除已经添加到 t_1 的元素
remove_elements_in_place(t, t_1) # 在列表 t 中移除所有出现在 t_1 中的元素
return s_1, t_1 # 返回筛选出的元素列表
def remove_elements_in_place(list_a, list_b):
"""
从 list_a 中移除 list_b 中的所有元素。
参数:
list_a (list): 要修改的列表
list_b (list): 包含要移除元素的列表