liemermaid 0.3.1

Mermaid diagram parser + renderer (SVG / vello_cpu PNG), unified through lievisual Scene IR.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
//! `EdgeRouter`:边路由 + 节点回避。
//!
//! 输入 [`crate::builder::ir::geograph::Geograph`],原地更新每条边的 `route`。
//! 路由策略按边类型分派:
//!
//! 1. **自环**(source == target):节点右侧外凸环(单段贝塞尔)。
//! 2. **双向对**(u↔v 同时存在):小偏移 + 轻微外弓的平行曲线(官方风格)。
//! 3. **回边**(source 在主轴下游):中间无阻挡 → 侧向 U 形贝塞尔;
//!    有阻挡 → 侧边通道绕行,避免长回边斜穿中间层节点。
//! 4. **普通边**:Spline → 单段贝塞尔,穿过节点时退化为「stub + 侧边通道 +
//!    圆角折线」绕行;Orthogonal → 曼哈顿折线,主干步进搜索无冲突位置。
//!
//! 端口分散:同一节点同一端口的多条边按对端投影排序后沿节点边错开;
//! 端口点对 Diamond / Circle 形状做**边界投影**(端口永远落在形状边界上)。
//!
//! **容器避障**:`route_edges` 额外接收容器框(`GGContainer.bounds`)作为障碍,
//! 边只绕开「两端均非其成员」的容器;容器内边 / 跨容器边(至少一端为成员)
//! 允许正常穿过容器边界。

use std::collections::{HashMap, HashSet};

use lievisual::geometry::{Point, Rect};

use crate::builder::ir::common::RoutingHint;
use crate::builder::ir::geograph::{GGContainer, GGNode, Geograph, RoutePath, RouteSegment};
use crate::builder::ir::shape::ShapeKind;

/// 出线 stub 长度(端口法向延伸距离)。
const STUB: f64 = 18.0;
/// 端口槽位:该边在节点对应端口边(Top/Bottom/Left/Right)上的序号与总数。
#[derive(Clone, Copy)]
pub(crate) struct Slot {
    pub(crate) idx: usize,
    pub(crate) total: usize,
}
/// 样条控制点沿端口法向延伸的上下限。
const MIN_ARC: f64 = 28.0;
const MAX_ARC: f64 = 140.0;
/// 端口分散:相邻槽位的最小间距(节点很窄 / 边很多时的下限)。
const PORT_MIN_SPACING: f64 = 14.0;
/// 端口分散:相邻槽位间距上限(相对该条边的长度)。
const PORT_MAX_SPACING_RATIO: f64 = 0.4;
/// 端口分散:端口点距节点角的最小留白。
const PORT_CORNER_MARGIN: f64 = 8.0;
/// 节点回避偏移步进。
const OFFSET_STEP: f64 = 12.0;
/// 回避最大尝试次数。
const MAX_OFFSET_TRIES: usize = 8;
/// 通道与节点包围盒的间距。
const CHANNEL_MARGIN: f64 = 16.0;
/// 通道堆叠间距:多条边的通道重叠时,沿侧向逐格错开。
const CH_STACK: f64 = 28.0;
/// 回边阻挡检测带的外扩(沿同层轴)。
const BAND_PAD: f64 = 30.0;
/// 折线圆角半径(通道绕行路径的平滑度)。
const CORNER_RADIUS: f64 = 14.0;
/// 容器框避让 padding:容器包围盒外扩,防止边贴边框。
const CONTAINER_PAD: f64 = 6.0;

/// 每条边的路由计划(分类结果)。
#[derive(Clone, Copy)]
enum Plan {
    /// 自环(source == target)。
    SelfLoop,
    /// 双向对(u↔v):`side` = +1 右侧 / -1 左侧。
    Mutual { side: f64 },
    /// 回边(无阻挡):侧向 U 形贝塞尔。
    BackBow { side: f64 },
    /// 回边(有阻挡):沿 `ch`(主轴法向坐标,垂直空间)的通道绕行。
    BackChannel { ch: f64, side: f64 },
    /// 普通边。
    Normal,
}

/// 均匀网格空间索引:把避让矩形按格分桶,使「某区域有哪些矩形」的查询
/// 从 O(V) 降到 O(命中数)。
///
/// 查询**不去重**(同一矩形跨多格会重复出现);本模块的调用方全部是
/// 布尔 / min / max / 「某段是否存在冲突」语义,重复不影响结果,因此省掉
/// 去重所需的额外分配与 seen 数组。
struct RectGrid {
    cell: f64,
    min_x: f64,
    min_y: f64,
    cols: usize,
    rows: usize,
    buckets: Vec<Vec<usize>>,
}

impl RectGrid {
    const EMPTY: Self = RectGrid {
        cell: 1.0,
        min_x: 0.0,
        min_y: 0.0,
        cols: 0,
        rows: 0,
        buckets: Vec::new(),
    };

    /// 每个矩形约占一格(cell ≈ √(面积/数量)),并限制总格数上限。
    fn new(rects: &[Rect]) -> Self {
        if rects.is_empty() {
            return Self::EMPTY;
        }
        let mut min_x = f64::INFINITY;
        let mut min_y = f64::INFINITY;
        let mut max_x = f64::NEG_INFINITY;
        let mut max_y = f64::NEG_INFINITY;
        for r in rects {
            min_x = min_x.min(r.min_x());
            min_y = min_y.min(r.min_y());
            max_x = max_x.max(r.max_x());
            max_y = max_y.max(r.max_y());
        }
        if !min_x.is_finite() || !min_y.is_finite() {
            return Self::EMPTY;
        }
        let w = (max_x - min_x).max(1e-6);
        let h = (max_y - min_y).max(1e-6);
        let n = rects.len() as f64;
        let mut cell = ((w * h) / n).sqrt().clamp(8.0, 512.0);
        loop {
            let cols = (w / cell).ceil() as usize + 1;
            let rows = (h / cell).ceil() as usize + 1;
            if cols.saturating_mul(rows) <= MAX_GRID_CELLS {
                let mut buckets = vec![Vec::new(); cols * rows];
                for (i, r) in rects.iter().enumerate() {
                    let c0 = col_of(r.min_x(), min_x, cell, cols);
                    let c1 = col_of(r.max_x(), min_x, cell, cols);
                    let r0 = row_of(r.min_y(), min_y, cell, rows);
                    let r1 = row_of(r.max_y(), min_y, cell, rows);
                    for row in r0..=r1 {
                        for col in c0..=c1 {
                            buckets[row * cols + col].push(i);
                        }
                    }
                }
                return RectGrid {
                    cell,
                    min_x,
                    min_y,
                    cols,
                    rows,
                    buckets,
                };
            }
            cell *= 2.0;
        }
    }

    fn col_of(&self, x: f64) -> usize {
        clamp_cell(((x - self.min_x) / self.cell).floor() as isize, self.cols)
    }

    fn row_of(&self, y: f64) -> usize {
        clamp_cell(((y - self.min_y) / self.cell).floor() as isize, self.rows)
    }

    /// 遍历包围盒 `area` 覆盖的所有候选矩形下标(可能重复,见结构体注释)。
    fn for_each(&self, area: Rect, mut f: impl FnMut(usize)) {
        if self.buckets.is_empty() {
            return;
        }
        let (c0, c1) = (self.col_of(area.min_x()), self.col_of(area.max_x()));
        let (r0, r1) = (self.row_of(area.min_y()), self.row_of(area.max_y()));
        for row in r0..=r1 {
            for col in c0..=c1 {
                for &i in &self.buckets[row * self.cols + col] {
                    f(i);
                }
            }
        }
    }
}

/// 网格总格数上限(超出则放大 cell)。
const MAX_GRID_CELLS: usize = 1 << 20;

fn clamp_cell(v: isize, n: usize) -> usize {
    if v < 0 {
        0
    } else if v as usize >= n {
        n - 1
    } else {
        v as usize
    }
}

fn col_of(x: f64, min_x: f64, cell: f64, cols: usize) -> usize {
    clamp_cell(((x - min_x) / cell).floor() as isize, cols)
}

fn row_of(y: f64, min_y: f64, cell: f64, rows: usize) -> usize {
    clamp_cell(((y - min_y) / cell).floor() as isize, rows)
}

/// 避让矩形集合:节点 + 容器。
///
/// 容器避让规则:边只绕开「两端均非其成员」的容器;
/// 若边至少一端是某容器成员,则该边允许穿过该容器(容器内边 / 跨容器边)。
///
/// `grid` 作用于原始坐标,`grid_t` 作用于转置(垂直)空间——水平主轴的
/// 路由统一在垂直空间规划,两套索引只建一次,避免每边克隆矩形列表。
struct Obstacles {
    /// 全部避让矩形(owner 为节点 id 或容器 id),已含 padding。
    rects: Vec<(usize, Rect, String)>,
    /// 转置(垂直)空间的同一批矩形(下标 / owner 与 `rects` 一致)。
    rects_t: Vec<(usize, Rect, String)>,
    grid: RectGrid,
    grid_t: RectGrid,
    /// 全部矩形的 x 范围(原始 / 转置空间各一份),供兜底通道使用。
    all_min_x: f64,
    all_max_x: f64,
    all_min_x_t: f64,
    all_max_x_t: f64,
    /// 容器 id → 成员节点 id 集合(用于跳过「本容器内部 / 跨本容器的边」)。
    members: HashMap<String, HashSet<String>>,
}

/// 为一条边构造「是否跳过某避让矩形」的判定:
/// - 端点节点自身不避让;
/// - 边至少一端是某容器成员时,该容器不避让(容器内边 / 跨容器边)。
fn edge_skip<'a>(
    s_id: &'a str,
    t_id: &'a str,
    members: &'a HashMap<String, HashSet<String>>,
) -> impl Fn(&str) -> bool + 'a {
    move |owner: &str| -> bool {
        if owner == s_id || owner == t_id {
            return true;
        }
        members
            .get(owner)
            .is_some_and(|m| m.contains(s_id) || m.contains(t_id))
    }
}

/// 为 GG 中所有边生成路由。
///
/// `containers`:子图容器框。边只绕开「两端均非其成员」的容器,
/// 容器内边 / 跨容器边(至少一端为成员)允许穿过容器边界。
pub fn route_edges(
    gg: &mut Geograph,
    direction: crate::ast::Direction,
    containers: &[GGContainer],
) {
    let node_map: HashMap<&String, &GGNode> = gg.nodes.iter().map(|n| (&n.id, n)).collect();

    // 节点包围盒(含少量 padding)。
    let node_rects: Vec<(usize, Rect, String)> = gg
        .nodes
        .iter()
        .enumerate()
        .map(|(i, n)| (i, node_rect(n), n.id.clone()))
        .collect();

    // 容器包围盒(外扩 CONTAINER_PAD)与成员映射:参与边避让。
    let container_rects: Vec<(usize, Rect, String)> = containers
        .iter()
        .enumerate()
        .map(|(i, c)| {
            let b = c.bounds;
            (
                i,
                Rect::new(
                    b.min_x() - CONTAINER_PAD,
                    b.min_y() - CONTAINER_PAD,
                    b.max_x() + CONTAINER_PAD,
                    b.max_y() + CONTAINER_PAD,
                ),
                c.id.clone(),
            )
        })
        .collect();
    let mut members: HashMap<String, HashSet<String>> = HashMap::new();
    for c in containers {
        members.insert(c.id.clone(), c.member_ids.iter().cloned().collect());
    }
    let mut all_rects = node_rects.clone();
    all_rects.extend(container_rects);
    // 转置空间(水平主轴时统一在垂直空间规划)+ 两套空间索引,全部只建一次。
    let rects_t: Vec<(usize, Rect, String)> = all_rects
        .iter()
        .map(|(i, r, o)| (*i, tr_rect(*r), o.clone()))
        .collect();
    let grid = RectGrid::new(&all_rects.iter().map(|(_, r, _)| *r).collect::<Vec<_>>());
    let grid_t = RectGrid::new(&rects_t.iter().map(|(_, r, _)| *r).collect::<Vec<_>>());
    let (all_min_x, all_max_x) = x_range(&all_rects);
    let (all_min_x_t, all_max_x_t) = x_range(&rects_t);
    let obstacles = Obstacles {
        rects: all_rects,
        rects_t,
        grid,
        grid_t,
        all_min_x,
        all_max_x,
        all_min_x_t,
        all_max_x_t,
        members,
    };

    // mutual 对(u↔v)的绕行侧分配记录:key = (source, target),value = ±1。
    let mut mutual_side: HashMap<(String, String), f64> = HashMap::new();

    // 已占用的绕行通道(垂直空间):(ch, y0, y1)。边间避让:后规划的边
    // 不得与已占用通道同坐标且跨度重叠,否则两条边画在同一条线上。
    let mut used_channels: Vec<(f64, f64, f64)> = Vec::new();
    // 已占用的正交主干(trunk 坐标, 跨度起, 跨度止):同层间的并行正交边
    // 主干互斥,避免多条边画在同一条主干线上(阶段 0.6)。
    let mut used_trunks: Vec<(f64, f64, f64)> = Vec::new();
    let edge_pairs: Vec<(String, String)> = gg
        .edges
        .iter()
        .map(|e| (e.source.clone(), e.target.clone()))
        .collect();
    // 节点中心索引:回边交叉者计数需要 O(1) 取节点中心(旧实现线性扫 node_rects)。
    let centers: HashMap<&str, Point> =
        gg.nodes.iter().map(|n| (n.id.as_str(), n.center)).collect();

    // 预计算 mutual 对集合(u↔v 与 v↔u 同时存在)。
    // 用有向边集合做 O(1) 反向查询,替代旧的双重循环 O(E²)。
    let directed: std::collections::HashSet<(&str, &str)> = gg
        .edges
        .iter()
        .map(|e| (e.source.as_str(), e.target.as_str()))
        .collect();
    let mut mutual_pairs: std::collections::HashSet<(String, String)> =
        std::collections::HashSet::new();
    for e in &gg.edges {
        if directed.contains(&(e.target.as_str(), e.source.as_str())) {
            mutual_pairs.insert((e.source.clone(), e.target.clone()));
        }
    }

    // —— 分类:为每条边生成路由计划 ——
    let mut plans: Vec<Option<Plan>> = Vec::with_capacity(gg.edges.len());
    for e in &gg.edges {
        match (node_map.get(&e.source), node_map.get(&e.target)) {
            (Some(s), Some(t)) => {
                let plan = if s.id == t.id {
                    Plan::SelfLoop
                } else if mutual_pairs.contains(&(e.source.clone(), e.target.clone())) {
                    Plan::Mutual {
                        side: mutual_side_for(&e.source, &e.target, &mut mutual_side),
                    }
                } else if is_back_edge(s, t, direction) {
                    let side = mutual_side_for(&e.source, &e.target, &mut mutual_side);
                    // 所有回边(含 Orthogonal)都先尝试侧通道绕行:有阻挡 → 通道
                    // (绕开中间节点),无阻挡 → U 形贝塞尔(本来就安全)。
                    // 旧实现仅对 Spline 尝试通道,Orthogonal 回边直接 BackBow,
                    // 长回边会斜穿中间层节点。
                    match plan_back_channel(
                        s,
                        t,
                        direction,
                        &obstacles,
                        &edge_pairs,
                        &centers,
                        &mut used_channels,
                    ) {
                        Some((ch, ch_side)) => Plan::BackChannel { ch, side: ch_side },
                        None => Plan::BackBow { side },
                    }
                } else {
                    Plan::Normal
                };
                plans.push(Some(plan));
            }
            _ => plans.push(None),
        }
    }

    // —— 端口分配 ——
    // 自环 / 双向对使用专用路由,不参与槽位分组。
    //
    // 分两轮:先普通边,并记录每个节点的出/入端面占用;回边随后避让 ——
    // 默认端面已被**反向用途**占用(同一端面一进一出)时改走侧面端口,
    // 让输入与输出尽量不挤在节点的同一个端面上。
    let mut edge_ports: Vec<Option<(Port, Port)>> = vec![None; gg.edges.len()];
    let mut used_out: HashMap<String, HashSet<Port>> = HashMap::new();
    let mut used_in: HashMap<String, HashSet<Port>> = HashMap::new();

    // 第一轮:普通边(pick_port 只依赖节点对,与分配顺序无关)。
    for (i, e) in gg.edges.iter().enumerate() {
        if !matches!(plans[i], Some(Plan::Normal) | None) {
            continue;
        }
        if let (Some(s), Some(t)) = (node_map.get(&e.source), node_map.get(&e.target)) {
            let sp = edge_port(s, t, direction, true, false, 0.0, used_in.get(&e.source));
            let tp = edge_port(t, s, direction, false, false, 0.0, used_out.get(&e.target));
            used_out.entry(e.source.clone()).or_default().insert(sp);
            used_in.entry(e.target.clone()).or_default().insert(tp);
            edge_ports[i] = Some((sp, tp));
        }
    }

    // 第二轮:回边按优先级序列避让;BackChannel 固定侧面通道端口(通道几何
    // 要求),但同样登记占用,供后续边避让侧面。
    for (i, e) in gg.edges.iter().enumerate() {
        match plans[i] {
            Some(Plan::BackBow { side }) => {
                if let (Some(s), Some(t)) = (node_map.get(&e.source), node_map.get(&e.target)) {
                    let sp = edge_port(s, t, direction, true, true, side, used_in.get(&e.source));
                    let tp = edge_port(t, s, direction, false, true, side, used_out.get(&e.target));
                    used_out.entry(e.source.clone()).or_default().insert(sp);
                    used_in.entry(e.target.clone()).or_default().insert(tp);
                    edge_ports[i] = Some((sp, tp));
                }
            }
            Some(Plan::BackChannel { side, .. }) => {
                let (sp, tp) = channel_ports(side, direction);
                used_out.entry(e.source.clone()).or_default().insert(sp);
                used_in.entry(e.target.clone()).or_default().insert(tp);
                edge_ports[i] = Some((sp, tp));
            }
            _ => {}
        }
    }

    // 分组:key = (节点 id, 端口),value = [(边索引, 对端在该端口边轴向上的投影)]
    let mut src_groups: HashMap<(String, Port), Vec<(usize, f64)>> = HashMap::new();
    let mut tgt_groups: HashMap<(String, Port), Vec<(usize, f64)>> = HashMap::new();
    for (i, e) in gg.edges.iter().enumerate() {
        let Some((sp, tp)) = edge_ports[i] else {
            continue;
        };
        let (Some(s), Some(t)) = (node_map.get(&e.source), node_map.get(&e.target)) else {
            continue;
        };
        src_groups
            .entry((e.source.clone(), sp))
            .or_default()
            .push((i, axis_projection(sp, t.center)));
        tgt_groups
            .entry((e.target.clone(), tp))
            .or_default()
            .push((i, axis_projection(tp, s.center)));
    }

    // 组内按对端投影排序 → 槽位 (idx, total):
    // 端口排列顺序与对端的空间顺序一致,fan-out/fan-in 不会互相错位打结。
    let mut slots: Vec<(Slot, Slot)> =
        vec![(Slot { idx: 0, total: 1 }, Slot { idx: 0, total: 1 }); gg.edges.len()];
    rank_slots(src_groups, &mut slots, true);
    rank_slots(tgt_groups, &mut slots, false);

    // —— 逐边生成路由 ——
    for (i, e) in gg.edges.iter_mut().enumerate() {
        let Some(plan) = plans[i] else {
            continue;
        };
        let (Some(s), Some(t)) = (node_map.get(&e.source), node_map.get(&e.target)) else {
            continue;
        };
        let s = *s;
        let t = *t;
        let (s_slot, t_slot) = slots[i];

        let mut anchor_override = None;
        // 自环 / 双向对使用专用路由(无端口概念),其余边需要端口配对。
        let route = match plan {
            Plan::SelfLoop => {
                let (r, anchor) = self_loop_route(s);
                anchor_override = Some(anchor);
                r
            }
            Plan::Mutual { side } => mutual_dual_route(s, t, side),
            _ => {
                let Some((sp, tp)) = edge_ports[i] else {
                    continue;
                };
                let s_port = (sp, s_slot);
                let t_port = (tp, t_slot);
                match plan {
                    Plan::BackBow { side } => back_edge_route(s, t, &s_port, &t_port, side),
                    Plan::BackChannel { ch, .. } => channel_route(s, t, ch, &s_port, &t_port),
                    _ => match e.routing_hint {
                        RoutingHint::Spline => spline_route_safe(
                            s,
                            t,
                            &s_port,
                            &t_port,
                            &obstacles,
                            &mut used_channels,
                        ),
                        _ => orthogonal_route(s, t, &s_port, &t_port, &obstacles, &mut used_trunks),
                    },
                }
            }
        };
        // 边标签锚点:取路由中段的中点;自环取环外侧。
        if e.label_text.is_some() {
            e.label_anchor = Some(anchor_override.unwrap_or_else(|| route.midpoint()));
        }
        e.route = route;
    }
}

/// 检测 mutual 对并为每条边分配绕行侧。
/// `side`: +1 = 右侧,-1 = 左侧。两条相对边一左一右对称。
fn mutual_side_for(source: &str, target: &str, seen: &mut HashMap<(String, String), f64>) -> f64 {
    let key = (source.to_owned(), target.to_owned());
    if let Some(&s) = seen.get(&key) {
        return s;
    }
    let reverse_key = (target.to_owned(), source.to_owned());
    // 若反向边已分配,则本条分配相反侧。
    if let Some(&rev) = seen.get(&reverse_key) {
        let side = -rev;
        seen.insert(key, side);
        side
    } else {
        seen.insert(key, 1.0);
        1.0
    }
}

// ============================================================
// 自环
// ============================================================

/// 自环路由:从节点右侧伸出一个小环,绕回节点右侧(官方 mermaid 风格)。
/// 返回(路由,环外侧的标签锚点)。
fn self_loop_route(n: &GGNode) -> (RoutePath, Point) {
    let (hw, hh) = (n.size.width / 2.0, n.size.height / 2.0);
    // 起止点取在形状边界上(菱形/圆按边界投影)。
    let p0 = port_point_offset(n, Port::Right, -hh * 0.3);
    let p3 = port_point_offset(n, Port::Right, hh * 0.3);
    let ext = (hh * 1.2).max(30.0);
    let outer = n.center.x + hw + ext;
    let p1 = Point::new(outer, n.center.y - hh * 0.9);
    let p2 = Point::new(outer, n.center.y + hh * 0.9);
    let anchor = Point::new(outer + 10.0, n.center.y);

    let mut r = RoutePath::new();
    r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
    (r, anchor)
}

// ============================================================
// 双向对(u↔v)
// ============================================================

/// 双向对路由:两条相对边小偏移 + 轻微外弓(官方 mermaid 的"双线紧贴"风格)。
///
/// 偏移量约为节点宽度的 7%(clamp 4~12px),两条线一左一右几乎平行,
/// 控制点沿同侧再外弓一点,形成柔和的弧线而非僵硬直线。
fn mutual_dual_route(s: &GGNode, t: &GGNode, side: f64) -> RoutePath {
    let dx = (t.center.x - s.center.x).abs();
    let dy = (t.center.y - s.center.y).abs();
    let off = (s.size.width.min(t.size.width) * 0.07).clamp(4.0, 12.0);
    let bow = off * 0.9;

    let mut r = RoutePath::new();
    if dy >= dx {
        // 垂直堆叠:上节点底边出、下节点顶边入(反向边反之)。
        let forward = t.center.y >= s.center.y;
        let sp = if forward { Port::Bottom } else { Port::Top };
        let tp = if forward { Port::Top } else { Port::Bottom };
        let p0 = port_point_offset(s, sp, side * off);
        let p3 = port_point_offset(t, tp, side * off);
        let h = p3.y - p0.y;
        let p1 = Point::new(p0.x + side * bow, p0.y + h * 0.45);
        let p2 = Point::new(p3.x + side * bow, p3.y - h * 0.45);
        r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
    } else {
        // 水平堆叠:左节点右边出、右节点左边入。
        let forward = t.center.x >= s.center.x;
        let sp = if forward { Port::Right } else { Port::Left };
        let tp = if forward { Port::Left } else { Port::Right };
        let p0 = port_point_offset(s, sp, side * off);
        let p3 = port_point_offset(t, tp, side * off);
        let w = p3.x - p0.x;
        let p1 = Point::new(p0.x + w * 0.45, p0.y + side * bow);
        let p2 = Point::new(p3.x - w * 0.45, p3.y + side * bow);
        r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
    }
    r
}

// ============================================================
// 回边
// ============================================================

/// 回边路由:两端都在主轴上时为侧向 U 形贝塞尔(源主轴反向端口出、目标对侧
/// 端口入,控制点沿主轴外扩);端口避让后任一端改走侧面端口时,控制点分别
/// 沿两端口的**外法向**延伸,形成「源端口直出 → 转弯 → 沿目标端口法向进入」
/// 的光滑弧线。
fn back_edge_route(
    s: &GGNode,
    t: &GGNode,
    s_slot: &(Port, Slot),
    t_slot: &(Port, Slot),
    side: f64,
) -> RoutePath {
    let (sp, tp) = (s_slot.0, t_slot.0);
    let p0 = port_point_at(s, sp, s_slot.1);
    let p3 = port_point_at(t, tp, t_slot.1);

    let mut r = RoutePath::new();
    let src_side = matches!(sp, Port::Left | Port::Right);
    let tgt_side = matches!(tp, Port::Left | Port::Right);
    if src_side || tgt_side {
        // 任一端走侧面端口:控制点 = 端口点 + 外法向 × 延伸量。
        let dx = p3.x - p0.x;
        let dy = p3.y - p0.y;
        let ext = ((dx * dx + dy * dy).sqrt() * 0.45).clamp(24.0, 200.0);
        let (n1, n2) = (port_normal(sp), port_normal(tp));
        let p1 = Point::new(p0.x + n1.0 * ext, p0.y + n1.1 * ext);
        let p2 = Point::new(p3.x + n2.0 * ext, p3.y + n2.1 * ext);
        r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
        return r;
    }
    if matches!(sp, Port::Top | Port::Bottom) {
        // 垂直主轴:控制点沿 y 外扩、x 按 side 错开。
        let d = if p3.y >= p0.y { 1.0 } else { -1.0 };
        let y_ext = (p3.y - p0.y).abs() * 0.4 + 20.0;
        let x_off = s.size.width.max(t.size.width) * 0.25;
        let p1 = Point::new(p0.x + side * x_off, p0.y + d * y_ext);
        let p2 = Point::new(p3.x - side * x_off, p3.y - d * y_ext);
        r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
    } else {
        // 水平主轴:控制点沿 x 外扩、y 按 side 错开。
        let d = if p3.x >= p0.x { 1.0 } else { -1.0 };
        let x_ext = (p3.x - p0.x).abs() * 0.4 + 20.0;
        let y_off = s.size.height.max(t.size.height) * 0.25;
        let p1 = Point::new(p0.x + d * x_ext, p0.y + side * y_off);
        let p2 = Point::new(p3.x - d * x_ext, p3.y - side * y_off);
        r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
    }
    r
}

/// 回边通道绕行路由:从源/目标的侧边端口出发,经垂直(或水平)通道直连,
/// 圆角折线平滑过渡。
fn channel_route(
    s: &GGNode,
    t: &GGNode,
    ch: f64,
    s_slot: &(Port, Slot),
    t_slot: &(Port, Slot),
) -> RoutePath {
    let (sp, tp) = (s_slot.0, t_slot.0);
    let p0 = port_point_at(s, sp, s_slot.1);
    let p3 = port_point_at(t, tp, t_slot.1);
    // 侧端口(Left/Right)→ 垂直通道,直接在真实空间构建;
    // 顶/底端口 → 水平通道,转置到垂直空间构建后转置回来。
    let vertical = matches!(sp, Port::Left | Port::Right);
    let tr = |p: Point| if vertical { p } else { Point::new(p.y, p.x) };
    let (a, b) = (tr(p0), tr(p3));
    let pts_v = [a, Point::new(ch, a.y), Point::new(ch, b.y), b];
    if vertical {
        rounded_polyline(&pts_v, CORNER_RADIUS)
    } else {
        let pts: Vec<Point> = pts_v.iter().map(|p| tr(*p)).collect();
        rounded_polyline(&pts, CORNER_RADIUS)
    }
}

/// 通道是否与已占用通道同坐标且跨度重叠。
fn channel_conflicts_used(ch: f64, top: f64, bot: f64, used: &[(f64, f64, f64)]) -> bool {
    used.iter()
        .any(|(c, y0, y1)| (c - ch).abs() < 2.0 && *y1 > top && *y0 < bot)
}

/// 正交主干是否与已占用主干同坐标且跨度重叠。
fn trunk_conflicts_used(trunk: f64, s0: f64, s1: f64, used: &[(f64, f64, f64)]) -> bool {
    used.iter()
        .any(|(t, u0, u1)| (t - trunk).abs() < 2.0 && *u1 > s0 && *u0 < s1)
}

/// 尝试某一侧的通道:先让开节点/容器,再避开已占用通道(冲突时沿侧向堆叠一格重试)。
#[allow(clippy::too_many_arguments)]
fn try_channel_side(
    start: f64,
    to_right: bool,
    top: f64,
    bot: f64,
    rects: &[(usize, Rect, String)],
    grid: &RectGrid,
    skip: &dyn Fn(&str) -> bool,
    ends_clear: &dyn Fn(f64) -> bool,
    used: &[(f64, f64, f64)],
) -> Option<f64> {
    let mut ch = settle_channel(start, to_right, top, bot, rects, grid, skip)?;
    if !ends_clear(ch) {
        return None;
    }
    for _ in 0..6 {
        if !channel_conflicts_used(ch, top, bot, used) {
            return Some(ch);
        }
        // 沿远离通道的方向堆叠一格,并重新让开节点/容器。
        ch = if to_right {
            ch + CH_STACK
        } else {
            ch - CH_STACK
        };
        ch = settle_channel(ch, to_right, top, bot, rects, grid, skip)?;
        if !ends_clear(ch) {
            return None;
        }
    }
    None
}

/// 规划回边通道:若 source/target 之间存在节点或容器阻挡,计算一条无冲突且
/// 不与其他边通道重叠的侧边通道坐标(垂直空间)。
/// 成功时登记占用并返回(通道坐标, 侧向 ±1);无阻挡或无解 → None。
///
/// 所有矩形查询走网格索引(`obstacles.grid` / `grid_t`,按主轴方向选用),
/// 不再为每条回边克隆整份矩形列表。
fn plan_back_channel(
    s: &GGNode,
    t: &GGNode,
    direction: crate::ast::Direction,
    obstacles: &Obstacles,
    edges: &[(String, String)],
    centers: &HashMap<&str, Point>,
    used: &mut Vec<(f64, f64, f64)>,
) -> Option<(f64, f64)> {
    use crate::ast::Direction;
    let vert = matches!(direction, Direction::TB | Direction::TD | Direction::BT);
    let tr = |p: Point| if vert { p } else { Point::new(p.y, p.x) };
    let (rects, grid) = if vert {
        (&obstacles.rects, &obstacles.grid)
    } else {
        (&obstacles.rects_t, &obstacles.grid_t)
    };
    let sc = tr(s.center);
    let tc = tr(t.center);
    // 回边:源在 target 下游(垂直空间 sc.y > tc.y)。异常朝向放弃通道。
    if sc.y <= tc.y {
        return None;
    }
    let (top, bot) = (tc.y, sc.y);
    let mid_x = (sc.x + tc.x) / 2.0;
    let skip = edge_skip(&s.id, &t.id, &obstacles.members);

    // 阻挡带:两中心之间的矩形(外扩 BAND_PAD),命中即视为阻挡。
    let band = Rect::new(
        (sc.x.min(tc.x) - BAND_PAD).min(mid_x - BAND_PAD),
        top,
        (sc.x.max(tc.x) + BAND_PAD).max(mid_x + BAND_PAD),
        bot,
    );
    let mut any = false;
    let mut ob_min = f64::INFINITY;
    let mut ob_max = f64::NEG_INFINITY;
    grid.for_each(band, |i| {
        let (_, r, owner) = &rects[i];
        if skip(owner) {
            return;
        }
        if rects_overlap(*r, band) {
            any = true;
            ob_min = ob_min.min(r.min_x());
            ob_max = ob_max.max(r.max_x());
        }
    });
    if !any {
        return None;
    }

    // 左右两侧各推一条通道,并验证水平引入段(源/目标中心高度处)。
    let ends_clear = |ch: f64| -> bool {
        [sc.y, tc.y].iter().all(|&y| {
            polyline_clear(
                &[Point::new(sc.x.min(ch), y), Point::new(sc.x.max(ch), y)],
                &skip,
                rects,
                grid,
            ) && polyline_clear(
                &[Point::new(tc.x.min(ch), y), Point::new(tc.x.max(ch), y)],
                &skip,
                rects,
                grid,
            )
        })
    };
    let right = try_channel_side(
        ob_max + CHANNEL_MARGIN,
        true,
        top,
        bot,
        rects,
        grid,
        &skip,
        &ends_clear,
        used,
    );
    let left = try_channel_side(
        ob_min - CHANNEL_MARGIN,
        false,
        top,
        bot,
        rects,
        grid,
        &skip,
        &ends_clear,
        used,
    );

    // 交叉者计数:候选通道之外(beyond)若有节点,且该节点有边连到
    // 「通道跨度内的节点」(如 E→C 连 C,C 位于 B..D 跨度内),则该边的
    // 绕行/进入段会与本通道交叉 —— 该侧应降级。用于在两侧等距时选空侧。
    let crossers = |ch: f64, to_right: bool| -> usize {
        let beyond = |x: f64| if to_right { x > ch } else { x < ch };
        let mut cnt = 0usize;
        for (u, v) in edges {
            if u == &s.id || u == &t.id || v == &s.id || v == &t.id {
                continue;
            }
            let (Some(&uc), Some(&vc)) = (centers.get(u.as_str()), centers.get(v.as_str())) else {
                continue;
            };
            // 统一在垂直空间比较(通道规划空间)。
            let (uc, vc) = (tr(uc), tr(vc));
            let in_span = if beyond(uc.x) {
                vc.y
            } else if beyond(vc.x) {
                uc.y
            } else {
                continue;
            };
            if in_span > top && in_span < bot {
                cnt += 1;
            }
        }
        cnt
    };
    let score =
        |ch: f64, to_right: bool| -> (usize, f64) { (crossers(ch, to_right), (ch - mid_x).abs()) };

    let chosen = match (right, left) {
        (Some(r), Some(l)) => {
            if score(r, true) <= score(l, false) {
                Some((r, 1.0))
            } else {
                Some((l, -1.0))
            }
        }
        (Some(r), None) => Some((r, 1.0)),
        (None, Some(l)) => Some((l, -1.0)),
        (None, None) => None,
    };
    if let Some((ch, _)) = chosen {
        used.push((ch, top, bot));
    }
    chosen
}

/// 从初始通道坐标出发,沿 `to_right` 方向逐个让开挡路的节点/容器矩形,
/// 直到垂直段 (ch, top..bot) 无冲突。超过迭代上限返回 None。
///
/// 候选阻挡用网格粗筛(x 夹住 ch、y 与 [top, bot] 相交的薄带);
/// 多个阻挡时取**下标最小**者,与原「按 rects 顺序取第一个」行为一致。
fn settle_channel(
    mut ch: f64,
    to_right: bool,
    top: f64,
    bot: f64,
    rects: &[(usize, Rect, String)],
    grid: &RectGrid,
    skip: &dyn Fn(&str) -> bool,
) -> Option<f64> {
    for _ in 0..60 {
        let area = Rect::new(ch - 1e-6, top, ch + 1e-6, bot);
        let mut blocker: Option<usize> = None;
        grid.for_each(area, |i| {
            let (_, r, owner) = &rects[i];
            if skip(owner) {
                return;
            }
            if r.min_y() <= bot && r.max_y() >= top && r.min_x() < ch && r.max_x() > ch {
                blocker = Some(blocker.map_or(i, |b: usize| b.min(i)));
            }
        });
        let Some(i) = blocker else { return Some(ch) };
        let r = rects[i].1;
        ch = if to_right {
            r.max_x() + CHANNEL_MARGIN
        } else {
            r.min_x() - CHANNEL_MARGIN
        };
    }
    None
}

// ============================================================
// 普通边:Spline
// ============================================================

/// Spline 路由:单段三次贝塞尔(官方 mermaid 风格)。
///
/// 端口锚点(含槽位分散)作为 P0/P3;控制点 P1/P2 沿源/目标端口法向延伸,
/// 并沿**垂直流向**(横向)偏置,产生平滑的扇形弧线。
///
/// 关键约束(保证 fan-out / fan-in 多条边**互不交叉**):
/// - 法向延伸量 `arc` 只取**两端口沿流向的净间距** `gap` 的一半,绝不超过 `gap`。
///   若按 `max(|dx|,|dy|)` 取弧度,`gap` 很小而横向跨度很大的边(典型 fan-out)
///   会把控制点甩过目标行的外侧,形成 S 形回绕 —— 相邻出边因而互相穿过。
/// - `arc <= gap` 时曲线在主轴上是**单调**的,同一源节点的多条出边不会相交。
fn spline_route(s: &GGNode, t: &GGNode, s_slot: &(Port, Slot), t_slot: &(Port, Slot)) -> RoutePath {
    let sp = s_slot.0;
    let tp = t_slot.0;
    let p0 = port_point_at(s, sp, s_slot.1);
    let p3 = port_point_at(t, tp, t_slot.1);

    // 端口出/入方向:沿端口法向(离开节点)。
    let (ox, oy) = port_normal(sp);
    let (ix, iy) = port_normal(tp);

    // 沿流向(source 端口法向)的净间距 = 两端口之间可用的"空白走廊"长度。
    // 负值(逆流,如非 back-edge 的回绕情形)按 0 处理。
    let gap = ((p3.x - p0.x) * ox + (p3.y - p0.y) * oy).max(0.0);

    // 弧度:取间距的一半并 clamp;再约束 `arc <= gap`,避免控制点越过对端造成回绕。
    // `gap == 0`(同层同高)时退回 MIN_ARC,让边略微鼓出而不至于退化成直线。
    let mut arc = (gap * 0.5).clamp(MIN_ARC, MAX_ARC);
    if gap > 0.0 {
        arc = arc.min(gap);
    }

    // 横向偏置:取垂直于流向的位移分量(纵向端口 → 横向;横向端口 → 纵向),
    // 让 fan-out / fan-in 的多条边呈对称扇形展开。系数 1/4 保证 x 控制点逐项单调
    // (P0 → P0+1/4 → P3-1/4 → P3),曲线在横向也单调。
    let vx = p3.x - p0.x;
    let vy = p3.y - p0.y;
    let tvx = vx * (1.0 - ox * ox) - vy * ox * oy;
    let tvy = vy * (1.0 - oy * oy) - vx * ox * oy;
    let bias = 0.25;

    let p1 = Point::new(p0.x + ox * arc + tvx * bias, p0.y + oy * arc + tvy * bias);
    let p2 = Point::new(p3.x + ix * arc - tvx * bias, p3.y + iy * arc - tvy * bias);

    let mut r = RoutePath::new();
    r.push(RouteSegment::CubicBezier { p0, p1, p2, p3 });
    r
}

/// 带节点/容器回避的 Spline 路由:先按朴素贝塞尔生成并采样检测;
/// 若穿过非端点节点或「两端均非成员」的容器,则改为「stub + 侧边通道 +
/// 圆角折线」的绕行路径(官方 mermaid 的长边风格:出节点后平移到旁侧空通道,
/// 直行,再平滑进入目标)。
fn spline_route_safe(
    s: &GGNode,
    t: &GGNode,
    s_slot: &(Port, Slot),
    t_slot: &(Port, Slot),
    obstacles: &Obstacles,
    used_channels: &mut Vec<(f64, f64, f64)>,
) -> RoutePath {
    let naive = spline_route(s, t, s_slot, t_slot);
    let (sp, tp) = (s_slot.0, t_slot.0);
    let vertical = matches!(sp, Port::Top | Port::Bottom);
    let tr = |p: Point| if vertical { p } else { Point::new(p.y, p.x) };
    let skip = edge_skip(&s.id, &t.id, &obstacles.members);
    // 统一在垂直空间做检测与规划(水平主轴时转置,垂直主轴保持原样);
    // 矩形/索引从 Obstacles 取,避免每边克隆整份列表。
    let (rects_v, grid_v) = if vertical {
        (&obstacles.rects, &obstacles.grid)
    } else {
        (&obstacles.rects_t, &obstacles.grid_t)
    };

    // 采样朴素曲线,收集穿过的非端点节点 / 非成员容器。
    let samples_v: Vec<Point> = sample_path(&naive, 32).iter().map(|p| tr(*p)).collect();
    let mut hit = false;
    let mut hit_min = f64::INFINITY;
    let mut hit_max = f64::NEG_INFINITY;
    for w in samples_v.windows(2) {
        let area = seg_bbox(w[0], w[1]);
        grid_v.for_each(area, |i| {
            let (_, r, owner) = &rects_v[i];
            if skip(owner) {
                return;
            }
            if segment_intersects_rect(w[0], w[1], r) {
                hit = true;
                hit_min = hit_min.min(r.min_x());
                hit_max = hit_max.max(r.max_x());
            }
        });
    }
    if !hit {
        return naive;
    }

    // 绕行折线(垂直空间):p0 → 出 stub → 平移到通道 → 直行 → 入 stub → p3。
    let p0v = tr(port_point_at(s, sp, s_slot.1));
    let p3v = tr(port_point_at(t, tp, t_slot.1));
    let down = p3v.y >= p0v.y;
    let av = Point::new(p0v.x, p0v.y + if down { STUB } else { -STUB });
    let bv = Point::new(p3v.x, p3v.y + if down { -STUB } else { STUB });
    let top = av.y.min(bv.y);
    let bot = av.y.max(bv.y);

    // 候选通道:命中障碍两侧 + 所有障碍最外侧(兜底)。
    let mut candidates: Vec<f64> = Vec::new();
    if let Some(ch) = settle_channel(
        hit_max + CHANNEL_MARGIN,
        true,
        top,
        bot,
        rects_v,
        grid_v,
        &skip,
    ) {
        candidates.push(ch);
    }
    if let Some(ch) = settle_channel(
        hit_min - CHANNEL_MARGIN,
        false,
        top,
        bot,
        rects_v,
        grid_v,
        &skip,
    ) {
        candidates.push(ch);
    }
    let (all_min, all_max) = if vertical {
        (obstacles.all_min_x, obstacles.all_max_x)
    } else {
        (obstacles.all_min_x_t, obstacles.all_max_x_t)
    };
    candidates.push(all_max + CHANNEL_MARGIN);
    candidates.push(all_min - CHANNEL_MARGIN);

    // 两轮评估:先要求不与已占用通道重叠(边间避让),
    // 全部被拒时放宽(节点/容器避让优先于边间避让)。
    let mut best: Option<(f64, f64)> = None; // (cost, ch)
    for strict in [true, false] {
        for ch in &candidates {
            let pts = [
                p0v,
                av,
                Point::new(*ch, av.y),
                Point::new(*ch, bv.y),
                bv,
                p3v,
            ];
            if !polyline_clear(&pts, &skip, rects_v, grid_v) {
                continue;
            }
            if strict && channel_conflicts_used(*ch, top, bot, used_channels) {
                continue;
            }
            let cost = (ch - p0v.x).abs() + (ch - p3v.x).abs();
            let better = match best {
                None => true,
                Some((bc, bch)) => cost < bc || (cost == bc && *ch > bch),
            };
            if better {
                best = Some((cost, *ch));
            }
        }
        if best.is_some() {
            break;
        }
    }
    let Some((_, ch)) = best else { return naive };
    used_channels.push((ch, top, bot));

    let pts_v = [p0v, av, Point::new(ch, av.y), Point::new(ch, bv.y), bv, p3v];
    if vertical {
        rounded_polyline(&pts_v, CORNER_RADIUS)
    } else {
        let pts: Vec<Point> = pts_v.iter().map(|p| tr(*p)).collect();
        rounded_polyline(&pts, CORNER_RADIUS)
    }
}

// ============================================================
// 普通边:Orthogonal
// ============================================================

/// 正交路由:stub + 主干折线(曼哈顿)。
///
/// 主干位置(垂直主轴 = 共享 x;水平主轴 = 共享 y)从两 stub 端点中点出发,
/// 沿主轴法向双向步进搜索,取「冲突最少、偏移最小」的落点,
/// 处理长边跨层(主干初始位置落在中间层节点内)的情况。
///
/// 阶段 0.6(边-边避让):正交主干互斥 —— 后规划的边不与已占用主干同坐标
/// 且跨度重叠,同层间的并行边沿侧向错开,避免画在同一条线上。
/// 评分字典序 (节点冲突数, 是否占用主干, |偏移|),节点避让优先于边间避让
/// (与 Spline 通道的两轮严格/放宽评估一致)。
fn orthogonal_route(
    s: &GGNode,
    t: &GGNode,
    s_slot: &(Port, Slot),
    t_slot: &(Port, Slot),
    obstacles: &Obstacles,
    used_trunks: &mut Vec<(f64, f64, f64)>,
) -> RoutePath {
    let (sp, tp) = (s_slot.0, t_slot.0);
    let start = port_point_at(s, sp, s_slot.1);
    let end = port_point_at(t, tp, t_slot.1);
    let skip = edge_skip(&s.id, &t.id, &obstacles.members);
    let vertical_main = matches!(sp, Port::Top | Port::Bottom);

    // 出/入 stub 端点(沿端口法向)。
    let (a, b) = if vertical_main {
        let out = if sp == Port::Bottom { STUB } else { -STUB };
        let inn = if tp == Port::Top { -STUB } else { STUB };
        (
            Point::new(start.x, start.y + out),
            Point::new(end.x, end.y + inn),
        )
    } else {
        let out = if sp == Port::Right { STUB } else { -STUB };
        let inn = if tp == Port::Left { -STUB } else { STUB };
        (
            Point::new(start.x + out, start.y),
            Point::new(end.x + inn, end.y),
        )
    };
    let base_trunk = if vertical_main {
        (a.x + b.x) / 2.0
    } else {
        (a.y + b.y) / 2.0
    };
    // 主干沿主轴方向的跨度(垂直主干 = y 跨度;水平主干 = x 跨度)。
    let (s0, s1) = if vertical_main {
        (a.y.min(b.y), a.y.max(b.y))
    } else {
        (a.x.min(b.x), a.x.max(b.x))
    };

    let build = |trunk: f64| -> Vec<Point> {
        if vertical_main {
            // 垂直主干:在 stub 端点高度接入主干,保持全程曼哈顿。
            vec![
                start,
                a,
                Point::new(trunk, a.y),
                Point::new(trunk, b.y),
                b,
                end,
            ]
        } else {
            vec![
                start,
                a,
                Point::new(a.x, trunk),
                Point::new(b.x, trunk),
                b,
                end,
            ]
        }
    };

    let mut candidates: Vec<f64> = vec![base_trunk];
    for step in 1..=MAX_OFFSET_TRIES {
        candidates.push(base_trunk + step as f64 * OFFSET_STEP);
        candidates.push(base_trunk - step as f64 * OFFSET_STEP);
    }
    let mut best: Option<(usize, bool, f64, Vec<Point>)> = None;
    for trunk in candidates {
        let pts = build(trunk);
        let c = polyline_conflicts(&pts, &skip, &obstacles.rects, &obstacles.grid);
        let used = trunk_conflicts_used(trunk, s0, s1, used_trunks);
        let better = match &best {
            None => true,
            Some((bc, bu, bt, _)) => {
                c < *bc
                    || (c == *bc && (!used && *bu))
                    || (c == *bc
                        && used == *bu
                        && (trunk - base_trunk).abs() < (bt - base_trunk).abs())
            }
        };
        if better {
            best = Some((c, used, trunk, pts));
        }
        if c == 0 && !used {
            break;
        }
    }
    let Some((c, _, trunk, pts)) = best else {
        return points_to_route(&build(base_trunk));
    };
    // 主干不穿节点时才登记占用(穿节点的坏主干不占用,避免影响其它边)。
    if c == 0 {
        used_trunks.push((trunk, s0, s1));
    }
    points_to_route(&pts)
}

// ============================================================
// 端口
// ============================================================

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
enum Port {
    Top,
    Bottom,
    Left,
    Right,
}

/// 端口法向(离开节点方向)单位向量。
fn port_normal(p: Port) -> (f64, f64) {
    match p {
        Port::Top => (0.0, -1.0),
        Port::Bottom => (0.0, 1.0),
        Port::Left => (-1.0, 0.0),
        Port::Right => (1.0, 0.0),
    }
}

/// 节点包围盒(含少量 padding 以避免边贴边)。
fn node_rect(n: &GGNode) -> Rect {
    let half_w = n.size.width / 2.0 + 2.0;
    let half_h = n.size.height / 2.0 + 2.0;
    Rect::new(
        n.center.x - half_w,
        n.center.y - half_h,
        n.center.x + half_w,
        n.center.y + half_h,
    )
}

/// 选端口:从 `from` 节点朝 `toward` 节点出线(或入线)时,用哪一面。
///
/// 规则:
/// - **优先沿布局主轴**(TB/BT → Top/Bottom;LR/RL → Left/Right),让 fan-out/fan-in
///   的多条边在同一条边上分散,符合流程图的流动方向;
/// - **当对端几乎位于侧方时切换到侧面端口**(`SIDE_BIAS` 判据:两节点中心连线与
///   节点矩形边界的交点落在哪条边)。否则 TB 布局里同层横向相邻的两点会被迫
///   "从底边出、绕到下方、再回到对面顶边",走一大圈。
fn pick_port(from: &GGNode, toward: &GGNode, direction: crate::ast::Direction) -> Port {
    use crate::ast::Direction;
    const SIDE_BIAS: f64 = 2.5;
    let dx = toward.center.x - from.center.x;
    let dy = toward.center.y - from.center.y;
    let hw = (from.size.width / 2.0).max(1.0);
    let hh = (from.size.height / 2.0).max(1.0);

    match direction {
        // TB/BT:主轴垂直;侧向(水平)显著时改用 Left/Right。
        Direction::TB | Direction::TD | Direction::BT => {
            if dx.abs() * hh > dy.abs() * hw * SIDE_BIAS {
                if dx >= 0.0 { Port::Right } else { Port::Left }
            } else if dy >= 0.0 {
                Port::Bottom
            } else {
                Port::Top
            }
        }
        // LR/RL:主轴水平;侧向(垂直)显著时改用 Top/Bottom。
        Direction::LR | Direction::RL => {
            if dy.abs() * hw > dx.abs() * hh * SIDE_BIAS {
                if dy >= 0.0 { Port::Bottom } else { Port::Top }
            } else if dx >= 0.0 {
                Port::Right
            } else {
                Port::Left
            }
        }
    }
}

/// back edge 检测:source 在主轴方向上位于 target 「下游」(逆主轴)。
fn is_back_edge(s: &GGNode, t: &GGNode, direction: crate::ast::Direction) -> bool {
    use crate::ast::Direction;
    match direction {
        Direction::TB | Direction::TD => s.center.y > t.center.y,
        Direction::BT => s.center.y < t.center.y,
        Direction::LR => s.center.x > t.center.x,
        Direction::RL => s.center.x < t.center.x,
    }
}

/// 通道绕行边的端口:垂直主轴用侧端口(Right/Left),水平主轴用顶/底端口。
fn channel_ports(side: f64, direction: crate::ast::Direction) -> (Port, Port) {
    use crate::ast::Direction;
    let vert = matches!(direction, Direction::TB | Direction::TD | Direction::BT);
    if vert {
        if side > 0.0 {
            (Port::Right, Port::Right)
        } else {
            (Port::Left, Port::Left)
        }
    } else if side > 0.0 {
        (Port::Bottom, Port::Bottom)
    } else {
        (Port::Top, Port::Top)
    }
}

/// 单端端口选择(简易避让规则):
///
/// 优先级 —— **出边:下端 → 左右 → 上端;入边:上端 → 左右 → 下端**(垂直主轴;
/// 水平主轴转置)。回边沿主轴反向,序列随之反转;侧面(或上下)的先后按对端
/// 相对位置决定,朝向对端一侧优先(回边则由绕行侧 `side` 统一决定,保证两端
/// 同侧绕行、曲线不横穿节点群)。
///
/// 首选:普通边沿用 [`pick_port`] 的几何判断(斜边会直接选中侧面,通常就是
/// 序列首位);回边用主轴反向端口。若首选已被**反向用途**占用(同一端面
/// 一进一出),按序列跳到下一个空闲端面;全部冲突时退回首选,交给槽位分散。
///
/// 统计槽位与生成路由**必须**共用此函数的分配结果,否则槽位序号会与端口错配。
fn edge_port(
    n: &GGNode,
    peer: &GGNode,
    direction: crate::ast::Direction,
    is_out: bool,
    is_back: bool,
    side: f64,
    used_reverse: Option<&HashSet<Port>>,
) -> Port {
    use crate::ast::Direction;
    // 主轴端口:forward 出 = 顺主轴面,forward 入 = 逆主轴面;回边反转。
    let (out_main, in_main) = match direction {
        Direction::TB | Direction::TD => (Port::Bottom, Port::Top),
        Direction::BT => (Port::Top, Port::Bottom),
        Direction::LR => (Port::Right, Port::Left),
        Direction::RL => (Port::Left, Port::Right),
    };
    let (main, anti) = match (is_back, is_out) {
        (false, true) | (true, false) => (out_main, in_main),
        (false, false) | (true, true) => (in_main, out_main),
    };
    // 侧面顺序:普通边朝向对端一侧优先;回边按绕行侧(两端统一)。
    let (s1, s2) = if is_back {
        side_pair_centered(direction, side)
    } else {
        side_pair(n, peer, direction)
    };

    let first = if is_back {
        main
    } else {
        pick_port(n, peer, direction)
    };
    let mut cands = vec![first];
    for p in [main, s1, s2, anti] {
        if !cands.contains(&p) {
            cands.push(p);
        }
    }
    match used_reverse {
        Some(m) => cands.into_iter().find(|p| !m.contains(p)).unwrap_or(first),
        None => first,
    }
}

/// 回边与普通边共用的侧面顺序生成器:
/// - 垂直主轴:`bias > 0` → 右先左后,否则左先右后;
/// - 水平主轴:`bias > 0` → 下先上后,否则上先下后。
fn side_pair_centered(direction: crate::ast::Direction, bias: f64) -> (Port, Port) {
    use crate::ast::Direction;
    match direction {
        Direction::TB | Direction::TD | Direction::BT => {
            if bias > 0.0 {
                (Port::Right, Port::Left)
            } else {
                (Port::Left, Port::Right)
            }
        }
        Direction::LR | Direction::RL => {
            if bias > 0.0 {
                (Port::Bottom, Port::Top)
            } else {
                (Port::Top, Port::Bottom)
            }
        }
    }
}

/// 普通边的侧面顺序:按对端相对位置,朝向对端一侧优先
/// (垂直主轴比水平位置,水平主轴比垂直位置)。
fn side_pair(n: &GGNode, peer: &GGNode, direction: crate::ast::Direction) -> (Port, Port) {
    let bias = match direction {
        crate::ast::Direction::TB | crate::ast::Direction::TD | crate::ast::Direction::BT => {
            if peer.center.x >= n.center.x {
                1.0
            } else {
                -1.0
            }
        }
        _ => {
            if peer.center.y >= n.center.y {
                1.0
            } else {
                -1.0
            }
        }
    };
    side_pair_centered(direction, bias)
}

/// 对端节点在 `port` 所在边**轴向**上的投影坐标,用于给同一端口上的多条边排序。
/// Top/Bottom 边沿 x 展开 → 取 x;Left/Right 边沿 y 展开 → 取 y。
fn axis_projection(port: Port, peer: Point) -> f64 {
    match port {
        Port::Top | Port::Bottom => peer.x,
        Port::Left | Port::Right => peer.y,
    }
}

/// 组内按对端投影排序 → 写入槽位 (idx, total)。
fn rank_slots(
    groups: HashMap<(String, Port), Vec<(usize, f64)>>,
    slots: &mut [(Slot, Slot)],
    is_src: bool,
) {
    use std::cmp::Ordering;
    for (_, mut members) in groups {
        members.sort_by(|a, b| {
            a.1.partial_cmp(&b.1)
                .unwrap_or(Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });
        let total = members.len();
        for (idx, (edge_i, _)) in members.into_iter().enumerate() {
            let slot = Slot { idx, total };
            if is_src {
                slots[edge_i].0 = slot;
            } else {
                slots[edge_i].1 = slot;
            }
        }
    }
}

/// 节点边上按槽位分散的端口点(形状感知:端口永远落在形状边界上)。
fn port_point_at(n: &GGNode, p: Port, slot: Slot) -> Point {
    let span = match p {
        Port::Top | Port::Bottom => n.size.width,
        Port::Left | Port::Right => n.size.height,
    };
    if slot.total <= 1 {
        return port_point_offset(n, p, 0.0);
    }
    // 分散间距:先把边长按边数均分(铺满整条边),再 clamp:
    // - 下限 PORT_MIN_SPACING:节点很窄 / 边很多时不至于重叠成一团;
    // - 上限 span * PORT_MAX_SPACING_RATIO:宽节点上的少数几条边也能真正分散开。
    // 上限需不小于下限:节点很窄(span < PORT_MIN_SPACING / RATIO)时,
    // `span * RATIO` 会小于下限,导致 clamp 上下限倒置 panic。
    let mut spacing = (span / slot.total as f64).clamp(
        PORT_MIN_SPACING,
        (span * PORT_MAX_SPACING_RATIO).max(PORT_MIN_SPACING),
    );
    // 总跨度不得超出「边长 - 两侧留白」,避免端口点贴到节点角上。
    let usable = (span - 2.0 * PORT_CORNER_MARGIN).max(PORT_MIN_SPACING);
    spacing = spacing.min(usable / (slot.total - 1) as f64);
    // 居中对称:idx 从 -(total-1)/2 .. +(total-1)/2
    let offset = (slot.idx as f64 - (slot.total as f64 - 1.0) / 2.0) * spacing;
    port_point_offset(n, p, offset)
}

/// 端口点:节点 `p` 面上、沿该面偏移 `offset` 后的边界点。
///
/// 形状感知:
/// - Rectangle(及默认):端口在包围盒边上;
/// - Diamond:端口投影到菱形边界 `|dx|/hw + |dy|/hh = 1`(否则端口悬在包围盒
///   与菱形之间的空隙里,连线"连不上"菱形);
/// - Circle / DoubleCircle / StartDot / EndDot:投影到内切椭圆边界。
fn port_point_offset(n: &GGNode, p: Port, offset: f64) -> Point {
    let (hw, hh) = (n.size.width / 2.0, n.size.height / 2.0);
    let (cx, cy) = (n.center.x, n.center.y);
    match n.shape {
        ShapeKind::Diamond => match p {
            Port::Top | Port::Bottom => {
                let dx = offset.clamp(-hw * 0.75, hw * 0.75);
                let sign = if p == Port::Top { -1.0 } else { 1.0 };
                let y = hh * (1.0 - dx.abs() / hw.max(1e-9));
                Point::new(cx + dx, cy + sign * y)
            }
            Port::Left | Port::Right => {
                let dy = offset.clamp(-hh * 0.75, hh * 0.75);
                let sign = if p == Port::Left { -1.0 } else { 1.0 };
                let x = hw * (1.0 - dy.abs() / hh.max(1e-9));
                Point::new(cx + sign * x, cy + dy)
            }
        },
        ShapeKind::Circle | ShapeKind::DoubleCircle | ShapeKind::StartDot | ShapeKind::EndDot => {
            match p {
                Port::Top | Port::Bottom => {
                    let dx = offset.clamp(-hw * 0.9, hw * 0.9);
                    let sign = if p == Port::Top { -1.0 } else { 1.0 };
                    let t = (1.0 - (dx / hw.max(1e-9)).powi(2)).max(0.0).sqrt();
                    Point::new(cx + dx, cy + sign * hh * t)
                }
                Port::Left | Port::Right => {
                    let dy = offset.clamp(-hh * 0.9, hh * 0.9);
                    let sign = if p == Port::Left { -1.0 } else { 1.0 };
                    let t = (1.0 - (dy / hh.max(1e-9)).powi(2)).max(0.0).sqrt();
                    Point::new(cx + sign * hw * t, cy + dy)
                }
            }
        }
        _ => match p {
            Port::Top => Point::new(cx + offset, cy - hh),
            Port::Bottom => Point::new(cx + offset, cy + hh),
            Port::Left => Point::new(cx - hw, cy + offset),
            Port::Right => Point::new(cx + hw, cy + offset),
        },
    }
}

// ============================================================
// 几何工具
// ============================================================

/// 矩形转置 (x, y) 互换。
fn tr_rect(r: Rect) -> Rect {
    Rect::new(r.min_y(), r.min_x(), r.max_y(), r.max_x())
}

/// 两矩形是否相交(AABB)。
fn rects_overlap(a: Rect, b: Rect) -> bool {
    a.min_x() <= b.max_x()
        && a.max_x() >= b.min_x()
        && a.min_y() <= b.max_y()
        && a.max_y() >= b.min_y()
}

/// 矩形集合的 x 范围(用于兜底通道坐标)。
fn x_range(rects: &[(usize, Rect, String)]) -> (f64, f64) {
    let mut min = f64::INFINITY;
    let mut max = f64::NEG_INFINITY;
    for (_, r, _) in rects {
        min = min.min(r.min_x());
        max = max.max(r.max_x());
    }
    (min, max)
}

/// 折线是否完全避开非端点节点 / 非成员容器(true = 无冲突)。
///
/// `grid` 与 `rects` 必须属于同一空间(原始或转置);网格只做粗筛,
/// 精确判定仍走 `segment_intersects_rect`。
fn polyline_clear(
    pts: &[Point],
    skip: &dyn Fn(&str) -> bool,
    rects: &[(usize, Rect, String)],
    grid: &RectGrid,
) -> bool {
    for w in pts.windows(2) {
        let area = seg_bbox(w[0], w[1]);
        let mut clear = true;
        grid.for_each(area, |i| {
            if clear {
                let (_, r, owner) = &rects[i];
                if !skip(owner) && segment_intersects_rect(w[0], w[1], r) {
                    clear = false;
                }
            }
        });
        if !clear {
            return false;
        }
    }
    true
}

/// 折线穿过非端点节点 / 非成员容器的段数。
fn polyline_conflicts(
    pts: &[Point],
    skip: &dyn Fn(&str) -> bool,
    rects: &[(usize, Rect, String)],
    grid: &RectGrid,
) -> usize {
    pts.windows(2)
        .filter(|w| {
            let area = seg_bbox(w[0], w[1]);
            let mut hit = false;
            grid.for_each(area, |i| {
                if !hit {
                    let (_, r, owner) = &rects[i];
                    if !skip(owner) && segment_intersects_rect(w[0], w[1], r) {
                        hit = true;
                    }
                }
            });
            hit
        })
        .count()
}

/// 线段的轴对齐包围盒(网格查询窗口)。
fn seg_bbox(a: Point, b: Point) -> Rect {
    Rect::new(a.x.min(b.x), a.y.min(b.y), a.x.max(b.x), a.y.max(b.y))
}

/// 把折线点序列转成路由段序列(跳过重复点;相邻段合并共线)。
fn points_to_route(points: &[Point]) -> RoutePath {
    let mut pts: Vec<Point> = Vec::new();
    for &p in points {
        if let Some(last) = pts.last()
            && (p.x - last.x).abs() < 1e-9
            && (p.y - last.y).abs() < 1e-9
        {
            continue;
        }
        // 与倒数第二点共线则替换(用更远点,保持方向)。
        if pts.len() >= 2 {
            let a = pts[pts.len() - 2];
            let b = *pts.last().unwrap();
            if ((b.x - a.x) * (p.y - b.y) - (b.y - a.y) * (p.x - b.x)).abs() < 1e-6 {
                *pts.last_mut().unwrap() = p;
                continue;
            }
        }
        pts.push(p);
    }
    crate::builder::ir::geograph::line_route(&pts)
}

/// 圆角折线路由:每个拐点用二次贝塞尔(升阶为三次)平滑过渡,
/// 其余部分保持直线。用于通道绕行路径,观感接近官方的平滑长边。
fn rounded_polyline(pts: &[Point], radius: f64) -> RoutePath {
    let mut r = RoutePath::new();
    if pts.len() < 2 {
        return crate::builder::ir::geograph::line_route(pts);
    }
    let mut cur = pts[0];
    for i in 1..pts.len().saturating_sub(1) {
        let corner = pts[i];
        let next = pts[i + 1];
        let (d1x, d1y) = (corner.x - cur.x, corner.y - cur.y);
        let (d2x, d2y) = (next.x - corner.x, next.y - corner.y);
        let l1 = (d1x * d1x + d1y * d1y).sqrt();
        let l2 = (d2x * d2x + d2y * d2y).sqrt();
        if l1 < 1e-9 || l2 < 1e-9 {
            continue;
        }
        let rr = radius.min(l1 * 0.5).min(l2 * 0.5);
        let cs = Point::new(corner.x - d1x / l1 * rr, corner.y - d1y / l1 * rr);
        let ce = Point::new(corner.x + d2x / l2 * rr, corner.y + d2y / l2 * rr);
        if (cs.x - cur.x).abs() > 1e-6 || (cs.y - cur.y).abs() > 1e-6 {
            r.push(RouteSegment::Line { from: cur, to: cs });
        }
        // 二次贝塞尔 (cs, corner, ce) 升阶为三次。
        let p1 = Point::new(
            cs.x + (corner.x - cs.x) * 2.0 / 3.0,
            cs.y + (corner.y - cs.y) * 2.0 / 3.0,
        );
        let p2 = Point::new(
            ce.x + (corner.x - ce.x) * 2.0 / 3.0,
            ce.y + (corner.y - ce.y) * 2.0 / 3.0,
        );
        r.push(RouteSegment::CubicBezier {
            p0: cs,
            p1,
            p2,
            p3: ce,
        });
        cur = ce;
    }
    let last = *pts.last().unwrap();
    if (last.x - cur.x).abs() > 1e-6 || (last.y - cur.y).abs() > 1e-6 {
        r.push(RouteSegment::Line {
            from: cur,
            to: last,
        });
    }
    r
}

/// 把路由采样成折线点序列(贝塞尔按 `n` 等分采样),用于碰撞检测。
fn sample_path(r: &RoutePath, n: usize) -> Vec<Point> {
    let mut out = Vec::new();
    for seg in r.iter() {
        match *seg {
            RouteSegment::Line { from, to } => {
                for i in 0..=n {
                    let t = i as f64 / n as f64;
                    out.push(Point::new(
                        from.x + (to.x - from.x) * t,
                        from.y + (to.y - from.y) * t,
                    ));
                }
            }
            RouteSegment::CubicBezier { p0, p1, p2, p3 } => {
                for i in 0..=n {
                    let t = i as f64 / n as f64;
                    let u = 1.0 - t;
                    let (a, b, c, d) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t);
                    out.push(Point::new(
                        a * p0.x + b * p1.x + c * p2.x + d * p3.x,
                        a * p0.y + b * p1.y + c * p2.y + d * p3.y,
                    ));
                }
            }
        }
    }
    out
}

use super::spatial::segment_intersects_rect;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::builder::ir::common::{
        ArrowKind, ArrowSpec, ContainerKind, NodeDetail, NodeRole, ResolvedPorts,
    };
    use crate::builder::ir::geograph::{GGContainer, GGEdge, GGNode, Geograph};
    use crate::builder::ir::shape::ShapeKind;
    use crate::builder::ir::unigraph::EdgeKind;

    fn mk_node(id: &str, x: f64, y: f64, w: f64, h: f64) -> GGNode {
        let p = Point::new(x, y);
        GGNode {
            id: id.to_string(),
            role: NodeRole::Atom,
            center: p,
            size: lievisual::geometry::Size::new(w, h),
            shape: ShapeKind::Rectangle,
            ports: ResolvedPorts {
                top: Point::new(x, y - h / 2.0),
                bottom: Point::new(x, y + h / 2.0),
                left: Point::new(x - w / 2.0, y),
                right: Point::new(x + w / 2.0, y),
            },
            label: None,
            detail: NodeDetail::None,
        }
    }

    fn mk_edge(s: &str, t: &str) -> GGEdge {
        GGEdge {
            id: format!("{}-{}", s, t),
            source: s.to_string(),
            target: t.to_string(),
            route: RoutePath::new(),
            label_text: None,
            label_anchor: None,
            kind: EdgeKind::Flow,
            arrow: ArrowSpec {
                start: ArrowKind::None,
                end: ArrowKind::Arrow,
            },
            routing_hint: crate::builder::ir::common::RoutingHint::Orthogonal,
            line_kind: crate::builder::ir::common::LineKind::Solid,
            cardinality: (None, None),
            cardinality_text: (None, None),
        }
    }

    /// 线段相交判定(含端点接触)。
    fn segs_cross(a: Point, b: Point, c: Point, d: Point) -> bool {
        let cross =
            |o: Point, p: Point, q: Point| (p.x - o.x) * (q.y - o.y) - (p.y - o.y) * (q.x - o.x);
        let d1 = cross(c, d, a);
        let d2 = cross(c, d, b);
        let d3 = cross(a, b, c);
        let d4 = cross(a, b, d);
        ((d1 > 0.0) != (d2 > 0.0)) && ((d3 > 0.0) != (d4 > 0.0))
    }

    /// fan-out:一个源节点扇出到同层横向铺开的多个目标(层间距小、横向跨度大)。
    ///
    /// 回归用例:旧实现按 `max(|dx|,|dy|)` 取控制点弧度,会把控制点甩到目标行之外
    /// 形成 S 形回绕,导致相邻出边互相穿过。要求:
    /// 1. 曲线整体落在两行之间的走廊内(不回绕到源/目标行之外);
    /// 2. 任意两条出边互不相交。
    #[test]
    fn fan_out_spline_edges_stay_in_corridor_and_never_cross() {
        let xs = [-336.0, -168.0, 0.0, 168.0, 336.0];
        let mut nodes = vec![mk_node("A", 0.0, 0.0, 120.0, 60.0)];
        for (i, x) in xs.iter().enumerate() {
            nodes.push(mk_node(&format!("C{i}"), *x, 116.0, 120.0, 60.0));
        }
        let mut edges = Vec::new();
        for (i, _) in xs.iter().enumerate() {
            let mut e = mk_edge("A", &format!("C{i}"));
            e.routing_hint = crate::builder::ir::common::RoutingHint::Spline;
            edges.push(e);
        }
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes,
            edges,
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);

        // A 底边 y = 30,子节点顶边 y = 86 → 走廊 [30, 86]
        for e in &gg.edges {
            for p in sample_path(&e.route, 48) {
                assert!(
                    p.y >= 30.0 - 1e-6 && p.y <= 86.0 + 1e-6,
                    "出边 {}-{} 逸出层间走廊: y={} (期望 30..=86)",
                    e.source,
                    e.target,
                    p.y
                );
            }
        }

        // 两两不相交
        let samples: Vec<Vec<Point>> = gg.edges.iter().map(|e| sample_path(&e.route, 48)).collect();
        for i in 0..samples.len() {
            for j in (i + 1)..samples.len() {
                for k in 0..samples[i].len() - 1 {
                    for l in 0..samples[j].len() - 1 {
                        assert!(
                            !segs_cross(
                                samples[i][k],
                                samples[i][k + 1],
                                samples[j][l],
                                samples[j][l + 1]
                            ),
                            "出边 {} 与 {} 发生交叉: seg1 {:?}->{:?}, seg2 {:?}->{:?}",
                            gg.edges[i].target,
                            gg.edges[j].target,
                            samples[i][k],
                            samples[i][k + 1],
                            samples[j][l],
                            samples[j][l + 1]
                        );
                    }
                }
            }
        }
    }

    /// 端口避让规则(回归):回边 D→B 的目标端默认端口(Bottom)已被 B→C 的
    /// 出边占用(同一端面一进一出),应按优先级序列改走侧面端口。
    /// 注意 B↔C 不能同时互指(那是 mutual 对,走专用双向路由,无端口概念)。
    #[test]
    fn back_edge_enters_target_on_a_free_side() {
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("A", 0.0, 0.0, 60.0, 40.0),
                mk_node("B", 0.0, 150.0, 60.0, 40.0),
                mk_node("C", 0.0, 300.0, 60.0, 40.0),
                mk_node("D", 0.0, 450.0, 60.0, 40.0),
            ],
            edges: vec![
                mk_edge("A", "B"),
                mk_edge("B", "C"),
                mk_edge("C", "D"),
                mk_edge("D", "B"),
            ],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);

        // 回边 D→B:入端不得落在 B 的底边(y = 170,已被 B→C 出边占用),
        // 应落在 B 的左/右侧面(x = ±30)。
        let back = &gg.edges[3];
        let end = back.route.end();
        let on_side = ((end.x - 30.0).abs() < 1.0) || ((end.x + 30.0).abs() < 1.0);
        assert!(on_side, "回边应从 B 的侧面进入, end={end:?}");
        assert!(
            (end.y - 170.0).abs() > 1.0,
            "回边不应与 B→C 的出边共用底部端面: end={end:?}"
        );

        // 普通边行为不变:A→B 仍从 B 的顶部进入。
        let end = gg.edges[0].route.end();
        assert!(
            (end.y - 130.0).abs() < 1.0,
            "A→B 仍应从 B 顶部进入, end={end:?}"
        );
    }

    /// sketch_demo 拓扑:Retry→Input 回边不得与 Input→Decision 的出边共用
    /// Input 的底部端面(同端面一进一出)。
    #[test]
    fn retry_back_edge_avoids_input_bottom() {
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("Input", 0.0, 104.0, 60.0, 40.0),
                mk_node("Decision", 0.0, 208.0, 60.0, 40.0),
                mk_node("Retry", 80.0, 312.0, 60.0, 40.0),
            ],
            edges: vec![
                mk_edge("Input", "Decision"),
                mk_edge("Decision", "Retry"),
                mk_edge("Retry", "Input"),
            ],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);

        let back = &gg.edges[2];
        let end = back.route.end();
        // Input 的底边 y = 124(x≈0)已被 Input→Decision 出边占用;
        // 侧面在 x = ±30。
        let on_side = ((end.x - 30.0).abs() < 1.0) || ((end.x + 30.0).abs() < 1.0);
        assert!(on_side, "回边应从 Input 的侧面进入, end={end:?}");
        let on_bottom = end.y > 123.0 && end.x.abs() < 1.0;
        assert!(!on_bottom, "回边不应从 Input 底部进入: end={end:?}");
    }

    #[test]
    fn orthogonal_route_has_orthogonal_segments_and_avoids_node() {
        // A 在左、B 在右,中间 C 阻挡
        // A(0,0) - B(200,0),C(100,0) 正好在连线上
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("A", 0.0, 0.0, 60.0, 40.0),
                mk_node("B", 200.0, 0.0, 60.0, 40.0),
                mk_node("C", 100.0, 0.0, 60.0, 40.0),
            ],
            edges: vec![mk_edge("A", "B")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);
        let route = &gg.edges[0].route;
        // 正交折线应 >= 3 个段(含 stub + 中间拐点)
        assert!(route.len() >= 3, "正交路由段过少: {:?}", route);

        // 路由不应穿过 C 的包围盒(除非 C 是端点,但 C 不是)
        let c_rect = Rect::new(100.0 - 32.0, 0.0 - 22.0, 100.0 + 32.0, 0.0 + 22.0);
        for seg in route.iter() {
            let (a, b) = (seg.start(), seg.end());
            assert!(
                !segment_intersects_rect(a, b, &c_rect),
                "路由穿越中间阻挡节点 C: {:?} -> {:?}",
                a,
                b
            );
        }
    }

    #[test]
    fn route_preserves_endpoints() {
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("A", 0.0, 0.0, 60.0, 40.0),
                mk_node("B", 0.0, 150.0, 60.0, 40.0),
            ],
            edges: vec![mk_edge("A", "B")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);
        let route = &gg.edges[0].route;
        // 垂直主导:A 出 Bottom 端口(0,20),B 入 Top 端口(0,130)
        let start = route.start();
        assert!((start.x - 0.0).abs() < 1.0, "起点 x 应≈0");
        assert!(
            start.y > 0.0 && start.y < 40.0,
            "起点应在 A 底部出线, got {:?}",
            start
        );
        let end = route.end();
        assert!(
            end.y > 110.0 && end.y < 150.0,
            "终点应在 B 顶部入线, got {:?}",
            end
        );
    }

    /// 自环路由:应生成外凸环(非退化路径),起止点都在节点右边缘。
    #[test]
    fn self_loop_route_bulges_right() {
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![mk_node("B", 0.0, 0.0, 120.0, 60.0)],
            edges: vec![mk_edge("B", "B")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);
        let route = &gg.edges[0].route;
        assert!(!route.is_empty(), "自环路由不应为空");
        let samples = sample_path(route, 32);
        // 环应伸到节点右边缘之外
        let max_x = samples
            .iter()
            .map(|p| p.x)
            .fold(f64::NEG_INFINITY, f64::max);
        assert!(max_x > 60.0 + 10.0, "自环应外凸出右边缘, max_x={max_x}");
        // 起止点应在节点右边缘上
        assert!((route.start().x - 60.0).abs() < 1e-6);
        assert!((route.end().x - 60.0).abs() < 1e-6);
    }

    /// 双向对:两条相对边应一左一右分开(不重合)。
    #[test]
    fn mutual_pair_edges_are_separated() {
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("B", 0.0, 0.0, 120.0, 60.0),
                mk_node("C", 0.0, 140.0, 120.0, 60.0),
            ],
            edges: vec![mk_edge("B", "C"), mk_edge("C", "B")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        gg.edges[0].routing_hint = crate::builder::ir::common::RoutingHint::Spline;
        gg.edges[1].routing_hint = crate::builder::ir::common::RoutingHint::Spline;
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);
        let x0 = gg.edges[0].route.start().x;
        let x1 = gg.edges[1].route.start().x;
        assert!(
            (x0 - x1).abs() > 2.0,
            "双向对两条边应左右分开: {x0} vs {x1}"
        );
        assert!(
            (x0 - 0.0).abs() < 20.0 && (x1 - 0.0).abs() < 20.0,
            "双向对偏移应很小(紧贴风格)"
        );
    }

    /// 菱形端口:端口点应落在菱形边界上(而非包围盒角部悬空)。
    #[test]
    fn diamond_ports_lie_on_shape_boundary() {
        let n = mk_node("D", 0.0, 0.0, 200.0, 120.0);
        let mut n = n;
        n.shape = ShapeKind::Diamond;
        // 底面偏移 66(约 hw/3):边界 y = hh * (1 - 66/100) = 19.8
        let p = port_point_offset(&n, Port::Bottom, 66.0);
        let ratio = (p.x / 100.0).abs() + (p.y / 60.0).abs();
        assert!(
            (ratio - 1.0).abs() < 1e-6,
            "端口应落在菱形边界上, ratio={ratio}"
        );
        // 顶面中点 = 顶点
        let top = port_point_offset(&n, Port::Top, 0.0);
        assert!((top.y - (-60.0)).abs() < 1e-6 && (top.x - 0.0).abs() < 1e-6);
    }

    /// 0.3 容器避障:两端点均非容器成员时,普通边应绕开容器框(不得穿过)。
    #[test]
    fn normal_edge_avoids_container() {
        // 容器框覆盖 A 与 B 之间走廊的中部(x∈[-40,40], y∈[-20,100]),
        // 两侧都留有足够的空白走廊(A 上方 / B 下方),侧边通道可绕行。
        let container = GGContainer {
            id: "sub1".into(),
            bounds: Rect::new(-40.0, -20.0, 40.0, 100.0),
            title: None,
            kind: ContainerKind::Subgraph,
            member_ids: vec![],
        };
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("A", 0.0, -140.0, 60.0, 40.0),
                mk_node("B", 0.0, 200.0, 60.0, 40.0),
            ],
            edges: vec![mk_edge("A", "B")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        gg.edges[0].routing_hint = crate::builder::ir::common::RoutingHint::Spline;
        route_edges(&mut gg, crate::ast::Direction::TB, &[container]);
        let route = &gg.edges[0].route;
        // 朴素 Spline 会沿 x≈0 直线穿过容器;避障后任何采样点不得落入容器框内部。
        for p in sample_path(route, 64) {
            assert!(
                !(p.x > -40.0 && p.x < 40.0 && p.y > -20.0 && p.y < 100.0),
                "边 A->B 穿过容器框: {p:?}"
            );
        }
    }

    /// 0.3 容器避障:容器内边(两端点均为成员)允许保持原路由,不绕行。
    #[test]
    fn intra_container_edge_stays_inside() {
        let container = GGContainer {
            id: "sub1".into(),
            bounds: Rect::new(-80.0, -30.0, 80.0, 130.0),
            title: None,
            kind: ContainerKind::Subgraph,
            member_ids: vec!["A".into(), "B".into()],
        };
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("A", 0.0, 0.0, 60.0, 40.0),
                mk_node("B", 0.0, 80.0, 60.0, 40.0),
            ],
            edges: vec![mk_edge("A", "B")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        gg.edges[0].routing_hint = crate::builder::ir::common::RoutingHint::Spline;
        route_edges(&mut gg, crate::ast::Direction::TB, &[container]);
        // 朴素 Spline 不穿过任何障碍 → 应保持朴素路由(单段贝塞尔),不生成绕行折线。
        assert_eq!(gg.edges[0].route.len(), 1, "容器内边不应被绕行");
    }

    /// 0.6 正交边-边避让:同层间的并行正交边主干不得重叠(同 x 且 y 跨度交叠)。
    #[test]
    fn orthogonal_edges_do_not_share_trunk() {
        // 交叉对:A1→B1 与 A2→B2 的主干中线都为 x=0 且跨度重叠,
        // 若无边间避让,两条边会画在同一条垂直主干上。
        let mut gg = Geograph {
            size: lievisual::geometry::Size::new(0.0, 0.0),
            background: lievisual::geometry::Color::default(),
            nodes: vec![
                mk_node("A1", -30.0, 0.0, 60.0, 40.0),
                mk_node("A2", 30.0, 0.0, 60.0, 40.0),
                mk_node("B1", 30.0, 200.0, 60.0, 40.0),
                mk_node("B2", -30.0, 200.0, 60.0, 40.0),
            ],
            edges: vec![mk_edge("A1", "B1"), mk_edge("A2", "B2")],
            containers: vec![],
            title: None,
            show_data: false,
            activations: vec![],
            sequence_dividers: vec![],
        };
        route_edges(&mut gg, crate::ast::Direction::TB, &[]);
        // 收集所有长垂直主干段(stub 长 18 < 40,只捕获主干)。
        let mut trunk_x: Vec<f64> = Vec::new();
        for e in &gg.edges {
            for seg in e.route.iter() {
                if let RouteSegment::Line { from, to } = seg
                    && (from.x - to.x).abs() < 1e-6
                    && (from.y - to.y).abs() > 40.0
                {
                    trunk_x.push(from.x);
                }
            }
        }
        assert!(trunk_x.len() >= 2, "应有至少两条正交主干: {trunk_x:?}");
        for i in 0..trunk_x.len() {
            for j in (i + 1)..trunk_x.len() {
                assert!(
                    (trunk_x[i] - trunk_x[j]).abs() > 2.0,
                    "两条正交主干重叠: {trunk_x:?}"
                );
            }
        }
    }
}

#[cfg(test)]
mod grid_tests {
    use super::*;

    fn rects(items: &[(f64, f64, f64, f64)]) -> Vec<Rect> {
        items
            .iter()
            .map(|&(x0, y0, x1, y1)| Rect::new(x0, y0, x1, y1))
            .collect()
    }

    #[test]
    fn grid_query_returns_only_overlapping_rects() {
        let r = rects(&[
            (0.0, 0.0, 100.0, 100.0),
            (500.0, 500.0, 600.0, 600.0),
            (1000.0, 0.0, 1100.0, 50.0),
        ]);
        let grid = RectGrid::new(&r);
        let mut hits: Vec<usize> = Vec::new();
        grid.for_each(Rect::new(90.0, 90.0, 110.0, 110.0), |i| hits.push(i));
        assert!(hits.contains(&0), "查询区域应与 0 号矩形相交: {hits:?}");
        assert!(!hits.contains(&1), "远端矩形不应被返回: {hits:?}");
        assert!(!hits.contains(&2), "远端矩形不应被返回: {hits:?}");
    }

    #[test]
    fn grid_query_handles_out_of_range_and_empty() {
        let r = rects(&[(0.0, 0.0, 10.0, 10.0)]);
        let grid = RectGrid::new(&r);
        // 网格外查询会被 clamp 到边缘格,可能返回候选,但精确判定必须过滤掉。
        let query = Rect::new(-1e6, -1e6, -9e5, -9e5);
        let mut exact = 0usize;
        grid.for_each(query, |i| {
            if rects_overlap(r[i], query) {
                exact += 1;
            }
        });
        assert_eq!(exact, 0, "网格外查询不得产生精确命中");

        let empty = RectGrid::new(&[]);
        let mut hits = 0usize;
        empty.for_each(Rect::new(-1.0, -1.0, 1.0, 1.0), |_| hits += 1);
        assert_eq!(hits, 0, "空网格不应命中");
    }

    /// 网格查询是粗筛:精确判定仍由 `segment_intersects_rect` 完成,
    /// 因此跨格矩形可能重复返回(布尔 / min / max 语义下无影响)。
    #[test]
    fn grid_may_return_duplicates_but_never_misses() {
        let r = rects(&[(-50.0, -50.0, 50.0, 50.0), (400.0, 400.0, 500.0, 500.0)]);
        let grid = RectGrid::new(&r);
        let mut hits: Vec<usize> = Vec::new();
        grid.for_each(Rect::new(-60.0, -60.0, 60.0, 60.0), |i| hits.push(i));
        assert!(hits.contains(&0));
        assert!(!hits.contains(&1));
        assert!(!hits.is_empty(), "查询区域至少应命中自身所在格: {hits:?}");
    }
}