summaryrefslogtreecommitdiff
path: root/node.c
blob: 8662e38c3da8c658e27a67e458acf894c4b73ada (plain)
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
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
/*--------------------------------------------------------------*/
/* node.c -- Generation	of detailed network and obstruction	*/
/* information on the routing grid based on the geometry of the	*/
/* layout of the standard cell macros.				*/
/*								*/
/*--------------------------------------------------------------*/
/* Written by Tim Edwards, June, 2011, based on work by Steve	*/
/* Beccue.							*/
/*--------------------------------------------------------------*/

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "qrouter.h"
#include "node.h"
#include "qconfig.h"
#include "lef.h"
#include "def.h"
#include "output.h"

/*--------------------------------------------------------------*/
/* SetNodeinfo --						*/
/*	Allocate a NODEINFO record and put it in the Nodeinfo	*/
/*	array at position (gridx, gridy, d->layer).  Return the	*/
/* 	pointer to the location.				*/
/*--------------------------------------------------------------*/

NODEINFO
SetNodeinfo(int gridx, int gridy, int layer, NODE node)
{
    DPOINT dp;
    NODEINFO *lnodeptr;

    lnodeptr = &NODEIPTR(gridx, gridy, layer);
    if (*lnodeptr == NULL) {
	*lnodeptr = (NODEINFO)calloc(1, sizeof(struct nodeinfo_));

	/* Make sure this position is in the list of node's taps.  Add	*/
	/* it if it is not there.					*/

	for (dp = (DPOINT)node->taps; dp; dp = dp->next)
	    if (dp->gridx == gridx && dp->gridy == gridy && dp->layer == layer)
		break;
	if (dp == NULL)
	    for (dp = (DPOINT)node->extend; dp; dp = dp->next)
		if (dp->gridx == gridx && dp->gridy == gridy && dp->layer == layer)
		    break;
	if (dp == NULL) {
	    dp = (DPOINT)malloc(sizeof(struct dpoint_));
	    dp->gridx = gridx;
	    dp->gridy = gridy;
	    dp->layer = layer;
	    dp->x = (gridx * PitchX) + Xlowerbound;
	    dp->y = (gridy * PitchY) + Ylowerbound;
	    dp->next = node->extend;
	    node->extend = dp;
	}
    }
    return *lnodeptr;
}

/*--------------------------------------------------------------*/
/* FreeNodeinfo --						*/
/*	Free a NODEINFO record at array Nodeinfo position	*/
/*	(gridx, gridy, d->layer).  Set the position pointer to	*/
/*	NULL.  							*/
/*--------------------------------------------------------------*/

void
FreeNodeinfo(int gridx, int gridy, int layer)
{
    NODEINFO *lnodeptr;
    lnodeptr = &NODEIPTR(gridx, gridy, layer);

    if (*lnodeptr != NULL) {
        free(*lnodeptr);
	*lnodeptr = NULL;
    }
}

/*--------------------------------------------------------------*/
/* count_reachable_taps()					*/
/*								*/
/*  For each grid point in the layout, find if it corresponds	*/
/*  to a node and is unobstructed.  If so, increment the node's	*/
/*  count of reachable taps.  Then work through the list of	*/
/*  nodes and determine if any are completely unreachable.  If	*/
/*  so, then unobstruct any position that is inside tap		*/
/*  geometry that can contain a via.				*/
/*								*/
/*  NOTE:  This routine should check for tap rectangles that	*/
/*  may combine to form an area large enough to place a via;	*/
/*  also, it should check for tap points that are routable	*/
/*  by a wire and not a tap.  However, those conditions are	*/
/*  rare and are left unhandled for now.			*/
/*								*/
/*  If "unblock_all" is 1, then unblock all grid points that	*/
/*  are cleanly routable by being completely inside a pin with	*/
/*  available margins for placing a via, regardless of whether	*/
/*  or not the pin has other available tap points.  This should	*/
/*  only be used if obstructions are drawn in the style where	*/
/*  they can abut pins (e.g., part of the pin has been marked	*/
/*  as an obstruction).						*/
/*--------------------------------------------------------------*/

void
count_reachable_taps(u_char unblock_all)
{
    NODE node;
    NODEINFO lnode;
    GATE g;
    DSEG ds;
    int l, i, j, orient;
    int gridx, gridy;
    double deltax, deltay;
    double dx, dy;

    for (l = 0; l < Num_layers; l++) {
	for (j = 0; j < NumChannelsX * NumChannelsY; j++) {
	    if (Nodeinfo[l][j]) {
		node = Nodeinfo[l][j]->nodeloc;
		if (node != NULL) {

		    // Redundant check;  if Obs has NO_NET set, then
		    // Nodeinfo->nodeloc for that position should already
		    // be NULL

		    if (!(Obs[l][j] & NO_NET))
			node->numtaps++;
		}
	    }
	}
    }

    for (g = Nlgates; g; g = g->next) {
	for (i = 0; i < g->nodes; i++) {
	    node = g->noderec[i];
	    if (node == NULL) continue;
	    if (node->numnodes == 0) continue;	 // e.g., vdd or gnd bus
	    if ((node->numtaps == 0) || (unblock_all == TRUE)) {

		/* Will try more than one via if available */
		for (orient = 0; orient < 4; orient += 2) {
		    for (ds = g->taps[i]; ds; ds = ds->next) {
			deltax = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 0, orient);
			deltay = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 1, orient);

			gridx = (int)((ds->x1 - Xlowerbound) / PitchX) - 1;
			if (gridx < 0) gridx = 0;
			while (1) {
			    dx = (gridx * PitchX) + Xlowerbound;
			    if (dx > ds->x2 || gridx >= NumChannelsX) break;

			    if (((dx - ds->x1 + EPS) > deltax) &&
					((ds->x2 - dx + EPS) > deltax)) {
				gridy = (int)((ds->y1 - Ylowerbound)
					/ PitchY) - 1;
				if (gridy < 0) gridy = 0;
				while (1) {
				    dy = (gridy * PitchY) + Ylowerbound;
				    if (dy > ds->y2 || gridy >= NumChannelsY)
					break;

				    if (((dy - ds->y1 + EPS) > deltay) &&
						((ds->y2 - dy + EPS) > deltay)) {

					if ((ds->layer == Num_layers - 1) ||
						!(OBSVAL(gridx, gridy, ds->layer + 1)
						& NO_NET)) {

					    // Grid position is clear for placing a via

					    if ((orient == 0) && (Verbose > 1))
						Fprintf(stdout, "Tap position (%g, %g)"
						    " appears to be technically routable"
						    " so it is being forced routable.\n",
						    dx, dy);
					    else if (Verbose > 1)
						Fprintf(stdout, "Tap position (%g, %g)"
						    " appears to be technically routable"
						    " with alternate via, so it is being"
						    " forced routable.\n", dx, dy);

					    OBSVAL(gridx, gridy, ds->layer) =
						(OBSVAL(gridx, gridy, ds->layer)
						& BLOCKED_MASK)
						| (u_int)node->netnum;
					    lnode = SetNodeinfo(gridx, gridy, ds->layer,
							node);
					    lnode->nodeloc = node;
					    lnode->nodesav = node;

					    /* If we got to orient = 2, mark NI_NO_VIAX */
					    if (orient == 2) lnode->flags |= NI_NO_VIAX;
					    /* This is a bit harsh, but should work */
					    else lnode->flags |= NI_NO_VIAY;
					    node->numtaps++;
					}
				    }
				    gridy++;
				}
			    }
			    gridx++;
			}
		    }
		    /* If there's a solution, don't go looking at other vias */
		    if (node->numtaps > 0) break;
		}
	    }
	    if (node->numtaps == 0) {
		/* Node wasn't cleanly within tap geometry when centered */
		/* on a grid point.  But if the via can be offset and is */
		/* cleanly within the tap geometry, then allow it.	 */

		double dist, mindist;
		int dir, mask, tapx, tapy, tapl;

		/* Will try more than one via if available */
		for (orient = 0; orient < 4; orient += 2) {

		    /* Initialize mindist to a large value */
		    mask = 0;
		    mindist = PitchX + PitchY;
		    dir = 0;	/* Indicates no solution found */

		    for (ds = g->taps[i]; ds; ds = ds->next) {
			deltax = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 0, orient);
			deltay = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 1, orient);

			gridx = (int)((ds->x1 - Xlowerbound) / PitchX) - 1;
			if (gridx < 0) gridx = 0;
			while (1) {
			    dx = (gridx * PitchX) + Xlowerbound;
			    if (dx > ds->x2 || gridx >= NumChannelsX) break;

			    if (((dx - ds->x1 + EPS) > -deltax) &&
					((ds->x2 - dx + EPS) > -deltax)) {
				gridy = (int)((ds->y1 - Ylowerbound) / PitchY) - 1;
				if (gridy < 0) gridy = 0;

				while (1) {
				    dy = (gridy * PitchY) + Ylowerbound;
				    if (dy > ds->y2 || gridy >= NumChannelsY)
					break;

				    // Check that the grid position is inside the
				    // tap rectangle.

				    // NOTE: If the point above the grid is blocked,
				    // then a via cannot be placed here, so skip it.
				    // This currently looks only for completely
				    // obstructed positions.  To do:  For directionally
				    // obstructed positions, see if the obstruction
				    // is in the opposite direction of the via's
				    // offset and at least the same distance.
				    // Otherwise, it won't clear.

				    if (((ds->layer == Num_layers - 1) ||
						!(OBSVAL(gridx, gridy, ds->layer + 1)
						& (NO_NET || OBSTRUCT_MASK))) &&
						((dy - ds->y1 + EPS) > -deltay) &&
						((ds->y2 - dy + EPS) > -deltay)) {

					// Grid point is inside tap geometry.
					// Since it did not pass the simple insideness
					// test previously, it can be assumed that
					// one of the edges is closer to the grid point
					// than 1/2 via width.  Find that edge and use
					// it to determine the offset.

					// Check right edge
					if ((ds->x2 - dx + EPS) < deltax) {
					    dist = deltax - ds->x2 + dx;
					    // Confirm other edges
					    if ((dx - dist - deltax + EPS > ds->x1) &&
							(dy - deltay + EPS > ds->y1) &&
							(dy + deltay - EPS < ds->y2)) {
						if (dist < fabs(mindist)) {
						    mindist = dist;
						    mask = STUBROUTE;
						    dir = NI_STUB_EW;
						    tapx = gridx;
						    tapy = gridy;
						    tapl = ds->layer;
						}
					    }
					}
					// Check left edge
					if ((dx - ds->x1 + EPS) < deltax) {
					    dist = deltax - dx + ds->x1;
					    // Confirm other edges
					    if ((dx + dist + deltax - EPS < ds->x2) &&
							(dy - deltay + EPS > ds->y1) &&
							(dy + deltay - EPS < ds->y2)) {
						if (dist < fabs(mindist)) {
						    mindist = -dist;
						    mask = STUBROUTE;
						    dir = NI_STUB_EW;
						    tapx = gridx;
						    tapy = gridy;
						    tapl = ds->layer;
						}
					    }
					}
					// Check top edge
					if ((ds->y2 - dy + EPS) < deltay) {
					    dist = deltay - ds->y2 + dy;
					    // Confirm other edges
					    if ((dx - deltax + EPS > ds->x1) &&
						    (dx + deltax - EPS < ds->x2) &&
						    (dy - dist - deltay + EPS > ds->y1)) {
						if (dist < fabs(mindist)) {
						    mindist = -dist;
						    mask = STUBROUTE;
						    dir = NI_STUB_NS;
						    tapx = gridx;
						    tapy = gridy;
						    tapl = ds->layer;
						}
					    }
					}
					// Check bottom edge
					if ((dy - ds->y1 + EPS) < deltay) {
					    dist = deltay - dy + ds->y1;
					    // Confirm other edges
					    if ((dx - deltax + EPS > ds->x1) &&
						    (dx + deltax - EPS < ds->x2) &&
						    (dy + dist + deltay - EPS < ds->y2)) {
						if (dist < fabs(mindist)) {
					 	    mindist = dist;
						    mask = STUBROUTE;
						    dir = NI_STUB_NS;
						    tapx = gridx;
						    tapy = gridy;
						    tapl = ds->layer;
						}
					    }
					}
				    }
				    gridy++;
				}
			    }
			    gridx++;
			}
		    }

		    /* Was a solution found? */
		    if (mask != 0) {
			// Grid position is clear for placing a via

			if (Verbose > 1)
			    Fprintf(stdout, "Tap position (%d, %d) appears to be"
					" technically routable with an offset, so"
					" it is being forced routable.\n",
					tapx, tapy);

			OBSVAL(tapx, tapy, tapl) =
				(OBSVAL(tapx, tapy, tapl) & BLOCKED_MASK)
				| mask | (u_int)node->netnum;
			lnode = SetNodeinfo(tapx, tapy, tapl, node);
			lnode->nodeloc = node;
			lnode->nodesav = node;
			lnode->stub = dist;
			lnode->flags |= dir;

			/* If we got to orient = 2 then mark NI_NO_VIAX */
			if (orient == 2) lnode->flags |= NI_NO_VIAX;

			node->numtaps++;
		    }

		    /* If there's a solution, don't go looking at other vias */
		    if (node->numtaps > 0) break;
		}
	    }
	}
    }

    /* Last pass to output error messages for any taps that were not	*/
    /* handled by the code above.					*/

    for (g = Nlgates; g; g = g->next) {
	for (i = 0; i < g->nodes; i++) {
	    node = g->noderec[i];
	    if (node == NULL) continue;
	    if (node->numnodes == 0) continue;	 // e.g., vdd or gnd bus
	    if (node->numtaps == 0) {
		Fprintf(stderr, "Error: Node %s of net \"%s\" has no taps!\n",
			print_node_name(node), node->netname);
		Fprintf(stderr, "Qrouter will not be able to completely"
			" route this net.\n");
		if (Verbose > 1) {
		    int found_inside, found_inrange;
		    Fprintf(stderr, "Tap position blockage analysis:\n");

		    /* Unreachable taps are the most common problem with    */
		    /* new processes or buggy code, so make a detailed	    */
		    /* report of blockages affecting routing for debugging. */

		    found_inside = found_inrange = 0;
		    for (ds = g->taps[i]; ds; ds = ds->next) {
			unsigned char is_inside, is_inrange;

			deltax = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 0, 0);
			deltay = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 1, 0);

			Fprintf(stderr, "Tap geometry (%g %g) to (%g %g):\n",
				ds->x1, ds->y1, ds->x2, ds->y2);

			gridx = (int)(((ds->x1 - 1) - Xlowerbound) / PitchX) - 1;
			if (gridx < 0) gridx = 0;
			while (1) {
			    dx = (gridx * PitchX) + Xlowerbound;
			    if (dx > (ds->x2 + 1) || gridx >= NumChannelsX) break;
			    gridy = (int)(((ds->y1 - 1) - Ylowerbound) / PitchY) - 1;
			    if (gridy < 0) gridy = 0;
			    while (1) {
				dy = (gridy * PitchY) + Ylowerbound;
				if (dy > (ds->y2 + 1) || gridy >= NumChannelsY)
				    break;

				is_inside = (dx >= ds->x1 && dx <= ds->x2 &&
					     dy >= ds->y1 && dy <= ds->y2) ? 1 : 0;

				is_inrange = (dx > ds->x1 - deltax &&
					     dx < ds->x2 + deltax &&
					     dy > ds->y1 - deltay &&
					     dy < ds->y2 + deltay) ? 1 : 0;

			        if (is_inrange) {
				    Fprintf(stderr, "Grid position (%d %d) at (%g %g) "
					    "layer %d is %s tap geometry.\n",
					    gridx, gridy, dx, dy, ds->layer,
					    (is_inside == 1) ? "inside" : "outside");
				    print_grid_information(gridx, gridy, ds->layer);
				    found_inrange++;
				    if (is_inside) found_inside++;
				}
				gridy++;
			    }
			    gridx++;
			}
		    }
		    if (found_inrange == 0) Fprintf(stderr, "No positions analyzed.\n");
		    Fprintf(stderr, "%d grid position%s found "
			    "inside tap geometry\n", found_inside,
			    ((found_inside == 1) ? " was" : "s were"));
		    Fprintf(stderr, "%d grid position%s found "
			    "nearby tap geometry\n", found_inrange,
			    ((found_inrange == 1) ? " was" : "s were"));
		}
	    }
	}
    }
}

/*--------------------------------------------------------------*/
/* check_variable_pitch()					*/
/*								*/
/*  This routine is used by the routine below it to generate	*/
/*  obstructions that force routes to be placed in 1-of-N	*/
/*  tracks.  However, it is also used to determine the same	*/
/*  information for the .info file, so that the effective	*/
/*  pitch is output, not the pitch copied from the LEF file.	*/
/*  Output is the vertical and horizontal pitch multipliers,	*/
/*  passed back through pointers.				*/
/*--------------------------------------------------------------*/

void check_variable_pitch(int l, int *hptr, int *vptr)
{
   int o, hnum, vnum;
   double vpitch, hpitch, wvia, wviax, wviay;

   o = LefGetRouteOrientation(l);

   // Note that when "horizontal" (o = 1) is passed to LefGetXYViaWidth,
   // it returns the via width top-to-bottom (orient meaning is
   // reversed for LefGetXYViaWidth), which is what we want. . .

   // Try both via orientations and choose the best, assuming that it is
   // a lot easier to rotate and shift vias around than it is to lose
   // half the routing grid.

   if (l == 0) {
	 wviax = LefGetXYViaWidth(l, l, o, 0);
	 wviay = LefGetXYViaWidth(l, l, o, 3);
   }
   else {
	 wviax = LefGetXYViaWidth(l - 1, l, o, 0);
	 wviay = LefGetXYViaWidth(l - 1, l, o, 3);
   }
   wvia = (wviax < wviay) ? wviax : wviay;

   if (o == 1) {	// Horizontal route
      vpitch = LefGetRoutePitch(l);
      // Changed:  routes must be able to accomodate the placement
      // of a via in the track next to it.

      // hpitch = LefGetRouteWidth(l) + LefGetRouteSpacing(l);
      hpitch = 0.5 * (LefGetRouteWidth(l) + wvia) + LefGetRouteSpacing(l);
   }
   else {		// Vertical route
      hpitch = LefGetRoutePitch(l);
      // vpitch = LefGetRouteWidth(l) + LefGetRouteSpacing(l);
      vpitch = 0.5 * (LefGetRouteWidth(l) + wvia) + LefGetRouteSpacing(l);
   }

   vnum = (int)((vpitch / PitchY) - EPS) + 1;
   hnum = (int)((hpitch / PitchX) - EPS) + 1;

   // To mark blockages, either none of (hnum & vnum) should be
   // larger than 1, or both of them should be.  Further info in
   // create_obstructions_from_variable_pitch(), below.

   if (vnum > 1 && hnum == 1) hnum++;
   if (hnum > 1 && vnum == 1) vnum++;

   *vptr = vnum;
   *hptr = hnum;
}

/*--------------------------------------------------------------*/
/* create_obstructions_from_variable_pitch()			*/
/*								*/
/*  Although it would be nice to have an algorithm that would	*/
/*  work with any arbitrary pitch, qrouter will work around	*/
/*  having larger pitches on upper metal layers by selecting	*/
/*  1 out of every N tracks for routing, and placing 		*/
/*  obstructions in the interstices.  This makes the possibly	*/
/*  unwarranted assumption that the contact down to the layer	*/
/*  below does not cause spacing violations to neighboring	*/
/*  tracks.  If that assumption fails, this routine will have	*/
/*  to be revisited.						*/
/*--------------------------------------------------------------*/

void create_obstructions_from_variable_pitch(void)
{
   int l, vnum, hnum, hoff, voff, x, y;
   NODEINFO lnode;
   TRACKS tracksinfo, tracksinfoother;

   for (l = 0; l < Num_layers; l++) {

      // check_variable_pitch() guarantees that either hnum
      // and vnum are both one, or both are larger than 1.

      check_variable_pitch(l, &hnum, &vnum);

      if (hnum == 1 && vnum == 1) continue;	// No obstructions needed

      // Compute the offset of the tracks to mark obstructions
      tracksinfo = DefGetTracks(l);
      if (tracksinfo == NULL) {
	 // Should look at standard cell placement and LEF offset
	 // to determine offset here.  For now, just use 0.
	 hoff = voff = 0;
      }
      else {
	 // Use the start position relative to lowerbound to determine
	 // the offset.  use the offset of the higher or lower layer
	 // for determining the offset in the opposite direction of
	 // the current layer orientation.
	 if (l < Num_layers - 1) {
	    // If not the top layer, then use the upper layer as the other layer
	    tracksinfoother = DefGetTracks(l + 1);
	 }
	 else if (l > 0) {
	    // Otherwise, use the lower layer
	    tracksinfoother = DefGetTracks(l - 1);
         }
	 else {
	    // Should not happen, as it means routing is done with one layer. . .
	    tracksinfoother = (TRACKS)NULL;
	 }

	 if (Vert[l]) {
	    hoff = (int)((tracksinfo->start - Xlowerbound) / PitchX + 0.5);
	    voff = (tracksinfoother == (TRACKS)NULL) ?
		    0 : (int)((tracksinfoother->start - Ylowerbound) / PitchY + 0.5);
	 }
	 else {
	    voff = (int)((tracksinfo->start - Ylowerbound) / PitchY + 0.5);
	    hoff = (tracksinfoother == (TRACKS)NULL) ?
		    0 : (int)((tracksinfoother->start - Xlowerbound) / PitchX + 0.5);
	 }
      }

      // This could be better handled by restricting
      // access from specific directions rather than
      // marking a position as NO_NET.  Since the
      // routine below will mark no positions restricted
      // if either hnum is 1 or vnum is 1, regardless of
      // the other value, then we force both values to
      // be at least 2.

      if (vnum > 1 || hnum > 1) {
	 for (x = 0; x < NumChannelsX; x++) {
	    if ((x - hoff) % hnum == 0) continue;
	    for (y = 0; y < NumChannelsY; y++) {
	       if ((y - voff) % vnum == 0) continue;

	       // If the grid position itself is a node, don't restrict
	       // routing based on variable pitch.
	       if (((lnode = NODEIPTR(x, y, l)) != NULL) && (lnode->nodeloc != NULL))
		  continue;

	       // If there is a node in an adjacent grid then allow
	       // routing from that direction.

	       if ((x > 0) && ((lnode = NODEIPTR(x - 1, y, l)) != NULL) &&
			(lnode->nodeloc != NULL))
		  OBSVAL(x, y, l) = BLOCKED_MASK & ~BLOCKED_W;
	       else if ((y > 0) && ((lnode = NODEIPTR(x , y - 1, l)) != NULL) &&
			(lnode->nodeloc != NULL))
		  OBSVAL(x, y, l) = BLOCKED_MASK & ~BLOCKED_S;
	       else if ((x < NumChannelsX - 1)
			&& ((lnode = NODEIPTR(x + 1, y, l)) != NULL) &&
			(lnode->nodeloc != NULL))
		  OBSVAL(x, y, l) = BLOCKED_MASK & ~BLOCKED_E;
	       else if ((y < NumChannelsY - 1)
			&& ((lnode = NODEIPTR(x, y + 1, l)) != NULL) &&
			(lnode->nodeloc != NULL))
		  OBSVAL(x, y, l) = BLOCKED_MASK & ~BLOCKED_N;
	       else
		  OBSVAL(x, y, l) = NO_NET;
	    }
	 }
      }
   }
}

/*--------------------------------------------------------------*/
/* disable_gridpos() ---					*/
/*	Render the position at (x, y, lay) unroutable by	*/
/*	setting its Obs[] entry to NO_NET and removing it from	*/
/*	the Nodeinfo->nodeloc and Nodeinfo->nodesav records.	*/
/*--------------------------------------------------------------*/

static void
disable_gridpos(int x, int y, int lay)
{
    int apos = OGRID(x, y);

    Obs[lay][apos] = (u_int)(NO_NET | OBSTRUCT_MASK);
    if (Nodeinfo[lay][apos]) {
	free(Nodeinfo[lay][apos]);
	Nodeinfo[lay][apos] = NULL;
    }
}

/*--------------------------------------------------------------*/
/* count_pinlayers()---						*/
/*	Check which layers have non-NULL Nodeinfo entries.  	*/
/*	Then set "Pinlayers" and free all the unused layers.	*/
/* 	This saves a lot of memory, especially when the number	*/
/*	of routing layers becomes large.			*/ 
/*--------------------------------------------------------------*/

void
count_pinlayers(void)
{
   int j, l;

   Pinlayers = 0;
   for (l = 0; l < Num_layers; l++) {
      for (j = 0; j < NumChannelsX * NumChannelsY; j++) {
	 if (Nodeinfo[l][j]) {
	    Pinlayers = l + 1;
	    break;
	 }
      }
   }

   for (l = Pinlayers; l < Num_layers; l++) {
      free(Nodeinfo[l]);
      Nodeinfo[l] = NULL;
   }
}

/*--------------------------------------------------------------*/
/* check_obstruct()---						*/
/*	Called from create_obstructions_from_gates(), this	*/
/* 	routine takes a grid point at (gridx, gridy) (physical	*/
/* 	position (dx, dy)) and an obstruction defined by the	*/
/*	rectangle "ds", and sets flags and fills the Obsinfo	*/
/*	array to reflect how the obstruction affects routing to	*/
/*	the grid position.					*/
/*--------------------------------------------------------------*/

static void
check_obstruct(int gridx, int gridy, DSEG ds, double dx, double dy, double delta)
{
    u_int *obsptr;
    float dist;

    obsptr = &(OBSVAL(gridx, gridy, ds->layer));
    dist = OBSINFO(gridx, gridy, ds->layer);

    // Grid point is inside obstruction + halo.
    *obsptr |= NO_NET;

    // Completely inside obstruction?
    if (dy > ds->y1 && dy < ds->y2 && dx > ds->x1 && dx < ds->x2)
       *obsptr |= OBSTRUCT_MASK;

    else {
       // Make more detailed checks in each direction

       if (dy <= ds->y1) {
	  if ((*obsptr & (OBSTRUCT_MASK & ~OBSTRUCT_N)) == 0) {
	     if ((dist == 0) || ((ds->y1 - dy) < dist))
		OBSINFO(gridx, gridy, ds->layer) = ds->y1 - dy;
	     *obsptr |= OBSTRUCT_N;
	  }
	  else *obsptr |= OBSTRUCT_MASK;
       }
       else if (dy >= ds->y2) {
	  if ((*obsptr & (OBSTRUCT_MASK & ~OBSTRUCT_S)) == 0) {
	     if ((dist == 0) || ((dy - ds->y2) < dist))
		OBSINFO(gridx, gridy, ds->layer) = dy - ds->y2;
	     *obsptr |= OBSTRUCT_S;
	  }
	  else *obsptr |= OBSTRUCT_MASK;
       }

       if (dx <= ds->x1) {
	  if ((*obsptr & (OBSTRUCT_MASK & ~OBSTRUCT_E)) == 0) {
	     if ((dist == 0) || ((ds->x1 - dx) < dist))
		OBSINFO(gridx, gridy, ds->layer) = ds->x1 - dx;
             *obsptr |= OBSTRUCT_E;
	  }
	  else *obsptr |= OBSTRUCT_MASK;
       }
       else if (dx >= ds->x2) {
	  if ((*obsptr & (OBSTRUCT_MASK & ~OBSTRUCT_W)) == 0) {
	     if ((dist == 0) || ((dx - ds->x2) < dist))
		OBSINFO(gridx, gridy, ds->layer) = dx - ds->x2;
	     *obsptr |= OBSTRUCT_W;
	  }
	  else *obsptr |= OBSTRUCT_MASK;
       }
    }
}

/*--------------------------------------------------------------*/
/* Find the amount of clearance needed between an obstruction	*/
/* and a route track position.  This takes into consideration	*/
/* whether the obstruction is wide or narrow metal, if the	*/
/* spacing rules are graded according to metal width, and if a	*/
/* via placed at the position is or is not symmetric in X and Y	*/
/* "orient" is the via orientation, of which there are four;	*/
/* however, as this only concerns the via bottom layer, the	*/
/* two orientations are represented by orient = 0 and 2.	*/
/*--------------------------------------------------------------*/

static double get_via_clear(int lay, int horiz, int orient, DSEG rect) {
   double vdelta, v2delta, mdelta, mwidth;

   vdelta = LefGetXYViaWidth(lay, lay, 1 - horiz, orient);
   if (lay > 0) {
	v2delta = LefGetXYViaWidth(lay - 1, lay, 1 - horiz, orient);
	if (v2delta > vdelta) vdelta = v2delta;
   }
   vdelta = vdelta / 2.0;

   // Spacing rule is determined by the minimum metal width,
   // either in X or Y, regardless of the position of the
   // metal being checked.

   mwidth = MIN(rect->x2 - rect->x1, rect->y2 - rect->y1);
   mdelta = LefGetRouteWideSpacing(lay, mwidth);

   return vdelta + mdelta;
}

/*--------------------------------------------------------------*/
/* Find the distance from an obstruction to a grid point, 	*/
/* considering only routes which are placed at the position,	*/
/* not vias.							*/
/*--------------------------------------------------------------*/

static double get_route_clear(int lay, DSEG rect) {
   double rdelta, mdelta, mwidth;

   rdelta = LefGetRouteWidth(lay);
   rdelta = rdelta / 2.0;

   // Spacing rule is determined by the minimum metal width,
   // either in X or Y, regardless of the position of the
   // metal being checked.

   mwidth = MIN(rect->x2 - rect->x1, rect->y2 - rect->y1);
   mdelta = LefGetRouteWideSpacing(lay, mwidth);

   return rdelta + mdelta;
}

/*--------------------------------------------------------------*/
/* Truncate gates to the set of tracks.  Warn about any gates	*/
/* with nodes that are clipped entirely outside the routing	*/
/* area.							*/
/*--------------------------------------------------------------*/

void clip_gate_taps(void)
{
    NET net; 
    NODE node;
    DPOINT dp, dpl;
    int i, lay;

    for (i = 0; i < Numnets; i++) {
	net = Nlnets[i];
	for (node = net->netnodes; node; node = node->next) {
	    dpl = NULL;
	    for (dp = (DPOINT)node->taps; dp; ) {

		lay = dp->layer;

		if (dp->gridx < 0 || dp->gridy < 0 ||
			dp->gridx >= NumChannelsX ||
			dp->gridy >= NumChannelsY) {
		    Fprintf(stderr, "Tap of port of node %d of net %s"
			" is outside of route area\n",
			node->nodenum, node->netname);

		    if (dpl == NULL)
			node->taps = dp->next;
		    else
			dpl->next = dp->next;

		    free(dp);
		    dp = (dpl == NULL) ? node->taps : dpl->next;
		}
		else {
		    dpl = dp;
		    dp = dp->next;
		}
	    }
	}
    }
}

/*--------------------------------------------------------------*/
/* create_obstructions_from_gates()				*/
/*								*/
/*  Fills in the Obs[][] grid from obstructions that were	*/
/*  defined for each macro in the technology LEF file and	*/
/*  translated into a list of grid coordinates in each		*/
/*  instance.							*/
/*								*/
/*  Also, fills in the Obs[][] grid with obstructions that	*/
/*  are defined by nodes of the gate that are unconnected in	*/
/*  this netlist.						*/
/*--------------------------------------------------------------*/

void create_obstructions_from_gates(void)
{
    GATE g;
    DSEG ds;
    int i, gridx, gridy, orient;
    double deltax, deltay, delta[MAX_LAYERS];
    double dx, dy, deltaxy;

    // Give a single net number to all obstructions, over the range of the
    // number of known nets, so these positions cannot be routed through.
    // If a grid position is not wholly inside an obstruction, then we
    // maintain the direction of the nearest obstruction in Obs and the
    // distance to it in Obsinfo.  This indicates that a route can avoid
    // the obstruction by moving away from it by the amount in Obsinfo
    // plus spacing clearance.  If another obstruction is found that
    // prevents such a move, then all direction flags will be set, indicating
    // that the position is not routable under any condition. 

    for (g = Nlgates; g; g = g->next) {
       orient = 0;
       for (ds = g->obs;; ds = ds->next) {
	  // Run through ds list twice, checking against horizontally and
	  // vertically oriented vias.

	  if (ds == NULL) {
	     if (orient == 2)
		break;
	     else {
		orient = 2;
	 	ds = g->obs;
		if (ds == NULL) break;
	     }
	  }

	  deltax = get_via_clear(ds->layer, 1, orient, ds);
	  gridx = (int)((ds->x1 - Xlowerbound - deltax) / PitchX) - 1;
	  while (1) {
	     dx = (gridx * PitchX) + Xlowerbound;
	     if ((dx + EPS) > (ds->x2 + deltax)
			|| gridx >= NumChannelsX) break;
	     else if ((dx - EPS) > (ds->x1 - deltax) && gridx >= 0) {
		deltay = get_via_clear(ds->layer, 0, orient, ds);
	        gridy = (int)((ds->y1 - Ylowerbound - deltay) / PitchY) - 1;
	        while (1) {
		   dy = (gridy * PitchY) + Ylowerbound;
	           if ((dy + EPS) > (ds->y2 + deltay)
				|| gridy >= NumChannelsY) break;
		   if ((dy - EPS) > (ds->y1 - deltay) && gridy >= 0) {
		      double s, edist, xp, yp;

		      // Check Euclidean distance measure
		      s = LefGetRouteSpacing(ds->layer);

		      if (dx < (ds->x1 + s - deltax)) {
		         xp = dx + deltax - s;
			 edist = (ds->x1 - xp) * (ds->x1 - xp);
		      }
		      else if (dx > (ds->x2 - s + deltax)) {
		         xp = dx - deltax + s;
			 edist = (xp - ds->x2) * (xp - ds->x2);
		      }
		      else edist = 0;
		      if ((edist > 0) && (dy < (ds->y1 + s - deltay))) {
		         yp = dy + deltay - s;
			 edist += (ds->y1 - yp) * (ds->y1 - yp);
		      }
		      else if ((edist > 0) && (dy > (ds->y2 - s + deltay))) {
		         yp = dy - deltay + s;
			 edist += (yp - ds->y2) * (yp - ds->y2);
		      }
		      else edist = 0;

		      if ((edist + EPS) < (s * s)) {
			 check_obstruct(gridx, gridy, ds, dx, dy, s);
			 if (is_testpoint(gridx, gridy, g, -1, ds) != NULL)
			    Fprintf(stderr, " Position blocked by gate obstruction.\n");
		      }
		      else
			 edist = 0;	// diagnostic break
		   }
		   gridy++;
		}
	     }
	     gridx++;
	  }
       }

       for (i = 0; i < g->nodes; i++) {
	  if (g->netnum[i] == 0) {	/* Unconnected node */
	     // Diagnostic, and power bus handling
	     if (g->node[i]) {
		// Should we flag a warning if we see something that looks
		// like a power or ground net here?
		if (Verbose > 1)
		   Fprintf(stdout, "Gate instance %s unconnected node %s\n",
			g->gatename, g->node[i]);
	     }
	     else {
		if (Verbose > 1)
	           Fprintf(stdout, "Gate instance %s unconnected node (%d)\n",
			g->gatename, i);
	     }
             for (ds = g->taps[i]; ds; ds = ds->next) {

		deltax = get_via_clear(ds->layer, 1, orient, ds);
		gridx = (int)((ds->x1 - Xlowerbound - deltax) / PitchX) - 1;
		while (1) {
		   dx = (gridx * PitchX) + Xlowerbound;
		   if (dx > (ds->x2 + deltax)
				|| gridx >= NumChannelsX) break;
		   else if (dx >= (ds->x1 - deltax) && gridx >= 0) {
		      deltay = get_via_clear(ds->layer, 0, orient, ds);
		      gridy = (int)((ds->y1 - Ylowerbound - deltay) / PitchY) - 1;
		      while (1) {
		         dy = (gridy * PitchY) + Ylowerbound;
		         if ((dy + EPS) > (ds->y2 + deltay)
					|| gridy >= NumChannelsY) break;
		         if ((dy - EPS) >= (ds->y1 - deltay) && gridy >= 0) {

		            double s, edist = 0.0, xp, yp;

		            // Check Euclidean distance measure
		            s = LefGetRouteSpacing(ds->layer);

		            if (dx < (ds->x1 + s - deltax)) {
		               xp = dx + deltax - s;
			       edist += (ds->x1 - xp) * (ds->x1 - xp);
		            }
		            else if (dx > (ds->x2 - s + deltax)) {
		               xp = dx - deltax + s;
			       edist += (xp - ds->x2) * (xp - ds->x2);
		            }
			    else edist = 0;
		            if ((edist > 0) && (dy < (ds->y1 + s - deltay))) {
		               yp = dy + deltay - s;
			       edist += (ds->y1 - yp) * (ds->y1 - yp);
		            }
		            else if ((edist > 0) && (dy > (ds->y2 - s + deltay))) {
		               yp = dy - deltay + s;
			       edist += (yp - ds->y2) * (yp - ds->y2);
		            }
			    else edist = 0;

		            if ((edist + EPS) < (s * s)) {
			       check_obstruct(gridx, gridy, ds, dx, dy, s);
			       if (is_testpoint(gridx, gridy, g, i, ds) != NULL)
				  Fprintf(stderr, " Position blocked by unused"
					    " gate pin.\n");
			    }
			 }
		         gridy++;
		      }
		   }
		   gridx++;
		}
	     }
	  }
       }
    }

    // Create additional obstructions from the UserObs list
    // These obstructions are not considered to be metal layers,
    // so we don't compute a distance measure.  However, we need
    // to compute a boundary of 1/2 route width to avoid having
    // the route overlapping the obstruction area.

    for (i = 0; i < Num_layers; i++) {
	delta[i] = LefGetRouteWidth(i) / 2.0;
    }

    for (ds = UserObs; ds; ds = ds->next) {
	if (ds->layer >= Num_layers) continue;
	gridx = (int)((ds->x1 - Xlowerbound - delta[ds->layer]) / PitchX) - 1;
	while (1) {
	    dx = (gridx * PitchX) + Xlowerbound;
	    if (dx > (ds->x2 + delta[ds->layer])
			|| gridx >= NumChannelsX) break;
	    else if (dx >= (ds->x1 - delta[ds->layer]) && gridx >= 0) {
		gridy = (int)((ds->y1 - Ylowerbound - delta[ds->layer]) / PitchY) - 1;
		while (1) {
		    dy = (gridy * PitchY) + Ylowerbound;
		    if (dy > (ds->y2 + delta[ds->layer])
				|| gridy >= NumChannelsY) break;
		    if (dy >= (ds->y1 - delta[ds->layer]) && gridy >= 0) {
		       check_obstruct(gridx, gridy, ds, dx, dy, delta[i]);
		       if (is_testpoint(gridx, gridy, NULL, -1, ds) != NULL)
			  Fprintf(stderr, " Position blocked by defined obstruction.\n");
		    }

		    gridy++;
		}
	    }
	    gridx++;
	}
    }
}

/*--------------------------------------------------------------*/
/* expand_tap_geometry()					*/
/*								*/
/*  For each rectangle defining part of a gate terminal,	*/
/*  search the surrounding terminal geometry.  If the rectangle	*/
/*  can expand in any direction, then allow it to grow to the	*/
/*  maximum size.  This generates overlapping geometry for each	*/
/*  terminal, but avoids bad results for determining how to	*/
/*  route to a terminal point if the terminal is broken up into	*/
/*  numerous nonoverlapping rectangles.				*/
/*								*/
/*  Note that this is not foolproof.  It also needs a number of	*/
/*  enhancements.  For example, to expand east, other geometry	*/
/*  should be looked at in order of increasing left edge X	*/
/*  value, and so forth.					*/
/*--------------------------------------------------------------*/

void expand_tap_geometry(void)
{
    DSEG ds, ds2;
    GATE g;
    int i;
    u_char expanded;

    for (g = Nlgates; g; g = g->next) {
	for (i = 0; i < g->nodes; i++) {
	    if (g->netnum[i] == 0) continue;
	    if (g->taps == NULL) continue;

	    for (ds = g->taps[i]; ds; ds = ds->next) {
		expanded = TRUE;
		while (expanded == TRUE) {
		    expanded = FALSE;

		    for (ds2 = g->taps[i]; ds2; ds2 = ds2->next) {
		        if (ds == ds2) continue;
			if (ds->layer != ds2->layer) continue;
		    
		        if ((ds2->y1 <= ds->y1) && (ds2->y2 >= ds->y2)) {
			    // Expand east
			    if ((ds2->x1 > ds->x1) && (ds2->x1 <= ds->x2))
			        if (ds->x2 < ds2->x2) {
				    ds->x2 = ds2->x2;
				    expanded = TRUE;
			        }
		    
			    // Expand west
			    if ((ds2->x2 < ds->x2) && (ds2->x2 >= ds->x1))
			        if (ds->x1 > ds2->x1) {
				    ds->x1 = ds2->x1;
				    expanded = TRUE;
			        }
		        }
		    
		        if ((ds2->x1 <= ds->x1) && (ds2->x2 >= ds->x2)) {
			    // Expand north
			    if ((ds2->y1 > ds->y1) && (ds2->y1 <= ds->y2))
			        if (ds->y2 < ds2->y2) {
				    ds->y2 = ds2->y2;
				    expanded = TRUE;
			        }
		    
			    // Expand south
			    if ((ds2->y2 < ds->y2) && (ds2->y2 >= ds->y1))
			        if (ds->y1 > ds2->y1) {
				    ds->y1 = ds2->y1;
				    expanded = TRUE;
			        }
		        }
		    }
		}
	    }
	}
    }
}

/*--------------------------------------------------------------*/
/* is_testpoint()						*/
/*								*/
/*  Check if a grid position is on the testpoint list.  If so,	*/
/*  return the pointer to the testpoint structure.		*/
/*--------------------------------------------------------------*/

DPOINT
is_testpoint(int gridx, int gridy, GATE g, int nidx, DSEG ds)
{
    int layer;
    DPOINT trypoint;
    NODE node = NULL, onode;
    NODEINFO lnode;

    layer = ds->layer;
    for (trypoint = testpoint; trypoint; trypoint = trypoint->next) {
	if (trypoint->gridx == gridx && trypoint->gridy == gridy &&
		trypoint->layer == layer) {
	    Fprintf(stderr, "Watchpoint (%g, %g) layer %d"
		    " grid (%d, %d):\n",
		    trypoint->x, trypoint->y, trypoint->layer,
		    trypoint->gridx, trypoint->gridy); 
	    if (g != NULL) {
		Fprintf(stderr, "  Gate instance = \"%s\"\n", g->gatename);
		if (g->gatetype)
		    Fprintf(stderr, "  Gate cell = \"%s\"\n", g->gatetype->gatename);

		if (nidx >= 0) {
		    Fprintf(stderr, "  Gate pin = \"%s\"\n", g->node[nidx]);
		    Fprintf(stderr, "  Pin geometry = (%g, %g) to (%g, %g)\n",
			    ds->x1, ds->y1, ds->x2, ds->y2);
		    node = g->noderec[nidx];
		    Fprintf(stderr, "  Connects to net \"%s\"\n", node->netname);
		}
	    }
	    if (nidx < 0) {
		Fprintf(stderr, "  Obstruction geometry = (%g, %g) to (%g, %g)\n",
			ds->x1, ds->y1, ds->x2, ds->y2);
	    }
	    lnode = NODEIPTR(gridx, gridy, layer);
	    if (lnode != NULL) {
		onode = lnode->nodesav;
		if (onode != NULL) {
		    if (node && (onode->netnum != node->netnum)) {
			if (onode->netname)
			    Fprintf(stderr, "  Position was previously assigned"
				    " to node %s on net %s\n", print_node_name(onode),
				    onode->netname);
			else
			    Fprintf(stderr, "  Position was previously assigned"
				    " to node %s on different net\n",
				    print_node_name(onode));
		    }
		    else {
			Fprintf(stderr, "  Position was previously assigned"
			    " to node %s on the same net\n",
			    print_node_name(onode));
		    }
		}
		else Fprintf(stderr, "  Position was previously assigned"
			    " to a node that has been disabled.\n");
	    }
	    else Fprintf(stderr, "  Position was not previously assigned"
			    " to a node\n");

	    Fprintf(stderr, "Disabled position because:\n");
	    return trypoint;
	}
    }
    return NULL;
}

/*--------------------------------------------------------------*/
/* create_obstructions_inside_nodes()				*/
/*								*/
/*  Fills in the Obs[][] grid from the position of each node	*/
/*  (net terminal), which may have multiple unconnected		*/
/*  positions.							*/
/*								*/
/*  Also fills in the Nodeinfo.nodeloc[] grid with the node	*/
/*  number, which causes the router to put a premium on		*/
/*  routing other nets over or under this position, to		*/
/*  discourage boxing in a pin position and making it 		*/
/*  unroutable.							*/
/*								*/
/*  This routine is split into two passes.  This pass adds	*/
/*  information for points inside node regions.			*/
/*								*/
/*  ARGS: none.							*/
/*  RETURNS: nothing						*/
/*  SIDE EFFECTS: none						*/
/*  AUTHOR:  Tim Edwards, June 2011, based on code by Steve	*/
/*	Beccue.							*/
/*--------------------------------------------------------------*/

void create_obstructions_inside_nodes(void)
{
    NODE node;
    NODEINFO lnode;
    GATE g;
    DSEG ds;
    DPOINT tpoint;
    u_int dir, mask, k;
    int i, gridx, gridy;
    double dx, dy, xdist, vwx, vwy;
    u_char o0okay, o2okay, duplicate;
    float dist;

    // For each node terminal (gate pin), mark each grid position with the
    // net number.  This overrides any obstruction that may be placed at that
    // point.

    // For each pin position, we also find the "keepout" area around the
    // pin where we may not place an unrelated route.  For this, we use a
    // flag bit, so that the position can be ignored when routing the net
    // associated with the pin.  Normal obstructions take precedence.

    for (g = Nlgates; g; g = g->next) {
       for (i = 0; i < g->nodes; i++) {
	  if (g->netnum[i] != 0) {

	     // Get the node record associated with this pin.
	     node = g->noderec[i];
	     if (node == NULL) continue;

	     // First mark all areas inside node geometry boundary.

             for (ds = g->taps[i]; ds; ds = ds->next) {
		gridx = (int)((ds->x1 - Xlowerbound) / PitchX) - 1;
		if (gridx < 0) gridx = 0;
		while (1) {
		   dx = (gridx * PitchX) + Xlowerbound;
		   if (dx > ds->x2 || gridx >= NumChannelsX) break;
		   else if (dx >= ds->x1 && gridx >= 0) {
		      gridy = (int)((ds->y1 - Ylowerbound) / PitchY) - 1;
		      if (gridy < 0) gridy = 0;
		      while (1) {
		         dy = (gridy * PitchY) + Ylowerbound;
		         if (dy > ds->y2 || gridy >= NumChannelsY) break;

			 // Area inside defined pin geometry

			 if (dy > ds->y1 && gridy >= 0) {
			     int orignet = OBSVAL(gridx, gridy, ds->layer);

			     duplicate = FALSE;
			     lnode = NULL;
			     if ((orignet & ROUTED_NET_MASK & ~ROUTED_NET)
					== (u_int)node->netnum) {

				// Duplicate tap point, or pre-existing route.
				// Re-process carefully.  Check for alternate
				// restrictions.  This geometry may be better
				// than the last one(s) processed.

				if (((lnode = NODEIPTR(gridx, gridy, ds->layer)) != NULL)
					&& (lnode->nodeloc != NULL))
				    duplicate = TRUE;
			     }

			     else if (!(orignet & NO_NET) &&
					((orignet & ROUTED_NET_MASK) != (u_int)0)) {

				// Net was assigned to other net, but is inside
				// this pin's geometry.  Declare point to be
				// unroutable, as it is too close to both pins.
				// NOTE:  This is a conservative rule and could
				// potentially make a pin unroutable.
				// Another note:  By setting Obs[] to
				// OBSTRUCT_MASK as well as NO_NET, we ensure
				// that it falls through on all subsequent
				// processing.

				if (is_testpoint(gridx, gridy, g, i, ds) != NULL)
				    Fprintf(stderr, " Position is inside pin but cannot "
					    "be routed without causing violation.\n");
				disable_gridpos(gridx, gridy, ds->layer);
				gridy++;
				continue;
			     }

			     if (!(orignet & NO_NET)) {

				// A grid point that is within 1/2 route width
				// of a tap rectangle corner can violate metal
				// width rules, and so should declare a stub.
				
				mask = 0;
				dir = 0;
				dist = 0.0;
			        xdist = 0.5 * LefGetRouteWidth(ds->layer);

				if (dx >= ds->x2 - xdist) {
				   if (dy > ds->y2 - xdist + EPS) {
				      // Check northeast corner

				      if ((ds->x2 - dx) > (ds->y2 - dy)) {
					 // West-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_EW;
					 dist = ds->x2 - dx - 2.0 * xdist;
				      }
				      else {
					 // South-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_NS;
					 dist = ds->y2 - dy - 2.0 * xdist;
				      }

				   }
				   else if (dy < ds->y1 + xdist - EPS) {
				      // Check southeast corner

				      if ((ds->x2 - dx) > (dy - ds->y1)) {
					 // West-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_EW;
					 dist = ds->x2 - dx - 2.0 * xdist;
				      }
				      else {
					 // North-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_NS;
					 dist = ds->y1 - dy + 2.0 * xdist;
				      }
				   }
				}
				else if (dx <= ds->x1 + xdist) {
				   if (dy > ds->y2 - xdist + EPS) {
				      // Check northwest corner

				      if ((dx - ds->x1) > (ds->y2 - dy)) {
					 // East-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_EW;
					 dist = ds->x1 - dx + 2.0 * xdist;
				      }
				      else {
					 // South-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_NS;
					 dist = ds->y2 - dy - 2.0 * xdist;
				      }

				   }
				   else if (dy < ds->y1 + xdist - EPS) {
				      // Check southwest corner

				      if ((dx - ds->x2) > (dy - ds->y1)) {
					 // East-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_EW;
					 dist = ds->x1 - dx + 2.0 * xdist;
				      }
				      else {
					 // North-pointing stub
					 mask = STUBROUTE;
					 dir = NI_STUB_NS;
					 dist = ds->y1 - dy + 2.0 * xdist;
				      }
				   }
				}

				if (!duplicate) {
			           OBSVAL(gridx, gridy, ds->layer)
			        	= (OBSVAL(gridx, gridy, ds->layer)
					   & BLOCKED_MASK) | (u_int)node->netnum | mask;
				   if (!lnode)
				      lnode = SetNodeinfo(gridx, gridy, ds->layer,
						node);
				   lnode->nodeloc = node;
				   lnode->nodesav = node;
				   lnode->stub = dist;
				   lnode->flags |= dir;
				}

				/* If a horizontal or vertical via fits completely */
				/* inside the pin but the other orientation	   */
				/* doesn't, then mark as prohibiting the other	   */
				/* orientation.					   */

				vwx = LefGetXYViaWidth(ds->layer, ds->layer, 0, 0) / 2.0;
				vwy = LefGetXYViaWidth(ds->layer, ds->layer, 1, 0) / 2.0;
				if ((dx - vwx > ds->x1 - EPS) &&
					(dx + vwx < ds->x2 + EPS) &&
				    	(dy - vwy > ds->y1 - EPS) &&
					(dy + vwy < ds->y2 + EPS)) {
				    o0okay = TRUE;
				} else {
				    o0okay = FALSE;
				}
				vwx = LefGetXYViaWidth(ds->layer, ds->layer, 0, 2) / 2.0;
				vwy = LefGetXYViaWidth(ds->layer, ds->layer, 1, 2) / 2.0;
				if ((dx - vwx > ds->x1 - EPS) &&
					(dx + vwx < ds->x2 + EPS) &&
				    	(dy - vwy > ds->y1 - EPS) &&
					(dy + vwy < ds->y2 + EPS)) {
				    o2okay = TRUE;
				} else {
				    o2okay = FALSE;
				}
				if ((o0okay == TRUE) && (o2okay == FALSE))
				    lnode->flags |= NI_NO_VIAY;
				else if ((o0okay == FALSE) && (o2okay == TRUE))
				    lnode->flags |= NI_NO_VIAX;
			     }
			     else if ((orignet & NO_NET) && ((orignet & OBSTRUCT_MASK)
					!= OBSTRUCT_MASK)) {
				/* Handled on next pass */
			     }

			     // Check that we have not created a PINOBSTRUCT
			     // route directly over this point.
			     if ((!duplicate) && (ds->layer < Num_layers - 1)) {
			        k = OBSVAL(gridx, gridy, ds->layer + 1);
			        if (k & PINOBSTRUCTMASK) {
			           if ((k & ROUTED_NET_MASK) != (u_int)node->netnum) {
				       OBSVAL(gridx, gridy, ds->layer + 1) = NO_NET;
				       FreeNodeinfo(gridx, gridy, ds->layer + 1);
				   }
				}
			     }
			 }
		         gridy++;
		      }
		   }
		   gridx++;
		}
	     }
	  }
       }
    }

} /* void create_obstructions_inside_nodes( void ) */

/*--------------------------------------------------------------*/
/* create_obstructions_outside_nodes()				*/
/*								*/
/*  Fills in the Obs[][] grid from the position of each node	*/
/*  (net terminal), which may have multiple unconnected		*/
/*  positions.							*/
/*								*/
/*  Also fills in the Nodeinfo.nodeloc[] grid with the node	*/
/*  number, which causes the router to put a premium on		*/
/*  routing other nets over or under this position, to		*/
/*  discourage boxing in a pin position and making it 		*/
/*  unroutable.							*/
/*								*/
/*  This routine is split into two passes.  This pass adds	*/
/*  information for points outside node regions but close	*/
/*  enough to interact with the node.				*/
/*								*/
/*  ARGS: none.							*/
/*  RETURNS: nothing						*/
/*  SIDE EFFECTS: none						*/
/*  AUTHOR:  Tim Edwards, June 2011, based on code by Steve	*/
/*	Beccue.							*/
/*--------------------------------------------------------------*/


void create_obstructions_outside_nodes(void)
{
    NODE node, n2;
    NODEINFO lnode;
    GATE g;
    DSEG ds;
    DPOINT tpoint;
    u_int dir, mask, k;
    int i, gridx, gridy, orient;
    double dx, dy, xdist, deltax, deltay;
    float dist;

    // For each node terminal (gate pin), mark each grid position with the
    // net number.  This overrides any obstruction that may be placed at that
    // point.

    // For each pin position, we also find the "keepout" area around the
    // pin where we may not place an unrelated route.  For this, we use a
    // flag bit, so that the position can be ignored when routing the net
    // associated with the pin.  Normal obstructions take precedence.

    for (g = Nlgates; g; g = g->next) {
       for (i = 0; i < g->nodes; i++) {
	  if (g->netnum[i] != 0) {

	     // Get the node record associated with this pin.
	     node = g->noderec[i];
	     if (node == NULL) continue;

	     // Repeat this whole exercise for areas in the halo outside
	     // the node geometry.  We have to do this after enumerating
	     // all inside areas because the tap rectangles often overlap,
	     // and one rectangle's halo may be inside another tap.

	     orient = 0;
             for (ds = g->taps[i];; ds = ds->next) {
		if (ds == NULL) {
		    if (orient == 2)
			break;
		    else {
			orient = 2;
			ds = g->taps[i];
			if (ds == NULL) break;
		    }
		}

		// Note:  Should be handling get_route_clear as a less
		// restrictive case, as was done above.
 
		deltax = get_via_clear(ds->layer, 1, orient, ds);
		gridx = (int)((ds->x1 - Xlowerbound - deltax) / PitchX) - 1;
		if (gridx < 0) gridx = 0;

		while (1) {
		   dx = (gridx * PitchX) + Xlowerbound;

		   // Check if obstruction position is too close for
		   // a via in either orientation.  The position can
		   // be marked as prohibiting one orientation or the
		   // other.

		   if (((dx + EPS) > (ds->x2 + deltax)) ||
				(gridx >= NumChannelsX))
		      break;

		   else if ((dx - EPS) > (ds->x1 - deltax) && gridx >= 0) {
		      deltay = get_via_clear(ds->layer, 0, orient, ds);
		      gridy = (int)((ds->y1 - Ylowerbound - deltay) / PitchY) - 1;
		      if (gridy < 0) gridy = 0;

		      while (1) {
		         dy = (gridy * PitchY) + Ylowerbound;

		         if (((dy + EPS) > (ds->y2 + deltay)) ||
				(gridy >= NumChannelsY))
			    break;

			 // 2nd pass on area inside defined pin geometry,
			 // allowing terminal connections to be made using
			 // an offset tap, where possible.

			 if ((dy >= ds->y1 && gridy >= 0) && (dx >= ds->x1)
					&& (dy <= ds->y2) && (dx <= ds->x2)) {
			     int orignet = OBSVAL(gridx, gridy, ds->layer);

			     if ((orignet & ROUTED_NET_MASK) == (u_int)node->netnum) {

				// Duplicate tap point.   Don't re-process it.
				gridy++;
				continue;
			     }

			     if (!(orignet & NO_NET) &&
					((orignet & ROUTED_NET_MASK) != (u_int)0)) {
				/* Do nothing;  previously handled */
			     }

			     else if ((orignet & NO_NET) && ((orignet & OBSTRUCT_MASK)
					!= OBSTRUCT_MASK)) {
				double sdistxx, sdistxy, sdistyx, sdistyy, offd;

				sdistxx = LefGetXYViaWidth(ds->layer, ds->layer,
					0, 0) / 2.0 +
					LefGetRouteSpacing(ds->layer);
				sdistxy = LefGetXYViaWidth(ds->layer, ds->layer,
					0, 2) / 2.0 +
					LefGetRouteSpacing(ds->layer);
				sdistyx = LefGetXYViaWidth(ds->layer, ds->layer,
					1, 0) / 2.0 +
					LefGetRouteSpacing(ds->layer);
				sdistyy = LefGetXYViaWidth(ds->layer, ds->layer,
					1, 2) / 2.0 +
					LefGetRouteSpacing(ds->layer);

				// Define a maximum offset we can have in X or
				// Y above which the placement of a via will
				// cause a DRC violation with a wire in the
				// adjacent route track in the direction of the
				// offset.

				int maxerr = 0;

				// If a cell is positioned off-grid, then a grid
				// point may be inside a pin and still be unroutable.
				// The Obsinfo[] array tells where an obstruction is,
				// if there was only one obstruction in one direction
				// blocking the grid point.  If so, then we set the
				// Nodeinfo.stub[] distance to move the tap away from
				// the obstruction to resolve the DRC error.

				// Make sure we have marked this as a node.
				lnode = SetNodeinfo(gridx, gridy, ds->layer, node);
				lnode->nodeloc = node;
				lnode->nodesav = node;
			        OBSVAL(gridx, gridy, ds->layer)
			        	= (OBSVAL(gridx, gridy, ds->layer)
					   & BLOCKED_MASK) | (u_int)node->netnum;

			        offd = OBSINFO(gridx, gridy, ds->layer);
				if (orignet & OBSTRUCT_N) {
				   if (sdistyy - offd > EPS) {
				      lnode->flags |= NI_NO_VIAY;
				      if (sdistyx - offd > EPS) {
					 /* Cannot route cleanly, so use offset */
				         if (offd - sdistyx > PitchX / 2.0) 
					    /* Offset distance is too large */
					    maxerr = 1;
					 else {

			                    OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				            lnode->offset = offd - sdistyx;
				            lnode->flags |= NI_OFFSET_NS;

				            /* If position above has obstruction, then */
				            /* add up/down block to prevent vias.      */

				            if ((ds->layer < Num_layers - 1) &&
							(gridy > 0) &&
							(OBSVAL(gridx, gridy - 1,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					       block_route(gridx, gridy, ds->layer, UP);
					    }
					 }
				      }
				   }
				}
				else if (orignet & OBSTRUCT_S) {
				   if (sdistyy - offd > EPS) {
				      lnode->flags |= NI_NO_VIAY;
				      if (sdistyx - offd > EPS) {
				         if (offd - sdistyx > PitchX / 2.0)
					    /* Offset distance is too large */
					    maxerr = 1;
					 else {
			                    OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				            lnode->offset = sdistyx - offd;
				            lnode->flags |= NI_OFFSET_NS;

				            /* If position above has obstruction, then */
				            /* add up/down block to prevent vias.      */

				             if ((ds->layer < Num_layers - 1) &&
							(gridy < NumChannelsY - 1) &&
						   	(OBSVAL(gridx, gridy + 1,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					        block_route(gridx, gridy, ds->layer, UP);
					     }
					 }
				      }
				   }
				}
				else if (orignet & OBSTRUCT_E) {
				   if (sdistxx - offd > EPS) {
				      lnode->flags |= NI_NO_VIAX;
				      if (sdistxy - offd > EPS) {
				         if (offd - sdistxy > PitchY / 2.0)
					    /* Offset distance is too large */
					    maxerr = 1;
					 else {
			                    OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				            lnode->offset = offd - sdistxy;
				            lnode->flags |= NI_OFFSET_EW;

				            /* If position above has obstruction, then */
				            /* add up/down block to prevent vias.      */

				             if ((ds->layer < Num_layers - 1) &&
							(gridx > 0) &&
							(OBSVAL(gridx - 1, gridy,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					        block_route(gridx, gridy, ds->layer, UP);
					     }
					 }
				      }
				   }
				}
				else if (orignet & OBSTRUCT_W) {
				   if (sdistxx - offd > EPS) {
				      lnode->flags |= NI_NO_VIAX;
				      if (sdistxy - offd > EPS) {
				         if (offd - sdistxy > PitchY / 2.0)
					    /* Offset distance is too large */
					    maxerr = 1;
					 else {
			                    OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				            lnode->offset = sdistxy - offd;
				            lnode->flags |= NI_OFFSET_EW;

				            /* If position above has obstruction, then */
				            /* add up/down block to prevent vias.      */

				            if ((ds->layer < Num_layers - 1) &&
							(gridx < NumChannelsX - 1) &&
							(OBSVAL(gridx + 1, gridy,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					       block_route(gridx, gridy, ds->layer, UP);
					    }
					 }
				      }
				   }
				}

			        if (maxerr == 1) {
				    if (is_testpoint(gridx, gridy, g, i, ds) != NULL)
					Fprintf(stderr,
					    "Attempted to clear obstruction with"
					    " offset, but offset is more than 1/2"
					    " route pitch.\n");
				    disable_gridpos(gridx, gridy, ds->layer);
				}

				// Diagnostic
				else if (Verbose > 3)
				   Fprintf(stderr, "Port overlaps obstruction"
					" at grid %d %d, position %g %g\n",
					gridx, gridy, dx, dy);
			     }
			 }

		         if ((dy - EPS) > (ds->y1 - deltay) && gridy >= 0) {

			    double s, edist, xp, yp;
			    unsigned char epass = 0;

			    // Area inside halo around defined pin geometry.
			    // Exclude areas already processed (areas inside
			    // some pin geometry have been marked with netnum)

			    // Also check that we are not about to define a
			    // route position for a pin on a layer above 0 that
			    // blocks a pin underneath it.

			    // Flag positions that pass a Euclidean distance check.
			    // epass = 1 indicates that position clears a
			    // Euclidean distance measurement.

			    s = LefGetRouteSpacing(ds->layer);

			    if (dx < (ds->x1 + s - deltax)) {
				xp = dx + deltax - s;
				edist = (ds->x1 - xp) * (ds->x1 - xp);
			    }
			    else if (dx > (ds->x2 - s + deltax)) {
				xp = dx - deltax + s;
				edist = (xp - ds->x2) * (xp - ds->x2);
			    }
			    else edist = 0;
			    if ((edist > 0) && (dy < (ds->y1 + s - deltay))) {
				yp = dy + deltay - s;
				edist += (ds->y1 - yp) * (ds->y1 - yp);
			    }
			    else if ((edist > 0) && (dy > (ds->y2 - s + deltay))) {
				yp = dy - deltay + s;
				edist += (yp - ds->y2) * (yp - ds->y2);
			    }
			    else edist = 0;
			    if ((edist + EPS) > (s * s)) epass = 1;

			    xdist = 0.5 * LefGetRouteWidth(ds->layer);

			    n2 = NULL;
			    if (ds->layer > 0) {
			       lnode = NODEIPTR(gridx, gridy, ds->layer - 1);
			       n2 = (lnode) ? lnode->nodeloc : NULL;
			    }
			    if (n2 == NULL) {
			       lnode = NODEIPTR(gridx, gridy, ds->layer);
			       n2 = (lnode) ? lnode->nodeloc : NULL;
			    }
			    else {
			       // Watch out for the case where a tap crosses
			       // over a different tap.  Don't treat the tap
			       // on top as if it is not there!

			       NODE n3;
			       lnode = NODEIPTR(gridx, gridy, ds->layer);
			       n3 = (lnode) ? lnode->nodeloc : NULL;
			       if (n3 != NULL && n3 != node) n2 = n3;
			    }

			    // Ignore my own node.
			    if (n2 == node) n2 = NULL;

			    k = OBSVAL(gridx, gridy, ds->layer);

			    // In case of a port that is inaccessible from a grid
			    // point, or not completely overlapping it, the
			    // stub information will show how to adjust the
			    // route position to cleanly attach to the port.

			    mask = STUBROUTE;
			    dir = NI_STUB_NS | NI_STUB_EW;
			    dist = 0.0;

			    if (((k & ROUTED_NET_MASK) != (u_int)node->netnum)
					&& (n2 == NULL)) {

				if ((k & OBSTRUCT_MASK) != 0) {
				   float sdist = OBSINFO(gridx, gridy, ds->layer);

				   // If the point is marked as close to an
				   // obstruction, we can declare this an
				   // offset tap if we are not on a corner.
				   // Because we cannot define both an offset
				   // and a stub simultaneously, if the distance
				   // to clear the obstruction does not make the
				   // route reach the tap, then we mark the grid
				   // position as unroutable.

				   if (dy >= (ds->y1 - xdist) &&
						dy <= (ds->y2 + xdist)) {
				      if ((dx >= ds->x2) &&
						((k & OBSTRUCT_MASK) == OBSTRUCT_E)) {
				         dist = sdist - LefGetRouteKeepout(ds->layer);
					 if ((dx - ds->x2 + dist) < xdist) {
				 	    mask = OFFSET_TAP;
				 	    dir = NI_OFFSET_EW;

				            if ((ds->layer < Num_layers - 1) &&
							(gridx > 0) &&
							(OBSVAL(gridx - 1, gridy,
							ds->layer + 1)
							& OBSTRUCT_MASK)) {
					       block_route(gridx, gridy, ds->layer, UP);
					    }
					 }
				      }
				      else if ((dx <= ds->x1) &&
						((k & OBSTRUCT_MASK) == OBSTRUCT_W)) {
				         dist = LefGetRouteKeepout(ds->layer) - sdist;
					 if ((ds->x1 - dx - dist) < xdist) {
				 	    mask = OFFSET_TAP;
				            dir = NI_OFFSET_EW;

				            if ((ds->layer < Num_layers - 1) &&
							gridx <
							(NumChannelsX - 1)
							&& (OBSVAL(gridx + 1, gridy,
							ds->layer + 1)
							& OBSTRUCT_MASK)) {
					       block_route(gridx, gridy, ds->layer, UP);
					    }
					 }
				      }
			 	   }	
				   if (dx >= (ds->x1 - xdist) &&
						dx <= (ds->x2 + xdist)) {
				      if ((dy >= ds->y2) &&
						((k & OBSTRUCT_MASK) == OBSTRUCT_N)) {
				         dist = sdist - LefGetRouteKeepout(ds->layer);
					 if ((dy - ds->y2 + dist) < xdist) {
				 	    mask = OFFSET_TAP;
				            dir = NI_OFFSET_NS;

				            if ((ds->layer < Num_layers - 1) &&
							gridy < 
							(NumChannelsY - 1)
							&& (OBSVAL(gridx, gridy - 1,
							ds->layer + 1)
							& OBSTRUCT_MASK)) {
					       block_route(gridx, gridy, ds->layer, UP);
					    }
					 }
				      }
				      else if ((dy <= ds->y1) &&
						((k & OBSTRUCT_MASK) == OBSTRUCT_S)) {
				         dist = LefGetRouteKeepout(ds->layer) - sdist;
					 if ((ds->y1 - dy - dist) < xdist) {
				 	    mask = OFFSET_TAP;
				            dir = NI_OFFSET_NS;

				            if ((ds->layer < Num_layers - 1) &&
							(gridy > 0) &&
							(OBSVAL(gridx, gridy + 1,
							ds->layer + 1)
							& OBSTRUCT_MASK)) {
					       block_route(gridx, gridy, ds->layer, UP);
					    }
					 }
				      }
				   }
				   // Otherwise, dir is left as NI_STUB_MASK
				}
				else {

				   // Cleanly unobstructed area.  Define stub
				   // route from point to tap, with a route width
				   // overlap if necessary to avoid a DRC width
				   // violation.

				   if ((dx >= ds->x2) &&
					((dx - ds->x2) > (dy - ds->y2)) &&
					((dx - ds->x2) > (ds->y1 - dy))) {
				      // West-pointing stub
				      if ((dy - ds->y2) <= xdist &&
					  (ds->y1 - dy) <= xdist) {
					 // Within reach of tap rectangle
					 mask = STUBROUTE;
					 dir = NI_STUB_EW;
					 dist = ds->x2 - dx;
					 if (dy < (ds->y2 - xdist) &&
						dy > (ds->y1 + xdist)) {
					    if (dx < ds->x2 + xdist) dist = 0.0;
					 }
					 else {
					    dist -= 2.0 * xdist;
					 }
				      }
				   }
				   else if ((dx <= ds->x1) &&
					((ds->x1 - dx) > (dy - ds->y2)) &&
					((ds->x1 - dx) > (ds->y1 - dy))) {
				      // East-pointing stub
				      if ((dy - ds->y2) <= xdist &&
					  (ds->y1 - dy) <= xdist) {
					 // Within reach of tap rectangle
					 mask = STUBROUTE;
					 dir = NI_STUB_EW;
					 dist = ds->x1 - dx;
					 if (dy < (ds->y2 - xdist) &&
						dy > (ds->y1 + xdist)) {
					    if (dx > ds->x1 - xdist) dist = 0.0;
					 }
					 else {
					    dist += 2.0 * xdist;
					 }
				      }
				   }
				   else if ((dy >= ds->y2) &&
					((dy - ds->y2) > (dx - ds->x2)) &&
					((dy - ds->y2) > (ds->x1 - dx))) {
				      // South-pointing stub
				      if ((dx - ds->x2) <= xdist &&
					  (ds->x1 - dx) <= xdist) {
					 // Within reach of tap rectangle
					 mask = STUBROUTE;
					 dir = NI_STUB_NS;
					 dist = ds->y2 - dy;
					 if (dx < (ds->x2 - xdist) &&
						dx > (ds->x1 + xdist)) {
					    if (dy < ds->y2 + xdist) dist = 0.0;
					 }
					 else {
					    dist -= 2.0 * xdist;
					 }
				      }
				   }
				   else if ((dy <= ds->y1) &&
					((ds->y1 - dy) > (dx - ds->x2)) &&
					((ds->y1 - dy) > (ds->x1 - dx))) {
				      // North-pointing stub
				      if ((dx - ds->x2) <= xdist &&
					  (ds->x1 - dx) <= xdist) {
					 // Within reach of tap rectangle
					 mask = STUBROUTE;
					 dir = NI_STUB_NS;
					 dist = ds->y1 - dy;
					 if (dx < (ds->x2 - xdist) &&
						dx > (ds->x1 + xdist)) {
					    if (dy > ds->y1 - xdist) dist = 0.0;
					 }
					 else {
					    dist += 2.0 * xdist;
					 }
				      }
				   }

				   if ((mask == STUBROUTE) && (dir == NI_STUB_MASK)) {

				      // Outside of pin at a corner.  First, if one
				      // direction is too far away to connect to a
				      // pin, then we must route the other direction.

				      if (dx < ds->x1 - xdist || dx > ds->x2 + xdist) {
				         if (dy >= ds->y1 - xdist &&
							dy <= ds->y2 + xdist) {
					    mask = STUBROUTE;
				            dir = NI_STUB_EW;
				            dist = (float)(((ds->x1 + ds->x2) / 2.0)
							- dx);
					 }
				      }
				      else if (dy < ds->y1 - xdist ||
							dy > ds->y2 + xdist) {
					 mask = STUBROUTE;
				         dir = NI_STUB_NS;
				         dist = (float)(((ds->y1 + ds->y2) / 2.0) - dy);
				      }

				      // Otherwise we are too far away at a diagonal
				      // to reach the pin by moving in any single
				      // direction.  To be pedantic, we could define
				      // some jogged stub, but for now, we just call
				      // the point unroutable (leave dir = NI_STUB_MASK)

				      // To do:  Apply offset + stub
				   }
				}

				// Additional checks on stub routes

				// Stub distances of <= 1/2 route width are
				// unnecessary, so don't create them.

				if (mask == STUBROUTE && (dir == NI_STUB_NS
					|| dir == NI_STUB_EW) &&
					(fabs(dist) < (xdist + EPS))) {
				    mask = 0;
				    dir = 0;
				    dist = 0.0;
				}
				else if (mask == STUBROUTE && (dir == NI_STUB_NS
					|| dir == NI_STUB_EW)) {
				   struct dseg_ de;
				   DSEG ds2;
				   u_char errbox = TRUE;

				   // Additional check:  Sometimes the above
				   // checks put stub routes where they are
				   // not needed because the stub is completely
				   // covered by other tap geometry.  Take the
				   // stub area and remove parts covered by
				   // other tap rectangles.  If the entire
				   // stub is gone, then don't put a stub here.

				   if (dir == NI_STUB_NS) {
				       de.x1 = dx - xdist;
				       de.x2 = dx + xdist;
				       if (dist > 0) {
					  de.y1 = dy + xdist;
					  de.y2 = dy + dist;
				       }
				       else {
					  de.y1 = dy + dist;
					  de.y2 = dy - xdist;
				       }
				   }
				   if (dir == NI_STUB_EW) {
				       de.y1 = dy - xdist;
				       de.y2 = dy + xdist;
				       if (dist > 0) {
					  de.x1 = dx + xdist;
					  de.x2 = dx + dist;
				       }
				       else {
					  de.x1 = dx + dist;
					  de.x2 = dx - xdist;
				       }
				   }

				   // For any tap that overlaps the
				   // stub extension box, remove that
				   // part of the box.

			           for (ds2 = g->taps[i]; ds2; ds2 = ds2->next) {
				      if (ds2 == ds) continue;
				      if (ds2->layer != ds->layer) continue;

				      if (ds2->x1 <= de.x1 && ds2->x2 >= de.x2 &&
						ds2->y1 <= de.y1 && ds2->y2 >= de.y2) {
				         errbox = FALSE;	// Completely covered
				         break;
				      }

				      // Look for partial coverage.  Note that any
				      // change can cause a change in the original
				      // two conditionals, so we have to keep
				      // evaluating those conditionals.

				      // ds2 covers left side of de
				      if (ds2->x1 < de.x2 && ds2->x2 > de.x1 + EPS)
				         if (ds2->y1 < de.y2 && ds2->y2 > de.y1)
					    if (ds2->x1 < de.x1 + EPS &&
							ds2->x2 < de.x2 - EPS) {
					       de.x1 = ds2->x2;
					       if (de.x1 > de.x2 - EPS) errbox = FALSE;
					    }

				      // ds2 covers right side of de
				      if (ds2->x1 < de.x2 - EPS && ds2->x2 > de.x1)
				         if (ds2->y1 < de.y2 && ds2->y2 > de.y1)
					    if (ds2->x2 > de.x2 - EPS &&
							ds2->x1 > de.x1 + EPS) {
					       de.x2 = ds2->x1;
					       if (de.x2 < de.x1 + EPS) errbox = FALSE;
					    }

				      // ds2 covers bottom side of de
				      if (ds2->y1 < de.y2 && ds2->y2 > de.y1 + EPS)
				         if (ds2->x1 < de.x2 && ds2->x2 > de.x1)
					    if (ds2->y1 < de.y1 + EPS &&
							ds2->y2 < de.y2 - EPS) {
					       de.y1 = ds2->y2;
					       if (de.y1 > de.y2 - EPS) errbox = FALSE;
					    }

				      // ds2 covers top side of de
				      if (ds2->y1 < de.y2 - EPS && ds2->y2 > de.y1)
				         if (ds2->x1 < de.x2 && ds2->x2 > de.x1)
					    if (ds2->y2 > de.y2 - EPS &&
							ds2->y1 > de.y1 + EPS) {
					       de.y2 = ds2->y1;
					       if (de.y2 < de.y1 + EPS) errbox = FALSE;
					    }
			           }

				   // If nothing is left of the stub box,
				   // then remove the stub.

				   if (errbox == FALSE) {
				      mask = 0;
				      dir = 0;
				      dist = 0;
				   }
                                }

				lnode = SetNodeinfo(gridx, gridy, ds->layer, node);
				lnode->nodeloc = node;
				lnode->nodesav = node;

				if ((k < Numnets) && (dir != NI_STUB_MASK)) {
				   OBSVAL(gridx, gridy, ds->layer)
				   	= (OBSVAL(gridx, gridy, ds->layer)
					  & BLOCKED_MASK) | (u_int)g->netnum[i] | mask; 
				   lnode->flags |= dir;
				}
				else if ((OBSVAL(gridx, gridy, ds->layer)
					& NO_NET) != 0) {
				   // Keep showing an obstruction, but add the
				   // direction info and log the stub distance.
				   OBSVAL(gridx, gridy, ds->layer) |= mask;
				   lnode->flags |= dir;
				}
				else {
				   OBSVAL(gridx, gridy, ds->layer)
					|= (mask | (g->netnum[i] & ROUTED_NET_MASK));
				   lnode->flags |= dir;
				}
				if ((mask & STUBROUTE) != 0) {
				   lnode->stub = dist;
				}
				else if (((mask & OFFSET_TAP) != 0) || (dist != 0.0)) {
				   lnode->offset = dist;
				}
				
				// Remove entries with NI_STUB_MASK---these
				// are blocked-in taps that are not routable
				// without causing DRC violations (formerly
				// called STUBROUTE_X).

				if (dir == NI_STUB_MASK) {
				    if (is_testpoint(gridx, gridy, g, i, ds) != NULL)
					Fprintf(stderr, "Tap point is blocked in "
					    "and cannot be routed without causing "
					    "DRC violations.\n");
				    disable_gridpos(gridx, gridy, ds->layer);
				}
			    }
			    else if (epass == 0) {

			       // Position fails euclidean distance check

			       int othernet = (k & ROUTED_NET_MASK);

			       if (othernet != 0 && othernet != (u_int)node->netnum) {

			          // This location is too close to two different
				  // node terminals and should not be used

				  // If there is a stub, then we can't specify
				  // an offset tap, so just disable it.  If
				  // there is already an offset, then just
				  // disable it.  Otherwise, check if othernet
				  // could be routed using a tap offset.

				  // To avoid having to check all nearby
				  // geometry, place a restriction that the
				  // next grid point in the direction of the
				  // offset must be free (not a tap point of
				  // any net, including this one).  That is
				  // still "more restrictive than necessary",
				  // but since the alternative is an efficient
				  // area search for interacting geometry, this
				  // restriction will stand until an example
				  // comes along that requires the detailed
				  // search.

				  // Such an example has come along, leading to
				  // an additional relaxation allowing an offset
				  // if the neighboring channel does not have a
				  // node record.  This will probably need
				  // revisiting.
				
				  if ((k & PINOBSTRUCTMASK) != 0) {
				     if (is_testpoint(gridx, gridy, g, i, ds) != NULL) {
					if (k & STUBROUTE)
					    Fprintf(stderr, "Position marked as a "
						"stub route for the net.\n");
					else
					    Fprintf(stderr, "Position marked as a "
						"tap offset for the net.\n");
				     }
				     disable_gridpos(gridx, gridy, ds->layer);
				  }
				  else if ((lnode = NODEIPTR(gridx, gridy, ds->layer))
					!= NULL && (lnode->nodesav != NULL)) {

				     u_char no_offsets = TRUE;
				     int offset_net;

				     // By how much would a tap need to be moved
				     // to clear the obstructing geometry?

				     // Check tap to right

				     if ((dx > ds->x2) && (gridx <
						NumChannelsX - 1)) {
					offset_net = OBSVAL(gridx + 1, gridy, ds->layer);
					if (offset_net == 0 || offset_net == othernet) {
					   xdist = 0.5 * LefGetXYViaWidth(ds->layer,
							ds->layer, 0, orient);
					   dist = ds->x2 - dx + xdist +
							LefGetRouteSpacing(ds->layer);
					   // Only accept an alternative solution if
					   // it has a smaller offset than previously
					   // found.
					   if ((no_offsets == FALSE) ||
						    (fabs(lnode->offset) > fabs(dist))) {
					      mask = OFFSET_TAP;
					      dir = NI_OFFSET_EW;
					      OBSVAL(gridx, gridy, ds->layer) |= mask;
					      lnode->offset = dist;
					      lnode->flags &= ~(NI_OFFSET_NS);
					      lnode->flags |= dir;
					      no_offsets = FALSE;

				              if ((ds->layer < Num_layers - 1) &&
							(gridx > 0) &&
							(OBSVAL(gridx + 1, gridy,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
				              else if ((ds->layer < Num_layers - 1) &&
							(gridx > 0) &&
							(dist > PitchX / 2)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
					   }
					}
				     }

				     // Check tap to left

				     if ((dx < ds->x1) && (gridx > 0)) {
					offset_net = OBSVAL(gridx - 1, gridy, ds->layer);
					if (offset_net == 0 || offset_net == othernet) {
					   xdist = 0.5 * LefGetXYViaWidth(ds->layer,
							ds->layer, 0, orient);
					   dist = ds->x1 - dx - xdist -
							LefGetRouteSpacing(ds->layer);
					   if ((no_offsets == FALSE) ||
						    (fabs(lnode->offset) > fabs(dist))) {
					      mask = OFFSET_TAP;
					      dir = NI_OFFSET_EW;
					      OBSVAL(gridx, gridy, ds->layer) |= mask;
					      lnode->offset = dist;
					      lnode->flags &= ~(NI_OFFSET_NS);
					      lnode->flags |= dir;
					      no_offsets = FALSE;

				              if ((ds->layer < Num_layers - 1) && gridx <
							(NumChannelsX - 1) &&
							(OBSVAL(gridx - 1, gridy,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
				              else if ((ds->layer < Num_layers - 1) &&
							gridx <
							(NumChannelsX - 1) &&
							(dist < -PitchX / 2)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
					   }
					}
				     }

				     // Check tap up

				     if ((dy > ds->y2) && (gridy <
						NumChannelsY - 1)) {
					offset_net = OBSVAL(gridx, gridy + 1, ds->layer);
					if (offset_net == 0 || offset_net == othernet) {
					   xdist = 0.5 * LefGetXYViaWidth(ds->layer,
							ds->layer, 1, orient);
					   dist = ds->y2 - dy + xdist +
							LefGetRouteSpacing(ds->layer);
					   if ((no_offsets == FALSE) ||
						    (fabs(lnode->offset) > fabs(dist))) {
					      mask = OFFSET_TAP;
					      dir = NI_OFFSET_NS;
					      OBSVAL(gridx, gridy, ds->layer) |= mask;
					      lnode->offset = dist;
					      lnode->flags &= ~(NI_OFFSET_EW);
					      lnode->flags |= dir;
					      no_offsets = FALSE;

				              if ((ds->layer < Num_layers - 1) &&
							(gridy > 0) && (OBSVAL(gridx,
							gridy + 1, ds->layer + 1)
							& OBSTRUCT_MASK)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
				              else if ((ds->layer < Num_layers - 1) &&
							(gridy > 0) &&
							(dist > PitchY / 2)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
					   }
					}
				     }

				     // Check tap down

				     if ((dy < ds->y1) && (gridy > 0)) {
					offset_net = OBSVAL(gridx, gridy - 1, ds->layer);
					if (offset_net == 0 || offset_net == othernet) {
					   xdist = 0.5 * LefGetXYViaWidth(ds->layer,
							ds->layer, 1, orient);
					   dist = ds->y1 - dy - xdist -
							LefGetRouteSpacing(ds->layer);
					   if ((no_offsets == FALSE) ||
						    (fabs(lnode->offset) > fabs(dist))) {
					      mask = OFFSET_TAP;
					      dir = NI_OFFSET_NS;
					      OBSVAL(gridx, gridy, ds->layer) |= mask;
					      lnode->offset = dist;
					      lnode->flags &= ~(NI_OFFSET_EW);
					      lnode->flags |= dir;
					      no_offsets = FALSE;

				              if ((ds->layer < Num_layers - 1) &&
							gridx <
							(NumChannelsX - 1) &&
							(OBSVAL(gridx, gridy - 1,
							ds->layer + 1) & OBSTRUCT_MASK)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
				              else if ((ds->layer < Num_layers - 1) &&
							gridx <
							(NumChannelsX - 1) &&
							(dist < -PitchY / 2)) {
					         block_route(gridx, gridy, ds->layer, UP);
					      }
					   }
					}
				     }

				     // No offsets were possible.  If orient is 0
				     // then mark as NI_NO_VIAX and try again with
				     // orient 2.  If orient is 2 and no offsets
				     // are possible, then disable the position.

				     if (no_offsets == TRUE) {
				 	if (orient == 2) {
					    // Maybe no need to revert the flag?
					    lnode->flags &= ~NI_NO_VIAX;
					    if (is_testpoint(gridx, gridy,
						    g, i, ds) != NULL)
						Fprintf(stderr, "Unable to find "
							"a viable offset for a tap.\n");
				            disable_gridpos(gridx, gridy, ds->layer);
					}
					else
					    lnode->flags |= NI_NO_VIAX;
				     }
				  }
				  else {
				     if (is_testpoint(gridx, gridy,
						    g, i, ds) != NULL)
					Fprintf(stderr, "Tap point is too "
						"close to two different nodes "
						"and no offsets are possible.\n");
				     disable_gridpos(gridx, gridy, ds->layer);
				  }
			       }

			       /* If we are on a layer > 0, then this geometry	*/
			       /* may block or partially block a pin on layer	*/
			       /* zero.  Mark this point as belonging to the	*/
			       /* net with a stub route to it.			*/
			       /* NOTE:  This is possibly too restrictive.	*/
			       /* May want to force a tap offset for vias on	*/
			       /* layer zero. . .				*/

			       if ((ds->layer > 0) && (n2 != NULL) && (n2->netnum
					!= node->netnum) && ((othernet == 0) ||
					(othernet == (u_int)node->netnum))) {

				  lnode = NODEIPTR(gridx, gridy, ds->layer);
				  xdist = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer,
						0, orient);
				  if ((dy + xdist + LefGetRouteSpacing(ds->layer) >
					ds->y1) && (dy + xdist < ds->y1)) {
				     if ((dx - xdist < ds->x2) &&
						(dx + xdist > ds->x1) &&
						(lnode == NULL || lnode->stub
						== 0.0)) {
					OBSVAL(gridx, gridy, ds->layer)
				   		= (OBSVAL(gridx, gridy, ds->layer)
						& BLOCKED_MASK) |
						node->netnum | STUBROUTE;
				        lnode = SetNodeinfo(gridx, gridy, ds->layer,
						node);
					lnode->nodeloc = node;
					lnode->nodesav = node;
					lnode->stub = ds->y1 - dy;
					lnode->flags |= NI_STUB_NS;
				     }
				  }
				  if ((dy - xdist - LefGetRouteSpacing(ds->layer) <
					ds->y2) && (dy - xdist > ds->y2)) {
				     if ((dx - xdist < ds->x2) &&
						(dx + xdist > ds->x1) &&
						(lnode == NULL || lnode->stub
						== 0.0)) {
					OBSVAL(gridx, gridy, ds->layer)
				   		= (OBSVAL(gridx, gridy, ds->layer)
						& BLOCKED_MASK) |
						node->netnum | STUBROUTE;
				        lnode = SetNodeinfo(gridx, gridy, ds->layer,
						node);
					lnode->nodeloc = node;
					lnode->nodesav = node;
					lnode->stub = ds->y2 - dy;
					lnode->flags |= NI_STUB_NS;
				     }
				  }

				  xdist = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer,
						1, orient);
				  if ((dx + xdist + LefGetRouteSpacing(ds->layer) >
					ds->x1) && (dx + xdist < ds->x1)) {
				     if ((dy - xdist < ds->y2) &&
						(dy + xdist > ds->y1) &&
						(lnode == NULL || lnode->stub
						 == 0.0)) {
					OBSVAL(gridx, gridy, ds->layer)
				   		= (OBSVAL(gridx, gridy, ds->layer)
						& BLOCKED_MASK) |
						node->netnum | STUBROUTE;
				        lnode = SetNodeinfo(gridx, gridy, ds->layer,
						node);
					lnode->nodeloc = node;
					lnode->nodesav = node;
					lnode->stub = ds->x1 - dx;
					lnode->flags |= NI_STUB_EW;
				     }
				  }
				  if ((dx - xdist - LefGetRouteSpacing(ds->layer) <
					ds->x2) && (dx - xdist > ds->x2)) {
				     if ((dy - xdist < ds->y2) &&
						(dy + xdist > ds->y1) &&
						(lnode == NULL || lnode->stub
						== 0.0)) {
					OBSVAL(gridx, gridy, ds->layer)
				   		= (OBSVAL(gridx, gridy, ds->layer)
						& BLOCKED_MASK) |
						node->netnum | STUBROUTE;
				        lnode = SetNodeinfo(gridx, gridy, ds->layer,
						node);
					lnode->nodeloc = node;
					lnode->nodesav = node;
					lnode->stub = ds->x2 - dx;
					lnode->flags |= NI_STUB_EW;
				     }
				  }
			       }
			    }
		         }
		         gridy++;
		      }
		   }
		   gridx++;
		}
	     }
	  }
       }
    }

} /* void create_obstructions_outside_nodes( void ) */

/*--------------------------------------------------------------*/
/* tap_to_tap_interactions()					*/
/*								*/
/*  Similar to create_obstructions_from_nodes(), but looks at	*/
/*  each node's tap geometry, looks at every grid point in a	*/
/*  wider area surrounding the tap.  If any other node has an	*/
/*  offset that would place it too close to this node's	tap	*/
/*  geometry, then we mark the other node as unroutable at that	*/
/*  grid point.							*/
/*--------------------------------------------------------------*/

void tap_to_tap_interactions(void)
{
    NODEINFO lnode;
    GATE g;
    DSEG ds;
    DPOINT tpoint;
    struct dseg_ de;
    int mingridx, mingridy, maxgridx, maxgridy;
    int i, gridx, gridy, net, orignet;
    double dx, dy;
    float dist;

    double deltaxx[MAX_LAYERS];
    double deltaxy[MAX_LAYERS];
    double deltayx[MAX_LAYERS];
    double deltayy[MAX_LAYERS];

    for (i = 0; i < Num_layers; i++) {
	deltaxx[i] = 0.5 * LefGetXYViaWidth(i, i, 0, 0) + LefGetRouteSpacing(i);
	deltayx[i] = 0.5 * LefGetXYViaWidth(i, i, 1, 0) + LefGetRouteSpacing(i);
	deltaxy[i] = 0.5 * LefGetXYViaWidth(i, i, 0, 2) + LefGetRouteSpacing(i);
	deltayy[i] = 0.5 * LefGetXYViaWidth(i, i, 1, 2) + LefGetRouteSpacing(i);
    }

    for (g = Nlgates; g; g = g->next) {
       for (i = 0; i < g->nodes; i++) {
	  net = g->netnum[i];
	  if (net > 0) {
             for (ds = g->taps[i]; ds; ds = ds->next) {

		mingridx = (int)((ds->x1 - Xlowerbound) / PitchX) - 1;
		if (mingridx < 0) mingridx = 0;
		maxgridx = (int)((ds->x2 - Xlowerbound) / PitchX) + 2;
		if (maxgridx >= NumChannelsX)
		   maxgridx = NumChannelsX - 1;
		mingridy = (int)((ds->y1 - Ylowerbound) / PitchY) - 1;
		if (mingridy < 0) mingridy = 0;
		maxgridy = (int)((ds->y2 - Ylowerbound) / PitchY) + 2;
		if (maxgridy >= NumChannelsY)
		   maxgridy = NumChannelsY - 1;

		for (gridx = mingridx; gridx <= maxgridx; gridx++) {
		   for (gridy = mingridy; gridy <= maxgridy; gridy++) {

		      /* Is there an offset tap at this position, and	*/
		      /* does it belong to a net that is != net?	*/

		      orignet = OBSVAL(gridx, gridy, ds->layer);
		      if (orignet & OFFSET_TAP) {
			 orignet &= ROUTED_NET_MASK;
			 if (orignet != net) {

		            dx = (gridx * PitchX) + Xlowerbound;
		            dy = (gridy * PitchY) + Ylowerbound;

			    lnode = NODEIPTR(gridx, gridy, ds->layer);
			    dist = (lnode) ? lnode->offset : 0.0;

			    /* "de" is the bounding box of a via placed	  */
			    /* at (gridx, gridy) and offset as specified. */
			    /* Expanded by metal spacing requirement.	  */

			    de.x1 = dx - deltaxx[ds->layer];
			    de.x2 = dx + deltaxx[ds->layer];
			    de.y1 = dy - deltayx[ds->layer];
			    de.y2 = dy + deltayx[ds->layer];

			    if (lnode->flags & NI_OFFSET_NS) {
			       de.y1 += dist;
			       de.y2 += dist;
			    }
			    else if (lnode->flags & NI_OFFSET_EW) {
			       de.x1 += dist;
			       de.x2 += dist;
			    }

			    // Shrink by EPS to avoid roundoff errors
			    de.x1 += EPS;
			    de.x2 -= EPS;
			    de.y1 += EPS;
			    de.y2 -= EPS;

			    /* Does the via bounding box interact with	*/
			    /* the tap geometry?			*/

			    if ((de.x1 < ds->x2) && (ds->x1 < de.x2) &&
					(de.y1 < ds->y2) && (ds->y1 < de.y2)) {
			       if (is_testpoint(gridx, gridy,
						    g, i, ds) != NULL)
				    Fprintf(stderr, "Offset tap interferes "
					    "with position.\n");
			       disable_gridpos(gridx, gridy, ds->layer);
			    }
			 }
		      }

		      /* Does the distance to the tap prohibit a specific */
		      /* via orientation?				  */
		      if ((orignet & (~(BLOCKED_N | BLOCKED_S | BLOCKED_E | BLOCKED_W)))
				== 0) {
			 lnode = NODEIPTR(gridx, gridy, ds->layer);
			 /* Positions belonging to nodes should have	*/
			 /* already been handled.			*/
			 if (lnode == NULL) {
		            dx = (gridx * PitchX) + Xlowerbound;
		            dy = (gridy * PitchY) + Ylowerbound;

			    /* For a horizontally-oriented via on ds->layer */
			    de.x1 = dx - deltaxx[ds->layer];
			    de.x2 = dx + deltaxx[ds->layer];
			    de.y1 = dy - deltayx[ds->layer];
			    de.y2 = dy + deltayx[ds->layer];

			    if (ds->x2 > de.x1 && ds->x1 < de.x2) {
			        /* Check north and south */
			        if ((ds->y1 < de.y2 && ds->y2 > de.y2) ||
			 		(ds->y2 > de.y1 && ds->y1 < de.y1)) {
				    /* prohibit horizontal via */
				    lnode = SetNodeinfo(gridx, gridy, ds->layer,
						g->noderec[i]);
				    lnode->flags |= NI_NO_VIAX;
				}
			    }

			    if (ds->y2 > de.y1 && ds->y1 < de.y2) {
			        /* Check east and west*/
			        if ((ds->x1 < de.x2 && ds->x2 > de.x2) ||
					(ds->x2 > de.x1 && ds->x1 < de.x1)) {
				    /* prohibit horizontal via */
				    lnode = SetNodeinfo(gridx, gridy, ds->layer,
						g->noderec[i]);
				    lnode->flags |= NI_NO_VIAX;
				}
			    }

			    /* For a vertically-oriented via on ds->layer */
			    de.x1 = dx - deltaxy[ds->layer];
			    de.x2 = dx + deltaxy[ds->layer];
			    de.y1 = dy - deltayy[ds->layer];
			    de.y2 = dy + deltayy[ds->layer];

			    if (ds->x2 > de.x1 && ds->x1 < de.x2) {
			        /* Check north and south */
			        if ((ds->y1 < de.y2 && ds->y2 > de.y2) ||
			 		(ds->y2 > de.y1 && ds->y1 < de.y1)) {
				    /* prohibit horizontal via */
				    lnode = SetNodeinfo(gridx, gridy, ds->layer,
						g->noderec[i]);
				    lnode->flags |= NI_NO_VIAY;
				}
			    }

			    if (ds->y2 > de.y1 && ds->y1 < de.y2) {
			        /* Check east and west*/
			        if ((ds->x1 < de.x2 && ds->x2 > de.x2) ||
					(ds->x2 > de.x1 && ds->x1 < de.x1)) {
				    /* prohibit horizontal via */
				    lnode = SetNodeinfo(gridx, gridy, ds->layer,
						g->noderec[i]);
				    lnode->flags |= NI_NO_VIAY;
				}
			    }
			 }
		      }
		   }
		}
	     }
	  }
       }
    }
}

/*--------------------------------------------------------------*/
/* make_routable()						*/
/*								*/
/*  In the case that a node can't be routed because it has no	*/
/*  available tap points, but there is tap geometry recorded	*/
/*  for the node, then take the first available grid location	*/
/*  near the tap.  This, of course, bypasses all of qrouter's	*/
/*  DRC checks.  But it is only meant to be a stop-gap measure	*/
/*  to get qrouter to complete all routes, and may work in	*/
/*  cases where, say, the tap passes euclidean rules but not	*/
/*  manhattan rules.						*/
/*--------------------------------------------------------------*/

void
make_routable(NODE node)
{
    NODEINFO lnode;
    GATE g;
    DSEG ds;
    int i, gridx, gridy;
    double dx, dy;

    /* The database is not organized to find tap points	*/
    /* from nodes, so we have to search for the node.	*/
    /* Fortunately this routine isn't normally called.	*/

    for (g = Nlgates; g; g = g->next) {
       for (i = 0; i < g->nodes; i++) {
	  if (g->noderec[i] == node) {
             for (ds = g->taps[i]; ds; ds = ds->next) {
		gridx = (int)((ds->x1 - Xlowerbound) / PitchX) - 1;
		if (gridx < 0) gridx = 0;
		while (1) {
		   dx = (gridx * PitchX) + Xlowerbound;
		   if (dx > ds->x2 || gridx >= NumChannelsX) break;
		   else if (dx >= ds->x1 && gridx >= 0) {
		      gridy = (int)((ds->y1 - Ylowerbound) / PitchY) - 1;
		      if (gridy < 0) gridy = 0;
		      while (1) {
		         dy = (gridy * PitchY) + Ylowerbound;
		         if (dy > ds->y2 || gridy >= NumChannelsY) break;

			 // Area inside defined pin geometry

			 if (dy > ds->y1 && gridy >= 0) {
			    int orignet = OBSVAL(gridx, gridy, ds->layer);

			    if (orignet & NO_NET) {
				OBSVAL(gridx, gridy, ds->layer) = g->netnum[i];
				lnode = SetNodeinfo(gridx, gridy, ds->layer,
						g->noderec[i]);
				lnode->nodeloc = node;
				lnode->nodesav = node;
				return;
			    }
			 }
			 gridy++;
		      }
		   }
		   gridx++;
	        }
	     }
	  }
       }
    }
}

/*--------------------------------------------------------------*/
/* adjust_stub_lengths()					*/
/*								*/
/*  Makes an additional pass through the tap and obstruction	*/
/*  databases, checking geometry against the potential stub	*/
/*  routes for DRC spacing violations.  Adjust stub routes as	*/
/*  necessary to resolve the DRC error(s).			*/
/*								*/
/*  ARGS: none.							*/
/*  RETURNS: nothing						*/
/*  SIDE EFFECTS: none						*/
/*  AUTHOR:  Tim Edwards, April 2013				*/
/*--------------------------------------------------------------*/

void adjust_stub_lengths(void)
{
    NODE node;
    NODEINFO lnode;
    GATE g;
    DSEG ds, ds2;
    struct dseg_ dt, de;
    int i, gridx, gridy, orignet, o;
    double dx, dy, wx, wy, s;
    float dist;
    u_char errbox;
    int orient = 0;

    // For each node terminal (gate pin), look at the surrounding grid points.
    // If any define a stub route or an offset, check if the stub geometry
    // or offset geometry would create a DRC spacing violation.  If so, adjust
    // the stub route to resolve the error.  If the error cannot be resolved,
    // mark the position as unroutable.  If it is the ONLY grid point accessible
    // to the pin, keep it as-is and flag a warning.

    // Unlike blockage-finding routines, which look in an area of a size equal
    // to the DRC interaction distance around a tap rectangle, this routine looks
    // out one grid pitch in each direction, to catch information about stubs that
    // may terminate within a DRC interaction distance of the tap rectangle.

    for (g = Nlgates; g; g = g->next) {
       for (i = 0; i < g->nodes; i++) {
	  if (g->netnum[i] != 0) {

	     // Get the node record associated with this pin.
	     node = g->noderec[i];
	     if (node == NULL) continue;

	     // Work through each rectangle in the tap geometry twice.  The
	     // second sweep checks for errors created by geometry that
	     // interacts with stub routes created by the first sweep, and
	     // also for geometry that interacts with vias in 90 degree
	     // orientation.

	     orient = 0;
             for (ds = g->taps[i];; ds = ds->next) {
		if (ds == NULL) {
		    if (orient == 0) {
			orient = 2;
			ds = g->taps[i];
			if (ds == NULL) break;
		    }
		    else
			break;
		}
		wx = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 0, orient);
		wy = 0.5 * LefGetXYViaWidth(ds->layer, ds->layer, 1, orient);
		s = LefGetRouteSpacing(ds->layer);
		gridx = (int)((ds->x1 - Xlowerbound - PitchX) / PitchX) - 1;
		while (1) {
		   dx = (gridx * PitchX) + Xlowerbound;
		   if (dx > (ds->x2 + PitchX) ||
				gridx >= NumChannelsX) break;
		   else if (dx >= (ds->x1 - PitchX) && gridx >= 0) {
		      gridy = (int)((ds->y1 - Ylowerbound - PitchY) / PitchY) - 1;
		      while (1) {
		         dy = (gridy * PitchY) + Ylowerbound;
		         if (dy > (ds->y2 + PitchY) ||
				gridy >= NumChannelsY) break;
		         if (dy >= (ds->y1 - PitchY) && gridy >= 0) {

			     orignet = OBSVAL(gridx, gridy, ds->layer);

			     // Ignore this location if it is assigned to another
			     // net, or is assigned to NO_NET.

			     if ((orignet & ROUTED_NET_MASK) != node->netnum) {
				gridy++;
				continue;
			     }
			     lnode = NODEIPTR(gridx, gridy, ds->layer);

			     // Even if it's on the same net, we need to check
			     // if the stub is to this node, otherwise it is not
			     // an issue.
			     if ((!lnode) || (lnode->nodesav != node)) {
				gridy++;
				continue;
			     }

			     // NI_STUB_MASK are unroutable;  leave them alone
			     if (orignet & STUBROUTE) {
				if ((lnode->flags & NI_OFFSET_MASK) == NI_OFFSET_MASK) {
				   gridy++;
				   continue;
				}
			     }

			     // define a route box around the grid point

			     errbox = FALSE;
			     dt.x1 = dx - wx;
			     dt.x2 = dx + wx;
			     dt.y1 = dy - wy;
			     dt.y2 = dy + wy;

			     // adjust the route box according to the stub
			     // or offset geometry, provided that the stub
			     // is longer than the route box.

			     if (orignet & OFFSET_TAP) {
			        dist = lnode->offset;
				if (lnode->flags & NI_OFFSET_EW) {
				   dt.x1 += dist;
				   dt.x2 += dist;
				}
				else if (lnode->flags & NI_OFFSET_NS) {
				   dt.y1 += dist;
				   dt.y2 += dist;
				}
			     }
			     else if (orignet & STUBROUTE) {
			        dist = (double)lnode->stub;
				if (lnode->flags & NI_STUB_EW) {
				   if (dist > EPS) {
				      if (dx + dist > dt.x2)
				 	 dt.x2 = dx + dist;
				   }
				   else {
				      if (dx + dist < dt.x1)
					 dt.x1 = dx + dist;
				   }
				}
				else if (lnode->flags & NI_STUB_NS) {
				   if (dist > EPS) {
				      if (dy + dist > dt.y2)
					 dt.y2 = dy + dist;
				   }
				   else {
				      if (dy + dist < dt.y1)
					 dt.y1 = dy + dist;
				   }
				}
			     }

			     de = dt;

			     // check for DRC spacing interactions between
			     // the tap box and the route box

			     if ((dt.y1 - ds->y2) > EPS && (dt.y1 - ds->y2) + EPS < s) {
				if (ds->x2 > (dt.x1 - s) && ds->x1 < (dt.x2 + s)) {
				   de.y2 = dt.y1;
				   de.y1 = ds->y2;
				   if (ds->x2 + s < dt.x2) de.x2 = ds->x2 + s;
				   if (ds->x1 - s > dt.x1) de.x1 = ds->x1 - s;
				   errbox = TRUE;
				}
			     }
			     else if ((ds->y1 - dt.y2) > EPS && (ds->y1 - dt.y2) + EPS < s) {
				if (ds->x2 > (dt.x1 - s) && ds->x1 < (dt.x2 + s)) {
				   de.y1 = dt.y2;
				   de.y2 = ds->y1;
				   if (ds->x2 + s < dt.x2) de.x2 = ds->x2 + s;
				   if (ds->x1 - s > dt.x1) de.x1 = ds->x1 - s;
				   errbox = TRUE;
				}
			     }

			     if ((dt.x1 - ds->x2) > EPS && (dt.x1 - ds->x2) + EPS < s) {
				if (ds->y2 > (dt.y1 - s) && ds->y1 < (dt.y2 + s)) {
				   de.x2 = dt.x1;
				   de.x1 = ds->x2;
				   if (ds->y2 + s < dt.y2) de.y2 = ds->y2 + s;
				   if (ds->y1 - s > dt.y1) de.y1 = ds->y1 - s;
				   errbox = TRUE;
				}
			     }
			     else if ((ds->x1 - dt.x2) > EPS && (ds->x1 - dt.x2) + EPS < s) {
				if (ds->y2 > (dt.y1 - s) && ds->y1 < (dt.y2 + s)) {
				   de.x1 = dt.x2;
				   de.x2 = ds->x1;
				   if (ds->y2 + s < dt.y2) de.y2 = ds->y2 + s;
				   if (ds->y1 - s > dt.y1) de.y1 = ds->y1 - s;
				   errbox = TRUE;
				}
			     }

			     if (errbox == TRUE) {
	
			        // Chop areas off the error box that are covered by
			        // other taps of the same port.

			        for (ds2 = g->taps[i]; ds2; ds2 = ds2->next) {
				   if (ds2 == ds) continue;
				   if (ds2->layer != ds->layer) continue;

				   if (ds2->x1 <= de.x1 && ds2->x2 >= de.x2 &&
					ds2->y1 <= de.y1 && ds2->y2 >= de.y2) {
				      errbox = FALSE;	// Completely covered
				      break;
				   }

				   // Look for partial coverage.  Note that any
				   // change can cause a change in the original
				   // two conditionals, so we have to keep
				   // evaluating those conditionals.

				   if (ds2->x1 < de.x2 && ds2->x2 > de.x1)
				      if (ds2->y1 < de.y2 && ds2->y2 > de.y1)
					 // if (ds2->x1 < de.x1 - EPS &&
					 if (ds2->x1 < de.x1 + EPS &&
							ds2->x2 < de.x2 - EPS) {
					    de.x1 = ds2->x2;
					    if (ds2->x2 >= ds->x2) errbox = FALSE;
					 }

				   if (ds2->x1 < de.x2 && ds2->x2 > de.x1)
				      if (ds2->y1 < de.y2 && ds2->y2 > de.y1)
					 // if (ds2->x2 > de.x2 + EPS &&
					 if (ds2->x2 > de.x2 - EPS &&
							ds2->x1 > de.x1 + EPS) {
					    de.x2 = ds2->x1;
					    if (ds2->x1 <= ds->x1) errbox = FALSE;
					 }

				   if (ds2->x1 < de.x2 && ds2->x2 > de.x1)
				      if (ds2->y1 < de.y2 && ds2->y2 > de.y1)
					 // if (ds2->y1 < de.y1 - EPS &&
					 if (ds2->y1 < de.y1 + EPS &&
							ds2->y2 < de.y2 - EPS) {
					    de.y1 = ds2->y2;
					    if (ds2->y2 >= ds->y2) errbox = FALSE;
					 }

				   if (ds2->x1 < de.x2 && ds2->x2 > de.x1)
				      if (ds2->y1 < de.y2 && ds2->y2 > de.y1)
					 // if (ds2->y2 > de.y2 + EPS &&
					 if (ds2->y2 > de.y2 - EPS &&
							ds2->y1 > de.y1 + EPS) {
					    de.y2 = ds2->y1;
					    if (ds2->y1 <= ds->y1) errbox = FALSE;
					 }
				}
			     }

			     // Any area left over is a potential DRC error.

			     if ((de.x2 <= de.x1) || (de.y2 <= de.y1))
				errbox = FALSE;
		
			     if (errbox == TRUE) {

				// Create stub route to cover error box, or
				// if possible, stretch existing stub route
				// to cover error box.

				// Allow EW stubs to be changed to NS stubs and
				// vice versa if the original stub length was less
				// than a route width.  This means the grid position
				// makes contact without the stub.  Moving the stub
				// to another side should not create an error.

				// NOTE:  error box must touch ds geometry, and by
				// more than just a point.

				// 8/31/2016:
				// If DRC violations are created on two adjacent
				// sides, then create both a stub route and a tap
				// offset.  Put the stub route in the preferred
				// metal direction of the layer, and set the tap
				// offset to prevent the DRC error in the other
				// direction.
				// 10/3/2016:  The tap offset can be set either
				// by moving toward the obstructing edge to
				// remove the gap, or moving away from it to
				// avoid the DRC spacing error.  Choose the one
				// that offsets by the smaller distance.

				if ((de.x2 > dt.x2) && (de.y1 < ds->y2) &&
						(de.y2 > ds->y1)) {
				   if ((orignet & STUBROUTE) == 0) {
			              OBSVAL(gridx, gridy, ds->layer) |= STUBROUTE;
				      lnode->stub = de.x2 - dx;
				      lnode->flags |= NI_STUB_EW;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_EW)) {
				      // Beware, if dist > 0 then this reverses
				      // the stub.  For U-shaped ports may need
				      // to have separate E and W stubs.
				      lnode->stub = de.x2 - dx;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_NS)) {

				      // If preferred route direction is
				      // horizontal, then change the stub

			              OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				      if (LefGetRouteOrientation(ds->layer) == 1) {
					 lnode->flags = NI_OFFSET_NS | NI_STUB_EW;
					 if (lnode->stub > 0) {
					    lnode->offset = lnode->stub - wy;
					    if (lnode->offset < 0) lnode->offset = 0;
					 }
					 else {
					    lnode->offset = lnode->stub + wy;
					    if (lnode->offset > 0) lnode->offset = 0;
					 }
				         lnode->stub = de.x2 - dx;
				         errbox = FALSE;
				      }
				      else {
					 // Add the offset
				         lnode->offset = de.x2 - dx - wx;
					 if (lnode->offset > s - lnode->offset)
					     lnode->offset -= s;
					 lnode->flags |= NI_OFFSET_EW;
				         errbox = FALSE;
				      }
				   }
				}
				else if ((de.x1 < dt.x1) && (de.y1 < ds->y2) &&
						(de.y2 > ds->y1)) {
				   if ((orignet & STUBROUTE) == 0) {
			              OBSVAL(gridx, gridy, ds->layer) |= STUBROUTE;
				      lnode->stub = de.x1 - dx;
				      lnode->flags |= NI_STUB_EW;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_EW)) {
				      // Beware, if dist > 0 then this reverses
				      // the stub.  For U-shaped ports may need
				      // to have separate E and W stubs.
				      lnode->stub = de.x1 - dx;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_NS)) {

				      // If preferred route direction is
				      // horizontal, then change the stub

			              OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				      if (LefGetRouteOrientation(ds->layer) == 1) {
					 lnode->flags = NI_OFFSET_NS | NI_STUB_EW;
					 if (lnode->stub > 0) {
					    lnode->offset = lnode->stub - wy;
					    if (lnode->offset < 0) lnode->offset = 0;
					 }
					 else {
					    lnode->offset = lnode->stub + wy;
					    if (lnode->offset > 0) lnode->offset = 0;
					 }
				         lnode->stub = de.x1 - dx;
				         errbox = FALSE;
				      }
				      else {
					 // Add the offset
				         lnode->offset = de.x1 - dx + wx;
					 if (-lnode->offset > s + lnode->offset)
					     lnode->offset += s;
					 lnode->flags |= NI_OFFSET_EW;
				         errbox = FALSE;
				      }
				   }
				}
				else if ((de.y2 > dt.y2) && (de.x1 < ds->x2) &&
					(de.x2 > ds->x1)) {
				   if ((orignet & STUBROUTE) == 0) {
			              OBSVAL(gridx, gridy, ds->layer) |= STUBROUTE;
				      lnode->stub = de.y2 - dy;
				      lnode->flags |= NI_STUB_NS;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_NS)) {
				      // Beware, if dist > 0 then this reverses
				      // the stub.  For C-shaped ports may need
				      // to have separate N and S stubs.
				      lnode->stub = de.y2 - dy;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_EW)) {

				      // If preferred route direction is
				      // vertical, then change the stub

			              OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				      if (LefGetRouteOrientation(ds->layer) == 0) {
					 lnode->flags = NI_OFFSET_EW | NI_STUB_NS;
					 if (lnode->stub > 0) {
					    lnode->offset = lnode->stub - wx;
					    if (lnode->offset < 0) lnode->offset = 0;
					 }
					 else {
					    lnode->offset = lnode->stub + wx;
					    if (lnode->offset > 0) lnode->offset = 0;
					 }
				         lnode->stub = de.y2 - dy;
				         errbox = FALSE;
				      }
				      else {
					 // Add the offset
				         lnode->offset = de.y2 - dy - wy;
					 if (lnode->offset > s - lnode->offset)
					     lnode->offset -= s;
					 lnode->flags |= NI_OFFSET_NS;
				         errbox = FALSE;
				      }
				   }
				}
				else if ((de.y1 < dt.y1) && (de.x1 < ds->x2) &&
					(de.x2 > ds->x1)) {
				   if ((orignet & STUBROUTE) == 0) {
			              OBSVAL(gridx, gridy, ds->layer) |= STUBROUTE;
				      lnode->stub = de.y1 - dy;
				      lnode->flags |= NI_STUB_NS;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_NS)) {
				      // Beware, if dist > 0 then this reverses
				      // the stub.  For C-shaped ports may need
				      // to have separate N and S stubs.
				      lnode->stub = de.y1 - dy;
				      errbox = FALSE;
				   }
				   else if ((orignet & STUBROUTE)
						&& (lnode->flags & NI_STUB_EW)) {

				      // If preferred route direction is
				      // vertical, then change the stub

			              OBSVAL(gridx, gridy, ds->layer) |= OFFSET_TAP;
				      if (LefGetRouteOrientation(ds->layer) == 0) {
					 lnode->flags = NI_OFFSET_EW | NI_STUB_NS;
					 if (lnode->stub > 0) {
					    lnode->offset = lnode->stub - wx;
					    if (lnode->offset < 0) lnode->offset = 0;
					 }
					 else {
					    lnode->offset = lnode->stub + wx;
					    if (lnode->offset > 0) lnode->offset = 0;
					 }
				         lnode->stub = de.y1 - dy + wy;
				         errbox = FALSE;
				      }
				      else {
					 // Add the offset
				         lnode->offset = de.y1 - dy + wy;
					 if (-lnode->offset > s + lnode->offset)
					     lnode->offset += s;
					 lnode->flags |= NI_OFFSET_NS;
				         errbox = FALSE;
				      }
				   }
				}

				// Where the error box did not touch the stub
				// route, there is assumed to be no error.

				if (errbox == TRUE)
				   if ((de.x2 > dt.x2) || (de.x1 < dt.x1) ||
					(de.y2 > dt.y2) || (de.y1 < dt.y1))
				      errbox = FALSE;

				if (errbox == TRUE) {
				   // Unroutable position, so mark it unroutable
			           OBSVAL(gridx, gridy, ds->layer) |= STUBROUTE;
				   lnode->flags |= NI_STUB_MASK;
				}
			     }
		         }
		         gridy++;
		      }
		   }
		   gridx++;
		}
	     }
	  }
       }
    }

} /* void adjust_stub_lengths() */

/*--------------------------------------------------------------*/
/* block_route()						*/
/*								*/
/*  Mark a specific length along the route tracks as unroutable	*/
/*  by finding the grid point in the direction indicated, and	*/
/*  setting the appropriate block bit in the Obs[] array for	*/
/*  that position.  The original grid point is marked as	*/
/*  unroutable in the opposite direction, for symmetry.		*/
/*--------------------------------------------------------------*/

void
block_route(int x, int y, int lay, u_char dir)
{
   int bx, by, bl, ob;

   bx = x;
   by = y;
   bl = lay;

   switch (dir) {
      case NORTH:
	 if (y == NumChannelsY - 1) return;
	 by = y + 1;
	 break;
      case SOUTH:
	 if (y == 0) return;
	 by = y - 1;
	 break;
      case EAST:
	 if (x == NumChannelsX - 1) return;
	 bx = x + 1;
	 break;
      case WEST:
	 if (x == 0) return;
	 bx = x - 1;
	 break;
      case UP:
	 if (lay == Num_layers - 1) return;
	 bl = lay + 1;
	 break;
      case DOWN:
	 if (lay == 0) return;
	 bl = lay - 1;
	 break;
   }
   
   ob = OBSVAL(bx, by, bl);

   if ((ob & NO_NET) != 0) return;

   switch (dir) {
      case NORTH:
	 OBSVAL(bx, by, bl) |= BLOCKED_S;
	 OBSVAL(x, y, lay) |= BLOCKED_N;
	 break;
      case SOUTH:
	 OBSVAL(bx, by, bl) |= BLOCKED_N;
	 OBSVAL(x, y, lay) |= BLOCKED_S;
	 break;
      case EAST:
	 OBSVAL(bx, by, bl) |= BLOCKED_W;
	 OBSVAL(x, y, lay) |= BLOCKED_E;
	 break;
      case WEST:
	 OBSVAL(bx, by, bl) |= BLOCKED_E;
	 OBSVAL(x, y, lay) |= BLOCKED_W;
	 break;
      case UP:
	 OBSVAL(bx, by, bl) |= BLOCKED_D;
	 OBSVAL(x, y, lay) |= BLOCKED_U;
	 break;
      case DOWN:
	 OBSVAL(bx, by, bl) |= BLOCKED_U;
	 OBSVAL(x, y, lay) |= BLOCKED_D;
	 break;
   }
}

/*--------------------------------------------------------------*/
/* find_route_blocks() ---					*/
/*								*/
/*	Search tap geometry for edges that cause DRC spacing	*/
/*	errors with route edges.  This specifically checks	*/
/*	edges of the route tracks, not the intersection points.	*/
/*	If a tap would cause an error with a route segment,	*/
/*	the grid points on either end of the segment are	*/
/*	flagged to prevent generating a route along that	*/
/*	specific segment.					*/
/*--------------------------------------------------------------*/

void
find_route_blocks()
{
   GATE g;
   NODEINFO lnode;
   DSEG ds;
   struct dseg_ dt, lds;
   int i, gridx, gridy;
   double dx, dy, w, v, s, u;
   double dist;
   int orient = 0;	/* Need to check orient = 2! */

   for (g = Nlgates; g; g = g->next) {
      for (i = 0; i < g->nodes; i++) {
	 if (g->netnum[i] != 0) {

	    // Work through each rectangle in the tap geometry

            for (ds = g->taps[i]; ds; ds = ds->next) {
	       lds = *ds;	/* Make local copy of tap rect */

	       /* Trim to array bounds and reject if out-of-bounds */
	       gridx = (int)((lds.x1 - Xlowerbound) / PitchX);
	       if (gridx >= NumChannelsX) continue;
	       if (gridx < 0) lds.x1 = Xlowerbound;

	       gridx = (int)((lds.x2 - Xlowerbound) / PitchX);
	       if (gridx < 0) continue;
	       if (gridx >= NumChannelsX)
		   lds.x2 = Xlowerbound + (NumChannelsX * PitchX);
	       
	       gridy = (int)((lds.y1 - Ylowerbound) / PitchY);
	       if (gridy >= NumChannelsY) continue;
	       if (gridy < 0) lds.y1 = Ylowerbound;

	       gridy = (int)((lds.y2 - Ylowerbound) / PitchY);
	       if (gridy < 0) continue;
	       if (gridy >= NumChannelsY)
		   lds.y2 = Ylowerbound + (NumChannelsY * PitchY);
	       
	       w = 0.5 * LefGetRouteWidth(lds.layer);
	       v = 0.5 * LefGetXYViaWidth(lds.layer, lds.layer, 0, orient);
	       s = LefGetRouteSpacing(lds.layer);

	       // Look west

	       gridx = (int)((lds.x1 - Xlowerbound) / PitchX);
	       dx = (gridx * PitchX) + Xlowerbound;
	       dist = lds.x1 - dx - w;
	       if (dist > 0 && dist < s && gridx >= 0) {
		  dt.x1 = dt.x2 = dx;
		  dt.y1 = lds.y1;
		  dt.y2 = lds.y2;

		  // Check for other taps covering this edge
		  // (to do)

		  // Find all grid points affected
	          gridy = (int)((lds.y1 - Ylowerbound - PitchY) / PitchY);
	          dy = (gridy * PitchY) + Ylowerbound;
		  while (dy < lds.y1 - s) {
		     dy += PitchY;
		     gridy++;
		  }
		  while (dy < lds.y2 + s) {
		     lnode = NODEIPTR(gridx, gridy, lds.layer);
		     u = ((OBSVAL(gridx, gridy, lds.layer) & STUBROUTE)
				&& (lnode->flags & NI_STUB_EW)) ? v : w;
		     if (dy + EPS < lds.y2 - u) {
			block_route(gridx, gridy, lds.layer, NORTH);
		     }
		     if (dy - EPS > lds.y1 + u) {
			block_route(gridx, gridy, lds.layer, SOUTH);
		     }
		     dy += PitchY;
		     gridy++;
		  }
	       }

	       // Look east

	       gridx = (int)(1.0 + (lds.x2 - Xlowerbound) / PitchX);
	       dx = (gridx * PitchX) + Xlowerbound;
	       dist = dx - lds.x2 - w;
	       if (dist > 0 && dist < s && gridx < NumChannelsX) {
		  dt.x1 = dt.x2 = dx;
		  dt.y1 = lds.y1;
		  dt.y2 = lds.y2;

		  // Check for other taps covering this edge
		  // (to do)

		  // Find all grid points affected
	          gridy = (int)((lds.y1 - Ylowerbound - PitchY) / PitchY);
	          dy = (gridy * PitchY) + Ylowerbound;
		  while (dy < lds.y1 - s) {
		     dy += PitchY;
		     gridy++;
		  }
		  while (dy < lds.y2 + s) {
		     lnode = NODEIPTR(gridx, gridy, lds.layer);
		     u = ((OBSVAL(gridx, gridy, lds.layer) & STUBROUTE)
				&& (lnode->flags & NI_STUB_EW)) ? v : w;
		     if (dy + EPS < lds.y2 - u) {
			block_route(gridx, gridy, lds.layer, NORTH);
		     }
		     if (dy - EPS > lds.y1 + u) {
			block_route(gridx, gridy, lds.layer, SOUTH);
		     }
		     dy += PitchY;
		     gridy++;
		  }
	       }

	       // Look south

	       gridy = (int)((lds.y1 - Ylowerbound) / PitchY);
	       dy = (gridy * PitchY) + Ylowerbound;
	       dist = lds.y1 - dy - w;
	       if (dist > 0 && dist < s && gridy >= 0) {
		  dt.x1 = lds.x1;
		  dt.x2 = lds.x2;
		  dt.y1 = dt.y2 = dy;

		  // Check for other taps covering this edge
		  // (to do)

		  // Find all grid points affected
	          gridx = (int)((lds.x1 - Xlowerbound - PitchX) / PitchX);
	          dx = (gridx * PitchX) + Xlowerbound;
		  while (dx < lds.x1 - s) {
		     dx += PitchX;
		     gridx++;
		  }
		  while (dx < lds.x2 + s) {
		     lnode = NODEIPTR(gridx, gridy, lds.layer);
		     u = ((OBSVAL(gridx, gridy, lds.layer) & STUBROUTE)
				&& (lnode->flags & NI_STUB_NS)) ? v : w;
		     if (dx + EPS < lds.x2 - u) {
			block_route(gridx, gridy, lds.layer, EAST);
		     }
		     if (dx - EPS > lds.x1 + u) {
			block_route(gridx, gridy, lds.layer, WEST);
		     }
		     dx += PitchX;
		     gridx++;
		  }
	       }

	       // Look north

	       gridy = (int)(1.0 + (lds.y2 - Ylowerbound) / PitchY);
	       dy = (gridy * PitchY) + Ylowerbound;
	       dist = dy - lds.y2 - w;
	       if (dist > 0 && dist < s && gridy < NumChannelsY) {
		  dt.x1 = lds.x1;
		  dt.x2 = lds.x2;
		  dt.y1 = dt.y2 = dy;

		  // Check for other taps covering this edge
		  // (to do)

		  // Find all grid points affected
	          gridx = (int)((lds.x1 - Xlowerbound - PitchX) / PitchX);
	          dx = (gridx * PitchX) + Xlowerbound;
		  while (dx < lds.x1 - s) {
		     dx += PitchX;
		     gridx++;
		  }
		  while (dx < lds.x2 + s) {
		     lnode = NODEIPTR(gridx, gridy, lds.layer);
		     u = ((OBSVAL(gridx, gridy, lds.layer) & STUBROUTE)
				&& (lnode->flags & NI_STUB_NS)) ? v : w;
		     if (dx + EPS < lds.x2 - u) {
			block_route(gridx, gridy, lds.layer, EAST);
		     }
		     if (dx - EPS > lds.x1 + u) {
			block_route(gridx, gridy, lds.layer, WEST);
		     }
		     dx += PitchX;
		     gridx++;
		  }
	       }
	    }
	 }
      }
   }
}

/* node.c */