| 12
 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
 
 | /*
 *	Dirichlet_construction.c
 *
 *	The Dirichlet domain code is divided among several files.  The header
 *	file Dirichlet.h explains the organization of the three files, and
 *	provides the common definitions and declarations.
 *
 *	This file provides the function
 *
 *		WEPolyhedron *compute_Dirichlet_domain(	MatrixPairList	*gen_list,
 *												double			vertex_epsilon);
 *
 *	compute_Dirichlet_domain() computes the Dirichlet domain defined by
 *	the list of generators, and sets the list of generators equal to
 *	the face pairings of the Dirichlet domain, sorted by order of increasing
 *	image height (see Dirichlet_basepoint.c for the definition of "image
 *	height").  It does not set certain cosmetic fields (vertex->ideal, etc.)
 *	which aren't needed for the computation;  it assumes the calling
 *	function will set them at the very end (cf. bells_and_whistles() in
 *	Dirichlet_extras.c).  If compute_Dirichlet_domain() fails (as explained
 *	immediately below) it returns NULL.
 *
 *	Error detection.  No Dirichlet domain algorithm can be perfect.
 *	If you give it, say, the generators for a (p,q) Dehn surgery on the
 *	figure eight knot, then it will surely fail for large enough p and q,
 *	because the true Dirichlet domain will have a vast number of tiny
 *	faces, and the numerical precision of the underlying hardware won't
 *	be good enough to resolve them all correctly.  The present algorithm
 *	will attempt to resolve the faces as best it can, but will return
 *	NULL if it fails.  Its strategy is to start with a cube and intersect
 *	it with appropriate half planes, one at a time.  At each step it checks
 *	how the slicing plane intersects the existing polyhedron.  Vertices
 *	lying very close to the slicing plane are assumed to lie on it (the
 *	precise interpretation of "very close" is defined by vertex_epsilon).
 *	Each edge passing from one side of the slicing plane to the other is
 *	divided in two by introducing a new vertex at the point where the edge
 *	intersects the slicing plane.  Similarly, each face which passes through
 *	the slicing plane is divided in two.  The program then checks rigorously
 *	that the slicing plane divides the surface of the polyhedron into two
 *	combinatorial disks.  If it does, the vertices, edges and faces on
 *	the "far" side of the slicing plane are discarded, a new face is
 *	introduced, and we know for sure that the surface of the polyhedron
 *	is still a combinatorial ball.  If the slicing plane does not divide
 *	the surface of the polyhedron into two combinatorial disk (as could
 *	happen when the vertices are so close together that roundoff error
 *	introduces inconsistent combinatorial relations), the program gives up
 *	and returns NULL.
 *
 *	The basic approach of the algorithm is to first use the given generators
 *	to find a polyhedron which
 *
 *	(1)	is a superset of the true Dirichlet domain, and
 *
 *	(2)	does not extend beyond the sphere at infinity.
 *
 *	Once it has such a polyhedron, it checks whether the faces match correctly.
 *	If they all do, it's done.  Otherwise it uses the pairs of nonmatching
 *	faces to find new group elements which are guaranteed to slice off at
 *	least one vertex of the candidate polyhedron.  In this way it is guaranteed
 *	to arrive at the true Dirichlet domain in a finite (and typically small)
 *	number of steps.  The algorithm is described in more detail in the code
 *	itself.
 *
 *	For closed manifolds the algorithm works quickly and reliably.  For
 *	large cusped manifolds, computing a candidate polyhedron satisfying
 *	(1) and (2) above is a bottleneck.  I hope to eventually find a quicker
 *	way to compute it.
 *
 *	The Dirichlet domain algorithm uses matrices in O(3,1). Despite their
 *	many theoretical advantages, they lead to rapidly accumulating numerical
 *	errors when the manifold is at all large (see Dirichlet_precision.c for
 *	a more detailed discussion of the problem).  Occasionally the algorithm will
 *	encounter topological inconsistencies and fail, and will return NULL.
 *	Fortunately the problem can usually be corrected.  The root of the problem
 *	is the need to decide whether two closely spaced vertices should be
 *	interpreted as distinct vertices of the Dirichlet domain, or as two images
 *	of a single vertex, differing only by roundoff error.  The decision is
 *	guided by the value of the vertex_epsilon parameter.  Use a small value
 *	(e.g. 1e-12) to compute Dirichlet domains with many small faces, e.g. a
 *	(100,1) Dehn surgery on the figure eight knot.  Use a larger value when
 *	computing simple Dirichlet domains for larger manifolds.  (Larger manifolds
 *	tend to have less accurately defined face pairings, due to the numerical
 *	problems in O(3,1).)  The most extreme vertex resolution values (say less
 *	than 1e-16 or more than 1e-2) usually fail, so try to stay within that
 *	range unless you're really desperate.
 *
 *	Technical note:  the WEPolyhedron's num_vertices, num_edges and num_faces
 *	fields are not maintained during the construction, but are set at the end.
 *
 *	By the way, compute_Dirichlet_domain() assumes no nontrivial group element
 *	fixes the origin.  In other words, it assumes that the basepoint does not
 *	lie in an orbifold's singular set.  Dirichlet_from_generators_with_displacement()
 *	in Dirichlet.c makes sure that doesn't happen.
 */
#include "kernel.h"
#include "Dirichlet.h"
#include <stdlib.h>		/* needed for qsort() */
/*
 *	The Dirichlet domain computation begins with a large cube enclosing the
 *	projective model of hyperbolic space, and intersects it with the half
 *	spaces corresponding to the initial set of generators.  CUBE_SIZE
 *	is half the length of the cube's side.  CUBE_SIZE must be at least 1.0
 *	so that the Dirichlet domain fits inside, but it shouldn't be so large
 *	that numerical accuracy suffers.
 */
#define CUBE_SIZE			2.0
/*
 *	If the distance between the basepoint (1, 0, 0, 0) and one
 *	of its translates is less than about ERROR_EPSILON,
 *	compute_normal_to_Dirichlet_plane() returns func_failed.  If it
 *	weren't for roundoff error this should never happen, since we
 *	take care to move t moves towards a maximum of the injectivity
 *	radius it should go still further from the singular set.  But
 *	roundoff error may produce elements which should be the identity,
 *	but aren't close enough to have been recognized as such.
 *	Note that ERROR_EPSILON needs to be coordinated with MATRIX_EPSILON
 *	in Dirichlet.h, but there not much slack between them.
 */
#define ERROR_EPSILON		1e-4
/*
 *	A vertex is considered hyperideal iff o31_inner_product(vertex->x, vertex->x)
 *	is greater than HYPERIDEAL_EPSILON.  Recall that vertex->x[0] is always 1,
 *	so that if the vertex is at a Euclidean distance (1 + epsilon) from the origin
 *	in the Klein model, o31_inner_product(vertex->x, vertex->x) will be
 *	-1 + (1 + epsilon)^2 = 2*epsilon.
 *
 *	96/9/5  Changed HYPERIDEAL_EPSILON from 1e-4 to 1e-3 to avoid
 *	infinite loop when computing Dirichlet domain for x004 with
 *	"coarse" vertex resolution.
 */
#define HYPERIDEAL_EPSILON	1e-3
/*
 *	verify_group() considers one O31Matrix to be simpler than
 *	another if its height is at least VERIFY_EPSILON less.
 */
#define VERIFY_EPSILON		1e-4
/*
 *	intersect_with_halfspaces() will report a failure if the MatrixPair
 *	it is given deviates from O(3,1) by more than DEVIATION_EPSILON.
 */
#define DEVIATION_EPSILON	1e-3
static WEPolyhedron	*initial_polyhedron(MatrixPairList *gen_list, double vertex_epsilon);
static WEPolyhedron	*new_WEPolyhedron(void);
static void			make_cube(WEPolyhedron *polyhedron);
static FuncResult	slice_polyhedron(WEPolyhedron *polyhedron, MatrixPairList *gen_list);
static FuncResult	intersect_with_halfspaces(WEPolyhedron *polyhedron, MatrixPair *matrix_pair);
static Boolean		same_image_of_origin(O31Matrix m0, O31Matrix m1);
static FuncResult	slice_with_hyperplane(WEPolyhedron *polyhedron, O31Matrix m, WEFace **new_face);
static FuncResult	compute_normal_to_Dirichlet_plane(O31Matrix m, O31Vector normal_vector);
static void			compute_vertex_to_hyperplane_distances(WEPolyhedron *polyhedron, O31Vector normal_vector);
static Boolean		positive_vertices_exist(WEPolyhedron *polyhedron);
static void			cut_edges(WEPolyhedron *polyhedron);
static FuncResult	cut_faces(WEPolyhedron *polyhedron);
static FuncResult	check_topology_of_cut(WEPolyhedron *polyhedron);
static void			install_new_face(WEPolyhedron *polyhedron, WEFace *new_face);
static Boolean		face_still_exists(WEPolyhedron *polyhedron, WEFace *face);
static Boolean		has_hyperideal_vertices(WEPolyhedron *polyhedron);
static void			compute_all_products(WEPolyhedron *polyhedron, MatrixPairList *product_list);
static void			poly_to_current_list(WEPolyhedron *polyhedron, MatrixPairList *current_list);
static void			current_list_to_product_tree(MatrixPairList *current_list, MatrixPair  **product_tree);
static Boolean		already_on_product_tree(O31Matrix product, MatrixPair *product_tree);
static void			add_to_product_tree(O31Matrix product, MatrixPair **product_tree);
static void			product_tree_to_product_list(MatrixPair  *product_tree, MatrixPairList *product_list);
static void			append_tree_to_list(MatrixPair *product_tree, MatrixPair *list_end);
static FuncResult	check_faces(WEPolyhedron *polyhedron);
static FuncResult	pare_face(WEFace *face, WEPolyhedron *polyhedron, Boolean *face_was_pared);
static FuncResult	pare_mated_face(WEFace *face, WEPolyhedron *polyhedron, Boolean *face_was_pared);
static FuncResult	pare_mateless_face(WEFace *face, WEPolyhedron *polyhedron, Boolean *face_was_pared);
static FuncResult	try_this_alpha(O31Matrix *alpha, WEFace *face, WEPolyhedron *polyhedron, Boolean *face_was_pared);
static void			count_cells(WEPolyhedron *polyhedron);
static void			sort_faces(WEPolyhedron *polyhedron);
static int CDECL	compare_face_distance(const void *ptr1, const void *ptr2);
static Boolean		verify_faces(WEPolyhedron *polyhedron);
static FuncResult	verify_group(WEPolyhedron *polyhedron, MatrixPairList *gen_list);
static void			rewrite_gen_list(WEPolyhedron *polyhedron, MatrixPairList *gen_list);
WEPolyhedron *compute_Dirichlet_domain(
	MatrixPairList	*gen_list,
	double			vertex_epsilon)
{
	WEPolyhedron	*polyhedron;
	/*
	 *	Initialize the polyhedron to be the intersection of the
	 *	half spaces defined by the elements of the gen_list.
	 */
	polyhedron = initial_polyhedron(gen_list, vertex_epsilon);
	/*
	 *	If topological problems caused by roundoff errors get
	 *	in the way, report a failure.
	 */
	if (polyhedron == NULL)
		return NULL;
	/*
	 *	Check whether pairs of faces match.  If a pair fails to
	 *	match exactly, use the corresponding group elements to
	 *	create a new face plane which slices off another bit of
	 *	the WEPolyhedron.  Perform a similar operation if some
	 *	face lacks a mate.  If roundoff errors get in the way,
	 *	free the polyhedron and return NULL.
	 */
	if (check_faces(polyhedron) == func_failed)
	{
		free_Dirichlet_domain(polyhedron);
		return NULL;
	}
	/*
	 *	Count the number of vertices, edges and faces.
	 */
	count_cells(polyhedron);
	/*
	 *	Put the faces in order of increasing distance from the basepoint.
	 */
	sort_faces(polyhedron);
	/*
	 *	Count the number of edges incident to each face.  If a face and
	 *	its mate don't have the same number of incident edges, free the
	 *	polyhedron and return NULL.
	 */
	if (verify_faces(polyhedron) == func_failed)
	{
		free_Dirichlet_domain(polyhedron);
		return NULL;
	}
	/*
	 *	Verify that the face pairings generate the group as
	 *	originally specified by gen_list.  This ensures that
	 *	we have a Dirichlet domain for the manifold itself,
	 *	not some finite-sheeted cover.
	 */
	if (verify_group(polyhedron, gen_list) == func_failed)
	{
		free_Dirichlet_domain(polyhedron);
		return NULL;
	}
	/*
	 *	Discard the old list to generators and replace it with the
	 *	set of face pairing transformations (plus the identity).
	 *	They will be in ascending order, because we've already sorted
	 *	the face list.
	 */
	rewrite_gen_list(polyhedron, gen_list);
	return polyhedron;
}
static WEPolyhedron	*initial_polyhedron(
	MatrixPairList	*gen_list,
	double			vertex_epsilon)
{
	WEPolyhedron	*polyhedron;
	MatrixPairList	product_list;
	/*
	 *	Allocate a WEPolyhedron data structure, and initialize
	 *	its doubly-linked lists of vertices, edges and faces.
	 */
	polyhedron = new_WEPolyhedron();
	/*
	 *	Set the vertex_epsilon;
	 */
	polyhedron->vertex_epsilon = vertex_epsilon;
	/*
	 *	Initialize the polyhedron to be a big cube.
	 */
	make_cube(polyhedron);
	/*
	 *	Intersect the cube with the halfspaces defined by the elements
	 *	of gen_list.  If topological problems due to roundoff error
	 *	get in the way, free the polyhedron and return NULL.
	 */
	if (slice_polyhedron(polyhedron, gen_list) == func_failed)
	{
		free_Dirichlet_domain(polyhedron);
		return NULL;
	}
	/*
	 *	While hyperideal vertices remain (i.e. vertices lying beyond the
	 *	sphere at infinity), compute all products of face->group_elements,
	 *	and intersect the polyhedron with the corresponding half spaces.
	 */
	while (has_hyperideal_vertices(polyhedron) == TRUE)
	{
		compute_all_products(polyhedron, &product_list);
		if (slice_polyhedron(polyhedron, &product_list) == func_failed)
		{
			free_matrix_pairs(&product_list);
			free_Dirichlet_domain(polyhedron);
			return NULL;
		}
		free_matrix_pairs(&product_list);
	}
	return polyhedron;
}
static WEPolyhedron *new_WEPolyhedron()
{
	WEPolyhedron	*new_polyhedron;
	new_polyhedron = NEW_STRUCT(WEPolyhedron);
	new_polyhedron->num_vertices	= 0;
	new_polyhedron->num_edges		= 0;
	new_polyhedron->num_faces		= 0;
	new_polyhedron->vertex_list_begin.prev	= NULL;
	new_polyhedron->vertex_list_begin.next	= &new_polyhedron->vertex_list_end;
	new_polyhedron->vertex_list_end  .prev	= &new_polyhedron->vertex_list_begin;
	new_polyhedron->vertex_list_end  .next	= NULL;
	new_polyhedron->edge_list_begin.prev	= NULL;
	new_polyhedron->edge_list_begin.next	= &new_polyhedron->edge_list_end;
	new_polyhedron->edge_list_end  .prev	= &new_polyhedron->edge_list_begin;
	new_polyhedron->edge_list_end  .next	= NULL;
	new_polyhedron->face_list_begin.prev	= NULL;
	new_polyhedron->face_list_begin.next	= &new_polyhedron->face_list_end;
	new_polyhedron->face_list_end  .prev	= &new_polyhedron->face_list_begin;
	new_polyhedron->face_list_end  .next	= NULL;
	new_polyhedron->vertex_class_begin.prev	= NULL;
	new_polyhedron->vertex_class_begin.next	= &new_polyhedron->vertex_class_end;
	new_polyhedron->vertex_class_end  .prev	= &new_polyhedron->vertex_class_begin;
	new_polyhedron->vertex_class_end  .next	= NULL;
	new_polyhedron->edge_class_begin.prev	= NULL;
	new_polyhedron->edge_class_begin.next	= &new_polyhedron->edge_class_end;
	new_polyhedron->edge_class_end  .prev	= &new_polyhedron->edge_class_begin;
	new_polyhedron->edge_class_end  .next	= NULL;
	new_polyhedron->face_class_begin.prev	= NULL;
	new_polyhedron->face_class_begin.next	= &new_polyhedron->face_class_end;
	new_polyhedron->face_class_end  .prev	= &new_polyhedron->face_class_begin;
	new_polyhedron->face_class_end  .next	= NULL;
	return new_polyhedron;
}
static void make_cube(
	WEPolyhedron	*polyhedron)
{
	/*
	 *	This function accepts a pointer to an empty WEPolyhedron (i.e. the
	 *	WEPolyhedron data structure is allocated and the doubly linked lists
	 *	are initialized, but the WEPolyhedron has no vertices, edges or
	 *	faces), and sets it to be a cube of side twice CUBE_SIZE.  Each face
	 *	of the cube has its group_element field set to NULL to indicate that
	 *	it doesn't correspond to any element of the group.  The vertices,
	 *	faces and edges of the cube are indexed as shown in the following
	 *	diagram of a cut open cube.
	 *
	 *                        1-------5->-------3
	 *		                  |                 |
	 *		                  |                 |
	 *		                  2                 3
	 *		                  |        5        |
	 *		                  V                 V
	 *		                  |                 |
	 *		                  |                 |
	 *		1-------2->-------5-------7->-------7-------<-3-------3
	 *		|                 |                 |                 |
	 *		|                 |                 |                 |
	 *		^                 ^                 ^                 ^
	 *		|        2        |        1        |        3        |
	 *		8                 9                11                10
	 *		|                 |                 |                 |
	 *		|                 |                 |                 |
	 *		0-------0->-------4-------6->-------6-------<-1-------2
	 *		                  |                 |
	 *		                  |                 |
	 *		                  ^                 ^
	 *		                  |        4        |
	 *		                  0                 1
	 *		                  |                 |
	 *		                  |                 |
	 *                        0-------4->-------2
	 *		                  |                 |
	 *		                  |                 |
	 *		                  8                10
	 *		                  |        0        |
	 *		                  V                 V
	 *		                  |                 |
	 *		                  |                 |
	 *                        1-------5->-------3
	 */
	int			i,
				j,
				k;
	WEVertex	*initial_vertices[8];
	WEEdge		*initial_edges[12];
	WEFace		*initial_faces[6];
	const static int evdata[12][2] =
		{
			{0, 4},
			{2, 6},
			{1, 5},
			{3, 7},
			{0, 2},
			{1, 3},
			{4, 6},
			{5, 7},
			{0, 1},
			{4, 5},
			{2, 3},
			{6, 7}
		};
	const static int eedata[12][2][2] =
		{
			{{ 8,  4}, { 9,  6}},
			{{ 4, 10}, { 6, 11}},
			{{ 5,  8}, { 7,  9}},
			{{10,  5}, {11,  7}},
			{{ 0,  8}, { 1, 10}},
			{{ 8,  2}, {10,  3}},
			{{ 9,  0}, {11,  1}},
			{{ 2,  9}, { 3, 11}},
			{{ 4,  0}, { 5,  2}},
			{{ 0,  6}, { 2,  7}},
			{{ 1,  4}, { 3,  5}},
			{{ 6,  1}, { 7,  3}}
		};
	const static int efdata[12][2] =
		{
			{2, 4},
			{4, 3},
			{5, 2},
			{3, 5},
			{4, 0},
			{0, 5},
			{1, 4},
			{5, 1},
			{0, 2},
			{2, 1},
			{3, 0},
			{1, 3}
		};
	const static int fdata[6] = {4, 6, 0, 1, 0, 2};
	/*
	 *	If we were keeping track of the number of vertices, edges and faces,
	 *	we'd want to set the counts here.  But we're not, so we won't.
	 *
	 *	polyhedron->num_vertices	= 8;
	 *	polyhedron->num_edges		= 12;
	 *	polyhedron->num_faces		= 6;
	 */
	for (i = 0; i < 8; i++)
	{
		initial_vertices[i] = NEW_STRUCT(WEVertex);
		INSERT_BEFORE(initial_vertices[i], &polyhedron->vertex_list_end);
	}
	for (i = 0; i < 12; i++)
	{
		initial_edges[i] = NEW_STRUCT(WEEdge);
		INSERT_BEFORE(initial_edges[i], &polyhedron->edge_list_end);
	}
	for (i = 0; i < 6; i++)
	{
		initial_faces[i] = NEW_STRUCT(WEFace);
		INSERT_BEFORE(initial_faces[i], &polyhedron->face_list_end);
	}
	for (i = 0; i < 8; i++)
	{
		initial_vertices[i]->x[0] = 1.0;
		initial_vertices[i]->x[1] = (i & 4) ? CUBE_SIZE : -CUBE_SIZE;
		initial_vertices[i]->x[2] = (i & 2) ? CUBE_SIZE : -CUBE_SIZE;
		initial_vertices[i]->x[3] = (i & 1) ? CUBE_SIZE : -CUBE_SIZE;
	}
	for (i = 0; i < 12; i++)
	{
		for (j = 0; j < 2; j++)			/*	j = tail, tip	*/
			initial_edges[i]->v[j] = initial_vertices[evdata[i][j]];
		for (j = 0; j < 2; j++)			/*	j = tail, tip	*/
			for (k = 0; k < 2; k++)		/*	k = left, right	*/
				initial_edges[i]->e[j][k] = initial_edges[eedata[i][j][k]];
		for (j = 0; j < 2; j++)			/*	j = left, right	*/
			initial_edges[i]->f[j]	= initial_faces[efdata[i][j]];
	}
	for (i = 0; i < 6; i++)
	{
		initial_faces[i]->some_edge		= initial_edges[fdata[i]];
		initial_faces[i]->mate			= NULL;
		initial_faces[i]->group_element	= NULL;
	}
}
static FuncResult slice_polyhedron(
	WEPolyhedron	*polyhedron,
	MatrixPairList	*gen_list)
{
	MatrixPair	*matrix_pair;
	/*
	 *	Intersect the polyhedron with the pair of halfspaces corresponding
	 *	to each MatrixPair on gen_list, except for the identity.
	 *
	 *	Technical note:  intersect_with_halfspaces() may set some faces'
	 *	face->clean fields to FALSE.  We aren't using the face->clean field
	 *	at this point, so these operations are unnecessary but harmless.
	 *	check_faces() uses the face->clean fields, and calls low-level
	 *	functions which call intersect_with_halfspaces().
	 */
	for (matrix_pair = gen_list->begin.next;
		 matrix_pair != &gen_list->end;
		 matrix_pair = matrix_pair->next)
		if (o31_equal(matrix_pair->m[0], O31_identity, MATRIX_EPSILON) == FALSE)
			if (intersect_with_halfspaces(polyhedron, matrix_pair) == func_failed)
				return func_failed;
	return func_OK;
}
static FuncResult intersect_with_halfspaces(
	WEPolyhedron	*polyhedron,
	MatrixPair		*matrix_pair)
{
	/*
	 *	Intersect the polyhedron with the halfspaces corresponding
	 *	to each of the O31Matrices in the matrix_pair.  This will create
	 *	0, 1 or 2 new faces, depending on whether the hyperplanes actually
	 *	slice off a nontrivial part of the polyhedron.
	 */
	int		i;
	WEFace	*new_face[2];
	/*
	 *	If the given MatrixPair deviates too far from O(3,1), report a failure.
	 *	(It suffices to check matrix_pair->m[0], because matrix_pair->m[1] is
	 *	computed as the transpose of matrix_pair->m[0] with appropriate
	 *	elements negated.)
	 */
	if (o31_deviation(matrix_pair->m[0]) > DEVIATION_EPSILON)
		return func_failed;
	/*
	 *	To allow for orbifolds as well as manifolds, we must be prepared
	 *	for the possibility that matrix_pair->m[0] and matrix_pair->m[1]
	 *	define the same hyperplane, which will be its own mate.  Let
	 *	f0 and f1 be the isometries of hyperbolic 3-space defined by
	 *	matrix_pair->m[0] and matrix_pair->m[1], respectively.
	 *
	 *	Proposition.  The following are equivalent.
	 *
	 *	(1)	The hyperplanes defined by f0 and f1 are equal.
	 *
	 *	(2)	f0(origin) = f1(origin)
	 *
	 *	(3)	f0(f0(origin)) = origin
	 *
	 *	(4)		f0 is a half turn about an axis which passes orthogonally
	 *				through the midpoint of the segment connecting the
	 *				origin to f0(origin).
	 *		or
	 *			f0 is a reflection in the plane which passes orthogonally
	 *				through the midpoint of the segment connecting the
	 *				origin to f0(origin).
	 *		or
	 *			f0 is a reflection through the midpoint of the segment
	 *				connecting the origin to f0(origin).
	 *
	 *	(5)	f0 = f1
	 *
	 *	Proof.  Throughout this proof, keep in mind that we first gave the
	 *	basepoint a small random displacement, and then perhaps moved it
	 *	towards a point of maximum injectivity radius, so we may assume
	 *	that its images under the covering transformation group are distinct.
	 *
	 *	(1 => 2).  Obvious.
	 *
	 *	(2 => 3).  f0(f0(origin)) = f0(f1(origin)) = origin, because
	 *	f0 and f1 are inverses of one another.
	 *
	 *	(3 => 4).  f0 interchanges the origin and f0(origin).  Therefore
	 *	if fixes the midpoint M of the segment S connecting the origin
	 *	to f0(origin).  Furthermore, the plane P which passes through
	 *	M and is orthogonal to S will be taken to itself (setwise, but
	 *	probably not pointwise).  Consider the possibilites for the
	 *	action of f0 on P.
	 *
	 *	Case 1.  f0 preserves the orientation of hyperbolic 3-space, and
	 *	therefore reverses the orientation of P.  Because f0 fixes the
	 *	point M, it must act on P as a reflection in some line L which
	 *	contains M and is contained in P.  It follows that f0 acts on
	 *	hyperbolic 3-space as a half turn about the line L.
	 *
	 *	Case 2.  f0 reverses the orientation of hyperbolic 3-space, and
	 *	therefore preserves the orientation of P.  Because f0 fixes the
	 *	point M, it must act on P as a rotation about M through some angle
	 *	theta.  This implies that f0^2 acts on hyperbolic 3-space as a
	 *	rotation about the line containing S through an angle 2*theta.
	 *	But the nondegenerate choice of basepoint (cf. the comments at the
	 *	beginning of this proof) implies that 2*theta = 0 (mod 2pi).
	 *	So theta = 0 or pi (mod 2pi).  If theta = 0, f0 acts on hyperbolic
	 *	3-space as a reflection in the plane P.  If theta = pi, f0 acts on
	 *	hyperbolic 3-space as a reflection in the point M.
	 *
	 *	(4 => 5).  In each of the three cases lists (half turn about axis,
	 *	reflection in plane, reflection in point) f0 is its own inverse.
	 *	Therefore f0 = f1.
	 *
	 *	(5 => 1).  Trivial.
	 *
	 *	Q.E.D.
	 */
	if (same_image_of_origin(matrix_pair->m[0], matrix_pair->m[1]) == TRUE)
	{
		/*
		 *	The images of the origin are the same, so the above proposition
		 *	implies that the matrices must be the same as well.  As a
		 *	guard against errors, let's check that this really is the case.
		 */
		if (o31_equal(matrix_pair->m[0], matrix_pair->m[1], MATRIX_EPSILON) == FALSE)
			uFatalError("intersect_with_halfspaces", "Dirichlet_construction");
		/*
		 *	For documentation on these operations,
		 *	please see the generic case below.
		 */
		if (slice_with_hyperplane(polyhedron, matrix_pair->m[0], &new_face[0]) == func_failed)
			return func_failed;
		if (new_face[0] != NULL)
			new_face[0]->mate = new_face[0];
		return func_OK;
	}
	/*
	 *	Slice the polyhedron with each of the two hyperplanes, and
	 *	record the newly created face, if any.  If no new face is created,
	 *	slice_with_hyperplane() sets new_face[i] to NULL.  If error
	 *	conditions (e.g. due to roundoff error) are encountered, return
	 *	func_failed.
	 */
	for (i = 0; i < 2; i++)
		if (slice_with_hyperplane(polyhedron, matrix_pair->m[i], &new_face[i]) == func_failed)
			return func_failed;
	/*
	 *	The first hyperplane might have created a new face, only to have it
	 *	completely removed by the second hyperplane.  (Technical note:
	 *	this check relies on the fact that slice_with_hyperplane() frees the
	 *	faces it removes AFTER allocating the new face.)
	 */
	if (new_face[0] != NULL && face_still_exists(polyhedron, new_face[0]) == FALSE)
		new_face[0] = NULL;
	/*
	 *	Tell the new_faces about each other.
	 */
	for (i = 0; i < 2; i++)
		if (new_face[i] != NULL)
			new_face[i]->mate = new_face[!i];
	return func_OK;
}
static Boolean same_image_of_origin(
	O31Matrix	m0,
	O31Matrix	m1)
{
	int	i;
	for (i = 0; i < 4; i++)
		if (fabs(m0[i][0] - m1[i][0]) > MATRIX_EPSILON)
			return FALSE;
	return TRUE;
}
static FuncResult slice_with_hyperplane(
	WEPolyhedron	*polyhedron,
	O31Matrix		m,
	WEFace			**new_face)
{
	/*
	 *	Remove the parts of the polyhedron cut off by the hyperplane P
	 *	determined by the matrix m.  Set all fields except the mate field
	 *	of the newly created face, which is temporarily set to NULL here,
	 *	and set correctly by the calling routine.
	 */
	O31Vector		normal_vector;
	/*
	 *	Initialize *new_face to NULL, just in case we detect an error
	 *	condition and return before creating *new_face.
	 */
	*new_face = NULL;
	/*
	 *	Compute a vector normal to the hyperplane P determined by
	 *	the matrix m.  The normal vector is, of course, defined relative
	 *	to the Minkowski space metric, not the Euclidean metric.
	 */
	if (compute_normal_to_Dirichlet_plane(m, normal_vector) == func_failed)
		return func_failed;
	/*
	 *	Let P' be the restriction of the hyperplane P to the projective
	 *	model, that is, to the 3-space x[0] == 1.  Measure the distance
	 *	from P' to each vertex of the polyhedron, and store the result
	 *	in the vertex's distance_to_plane field.  (Actually we compute
	 *	numbers which are proportional to the vertices' distances to P',
	 *	but that's good enough.)  In addition, set each vertex's
	 *	which_side_of_plane field to +1, 0 or -1 according to whether,
	 *	after accounting for possible roundoff error, the distance_to_plane
	 *	is positive, zero or negative, respectively.
	 */
	compute_vertex_to_hyperplane_distances(polyhedron, normal_vector);
	/*
	 *	If no vertices have which_side_of_plane == +1, then there is
	 *	nothing to be cut off, so return func_OK immediately.
	 *	If no vertices have which_side_of_plane == -1, then we're
	 *	cutting off everything, which merits a call to uFatalError().
	 */
	if (positive_vertices_exist(polyhedron) == FALSE)
		return func_OK;
	/*
	 *	Introduce a new vertex whereever an edge crosses the plane P'.
	 *	Such edges can be recognized by the fact that one endpoint
	 *	has which_side_of_plane == +1 while the other has
	 *	which_side_of_plane == -1.
	 */
	cut_edges(polyhedron);
	/*
	 *	Introduce a new edge whereever a face crosses the plane P'.
	 *	If any face has more than two 0-vertices (as might occur
	 *	due to roundoff error) return func_failed.
	 */
	if (cut_faces(polyhedron) == func_failed)
		return func_failed;
	/*
	 *	Check that the curve we're cutting along is a single,
	 *	simple closed curve.
	 *
	 *	Proposition.  The cut locus divides the surface of the ball into
	 *	two combinatorial disks.  All the interior vertices on one side
	 *	are positive (which_side_of_plane == +1) and all those on the
	 *	other side are negative (which_side_of_plane == -1).
	 *
	 *	Proof.  check_topology_of_cut() insures that the cut locus is a
	 *	simple closed curve.  The Schoenflies theorem says that a simple
	 *	closed curve divides the surface of a 2-sphere into two disks.
	 *	The function positive_vertices_exist() (cf. above) has already
	 *	checked that both positive and negative vertices exist.  The
	 *	function cut_edges() (cf. above) guarantees that no edge connects
	 *	a positive vertex to a negative one.  Therefore it follows that
	 *	one of the Schoenflies disks contains only positive vertices in its
	 *	interior, and the other contains only negative vertices.  Q.E.D.
	 *
	 *	Corollary.  If we remove
	 *
	 *	(1)	all positive vertices,
	 *	(2) all edges incident to at least one positive vertex, and
	 *	(3)	all faces incident to at least one positive vertex,
	 *
	 *	and replace them with a single new face, the new domain will still
	 *	be a combinatorial ball.
	 *
	 *	Proof.  This is almost obvious as a corollary of the above
	 *	proposition.  We just need to verify that conditions (1)-(3)
	 *	characterize the vertices, edges and face in the interior of
	 *	the positive Schoenflies disk.
	 *
	 *	(1)	We've already seen that all interior vertices must be positive.
	 *
	 *	(2)	An interior edge can't have two vertices on the boundary of the
	 *		Schoenflies disk, because if it did it would be a 0-edge and
	 *		would be part of the cut locus.  Therefore at least one vertex
	 *		is positive.
	 *
	 *	(3)	If all the vertices of a given face were 0-vertices, its
	 *		boundary would have to be the entire cut locus, and no vertices
	 *		could exist in its interior.  This would contradict the
	 *		existence of both positive and negative vertices.  Therefore
	 *		at least one vertex of an interior face must be positive.
	 *
	 *	Q.E.D.
	 */
	if (check_topology_of_cut(polyhedron) == func_failed)
		return func_failed;
	/*
	 *	Allocate the new_face.
	 */
	*new_face = NEW_STRUCT(WEFace);
	/*
	 *	Set its mate field to NULL and its clean field to FALSE.
	 */
	(*new_face)->mate  = NULL;
	(*new_face)->clean = FALSE;
	/*
	 *	Allocate space for its group_element, and copy in the matrix m.
	 */
	(*new_face)->group_element = NEW_STRUCT(O31Matrix);
	o31_copy(*(*new_face)->group_element, m);
	/*
	 *	Throw away the vertices, edges and faces which are no longer
	 *	needed, and install the new face.
	 */
	install_new_face(polyhedron, *new_face);
	return func_OK;
}
static FuncResult compute_normal_to_Dirichlet_plane(
	O31Matrix	m,
	O31Vector	normal_vector)
{
	/*
	 *	Compute a vector normal to the hyperplane P determined by
	 *	the matrix m.  The normal vector is, of course, defined relative
	 *	to the Minkowski space metric, not the Euclidean metric.
	 *
	 *	The group element represented by the matrix m takes the basepoint
	 *	b = (1, 0, 0, 0) to one of its translates m(b).  The hyperplane P
	 *	determined by m has normal vector m(b) - b.  Note that m(b) is
	 *	just the first column of the matrix m.
	 *
	 *	For numerical reasons, we want to scale the normal vector so that
	 *	its largest entries have absolute value one.  This gives
	 *	compute_vertex_to_hyperplane_distances() a consistent idea of
	 *	what sort of accuracy to expect when it computes the inner product
	 *	of the normal vector with the vertices of the polyhedron.
	 *	The absolute values of the entries in the (unscaled) normal vector
	 *	grow exponentially with the translation distance, so without the
	 *	scaling the magnitude of the anticipated error would also grow
	 *	exponentially.  With the scaling, the anticipated error will be
	 *	on the order of DBL_EPSILON.  (This is just the error in the inner
	 *	product computation -- other errors are of course introduced
	 *	elsewhere!)
	 */
	int		i;
	double	max_abs;
	/*
	 *	Read m(b) from the first column of the matrix m.
	 */
	for (i = 0; i < 4; i++)
		normal_vector[i] = m[i][0];
	/*
	 *	Subtract off b = (1, 0, 0, 0) to get the normal_vector.
	 */
	normal_vector[0] -= 1.0;
	/*
	 *	Scale the normal_vector so that the largest absolute value of its
	 *	entries is one.
	 *
	 *	Technical digression.  Except for very small translation distances
	 *	it would be good enough to simply divide through by 
	 *	fabs(normal_vector[0]).  This is because normal_vector[] is
	 *	numerically similar to a lightlike vector, with a large [0]
	 *	component whose square almost cancels the squares of the [1], [2]
	 *	and [3] components.  (Proof: m(b) is timelike while m(b) - b =
	 *	m(b) - 1.0 is spacelike.)  Here, though, we'll take a more
	 *	pedestrian approach.
	 */
	/*
	 *	Compute the largest absolute value of an entry.
	 */
	max_abs = 0.0;
	for (i = 0; i < 4; i++)
		if (fabs(normal_vector[i]) > max_abs)
			max_abs = fabs(normal_vector[i]);
	/*
	 *	If max_abs is close to zero, something has gone wrong.
	 *	In particular, we don't allow m(b) = b.  For an orbifold, we should
	 *	have already moved the basepoint away from the singular set, and
	 *	as it moves towards a maximum of the injectivity radius it should
	 *	go still further from the singular set.
	 */
	if (max_abs < ERROR_EPSILON)
		return func_failed;
	/*
	 *	Divide the normal vector by max_abs.
	 */
	for (i = 0; i < 4; i++)
		normal_vector[i] /= max_abs;
	return func_OK;
}
static void compute_vertex_to_hyperplane_distances(
	WEPolyhedron	*polyhedron,
	O31Vector		normal_vector)
{
	WEVertex	*vertex;
	/*
	 *	Compute the inner product of each vertex's coordinates x[] with the
	 *	normal_vector.  Because each vertex lies in the 3-space x[0] == 1,
	 *	the inner product <x, normal_vector> is proportional to the
	 *	3-dimensional Euclidean distance in the projective model from the
	 *	vertex to the Dirichlet plane defined by the normal_vector.
	 *
	 *	For numerical considerations, see compute_normal_to_Dirichlet_plane()
	 *	above.  (I may eventually also have to do some additional numerical
	 *	thinking here.)
	 */
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
	{
		vertex->distance_to_plane = o31_inner_product(vertex->x, normal_vector);
		/*
		 *	Decide whether the vertex lies beyond (+1), beneath (-1),
		 *	or on (0) the Dirichlet plane.  Allow for possible roundoff
		 *	error.
		 */
		if (vertex->distance_to_plane > polyhedron->vertex_epsilon)
			vertex->which_side_of_plane = +1;
		else if (vertex->distance_to_plane < - polyhedron->vertex_epsilon)
			vertex->which_side_of_plane = -1;
		else
			vertex->which_side_of_plane = 0;
	}
}
static Boolean positive_vertices_exist(
	WEPolyhedron *polyhedron)
{
	WEVertex	*vertex;
	Boolean		positive_vertices_exist,
				negative_vertices_exist;
	positive_vertices_exist = FALSE;
	negative_vertices_exist = FALSE;
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
	{
		if (vertex->which_side_of_plane == +1)
			positive_vertices_exist = TRUE;
		if (vertex->which_side_of_plane == -1)
			negative_vertices_exist = TRUE;
	}
	if (negative_vertices_exist == FALSE)
		uFatalError("positive_vertices_exist", "Dirichlet_construction");
	return positive_vertices_exist;
}
static void cut_edges(
	WEPolyhedron	*polyhedron)
{
	WEEdge		*edge;
	int			i,
				j;
	double		t;
	O31Vector	cut_point;
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		for (i = 0; i < 2; i++)
			if (edge->v[ i]->which_side_of_plane == -1
			 && edge->v[!i]->which_side_of_plane == +1)
			{
				/*
				 *	Place the new vertex at the point where
				 *	the edge crosses the Dirichlet plane.
				 */
				t = (             0.0                - edge->v[tail]->distance_to_plane) /
					(edge->v[tip]->distance_to_plane - edge->v[tail]->distance_to_plane);
				for (j = 0; j < 4; j++)
					cut_point[j] = (1.0 - t) * edge->v[tail]->x[j]  +  t * edge->v[tip]->x[j];
				split_edge(edge, cut_point, TRUE);
			}
}
void split_edge(
	WEEdge		*old_edge,
	O31Vector	cut_point,
	Boolean		set_Dirichlet_construction_fields)
{
	/*
	 *	This function is also called in Dirichlet_extras.c.  94/10/4  JRW
	 */
	/*
	 *	Introduce a new vertex in old_edge at the cut_point.
	 *
	 *				before					after
	 *
	 *				\   /					\   /
	 *				 \ /					 \ /
	 *				  |						  |
	 *				  |					old	  |
	 *				  |					edge  ^
	 *				  |						  |
	 *			old	  |						  |	 new
	 *			edge  ^						  o vertex
	 *				  |						  |
	 *				  |					new	  |
	 *				  |					edge  ^
	 *				  |						  |
	 *				  |						  |
	 *				 / \					 / \
	 *				/   \					/   \
	 */
	WEEdge		*new_edge,
				*left_neighbor,
				*right_neighbor;
	WEVertex	*new_vertex;
	/*
	 *	Allocate space for the new_edge and new_vertex.
	 */
	new_edge	= NEW_STRUCT(WEEdge);
	new_vertex	= NEW_STRUCT(WEVertex);
	/*
	 *	Set the fields of the new_edge.
	 */
	new_edge->v[tail] = old_edge->v[tail];
	new_edge->v[tip]  = new_vertex;
	new_edge->e[tail][left]  = old_edge->e[tail][left];
	new_edge->e[tail][right] = old_edge->e[tail][right];
	new_edge->e[tip ][left]  = old_edge;
	new_edge->e[tip ][right] = old_edge;
	new_edge->f[left]  = old_edge->f[left];
	new_edge->f[right] = old_edge->f[right];
	/*
	 *	Adjust the fields of the old_edge.
	 */
	old_edge->v[tail] = new_vertex;
	old_edge->e[tail][left]  = new_edge;
	old_edge->e[tail][right] = new_edge;
	/*
	 *	Adjust the fields of the other two edges which "see" new_edge.
	 */
	left_neighbor = new_edge->e[tail][left];
	if (left_neighbor->v[tip] == new_edge->v[tail])
		left_neighbor->e[tip][left] = new_edge;
	else
	if (left_neighbor->v[tail] == new_edge->v[tail])
		left_neighbor->e[tail][right] = new_edge;
	else
		uFatalError("split_edge", "Dirichlet_construction");
	right_neighbor = new_edge->e[tail][right];
	if (right_neighbor->v[tip] == new_edge->v[tail])
		right_neighbor->e[tip][right] = new_edge;
	else
	if (right_neighbor->v[tail] == new_edge->v[tail])
		right_neighbor->e[tail][left] = new_edge;
	else
		uFatalError("split_edge", "Dirichlet_construction");
	/*
	 *	Set the fields for the new vertex.
	 */
	o31_copy_vector(new_vertex->x, cut_point);
	if (set_Dirichlet_construction_fields)
	{
		new_vertex->distance_to_plane	= 0.0;
		new_vertex->which_side_of_plane	= 0;
	}
	/*
	 *	Insert new_edge and new_vertex on their respective lists.
	 *	Note that the new_edge goes on the list just before the old_edge;
	 *	this is fine because we don't need to check the new_edge in the
	 *	loop in cut_edges() above, or in subdivide_edges_where_necessary()
	 *	in Dirichlet_extras.c.
	 */
	INSERT_BEFORE(new_edge, old_edge);
	INSERT_BEFORE(new_vertex, old_edge->v[tip]);
	/*
	 *	Dirichlet_construction.c isn't maintaining the faces' num_sides
	 *	fields, but Dirichlet_extras.c is.
	 */
	old_edge->f[left] ->num_sides++;
	old_edge->f[right]->num_sides++;
}
static FuncResult cut_faces(
	WEPolyhedron	*polyhedron)
{
	WEFace	*face;
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		if (cut_face_if_necessary(face, TRUE) == func_failed)
			return func_failed;
	return func_OK;
}
/*
 *	cut_face_if_necessary() returns func_failed if any topological
 *		abnormality is found.  See details below.
 *	It returns func_OK otherwise.
 */
FuncResult cut_face_if_necessary(
	WEFace	*face,
	Boolean	called_from_Dirichlet_construction)
{
	/*
	 *	Dirichlet_extras.c also calls cut_face_if_necessary().
	 *	The argument called_from_Dirichlet_construction tells who's called us,
	 *	so we can handle the face pairing accordingly.  94/10/5  JRW
	 */
	int		i,
			count;
	WEEdge	*edge,
			*edge_before_vertex[2],
			*edge_after_vertex[2],
			*temp_edge;
	WEEdge	*new_edge;
	WEFace	*new_face;
	/*
	 *	Edges passing from the negative to the positive side of the
	 *	hyperplane have already been cut in cut_edges(), so we expect
	 *	each face to be of one of the following types.  A "0-vertex" is
	 *	a vertex whose which_side_of_plane field is 0.
	 *
	 *	No 0-vertices.  The face should be left alone.
	 *
	 *	One 0-vertex.  The face should be left alone.
	 *
	 *	Two 0-vertices.
	 *
	 *		If the 0-vertices are adjacent, the face should be left alone.
	 *
	 *		If the 0-vertices are not adjacent, the face should be cut.
	 *
	 *	In each of the above cases, we return func_OK.
	 *	If more than two 0-vertices occur, we return func_failed.
	 *		(For example, roundoff errors in especially tiny faces might
	 *		cause a face to have three 0-vertices.)
	 */
	/*
	 *	To simplify the subsequent code, reorient the WEEdges so all are
	 *	directed counterclockwise around the face.
	 */
	all_edges_counterclockwise(face, FALSE);
	/*
	 *	Count the number of 0-vertices.
	 */
	count = 0;
	edge = face->some_edge;
	do
	{
		if (edge->v[tip]->which_side_of_plane == 0)
		{
			/*
			 *	If this is our third 0-vertex, something has gone
			 *	wrong with the roundoff errors.
			 */
			if (count == 2)
				return func_failed;
			/*
			 *	Note which edge precedes the 0-vertex.
			 */
			edge_before_vertex[count] = edge;
			count++;
		}
		edge = edge->e[tip][left];
	} while (edge != face->some_edge);
	/*
	 *	If there are fewer than two 0-vertices, return.
	 */
	if (count < 2)
		return func_OK;
	/*
	 *	Note which edges follow the 0-vertices.
	 */
	for (i = 0; i < 2; i++)
		edge_after_vertex[i] = edge_before_vertex[i]->e[tip][left];
	/*
	 *	If the 0-vertices are adjacent, return.
	 */
	for (i = 0; i < 2; i++)
		if (edge_after_vertex[i] == edge_before_vertex[!i])
			return func_OK;
	/*
	 *	Cut the face in two by introducing a new edge connecting
	 *	the two 0-vertices.
	 *
	 *	Before:
	 *			      /               \
	 *	edge_before_vertex[1]          \
	 *			    /                   \_
	 *			  |/_                   |\	edge_after_vertex[0]
	 *			  /                       \
	 *			 /                         \
	 *			/           face            \
	 *			\                           /
	 *			 \                         /
	 *			  \                      _/
	 *			  _\|                    /|	edge_before_vertex[0]
	 *			    \                   /
	 *	edge_after_vertex[1]           /
	 *			      \               /
	 *
	 *
	 *	After:
	 *			      /               \
	 *	edge_before_vertex[1]          \
	 *			    /                   \_
	 *			  |/_                   |\	edge_after_vertex[0]
	 *			  /       new_face        \
	 *			 /                         \
	 *			/____________/______________\
	 *			\            \  new_edge    /
	 *			 \                         /
	 *			  \        face          _/
	 *			  _\|                    /|	edge_before_vertex[0]
	 *			    \                   /
	 *	edge_after_vertex[1]           /
	 *			      \               /
	 */
	/*
	 *	First, for convenience, make sure the lower half is the
	 *	half we want to keep.  (Because there are precisely two
	 *	incident 0-vertices, and no edge connects minus directly to plus,
	 *	we know that all the signs on a given side of the new_edge
	 *	must be the same.)
	 */
	if (edge_before_vertex[0]->v[tail]->which_side_of_plane == -1
	 && edge_after_vertex [0]->v[tip] ->which_side_of_plane == +1)
	{
		/*
		 *	Great.  This is what we want.  Do nothing.
		 */
	}
	else
	if (edge_before_vertex[0]->v[tail]->which_side_of_plane == +1
	 && edge_after_vertex [0]->v[tip] ->which_side_of_plane == -1)
	{
		/*
		 *	Swap the sides.
		 */
		temp_edge				= edge_before_vertex[0];
		edge_before_vertex[0]	= edge_before_vertex[1];
		edge_before_vertex[1]	= temp_edge;
		temp_edge				= edge_after_vertex[0];
		edge_after_vertex[0]	= edge_after_vertex[1];
		edge_after_vertex[1]	= temp_edge;
	}
	else
		/*
		 *	The two sides have the same signs.  Something has gone wrong.
		 */
		return func_failed;
	/*
	 *	Now allocate and install the new_edge and new_face.
	 */
	new_edge = NEW_STRUCT(WEEdge);
	new_face = NEW_STRUCT(WEFace);
	new_edge->v[tail]	= edge_before_vertex[0]->v[tip];
	new_edge->v[tip]	= edge_before_vertex[1]->v[tip];
	new_edge->e[tail][left]		= edge_before_vertex[0];
	new_edge->e[tail][right]	= edge_after_vertex[0];
	new_edge->e[tip] [left]		= edge_after_vertex[1];
	new_edge->e[tip] [right]	= edge_before_vertex[1];
	edge_before_vertex[0]->e[tip] [left]	= new_edge;
	edge_after_vertex [0]->e[tail][left]	= new_edge;
	edge_before_vertex[1]->e[tip] [left]	= new_edge;
	edge_after_vertex [1]->e[tail][left]	= new_edge;
	for (edge = edge_after_vertex[0]; edge != new_edge; edge = edge->e[tip][left])
		edge->f[left] = new_face;
	new_edge->f[left]	= face;
	new_edge->f[right]	= new_face;
	new_face->some_edge = new_edge;
	face    ->some_edge = new_edge;
	/*
	 *	How we handle the face pairings depends on whether we were called
	 *	from Dirichlet_construction.c or Dirichlet_extras.c.
	 */
	if (called_from_Dirichlet_construction == TRUE)
	{
		/*
		 *	new_face will be removed when we clear out all the vertices, edges
		 *	and faces detached by the cut.  So we set its mate and group_element
		 *	fields to NULL.
		 *
		 *	face keeps its same mate, which may or may not survive the cut.
		 *	(But at least if face->mate gets cut in two, the half that's
		 *	retained will still be called face->mate.)
		 */
		new_face->mate			= NULL;
		new_face->group_element	= NULL;
		/*
		 *	If face has a mate, we can no longer guarantee that it's a subset
		 *	of face under the action of the group_element.
		 */
		if (face->mate != NULL)
			face->mate->clean = FALSE;
	}
	else	/* called from Dirichlet_extras.c */
	{
		face->mate		= new_face;
		new_face->mate	= face;
		new_face->group_element = NEW_STRUCT(O31Matrix);
		o31_copy(*new_face->group_element, *face->group_element);
	}
	/*
	 *	We may insert new_face before face, because there is no need for
	 *	cut_faces() to check it.  new_edge may go anywhere.
	 */
	INSERT_BEFORE(new_edge, edge_before_vertex[0]);
	INSERT_BEFORE(new_face, face);
	return func_OK;
}
void all_edges_counterclockwise(
	WEFace	*face,
	Boolean	redirect_neighbor_fields)
{
	WEEdge	*edge;
	edge = face->some_edge;
	do
	{
		if (edge->f[left] != face)
			redirect_edge(edge, redirect_neighbor_fields);
		edge = edge->e[tip][left];
	} while (edge != face->some_edge);
}
void redirect_edge(
	WEEdge	*edge,
	Boolean	redirect_neighbor_fields)
{
	WEVertex	*temp_vertex;
	WEEdge		*temp_edge;
	WEFace		*temp_face;
	/*
	 *	Swap the tip and tail vertices.
	 */
	temp_vertex		= edge->v[tail];
	edge->v[tail]	= edge->v[tip];
	edge->v[tip]	= temp_vertex;
	/*
	 *	Swap the tail-left and tip-right edges.
	 */
	temp_edge				= edge->e[tail][left];
	edge->e[tail][left]		= edge->e[tip][right];
	edge->e[tip][right]		= temp_edge;
	/*
	 *	Swap the tail-right and tip-left edges.
	 */
	temp_edge				= edge->e[tail][right];
	edge->e[tail][right]	= edge->e[tip][left];
	edge->e[tip][left]		= temp_edge;
	/*
	 *	Swap the left and right faces.
	 */
	temp_face		= edge->f[left];
	edge->f[left]	= edge->f[right];
	edge->f[right]	= temp_face;
	/*
	 *	When called from Dirichlet_extras.c,
	 *	we need to preserve some additional fields.
	 */
	if (redirect_neighbor_fields)
	{
		WEEdge		*nbr_edge;
		WEEdgeSide	side,
					nbr_side;
		WEEdge		*temp_edge;
		Boolean		temp_boolean;
		/*
		 *	Note that the following code works even if
		 *
		 *	(1)  the two sides of the edge are glued to each other, or
		 *
		 *	(2)  one or both of the sides is glued to itself.
		 *
		 *	To convince yourself of this, trace through the following code
		 *	and note that in the degenerate cases the preserves_sides and
		 *	preserves_direction flags each get toggled twice.
		 */
		/*
		 *	Fix up the neighbors.
		 *	Toggle their preserves_sides and preserves_direction fields,
		 *	but leave their neighbor and preserves_orientation field alone.
		 */
		for (side = 0; side < 2; side++)
		{
			nbr_edge = edge->neighbor[side];
			nbr_side = (edge->preserves_sides[side] ? side : !side);
			nbr_edge->preserves_sides[nbr_side]
				= ! nbr_edge->preserves_sides[nbr_side];
			nbr_edge->preserves_direction[nbr_side]
				= ! nbr_edge->preserves_direction[nbr_side];
		}
		/*
		 *	Toggle our own preserves_sides and preserves_direction fields.
		 */
		for (side = 0; side < 2; side++)
		{
			edge->preserves_sides[side]		= ! edge->preserves_sides[side];
			edge->preserves_direction[side]	= ! edge->preserves_direction[side];
		}
		/*
		 *	Swap the left and right values.
		 */
		temp_edge				= edge->neighbor[left];
		edge->neighbor[left]	= edge->neighbor[right];
		edge->neighbor[right]	= temp_edge;
		temp_boolean						= edge->preserves_sides[left];
		edge->preserves_sides[left]			= edge->preserves_sides[right];
		edge->preserves_sides[right]		= temp_boolean;
		temp_boolean						= edge->preserves_direction[left];
		edge->preserves_direction[left]		= edge->preserves_direction[right];
		edge->preserves_direction[right]	= temp_boolean;
		temp_boolean						= edge->preserves_orientation[left];
		edge->preserves_orientation[left]	= edge->preserves_orientation[right];
		edge->preserves_orientation[right]	= temp_boolean;
	}
}
static FuncResult check_topology_of_cut(
	WEPolyhedron	*polyhedron)
{
	int			num_zero_edges,
				count;
	WEVertex	*vertex,
				*tip_vertex;
	WEEdge		*edge,
				*starting_edge;
	/*
	 *	Define a 0-vertex to be a vertex with which_side_of_plane == 0.
	 *
	 *	Define a 0-edge to be an edge incident to two 0-vertices.
	 *
	 *	We'll be cutting along the union of the 0-edges.  We want to
	 *	verify that it's a simple closed curve.  We do this in two steps:
	 *
	 *	(1)	Verify that precisely two 0-edges are incident to each 0-vertex.
	 *		This insures that the cut locus is a (possibly empty) union of
	 *		simple closed curves.
	 *
	 *	(2)	Verify that the cut locus is connected.
	 */
	/*
	 *	Initialize the zero_order fields to 0.
	 */
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
		vertex->zero_order = 0;
	/*
	 *	Count the number of 0-edges adjacent to each 0-vertex.
	 *
	 *	While we're at it, keep a global count of the number of the
	 *	number of 0-edges, for use below in checking that the cut locus
	 *	is connected.
	 */
	num_zero_edges = 0;
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		if (edge->v[tail]->which_side_of_plane == 0
		 && edge->v[tip] ->which_side_of_plane == 0)
		{
			edge->v[tail]->zero_order++;
			edge->v[tip] ->zero_order++;
			num_zero_edges++;
		}
	/*
	 *	Check that all 0-vertices are incident to precisely two 0-edges.
	 */
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
		if (vertex->which_side_of_plane == 0
		 && vertex->zero_order != 2)
			return func_failed;
	/*
	 *	We now know that the cut locus is a union of simple
	 *	closed curves.  We want to check that it is connected.
	 */
	/*
	 *	Find a starting edge.
	 */
	starting_edge = NULL;
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		if (edge->v[tail]->which_side_of_plane == 0
		 && edge->v[tip] ->which_side_of_plane == 0)
		{
			starting_edge = edge;
			break;
		}
	/*
	 *	If we didn't find a starting edge, something went horribly wrong.
	 */
	if (starting_edge == NULL)
		uFatalError("check_topology_of_cut", "Dirichlet_construction");
	/*
	 *	Traverse the cut locus, beginning at the starting_edge.
	 *	Count the number of edges in the locus.
	 */
	count = 0;
	edge = starting_edge;
	do
	{
		/*
		 *	Count this edge.
		 */
		count++;
		/*
		 *	Move on to the next edge.
		 *	To do so, rotate clockwise until we find another 0-edge.
		 *	For convenience, we reorient the edges as necessary as we
		 *	go along, so they always point towards the tip_vertex.
		 *	Once we finally get to another 0-edge we reorient it once
		 *	more, to preserve the direction of the curve (i.e. we now
		 *	want to point away from the old tip_vertex, and towards
		 *	what will become the new tip_vertex).
		 */
		tip_vertex = edge->v[tip];
		do
		{
			edge = edge->e[tip][left];
			if (edge->v[tip] != tip_vertex)
				redirect_edge(edge, FALSE);
		} while (edge->v[tail]->which_side_of_plane != 0);
		redirect_edge(edge, FALSE);
	} while (edge != starting_edge);
	/*
	 *	The cut locus will be connected iff count == num_zero_edges.
	 */
	if (count == num_zero_edges)
		return func_OK;
	else
		return func_failed;
}
static void install_new_face(
	WEPolyhedron	*polyhedron,
	WEFace			*new_face)
{
	WEFace		*face;
	WEEdge		*edge;
	WEVertex	*vertex;
	WEFace		*dead_face;
	WEEdge		*dead_edge;
	WEVertex	*dead_vertex;
	WEVertex	*tip_vertex;
	WEEdge		*left_edge,
				*right_edge;
	int			i;
	/*
	 *	The overall plan here is to remove those faces, edges and vertices
	 *	(in that order) which have been cut off by the slicing plane,
	 *	and install the new_face.
	 */
	/*
	 *	We'll want to remove precisely those faces which are incident to
	 *	at least one positive vertex.  (For a proof, see the corollary
	 *	in the documentation in slice_with_hyperplane().)  To find these
	 *	faces, we'll scan the list of edges, and whenever an edge is
	 *	incident to at least one positive vertex, we'll see the neighboring
	 *	faces' to_be_removed fields to TRUE.  (This is easier than
	 *	traversing the perimeter of each face explicitly, worrying about
	 *	the directions of the edges.)
	 */
	/*
	 *	Initialize all faces' to_be_removed fields to FALSE.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		face->to_be_removed = FALSE;
	/*
	 *	Scan the edge list to locate edges -- and hence faces -- which
	 *	are incident to at least one positive vertex.
	 */
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		if (edge->v[tail]->which_side_of_plane == +1
		 || edge->v[tip] ->which_side_of_plane == +1)
		{
			edge->f[left] ->to_be_removed = TRUE;
			edge->f[right]->to_be_removed = TRUE;
		}
	/*
	 *	Set the edge->f[] fields for edges which will be incident to the
	 *	new_face.  Also make sure the new_face's some_edge field sees
	 *	an appropriate edge (note that it's easier to keep setting
	 *	new_face->some_edge over and over than to check whether it's
	 *	already been set).
	 */
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		if (edge->v[tail]->which_side_of_plane == 0
		 && edge->v[tip] ->which_side_of_plane == 0)
			for (i = 0; i < 2; i++)		/*  i = left, right  */
				if (edge->f[i]->to_be_removed == TRUE)
				{
					edge->f[i] = new_face;
					new_face->some_edge = edge;
				}
	/*
	 *	Delete the faces which are being removed.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		if (face->to_be_removed == TRUE)
		{
			/*
			 *	If the face has a mate, set the mate's mate field to NULL
			 *	and its clean field to FALSE.
			 */
			if (face->mate != NULL && face->mate != face)
			{
				face->mate->mate  = NULL;
				face->mate->clean = FALSE;
			}
			/*
			 *	If the face has a group_element, free it.
			 */
			if (face->group_element != NULL)
				my_free(face->group_element);
			/*
			 *	Remove the face from the doubly-linked list.  First set
			 *	face to face->prev so the loop will continue correctly.
			 */
			dead_face = face;
			face = face->prev;
			REMOVE_NODE(dead_face);
			/*
			 *	Delete the face.
			 */
			my_free(dead_face);
		}
	/*
	 *	Delete the edges which are being removed.
	 */
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		if (edge->v[tail]->which_side_of_plane == +1
		 || edge->v[tip] ->which_side_of_plane == +1)
		{
			/*
			 *	If this edge is indicent to a 0-vertex,
			 *	fix up the neighbors' edge->e[][] fields.
			 */
			/*
			 *	If it's the tail which is incident to a 0-vertex, call
			 *	redirect_edge().  This simplifies the subsequent code,
			 *	because we may assume that if a 0-vertex is incident to the
			 *	edge, it will be at the tip.  Note that redirect_edge() will
			 *	be interchanging some dangling WEFace pointers, but that's OK.
			 */
			if (edge->v[tail]->which_side_of_plane == 0)
				redirect_edge(edge, FALSE);
			/*
			 *	Is there a 0-vertex at the tip?
			 */
			tip_vertex = edge->v[tip];
			if (tip_vertex->which_side_of_plane == 0)
			{
				/*
				 *	Who are the neighbors?
				 */
				left_edge  = edge->e[tip][left];
				right_edge = edge->e[tip][right];
				/*
				 *	Let the left_edge see the right_edge.
				 */
				if (left_edge->v[tip]  == tip_vertex)
					left_edge->e[tip][right] = right_edge;
				else
				if (left_edge->v[tail] == tip_vertex)
					left_edge->e[tail][left] = right_edge;
				else
					uFatalError("install_new_face", "Dirichlet_construction");
				/*
				 *	Let the right_edge see the left_edge.
				 */
				if (right_edge->v[tip]  == tip_vertex)
					right_edge->e[tip][left]   = left_edge;
				else
				if (right_edge->v[tail] == tip_vertex)
					right_edge->e[tail][right] = left_edge;
				else
					uFatalError("install_new_face", "Dirichlet_construction");
			}
			/*
			 *	Remove the edge from the doubly-linked list.  First set
			 *	edge to edge->prev so the loop will continue correctly.
			 */
			dead_edge = edge;
			edge = edge->prev;
			REMOVE_NODE(dead_edge);
			/*
			 *	Delete the edge.
			 */
			my_free(dead_edge);
		}
	/*
	 *	Delete the vertices which are being removed.
	 */
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
		if (vertex->which_side_of_plane == +1)
		{
			/*
			 *	Remove the vertex from the doubly-linked list.  First set
			 *	vertex to vertex->prev so the loop will continue correctly.
			 */
			dead_vertex = vertex;
			vertex = vertex->prev;
			REMOVE_NODE(dead_vertex);
			/*
			 *	Delete the vertex.
			 */
			my_free(dead_vertex);
		}
	/*
	 *	Install the new_face in the doubly-linked list.
	 */
	INSERT_BEFORE(new_face, &polyhedron->face_list_end);
}
static Boolean face_still_exists(
	WEPolyhedron	*polyhedron,
	WEFace			*face0)
{
	/*
	 *	Check whether face0 is still part of the polyhedron.
	 */
	WEFace	*face;
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		if (face == face0)
			return TRUE;
	return FALSE;
}
static Boolean has_hyperideal_vertices(
	WEPolyhedron	*polyhedron)
{
	WEVertex	*vertex;
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
		if (o31_inner_product(vertex->x, vertex->x) > HYPERIDEAL_EPSILON)
			return TRUE;
	return FALSE;
}
static void compute_all_products(
	WEPolyhedron	*polyhedron,
	MatrixPairList	*product_list)
{
	MatrixPairList	current_list;
	MatrixPair		*product_tree;
	/*
	 *	Compute a MatrixPairList containing all current face pairings.
	 */
	poly_to_current_list(polyhedron, ¤t_list);
	/*
	 *	Compute all products of two group elements from the current_list.
	 *
	 *	Record the products on a binary tree instead of a doubly-linked list,
	 *	so that:
	 *
	 *	(1)	We can check for duplications in log(n) time instead
	 *		of linear time.
	 *
	 *	(2)	The final list will be sorted by height (cf. the height field
	 *		in WEFace).  My expectation is that intersecting the polyhedron
	 *		with the lowest height group elements first will minimize the
	 *		time spent slicing.  That is, we'll make the most important
	 *		cuts first, rather than creating a lot of more distant faces
	 *		which are themselves later cut off.  (I don't have any firm
	 *		evidence that this will be the case, but it seems likely, and
	 *		in any case it costs us nothing.)
	 */
	current_list_to_product_tree(¤t_list, &product_tree);
	/*
	 *	Transfer the products from the product_tree to the product_list.
	 */
	product_tree_to_product_list(product_tree, product_list);
	/*
	 *	We're done with the current_list;
	 */
	free_matrix_pairs(¤t_list);
}
static void poly_to_current_list(
	WEPolyhedron	*polyhedron,
	MatrixPairList	*current_list)
{
	WEFace		*face;
	MatrixPair	*matrix_pair;
	/*
	 *	Initialize the current_list.
	 */
	current_list->begin.prev = NULL;
	current_list->begin.next = ¤t_list->end;
	current_list->end  .prev = ¤t_list->begin;
	current_list->end  .next = NULL;
	/*
	 *	Use the face->copied fields to avoid copying both a face and its
	 *	mate as separate MatrixPairs.  First initialize all face->copied
	 *	fields to FALSE.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		face->copied = FALSE;
	/*
	 *	Go down the list of faces, skipping faces of the original cube.
	 *	For each face which hasn't already been done, copy it's group_element
	 *	to a MatrixPair and append the MatrixPair to the current_list.
	 *	Set the face->copied field to TRUE, and if the face has a mate,
	 *	set its mate's copied field to TRUE too.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		if (face->group_element != NULL  &&  face->copied == FALSE)
		{
			matrix_pair = NEW_STRUCT(MatrixPair);
			o31_copy(matrix_pair->m[0], *face->group_element);
			o31_invert(matrix_pair->m[0], matrix_pair->m[1]);
			matrix_pair->height = matrix_pair->m[0][0][0];
			INSERT_BEFORE(matrix_pair, ¤t_list->end);
			face->copied = TRUE;
			if (face->mate != NULL)
				face->mate->copied = TRUE;
		}
}
static void current_list_to_product_tree(
	MatrixPairList	*current_list,
	MatrixPair		**product_tree)
{
	MatrixPair		*matrix_pair_a,
					*matrix_pair_b;
	int				i,
					j;
	O31Matrix		product;
	/*
	 *	Initialize the product_tree to NULL.
	 */
	*product_tree = NULL;
	/*	For each pair {a, A}, {b, B} of MatrixPairs on the current_list,
	 *	add the MatrixPairs {ab, BA}, {aB, bA}, {Ab, Ba} and {AB, ba}
	 *	to product_list if they aren't already there.
	 *
	 *	Note that once we've considered the ordered pair ({a, A}, {b, B})
	 *	of MatrixPairs, there is no need to consider the ordered pair
	 *	({b, B}, {a, A}).  It would generate the MatrixPairs
	 *	{ba, AB}, {bA, aB}, {Ba, Ab} and {BA, ab}, which are the same as
	 *	those generated by ({a, A}, {b, B}), only within each resulting
	 *	MatrixPair the order of the O31Matrices is reversed.  Therefore we
	 *	consider only order pairs ({a, A}, {b, B}) where the the MatrixPair
	 *	{a, A} occurs no later than {b, B} in the current_list.
	 */
	for (matrix_pair_a = current_list->begin.next;
		 matrix_pair_a != ¤t_list->end;
		 matrix_pair_a = matrix_pair_a->next)
		for (matrix_pair_b = matrix_pair_a;
			 matrix_pair_b != ¤t_list->end;
			 matrix_pair_b = matrix_pair_b->next)
			for (i = 0; i < 2; i++)			/*	which element of matrix_pair_a	*/
				for (j = 0; j < 2; j++)		/*	which element of matrix_pair_b	*/
				{
					precise_o31_product(matrix_pair_a->m[i], matrix_pair_b->m[j], product);
					if (already_on_product_tree(product, *product_tree) == FALSE)
						add_to_product_tree(product, product_tree);
				}
}
static Boolean already_on_product_tree(
	O31Matrix	product,
	MatrixPair	*product_tree)
{
	MatrixPair	*subtree_stack,
				*subtree;
	int			i;
	/*
	 *	Does the O31Matrix product already appear on the product_tree?
	 */
	/*
	 *	Implement the recursive search algorithm using our own stack
	 *	rather than the system stack, to avoid the possibility of a
	 *	stack/heap collision.
	 */
	/*
	 *	Initialize the stack to contain the whole product_tree.
	 */
	subtree_stack = product_tree;
	if (product_tree != NULL)
		product_tree->next_subtree = NULL;
	/*
	 *	Process the subtrees on the stack one at a time.
	 */
	while (subtree_stack != NULL)
	{
		/*
		 *	Pull a subtree off the stack.
		 */
		subtree					= subtree_stack;
		subtree_stack			= subtree_stack->next_subtree;
		subtree->next_subtree	= NULL;
		/*
		 *	If the product could possible appear on the left and/or right
		 *	subtrees, add them to the stack.
		 */
		if (subtree->left_child != NULL
		 && product[0][0] < subtree->height + MATRIX_EPSILON)
		{
			subtree->left_child->next_subtree = subtree_stack;
			subtree_stack = subtree->left_child;
		}
		if (subtree->right_child != NULL
		 && product[0][0] > subtree->height - MATRIX_EPSILON)
		{
			subtree->right_child->next_subtree = subtree_stack;
			subtree_stack = subtree->right_child;
		}
		/*
		 *	Check the subtree's root.
		 */
		for (i = 0; i < 2; i++)
			if (o31_equal(product, subtree->m[i], MATRIX_EPSILON) == TRUE)
				return TRUE;
	}
	return FALSE;
}
static void add_to_product_tree(
	O31Matrix	product,
	MatrixPair	**product_tree)
{
	MatrixPair	**home;
	double		product_height;
	/*
	 *	We need to find a home for the O31Matrix product on the product_tree.
	 *
	 *	We assume the product does not already appear on the product_tree.
	 *	(The call to already_on_product_tree() must return FALSE in
	 *	current_list_to_product_tree() for this function to be called.)
	 */
	product_height = product[0][0];
	home = product_tree;
	while (*home != NULL)
	{
		if (product_height < (*home)->height)
			home = &(*home)->left_child;
		else
			home = &(*home)->right_child;
	}
	(*home) = NEW_STRUCT(MatrixPair);
	o31_copy((*home)->m[0], product);
	o31_invert((*home)->m[0], (*home)->m[1]);
	(*home)->height = (*home)->m[0][0][0];
	(*home)->left_child		= NULL;
	(*home)->right_child	= NULL;
	(*home)->next_subtree	= NULL;
	(*home)->prev			= NULL;
	(*home)->next			= NULL;
}
static void product_tree_to_product_list(
	MatrixPair		*product_tree,
	MatrixPairList	*product_list)
{
	/*
	 *	Initialize the product_list.
	 */
	product_list->begin.prev = NULL;
	product_list->begin.next = &product_list->end;
	product_list->end  .prev = &product_list->begin;
	product_list->end  .next = NULL;
	/*
	 *	Transfer the MatrixPairs from the product_tree to the product_list,
	 *	maintaining their order.  Set the left_child and right_child fields
	 *	to NULL as we go along.
	 */
	append_tree_to_list(product_tree, &product_list->end);
}
static void append_tree_to_list(
	MatrixPair	*product_tree,
	MatrixPair	*list_end)
{
	MatrixPair	*subtree_stack,
				*subtree;
	/*
	 *	Implement the recursive tree traversal using our own stack
	 *	rather than the system stack, to avoid the possibility of a
	 *	stack/heap collision.
	 */
	/*
	 *	Initialize the stack to contain the whole product_tree.
	 */
	subtree_stack = product_tree;
	if (product_tree != NULL)
		product_tree->next_subtree = NULL;
	/*
	 *	Process the subtrees on the stack one at a time.
	 */
	while (subtree_stack != NULL)
	{
		/*
		 *	Pull a subtree off the stack.
		 */
		subtree					= subtree_stack;
		subtree_stack			= subtree_stack->next_subtree;
		subtree->next_subtree	= NULL;
		/*
		 *	If it has no further subtrees, append it to the list.
		 *
		 *	Otherwise break it into three chunks:
		 *
		 *		the left subtree
		 *		this node
		 *		the right subtree
		 *
		 *	and push them onto the stack in reverse order, so that they'll
		 *	come off in the correct order.  Set this node's left_child and
		 *	right_child fields to NULL, so the next time it comes off the
		 *	stack we'll know the subtrees have been accounted for.
		 */
		if (subtree->left_child == NULL  &&  subtree->right_child == NULL)
		{
			INSERT_BEFORE(subtree, list_end);
		}
		else
		{
			/*
			 *	Push the right subtree (if any) onto the stack.
			 */
			if (subtree->right_child != NULL)
			{
				subtree->right_child->next_subtree = subtree_stack;
				subtree_stack = subtree->right_child;
				subtree->right_child = NULL;
			}
			/*
			 *	Push this node onto the stack.
			 *	(Its left_child and right_child fields will soon be NULL.)
			 */
			subtree->next_subtree = subtree_stack;
			subtree_stack = subtree;
			/*
			 *	Push the left subtree (if any) onto the stack.
			 */
			if (subtree->left_child != NULL)
			{
				subtree->left_child->next_subtree = subtree_stack;
				subtree_stack = subtree->left_child;
				subtree->left_child = NULL;
			}
		}
	}
}
static FuncResult check_faces(
	WEPolyhedron	*polyhedron)
{
	WEFace		*face;
	Boolean		face_was_pared;
	/*
	 *	The face->clean fields keep track of which faces are known to be
	 *	subsets of their mates under the action of the group_element.
	 *	When all face->clean fields are TRUE, we've found the Dirichlet
	 *	domain.
	 */
	/*
	 *	Initialize all face->clean fields to FALSE.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		face->clean = FALSE;
	/*
	 *	Go down the doubly-linked list of faces, looking for a dirty one.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		/*
		 *	If a dirty one is found . . .
		 */
		if (face->clean == FALSE)
		{
			/*
			 *	. . . see whether some group element will pare away
			 *	some or all of it.
			 *
			 *	There are two possibilities.
			 *
			 *	(1)	The face was clean after all, and we just didn't know it.
			 *		In this case, pare_face() sets face->clean to TRUE and
			 *		*face_was_pared to FALSE, and returns func_OK.  The
			 *		polyhedron remains unchanged.  We keep going with the
			 *		for(;;) loop.
			 *
			 *	(2)	The face really was dirty.  In this case, pare_face()
			 *		slices the polyhedron with a plane which lops off a
			 *		nontrivial piece.  If pare_face() encounters topological
			 *		problems due to roundoff error, it returns func_failed,
			 *		and so do we.  Otherwise it sets the face->clean fields
			 *		to FALSE for all affected faces, sets *face_was_pared to
			 *		TRUE, and returns func_OK.  At this point we have no
			 *		idea which faces have been eliminated entirely, so we
			 *		restart the for(;;) loop.
			 */
			if (pare_face(face, polyhedron, &face_was_pared) == func_failed)
				return func_failed;
			if (face_was_pared == TRUE)
				face = &polyhedron->face_list_begin;
		}
	/*
	 *	The above for(;;) loop will terminate only when all the face->clean
	 *	fields are TRUE.  So we can return func_OK.
	 */
	return func_OK;
}
static FuncResult pare_face(
	WEFace			*face,
	WEPolyhedron	*polyhedron,
	Boolean			*face_was_pared)
{
	/*
	 *	pare_face()'s mission is to decide whether face is a subset of its
	 *	mate under the action of the group_element.  (If its mate does not
	 *	exist, then clearly it's not a subset.)
	 *
	 *	If face is a subset of its mate, set face->clean to TRUE and
	 *	*face_was_pared to FALSE.  (Setting *face_was_pared to FALSE
	 *	signifies that we didn't have to do anything:  the face was
	 *	already a subset of its mate under the action of the group_element,
	 *	even though we didn't know it.)
	 *
	 *	If face is not a subset of its mate, find a new group element
	 *	which cuts off a part of face.  Leave face->clean FALSE, and
	 *	set *face_was_pared to TRUE.
	 *
	 *	If we encounter topological problems due to roundoff error,
	 *	return func_failed.  Otherwise return func_OK.
	 *
	 *	We split into two cases, according to whether face has a mate.
	 */
	if (face->mate != NULL)
		return pare_mated_face(face, polyhedron, face_was_pared);
	else
		return pare_mateless_face(face, polyhedron, face_was_pared);
}
static FuncResult pare_mated_face(
	WEFace			*face,
	WEPolyhedron	*polyhedron,
	Boolean			*face_was_pared)
{
	/*
	 *	The face and its mate are both convex sets -- both are
	 *	intersections of half planes.  So to check that the face is
	 *	contained in the image of its mate under the action of the
	 *	group_element, it suffices to check that all the face's vertices
	 *	are contained in the image of the mate.
	 *
	 *	Schematically, the polyhedron looks like this.  Note that the
	 *	group_element takes the mate to the face, not the other way around.
	 *
	 *				 ____________
	 *			    /    face    \
	 *			   /              \
	 *			  /       ^        \
	 *			 /        |         \
	 *			/   group_element    \
	 *			\        /           /
	 *			 \      /           /
	 *		mate  \                /
	 *			   \              /
	 *			    \____________/
	 *
	 *	Look at the image of the mate under the action of the group_element.
	 *	The figure shows what happens when the face is not a subset of
	 *	the image of the mate.
	 *
	 *				  image
	 *			  \     of      /
	 *			   \   mate    /
	 *				\_________/
	 *				 ____________
	 *			    /    face    \
	 *			   /              \
	 *			  /       ^        \
	 *			 /        |         \
	 *			/   group_element    \
	 *			\        /           /
	 *			 \      /           /
	 *		mate  \                /
	 *			   \              /
	 *			    \____________/
	 *
	 *	One of the vertices of the face extends beyond one of the sides
	 *	of the image of the mate.  Let alpha be the group_element
	 *	belonging to the face adjacent to that side of mate.
	 *
	 *				  image   \   conjugate
	 *			  \     of     \/  of
	 *			   \   mate    /\ alpha
	 *				\_________/ _\|
	 *				 ____________
	 *			    /    face    \
	 *			   /              \
	 *			  /       ^        \
	 *			 /        |         \
	 *			/   group_element    \
	 *			\        /           /
	 *			 \      /           /
	 *		mate  \                /
	 *			   \      |       /
	 *			    \_____|______/
	 *					  |
	 *					alpha
	 *					  |
	 *					  V
	 *
	 *	Consider the product beta = (group_element)(alpha).  (Matrices act
	 *	on column vectors on the left, so (group_element)(alpha) means
	 *	first do alpha, then do group_element.)
	 *
	 *	Let V be the vertex in question -- the vertex of face which lies
	 *	outside the image of mate.
	 *
	 *		distance(V, origin) = distance(V, group_element(origin))
	 *
	 *			because V lies on face.
	 *
	 *		distance(V, group_element(origin)) > distance(V, beta(origin))
	 *
	 *			because V lies beyond that side of the image of the mate.
	 *
	 *	Combining the above two observations yields
	 *
	 *		distance(V, origin) > distance(V, beta(origin))
	 *
	 *	which implies that the half space defined by beta will cut the
	 *	vertex V off of the polyhedron.
	 *
	 *	So our algorithm will be
	 *
	 *		for (each neighbor of the mate)
	 *			compute beta = group_element * alpha
	 *			for (each vertex V of the face)
	 *				if (beta cuts off V)
	 *					add faces determined by beta and its inverse
	 *						to the polyhedron
	 *					if roundoff-related errors occured
	 *						return func_failed
	 *					otherwise
	 *						set *face_was_pared to TRUE
	 *						return func_OK
	 *		set face->clean to TRUE
	 *		set *face_was_pared to FALSE
	 *		return func_OK
	 *
	 *
	 *	Technical digression.
	 *
	 *		The old version of SnapPea used a "clever trick" to check that
	 *		a face is a subset of its mate.  I've chosen not to use that
	 *		trick here, and in this digression I will briefly explain why.
	 *		Feel free to skip this digression is you want.  I'm writing it
	 *		mainly for my own good, so if I come back a few years from now
	 *		and wonder why I didn't use the trick, I'll at least know what
	 *		I was thinking at the time.
	 *
	 *		The "clever trick" was to try to simultaneously traverse the
	 *		perimeters of the face and its mate, checking that the group
	 *		element beta (defined above) is already the group element
	 *		corresponding to one of the face's neighbors.  If this didn't
	 *		work, either because the faces didn't in fact match up, or
	 *		(less likely) because the edges weren't of order 3, then the
	 *		old code would fall back to an algorithm like the one described
	 *		above.
	 *
	 *		I've chosen not to use this "clever trick" in the present code
	 *		for the following reasons.
	 *
	 *		(1)	For nonorientable manifolds, the code would get messier
	 *			because we wouldn't know a priori which way we need to
	 *			traverse the faces.
	 *
	 *		(2)	The code wasn't that much more efficient to begin with.
	 *			To match two n-gons, it would have to do n matrix
	 *			multiplications.  Each matrix multiplication requires
	 *			about 4^3 operations, so the run time is about O(n * 4^3).
	 *			The simpler algorithm (the one used here) requires, in
	 *			addition, the computation of n^2 inner products, to
	 *			check whether each of n points lies on the correct side
	 *			of n hyperplanes.  This requires a time O(n^2 * 4).
	 *			At first glance O(n^2) looks worse than O(n), but we
	 *			have to remember that n is not a large number.  One can
	 *			prove that for a typical polyhedron, the average value
	 *			of n is around 6.  (For an atypical polyhedron it's even
	 *			less.)  So the simpler algorithm requires little additional
	 *			time.  Indeed, if one wanted to streamline it, once could
	 *			compute only the first column of beta, unless it turned
	 *			out that you needed the whole matrix to add a new face.
	 *			So the simpler algorithm might be even faster.
	 *
	 *		(3)	Whether the old algorithm is faster than the new one isn't
	 *			completely clear, but given that the run times will be
	 *			similar, I feel that the simplicity of the code should
	 *			be the top priority.
	 *
	 *	End of technical digression.
	 */
	WEEdge		*edge;
	O31Matrix	*alpha;
	/*
	 *	Consider each neighbor of face->mate.
	 */
	edge = face->mate->some_edge;
	do
	{
		/*
		 *	First an error check.
		 */
		if (edge->f[left] == edge->f[right])
			uFatalError("pare_mated_face", "Dirichlet_construction");
		/*
		 *	Find the element alpha defined above.
		 */
		if (edge->f[left] == face->mate)
			alpha = edge->f[right]->group_element;
		else
			alpha = edge->f[left] ->group_element;
		/*
		 *	Check whether this alpha defines a beta which cuts a vertex off
		 *	of face.  If so, intersect the polyhedron with the faces defined
		 *	by beta and its inverse.
		 *
		 *	If topological problems due to roundoff error are
		 *	encountered, return func_failed.
		 */
		if (try_this_alpha(alpha, face, polyhedron, face_was_pared) == func_failed)
			return func_failed;
		/*
		 *	If the face was actually pared, return func_OK.
		 *	Otherwise keep going with the loop.
		 */
		if (*face_was_pared == TRUE)
			return func_OK;
		/*
		 *	Move on to the next edge.
		 */
		if (edge->f[left] == face->mate)
			edge = edge->e[tip][left];
		else
			edge = edge->e[tail][right];
	} while (edge != face->mate->some_edge);
	/*
	 *	The face is a subset of the image of its mate.
	 */
	face->clean = TRUE;
	/*
	 *	We didn't have to do anything to the polyhedron,
	 *	so set *face_was_pared to FALSE.
	 */
	*face_was_pared = FALSE;
	/*
	 *	Given that we modified nothing, we could hardly have encountered
	 *	topological problems due to roundoff errors, so return func_OK.
	 */
	return func_OK;
}
static FuncResult pare_mateless_face(
	WEFace			*face,
	WEPolyhedron	*polyhedron,
	Boolean			*face_was_pared)
{
	/*
	 *	The situation here is similar to that in pare_mated_face() above.
	 *	(In particular, you should understand pare_mated_face()'s
	 *	documentation before you read this.)  The difference is that
	 *	mate has been entirely cut off by other group elements.
	 *	(Well, almost entirely cut off.  A 1-dimensional subset might
	 *	still remain, but no 2-dimensional subset.)  In the following
	 *	diagram, the plane of the nonexistant mate is shown by a line
	 *	of stars.  (By the "plane of the nonexistant mate", we mean the
	 *	image of the plane of face under the action of the inverse of
	 *	face->group_element.)
	 *
	 *				  \     /
	 *				   \   /
	 *				    \ /
	 *			image of mate
	 *				*************
	 *				 ____________
	 *			    /    face    \
	 *			   /              \
	 *			  /       ^        \
	 *			  |       |         \
	 *			  | group_element    \
	 *		   *  |      /           /
	 *			* |_________________/
	 *		mate *
	 *			  *
	 *			   *
	 *
	 *	Let P be a point which lies in the image of face under the action of
	 *	the inverse of face->group_element, but is not a point of the
	 *	polyhedron.  Some face plane of the polyhedron, with group element
	 *	alpha, must exclude P from the polyhedron.  (Proof:  If P were on
	 *	the "correct" side of all face planes, then it would be contained
	 *	in the polyhedron, since the polyhedron is the intersection of the
	 *	halfspaces corresponding to its face planes.)
	 *
	 *						   conjugate
	 *				  \   --/--> of
	 *				   \   /    alpha
	 *				    \ /
	 *			image of mate
	 *				*************
	 *				 _________P'_
	 *			    /    face    \
	 *			   /              \
	 *			  /       ^        \
	 *			  |       |         \
	 *			  | group_element    \
	 *		   *  |      /           /
	 *			* |________|________/
	 *		mate *		   |
	 *			  P		   | alpha
	 *			   *	   V
	 *
	 *	This says that
	 *
	 *		distance(P, origin) > distance(P, alpha(origin))
	 *
	 *	Let P' be the image of P under the action of face->group_element,
	 *	that is, P' = face->group_element(P).
	 *
	 *	Let face->group_element act on the preceding inequality:
	 *
	 *		distance(P', group_element(origin))
	 *			> distance(P', group_element(alpha(origin)))
	 *
	 *	Because P' lies on face,
	 *
	 *		distance(P', origin) = distance(P', group_element(origin))
	 *
	 *	Substituting this equation into the preceding inequality yields
	 *
	 *		distance(P', origin) > distance(P', group_element(alpha(origin)))
	 *
	 *	This implies that beta = group_element * alpha will define a
	 *	plane which cuts P' off of the polyhedron.  Because face is the
	 *	convex hull of its vertices, beta must also remove some vertex V
	 *	from the polyhedron.
	 *
	 *	How do we find the group element alpha?  I don't know of any
	 *	clever way to find it, so we use brute force.  We look at each
	 *	face in turn until we find one that works.  Here's the algorithm:
	 *
	 *		for each alpha belonging to a face of the polyhedron
	 *			compute beta = group_element * alpha
	 *			for (each vertex V of the face)
	 *				if (beta cuts off V)
	 *					add faces determined by beta and its inverse
	 *						to the polyhedron
	 *					if roundoff-related errors occured
	 *						return func_failed
	 *					otherwise
	 *						set *face_was_pared to TRUE
	 *						return func_OK
	 *		report a roundoff error, because we proved above that a
	 *			suitable alpha must exist
	 */
	WEFace		*face1;
	O31Matrix	*alpha;
	/*
	 *	Consider each face1 of the polyhedron.
	 */
	for (face1 = polyhedron->face_list_begin.next;
		 face1 != &polyhedron->face_list_end;
		 face1 = face1->next)
	{
		/*
		 *	Find alpha.
		 */
		alpha = face1->group_element;
		/*
		 *	Check whether this alpha defines a beta which cuts a vertex off
		 *	of face.  If so, intersect the polyhedron with the faces defined
		 *	by beta and its inverse.
		 *
		 *	If topological problems due to roundoff error are
		 *	encountered, return func_failed.
		 */
		if (try_this_alpha(alpha, face, polyhedron, face_was_pared) == func_failed)
			return func_failed;
		/*
		 *	If the face was actually pared, return func_OK.
		 *	Otherwise keep going with the loop.
		 */
		if (*face_was_pared == TRUE)
			return func_OK;
	}
	/*
	 *	We should never reach this point, because in the above documentation
	 *	we proved that some alpha must define a beta which cuts off a vertex.
	 *	So if we do end up at this point, it means that roundoff errors
	 *	have corrupted our polyhedron.
	 */
	return func_failed;
}
static FuncResult try_this_alpha(
	O31Matrix		*alpha,
	WEFace			*face,
	WEPolyhedron	*polyhedron,
	Boolean			*face_was_pared)
{
	/*
	 *	try_this_alpha() performs the calculations common to
	 *	pare_mated_face() and pare_mateless_face().
	 *
	 *	It computes beta = face->group_element * alpha (cf. the
	 *	documentation at the beginning of pare_mated_face()) and
	 *	checks whether beta cuts off any vertices of face.
	 *
	 *	If beta cuts off vertices, then
	 *		if topological problems due to roundoff error are
	 *			encountered, it returns func_failed
	 *		else it sets *face_was_pared to TRUE and returns func_OK
	 *	else
	 *		it sets *face_was_pared to FALSE and returns func_OK.
	 */
	WEVertex	*vertex;
	WEEdge		*edge;
	O31Matrix	beta;
	O31Vector	normal;
	MatrixPair	matrix_pair;
	/*
	 *	Compute beta = (group_element)(alpha) as explained in the
	 *	documentation in pare_mated_face().
	 *
	 *	(If you have an overwhelming urge to optimize you could
	 *	compute only the first column of beta until you need the
	 *	rest, but for now I'll prefer simplicity over speed.)
	 */
	precise_o31_product(*face->group_element, *alpha, beta);
	/*
	 *	Compute the normal vector to the hyperplane defined by beta.
	 */
	compute_normal_to_Dirichlet_plane(beta, normal);
	/*
	 *	Consider each vertex of face.
	 *	(Technically speaking, we consider each edge, but then look
	 *	at the vertex on its counterclockwise end.)
	 */
	edge = face->some_edge;
	do
	{
		/*
		 *	Look at the vertex at the counterclockwise end of edge.
		 */
		if (edge->f[left] == face)
			vertex = edge->v[tip];
		else
			vertex = edge->v[tail];
		/*
		 *	Does the vertex lie beyond the hyperplane determined by beta?
		 */
		if (o31_inner_product(vertex->x, normal) > polyhedron->vertex_epsilon)
		{
			/*
			 *	Great.
			 *	We've found a vertex which will be cut off by beta.
			 */
			/*
			 *	Set up matrix_pair.m[0] and matrix_pair.m[1].
			 */
			o31_copy(matrix_pair.m[0], beta);
			o31_invert(matrix_pair.m[0], matrix_pair.m[1]);
			/*
			 *	The other fields in matrix_pair aren't needed here.
			 */
			matrix_pair.height	= 0.0;
			matrix_pair.prev	= NULL;
			matrix_pair.next	= NULL;
			/*
			 *	Intersect the polyhedron with the half spaces.
			 *	If roundoff errors cause topological problems,
			 *	return func_failed.
			 */
			if (intersect_with_halfspaces(polyhedron, &matrix_pair) == func_failed)
				return func_failed;
			/*
			 *	We've modified the polyhedron,
			 *	so set *face_was_pared to TRUE.
			 */
			*face_was_pared = TRUE;
			/*
			 *	intersect_with_halfspaces() encountered no topological
			 *	problems due to roundoff errors, so return func_OK.
			 */
			return func_OK;
		}
		/*
		 *	Move on to the next vertex.
		 */
		if (edge->f[left] == face)
			edge = edge->e[tip][left];
		else
			edge = edge->e[tail][right];
	} while (edge != face->some_edge);
	/*
	 *	Beta didn't cut off any vertices, so set *face_was_pared to FALSE.
	 */
	*face_was_pared = FALSE;
	/*
	 *	We didn't cut the polyhedron, so we could hardly have encountered
	 *	topological problems due to roundoff error.  Return func_OK.
	 */
	return func_OK;
}
static void count_cells(
	WEPolyhedron	*polyhedron)
{
	WEVertex	*vertex;
	WEEdge		*edge;
	WEFace		*face;
	/*
	 *	The counts were intialized in new_WEPolyhedron(),
	 *	but we'll reinitialize them here just for good form.
	 */
	polyhedron->num_vertices	= 0;
	polyhedron->num_edges		= 0;
	polyhedron->num_faces		= 0;
	/*
	 *	Count the vertices.
	 */
	for (vertex = polyhedron->vertex_list_begin.next;
		 vertex != &polyhedron->vertex_list_end;
		 vertex = vertex->next)
		polyhedron->num_vertices++;
	/*
	 *	Count the edges.
	 */
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
		polyhedron->num_edges++;
	/*
	 *	Count the faces.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		polyhedron->num_faces++;
	/*
	 *	Check the Euler characteristic.
	 */
	if (polyhedron->num_vertices - polyhedron->num_edges + polyhedron->num_faces != 2)
		uFatalError("count_cells", "Dirichlet_construction");
}
static void sort_faces(
	WEPolyhedron	*polyhedron)
{
	/*
	 *	Sort the faces by order of increasing distance from the basepoint.
	 *
	 *	Note:  sort_faces() assumes polyhedron->num_faces is correct, which
	 *	is true in the present context because compute_Dirichlet_domain()
	 *	calls count_cells() before sort_faces().
	 */
	WEFace	**array,
			*face;
	int		i;
	/*
	 *	This code assumes the polyhedron has at least two WEFaces.
	 *	But as long as we're doing an error check, let's insist that
	 *	the polyhedron have at least four faces.
	 */
	if (polyhedron->num_faces < 4)
		uFatalError("sort_faces", "Dirichlet_construction");
	/*
	 *	Allocate an array to hold the addresses of the WEFaces.
	 */
	array = NEW_ARRAY(polyhedron->num_faces, WEFace *);
	/*
	 *	Copy the addresses into the array.
	 */
	for (face = polyhedron->face_list_begin.next,
			i = 0;
		 face != &polyhedron->face_list_end;
		 face = face->next,
		 	i++)
		array[i] = face;
	/*
	 *	Do a quick error check to make sure we copied
	 *	the right number of elements.
	 */
	if (i != polyhedron->num_faces)
		uFatalError("sort_faces", "Dirichlet_construction");
	/*
	 *	Sort the array of pointers.
	 */
	qsort(	array,
			polyhedron->num_faces,
			sizeof(WEFace *),
			compare_face_distance);
	/*
	 *	Adjust the WEFaces' prev and next fields to reflect the new ordering.
	 */
	polyhedron->face_list_begin.next = array[0];
	array[0]->prev = &polyhedron->face_list_begin;
	array[0]->next = array[1];
	for (i = 1; i < polyhedron->num_faces - 1; i++)
	{
		array[i]->prev = array[i-1];
		array[i]->next = array[i+1];
	}
	array[polyhedron->num_faces - 1]->prev = array[polyhedron->num_faces - 2];
	array[polyhedron->num_faces - 1]->next = &polyhedron->face_list_end;
	polyhedron->face_list_end.prev = array[polyhedron->num_faces - 1];
	/*
	 *	Free the array.
	 */
	my_free(array);
}
static int CDECL compare_face_distance(
	const void	*ptr1,
	const void	*ptr2)
{
	double	diff;
	diff = (*(*((WEFace **)ptr1))->group_element)[0][0]
		 - (*(*((WEFace **)ptr2))->group_element)[0][0];
	if (diff < 0.0)
		return -1;
	if (diff > 0.0)
		return +1;
	return 0;
}
static Boolean verify_faces(
	WEPolyhedron	*polyhedron)
{
	WEEdge	*edge;
	WEFace	*face;
	/*
	 *	Initialize each face->num_sides to 0.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		face->num_sides = 0;
	/*
	 *	Add the contribution of each edge to the adjacent face->num_sides.
	 */
	for (edge = polyhedron->edge_list_begin.next;
		 edge != &polyhedron->edge_list_end;
		 edge = edge->next)
	{
		edge->f[left ]->num_sides++;
		edge->f[right]->num_sides++;
	}
	/*
	 *	Check that each face and its mate have the same num_sides.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		if (face->num_sides != face->mate->num_sides)
			return func_failed;
	return func_OK;
}
static FuncResult verify_group(
	WEPolyhedron	*polyhedron,
	MatrixPairList	*gen_list)
{
	/*
	 *	Check that the face pairing isometries generate all the generators
	 *	on the original gen_list, to be sure that we have a Dirichlet domain
	 *	for the manifold/orbifold itself and not some finite-sheeted cover.
	 *	This check should proceed quickly, because
	 *
	 *	(1)	The gen_list was simplified in the function
	 *		Dirichlet_from_generators_with_displacement() in Dirichlet.c, so
	 *		typically the elements on gen_list will all be face pairing
	 *		isometries to begin with, or close to it.
	 *
	 *	(2)	compute_Dirichlet_domain() has already called sort_faces() to
	 *		sort the polyhedron's faces in order of increasing basepoint
	 *		translation distance, so the face pairings we're most likely to
	 *		need will be encountered near the beginning of the list.
	 */
	MatrixPair	*matrix_pair;
	O31Matrix	m,
				candidate;
	Boolean		progress;
	WEFace		*face;
	double		verify_epsilon;
	for (matrix_pair = gen_list->begin.next;
		 matrix_pair != &gen_list->end;
		 matrix_pair = matrix_pair->next)
	{
		o31_copy(m, matrix_pair->m[0]);
		verify_epsilon = VERIFY_EPSILON;
		while (o31_equal(m, O31_identity, MATRIX_EPSILON) == FALSE)
		{
			progress = FALSE;
			for (face = polyhedron->face_list_begin.next;
				 face != &polyhedron->face_list_end;
				 face = face->next)
			{
				o31_product(m, *face->group_element, candidate);
				if (m[0][0] - candidate[0][0] > verify_epsilon)
				{
					o31_copy(m, candidate);
					progress = TRUE;
					break;
				}
			}
			if (progress == FALSE)
			{
				/*
				 *	There are two possibilities, either
				 *
				 *	(1)	We have a Dirichlet domain for a finite-sheeted cover
				 *		of the manifold/orbifold, or 
				 *
				 *	(2)	We have an orbifold -- perhaps one whose basepoint
				 *		is only slightly displaced from a fixed point --
				 *		with nontrivial group elements which move the basepoint
				 *		only a small distance.
				 *
				 *	To decide which case we're in and respond appropriately,
				 *	we set verify_epsilon to 0.0 and keep going.  If we still
				 *	can't make progress, then we know we are in case (1).
				 */
				if (verify_epsilon > 0.0)
					verify_epsilon = 0.0;
				else
				{
					uAcknowledge("Please tell Jeff Weeks that SnapPea seems to have computed a Dirichlet domain for a finite-sheeted cover of the manifold/orbifold.");
					return func_failed;
				}
			}
		}
	}
	return func_OK;
}
static void rewrite_gen_list(
	WEPolyhedron	*polyhedron,
	MatrixPairList	*gen_list)
{
	WEFace		*face,
				*mate;
	MatrixPair	*new_matrix_pair;
	/*
	 *	First discard the gen_list's present contents.
	 */
	free_matrix_pairs(gen_list);
	/*
	 *	Add the identity.
	 */
	new_matrix_pair = NEW_STRUCT(MatrixPair);
	o31_copy(new_matrix_pair->m[0], O31_identity);
	o31_copy(new_matrix_pair->m[1], O31_identity);
	new_matrix_pair->height = 1.0;
	INSERT_BEFORE(new_matrix_pair, &gen_list->end);
	/*
	 *	Use the face->copied fields to avoid copying both a face and its
	 *	mate as separate MatrixPairs.  First initialize all face->copied
	 *	fields to FALSE.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		face->copied = FALSE;
	/*
	 *	Go down the list of faces, and for each face which hasn't already
	 *	been done, copy it's and it's mate's group_elements to a MatrixPair,
	 *	and append the MatrixPair to the gen_list.
	 *
	 *	Note that the gen_list will be presorted,
	 *	because the list of faces has been sorted.
	 */
	for (face = polyhedron->face_list_begin.next;
		 face != &polyhedron->face_list_end;
		 face = face->next)
		if (face->copied == FALSE)
		{
			mate = face->mate;
			new_matrix_pair = NEW_STRUCT(MatrixPair);
			o31_copy(new_matrix_pair->m[0], *face->group_element);
			o31_copy(new_matrix_pair->m[1], *mate->group_element);
			new_matrix_pair->height = (*face->group_element)[0][0];
			INSERT_BEFORE(new_matrix_pair, &gen_list->end);
			face->copied = TRUE;
			mate->copied = TRUE;
		}
}
 |