1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364
|
/*
* ArgyllCMS Color Correction System
*
* Optimised Farthest Point Sampling - NN
*
* Author: Graeme W. Gill
* Date: 6/9/2004
*
* Copyright 2004, 2009 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*/
/* Latest version using vertex nets to reduce internal accounting overhead, */
/* in an attempt to improve performance scaling with larger numbers of points. */
/* TTBD:
This code shouldn't exit on an error - this causes an unnecessary failure
when ofps is used to evaluate the point distribution of other
distribution algorithms.
There is a bug when the ink limit == dimensions-1 (200% for CMY), and
the number of bit mask then exceeds > 32. This is not so +/- 0.2% either side
of 200%.
(see "Hack to workaround pathalogical")
There is a bug for CMYK when the ink limit == 100%
(see "Hack to workaround pathalogical")
One way of addressing the performance issues would be to use multiple
threads to call dnsq. A pool could be setup, one for each CPU.
Some profiles are too rough, and slow/stall vertex placement.
Reducing the cache grid and/or smoothing the rspl values
may mitigate this to some degree, and make this more robust ??
*/
/*
Description:
We create a function that estimates the sample positioning error at
any location based on a weighted combination of perceptual and device distance,
and perceptual function curvature.
We then initially add sampling points at the largest estimated error
verticies of the veronoi natural neighbourhood.
This gives us an optimal distribution measuring in estimated position
error within a tollerance of 2:1
We then iteratively improve the distribution of point nodes by
moving them in the direction of the adjacent vertex with the
largest estimated sampling error, so that the errors are equally
distributed.
To ensure that there is a good distribution of sampling
points on the edges and faces of the gamut, the initial
points are given a slighte weighting towards these
elements, and then fastened to them. The points on
each lower dimensional element (edge, face) is then
optimized only amongst themselves, while higher
dimension points are aware of the lower dimension
ones. In this way the distribution of points on
lower dimensional surfaces is well spread, while
the higher dimension points take their positions into account.
*/
/*
Failings:
The distribution near the gamut surfaces has a characteristic
"buffer zone" layer that is not very nice. This is because
the surface concentrate the sufrace points forming a "force field".
It would be good to add a tweak factor to reduce this surface "gang effect".
The initial allocation of points to lower dimension surfaces
is a bit haphazard. It would be nice to have some mechanism
to add or subtract points to/from lower dimensional surfaces
if they were over or under sampled compared to everything else.
While the current algorithm meets many goals, such as minimizing the
maximum estimated error from any point in the space to the nearest
node, and placing nodes on sub dimensional surfaces with distributions
optimal within that sub dimensions, it has one obvious failing, and
that is that it fails to stratify the samples.
So if a device is dominantly channel indepenedent, it doesn't
take advantage of the number of samples used to fully explore
all possible device channel values. This also applies at
higher dimensions (ie. the CMYK values exploring response
to different K values doesn't spread the CMY values
evenly apart.)
Stratification seems to be somewhat at odds with the primary goal
of minimizing the maximum estimated error from any point in the
space to the nearest node, but it would be good if there were
some way of setting this balance.
How could stratification be added to the current approach ?
In general there are many sub-dimensions views, not all of
which would probably be regarded as important.
To measure spread, independent voronoi tessellations of
these sub dimensions would be needed, and they could be
used partly driver optimization (??).
For 1D device channels this wouldn't be so hard to
add, although it's hard to know how effective it would
be, or whether it would wreck the ND optimization. It
might also be unecessary if per channel calibration
has been applied.
For CMY this would need a 3D shadow veronoi.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#if defined(__IBMC__)
#include <float.h>
#endif
#include "aconfig.h"
#include "numlib.h"
#include "sort.h"
#include "counters.h"
#include "icc.h"
#include "xicc.h"
#include "xcolorants.h"
#include "targen.h"
#include "rspl.h"
#include "conv.h"
#include "ofps.h"
//#include <iperf.h>
#undef DEBUG
#undef WARNINGS /* Print warnings within DEBUG */
#undef STATS /* Show function stats */
/* Optimal fully adapted weightings : */
#define ADAPT_PERCWGHT 0.65 /* Degree of perceptual adaptation */
#define ADAPT_CURVWGHT 1.0 /* Degree of curvature */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#ifndef STANDALONE_TEST // targen settings
# define DOOPT /* Do optimization */
# define INDEP_SURFACE /* Make surface point distribution and optimization independent */
# undef MAXINDEP_2D /* Limit independent surfaces to 2D */
/* Seems to be best for ink limited devices to #undef ? */
//# define GAMUT_EDGE_FUDGE 1.5 /* Fudge factor to counteract gamut suface barrier effect */
// This increases edge point density as a side effect ??
# define KEEP_SURFACE /* Keep surface points on the surface during opt. */
# define INITIAL_SURFACE_PREF 1.50 /* Extra weighting for surface points at start of seeding */
# define FINAL_SURFACE_PREF 0.80 /* Extra weighting for surface points by end of seeding */
# define SURFTOL 0.0001 /* Proportion of average spacing to force to gamut boundary */
# define RANDOM_PERTERB /* Perpterb initial placement to break up patterns */
/* Good mode */
# define PERTERB_AMOUNT 0.5 /* and to aid surface point distribution with INDEP_SURFACE */
# define OPT_MAXITS 20 /* Number of optimisation itterations (0 to disable optimisation) */
# define OPT_TRANS_ITTERS 18 /* Numbers of itterations to transition overshoot and sepw */
# define OPT_TRANS_POW 1.6 /* Power curve to blend along */
# define OPT_INITIAL_OVERSHOOT 1.9 /* Optimisation movement initial overshoot */
# define OPT_FINAL_OVERSHOOT 0.1 /* Optimisation movement final overshoot */
# define OPT_INITIAL_SEP_WEIGHT 0.7 /* Weight to give separation of nodes during opt */
# define OPT_FINAL_SEP_WEIGHT 0.3 /* Weight to give separation of nodes during opt */
# define OPT_STOP_TOL 0.0005 /* Stopping tollerance */
/* Fast mode */
# define PERTERB_AMOUNT_2 0.1 /* and to aid surface point distribution with INDEP_SURFACE */
# define OPT_MAXITS_2 6 /* Number of optimisation itterations (0 to disable optimisation) */
# define OPT_TRANS_ITTERS_2 5 /* Numbers of itterations to transition overshoot and sepw */
# define OPT_TRANS_POW_2 1.7 /* Power curve to blend along */
# define OPT_INITIAL_OVERSHOOT_2 1.6 /* Optimisation movement initial overshoot */
# define OPT_FINAL_OVERSHOOT_2 0.05 /* Optimisation movement final overshoot */
# define OPT_INITIAL_SEP_WEIGHT_2 0.8 /* Weight to give separation of nodes during opt */
# define OPT_FINAL_SEP_WEIGHT_2 0.3 /* Weight to give separation of nodes during opt */
# define OPT_STOP_TOL_2 0.001 /* Stopping tollerance */
/* Diagnostic settings */
# undef DUMP_STRUCTURE /* Dump internal node & vertex structure */
# undef DUMP_PLOT_SEED /* Show on screen plot for each initial seed point */
# undef DUMP_PLOT /* Show on screen plot after each itteration */
# define DUMP_VTX 1 /* Display the vertex locations too */
# define DUMP_PLA 1 /* Display the node planes too */
# define PERC_PLOT 0 /* Emit perceptive space plots */
# define DO_WAIT 1 /* Wait for user key after each plot */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#else /* ofps standalone test settings */
# define DOOPT /* Do optimization */
# define INDEP_SURFACE /* Make surface point distribution and optimization independent */
# define MAXINDEP_2D /* Limit independent surfaces to 2D */
//# define GAMUT_EDGE_FUDGE 1.5 /* Fudge factor to counteract gamut suface barrier effect */
# define KEEP_SURFACE /* Keep surface points on the surface during opt. */
# define INITIAL_SURFACE_PREF 1.60 /* Extra weighting for surface points at start of seeding */
# define FINAL_SURFACE_PREF 0.80 /* Extra weighting for surface points by end of seeding */
# define SURFTOL 0.0001 /* Proportion of averag spacing to force to gamut boundary */
# define RANDOM_PERTERB /* Perpterb initial placement to break up patterns, */
# define PERTERB_AMOUNT 0.5
# define OPT_MAXITS 20 /* Number of optimisation itterations (0 to disable optimisation) */
# define OPT_TRANS_ITTERS 18 /* Numbers of itterations to transition overshoot and sepw */
# define OPT_TRANS_POW 2.5 /* Power curve to blend along */
# define OPT_INITIAL_OVERSHOOT 0.8 /* Optimisation movement initial overshoot */
# define OPT_FINAL_OVERSHOOT 0.1 /* Optimisation movement final overshoot */
# define OPT_INITIAL_SEP_WEIGHT 0.9 /* Weight to give separation of nodes during opt */
# define OPT_FINAL_SEP_WEIGHT 0.3 /* Weight to give separation of nodes during opt */
# define OPT_STOP_TOL 0.0005 /* Device stopping tollerance */
/* Diagnostic settings */
# undef DUMP_STRUCTURE /* Dump internal node & vertex structure */
# undef DUMP_PLOT_SEED /* Show on screen plot for each initial seed point */
# undef DUMP_PLOT_NCOL /* Show on screen plot after adding neighbours, before collecting */
# define DUMP_PLOT /* Show on screen plot after each itteration */
# undef DUMP_PLOT_RESEED /* Show on screen plot for each re-seed point */
# undef DUMP_OPT_PLOT /* Show on screen plot for each optimization pass */
# undef DUMP_PLOT_BEFORFIXUP /* Show plot after reposition but before fixups */
# undef DUMP_PLOT_EACHFIXUP /* Show each node fixup */
# define DUMP_VTX 1 /* Display the vertex locations too */
# define DUMP_PLA 1 /* Display the node planes too */
# define PERC_PLOT 0 /* Emit perceptive space plots */
# define DO_WAIT 1 /* Wait for user key after each plot */
# undef DUMP_EPERR /* Create .tiff of eperr */
# undef DUMP_FERR /* 10000 */ /* Create .tiff of function error >= 20 and stop. */
//# define SA_ADAPT 0.001 /* Standalone test, adaptation level */
# define SA_ADAPT -1.0 /* Standalone test, adaptation level (< 0.0, use individual) */
# define SA_DEVD_MULT 1.0 /* Delta E for each percent of device space distance */
# define SA_PERC_MULT 0.0 /* Delta E for each delta E of perceptual space distance */
# define SA_INTERP_MULT 0.0 /* Delta E for each delta E of estimated interpolation error */
#endif /* NEVER */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Overall algorithm */
#define NINSERTTRIES 100 /* Number of seedin insert tries befor failing with error() */
#define NUMTOL 1e-16 /* Numerical tollerance */
#define FTOL 1e-8 /* dnsqe function tollerance */
#define FGPMUL 5.0 /* Weighting of gamut plane error into dnsqe function */
#define COINTOL 1e-8 /* Tollerance for point cooincidence */
#define ILIMITEPS 1e-6 /* imin, imax and ilimit clip test margine */
#define FASTREJMULT1 20.5 /* Fast reject layer distance fudge factor */
#define FASTREJECTMULT 0.08 /* Fast reject cell skip threshold fudge factor */
#define CELLMAXEPERRFF 2.2 /* Cell worst case eperr from center fudge factor */
#define TNPAGRID 0.8 /* Target nodes per accelleration & peceptual cache grid cell */
#define TNPAGRIDMINRES 7 /* Perceptual cache grid min resolution */
#define TNPAGRIDMAXRES 33 /* Perceptual cache grid max resolution */
#undef FORCE_INCREMENTAL /* Force incremental update after itteration */
#define FORCE_RESEED /* Force reseed after itteration */
#define MAXTRIES 41 /* Maximum dnsq tries before giving up */
#define CACHE_PERCEPTUAL /* Cache the perceptual lookup function */
#define USE_DISJOINT_SETMASKS /* Reduce INDEP_SURFACE setmask size */
/* Sanity checks (slow) */
#undef SANITY_CHECK_SEED /* Sanity check the selection of the bigest eperr seed vertex */
#undef SANITY_CHECK_HIT /* Sanity check the hit detection */
#undef SANITY_CHECK_HIT_FATAL /* throw fatal error in sanity check */
#undef SANITY_CHECK_FIXUP /* Check that fixup was sucessful */
#undef SANITY_CHECK_FIXUP_FATAL /* throw fatal if it wasn't */
#undef SANITY_CHECK_CLOSEST /* Check that ofps_findclosest_xx() returns correct result */
#undef SANITY_CHECK_CLOSEST_FATAL /* throw fatal error on sanity fail */
#undef SANITY_CHECK_CONSISTENCY /* Check internal consistency at each itteration */
#undef SANITY_CHECK_CONSISTENCY_FATAL /* Throw a fatal if it wasn't */
#undef SANITY_RESEED_AFTER_FIXUPS /* Re-create veronoi from scratch after incremental update. */
#undef SANITY_CHECK_EXAUSTIVE_SEARCH_FOR_VERTEXES /* Do very, vert slow search for all vertexes */
#define ALWAYS
#undef NEVER
#ifdef STATS
# include "conv.h" /* System dependent convenience functions */
#endif
#if defined(DEBUG) || defined(DUMP_PLOT_SEED) || defined(DUMP_PLOT)
# include "plot.h"
# include "ui.h"
#endif
#if defined(DUMP_EPERR) || defined(DUMP_FERR)
#include "tiffio.h"
struct _vopt_cx;
static void dump_dnsqe(ofps *s, char *fname, int *nix, struct _vopt_cx *cx);
#endif
#if defined(DEBUG) || defined(DUMP_PLOT_SEED) || defined(DUMP_PLOT)
static void dump_image(ofps *s, int pcp, int dwt, int vtx, int dpla, int ferr, int noi);
#endif
#if defined(DEBUG) || defined (SANITY_CHECK_CONSISTENCY)
static void sanity_check(ofps *s, int check_nodelists);
#endif
#if defined(DEBUG) || defined(DUMP_STRUCTURE)
static void dump_node_vtxs(ofps *s, int check_nodelists);
//static void dump_node_vtxs2(ofps *s, char *cc);
#endif
static void ofps_binit(ofps *s);
static void ofps_stats(ofps *s);
static int ofps_point2cell(ofps *s, double *v, double *p);
static void ofps_gridcoords(ofps *s, int *c, double *v, double *p);
static void ofps_add_nacc(ofps *s, node *n);
static void ofps_rem_nacc(ofps *s, node *n);
static void ofps_add_vacc(ofps *s, vtx *vx);
static void ofps_rem_vacc(ofps *s, vtx *vx);
static void ofps_add_vseed(ofps *s, vtx *vx);
static void ofps_rem_vseed(ofps *s, vtx *vx);
static void ofps_re_create_node_node_vtx_lists(ofps *s);
static void do_batch_update1(ofps *s, int fixup);
static void do_batch_update2(ofps *s, int fixup);
static node *ofps_findclosest_node(ofps *s, double *ceperr, vtx *vx);
//static vtx *ofps_findclosest_vtx(ofps *s, double *ceperr, node *nn);
static int ofps_findhit_vtxs(ofps *s, node *nn);
static char *pco(int di, int *co);
static char *ppos(int di, double *p);
static char *pcomb(int di, int *n);
static char *peperr(double eperr);
static char *psm(ofps *s, setmask *sm);
/* Check the incremental vertexes against the re-seeded vertexes */
static void save_ivertexes(ofps *s);
static int check_vertexes(ofps *s);
/* Check that no node is closer to a vertex than its parent */
static int check_vertex_closest_node(ofps *s);
/* Do an exaustive check for missing vertexes */
static void check_for_missing_vertexes(ofps *s);
/* --------------------------------------------------- */
/* Setmask manipulation functions */
#ifdef USE_DISJOINT_SETMASKS
/* We assume the number of words is <= 1, */
/* and we can use macros */
/* Signal this is a single word mask by using -ve no. of bits */
#define sm_init(s, nbits) _sm_init(s, -(nbits))
#define sm_cp(s, sm_B, sm_A) \
((sm_B)->m[0] = (sm_A)->m[0])
#define sm_or(s, sm_C, sm_A, sm_B) \
((sm_C)->m[0] = (sm_A)->m[0] | (sm_B)->m[0])
#define sm_orand(s, sm_D, sm_A, sm_B, sm_C) \
((sm_D)->m[0] = (sm_A)->m[0] | ((sm_B)->m[0] & (sm_C)->m[0]))
#define sm_and(s, sm_C, sm_A, sm_B) \
((sm_C)->m[0] = (sm_A)->m[0] & (sm_B)->m[0])
#define sm_andnot(s, sm_C, sm_A, sm_B) \
((sm_C)->m[0] = (sm_A)->m[0] & (s->lwmask ^ (sm_B)->m[0]))
#define sm_andand(s, sm_D, sm_A, sm_B, sm_C) \
((sm_D)->m[0] = (sm_A)->m[0] & (sm_B)->m[0] & (sm_C)->m[0])
#define sm_test(s, sm_A) \
((sm_A)->m[0] & s->lwmask)
#define sm_equal(s, sm_A, sm_B) \
(((sm_A)->m[0] & s->lwmask) == ((sm_B)->m[0] & s->lwmask))
#define sm_andtest(s, sm_A, sm_B) \
((sm_A)->m[0] & (sm_B)->m[0])
#define sm_andnottest(s, sm_A, sm_B) \
((sm_A)->m[0] & (s->lwmask ^ (sm_B)->m[0]))
#define sm_vtx_vtx(s, v1, v2) \
((v1)->vm.m[0] & (v2)->vm.m[0] & s->sc[(v1)->cmask & (v2)->cmask].a_sm.m[0])
#define sm_vtx_node(s, vx, nn) \
((vx)->vm.m[0] & s->sc[(nn)->pmask].a_sm.m[0] & s->sc[(vx)->cmask & (nn)->pmask].a_sm.m[0])
#else
#define sm_init(s, nbits) _sm_init(s, nbits)
#define sm_cp(s, sm_B, sm_A) _sm_cp(s, sm_B, sm_A)
#define sm_or(s, sm_C, sm_A, sm_B) _sm_or(s, sm_C, sm_A, sm_B)
#define sm_orand(s, sm_D, sm_A, sm_B, sm_C) _sm_orand(s, sm_D, sm_A, sm_B, sm_C)
#define sm_and(s, sm_C, sm_A, sm_B) _sm_and(s, sm_C, sm_A, sm_B)
#define sm_andnot(s, sm_C, sm_A, sm_B) _sm_andnot(s, sm_C, sm_A, sm_B)
#define sm_andand(s, sm_D, sm_A, sm_B, sm_C) _sm_andand(s, sm_D, sm_A, sm_B, sm_C)
#define sm_test(s, sm_A) _sm_test(s, sm_A)
#define sm_equal(s, sm_A, sm_B) _sm_equal(s, sm_A, sm_B)
#define sm_andtest(s, sm_A, sm_B) _sm_andtest(s, sm_A, sm_B)
#define sm_andnottest(s, sm_A, sm_B) _sm_andnottest(s, sm_A, sm_B)
#define sm_vtx_node(s, vx, nn) _sm_vtx_node(s, vx, nn)
#define sm_vtx_vtx(s, v1, v2) _sm_vtx_vtx(s, v1, v2)
#endif
/* Compute set mask parameters */
static void _sm_init(ofps *s, int nbits) {
//printf("~1 _sm_init with %d bits\n",nbits);
s->bpsmw = sizeof(unsigned int) * 8;
if (nbits < 0) { /* Macro initialisation */
#ifdef DEBUG
printf("Disjoint sets being used\n");
#endif
nbits = -nbits;
if (nbits > s->bpsmw)
error("Attempt to use macro setmasks when nbits %d > a words bits %d",nbits,s->bpsmw);
}
s->smbits = nbits;
s->nsmw = (s->smbits + s->bpsmw - 1)/s->bpsmw;
s->lwmask = ~0;
s->lwmask >>= s->nsmw * s->bpsmw - s->smbits; /* Number of unused bits */
if (s->nsmw > MXSMASKW)
error("Not enough words for %d setmask bits, got %d need %d\n",s->smbits,MXSMASKW,s->nsmw);
}
/* Copy a setmask */
static void _sm_cp(ofps *s, setmask *sm_B, setmask *sm_A) {
int i;
for (i = 0; i < s->nsmw; i++)
sm_B->m[i] = sm_A->m[i];
}
/* Set the whole mask to zero or one */
static void sm_set(ofps *s, setmask *sm, int val) {
int i;
unsigned int vv = 0;
if (val & 1)
vv = ~0;
for (i = 0; i < s->nsmw; i++)
sm->m[i] = vv;
sm->m[i-1] &= s->lwmask;
}
/* Set the given bit to zero or one */
static void sm_setbit(ofps *s, setmask *sm, int bit, int val) {
int i;
unsigned int vv = 0;
if (bit > s->smbits)
error("assert, trying to set bit %d outside setmask size %d",bit,s->smbits);
i = bit / s->bpsmw;
vv = 1 << bit % s->bpsmw;
if (val & 1)
sm->m[i] |= vv;
else
sm->m[i] &= ~vv;
}
/* C = A | B */
static void _sm_or(ofps *s, setmask *sm_C, setmask *sm_A, setmask *sm_B) {
int i;
for (i = 0; i < s->nsmw; i++)
sm_C->m[i] = sm_A->m[i] | sm_B->m[i];
}
/* D = A | (B & C) */
static void _sm_orand(ofps *s, setmask *sm_D, setmask *sm_A, setmask *sm_B, setmask *sm_C) {
int i;
for (i = 0; i < s->nsmw; i++)
sm_D->m[i] = sm_A->m[i] | (sm_B->m[i] & sm_C->m[i]);
}
/* C = A & B */
/* Return zero if result is zero */
static unsigned int _sm_and(ofps *s, setmask *sm_C, setmask *sm_A, setmask *sm_B) {
unsigned int vv = 0;
int i;
for (i = 0; i < s->nsmw; i++)
vv |= sm_C->m[i] = sm_A->m[i] & sm_B->m[i];
return vv;
}
/* C = A & ~B */
/* Return zero if result is zero */
static unsigned int _sm_andnot(ofps *s, setmask *sm_C, setmask *sm_A, setmask *sm_B) {
unsigned int vv = 0;
int i;
for (i = 0; i < s->nsmw; i++) {
if (i < (s->nsmw-1))
vv |= sm_C->m[i] = sm_A->m[i] & ~sm_B->m[i];
else
vv |= sm_C->m[i] = sm_A->m[i] & (s->lwmask ^ sm_B->m[i]);
}
return vv;
}
/* D = A & B & C */
/* Return zero if result is zero */
static unsigned int _sm_andand(ofps *s, setmask *sm_D, setmask *sm_A, setmask *sm_B, setmask *sm_C) {
unsigned int vv = 0;
int i;
for (i = 0; i < s->nsmw; i++)
vv |= sm_D->m[i] = sm_A->m[i] & sm_B->m[i] & sm_C->m[i];
return vv;
}
/* Return zero if result is zero */
static unsigned int _sm_test(ofps *s, setmask *sm_A) {
unsigned int vv = 0;
int i;
for (i = 0; i < s->nsmw; i++) {
if (i < (s->nsmw-1))
vv |= sm_A->m[i];
else
vv |= sm_A->m[i] & s->lwmask;
}
return vv;
}
/* Return nz if the two are equal */
static unsigned int _sm_equal(ofps *s, setmask *sm_A, setmask *sm_B) {
int i;
for (i = 0; i < s->nsmw; i++) {
if (i < (s->nsmw-1)) {
if (sm_A->m[i] != sm_B->m[i])
return 0;
} else {
if ((sm_A->m[i] & s->lwmask) != (sm_B->m[i] & s->lwmask))
return 0;
}
}
return 1;
}
/* A & B and return zero if the result was zero. */
static unsigned int _sm_andtest(ofps *s, setmask *sm_A, setmask *sm_B) {
unsigned int vv = 0;
int i;
for (i = 0; i < s->nsmw; i++)
vv |= sm_A->m[i] & sm_B->m[i];
return vv;
}
/* A & ~B and return zero if result is zero */
static unsigned int _sm_andnottest(ofps *s, setmask *sm_A, setmask *sm_B) {
unsigned int vv = 0;
int i;
for (i = 0; i < s->nsmw; i++) {
if (i < (s->nsmw-1))
vv |= sm_A->m[i] & ~sm_B->m[i];
else
vv |= sm_A->m[i] & (s->lwmask ^ sm_B->m[i]);
}
return vv;
}
/* Test if two vertexes interact */
/* return nz if they do */
static unsigned int _sm_vtx_vtx(ofps *s, vtx *v1, vtx *v2) {
unsigned int vv = 0;
int i;
#ifdef USE_DISJOINT_SETMASKS
/* Because the mask bits are re-used across disjoint sets, */
/* we have to discount any intersection that occurs where */
/* the two items are disjoint, with the exception of the full-d set. */
for (i = 0; i < s->nsmw; i++)
vv |= v1->vm.m[i] & v2->vm.m[i] & s->sc[v1->cmask & v2->cmask].a_sm.m[i];
#else
for (i = 0; i < s->nsmw; i++)
vv |= v1->vm.m[i] & v2->vm.m[i];
# endif
return vv;
}
/* Test if a vertex and node interact */
static unsigned int _sm_vtx_node(ofps *s, vtx *vx, node *nn) {
unsigned int vv = 0;
int i;
/* Because the mask bits are re-used across disjoint sets, */
/* we have to discount any intersection that occurs where */
/* the two items are disjoint, with the exception of the full-d set. */
#ifdef USE_DISJOINT_SETMASKS
for (i = 0; i < s->nsmw; i++)
vv |= vx->vm.m[i] & s->sc[nn->pmask].a_sm.m[i] & s->sc[vx->cmask & nn->pmask].a_sm.m[i];
#else
for (i = 0; i < s->nsmw; i++)
vv |= vx->vm.m[i] & s->sc[nn->pmask].a_sm.m[i];
# endif
return vv;
}
/* Utility - return a string containing the mask in hex */
static char *psm(ofps *s, setmask *sm) {
static char buf[5][200];
static int ix = 0;
int e, f;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
sprintf(bp, "0x"); bp += strlen(bp);
for (f = 0, e = s->nsmw-1; e >= 0; e--) {
if (f || e == 0 || sm->m[e] != 0) {
if (f) {
sprintf(bp, "%08x", sm->m[e]); bp += strlen(bp);
} else {
sprintf(bp, "%x", sm->m[e]); bp += strlen(bp);
f = 1;
}
}
}
return buf[ix];
}
/* --------------------------------------------------- */
/* Swap the location of a node in s->n[]. This is assumed to */
/* be done _before_ a node is added to the veroni */
static void swap_nodes(ofps *s, int i, int j) {
node *n;
int xx;
n = s->n[i];
s->n[i] = s->n[j];
s->n[j] = n;
/* fix index number */
xx = s->n[i]->ix;
s->n[i]->ix = s->n[j]->ix;
s->n[j]->ix = xx;
xx = s->n[i]->ixm;
s->n[i]->ixm = s->n[j]->ixm;
s->n[j]->ixm = xx;
}
/* Shuffle all the nodes in the list along to put */
/* the given node at the start. */
static void move_node_to_front(ofps *s, int i) {
node *n;
int j;
n = s->n[i];
for (j = 1; j <= i; j++)
s->n[j] = s->n[j-1];
s->n[0] = n;
/* Fix ->ix and ixm */
for (j = 0; j <= i; j++) {
int bitp;
s->n[j]->ix = i;
bitp = 31 & (j + (j >> 4) + (j >> 8) + (j >> 12));
s->n[j]->ixm = (1 << bitp);
}
}
/* Randomly shuffle all the nodes */
static void shuffle_node_order(ofps *s) {
int i;
for (i = 0; i < s->tinp; i++) {
swap_nodes(s, i, i_rand(0, s->tinp-1));
}
}
/* Reverse the nodes order */
static void reverse_node_order(ofps *s) {
int i, j;
for (i = 0, j = s->tinp-1; i < j; i++, j--) {
swap_nodes(s, i, j);
}
}
/* --------------------------------------------------- */
/* Default convert the nodes device coordinates into approximate perceptual coordinates */
/* (usually overriden by caller supplied function) */
static void
default_ofps_to_percept(void *od, double *p, double *d) {
ofps *s = (ofps *)od;
int e;
/* Default Do nothing - copy device to perceptual. */
for (e = 0; e < s->di; e++) {
double tt = d[e];
p[e] = tt * 100.0;
}
}
/* Filtered perceptual lookup, used for setting up rspl cache values. */
/* Input is device values, output L*a*b* like perceptual values. */
static void
filtered_ofps_to_percept(void *ss, double *p, double *d) {
ofps *s = (ofps *)ss;
double rad = 1.0/s->pcache_res; /* Filter radius = grid res. */
int fres = 2; /* +/- 2 around center */
int e, f;
double pw; /* Accumulated Weight */
double off[MXPD]; /* Offset value */
double out[MXPD]; /* Offset output value */
double roff[MXPD]; /* Reflection offset value (for clip case) */
double rout[MXPD]; /* Reflected offset output value */
DCOUNT(co, MXPD, s->di, -fres, -fres, fres+1);
if (rad > 0.05) /* Don't loose too much detail */
rad = 0.05;
//printf("filtered called with %s\n",debPdv(s->di,d));
for (f = 0; f < s->di; f++)
p[f] = 0.0; /* Accumulated value */
pw = 0.0;
DC_INIT(co);
while (!DC_DONE(co)) {
double tw = 1.0;
int clip = 0;
//printf(" sub samp at %s\n", debPiv(s->di, co));
for (e = 0; e < s->di; e++) {
double w, ov;
ov = ((double)co[e])/(fres+1) * rad;
roff[e] = off[e] = d[e] + ov;
if (off[e] < 0.0) {
off[e] = 0.0;
roff[e] = 0.0 - ov;
clip = 1;
}
else if (off[e] > 1.0) {
off[e] = 1.0;
roff[e] = 1.0 - ov;
clip = 1;
}
//printf(" w[%d] = %f\n",e,(fres+1 - fabs((double)co[e]))/(fres+1));
w = (fres+1 - fabs((double)co[e]))/(fres+1);
tw *= w;
}
//printf(" off %s wt %f\n", debPdv(s->di,off),tw);
s->percept(s->od, out, off);
/* For clipped case, use reflected value from reflected location */
if (clip) {
s->percept(s->od, rout, roff);
//printf(" roff %s\n", debPdv(s->di, roff));
//printf(" out %s\n", debPdv(s->di, out));
//printf(" rout %s\n", debPdv(s->di, rout));
for (f = 0; f < s->di; f++)
out[f] = 2 * out[f] - rout[f];
//printf(" eout %s\n", debPdv(s->di, out));
}
for (f = 0; f < s->di; f++)
p[f] += tw * out[f];
pw += tw;
DC_INC(co);
}
for (f = 0; f < s->di; f++)
p[f] /= pw;
//s->percept(s->od, out, d);
//printf(" u out %s\n", debPdv(s->di, out));
//printf(" f out %s\n", debPdv(s->di, p));
//printf("\n");
}
/* Cached perceptual lookup */
static void
ofps_cache_percept(void *od, double *p, double *d) {
int e;
co tp;
rspl *pc = (rspl *)od;
for (e = 0; e < pc->di; e++)
tp.p[e] = d[e];
pc->interp(pc, &tp);
for (e = 0; e < pc->fdi; e++)
p[e] = tp.v[e];
}
/* Return the distance of the device value from the device gamut */
/* This will be -ve if the point is outside */
/* If bvp is non-null, the index of the closest dim times 2 */
/* will be returned for the 0.0 boundary, dim * 2 + 1 for the 1.0 */
/* boundary, and di * 2 for the ink limit boundary. */
static double
ofps_in_dev_gamut(ofps *s, double *d, int *bvp) {
int e, di = s->di;
double tt;
double dd = 100.0; /* Worst distance outside */
double ss = 0.0; /* Sum of values */
int bv = di;
for (e = 0; e < di; e++) {
tt = d[e] - s->imin[e];
if (tt < dd) {
dd = tt;
bv = e * 2;
}
tt = s->imax[e] - d[e];
if (tt < dd) {
dd = tt;
bv = e * 2 + 1;
}
ss += d[e]; /* Track sum */
}
ss = (s->ilimit - ss)/di; /* Axis aligned distance to ink limit */
tt = sqrt((double)di) * ss; /* Diagonal distance to ink limit */
if (tt < dd) {
dd = tt;
bv = di * 2;
}
if (bvp != NULL)
*bvp = bv;
return dd;
}
#ifdef NEVER /* Allow performance trace on ofps_clip_point usage */
static int ofps_clip_point(ofps *s, double *cd, double *d);
static int ofps_clip_point1(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point2(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point3(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point4(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point5(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point6(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point7(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point8(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point9(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
static int ofps_clip_point10(ofps *s, double *cd, double *d) {
return ofps_clip_point(s, cd, d); }
#else /* Production code */
#define ofps_clip_point1 ofps_clip_point
#define ofps_clip_point2 ofps_clip_point
#define ofps_clip_point3 ofps_clip_point
#define ofps_clip_point4 ofps_clip_point
#define ofps_clip_point5 ofps_clip_point
#define ofps_clip_point6 ofps_clip_point
#define ofps_clip_point7 ofps_clip_point
#define ofps_clip_point8 ofps_clip_point
#define ofps_clip_point9 ofps_clip_point
#define ofps_clip_point10 ofps_clip_point
#endif
/* Given the new intended device coordinates, */
/* clip the new position to the device gamut edge */
/* return non-zero if the point was clipped */
static int
ofps_clip_point(ofps *s, double *cd, double *d) {
int di = s->di;
double ss = 0.0;
int rv = 0;
#define STEP(IX) \
cd[IX] = d[IX]; \
if (cd[IX] < s->imin[IX]) { \
cd[IX] = s->imin[IX]; \
if (cd[IX] < (s->imin[IX] - ILIMITEPS)) \
rv |= 1; \
} else if (cd[IX] > s->imax[IX]) { \
cd[IX] = s->imax[IX]; \
if (cd[IX] > (s->imax[IX] + ILIMITEPS)) \
rv |= 1; \
} \
ss += cd[IX];
switch (di) {
case 4:
STEP(3)
case 3:
STEP(2)
case 2:
STEP(1)
case 1:
STEP(0)
}
#undef STEP
if (ss > s->ilimit) {
if (ss > (s->ilimit + ILIMITEPS))
rv |= 1;
ss = (ss - s->ilimit)/s->di;
switch (di) {
case 4:
cd[3] -= ss;
case 3:
cd[2] -= ss;
case 2:
cd[1] -= ss;
case 1:
cd[0] -= ss;
}
}
return rv;
}
/* Given the new intended device coordinates, */
/* return non-zero if the point would be clipped. */
static int
ofps_would_clip_point(ofps *s, double *d) {
int e;
double ss;
for (ss = 0.0, e = 0; e < s->di; e++) {
if (d[e] < (s->imin[e] - ILIMITEPS))
return 1;
else if (d[e] > (s->imax[e] + ILIMITEPS))
return 1;
ss += d[e];
}
if (ss > (s->ilimit + ILIMITEPS))
return 1;
return 0;
}
/* Return a out of gamut value. */
/* 0.0 is returned if the posistion is in gamut */
static double ofps_oog(ofps *s, double *p) {
int e, di = s->di;
double ss, oog = 0.0;
for (ss = 0.0, e = 0; e < di; e++) {
if (p[e] < (s->imin[e])) {
double tt = s->imin[e] - p[e];
if (tt > oog) oog = tt;
} else if (p[e] > (s->imax[e])) {
double tt = p[e] - s->imax[e];
if (tt > oog) oog = tt;
}
ss += p[e];
}
if (ss > s->ilimit) {
double tt;
ss = (ss - s->ilimit)/di; /* Axis aligned distance to ink limit */
tt = sqrt((double)di) * ss; /* Diagonal distance to ink limit */
if (tt > oog)
oog = tt;
}
return oog;
}
/* Unbounded perceptual lookup. */
/* return nz if it was actually clipped and extended */
static int ofps_cc_percept(ofps *s, double *v, double *p) {
co cp;
int clip;
clip = ofps_clip_point(s, cp.p, p);
if (s->pcache) { /* In line this for speed */
int e, di = s->di;
s->pcache->interp(s->pcache, &cp);
for (e = 0; e < di; e++)
v[e] = cp.v[e];
} else {
s->percept(s->od, v, cp.p);
}
/* Extend perceptual value using matrix model */
if (clip) {
int e, di = s->di;
double mcv[MXPD], zv[MXPD];
#ifdef DEBUG
if (s->pmod_init == 0)
error("ofps_cc_percept() called before pmod has been inited");
#endif
/* Lookup matrix mode of perceptual at clipped device */
icxCubeInterp(s->pmod, di, di, mcv, cp.p);
/* Compute a correction factor to add to the matrix model to */
/* give the actual perceptual value at the clipped location */
for (e = 0; e < di; e++)
zv[e] = v[e] - mcv[e];
/* Compute the unclipped matrix model perceptual value */
icxCubeInterp(s->pmod, di, di, v, p);
/* Add the correction value to it */
for (e = 0; e < di; e++)
v[e] += zv[e];
}
return clip;
}
/* --------------------------------------------------- */
/* Vertex alloc/free support */
/* Check if a vertex is in the cache index, */
/* and return it if it is. Return NULL otherwise */
static vtx *vtx_cache_get(ofps *s, int *nix) {
int e, di = s->di;
unsigned int hash;
vtx *vx;
hash = (unsigned int)nix[MXPD+1]; /* We assume it was put there by sort */
for (vx = s->vch[hash]; vx != NULL; vx = vx->chn) {
for (e = 0; e <= di; e++) { /* See if it is a match */
if (nix[e] != vx->nix[e])
break;
}
if (e > di) { /* It is */
return vx;
}
}
return vx;
}
/* Add a vertex to the cache index */
static void vtx_cache_add(ofps *s, vtx *vv) {
int e, di = s->di;
unsigned int hash;
hash = (unsigned int)vv->nix[MXPD+1];
/* Add it to the list */
vv->chn = s->vch[hash];
if (s->vch[hash] != NULL)
s->vch[hash]->pchn = &vv->chn;
s->vch[hash] = vv;
vv->pchn = &s->vch[hash];
}
/* Remove a vertex from the cache index */
static void vtx_cache_rem(ofps *s, vtx *vv) {
int e, di = s->di;
unsigned int hash;
vtx *vx;
hash = (unsigned int)vv->nix[MXPD+1];
for (vx = s->vch[hash]; vx != NULL; vx = vx->chn) {
if (vx == vv) {
if (vx->pchn != NULL) {
*vx->pchn = vx->chn;
if (vx->chn != NULL)
vx->chn->pchn = vx->pchn;
}
return;
}
}
/* Hmm. not in cache */
}
/* Each vtx returned gets a unique serial number */
static vtx *new_vtx(ofps *s) {
vtx *vv;
if (s->fvtx != NULL) { /* re-use one we've got */
vv = s->fvtx;
s->fvtx = vv->link;
memset((void *)vv, 0, sizeof(vtx));
} else {
if ((vv = (vtx *)calloc(sizeof(vtx), 1)) == NULL)
error("ofps: malloc failed on new vertex");
}
/* Link vertex to currently used list */
vv->link = s->uvtx;
if (s->uvtx != NULL)
s->uvtx->plp = &vv->link;
s->uvtx = vv;
vv->plp = &s->uvtx;
vv->no = s->nxvno++;
s->nv++;
s->nvtxcreated++;
vv->fuptol = NUMTOL;
return vv;
}
/* Remove a vertx from the used list, and put it on the hidden list. */
/* (Used for making inside and outside vertexes unavailabe) */
static void remu_vtx(ofps *s, vtx *v) {
//printf("~1 remu_vtx called on no %d\n",v->no);
/* Remove it from the used list */
if (v->plp != NULL) { /* If is on used list, remove it */
*v->plp = v->link;
if (v->link != NULL)
v->link->plp = v->plp;
}
v->plp = NULL;
v->link = NULL;
/* Add it to the hidden list */
v->link = s->hvtx;
if (s->hvtx != NULL)
s->hvtx->plp = &v->link;
s->hvtx = v;
v->plp = &s->hvtx;
s->nv--; /* Don't count hidden verts */
}
/* Remove a vertex from the cache and spatial accelleration grid, */
/* and and the used list. */
static void del_vtx1(ofps *s, vtx *vx) {
node *nn, *nnn;
if (vx->plp != NULL) { /* If is on used list, remove it */
*vx->plp = vx->link;
if (vx->link != NULL)
vx->link->plp = vx->plp;
s->nv--;
}
if (vx->pfchl != NULL) { /* If is on fixup check list, remove it */
*vx->pfchl = vx->fchl;
if (vx->fchl != NULL)
vx->fchl->pfchl = vx->pfchl;
}
vx->pfchl = NULL;
vx->fchl = NULL;
if (vx->ofake == 0) {
/* Remove it from cache */
vtx_cache_rem(s, vx);
/* Remove it from spatial accelleration grid */
ofps_rem_vacc(s, vx);
/* Remove it from seeding group */
ofps_rem_vseed(s, vx);
}
/* Remove vertex from the s->svtxs[] list */
/* so that it doesn't get used in fixups. */
if (vx->psvtxs != NULL) {
*vx->psvtxs = NULL;
vx->psvtxs = NULL;
}
}
/* Delete a vertex by removing it from the cache and spatial accelleration grid, */
/* and then moving it to the free list */
/* (It's assumed that it's been removed from all other */
/* structures by the caller) */
static void del_vtx(ofps *s, vtx *vx) {
//printf("~1 del_vtx called on no %d\n",vx->no);
/* Remove it from various lists */
del_vtx1(s, vx);
/* Free vertex net neighbours list */
if (vx->nv != NULL) {
/* !!!! if we need this, we're referencing deleted vertexes !!!! */
/* vx->nnv = vx->_nnv = 0; */
free(vx->nv);
/* vx->nv = NULL; */
}
/* Add to free list */
vx->link = s->fvtx;
vx->plp = NULL;
s->fvtx = vx;
s->nvtxdeleted++;
}
/* Add a vertex to a vertex's net */
static void vtx_add_vertex(ofps *s, vtx *vv, vtx *vx) {
int i;
//printf("~1 Adding vertex no %d comb %s vm %s to vtx %d\n",vx->no,pcomb(s->di,vx->nix),psm(s,&vx->vm),vv->no);
//printf("~1 Adding vertex no %d to vtx no %d\n",vx->no,vv->no);
//printf("~1 Before add, no %d list is :",vv->no); for (i = 0; i < vv->nnv; i++) printf(" %d",vv->nv[i]->no); printf("\n");
if (vv->_nnv == 0) {
vv->_nnv = 4; /* Initial allocation */
if ((vv->nv = (vtx **)malloc(sizeof(vtx *) * vv->_nnv)) == NULL)
error("ofps: malloc failed on node vertex pointers");
} else if (vv->nnv >= vv->_nnv) {
vv->_nnv *= 2; /* Double allocation */
if ((vv->nv = (vtx **)realloc(vv->nv, sizeof(vtx *) * vv->_nnv)) == NULL)
error("ofps: realloc failed on node vertex pointers");
}
#ifdef DEBUG
{
int i;
/* Check that we're not adding ourself */
if (vx == vv) {
printf("Adding vtx no %d comb %s to itself!\n",vx->no,pcomb(s->di,vx->nix)); fflush(stdout);
error("Adding vtx no %d comb %s to itself!\n",vx->no,pcomb(s->di,vx->nix));
}
/* Check that the vertex is not already here */
for (i = 0; i < vv->nnv; i++) {
if (vx == vv->nv[i]) {
printf("Adding vtx no %d comb %s to vtx %d when already there!\n",vx->no,pcomb(s->di,vx->nix),vv->no); fflush(stdout);
fprintf(stderr,"Adding vtx no %d comb %s to vtx %d when already there!\n",vx->no,pcomb(s->di,vx->nix),vv->no); fflush(stdout);
//*((char *)0) = 55;
return;
}
}
}
#endif /* DEBUG */
vv->nv[vv->nnv++] = vx;
//printf("~1 After add, no %d list is :",vv->no); for (i = 0; i < vv->nnv; i++) printf(" %d",vv->nv[i]->no); printf("\n");
}
/* Delete a vertex to a vertex's net */
static void vtx_rem_vertex(ofps *s, vtx *vv, vtx *vx) {
int i, j;
//printf("~1 Removing vertex no %d comb %s vm %s from vtx %d\n",vx->no,pcomb(s->di,vx->nix),psm(s,&vx->vm),vv->no);
//printf("~1 Before delete, no %d list is :",vv->no); for (i = 0; i < vv->nnv; i++) printf(" %d",vv->nv[i]->no); printf("\n");
for (i = j = 0; i < vv->nnv; i++) {
if (vv->nv[i] != vx) {
vv->nv[j] = vv->nv[i];
j++;
}
}
vv->nnv = j;
//printf("~1 After delete, no %d list is :",vv->no); for (i = 0; i < vv->nnv; i++) printf(" %d",vv->nv[i]->no); printf("\n");
}
/* Given two vertexes, check if they are neighbours, and if they are, */
/* add them to each others neighbourhood. */
/* If fixup is set, first check that they arn't already neighbours */
/* Return nz if ther were added to each other */
static int vtx_cnd_biadd_vtx(ofps *s, vtx *vx1, vtx *vx2, int fixup) {
int f, ff, e, di = s->di;
int aa, bb, cc; /* Probable hit check */
int nnm, nmix;
if (vx1 == vx2)
return 0;
#ifdef NEVER /* vertex net needs all neighbours ? */
#ifdef INDEP_SURFACE
/* Can only have a net between them if they are visible to each other */
if (sm_vtx_vtx(s, vx1, vx2) == 0)
return 0;
#endif
#endif
/* Use the nixm to quickly check if all but one parent node matches */
aa = vx1->nix[MXPD+2]; /* nixm */
bb = vx2->nix[MXPD+2]; /* nixm */
if ((aa & bb) == 0 || (cc = aa & ~bb, (cc & (cc-1)) != 0)) {
return 0; /* It's certainly not */
}
/* Do an exact check of all except one node match */
for (nnm = ff = e = 0; e <= di; e++) {
for (f = ff; f <= di; f++) {
if (vx1->nix[e] == vx2->nix[f]) {
ff = f; /* Start from here next time */
break;
}
if (vx1->nix[e] > vx2->nix[f]) /* No point in looking further */
f = di;
}
if (f > di) { /* Didn't match */
if (++nnm > 1)
break;
nmix = e;
}
}
if (e <= di) {
return 0; /* No match */
}
if (nnm == 0) {
fflush(stdout);
error("ofps: two vertexes have the same nodes !\n"
"no %d at %s nix %s\nno %d at %s nix %s",
vx1->no,ppos(di,vx1->p),pcomb(di,vx1->nix),
vx2->no,ppos(di,vx2->p),pcomb(di,vx2->nix));
}
/* If fixup or not INDEP_SURFACE, check that the vertex */
/* is not already here */
#ifndef INDEP_SURFACE
if (fixup)
#endif
{
int i;
vtx *va = vx1, *vb = vx2;
if (vx1->nnv > vx2->nnv) { /* Search the shortest list */
va = vx2;
vb = vx1;
}
for (i = 0; i < va->nnv; i++) {
if (vb == va->nv[i]) {
return 0;
}
}
}
//printf("~1 Adding net between vtx no %d and no %d\n",vx1->no,vx2->no);
/* vx2 is a neighbour, so add it to the vtx net */
vtx_add_vertex(s, vx1, vx2);
/* The reverse must apply too */
vtx_add_vertex(s, vx2, vx1);
return 1;
}
#ifdef NEVER /* Not used */
/* Clear any veroinoi content of a vertex, but not the vertex itself. */
static void vtx_clear(ofps *s, vtx *v) {
/* Clear the list of vertex net neighbours */
v->nnv = 0;
}
/* Free any allocated content of a vertex, but not the vertex itself. */
static void vtx_free(ofps *s, vtx *v) {
//printf("~1 freeing node ix %d and all contents\n",v->ix);
/* Free up list of Vertex net neighbours */
if (v->nv != NULL) {
v->nnv = v->_nnv = 0;
free(v->nv);
v->nv = NULL;
}
}
#endif /* NEVER */
/* vertex binary tree support */
static int vtx_aat_cmp_eperr(const void *p1, const void *p2) {
return ((vtx *)p1)->eperr == ((vtx *)p2)->eperr ? 0 :
(((vtx *)p1)->eperr < ((vtx *)p2)->eperr ? -1 : 1);
}
static int vtx_aat_cmp_eserr(const void *p1, const void *p2) {
return ((vtx *)p1)->eserr == ((vtx *)p2)->eserr ? 0 :
(((vtx *)p1)->eserr < ((vtx *)p2)->eserr ? -1 : 1);
}
/* --------------------------------------------------- */
/* Midpoint alloc/free support */
/* Each mid returned gets a unique serial number */
/* and a refc or 0 */
static mid *new_mid(ofps *s) {
mid *p;
if (s->fmid != NULL) { /* re-use one we've got */
p = s->fmid;
s->fmid = p->link;
memset((void *)p, 0, sizeof(mid));
} else {
if ((p = (mid *)calloc(sizeof(mid), 1)) == NULL)
error("ofps: malloc failed on new midpoint");
}
/* Link midpoint to currently used list */
p->link = s->umid;
if (s->umid != NULL)
s->umid->plp = &p->link;
s->umid = p;
p->plp = &s->umid;
p->no = s->nxmno++;
return p;
}
/* Decrement reference count, and midpoint to the free list */
static void del_mid(ofps *s, mid *p) {
//printf("~1 del_mid called on no %d, refc = %d\n",p->no,p->refc);
if (--p->refc <= 0) {
if (p->plp != NULL) { /* If is on used list, remove it */
*p->plp = p->link;
if (p->link != NULL)
p->link->plp = p->plp;
}
p->link = s->fmid; /* Add to free list */
p->plp = NULL;
s->fmid = p;
p->refc = 0;
}
}
/* --------------------------------------------------- */
/* Node basic support functions */
/* Clear any veroinoi content of a node, but not the node itself. */
static void node_clear(ofps *s, node *p) {
/* Clear the list of Voronoi verticies */
p->nvv = 0;
/* Clear any midpoints and nodes */
while (p->nvn > 0) {
if (p->mm[--p->nvn] != NULL)
del_mid(s, p->mm[p->nvn]);
}
}
/* Free any allocated content of a node, but not the node itself. */
static void node_free(ofps *s, node *p) {
//printf("~1 freeing node ix %d and all contents\n",p->ix);
/* Free up list of Voronoi verticies */
if (p->vv != NULL) {
free(p->vv);
p->vv = NULL;
p->nvv = p->_nvv = 0;
}
/* Free up list of voronoi node indexes */
if (p->vn != NULL) {
while (p->nvn > 0) {
if (p->mm[--p->nvn] != NULL)
del_mid(s, p->mm[p->nvn]);
}
p->nvn = p->_nvn = 0;
free(p->vn);
free(p->mm);
p->vn = NULL;
p->mm = NULL;
}
p->nsp = 0; /* No list of surface planes */
}
/* Add a vertex to the node vertex list */
static void node_add_vertex(ofps *s, node *pp, vtx *vx) {
//printf("~1 Adding vertex no %d comb %s vm %s to node %d\n",vx->no,pcomb(s->di,vx->nix),psm(s,&vx->vm),pp->ix);
#ifdef DEBUG
if (vx->del)
warning("!!!!! adding vertex no %d with delete flag set!!!",vx->no);
#endif
if (pp->_nvv == 0) {
pp->_nvv = 4; /* Initial allocation */
if ((pp->vv = (vtx **)malloc(sizeof(vtx *) * pp->_nvv)) == NULL)
error("ofps: malloc failed on node vertex pointers");
} else if (pp->nvv >= pp->_nvv) {
pp->_nvv *= 2; /* Double allocation */
if ((pp->vv = (vtx **)realloc(pp->vv, sizeof(vtx *) * pp->_nvv)) == NULL)
error("ofps: realloc failed on node vertex pointers");
}
#ifdef DEBUG
{
int i;
/* Check that the vertex is not already here */
for (i = 0; i < pp->nvv; i++) {
if (vx == pp->vv[i]) {
printf("Adding vtx no %d comb %s when already there!\n",vx->no,pcomb(s->di,vx->nix)); fflush(stdout);
error("Adding vtx no %d comb %s when already there!",vx->no,pcomb(s->di,vx->nix)); fflush(stdout);
}
}
}
#endif /* DEBUG */
pp->vv[pp->nvv++] = vx;
#ifdef NEVER
#ifdef DEBUG
{
int e, di = s->di;
printf("~1 +++ Node ix %d add vtx no %d pos %s err %f @ %d:",pp->ix,vx->no,ppos(di,vx->p),vx->eperr,pp->nvv);
for (e = 0; e < pp->nvv; e++)
printf("%d ",pp->vv[e]->no);
printf("\n");
}
#endif
#endif
}
/* Remove a vertex from the node vertex list */
static void node_rem_vertex(ofps *s, node *pp, vtx *vx) {
int i, j;
//printf("~1 Removing vertex no %d comb %s vm %s from node ix %d\n",vx->no,pcomb(s->di,vx->nix),psm(s,&vx->vm),pp->ix);
//printf("~1 Before delete, no %d list is :",vv->no); for (i = 0; i < vv->nnv; i++) printf(" %d",vv->nv[i]->no); printf("\n");
for (i = j = 0; i < pp->nvv; i++) {
if (pp->vv[i] != vx) {
pp->vv[j] = pp->vv[i];
j++;
}
}
pp->nvv = j;
}
/* Add a node index to the node */
static void node_add_nix(ofps *s, node *pp, int ix) {
if (pp->_nvn == 0) {
pp->_nvn = 4; /* Initial allocation */
if ((pp->vn = (int *)malloc(sizeof(int) * pp->_nvn)) == NULL)
error("ofps: malloc failed on node index list");
if ((pp->mm = (mid **)malloc(sizeof(mid *) * pp->_nvn)) == NULL)
error("ofps: malloc failed on midpoint pointer list");
} else if (pp->nvn >= pp->_nvn) {
pp->_nvn *= 2; /* Double allocation */
if ((pp->vn = (int *)realloc(pp->vn, sizeof(int) * pp->_nvn)) == NULL)
error("ofps: realloc failed on node index list");
if ((pp->mm = (mid **)realloc(pp->mm, sizeof(mid *) * pp->_nvn)) == NULL)
error("ofps: realloc failed on midpoint pointer list");
}
pp->vn[pp->nvn] = ix;
pp->mm[pp->nvn++] = NULL;
#ifdef NEVER
#ifdef DEBUG
{
int e, di = s->di;
printf("~1 +++ Node ix %d add node ix %d at %s @ %d: ",pp->ix,ix,ppos(di,pp->p),pp->nvn);
for (e = 0; e < pp->nvn; e++)
printf("%d ",pp->vn[e]);
printf("\n");
}
#endif
#endif
}
/* Recompute a nodes neighborhood nodes. */
/* (Invalidates and deletes any midpoints) */
static void node_recomp_nvn(
ofps *s,
node *pp
) {
int e, di = s->di;
int i, j, k;
#ifdef DEBUG
printf("node_recomp_nvn for node ix %d\n",pp->ix);
#endif
/* Clear any midpoints and nodes */
while (pp->nvn > 0) {
if (pp->mm[--pp->nvn] != NULL)
del_mid(s, pp->mm[pp->nvn]);
}
s->nvnflag++; /* Make sure each node is only added once */
pp->nvnflag = s->nvnflag; /* Don't put self in list */
for (i = 0; i < pp->nvv; i++) { /* For each vertex */
double rads;
vtx *vv = pp->vv[i];
for (j = 0; j <= di; j++) { /* For each node in vertex */
int ix = vv->nix[j];
node *ap = s->n[ix];
if (ap->nvnflag == s->nvnflag)
continue; /* Already done that node */
node_add_nix(s, pp, ix);
ap->nvnflag = s->nvnflag; /* Don't worry about it again */
}
}
}
/* Sort a vertex node index array of di+1 nodes, */
/* and add a hash at MXDP+1, and nixm at MXDP+2 */
/* This is to speed up searching for a match */
/* Sort largest to smallest (so fake gamut nodes are last) */
static void sort_nix(ofps *s, int *nix) {
int i, j, t;
int di = s->di; /* There are di+1 nodes */
unsigned int hash = 0;
int nixm = 0;
/* Do a really simple exchange sort */
for (i = 0; i < di; i++) {
for (j = i+1; j <= di; j++) {
if (nix[i] < nix[j]) {
t = nix[j];
nix[j] = nix[i];
nix[i] = t;
}
}
}
/* And then compute the hash and nixm */
for (i = 0; i <= di; i++) {
int bitp, ix = nix[i];
hash = hash * 17 + nix[i];
bitp = 31 & (ix + (ix >> 4) + (ix >> 8) + (ix >> 12));
nixm |= (1 << bitp);
}
hash %= VTXCHSIZE;
nix[MXPD+1] = (int)hash;
nix[MXPD+2] = nixm;
}
/* Check if the given locate is on the gamut boundary surface, */
/* and return the corresponding plane mask */
static unsigned int check_pos_gsurf(ofps *s, double *p) {
int i, e, di = s->di;
unsigned int pmask = 0;
/* For all the gamut boundary planes */
for (i = 0; i < s->nbp; i++) {
pleq *vp = &s->gpeqs[i];
double v;
for (v = vp->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += vp->pe[e] * p[e];
/* See if this location close to, or outside boundary plane */
if (v > -s->surftol)
pmask |= (1 << i);
}
#ifdef MAXINDEP_2D
if (s->sc[pmask].valid == 0)
pmask = 0;
#endif
return pmask;
}
/* Check if the given node is on the gamut boundary surface, */
/* and record the number of surfaces it is on. */
/* Return nz if the node is on one or more gamut boundaries. */
/* Set the state of the node clip flag too. */
static int det_node_gsurf(ofps *s, node *n, double *p) {
int i, e, di = s->di;
double ss;
n->nsp = 0;
n->pmask = 0;
/* For all the gamut boundary planes */
for (i = 0; i < s->nbp; i++) {
pleq *vp = &s->gpeqs[i];
double v;
for (v = vp->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += vp->pe[e] * p[e];
/* See if this location close to, or outside boundary plane */
if (v > -s->surftol) {
/* Add pointer to plane it falls on */
n->sp[n->nsp++] = vp;
n->pmask |= (1 << i);
if (n->nsp > MXPD+1)
error("Assert in ofps det_node_gsurf : nsp %d > MXPD +1 %d",n->nsp, MXPD+1);
}
}
#ifdef MAXINDEP_2D
if (s->sc[n->pmask].valid == 0) {
n->pmask = 0;
n->nsp = 0;
}
#endif
//printf("~1 node pmask = 0x%x\n",n->pmask);
return (n->nsp > 0);
}
/* Compute a cmask from an nix */
static unsigned int comp_cmask(ofps *s, int *nix) {
unsigned int smask = 0, cmask = ~0;
int i, e, di = s->di;
/* The composition mask indicates all the common surface planes */
/* that a vertexes parent nodes lie on. Given this, one expects */
/* the resulting location to be the same of within this. */
for (e = 0; e <= di; e++) {
int ix = nix[e];
if (ix < 0 && ix >= (-s->nbp)) { /* If fake surface node */
smask |= 1 << (-ix-1);
} else if (ix >= 0) { /* If real node */
cmask &= s->n[ix]->pmask;
}
}
if (smask != 0)
cmask &= smask;
#ifdef MAXINDEP_2D
if (s->sc[cmask].valid == 0)
cmask = 0;
#endif
return cmask;
}
/* Check if the given vertex is on the gamut boundary surface, */
/* and record the number of surfaces it is on. */
/* Also compute its cmask based on its parent nodes. */
/* Set the state of the clip flag too. */
static void det_vtx_gsurf(ofps *s, vtx *vx) {
int i, e, di = s->di;
unsigned int smask = 0;
vx->nsp = 0;
/* The composition mask indicates all the common surface planes */
/* that a vertexes parent nodes lie on. Given this, one expects */
/* the resulting location to be the same of within this. */
vx->cmask = ~0;
for (e = 0; e <= di; e++) {
int ix = vx->nix[e];
if (ix < 0 && ix >= -s->nbp) { /* If fake surface node */
smask |= 1 << (-ix-1);
} else if (ix >= 0) {
vx->cmask &= s->n[ix]->pmask;
}
}
if (smask != 0)
vx->cmask &= smask;
#ifdef MAXINDEP_2D
if (s->sc[vx->cmask].valid == 0)
vx->cmask = 0;
#endif
/* For all the gamut boundary planes */
for (i = 0; i < s->nbp; i++) {
pleq *vp = &s->gpeqs[i];
double v;
for (v = vp->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += vp->pe[e] * vx->p[e];
/* See if this location close to, or outside boundary plane */
if (v > -s->surftol) {
vx->sp[vx->nsp++] = vp;
vx->pmask |= (1 << i);
if (vx->nsp > di+1)
error("Assert in ofps det_vtx_gsurf : nsp %d > di+1 %d",vx->nsp, di+1);
}
}
#ifdef MAXINDEP_2D
if (s->sc[vx->pmask].valid == 0) {
vx->pmask = 0;
vx->nsp = 0;
}
#endif
//printf("~1 vertex pmask = 0x%x, cmask = 0x%x\n",vx->pmask,vx->cmask);
}
/* Given a device position and a list of surface planes, */
/* move the position to lie on the closest location */
/* on those planes. */
static void confineto_gsurf(ofps *s, double *p, pleq **psp, int nsp) {
if (nsp > 0) { /* It's a surface point, so keep it on the surface */
int i, j, e, di = s->di;
double nn, np, q;
/* Special case the common situation for speed. */
if (nsp == 1) {
pleq *sp = psp[0];
/* Compute the dot product of the plane equation normal */
for (nn = 0.0, e = 0; e < di; e++)
nn += sp->pe[e] * sp->pe[e];
/* Compute the dot product of the plane equation and the point */
for (np = 0.0, e = 0; e < di; e++)
np += sp->pe[e] * p[e];
/* Compute the parameter */
q = (sp->pe[di] + np)/nn;
/* Compute the closest point */
for (e = 0; e < di; e++)
p[e] -= q * sp->pe[e];
/* General case using matrix solution. */
/* We compute the proportion of each plane normal vector to add to point */
/* to map it onto all planes simultaniously (ie. to map the point to */
/* the intersection of all the planes). */
} else if (nsp > 1) {
double **ta, *TTA[MXPD + 1], TA[MXPD+1][MXPD + 1];
double *tb, TB[MXPD + 1];
for (e = 0; e < nsp; e++)
TTA[e] = TA[e];
ta = TTA;
tb = TB;
/* For each combination of planes */
for (i = 0; i < nsp; i++) {
pleq *spi = psp[i];
for (j = i; j < nsp; j++) {
pleq *spj = psp[j];
double vv;
/* Compute dot product of the two normals */
for (vv = 0.0, e = 0; e < di; e++)
vv += spi->pe[e] * spj->pe[e];
ta[j][i] = ta[i][j] = vv; /* Use symetry too */
}
/* Compute right hand side */
for (tb[i] = 0.0, e = 0; e < di; e++)
tb[i] += spi->pe[e] * p[e]; /* Dot prod of plane normal and point */
tb[i] += spi->pe[di]; /* plus plane constant */
}
/* Solve the simultaneous linear equations A.x = B */
/* Return 1 if the matrix is singular, 0 if OK */
if (solve_se(ta, tb, nsp) == 0) {
/* Compute the closest point */
for (i = 0; i < nsp; i++) {
pleq *spi = psp[i];
for (e = 0; e < di; e++)
p[e] -= tb[i] * spi->pe[e];
}
}
}
/* The mapping may leave it out of gamut */
ofps_clip_point2(s, p, p);
}
}
/* Given a device position and a list of surface planes, */
/* check that the point lies on all the planes. */
/* Return NZ if it does, Z if it doesn't */
static int checkon_gsurf(ofps *s, double *p, pleq **psp, int nsp) {
int i, e, di = s->di;
double nn, np, q;
if (nsp == 0)
return 1;
for (i = 0; i < nsp; i++) {
pleq *vp = psp[i];
double v;
for (v = vp->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += vp->pe[e] * p[e];
/* See if this location close to, or outside boundary plane */
if (fabs(v) > s->surftol) {
return 0;
}
}
return 1;
}
/* Compute the estimated positioning error given two locations. */
/* [ This seems to be the critical inner loop in regard to */
/* overall speed. The dominant callers are dnsq_solver() 10%, */
/* followed by add_node2voronoi() 5%, others <= 1% ] */
#ifdef NEVER /* Allow performance trace on eperr usage */
static double ofps_comp_eperr(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp);
static double ofps_comp_eperr1(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr2(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr3(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr4(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr5(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr6(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr7(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr8(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
static double ofps_comp_eperr9(ofps *s, double *pddist, double *v, double *p, double *nv, double *np, int nsp) {
return ofps_comp_eperr(s, pddist, v, p, nv, np, nsp); }
#else /* Production code */
#define ofps_comp_eperr1 ofps_comp_eperr
#define ofps_comp_eperr2 ofps_comp_eperr
#define ofps_comp_eperr3 ofps_comp_eperr
#define ofps_comp_eperr4 ofps_comp_eperr
#define ofps_comp_eperr5 ofps_comp_eperr
#define ofps_comp_eperr6 ofps_comp_eperr
#define ofps_comp_eperr7 ofps_comp_eperr
#define ofps_comp_eperr8 ofps_comp_eperr
#define ofps_comp_eperr9 ofps_comp_eperr
#endif
static double ofps_comp_eperr(
ofps *s,
double *pddist, /* If not NULL, return the device distance */
double *v, /* Device perceptual value */
double *p, /* Device sample location to be evaluated */
double *nv, /* Other perceptual value */
double *np, /* Other sample location value */
int nsp /* Number of surface planes */
) {
int ii, e, f, di = s->di;
int isc;
double tt, ddist, pdist;
double eperr;
/* Uncertaintly error computed from device and perceptual distance */
#ifndef NEVER /* unrolled code */
#if MXPD > 4
# error "ofps.c: Need to expand switch code for MXPD > 4"
#endif
/* Unrole the loop */
pdist = ddist = 0.0;
switch (di) {
case 4:
tt = (p[3] - np[3]); /* Device distance */
ddist += tt * tt;
tt = (v[3] - nv[3]); /* Perceptual distance */
pdist += tt * tt;
case 3:
tt = (p[2] - np[2]); /* Device distance */
ddist += tt * tt;
tt = (v[2] - nv[2]); /* Perceptual distance */
pdist += tt * tt;
case 2:
tt = (p[1] - np[1]); /* Device distance */
ddist += tt * tt;
tt = (v[1] - nv[1]); /* Perceptual distance */
pdist += tt * tt;
case 1:
tt = (p[0] - np[0]); /* Device distance */
ddist += tt * tt;
tt = (v[0] - nv[0]); /* Perceptual distance */
pdist += tt * tt;
}
#else
/* General code */
for (pdist = ddist = 0.0, e = 0; e < di; e++) {
/* Compute the device distance */
tt = (p[e] - np[e]);
ddist += tt * tt;
/* Compute the perceptual distance */
tt = (v[e] - nv[e]);
pdist += tt * tt;
}
#endif
if (pddist != NULL)
*pddist = ddist;
ddist *= 100.0 * 100.0;
//printf("~1 Device distance = %f, dev error = %f\n",ddist,s->devd_wght * ddist);
ddist = sqrt(ddist);
pdist = sqrt(pdist);
eperr = s->devd_wght * ddist + s->perc_wght * pdist;
#ifdef GAMUT_EDGE_FUDGE
/* Fudge factor to prevent gap at gamut boundaries */
if (nsp > 0) {
// int nn;
// for (nn = 0; nn < nsp; nn++)
eperr *= GAMUT_EDGE_FUDGE;
}
#endif /* GAMUT_EDGE_FUDGE */
//printf("~1 Percept distance = %f, perc error = %f\n",pdist,s->perc_wght * pdist);
return eperr;
}
/* Compute the per node estimated position and interpolation errors */
/* given a location and a list of up to di+1 neighborhood measurement nodes. */
static void ofps_pn_eperr(
ofps *s,
double *ce, /* return the curvature/interpolation error for each node (may be NULL) */
double *ee, /* return the uncertaintly error for each node */
double *sv, /* Perceptual value if known, othewise NULL */
double *sp, /* Device sample location to be evaluated */
node **nds, /* Array of pointers to measurement nodes */
int nnds /* Number of measurement nodes (>= 1) */
) {
int ii, e, di = s->di;
node *np;
double _sv[MXPD]; /* Sample perceptual value */
double iv[MXPD]; /* Interpolated perceptual value */
/* Lookup perceptual value at sample point location */
if (sv == NULL) {
sv = _sv;
ofps_cc_percept(s, sv, sp);
}
/* Uncertaintly error computed from device and perceptual distance */
for (ii = 0; ii < nnds; ii++)
ee[ii] = ofps_comp_eperr1(s, NULL, sv, sp, nds[ii]->v, nds[ii]->p, nds[ii]->nsp);
if (ce == NULL)
return;
/* This could be made more efficient by only computing it for every */
/* vertex during the initial seeding, and then on subsequent */
/* passes only computing it once the re-seed/fixups are done. */
if (s->curv_wght != 0.0) { /* Don't waste the time unless it's used */
/* Compute an error estimate that's related to curvature */
for (ii = 0; ii < nnds; ii++) {
double cp[MXPD]; /* Midway points location */
double civ[MXPD]; /* Midway points interpolated perceptual value */
double cv[MXPD]; /* Midway points actual perceptual value */
/* Compute a point midway between the sample location and the node */
for (e = 0; e < di; e++) {
cp[e] = 0.5 * (sp[e] + nds[ii]->p[e]);
civ[e] = 0.5 * (sv[e] + nds[ii]->v[e]);
}
/* Look the actual perceptual value */
/* (Computing this for each vertex consumes about 8% of execution time) */
ofps_cc_percept(s, cv, cp);
/* Compute the difference between the interpolated and actual perceptual values */
for (ce[ii] = 0.0, e = 0; e < di; e++) {
double tt;
tt = civ[e] - cv[e];
ce[ii] += tt * tt;
}
ce[ii] = s->curv_wght * sqrt(ce[ii]);
#ifdef GAMUT_EDGE_FUDGE
/* Fudge factor to prevent gap at gamut boundaries */
if (nds[ii]->nsp > 0) {
// int nn;
// for (nn = 0; nn < nds[ii]->nsp; nn++)
ce[ii] *= GAMUT_EDGE_FUDGE;
}
#endif /* GAMUT_EDGE_FUDGE */
}
} else {
for (ii = 0; ii < nnds; ii++)
ce[ii] = 0.0;
}
}
/* Compute the estimated position error given the results */
/* of ofps_pn_eperr() */
static double ofps_eperr2(
double *ee, /* Uncertainty error for each node */
int nnds /* Number of measurement nodes */
) {
double eperr;
int ii;
for (eperr = 1e80, ii = 0; ii < nnds; ii++) {
if (ee[ii] < eperr)
eperr = ee[ii];
}
//printf("~1 ofps_eperr returning %f\n",eperr);
return eperr;
}
/* Compute the estimated sampling error of a location given */
/* the results of ofps_pn_eperr() */
static double ofps_eserr2(
double *ce, /* The estimated curvature/interpolation error for each node */
double *ee, /* Uncertainty error for each node */
int nnds /* Number of measurement nodes */
) {
int ii;
double eserr;
double mxce;
#ifdef NEVER
/* We assume errors are inverse probabilities, */
/* and sum inverse squares. */
for (mxce = 0.0, eserr = 0.0, ii = 0; ii < nnds; ii++) {
double tt;
tt = ee[ii] * ee[ii];
if (tt > NUMTOL)
eserr += 1.0/tt;
else { /* One error is close to zero */
eserr = 0.0;
break;
}
if (ce[ii] > mxce)
mxce = ce[ii];
}
if (ii >= nnds)
eserr = 1.0/sqrt(eserr);
eserr += mxce;
#else /* This seems best ? */
/* Return nearest neighbor error metric for the moment, */
/* with the ce being the maximum of the surrounders. */
for (mxce = 0.0, eserr = 1e80, ii = 0; ii < nnds; ii++) {
if (ee[ii] < eserr)
eserr = ee[ii];
if (ce[ii] > mxce)
mxce = ce[ii];
}
eserr += mxce;
#endif
//printf("~1 ofps_eserr returning %f\n",eserr);
return eserr;
}
/* - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - */
/* Finding vertex location code using dnsqe() */
typedef struct {
double p[MXPD];
} loc;
/* Context for callback */
struct _vopt_cx {
ofps *s;
node *nds[MXPD+1]; /* List of real nodes */
int nn; /* Number of real nodes */
int on; /* Index of odd node */
pleq *sp[MXPD+1]; /* List of touched gamut surface planes */
int nsp; /* Number of touched gamut surface planes */
double srad; /* Search radius used */
double stp[MXPD]; /* Starting point used */
#ifdef DUMP_FERR
/* Debug: */
int debug; /* nz to trace search path */
loc *clist; /* List of points sampled */
int _nl; /* Allocated size */
int nl; /* Number of points */
#endif
}; typedef struct _vopt_cx vopt_cx;
/* calculate the functions at x[] */
int dnsq_solver( /* Return < 0 on abort */
void *fdata, /* Opaque data pointer */
int n, /* Dimenstionality */
double *x, /* Multivariate input values */
double *fvec, /* Multivariate output values */
int iflag /* Flag set to 0 to trigger debug output */
) {
vopt_cx *cx = (vopt_cx *)fdata;
ofps *s = cx->s;
int k, e, di = s->di;
int nn_1 = cx->nn-1;
double sv[MXPD];
double cee[MXPD+1], teperr;
#ifdef DUMP_FERR
/* record the points we visited */
if (cx->debug) {
if (cx->nl >= cx->_nl) {
cx->_nl = 2 * cx->_nl + 5;
if ((cx->clist = (loc *)realloc(cx->clist, sizeof(loc) * cx->_nl)) == NULL)
error("ofps: malloc failed on debug location array %d", cx->_nl);
}
for (e = 0; e < di; e++)
cx->clist[cx->nl].p[e] = x[e];
cx->nl++;
}
#endif
//printf("~1 dnsq_solver got %d nodes and %d planes\n",cx->nn,cx->nsp);
/* Get eperr at each real node */
ofps_cc_percept(s, sv, x); /* We have to compute it */
for (k = 0; k < cx->nn; k++) {
cee[k] = ofps_comp_eperr2(s, NULL, sv, x, cx->nds[k]->v, cx->nds[k]->p, cx->nds[k]->nsp);
}
//fprintf(stderr,"~1 maxeperr = %f\n",cmax);
//printf("~1 error =");
//for (k = 0; k < cx->nn; k++)
// printf(" %f",cee[k]);
//printf("\n");
/* We need to create nn-1 output values from nn eperr's */
/* cx->on is the odd one out. */
/* Difference to average (best) */
for (teperr = 0.0, k = 0; k < cx->nn; k++)
teperr += cee[k];
teperr /= (double)cx->nn;
for (k = e = 0; k < cx->nn; k++) {
if (k != cx->on) {
fvec[e++] = (teperr - cee[k]);
}
}
/* Compute plane errors */
for (k = 0; k < cx->nsp; k++) {
pleq *pl = cx->sp[k];
double v;
for (v = pl->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += pl->pe[e] * x[e];
fvec[nn_1 + k] = FGPMUL * v;
}
s->funccount++;
//for (k = 0; k < nn_1; k++)
//printf("~1 fvec[%d] = %f\n",k,fvec[k]);
//printf("dnsq_solver returning %s from %s\n",ppos(di,fvec),ppos(di,x));
//fprintf(stderr,"dnsq_solver returning %s from %s\n",ppos(di,fvec),ppos(di,x));
return 0;
}
/* Locate a vertex position that has the eperr from all the real nodes */
/* being equal. Set eperr, eserr and subjective value v[] too. */
/* vv->ceperr contains the current eperr that must be bettered. */
/* Return 0 if suceeded, 1 if best result is out of tollerance, 2 if failed. */
static int position_vtx(
ofps *s,
nodecomb *vv, /* Return the location and its error */
int startex, /* nz if current position is to be used as initial start point */
int repos, /* nz after an itteration and we expect out of gamut */
int fixup /* nz if doing fixups after itteration and expect out of gamut ??? */
) {
int e, di = s->di;
int k, ii;
double tw;
double atp[MXPD], mct[MXPD]; /* Average node position, middle of closest 2 nodes */
double osp[MXPD]; /* Original start position, start position */
double cdist, fdist; /* Closest/furthest two points distance apart */
double bsrad; /* Basic search radius, search radius */
double ftol = FTOL; /* Final eperr tolerance */
int tfev = 0, tcalls = 0; /* Track average successful fevs */
int maxfev = 50; /* Maximum function evaluations */
int tries, notries = MAXTRIES; /* Point to give up on. Giving up will error. */
vopt_cx cx, pcx; /* dnsq context + previous context */
#ifdef DEBUG
printf("Position_vtx called for comb %s\n",pcomb(di,vv->nix));
#endif
s->positions++;
s->sob->reset(s->sob);
#ifdef DUMP_FERR
cx.debug = 0;
#endif
/* Setup for dnsq to optimize for equal eperr */
cx.s = s;
/* Pointers to real nodes. Although we allow for the */
/* fake inner/outer nodes, eperr() will fail them later. */
for (ii = e = 0; e <= di; e++) {
if (vv->nix[e] >= 0 || vv->nix[e] < -s->nbp)
cx.nds[ii++] = s->n[vv->nix[e]];
}
cx.nn = ii;
if (ii == 0) {
fflush(stdout);
error("ofps: unexpectedely got no real nodes in vertex position %s",pcomb(di,vv->nix));
}
#ifdef DEBUG
printf("%d real nodes\n",ii);
for (k = 0; k < ii; k++)
printf("Node ix %d at %s\n",cx.nds[k]->ix,ppos(di,cx.nds[k]->p));
#endif
/* Setup gamut suface planes */
cx.nsp = 0;
if (ii < (di+1)) {
/* Create list of pointers to the gamut surface planes involved */
for (e = 0, k = ii; k <= di; e++, k++) {
#ifdef DEBUG
printf("Adding plane for node ix %d\n",vv->nix[k]);
#endif
cx.sp[e] = &s->gpeqs[-1 - vv->nix[k]];
}
cx.nsp = e;
}
/* If there is only one node, map it to the planes */
/* and we're done. */
if (ii == 1) {
double ee[MXPD+1];
for (e = 0; e < di; e++)
vv->p[e] = cx.nds[0]->p[e];
confineto_gsurf(s, vv->p, cx.sp, cx.nsp);
if (checkon_gsurf(s, vv->p, cx.sp, cx.nsp) == 0) {
#ifdef DEBUG
printf("Single node comb %s failed to confine to gamut surface\n",pcomb(di,vv->nix));
#endif
return 2;
}
/* Compute perceptual (can't clip because of confine) */
s->percept(s->od, vv->v, vv->p);
/* Compute the eperr's for each node. */
ofps_pn_eperr(s, vv->ce, ee, vv->v, vv->p, cx.nds, cx.nn);
/* Compute errors at returned location */
vv->eperr = ofps_eperr2(ee, cx.nn);
vv->eserr = ofps_eserr2(vv->ce, ee, cx.nn);
#ifdef DEBUG
printf("Single node, returning comb %s opt pos = %s, eperr = %f, eserr = %f\n",pcomb(di,vv->nix),ppos(di,vv->p),vv->eperr,vv->eserr);
#endif
return 0;
}
{
/* Compute average of real nodes */
for (e = 0; e < di; e++)
atp[e] = 0.0;
for (tw = 0.0, k = 0; k < ii; k++) {
double w = 1.0;
for (e = 0; e < di; e++)
atp[e] += cx.nds[k]->p[e];
tw += w;
}
for (e = 0; e < di; e++)
atp[e] /= tw;
#ifdef DEBUG
printf("Average of %d real node start pos = %s\n",ii,ppos(di,atp));
#endif
}
/* Locate the closest and furthest two nodes */
{
double ceperr = 1e200;
int i, j, bi = 0, bj = 0;
/* Find the two vectors that have the closest eperr. Brute force search */
/* and track the device position for the two points involved. */
/* Also locate the smallest device distance. */
cdist = 1e200;
fdist = -1.0;
for (i = 0; i < (ii-1); i++) {
for (j = i+1; j < ii; j++) {
double dist;
dist = ofps_comp_eperr3(s, NULL, cx.nds[i]->v, cx.nds[i]->p, cx.nds[j]->v, cx.nds[j]->p, cx.nds[i]->nsp) ;
if (dist < ceperr) {
ceperr = dist;
bi = i;
bj = j;
}
for (dist = 0.0, e = 0; e < di; e++) {
double tt = cx.nds[i]->p[e] - cx.nds[j]->p[e];
dist += tt * tt;
}
if (dist < cdist)
cdist = dist;
if (dist > fdist)
fdist = dist;
}
}
fdist = sqrt(fdist);
cdist = sqrt(cdist);
/* Compute the middle of the two closest eperr nodes */
for (e = 0; e < di; e++)
mct[e] = 0.5 * (cx.nds[bi]->p[e] + cx.nds[bj]->p[e]);
/* Set a step/search radius based on the distance */
/* between the two closest device distance nodes. */
if (cdist < COINTOL) {
#ifdef DEBUG
printf("Two nodes are cooincident! - dnsq will fail!\n");
#endif
if (s->verb > 1)
warning("Two nodes are cooincident! ix %d, pos %s and ix %d pos %s",cx.nds[bi]->ix,ppos(di,cx.nds[bi]->p),cx.nds[bj]->ix,ppos(di,cx.nds[bj]->p));
}
bsrad = 0.2 * cdist;
if (bsrad < 1e-5)
bsrad = 1e-5;
}
/* Set initial starting position */
// if (startex && ! ofps_would_clip_point(s, vv->p)) { }
if (startex) {
for (e = 0; e < di; e++)
osp[e] = vv->p[e];
// ofps_clip_point(s, vv->p, vv->p);
#ifdef DEBUG
printf("Startex startposition = %s\n",ppos(di,atp));
#endif
} else {
double mwt = 0.3;
/* Start at equalateral point between two closest */
/* nodes towards average. */
for (e = 0; e < di; e++) {
// osp[e] = mct[e]; /* Best for 2D ? */
// osp[e] = atp[e]; /* best for 3D/4D ? */
osp[e] = mwt * mct[e] + (1.0 - mwt) * atp[e]; /* Good compromize */
}
}
/* Try our computed starting position first, and if that fails, */
/* retry with a random offset starting location. */
cx.srad = bsrad;
for (e = 0; e < di; e++)
cx.stp[e] = osp[e];
cx.on = 0;
for (tries = 0; tries < notries; tries++) {
int rv;
double fvec[MXPD]; /* Return function value at solution */
int cfunccount;
if (tries > 0) { /* Determine a starting point */
/* Try all possible odd one outs */
cx.on++;
/* On carry, use a random start offset */
/* (Tried culling random starts and odd one outs */
/* by picking one with a low norm, but */
/* while this reduced the number of small */
/* retries, it worsened the number of failures */
/* and sucesses after a large number of retries.) */
if (cx.on >= cx.nn) {
double rscale = 1.0; /* Random scale */
double fval[MXPD];
int nc;
s->sob->next(s->sob, cx.stp);
/* Scale random value around original starting point */
for (e = 0; e < di; e++) {
cx.stp[e] = cx.stp[e] * 2.0 - 1.0; /* Make -1.0 to 1.0 range */
if (cx.stp[e] < 0.0) {
cx.stp[e] *= rscale * (osp[e] - s->imin[e]);
} else {
cx.stp[e] *= rscale * (s->imax[e] - osp[e]);
}
cx.stp[e] += atp[e];
}
ofps_clip_point4(s, cx.stp, cx.stp);
cx.on = 0;
}
}
/* Set start position */
for (e = 0; e < di; e++)
vv->p[e] = cx.stp[e];
#ifdef DEBUG
printf("Starting location = %s, srad = %f, on = %d\n",ppos(di,cx.stp),cx.srad,cx.on);
#endif
//printf("\nStarting location = %s, srad = %f\n",ppos(di,cx.stp),cx.srad);
/* Locate vertex */
cfunccount = s->funccount;
s->dnsqs++;
if (tcalls == 0)
maxfev = 500;
else
maxfev = 2 * tfev/tcalls;
rv = dnsqe((void *)&cx, dnsq_solver, NULL, di, vv->p, cx.srad, fvec, 0.0, ftol, maxfev, 0);
if ((s->funccount - cfunccount) > 20) {
//printf("More than 20: %d\n",s->funccount - cfunccount);
}
if ((s->funccount - cfunccount) > s->maxfunc) {
s->maxfunc = (s->funccount - cfunccount);
//printf("New maximum %d\n",s->maxfunc);
}
if (rv != 1 && rv != 3) {
/* Fail to converge */
#ifdef DEBUG
printf("dnsqe fail to converge, retuned %d\n",rv);
#endif
} else {
double ee[MXPD+1];
double max, min;
double ple = 0.0; /* Gamut plane error */
/* Evaluate the result */
/* Update average function evaluations */
tcalls++;
tfev += s->funccount - cfunccount;
#ifdef DEBUG
printf("dnsq pos %s\n",ppos(di,vv->p));
if (rv == 3)
printf("dnsq returned 3 - dtol too small\n");
#endif
/* Compute perceptual */
ofps_cc_percept(s, vv->v, vv->p);
/* Compute the eperr's for each node. */
ofps_pn_eperr(s, vv->ce, ee, vv->v, vv->p, cx.nds, cx.nn);
min = 1e80, max = -1e80;
for (e = 0; e < cx.nn; e++) {
if (min > ee[e])
min = ee[e];
if (max < ee[e])
max = ee[e];
}
/* Compute the worst plane equation error */
for (k = 0; k < cx.nsp; k++) {
double v;
for (v = cx.sp[k]->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += cx.sp[k]->pe[e] * vv->p[e];
v = fabs(v * FGPMUL);
//printf("~1 gamut plane %d err = %f\n",k,v);
if (v > ple)
ple = v;
}
#ifdef DEBUG
printf("new vertex pos %s has eperrs match by %f & gamut plane %f\n",ppos(di,vv->p),max-min,ple);
#endif
/* If not dtol too large, Check that the balance is acceptable */
if (/* rv != 3 && */
(((cx.nn > 1) && (max - min) > (ftol * 2.0))
|| ((cx.nsp > 0) && ple > (ftol * 2.0)))) {
/* Don't use this */
#ifdef DEBUG
printf("new vertex pos %s doesn't have sufficient matching eperrs and on gamut plane\n",ppos(di,vv->p),max-min,ple);
#endif
} else {
/* eperr balance is acceptable, so further */
/* evaluate the location found. */
double ss;
s->sucfunc += (s->funccount - cfunccount);
s->sucdnsq++;
/* Compute how much the result is out of gamut */
vv->oog = ofps_oog(s, vv->p);
#ifdef DEBUG
if (vv->oog > 0.01)
printf("dnsq returned out of gamut result by %e\n", vv->oog);
#endif
/* Compute errors at returned location */
vv->eperr = ofps_eperr2(ee, cx.nn);
vv->eserr = ofps_eserr2(vv->ce, ee, cx.nn);
/* Decide whether a vertex location is acceptable */
/* We accept a point that has an acceptable error balance */
/* and improves the eperr, and is in gamut if this is not a repos. */
/* (There's some mystery stuff in here for fixups) */
if ((cx.nn <= 1) || ((max - min) <= (ftol * 2.0)
&& ((!repos && vv->oog <= 0.01 && vv->eperr < (vv->ceperr + 0.1))
|| ( repos && vv->oog <= 0.01 && vv->eperr < (5.0 * vv->ceperr + 20.0))
|| ( repos && vv->oog > 0.0 && vv->eperr < 1000.0)
|| ( fixup && vv->oog < 20.0 && vv->eperr < (vv->ceperr + 0.01))
))) {
if (tries > s->maxretries)
s->maxretries = tries;
#ifdef DEBUG
printf(" - comb %s succeeded on retry %d (max %d)\n",pcomb(di,vv->nix),tries,s->maxretries);
printf(" oog = %f, eperr = %f, ceperr = %f\n",vv->oog,vv->eperr,vv->ceperr);
#endif
//if (tries > 10)
// printf(" - comb %s suceeded on retry %d (max %d)\n",pcomb(di,vv->nix),tries,s->maxretries);
//
//printf("Solution for comb %s has eperr %f < ceperr %f and not out of gamut by %f, retry %d\n",pcomb(di,vv->nix),vv->eperr,vv->ceperr,vv->oog,tries+1);
//printf("Solution is at %s (%s)\n",ppos(di,vv->p),ppos(di,vv->v));
//
//if (repos) printf("~1 vtx no %d dtav = %f, fdist = %f\n",vv->dtav,fdist);
//if (repos && vv->vv->no == 889) printf("~1 vtx no %d dtav = %f, fdist = %f\n",vv->vv->no,vv->dtav,fdist);
#ifdef DUMP_FERR /* Create .tiff of dnsq function error */
if (tries >= DUMP_FERR) {
printf("Succeeded on retry %d, dumping debug rasters\n",tries);
/* Re-run the last unsucessful dnsq, to trace the path */
pcx.debug = 1;
pcx.clist = NULL;
pcx._nl = 0;
pcx.nl = 0;
dnsqe((void *)&pcx, dnsq_solver, NULL, di, pcx.stp, pcx.srad, fvec, 0.0, ftol, maxfev, 0);
pcx.debug = 0;
dump_dnsqe(s, "dnsq_fail2.tif", vv->nix, &pcx);
free(pcx.clist);
/* Re-run the first unsucessful dnsq, to trace the path */
pcx.debug = 1;
pcx.clist = NULL;
pcx._nl = 0;
pcx.nl = 0;
pcx.on = 0; /* First odd one out */
for (e = 0; e < di; e++)
pcx.stp[e] = atp[e]; /* best start ? */
dnsqe((void *)&pcx, dnsq_solver, NULL, di, pcx.stp, pcx.srad, fvec, 0.0, ftol, maxfev, 0);
pcx.debug = 0;
dump_dnsqe(s, "dnsq_fail1.tif", vv->nix, &pcx);
free(pcx.clist);
/* Re-run the sucessful dnsq, to trace the path */
cx.debug = 1;
cx.clist = NULL;
cx._nl = 0;
cx.nl = 0;
dnsqe((void *)&cx, dnsq_solver, NULL, di, cx.stp, cx.srad, fvec, 0.0, ftol, maxfev, 0);
cx.debug = 0;
dump_dnsqe(s, "dnsq_suc.tif", vv->nix, &cx);
free(cx.clist);
exit(0);
}
#endif
break; /* Use the result now */
}
#ifdef DEBUG
printf("Solution for comb %s has eperr %f > ceperr %f or out of gamut by %f, retry %d\n",pcomb(di,vv->nix),vv->eperr,vv->ceperr,vv->oog,tries+1);
#endif
}
}
pcx = cx; /* Save unsucessful context for debug */
} /* Retry */
/* If we've run out of tries, return the best solution we found */
if (tries >= notries) {
/* Show up if this ever gets used */
for (e = 0; e < di; e++)
vv->p[e] = -0.1;
ofps_cc_percept(s, vv->v, vv->p);
#ifdef DEBUG
printf("vertex location solving failed after %d tries\n",tries);
#endif
if (s->verb > 1)
warning("vertex location solving failed after %d tries",tries);
return 2; /* Don't use this vertex */
}
#ifdef DEBUG
printf("Returning comb %s opt pos = %s val = %s, eperr = %f, eserr = %f\n",pcomb(di,vv->nix),ppos(di,vv->p),ppos(di,vv->v),vv->eperr,vv->eserr);
#endif
return 0;
}
/* --------------------------------------------------------- */
/* Deal with creating a dummy vertex to represent one that */
/* can't be positioned. We simply locate the best point we can. */
/* calculate the functions at x[] */
double powell_solver( /* Return < 0 on abort */
void *fdata, /* Opaque data pointer */
double *x /* Multivariate input values */
) {
vopt_cx *cx = (vopt_cx *)fdata;
ofps *s = cx->s;
int k, e, di = s->di;
int nn_1 = cx->nn-1;
double sv[MXPD];
double cee[MXPD+1], teperr;
double ss, oog;
double rv = 0.0;
//printf("~1 powell_solver got %d nodes and %d planes\n",cx->nn,cx->nsp);
/* Get eperr at each real node */
ofps_cc_percept(s, sv, x); /* We have to compute it */
for (k = 0; k < cx->nn; k++)
cee[k] = ofps_comp_eperr2(s, NULL, sv, x, cx->nds[k]->v, cx->nds[k]->p, cx->nds[k]->nsp);
//fprintf(stderr,"~1 maxeperr = %f\n",cmax);
//printf("~1 eprror =");
//for (k = 0; k < cx->nn; k++)
// printf(" %f",cee[k]);
//printf("\n");
/* The error is zero if the input value */
/* is within gamut, all the real node eperr's are */
/* the same, and the ditance to gamut planes is zero. */
/* Compute average eperr */
for (teperr = 0.0, k = 0; k < cx->nn; k++)
teperr += cee[k];
teperr /= (double)cx->nn;
//printf("~1 average = %f\n", teperr);
/* Add diference to average */
for (k = e = 0; k < cx->nn; k++) {
if (k != cx->on) {
double tt;
tt = teperr - cee[k];
rv += tt * tt;
}
}
//printf("~1 after diff to avg rv = %f\n", rv);
/* Compute distances to planes */
for (k = 0; k < cx->nsp; k++) {
pleq *pl = cx->sp[k];
double v;
for (v = pl->pe[di], e = 0; e < di; e++) /* Compute relation to plane equation */
v += pl->pe[e] * x[e];
v *= FGPMUL;
rv += v * v;
}
//printf("~1 after diff to planes rv = %f\n", rv);
/* Compute distance out of gamut */
for (ss = oog = 0.0, e = 0; e < di; e++) {
if (x[e] < (s->imin[e])) {
double tt = s->imin[e] - x[e];
if (tt > oog) oog = tt;
} else if (x[e] > (s->imax[e])) {
double tt = x[e] - s->imax[e];
if (tt > oog) oog = tt;
}
ss += x[e];
}
if (ss > s->ilimit) {
double tt;
ss = (ss - s->ilimit)/di; /* Axis aligned distance to ink limit */
tt = sqrt((double)di) * ss; /* Diagonal distance to ink limit */
if (tt > oog)
oog = tt;
}
rv += 1000.0 * oog * oog;
//printf("~1 after oog rv = %f\n", rv);
//printf("powell_solver returning %f from %s\n",rv, ppos(di,x));
//fprintf(stderr,"powell_solver returning %f from %s\n",rv, ppos(di,x));
return 0;
}
/* Fake up a vertex position when position_vtx() has failed. */
static void dummy_vtx_position(
ofps *s,
vtx *ev1, vtx *ev2, /* Deleted and non-deleted vertexes */
nodecomb *vv /* Return the location and its error */
) {
int e, di = s->di;
int k, ii;
vopt_cx cx; /* dnsq context */
double ss[MXPD];
double bl; /* Location on path between del and !del vertexes */
double ee[MXPD+1];
#ifdef DEBUG
printf("dummy_vtx_position called for comb %s\n",pcomb(di,vv->nix));
#endif
#ifdef DUMP_FERR
cx.debug = 0;
#endif
/* Setup for dnsq to optimize for equal eperr */
cx.s = s;
/* Pointers to real nodes. Although we allow for the */
/* fake inner/outer nodes, eperr() will fail them later. */
for (ii = e = 0; e <= di; e++) {
if (vv->nix[e] >= 0 || vv->nix[e] < -s->nbp)
cx.nds[ii++] = s->n[vv->nix[e]];
}
cx.nn = ii;
if (ii == 0) {
fflush(stdout);
error("ofps: unexpectedely got no real nodes in vertex position %s",pcomb(di,vv->nix));
}
#ifdef DEBUG
printf("%d real nodes\n",ii);
for (k = 0; k < ii; k++)
printf("Node ix %d at %s (%s)\n",cx.nds[k]->ix,ppos(di,cx.nds[k]->p),ppos(di,cx.nds[k]->v));
#endif
/* Setup gamut suface planes */
cx.nsp = 0;
if (ii < (di+1)) {
/* Create list of pointers to the gamut surface planes involved */
for (e = 0, k = ii; k <= di; e++, k++) {
#ifdef DEBUG
printf("Adding plane for node ix %d\n",vv->nix[k]);
#endif
cx.sp[e] = &s->gpeqs[-1 - vv->nix[k]];
}
cx.nsp = e;
}
/* Set search area */
for (e = 0; e < di; e++)
ss[e] = 0.001;
/* Compute a position on the locus between the del and !del vertexes */
bl = (ev1->nba_eperr - ev2->eperr)/(ev1->eperr - ev2->eperr);
if (bl < 0.0)
bl = 0.0;
else if (bl > 1.0)
bl = 1.0;
for (e = 0; e < di; e++) {
vv->p[e] = bl * vv->v1[0]->p[e] + (1.0 - bl) * vv->v2[0]->p[e];
}
ofps_clip_point5(s, vv->p, vv->p);
#ifndef NEVER
/* Seem to often fail due to pathalogical condition for max type eperr() */
if (powell(NULL, di, vv->p, ss, 1e-5, 1000, powell_solver, &cx, NULL, NULL)) {
warning("dummy_vtx_position powell failed");
}
#endif
ofps_clip_point5(s, vv->p, vv->p);
/* Compute perceptual (was clipped above) */
s->percept(s->od, vv->v, vv->p);
/* Compute the eperr's for each node. */
ofps_pn_eperr(s, vv->ce, ee, vv->v, vv->p, cx.nds, cx.nn);
/* Compute errors at returned location */
vv->eperr = ofps_eperr2(ee, cx.nn);
vv->eserr = ofps_eserr2(vv->ce, ee, cx.nn);
#ifdef DEBUG
printf("Returning comb %s opt pos = %s val = %s, eperr = %f, eserr = %f\n",pcomb(di,vv->nix),ppos(di,vv->p),ppos(di,vv->v),vv->eperr,vv->eserr);
#endif
}
/* --------------------------------------------------- */
/* Vertex add routines */
/* Comlete adding a node to a Voronoi surface. */
/* It's assumed that the hit nodes have been added to the s->nxh list */
/* and the s->nvcheckhits set to the number of hit vertexes. */
/* The nodes may be fake gamut boundary nodes, but must have */
/* real vertexes. Vertexes that are marked for deletion or new ones */
/* will be added to the batch update list for later execution. */
/* Nodes that have had vertexes added to them will added to the node 'to be updated' list */
/* and then a batch update will be executed. */
/* Return 0 if it wasn't added, */
/* Return 1 if it was added */
static int add_to_vsurf(
ofps *s,
node *nn, /* Node to add */
int fixup, /* 0 = seed, 1 = fixup ?? */
int abortonfail /* 0 = ignore position failures, 1 = abort add if there are any failures */
) {
int e, ff, f, di = s->di;
int i, j, k, ndi;
vtx *tev;
vtx *ev1, *ev2; /* Deleted and non-deleted vertexes */
int ndelvtx; /* Number of vertexes to delete */
int nncombs; /* Number of node combinations generated, allocated. */
#ifdef DEBUG
printf("\nAdd_to_vsurf node ix %d (p %s), i_sm %s, a_sm %s\n",nn->ix, ppos(di,nn->p),psm(s,&s->sc[nn->pmask].i_sm),psm(s,&s->sc[nn->pmask].a_sm));
#endif
#ifdef DEBUG
if (nn->ix < -s->nbp) {
printf("Fake node involved\n");
}
#endif
/* Update stats for the hit vertexes */
s->nsurfadds++;
s->nhitv += s->nvcheckhits;
if (s->nvcheckhits > s->maxhitv)
s->maxhitv = s->nvcheckhits;
if (s->nvcheckhits == 0) { /* Node doesn't improve any vertex eperrs */
#ifdef DEBUG
printf("Add_to_vsurf done - not better, not added\n");
#endif
return 0;
}
#ifdef DEBUG
printf("There are %d vertexes to replace, and %d potential replacement vertexes\n",s->nvcheckhits, di * s->nvcheckhits);
printf("Vertexes marked for deletion are:\n");
for (ev1 = s->nxh; ev1 != NULL; ev1 = ev1->nxh)
printf(" vtx no %d nix %s\n",ev1->no,pcomb(di,ev1->nix));
#endif
/* Generate all the potential new node combinations/replacement verticies. */
/* We check each deleted vertex against its non-deleted neighbours. */
/* The same replacement combination may be generated more than once. */
for (nncombs = 0, ev1 = s->nxh; ev1 != NULL; ev1 = ev1->nxh) {
for (ndi = 0; ndi < ev1->nnv; ndi++) {
int nix[MXNIX];
#ifdef INDEP_SURFACE
setmask cvm; /* visibility setmask for each vertex combination */
#endif
ev2 = ev1->nv[ndi];
if (ev2->del != 0)
continue; /* Can't pair with another deleted vertex */
#ifdef DEBUG
printf("\nDealing with vertex pair del no %d nix %s, and !del no %d nix %s\n",ev1->no,pcomb(di,ev1->nix),ev2->no,pcomb(di,ev2->nix));
#endif
#ifdef DEBUG
{
int aa, bb, cc; /* Probable hit check */
int nnm, nmix;
/* Use the nixm to quickly check if all but one parent node matches */
aa = ev1->nix[MXPD+2]; /* nixm */
bb = ev2->nix[MXPD+2]; /* nixm */
if ((aa & bb) == 0 || (cc = aa & ~bb, (cc & (cc-1)) != 0)) {
error("Vertexes %d comb %s and %d comb %s are vn neighbours that shouldn't be!", ev1->no,pcomb(di,ev1->nix),ev2->no,pcomb(di,ev2->nix));
}
/* Do an exact check of all except one node match */
for (nnm = ff = e = 0; e <= di; e++) {
for (f = ff; f <= di; f++) {
if (ev1->nix[e] == ev2->nix[f]) {
ff = f; /* Start from here next time */
break;
}
if (ev1->nix[e] > ev2->nix[f]) /* No point in looking further */
f = di;
}
if (f > di) { /* Didn't match */
if (++nnm > 1)
break;
nmix = e;
}
}
if (e <= di) {
error("Vertexes %d comb %s and %d comb %s are vn neighbours that shouldn't be!", ev1->no,pcomb(di,ev1->nix),ev2->no,pcomb(di,ev2->nix));
}
}
#endif /* DEBUG */
/* Create the node combination */
for (e = 0; e <= di; e++) {
nix[e] = ev1->nix[e];
for (f = 0; f <= di; f++) {
if (nix[e] == ev2->nix[f])
break;
}
if (f > di) /* Found one different */
nix[e] = nn->ix;
}
sort_nix(s, nix);
/* Check that the same node doesn't appear twice */
/* (~~99 Why do we need this - does it ever happen ??) */
for (e = 0; e < di; e++) {
for (k = e+1; k <= di; k++) {
if (nix[e] == nix[k]) {
#ifdef DEBUG
printf("New vertex with duplicate nodes %s from vertexes %d comb %s and %d comb %s ignored\n",pcomb(di,nix),ev1->no,pcomb(di,ev1->nix),ev2->no,pcomb(di,ev2->nix));
if (s->verb > 1)
warning("New vertex with duplicate nodes %s from vertexes %d comb %s and %d comb %s ignored",pcomb(di,nix),ev1->no,pcomb(di,ev1->nix),ev2->no,pcomb(di,ev2->nix));
#endif
break;
}
}
if (k <= di)
break;
}
if (e < di)
continue;
/* See if the combination has at least one real node, */
/* and none of the inner or outer fake nodes. */
/* (~~99 Do we need this - does it ever happen ??) */
k = 0;
for (e = 0; e <= di; e++) {
if (nix[e] >= 0)
k |= 1; /* Found one real node */
else if (nix[e] < -s->nbp)
break; /* There's a fake inner or outer node though */
}
if (e <= di || k == 0) {
#ifdef DEBUG
printf("Combination ix: %s, skipped because it has no real nodes\n",pcomb(di,nix));
if (s->verb > 1)
warning("Combination ix: %s, skipped because it has no real nodes",pcomb(di,nix));
#endif
continue;
}
#ifdef INDEP_SURFACE
/* Compute the pertinent visibility mask for this vertex creation. */
/* Note that we keep pairs of vertexes that aren't visible to each other */
/* so that we can add them to the vertex net. */
sm_andand(s, &cvm, &ev1->vm, &ev2->vm, &s->sc[comp_cmask(s, nix)].a_sm);
#ifdef DEBUG
printf("Combination ix: %s, vm %s, eperrs %f to %f\n",pcomb(di,nix),psm(s,&cvm),ev1->eperr,ev2->eperr);
#endif
#else /* !INDEP_SURFACE */
#ifdef DEBUG
printf("Combination ix: %s, eperrs %f to %f\n",pcomb(di,nix),ev1->eperr,ev2->eperr);
#endif
#endif /* !INDEP_SURFACE */
/* See if this combination is already in the list */
/* due to a pair having the same common nodes. */
for (k = 0; k < nncombs; k++) {
if (s->combs[k].nix[MXPD+1] != nix[MXPD+1]) /* Hashes don't match */
continue;
for (e = 0; e <= di; e++) { /* Do full check */
if (s->combs[k].nix[e] != nix[e]) {
break; /* No match */
}
}
if (e > di) { /* Match */
double ceperr;
if (s->combs[k].count >= s->combs[k]._count) {
s->combs[k]._count = 2 * s->combs[k]._count + 5;
if ((s->combs[k].v1 = (vtx **)realloc(s->combs[k].v1,
sizeof(vtx *) * s->combs[k]._count)) == NULL)
error ("ofps: malloc failed on node combination vertex list %d",
s->combs[k]._count);
if ((s->combs[k].v2 = (vtx **)realloc(s->combs[k].v2,
sizeof(vtx *) * s->combs[k]._count)) == NULL)
error ("ofps: malloc failed on node combination vertex list %d",
s->combs[k]._count);
}
s->combs[k].v1[s->combs[k].count] = ev1;
s->combs[k].v2[s->combs[k].count] = ev2;
s->combs[k].count++;
/* Update ceperr if this is higher */
ceperr = ev1->eperr > ev2->eperr ? ev1->eperr : ev2->eperr;
if (ceperr > s->combs[k].ceperr)
s->combs[k].ceperr = ceperr;
#ifdef DEBUG
printf("Vertex generation count now %d with ceperr %f\n",s->combs[k].count,s->combs[k].ceperr);
#endif
#ifdef INDEP_SURFACE
sm_or(s, &s->combs[k].vm, &s->combs[k].vm, &cvm);
#ifdef DEBUG
printf("Vertex combination vm now %s\n",psm(s,&s->combs[k].vm));
#endif
#endif
break;
}
}
if (k < nncombs)
continue; /* Already on list */
/* Add this combination to the list as a new entry */
if (nncombs >= s->_ncombs) {
int o_ncombs = s->_ncombs;
s->_ncombs = 2 * s->_ncombs + 5;
if ((s->combs = (nodecomb *)realloc(s->combs, sizeof(nodecomb) * s->_ncombs)) == NULL)
error ("ofps: malloc failed on node combination array length %d", s->_ncombs);
memset((void *)(s->combs + o_ncombs), 0,
(s->_ncombs - o_ncombs) * sizeof(nodecomb));
}
if (1 >= s->combs[nncombs]._count) {
s->combs[nncombs]._count = 2 * s->combs[nncombs]._count + 5;
if ((s->combs[nncombs].v1 = (vtx **)realloc(s->combs[nncombs].v1,
sizeof(vtx *) * s->combs[nncombs]._count)) == NULL)
error ("ofps: malloc failed on node combination vertex list %d",
s->combs[nncombs]._count);
if ((s->combs[nncombs].v2 = (vtx **)realloc(s->combs[nncombs].v2,
sizeof(vtx *) * s->combs[nncombs]._count)) == NULL)
error ("ofps: malloc failed on node combination vertex list %d",
s->combs[nncombs]._count);
}
s->combs[nncombs].v1[0] = ev1;
s->combs[nncombs].v2[0] = ev2;
s->combs[nncombs].count = 1;
for (e = 0; e <= di; e++)
s->combs[nncombs].nix[e] = nix[e];
s->combs[nncombs].nix[MXPD+1] = nix[MXPD+1]; /* Copy Hash */
s->combs[nncombs].nix[MXPD+2] = nix[MXPD+2]; /* Copy nixm */
s->combs[nncombs].ceperr = ev1->eperr > ev2->eperr ? ev1->eperr : ev2->eperr;
s->combs[nncombs].startex = 0;
s->combs[nncombs].pvalid = 0;
s->combs[nncombs].vv = NULL;
#ifdef INDEP_SURFACE
sm_cp(s, &s->combs[nncombs].vm, &cvm);
#else /* !INDEP_SURFACE */
sm_set(s, &s->combs[nncombs].vm, 0); /* Not used */
#endif /* !INDEP_SURFACE */
nncombs++;
#ifdef DEBUG
printf("Adding combination to list with ceperr %f, vm %s, list size %d\n",s->combs[nncombs-1].ceperr,psm(s,&s->combs[nncombs-1].vm), nncombs);
#endif
}
}
#ifdef DEBUG
printf("\nThere are %d unique node combinations in list, locating combs. in list:\n",nncombs);
#endif
/* Locate the replacement vertex positions */
for (i = 0; i < nncombs; i++) {
ev1 = s->combs[i].v1[0];
ev2 = s->combs[i].v2[0];
#ifdef INDEP_SURFACE
/* Ignore pairs of vertexes that don't form a visible new combination */
if (sm_test(s, &s->combs[i].vm) == 0) {
#ifdef DEBUG
printf("Combination ix: %s, skipped because vm %s == 0x0\n",pcomb(di,s->combs[i].nix),psm(s,&s->combs[i].vm));
#endif
continue;
}
#endif /* INDEP_SURFACE */
#ifdef DEBUG
printf("\nNode combination ix: %s\n",pcomb(di,s->combs[i].nix));
#endif
/* Try and locate existing vertex that is due to the same nodes */
if ((s->combs[i].vv = vtx_cache_get(s, s->combs[i].nix)) != NULL) {
#ifdef DEBUG
printf("Vertex is same as existing no %d\n",s->combs[i].vv->no);
#endif
s->combs[i].vv->add = 2; /* Updated vertex */
/* If a new vertex is not the same as the two nodes it's being created */
/* from yet has been marked for deletion, reprieve it. */
if (s->combs[i].vv->del) {
s->combs[i].vv->del = 0;
#ifdef DEBUG
printf("New existing vertex no %d is deleted vertex - reprieve it\n",s->combs[i].vv->no);
#endif
}
}
/* We need to create a replacement vertex, locate position for it */
if (s->combs[i].vv == NULL) {
#ifdef DEBUG
printf("About to locate comb ix: %s, ceperr %f\n",pcomb(di,s->combs[i].nix),s->combs[i].ceperr);
#endif
//printf("~1 About to locate comb ix: %s, ceperr %f\n",pcomb(di,s->combs[i].nix),s->combs[i].ceperr);
/* Compute a starting position between the deleted/not deleted pair */
/* This seems very slightly better than the default mct[] + atp[] scheme. */
if (nn->ix >= 0) { /* If not boundary */
double bl;
bl = (ev1->nba_eperr - ev2->eperr)/(ev1->eperr - ev2->eperr);
if (bl < 0.0)
bl = 0.0;
else if (bl > 1.0)
bl = 1.0;
for (e = 0; e < di; e++) {
s->combs[i].p[e] = bl * s->combs[i].v1[0]->p[e] + (1.0 - bl) * s->combs[i].v2[0]->p[e];
}
ofps_clip_point5(s, s->combs[i].p, s->combs[i].p);
//printf("Startex is %s\n",ppos(di,s->combs[i].p));
s->combs[i].startex = 1;
}
/* find vertex position of max eperr */
if (position_vtx(s, &s->combs[i], s->combs[i].startex, 0, fixup) != 0) {
if (s->verb > 1)
warning("Unable to locate vertex at node comb %s\n",pcomb(di,s->combs[i].nix));
s->posfails++;
s->posfailstp++;
if (abortonfail)
break;
} else {
s->combs[i].pvalid = 1;
}
}
} /* Next replacement vertex */
/* If we aborted because abortonfail is set and we failed to place a new node, */
/* erase our tracks and return failure. */
if (i < nncombs) {
for (i = 0; i < nncombs; i++) {
if (s->combs[i].vv != NULL) {
s->combs[i].vv->add = 0;
s->combs[i].vv->del = 0;
}
}
return 0;
}
#ifdef DEBUG
printf("\nNow converting positioned combinations to vertexes\n");
#endif
/* Convert from computed position to vertexes */
for (i = 0; i < nncombs; i++) {
#ifdef INDEP_SURFACE
/* Ignore combo that doesn't form a visible new vertex */
if (sm_test(s, &s->combs[i].vm) == 0)
continue;
#endif /* INDEP_SURFACE */
if (s->combs[i].vv == NULL) { /* Not an existing vertex */
if (s->combs[i].pvalid == 0) { /* No valid vertex found */
/* [ If a valid vertex location was not found we tried */
/* using the deleted vertex's location instead, and */
/* relying on it getting deleted at some later stage. */
/* This seems to stuff the incremental fixup code up */
/* completely, so we create a summy vertex position instead. ] */
/* Fake up a vertex position when position_vtx() has failed. */
dummy_vtx_position(s, ev1, ev2, &s->combs[i]);
goto lnew_vtx;
} else {
lnew_vtx:;
/* Allocate space for new vertex, and create it from location */
s->combs[i].vv = new_vtx(s);
#ifdef DEBUG
printf("Converting comb %s to from del %d !del %d to vtx no %d vm %s\n",pcomb(di,s->combs[i].nix),s->combs[i].v1[0]->no, s->combs[i].v2[0]->no, s->combs[i].vv->no,psm(s,&s->combs[i].vm));
#endif
for (e = 0; e < di; e++) {
s->combs[i].vv->nix[e] = s->combs[i].nix[e];
s->combs[i].vv->ce[e] = s->combs[i].ce[e];
s->combs[i].vv->p[e] = s->combs[i].p[e];
s->combs[i].vv->v[e] = s->combs[i].v[e];
}
s->combs[i].vv->nix[e] = s->combs[i].nix[e];
s->combs[i].vv->nix[MXPD+1] = s->combs[i].nix[MXPD+1]; /* Copy Hash */
s->combs[i].vv->nix[MXPD+2] = s->combs[i].nix[MXPD+2]; /* Copy nixm */
s->combs[i].vv->eperr = s->combs[i].eperr;
s->combs[i].vv->eserr = s->combs[i].eserr;
/* Count the number of gamut surfaces the vertex falls on */
det_vtx_gsurf(s, s->combs[i].vv);
/* Check if the node and vertex cooincide, and aren't going to move */
if (nn->nsp == di && s->combs[i].vv->nsp == di
&& nn->pmask == s->combs[i].vv->pmask) {
//printf("~1 Trapped node and vertex coincide - mark vertex as ghost\n");
s->combs[i].vv->ghost = 1;
}
s->combs[i].vv->del = 0;
s->combs[i].vv->add = 1; /* New vertex */
}
}
if (s->combs[i].vv != NULL) { /* There is a new or updated vertex */
#ifdef DEBUG
printf("Vertex no %d pmask 0x%x cmask 0x%x vm %s at %s being added to batch list\n",s->combs[i].vv->no, s->combs[i].vv->pmask, s->combs[i].vv->cmask, psm(s,&s->combs[i].vm),ppos(di,s->combs[i].vv->p));
#endif
/* Add to batch update list if it is not already there */
if (s->combs[i].vv->bch == 0) {
//printf("~1 adding vtx 0x%x no %d to batch list\n",s->combs[i].vv,s->combs[i].vv->no);
s->combs[i].vv->batch = s->batch;
s->batch = s->combs[i].vv;
s->combs[i].vv->bch = 1;
}
#ifdef INDEP_SURFACE
/* Set or update the vm */
sm_or(s, &s->combs[i].vv->buvm, &s->combs[i].vv->buvm, &s->combs[i].vm);
//printf("~1 Vertex no %d buvm set/update to %s\n",s->combs[i].vv->no,psm(s,&s->combs[i].vv->buvm));
#endif /* INDEP_SURFACE */
}
}
/* Add all vtx marked for deletion to the batch update list. */
for (ev1 = s->nxh; ev1 != NULL; ev1 = ev1->nxh) {
#ifdef DEBUG
printf("Vertex no %d being added to pending delete batch list, bdvm %s\n",ev1->no,psm(s,&ev1->bdvm));
#endif
if (ev1->bch == 0) {
//printf("~1 adding vtx 0x%x no %d to batch list\n",ev1,ev1->no);
ev1->batch = s->batch;
s->batch = ev1;
ev1->bch = 1;
}
#ifdef INDEP_SURFACE
/* Add node setmask to those that will be removed delete vertex visibility */
# ifdef USE_DISJOINT_SETMASKS
sm_orand(s, &ev1->bdvm, &ev1->bdvm, &s->sc[nn->pmask].a_sm, &s->sc[ev1->cmask & nn->pmask].a_sm);
# else
sm_or(s, &ev1->bdvm, &ev1->bdvm, &s->sc[nn->pmask].a_sm);
# endif
#endif /* INDEP_SURFACE */
}
/* Do first part of batch update. */
/* This will reset ->del on nodes that will be retained */
do_batch_update1(s, fixup);
/* Remove deleted vertex's from the vertex net, and their */
/* parent nodes. */
{
vtx *vx1, *vx2;
for (vx2 = s->nxh; vx2 != NULL; vx2 = vx2->nxh) {
int aa, bb, cc; /* Probable hit check */
int nnm, nmix;
//printf("~1 Removing deleted vertex no %d from net\n",vx2->no);
if (vx2->del == 0) { /* It's not really being deleted */
//printf("~1 vtx no %d is being retained\n",vx2->no);
continue;
}
/* Remove from vertex net */
for (j = 0; j < vx2->nnv; j++) {
vx1 = vx2->nv[j];
//printf("~1 Removing vtx no %d from vtx no %d\n",vx2->no, vx1->no);
// if (vx1->del == 0) { } /* Speed optimization */
{
vtx_rem_vertex(s, vx1, vx2);
}
//else printf("~1 Not removing from vtx no %d because it will be deleted anyway\n",vx1->no);
}
vx2->nnv = 0;
/* Remove from parent nodes */
for (e = 0; e <= di; e++) {
int ix = vx2->nix[e];
node *pp = s->n[ix];
node_rem_vertex(s, pp, vx2);
}
}
}
/* Create/modify the vertex neighbour net lists for all the new vertexes. */
/* Use a brute force search of local nodes to create the vertex net. */
{
vtx *vx1, *vx2;
/* For each new vertx */
for (i = 0; i < nncombs; i++) {
vx1 = s->combs[i].vv;
if (vx1 == NULL || vx1->del)
continue;
/* Possibly add other new vertexes as neighbours */
for (j = i+1; j < nncombs; j++) {
vx2 = s->combs[j].vv;
if (vx2 != NULL && vx2->del == 0)
vtx_cnd_biadd_vtx(s, vx1, vx2, fixup);
}
/* Possibly add deleted and non-deleted vertexes */
for (k = 0; k < s->combs[i].count; k++) {
#ifdef INDEP_SURFACE
/* Add deleted vertex if it isn't going to be deleted */
if (s->combs[i].v1[k]->del == 0) {
vtx_cnd_biadd_vtx(s, vx1, s->combs[i].v1[k], fixup);
}
#endif /* INDEP_SURFACE */
/* Add non-deleted vertex */
if (s->combs[i].v2[k]->del == 0)
vtx_cnd_biadd_vtx(s, vx1, s->combs[i].v2[k], fixup);
}
/* Add any existing vertexes of the node we're re-adding */
if (fixup) {
for (j = 0; j < nn->nvv; j++) {
if (nn->vv[j]->del)
continue;
vtx_cnd_biadd_vtx(s, vx1, nn->vv[j], fixup);
}
}
}
}
/* Do second part of batch update */
do_batch_update2(s, fixup);
#ifdef DEBUG
printf("Add_to_vsurf done - added node %d\n",nn->ix);
#endif
/* If we want intermediate fixup state: */
/* dump_node_vtxs(s, 0); */
/* sanity_check(s, 0); */
#ifdef NEVER
{
vtx *vx;
/* Dump vertex and associated vertex information */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
printf("Vertex no %d has Vtx net:",vx->no);
for (j = 0; j < vx->nnv; j++) {
vtx *vx2 = vx->nv[j];
printf(" %d",vx2->no);
}
printf("\n");
}
printf("\n");
fflush(stdout);
}
#endif /* NEVER */
return 1;
}
/* - - - - - - - - - - - */
/* Deal with verticies marked for deletion or addition, */
/* as well as updating the nodes consequently affects. */
/* If fixup is set, add any new or updates vertexes to the s->fchl */
/* Do the first part of the batch update */
static void do_batch_update1(ofps *s, int fixup) {
int e, di = s->di;
vtx *vv, *nvv;
node *pp;
#ifdef DEBUG
printf("Doing batch update to add/delete vertexes - 1\n");
#endif
/* Update a vertexes vm, and decide whether it is going */
/* to be deleted or just hidden. */
for (vv = s->batch; vv != NULL; vv = vv->batch) {
#ifdef DEBUG
printf("Pending vtx no %d del %d, add %d, vm %s |= %s &= %s\n",vv->no,vv->del,vv->add,psm(s,&vv->vm),psm(s,&vv->buvm),psm(s,&vv->bdvm));
if (vv->ofake)
error("An ofake vertex no %d was hit!\n",vv->ofake);
#endif
if (vv->add == 1) { /* New node */
#ifdef INDEP_SURFACE
sm_or(s, &vv->vm, &vv->vm, &vv->buvm);
#ifdef DEBUG
printf("Set vertex no %d vm to %s\n",vv->no,psm(s,&vv->vm));
#endif
#endif /* INDEP_SURFACE */
/* Add it to the cache */
vtx_cache_add(s, vv);
/* Add it to the spatial accelleration grid */
ofps_add_vacc(s, vv);
/* Add to seeding lists */
ofps_add_vseed(s, vv);
} else if (vv->add == 2) { /* Update the visibility setmask */
int was_inseed = 0, is_inseed = 0;
#ifdef INDEP_SURFACE
if (sm_andtest(s, &s->sc[0].a_sm, &vv->vm) != 0)
was_inseed = 1;
sm_or(s, &vv->vm, &vv->vm, &vv->buvm);
#ifdef DEBUG
printf("Updated vertex no %d vm to %s\n",vv->no,psm(s,&vv->vm));
#endif
if (sm_andtest(s, &s->sc[0].a_sm, &vv->vm) != 0)
is_inseed = 1;
if (vv->used == 0) {
/* Adjust presense in eserr tree if visibility has changed */
if (was_inseed && !is_inseed) {
//printf("Removing (1) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",vv->no,vv->used,vv->eserr,psm(s,&vv->vm),vv->nsp);
if ((aat_aerase(s->vtrees[vv->nsp], (void *)vv)) == 0)
error("aat_aerase vertex failed to find vertex no %d (1)", vv->no);
} else if (!was_inseed && is_inseed) {
//printf("Adding (1) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",vv->no,vv->used,vv->eserr,psm(s,&vv->vm),vv->nsp);
if ((aat_ainsert(s->vtrees[vv->nsp], (void *)vv)) == 0)
error("aat_ainsert vertex malloc failed");
}
}
#endif /* INDEP_SURFACE */
} else if (vv->del != 0) {
#ifdef INDEP_SURFACE
int was_inseed = 0, is_inseed = 0;
if (sm_andtest(s, &s->sc[0].a_sm, &vv->vm) != 0)
was_inseed = 1;
//printf("Checked was_inseed %d for vtx no %d, used %d, eserr %f, vm %s nsp %d\n",was_inseed,vv->no,vv->used,vv->eserr,psm(s,&vv->vm),vv->nsp);
/* Remove visibility due to any deletes */
sm_andnot(s, &vv->vm, &vv->vm, &vv->bdvm);
if (sm_andtest(s, &s->sc[0].a_sm, &vv->vm) != 0)
is_inseed = 1;
//printf("Checking is_inseed %d for vtx no %d, used %d, eserr %f, vm %s nsp %d\n",is_inseed,vv->no,vv->used,vv->eserr,psm(s,&vv->vm),vv->nsp);
/* Adjust presense in eserr tree if visibility has changed */
if (vv->used == 0 && was_inseed && !is_inseed) {
//printf("Removing (2) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",vv->no,vv->used,vv->eserr,psm(s,&vv->vm),vv->nsp);
if ((aat_aerase(s->vtrees[vv->nsp], (void *)vv)) == 0)
error("aat_aerase vertex failed to find vertex no %d (2)", vv->no);
}
#ifdef DEBUG
printf("Delete vertex no %d vm to %s\n",vv->no,psm(s,&vv->vm));
#endif
/* Don't delete vertex if it remains visible to some sub-surfaces. */
if (sm_test(s, &vv->vm) != 0) {
vv->del = 0;
vv->add = 2; /* Update it instead */
#ifdef DEBUG
printf("Retaining vtx no %d marked for deletion because vm is %s\n",vv->no,psm(s,&vv->vm));
#endif
}
#endif /* INDEP_SURFACE */
}
}
}
/* Do the second part of the batch update */
static void do_batch_update2(ofps *s, int fixup) {
int e, di = s->di;
vtx *vv, *nvv;
node *pp;
#ifdef DEBUG
printf("Doing batch update to add/delete vertexes - 2\n");
#endif
/* Add or delete a vertex */
for (vv = s->batch; vv != NULL; vv = nvv) {
nvv = vv->batch;
vv->batch = NULL; /* Ready for next time */
vv->bch = 0;
/* Setup vertex ready for another round */
sm_set(s, &vv->buvm, 0); /* Ready to OR in new visibility next time */
sm_set(s, &vv->bdvm, 0); /* Ready for OR in visibility to be remove next time */
if (vv->del) { /* delete vertex */
#ifdef DEBUG
printf("Deleting vertex no %d\n",vv->no); fflush(stdout);
#endif
/* Add all the parent nodes of this vertex to the update list */
for (e = 0; e <= di; e++) {
int ix = vv->nix[e];
node *pp = s->n[ix];
if (pp->upflag != s->flag) {
pp->nup = s->nup;
s->nup = pp;
pp->upflag = s->flag;
}
/* During fixups, maintain nodes vertexes lists */
/* (During re-seeding we update it as a batch) */
if (fixup)
node_rem_vertex(s, pp, vv);
}
del_vtx(s, vv);
#ifdef DEBUG
{
vtx *vx;
int i, k;
printf("~1 checking that no references to vertex remain after delete:\n");
/* Check vertexes references to vertexes */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
for (k = 0; k < vx->nnv; k++) {
if (vx->nv[k] == vv) {
printf("Vertex 0x%x no %d still in no %d nv list after deletion\n",vv,vv->no,vx->no);
#ifdef WARNINGS
warning("Vertex 0x%x no %d still in no %d nv list after deletion",vv,vv->no,vx->no);
#endif
}
}
}
/* Check nodes references to vertexes */
for (i = 0; i < s->np; i++) { /* For all nodes */
node *p1 = s->n[i];
for (k = 0; k < p1->nvv; k++) { /* For all its vertexes */
if (p1->vv[k] == vv) {
printf("Vertex 0x%x no %d still in ix %d vv list after deletion\n",vv,vv->no,p1->ix);
#ifdef WARNINGS
warning("Vertex 0x%x no %d still in ix %d vv list after deletion",vv,vv->no,p1->ix);
#endif
}
}
}
/* Fixup sorted list reference to vertex */
for (i = 0; i < s->nsvtxs; i++) {
if (s->svtxs[i] == vv) {
printf("Vertex 0x%x no %d still in svtxs[%d] after deletion\n",vv,vv->no,i);
#ifdef WARNINGS
warning("Vertex 0x%x no %d still in svtxs[%d] after deletion",vv,vv->no,i);
#endif
}
}
}
#endif /* DEBUG */
} else if (vv->add != 0) { /* New or updated vertex, set updates & checks */
#ifdef DEBUG
printf("Adding vertex no %d\n",vv->no);
#endif
/* Add all the parent nodes of this vertex to the update list */
for (e = 0; e <= di; e++) {
node *pp = s->n[vv->nix[e]];
if (pp->upflag != s->flag) {
/* Add node to update list */
pp->nup = s->nup;
s->nup = pp;
pp->upflag = s->flag;
}
/* During fixups, maintain nodes vertexes lists. */
/* (During re-seeding we update it as a batch) */
if (fixup && vv->add == 1)
node_add_vertex(s, pp, vv);
}
/* If this is a fixup and the vertex hasn't been added */
/* to the "check" list, do so */
if (fixup && vv->fflag != s->fflag) {
vv->fchl = s->fchl; /* Add vertex to the "to be checked" list */
if (s->fchl != NULL)
s->fchl->pfchl = &vv->fchl;
s->fchl = vv;
vv->pfchl = &s->fchl;
vv->fflag = s->fflag;
#ifdef DEBUG
printf("Adding vtx no %d to check list due to addition\n",vv->no);
#endif
}
}
}
s->batch = NULL; /* Nothing in pending delete list */
s->nup = NULL; /* Nothing in nodes to be updated list */
}
/* ------------------------------------------------------------------------------- */
/* Do a quick count of the number of verticies hit by their */
/* neighbour nodes. This is used during itteration to decide */
/* whether to reseed or fixup. */
/* Return the number of vertexes hit */
static int
ofps_quick_check_hits(ofps *s) {
int i, j, k, e, di = s->di;
int nvxhits = 0;
/* For all nodes */
for (i = -s->nbp; i < s->np; i++) {
node *nn = s->n[i]; /* Node being considered */
s->flag++; /* Marker flag for testing this node */
nn->flag = s->flag;
/* Check all the neighbors nodes */
for (j = 0; j < nn->nvn; j++) {
node *pp = s->n[nn->vn[j]];
/* Test nn against all of pp's vertexes */
for (k = 0; k < pp->nvv; k++) {
vtx *vx = pp->vv[k];
if (vx->cflag == s->flag)
continue; /* Don't test same node twice */
vx->cflag = s->flag;
/* If node that we're testing against is in vertex */
/* ignore it, we expect them to hit. */
for (e = 0; e <= di; e++) {
if (nn->ix == vx->nix[e])
break;
}
if (e <= di) {
continue;
}
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
if (nn->ix < 0) {
pleq *vp = &s->gpeqs[-1 - nn->ix];
double v = 0.0;
/* See if the vertex is on the wrong side of the plane */
for (v = vp->pe[di], e = 0; e < di; e++)
v += vp->pe[e] * vx->p[e];
if (v > 0.0) {
nvxhits++;
}
} else {
double eperr = ofps_comp_eperr7(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
/* See if the vertex eperr will be improved */
if (eperr < (vx->eperr + 0.0)) {
nvxhits++;
}
}
}
}
}
return nvxhits;
}
/* ------------------------------------------------------------------------------- */
/* Recursive hit search routine: */
/* Test a vertex for a possible hit from a node. */
/* Recursively search dorec recursions until there is at least */
/* one hit, and stop after beyhit recursions beyond the hit region. */
/* s->flag is assumed to be relevant for the given node. */
/* Any hit vertexes are added to the s->nxh list. */
/* s->vvchecks and s->nvcheckhits will be updated. */
/* Return nz if vertex was hit */
/* Breadth first search. */
/* (Assumes s->flag has been incremented) */
/* [We're assuming that there is a single connected hit region, which */
/* is not valid for SUBD. ] */
static int
ofps_check_vtx(ofps *s, node *nn, vtx *vx, int dorec, int beyhit) {
int i, j, e, di = s->di;
vtx *slist = NULL; /* Next to search list */
int dist; /* Distance from initial vertex */
int hit = 0;
double tol = 0.0; /* Tollerance */
#ifdef DEBUG
printf("ofps_check_vtx() for node ix %d starting at vertex no %d, dorec %d, beyhit %d\n",nn->ix,vx->no,dorec,beyhit);
#endif
#ifdef SANITY_CHECK_HIT
if (nn->ix < -s->nbp)
error("Calling ofps_check_node on fake outside node");
#endif
if (vx->cflag == s->flag)
return vx->del; /* Already been checked */
/* Put the starting node on the search list */
vx->slist = slist;
slist = vx;
vx->sflag = s->flag; /* Mark as done for pre-hit search */
/* until we run out of vertexes, or we are done */
for (dist = 0; slist != NULL && dist <= dorec; dist++) {
vtx *nvx;
/* For each vertex in the search list, check it and recursion. */
for (vx = slist, slist = NULL; vx != NULL; vx = nvx) {
nvx = vx->slist;
vx->opqsq = 0; /* Not on the list anymore */
if (vx->ofake)
continue; /* ofake vertexes can't be hit */
#ifdef DEBUG
printf("%d: Checking vtx no %d %s\n",dist, vx->no,hit ? "Post-Hit" : "Pre-Hit");
#endif
#ifdef INDEP_SURFACE
/* Only check for hit if the vertex is visible to the node */
if (sm_vtx_node(s, vx, nn) == 0) {
# ifdef DEBUG
printf("%d: Vertex no %d xmask 0x%x vm %s isn't visible to ix %d pmask 0x%x a_sm %s\n",dist,vx->no,vx->cmask,psm(s,&vx->vm),nn->ix,nn->pmask,psm(s,&s->sc[nn->pmask].a_sm));
# endif /* DEBUG */
continue;
}
#endif /* INDEP_SURFACE */
/* If the vertex hasn't been checked yet: */
if (vx->cflag != s->flag) {
vx->add = 0;
vx->del = 0;
vx->par = 0;
s->vvchecks++; /* Checking a vertex */
/* Check if node is already parent to this vertex. */
/* This only happens during fixups if the reposition fails and we */
/* retain the vertex with the deleted vertex location (not currently */
/* done), or by slim numerical margine, so ignore such hits. */
/* We treat a parent as a hit node for the purposes of recursion, */
/* and add it to a special list used to complete the vertex net. */
if (nn->ixm & vx->nix[MXPD+2]) { /* Is in nixm */
for (e = 0; e <= di; e++) { /* Do exact check */
if (nn->ix == vx->nix[e])
break;
}
if (e <= di) {
#ifdef DEBUG
printf("Vertex no %d has already got node ix %d\n",vx->no,nn->ix);
#endif
vx->par = 1;
}
}
if (nn->ix < 0) {
pleq *vp = &s->gpeqs[-1 - nn->ix];
double v = 0.0;
/* See if the vertex is on the wrong side of the plane */
for (v = vp->pe[di], e = 0; e < di; e++)
v += vp->pe[e] * vx->p[e];
if (!vx->par && v > tol) {
s->nvcheckhits++;
if (!hit)
slist = nvx = NULL; /* Abort pre-hit search */
hit = 1;
vx->del = 1; /* Mark for deletion */
vx->nxh = s->nxh; /* Add vertex to list */
s->nxh = vx;
vx->disth = 0; /* This is a hit vertex */
vx->hflag = s->flag;
#ifdef DEBUG
printf("%d: Gamut surface boundary plain hit by %f\n",dist,v);
#endif
}
#ifdef DEBUG
else { /* If worse */
printf("%d: Gamut surface boundary plain miss by %f\n",dist,v);
}
#endif
} else { /* Node rather than boundary plane */
/* nba_eperr is assumed to be valid if vx->cflag == s->flag */
vx->nba_eperr = ofps_comp_eperr7(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
#ifdef DEBUG
printf("%d: Computing nba_eperr of %f for vtx no %d\n",dist, vx->nba_eperr, vx->no);
#endif
/* See if the vertex eperr will be improved */
if (!vx->par && (vx->eperr - vx->nba_eperr) > tol) {
s->nvcheckhits++;
if (!hit)
slist = nvx = NULL; /* Abort pre-hit search */
hit = 1;
vx->del = 1; /* Mark for deletion */
vx->nxh = s->nxh; /* Add vertex to list */
s->nxh = vx;
vx->disth = 0; /* This is a hit vertex */
vx->hflag = s->flag;
#ifdef DEBUG
printf("%d: Vertex error improvement hit by %f (%f < %f)\n",dist, vx->eperr-vx->nba_eperr,vx->nba_eperr,vx->eperr);
if (vx->par) {
printf("Vertex no %d hit by its own parent ix %d\n",vx->no, nn->ix);
#ifdef WARNINGS
warning("Vertex no %d hit by its own parent ix %d",vx->no, nn->ix);
#endif
}
#endif
}
#ifdef DEBUG
else { /* If worse */
printf("%d: Vertex error not hit by %f (%f < %f)\n",dist, vx->eperr-vx->nba_eperr,vx->nba_eperr,vx->eperr);
}
#endif
}
vx->cflag = s->flag;
// ~~777
//if (vx->del && i_rand(1,1000) == 15) {
// printf("~1 failing to check vertex no %d\n",vx->no);
// vx->cflag = s->flag -1;
// vx->del = 0;
//}
}
/* Decide whether to recurse by adding vertexes to the new list */
if (!hit) {
/* Pre-hit recursion */
if (dist < dorec) { /* Still within search radius */
/* Add all the unsearched vertexes neighbors to the next search list */
for (j = 0; j < vx->nnv; j++) {
vtx *vx2 = vx->nv[j];
if (vx2->sflag == s->flag) /* Already been pre-hit searched */
continue;
#ifdef DEBUG
printf("%d: Adding vtx no %d to next pre-hit search list\n",dist, vx2->no);
#endif
/* Put the neighbour node on the search list */
vx2->slist = slist;
slist = vx2;
vx2->sflag = s->flag;
}
}
} else {
/* Post hit recursion */
if (vx->disth <= beyhit) { /* Still within post-hit search radius */
int disth = vx->disth + 1; /* Neighbours distance */
/* Add all the unsearched vertexes neighbors to the next search list */
for (j = 0; j < vx->nnv; j++) {
vtx *vx2 = vx->nv[j];
if (vx2->hflag == s->flag) { /* Already been post-hit searched */
if (disth >= vx2->disth)
continue; /* So skip it */
/* The already post-hit searched neighbour has an improved distance */
#ifdef DEBUG
printf("%d: Improving ph searched vtx %d disth from %d to %d\n",dist, vx2->no,vx2->disth,disth);
#endif
vx2->disth = disth; /* Improved distance to hit though */
if (vx2->disth > beyhit || vx2->opqsq)
continue; /* But it's still too far, or already on the list */
/* Search this neighbour again now that it is within radius */
} else {
vx2->disth = disth; /* Set hit distance */
}
#ifdef DEBUG
printf("%d: Adding vtx no %d to next post-hit search list (disth = %d)\n",dist, vx2->no,vx2->disth);
#endif
/* Put the neighbour node on the search list */
vx2->slist = slist;
slist = vx2;
vx2->sflag = vx2->hflag = s->flag;
vx2->opqsq = 1; /* On the list */
}
}
#ifdef DEBUG
else
printf("%d: Vertex %d disth %d is > beyhit %d so not recursing\n",dist, vx->no,vx->disth,beyhit);
#endif
}
} /* Next vertex in current list */
#ifdef DEBUG
printf("Finished inner loop because vx 0x%x = NULL\n",vx);
#endif
} /* Next list */
#ifdef DEBUG
printf("Finished outer loop because slist 0x%x = NULL, || dist %d > dorec %d\n",slist,dist,dorec);
#endif
return hit;
}
/* Non-recursive version of above used for sanity checking */
/* that doesn't set any flags on vx */
static int
ofps_check_vtx_sanity(ofps *s, node *nn, vtx *vx, int fixit) {
int i, j, e, di = s->di;
vtx *slist = NULL; /* Next to search list */
int dist; /* Distance from initial vertex */
double tol = 1e-6;
int hit = 0;
int par = 0;
#ifdef DEBUG
printf("ofps_check_vtx_sanity() for node ix %d and vertex no %d\n",nn->ix,vx->no);
#endif
if (vx->cflag == s->flag) {
#ifdef DEBUG
printf("Returning alread calculated del = %d\n",vx->del);
#endif
return vx->del; /* Already been checked */
}
if (vx->ofake) { /* ofake nodes can't be hit */
#ifdef DEBUG
printf("Returning ofake del = 0\n");
#endif
return 0;
}
#ifdef INDEP_SURFACE
/* Only check for hit if the vertex is visible to the node */
if (sm_vtx_node(s, vx, nn) == 0) {
#ifdef DEBUG
printf("Returning non-visible del = 0\n");
#endif
return 0;
}
#endif /* INDEP_SURFACE */
/* Check if node is already parent to this vertex. */
/* This only happens during fixups if the reposition fails and we */
/* retain the vertex with the deleted vertex location (not currently */
/* done), or by slim numerical margine, so ignore such hits. */
/* We treat a parent as a hit node for the purposes of recursion, */
/* and add it to a special list used to complete the vertex net. */
if (nn->ixm & vx->nix[MXPD+2]) { /* Is in nixm */
for (e = 0; e <= di; e++) { /* Do exact check */
if (nn->ix == vx->nix[e])
break;
}
if (e <= di)
par = 1;
}
if (nn->ix < 0) {
pleq *vp = &s->gpeqs[-1 - nn->ix];
double v = 0.0;
/* See if the vertex is on the wrong side of the plane */
for (v = vp->pe[di], e = 0; e < di; e++)
v += vp->pe[e] * vx->p[e];
if (!par && v > tol) {
hit = 1;
if (fixit) {
vx->slist = slist;
slist = vx;
vx->sflag = s->flag;
}
}
} else { /* Node rather than boundary plane */
double nba_eperr;
nba_eperr = ofps_comp_eperr7(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
/* See if the vertex eperr will be improved */
if (!par && (vx->eperr - nba_eperr) > tol) {
hit = 1;
if (fixit) {
vx->slist = slist;
slist = vx;
vx->sflag = s->flag;
}
}
}
#ifdef DEBUG
printf("Returning computed del = %d\n",hit);
#endif
return hit;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - */
/* Add a node to the currnent voronoi. */
/* Return nz if the addition fails due to there being no vetex hits or a cooincince. */
/* Return nz if abortonfail is set and we fail to position the node. */
/* (This theoretically shouldn't happen, but does, due to the perceptual */
/* geometry ?) */
static int add_node2voronoi(
ofps *s,
int poi, /* Index of sample point to update/create Voronoi surface */
int abortonfail /* 0 = ignore position failures, 1 = abort add if there are any failures */
) {
node *nn = s->n[poi]; /* Node in question */
int e, di = s->di;
int i, j;
vtx *vx = NULL; /* Closest vertex */
#ifdef DEBUG
printf("\nAdding Node ix %d pmask 0x%x at %s (perc %s) to Voronoi surface\n",poi,nn->pmask,ppos(di,nn->p),ppos(di,nn->v));
#endif
if (poi < 0)
error("Attempt to add fake point to veronoi surface");
if (nn->nvv > 0)
error("ofps: assert, node vertex info should be empty on add_node2voronoi() entry");
for (i = 0; i < 20; i++) {
int pci; /* Point cell list index */
acell *cp; /* Acceleration cell */
node *pp;
/* Check if by some misfortune, this node colides with an existing node. */
pci = ofps_point2cell(s, nn->v, nn->p); /* Grid index of cell of interest */
cp = &s->grid[pci];
for (pp = cp->head; pp != NULL; pp = pp->n) {
for (e = 0; e < di; e++) {
if (fabs(nn->v[e] - pp->v[e]) > (COINTOL * 100.0))
break; /* Not cooincident */
}
if (e >= di) { /* Cooincident */
#ifdef DEBUG
printf("Node oint collides with existing - joggling it\n");
// warning("Node oint collides with existing - joggling it");
#endif
/* Joggle it's position */
for (e = 0; e < di; e++) {
if (nn->p[e] < 0.5)
nn->p[e] += d_rand(0.0, 1e-4);
else
nn->p[e] -= d_rand(0.0, 1e-4);
}
/* Ignore confine planes. Next itter should fix it anyway ? */
ofps_clip_point6(s, nn->p, nn->p);
s->percept(s->od, nn->v, nn->p);
break;
}
}
if (pp == NULL)
break;
}
if (i >= 20) {
if (s->verb > 1)
warning("add_node2voronoi: Assert, was unable to joggle cooincindent point");
return 1;
}
#ifdef DEBUG
printf("Locating all the hit vertexs\n");
#endif
s->nvcheckhits = 0; /* Count number of vertexes hit by recursive check. */
s->batch = NULL; /* Nothing in pending delete list */
s->nup = NULL; /* Nothing in nodes to be updated list */
s->flag++; /* Marker flag for adding this node */
s->nxh = NULL; /* Nothing in nodes hit list */
#ifdef DEBUG
printf("Done check of vertexes for hits\n");
#endif
/* Number of nodes that would be checked by exaustive search */
s->vvpchecks += s->nv;
ofps_findhit_vtxs(s, nn);
#ifdef SANITY_CHECK_HIT
#ifdef DEBUG
printf("Doing sanity check of hits\n");
#endif
for (vx = s->uvtx; vx != NULL; vx = vx->link) { /* Check all vertexes */
if (vx->cflag != s->flag && ofps_check_vtx_sanity(s, nn, vx, 0)) {
warning("!!!!!! Sanity: Add hit missed vertex no %d at %s !!!!!!",vx->no,ppos(di,vx->p));
printf("!!!!!! Sanity: Add hit missed vertex no %d at %s !!!!!!\n",vx->no,ppos(di,vx->p));
/* Don't stop for out of gamut vertexes that would have been hit */
if (ofps_would_clip_point(s, vx->p))
continue;
/* Check if any of it's neighbours have been checked. */
for (j = 0; j < vx->nnv; j++) {
vtx *vx2 = vx->nv[j];
if (vx2->cflag == s->flag)
break; /* Yes */
}
if (j >= vx->nnv) {
warning("!!!!!! Sanity: Missed vertex was in isolated region");
} else {
warning("!!!!!! Sanity: Missed vertex was adjacent to no %d", vx->nv[j]->no);
}
#ifdef SANITY_CHECK_HIT_FATAL
error("Failed to locate all hit vertexes");
#endif /* SANITY_CHECK_HIT_FATAL */
}
}
#endif /* SANITY_CHECK_HIT */
#ifdef DEBUG
printf("There were %d vertexes that will be hit by adding node\n",s->nvcheckhits);
#endif
if (s->nvcheckhits == 0) {
if (s->verb > 1)
warning("Failed to get any vertex hits when adding a new node ix %d at %s",nn->ix,ppos(di,nn->p));
return 1;
}
/* Now turn all the hit vertexes into new vertexes. */
if (add_to_vsurf(s, nn, 0, abortonfail) > 0) {
s->add_hit++;
} else {
if (abortonfail)
return 1;
s->add_mis++;
}
ofps_add_nacc(s, nn); /* Add to spatial accelleration grid */
s->np++;
#ifdef DEBUG
printf("Done add_node2voronoi()\n");
#endif
return 0;
}
/* ------------------------------------------------------------------------------- */
/* Given a list of di plane equations, */
/* compute the intersection point. */
/* return nz if there is no intersection */
static int comp_vtx(ofps *s, double *p, pleq **peqs) {
int i, e, di = s->di;
double **ta, *TTA[MXPD], TA[MXPD][MXPD];
for (e = 0; e < di; e++)
TTA[e] = TA[e];
ta = TTA;
for (i = 0; i < di; i++) {
for (e = 0; e < di; e++)
ta[i][e] = peqs[i]->pe[e]; /* Plane normal becomes row of matrix */
p[i] = -peqs[i]->pe[di]; /* Plane constant becomes target */
}
/* Solve the simultaneous linear equations A.x = B */
/* Return 1 if the matrix is singular, 0 if OK */
if (polished_solve_se(ta, p, di))
return 1;
return 0;
}
/* --------------------------------------------------- */
/* Use a brute force search to (re-)create the vertex net. */
/* This is used in initialization. */
static void create_vtx_net(ofps *s) {
int ff, f, e, di = s->di;
vtx *vx1, *vx2;
#ifdef DEBUG
printf("Doing create_vtx_net\n");
#endif
/* For each vertx */
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
vx1->nnv = 0; /* Clear the current list */
#ifdef DEBUG
printf("Creating neighbourhood net for vtx no %d\n",vx1->no);
#endif
/* Search all other vertexes for neighbours */
for (vx2 = s->uvtx; vx2 != NULL; vx2 = vx2->link) {
int aa, bb, cc; /* Probable hit check */
int nnm, nmix;
//printf("~1 checking against vtx %d\n",vx2->no);
if (vx1 == vx2) {
//printf("~1 skip because it's the same\n");
continue;
}
/* Use the nixm to quickly check if all but one parent node matches */
aa = vx1->nix[MXPD+2]; /* nixm */
bb = vx2->nix[MXPD+2]; /* nixm */
if ((aa & bb) == 0 || (cc = aa & ~bb, (cc & (cc-1)) != 0)) {
//printf("~1 skip because nixm 0x%x and 0x%x don't match\n",aa,bb);
continue; /* It's certainly not */
}
/* Do an exact check of all except one node match */
for (nnm = ff = e = 0; e <= di; e++) {
for (f = ff; f <= di; f++) {
if (vx1->nix[e] == vx2->nix[f]) {
ff = f; /* Start from here next time */
break;
}
if (vx1->nix[e] > vx2->nix[f]) /* No point in looking further */
f = di;
}
if (f > di) { /* Didn't match */
if (++nnm > 1)
break;
nmix = e;
}
}
if (e <= di) {
//printf("~1 skip because nix %s and %s aren't one different\n",pcomb(di,vx1->nix),pcomb(di,vx2->nix));
continue; /* No match */
}
if (nnm == 0) {
error("ofps: two vertexes have the same nodes !\n"
"no %d at %s nix %s\nno %d at %s nix %s",
vx1->no,ppos(di,vx1->p),pcomb(di,vx1->nix),
vx2->no,ppos(di,vx2->p),pcomb(di,vx2->nix));
}
/* vx2 is a neighbour, so add it to the vtx net */
vtx_add_vertex(s, vx1, vx2);
//printf("~1 brute force: adding vtx %d as neighbour to %d\n",vx2->no,vx1->no);
}
}
}
/* --------------------------------------------------- */
/* Use a brute force search to discover all the valid */
/* sub-surface combinations. */
static void discover_subsuf(ofps *s) {
int co;
double p[MXPD];
pleq *peqs[MXPD];
int i, j, k, e, di = s->di;
setmask acm; /* Accumulated mask for special last entry */
if (s->sminit)
return; /* Do this once */
#ifdef DEBUG
printf("Computing subd face combinations\n");
#endif
if ((s->sc = (surfcomb *)calloc(sizeof(surfcomb), (1 << s->nbp))) == NULL)
error ("ofps: malloc failed on sufcomb array");
for (co = 0; co < (1 << s->nbp); co++) {
s->sc[co].co = co;
/* Count number of planes */
for (i = e = 0; e < s->nbp; e++) {
if (co & (1 << e))
i++;
/* Skip combo if odd and even dimension planes are set */
if ((e & 1) == 0 && e < (2 * di) && (co & (1 << e)) && (co & (1 << (e+1))))
break;
}
s->sc[co].nos = i;
if (i > di || e < s->nbp) {
s->sc[co].valid = 0;
continue;
}
/* Check that the combination results in a valid */
if (i == di) {
for ( j = e = 0; e < s->nbp; e++) {
if (co & (1 << e))
peqs[j++] = &s->gpeqs[e];
}
if (comp_vtx(s, p, peqs) != 0 || ofps_would_clip_point(s, p)) {
s->sc[co].valid = 0;
} else {
s->sc[co].valid = 1;
}
} else {
if (co == 0 || i == 1) {
s->sc[co].valid = 1;
} else {
s->sc[co].valid = -1;
}
}
//printf("~1 val %s sc[%d].valid = %d\n",icmPdv(di, p), co,s->sc[co].valid);
}
/* Go through the unknown combinations, and see if there */
/* is a valid lower dimensional combination that is valid. */
for (co = 0; co < (1 << s->nbp); co++) {
if (s->sc[co].valid == -1) {
for (i = co+1; i < (1 << s->nbp); i++) {
if ((i & co) == co && s->sc[i].valid == 1) {
s->sc[co].valid = 1;
break;
}
}
if (i >= (1 << s->nbp)) /* Failed to find a valid combination */
s->sc[co].valid = 0;
}
}
#ifdef USE_DISJOINT_SETMASKS
/* We can reduce the number of setmask bits by figuring out which */
/* combinations are disjoint, and using the same setmask bits for disjoint */
/* combinations. For CMYK, this reduces the setmask from 80-100 to less than 32 bits, */
/* permiting faster mask manipulation. */
{
surfcomb *scp, *zd = NULL; /* Zero Dimension combinations */
surfcomb *sets = NULL; /* Sets at a given nos */
int nsets = 0; /* Current number of sets */
int _nsets = 0; /* Allocated array size */
int nos; /* Number of surfaces */
/* init the circular lists, and add the 0D points to their list */
for (k = co = 0; co < (1 << s->nbp); co++) {
s->sc[co].ds = &s->sc[co]; /* Init circular list to itself */
if (s->sc[co].valid == 0)
continue;
/* Create a list of 0D points and count them */
if (s->sc[co].nos == di) {
k++;
if (zd == NULL)
zd = &s->sc[co];
else {
s->sc[co].ds = zd->ds;
zd->ds = &s->sc[co];
}
}
}
if (zd == NULL)
error("No zero-dim surface combinations (s->nbp = %d)",s->nbp);
//printf("~1 total 0D points = %d\n",k);
/* Temporarily use the setmask to track 0D hits */
sm_init(s, k);
k = 2; /* Count total disjoint sets, including 2 for di D and 0 D */
/* Locates sets for each dimension level */
for (nos = 1; nos < di; nos++) {
nsets = 0;
//printf("~1 doing nos = %d\n",nos);
/* Add the next combination to the sets */
for (co = 0; co < (1 << s->nbp); co++) {
if (s->sc[co].valid == 0 || s->sc[co].nos != nos)
continue;
//printf("~1 checking combo 0x%x\n",co);
/* Figure out 0D hits on this combo */
i = 0;
scp = zd;
do {
if ((co & scp->co) == co)
sm_setbit(s, &s->sc[co].i_sm, i, 1);
i++;
scp = scp->ds;
} while(scp != zd);
//printf("~1 combo 0x%x has hits %s\n",co,psm(s,&s->sc[co].i_sm));
/* Search through the existing sets, and see */
/* if this combo is disjoint */
for (j = 0; j < nsets; j++) {
setmask tsm;
if (sm_and(s, &tsm, &sets[j].i_sm, &s->sc[co].i_sm) == 0) {
/* Add this combo to the existing set */
//printf("~1 adding to set %d\n",j);
s->sc[co].ds = sets[j].ds->ds;
sets[j].ds->ds = &s->sc[co];
sm_or(s, &sets[j].i_sm, &sets[j].i_sm, &s->sc[co].i_sm);
break;
}
//else printf("Miss on set %d hits %s, AND %s\n",j,psm(s,&sets[j].i_sm),psm(s,&tsm));
}
/* If we can't use an existing set, create a new one */
if (j >= nsets) {
if (nsets >= _nsets) {
_nsets = 2 * _nsets + 5;
if ((sets = (surfcomb *)realloc(sets, sizeof(surfcomb) * _nsets)) == NULL)
error("malloc failed on disjoint sets size %d", _nsets);
}
sm_cp(s, &sets[j].i_sm, &s->sc[co].i_sm); /* Hits to this set */
sets[j].ds = &s->sc[co]; /* Only entry in circular list */
//printf("New set %d hits %s\n",j,psm(s,&sets[j].i_sm));
nsets++;
k++;
}
}
}
if (sets != NULL)
free(sets);
#ifdef DEBUG
printf("Total number of setmask disjoint sets = %d\n",k);
#endif
/* Setup the setmask params */
sm_init(s, k);
/* Assign the individual setmask bits */
for (i = co = 0; co < (1 << s->nbp); co++) {
if (s->sc[co].valid == 0 || s->sc[co].smset == 1)
continue;
//printf("~1 setting mask bit on comb 0x%x and its set\n",co);
/* Assign setmask to all in this set */
scp = &s->sc[co];
do {
//printf("~1 setting mask bit %d on comb 0x%x\n",i,scp->co);
sm_set(s, &scp->i_sm, 0); /* Clear temporary hit mask */
sm_setbit(s, &scp->i_sm, i, 1);
scp->smset = 1;
scp = scp->ds;
} while(scp != &s->sc[co]);
i++;
}
}
#else /* !USE_DISJOINT_SETMASKS */
/* Count the number of valid combinations */
for (i = co = 0; co < (1 << s->nbp); co++) {
if (s->sc[co].valid == 0)
continue;
i++;
}
#ifdef DEBUG
printf("Total number of setmask sets = %d\n",i);
#endif
/* Setup the setmask params */
sm_init(s, i);
/* Assign the individual setmask bits */
for (i = co = 0; co < (1 << s->nbp); co++) {
if (s->sc[co].valid == 0)
continue;
sm_setbit(s, &s->sc[co].i_sm, i, 1);
i++;
}
#endif /* !USE_DISJOINT_SETMASKS */
sm_set(s, &acm, 0); /* Init overall accumulated mask */
/* Compute the accumulated setmask bits */
for (i = 0; i < (1 << s->nbp); i++) {
if (s->sc[i].valid == 0)
continue;
for (j = 0; j < (1 << s->nbp); j++) {
if ((i & j) != j || s->sc[j].valid == 0)
continue;
sm_or(s, &s->sc[i].a_sm, &s->sc[i].a_sm, &s->sc[j].i_sm);
}
sm_or(s, &acm, &acm, &s->sc[i].i_sm);
}
/* Set special "all planes, all valid" combination as the last */
/* entry for use by fake surface nodes. */
s->sc[(1 << s->nbp)-1].valid = 1;
s->sc[(1 << s->nbp)-1].nos = s->nbp;
sm_cp(s, &s->sc[(1 << s->nbp)-1].i_sm, &acm);
sm_cp(s, &s->sc[(1 << s->nbp)-1].a_sm, &acm);
s->sc[(1 << s->nbp)-1].smset = 1;
s->sc[(1 << s->nbp)-1].ds = NULL;
#ifdef MAXINDEP_2D
/* Go through the combinations and invalidate any */
/* that are not full-d or more than 2D */
for (co = 0; co < (1 << s->nbp); co++) {
if (s->sc[co].valid) {
// if (s->sc[co].nos != 0 && (di - s->sc[co].nos) > 1) // test in 3D
if (s->sc[co].nos != 0 && (di - s->sc[co].nos) > 2)
s->sc[co].valid = 0;
}
}
#endif /* MAXINDEP_2D */
#ifdef DEBUG
/* Print diagnostics */
for (i = 0; i < (1 << s->nbp); i++) {
if (s->sc[i].valid == 0)
continue;
printf(" Mask 0x%x, setmasks i = %s, a = %s\n",i,psm(s,&s->sc[i].i_sm),psm(s,&s->sc[i].a_sm));
}
#endif
s->sminit = 1;
}
/* --------------------------------------------------- */
/* Compute a simple but unbounded model of the */
/* perceptual function. We use the current vertex values */
/* to setup the model */
/* (It would be faster to do the optimization per output channel!) */
/* Matrix optimisation function handed to powell() */
static double xfitfunc(void *edata, double *x) {
ofps *s = (ofps *)edata;
int e, di = s->di;
double rv = 0.0;
vtx *vx;
/* For all the vertexes */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
double v[MXPD], ev;
/* Apply matrix cube interpolation */
icxCubeInterp(x, di, di, v, vx->p);
/* Evaluate the error */
for (ev = 0.0, e = 0; e < di; e++) {
double tt;
tt = vx->v[e] - v[e];
ev += tt * tt;
}
rv += ev;
}
// printf("~1 rv = %f\n",rv);
return rv;
}
/* Fit the unbounded perceptual model to just the inside vertexes */
static void init_pmod(ofps *s) {
int e, di = s->di;
double sa[MXPD * (1 << MXPD)];
double rerr;
/* Setup matrix to be closest values initially */
for (e = 0; e < (1 << di); e++) { /* For each colorant combination */
int j, f;
double bdif = 1e6;
double ov[MXPD];
vtx *vx, *bvx = NULL;
/* Search the vertex list to find the one closest to this input combination */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
double dif = 0.0;
if (vx->ofake)
continue; /* Ignore outside vertexes */
for (j = 0; j < di; j++) {
double tt;
if (e & (1 << j))
tt = s->imax[j] - vx->p[j];
else
tt = s->imin[j] - vx->p[j];
dif += tt * tt;
}
if (dif < bdif) { /* best so far */
bdif = dif;
bvx = vx;
if (dif < 0.001)
break; /* Don't bother looking further */
}
}
for (f = 0; f < di; f++)
s->pmod[f * (1 << di) + e] = bvx->v[f];
}
for (e = 0; e < (di * (1 << di)); e++)
sa[e] = 10.0;
if (powell(&rerr, di * (1 << di), s->pmod, sa, 0.001, 1000,
xfitfunc, (void *)s, NULL, NULL) != 0) {
if (s->verb > 1)
warning("Powell failed to converge, residual error = %f",rerr);
}
#ifdef DEBUG
printf("Perceptual model fit residual = %f\n",sqrt(rerr));
#endif
s->pmod_init = 1;
}
/* --------------------------------------------------- */
/* Init fake node contents, and setup the initial */
/* voronoi surface with the first node. */
static void ofps_binit(ofps *s) {
int e, di = s->di;
int doink = 0;
int i, j;
DCOUNT(co, MXPD, di, 0, 0, 2); /* Count through corner verticies */
int iix = -2 * di - 2; /* Fake inside node index */
node *inp = s->n[iix]; /* Fake inside node */
int oix = -2 * di - 3; /* Fake outside node index */
node *onp = s->n[oix]; /* Fake outside node */
double ivtx_whts[] = { /* Initial vertex weightings */
3.7144267283692024e+165,
1.3997102851752585e-152,
6.1677886722367450e+223,
1.7281009363426126e+097,
2.0087766625640005e-139,
4.9406564584124654e-323,
7.7791723264315535e-260,
8.5733372291341995e+170,
6.0046007797559735e-067,
2.8214561724952793e+243,
5.0132438738338732e+262,
1.6259745436952323e-260,
7.9968034958246946e+001
};
double vtxwt; /* Combined initial vertexnode weighting */
unsigned int fullmask = 0;
#ifdef DEBUG
printf("Binit called\n");
#endif
if (s->ilimit < (double)di) /* Ink limit is active */
doink = 1;
/* Init fake inside and outside node */
inp->ix = iix;
inp->fx = 1;
inp->pmask = 0;
onp->ix = oix;
onp->fx = 1;
onp->pmask = 0;
for (i = 0; i < (2 * di); i++)
fullmask |= 1 << i;
if (doink)
fullmask |= 1 << i;
/* Init the axis aligned gamut surface plane equations */
/* and also setup nodes that are indexes by the fake indexes */
/* with just the information that will be used. */
for (i = 0; i < (2 * di); i++) { /* unit cell at 0 */
int ii = i >> 1; /* Dimension */
int ix; /* Surface "node" index */
pleq *vp; /* plane being initialized */
node *np;
ix = -i-1; /* -1 to -2di fake other nodes */
vp = &s->gpeqs[-1-ix]; /* Pointer to plane associated with fake node */
vp->ix = ix;
for (e = 0; e < di; e++)
vp->pe[e] = 0.0;
vp->pe[ii] = i & 1 ? 1.0 : -1.0; /* Normal */
vp->pe[di] = i & 1 ? -s->imax[ii] : s->imin[ii]; /* Constant */
np = s->n[ix];
np->ix = ix;
np->fx = 1; /* They don't move */
np->nsp = 1;
np->sp[0] = vp;
np->pmask = fullmask;
// np->pmask = 1 << i; /* fake surface node pmask is itself for cmask ?? */
// onp->pmask = inp->pmask |= np->pmask;
}
s->nbp = 2 * di; /* Number of boundary planes */
/* Add ink limit surface plane and its fake node */
if (doink) { /* Ink limit plane is orthogonal to diagonal */
int ix; /* Surface "node" index */
pleq *vp; /* plane being initialized */
node *np;
double len;
ix = -i-1; /* -1 to -2di fake other nodes */
vp = &s->gpeqs[-1-ix]; /* Pointer to plane associated with fake node */
vp->ix = ix;
len = 1.0/sqrt((double)di); /* Normalised length */
for (e = 0; e < di; e++)
vp->pe[e] = len;
vp->pe[di] = -s->ilimit * len;
np = s->n[ix];
np->ix = ix;
np->fx = 1; /* They don't move */
np->nsp = 1;
np->sp[0] = vp;
np->pmask = fullmask;
// np->pmask = 1 << i; /* fake surface node pmask is itself for cmask ?? */
// onp->pmask = inp->pmask |= np->pmask;
s->nbp++; /* Number of boundary planes */
} else {
s->n[-i-1]->ix = -i-1; /* Label unused node */
}
#ifdef DEBUG
printf("Number of boundary planes = %d\nDiscovering all valid veronoi sub-surfaces\n",s->nbp);
#endif
discover_subsuf(s);
#ifdef DEBUG
printf("Creating rectangular initial vertexes\n");
#endif
/* Compute initial node weighting */
for (vtxwt = 0.0, i = 0; i < (sizeof(ivtx_whts)/sizeof(double)-1); i++)
vtxwt += log(ivtx_whts[i]);
vtxwt += ivtx_whts[i];
/* Create initial verticies, one for each di combination of planes, */
/* and keep the ones that are in gamut. */
DC_INIT(co);
while(!DC_DONE(co)) {
double p[MXPD];
vtx *vi, *vo; /* Inside and outside vertex */
/* Compute vertex location */
for (e = 0; e < di; e++) {
if (co[e] != 0)
p[e] = s->imax[e];
else
p[e] = s->imin[e];
}
if (ofps_would_clip_point(s, p)) {
#ifdef DEBUG
printf("Position %s rejected, out of gamut\n",ppos(di,p));
#endif
goto next_co;
}
#ifdef DEBUG
printf("Position %s accepted\n",ppos(di,p));
#endif
vi = new_vtx(s);
vo = new_vtx(s);
for (e = 0; e < di; e++)
vi->p[e] = vtxwt * p[e];
ofps_cc_percept(s, vi->v, vi->p);
for (e = 0; e < di; e++)
vo->p[e] = (10.0 * (p[e] - 0.5)) + 0.5;
// ofps_cc_percept(s, vo->v, vo->p);
/* Compute nodes involved */
for (e = 0; e < di; e++) {
if (co[e] == 0) {
vo->nix[e] = vi->nix[e] = -1 - (2 * e + 0);
} else {
vo->nix[e] = vi->nix[e] = -1 - (2 * e + 1);
}
}
vi->nix[di] = iix; /* First nodee */
#ifdef DEBUG
printf("ivertex nix %s\n",pcomb(di,vi->nix));
#endif
sort_nix(s, vi->nix);
vi->eperr = 10000.0; /* Very bad, so they get chosen first */
vi->eserr = 10000.0;
det_vtx_gsurf(s, vi); /* Set pmask & cmask */
sm_cp(s, &vi->vm, &s->sc[vi->cmask].a_sm); /* Set visibility */
vi->ifake = 1; /* Inside fake */
vtx_cache_add(s, vi); /* Add it to the vertex cache and spatial accelleration grid */
ofps_add_vacc(s, vi);
ofps_add_vseed(s, vi);
vo->nix[di] = oix; /* Fake outside node */
#ifdef DEBUG
printf("overtex nix %s\n",pcomb(di,vo->nix));
#endif
sort_nix(s, vo->nix);
vo->eperr = vtxwt * -9.0; /* Better than zero error */
vo->eserr = vtxwt * -9.0;
/* Leave pmask,cmask = 0 */
vo->pmask = vi->pmask; /* Copy from inner vertexes */
vo->cmask = vi->cmask;
sm_cp(s, &vo->vm, &s->sc[vo->cmask].a_sm); /* Set visibility */
vo->ofake = 1; /* Outside fake - don't plot vnets and don't use */
/* for perceptual function extension. */
vo->used = 1; /* Not a candidate for seeding */
next_co:;
DC_INC(co);
}
/* Add ink limit vertexes */
if (doink) { /* Ink limit plane is orthogonal to diagonal */
COMBO(nco, MXPD, di-1, s->nbp-1); /* di-1 out of neighbor nodes combination counter */
#ifdef DEBUG
printf("Creating ink limit vertexes\n");
#endif
/* Intersect the ink limit plane with each combination of */
/* it and and di-1 of the existing planes, to generate */
/* potential vertexes, and keep the ones that are in gamut. */
CB_INIT(nco);
while (!CB_DONE(nco)) {
pleq *peqs[MXPD];
double p[MXPD];
for (e = 0; e < (di-1); e++) {
peqs[e] = &s->gpeqs[nco[e]];
}
peqs[e] = &s->gpeqs[2 * di];
/* Compute device location of intersection */
if (comp_vtx(s, p, peqs) == 0) {
vtx *vi, *vo; /* Inside and outside vertex */
if (ofps_would_clip_point(s, p)) {
#ifdef DEBUG
printf("Position %s rejected, out of gamut\n",ppos(di,p));
#endif
goto next_nco;
}
#ifdef DEBUG
printf("Position %s accepted\n",ppos(di,p));
#endif
vi = new_vtx(s);
vo = new_vtx(s);
/* Device and perceptual */
for (e = 0; e < di; e++)
vi->p[e] = vtxwt * p[e];
ofps_cc_percept(s, vi->v, vi->p);
for (e = 0; e < di; e++)
vo->p[e] = (10.0 * (p[e] - 0.5)) + 0.5;
// ofps_cc_percept(s, vo->v, vo->p);
for (e = 0; e < (di-1); e++)
vo->nix[e] = vi->nix[e] = -1-nco[e]; /* Fake gamut surface plane nodes */
vo->nix[e] = vi->nix[e] = -2 * di -1; /* Fake ink limit node */
vi->nix[di] = iix; /* First node */
#ifdef DEBUG
printf("ivertex nix %s\n",pcomb(di,vi->nix));
#endif
sort_nix(s, vi->nix);
vi->eperr = 10000.0; /* Very bad */
vi->eserr = 10000.0;
det_vtx_gsurf(s, vi); /* Set pmask & cmask */
sm_cp(s, &vi->vm, &s->sc[vi->cmask].a_sm); /* Set visibility */
vi->ifake = 1; /* Inside fake */
vtx_cache_add(s, vi); /* Add to vertex cache and spatial accelleration grid */
ofps_add_vacc(s, vi);
ofps_add_vseed(s, vi);
vo->nix[di] = oix; /* Fake outside node */
#ifdef DEBUG
printf("overtex nix %s\n",pcomb(di,vo->nix));
#endif
sort_nix(s, vo->nix);
vo->eperr = vtxwt * -9.0; /* Better than zero error */
vo->eserr = vtxwt * -9.0;
/* Leave pmask,cmask = 0 */
vo->pmask = vi->pmask; /* Copy from inner vertexes */
vo->cmask = vi->cmask;
sm_cp(s, &vo->vm, &s->sc[vo->cmask].a_sm); /* Set visibility */
vo->ofake = 1; /* Outside fake - don't plot vnets and don't use */
/* for perceptual function extension. */
vo->used = 1; /* Not a candidate for seeding */
}
next_nco:;
CB_INC(nco);
} /* Next combination */
}
/* Create an initial vertex network */
create_vtx_net(s);
/* Fit the unbounded perceptual model to just the inside vertexes */
if (s->pmod_init == 0)
init_pmod(s);
/* Compute the nodes node and vertex lists */
ofps_re_create_node_node_vtx_lists(s);
#ifdef DUMP_STRUCTURE
printf("Done binit\n");
dump_node_vtxs(s, 1);
// dump_node_vtxs2(s, "Done binit");
printf("=========================================================================\n");
#endif
#ifdef DUMP_PLOT_SEED
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 0, -1); /* Device, No wait, verticies */
#endif /* DUMP_PLOT_SEED */
}
/* --------------------------------------------------- */
/* Setup the perceptual lookup cache */
static void
ofps_init_pcache(ofps *s) {
int i, e;
int di = s->di;
int gr, gres[MXPD];
int tinp = s->tinp;
#ifdef DEBUG
printf("Initializing perceptual lookup cache\n");
#endif
/* Choose a grid resolution that aims for aproximately TNPAGRID nodes per grid */
if (tinp > 10000)
tinp = 10000;
gr = (int)(pow(tinp/TNPAGRID, 1.0/di) + 0.5);
gr |= 1; /* make it odd */
if (gr < TNPAGRIDMINRES)
gr = TNPAGRIDMINRES;
if (gr > TNPAGRIDMAXRES)
gr = TNPAGRIDMAXRES;
#ifndef DEBUG
if (s->verb)
#endif
{
printf("Perceptual cache resolution = %d\n",gr);
printf("Seeding cache..."); fflush(stdout);
}
/* Create a rspl to cache the perceptual lookup */
if ((s->pcache = new_rspl(RSPL_NOFLAGS, s->di, s->di)) == NULL)
error("new_rspl failed");
for (e = 0; e < di; e++)
gres[e] = gr;
s->pcache_res = gr;
// s->pcache->set_rspl(s->pcache, RSPL_SET_APXLS, s->od, s->percept, NULL, NULL, gres, NULL, NULL);
/* Filtering seems to make this more robust for some profiles, less for others. */
if (s->percept != default_ofps_to_percept)
s->pcache->set_rspl(s->pcache, RSPL_NOFLAGS, s, filtered_ofps_to_percept,
NULL, NULL, gres, NULL, NULL);
else
s->pcache->set_rspl(s->pcache, RSPL_NOFLAGS, s->od, s->percept,
NULL, NULL, gres, NULL, NULL);
/* Hmm. Should we store the underlying ->percept & ->od somewhere before we overwrite it ? */
s->percept = ofps_cache_percept;
s->od = s->pcache;
#ifndef DEBUG
if (s->verb)
#endif
printf("done\n");
}
/* --------------------------------------------------- */
/* Setup the acceleration grid structure and perceptual cache. */
/* The grid is in device space, although it is used to find the point */
/* with the smallest eperr. */
/* (Note that ofps_cc_percept() can't be called on clipped values yet) */
static void
ofps_init_acc1(ofps *s) {
int i, e;
int di = s->di;
int gres[MXPD];
int tinp = s->tinp;
#ifdef DEBUG
printf("Initializing accelleration array (1)\n");
#endif
/* Create acceleration grid array */
/* Choose a grid resolution that aims for aproximately TNPAGRID nodes per grid */
if (tinp > 10000)
tinp = 10000;
s->agres = (int)(pow(tinp/TNPAGRID, 1.0/di) + 0.5);
if (s->agres < 1)
s->agres = 1;
if (s->verb)
printf("Acceleration grid res = %d\n",s->agres);
#ifdef DEBUG
printf("Acceleration grid res = %d\n",s->agres);
#endif
/* Cell width in grid units */
s->gw = 1.0/s->agres;
/* Compute grid index multipliers */
/* (We allocate an two extra rows for boundary cells to be looked up.) */
for (s->gim[0] = 1, e = 1; e < di; s->gim[e] = s->gim[e-1] * (s->agres+2), e++)
;
/* Compute cell diagonal distance */
s->gcd = sqrt((double)di * s->gw * s->gw);
/* Compute number of cells in grid (with two extra rows) */
for (s->nig = 1, e = 0; e < di; e++)
s->nig *= (s->agres+2);
/* Allocate grid (with two extra rows) */
if ((s->_grid = (acell *)malloc(sizeof(acell) * s->nig)) == NULL)
error ("ofps: malloc failed for acceleration grid");
/* Set pointer to base of grid without extra row */
for (s->grid = s->_grid, e = 0; e < di; e++)
s->grid += s->gim[e];
/* Initialise grid (including extra gruard rows) */
{
DCOUNT(co, MXPD, di, -1, -1, (s->agres+1));
i = 0;
DC_INIT(co);
while (!DC_DONE(co)) {
acell *cp = &s->_grid[i];
unsigned int gflag = 0;
for (e = 0; e < di; e++) {
if (co[e] < 0 || co[e] >= s->agres)
gflag = BOUND_GFLAG;
cp->co[e] = co[e]; /* Grid coordinate of base of cell */
cp->p[e] = co[e] * s->gw; /* Device coord of base of cell */
cp->cp[e] = (co[e] + 0.5) * s->gw; /* Device coord of center of cell */
}
cp->gflag = gflag;
cp->head = NULL;
cp->vhead = NULL;
DC_INC(co);
i++;
}
}
s->gflag = 0;
/* Create the neighbour offset list */
/* There are 3^di -1 neighbours for each cell */
for (s->nacnl = 1, e = 0; e < di; s->nacnl *= 3, e++)
;
s->nacnl--;
if ((s->acnl = (int *)malloc(sizeof(int) * s->nacnl)) == NULL)
error ("ofps: malloc failed on acnl list");
/* Initialise list from cube */
{
DCOUNT(co, MXPD, di, -1, -1, 2);
i = 0;
DC_INIT(co);
while (!DC_DONE(co)) {
/* check we're not at the center cell */
for (e = 0; e < di; e++) {
if (co[e] != 0)
break;
}
if (e < di) { /* Not center cell */
/* Compute offset */
for (s->acnl[i] = 0, e = 0; e < di; e++) {
s->acnl[i] += co[e] * s->gim[e];
}
//printf("~1 acnl[%d] for co %s = %d\n",i,pco(di,co),s->acnl[i]);
i++;
}
DC_INC(co);
}
}
}
/* Init the grid location p[] and v[] values */
static void
ofps_init_acc2(ofps *s) {
int i, e, di = s->di;
int k;
DCOUNT(co, MXPD, di, 0, 0, 2);
double maxratio, avgratio, noratio;
double aitters = 0.0;
#ifdef DEBUG
printf("Initializing accelleration array (2)\n");
#endif
for (i = 0; i < s->nig; i++) {
acell *cp = &s->_grid[i];
/* Lookup perceptual base and center values */
ofps_cc_percept(s, cp->v, cp->p);
ofps_cc_percept(s, cp->cv, cp->cp);
}
/* Compute the worst case eperr from a corner to the center */
maxratio = -1.0;
avgratio = noratio = 0.0;
for (i = 0; i < s->nig; i++) {
acell *cp = &s->_grid[i];
double ratio;
double mov[MXPD];
if (cp->gflag == BOUND_GFLAG)
continue;
#define ACELITERS 20
for (k = 0; ; k++) {
double eperr_avg, eperr_min, eperr_max, no;
cp->eperr = 0.0;
DC_INIT(co);
eperr_avg = no = 0.0;
eperr_min = 1e300;
eperr_max = -1.0;
while (!DC_DONE(co)) {
acell *np = &s->_grid[i];
double eperr;
int j;
/* Locate cell corner */
for (j = 0, e = 0; e < di; e++)
j += co[e] * s->gim[e];
np = &s->_grid[i + j];
/* eperr from that corner to center of this cell */
eperr = ofps_comp_eperr(s, NULL, cp->cv, cp->cp, np->v, np->p, 0);
eperr_avg += eperr;
if (eperr > eperr_max)
eperr_max = eperr;
if (eperr < eperr_min)
eperr_min = eperr;
no++;
if (eperr > cp->eperr)
cp->eperr = eperr;
DC_INC(co);
}
eperr_avg /= no;
ratio = eperr_max/eperr_min;
if (k >= ACELITERS || ratio < 1.2) {
avgratio += ratio;
noratio++;
if (ratio > maxratio)
maxratio = ratio;
break;
} else {
/* Adjust the center position to minimuze range of eperr's */
for (e = 0; e < di; e++)
mov[e] = 0.0;
//printf("~1 cp was at %s\n",ppos(di,cp->cp));
DC_INIT(co);
while (!DC_DONE(co)) {
acell *np = &s->_grid[i];
double eperr, wf;
int j;
/* Locate cell corner */
for (j = 0, e = 0; e < di; e++)
j += co[e] * s->gim[e];
np = &s->_grid[i + j];
/* Compose new center point from weighted corner points. */
/* Weighting is proportional to eperr value */
eperr = ofps_comp_eperr(s, NULL, cp->cv, cp->cp, np->v, np->p, 0);
if (eperr < eperr_avg) {
/* Move away from corner */
wf = (eperr_avg - eperr)/eperr_avg;
//printf("~1 eperr %f, avg %f, min %f, wf %f\n",eperr,eperr_avg,eperr_min,wf);
for (e = 0; e < di; e++)
mov[e] += wf * (cp->cp[e] - np->p[e]);
} else {
/* Move towards corner */
wf = (eperr - eperr_avg)/eperr_avg;
//printf("~1 eperr %f, avg %f, max %f, wf %f\n",eperr,eperr_avg,eperr_max,wf);
for (e = 0; e < di; e++)
mov[e] += wf * (np->p[e] - cp->cp[e]);
}
DC_INC(co);
}
for (e = 0; e < di; e++) {
mov[e] = 1.2 * mov[e] / no;
cp->cp[e] += mov[e];
}
ofps_cc_percept(s, cp->cv, cp->cp);
//printf("~1 moving by %s to %s\n",ppos(di,mov),ppos(di,cp->cp));
}
}
aitters += k;
cp->eperr *= CELLMAXEPERRFF; /* Times the fudge factor */
}
aitters /= s->nig;
avgratio /= noratio;
#ifdef DEBUG
printf("Average acell eperr ratio = %f, maximum = %f, avg itters %f\n",avgratio,maxratio,aitters);
#endif
s->agrid_init = 1;
}
/* Convert a location into an acceleration cell index */
static int
ofps_point2cell(ofps *s, double *v, double *p) {
int i, e, di = s->di;
int agres = s->agres;
double pp[MXPD];
ofps_clip_point(s, pp, p);
for (i = e = 0; e < di; e++) {
int t;
t = (int)floor(agres * pp[e]);
if (t < 0)
t = 0;
else if (t >= agres)
t = (agres-1);
i += s->gim[e] * t;
}
return i;
}
/* Diagnostic: Return the grid coordinates */
static void ofps_gridcoords(ofps *s, int *c, double *v, double *p) {
int i, e, di = s->di;
int agres = s->agres;
double pp[MXPD];
ofps_clip_point(s, pp, p);
for (i = e = 0; e < di; e++) {
int t;
c[e] = (int)floor(agres * pp[e]);
if (c[e] < 0)
c[e] = 0;
else if (c[e] >= agres)
c[e] = (agres-1);
}
}
/* Add a node to the spatial acceleration grid */
/* Note that little more than the node perceptual value */
/* may be valid when this is called. */
static void
ofps_add_nacc(ofps *s, node *n) {
int pci;
acell *cp;
if (n->ix < 0)
return;
pci = ofps_point2cell(s, n->v, n->p);
cp = &s->grid[pci];
n->n = cp->head;
if (cp->head != NULL)
cp->head->pn = &n->n;
cp->head = n;
n->pn = &cp->head;
n->pci = pci;
n->cell = cp;
#ifdef SANITY_CHECK_CLOSEST
if (s->agrid_init) {
double eperr;
/* Check that the eperr to the center of the cell */
/* is less than the worst case for that cell */
eperr = ofps_comp_eperr(s, NULL, cp->cv, cp->cp, n->v, n->p, n->nsp);
if (eperr > cp->eperr) {
warning("Sanity check ofps_add_nacc() node ix %d eperr %f > cell eperr %f",n->ix,eperr,cp->eperr);
printf("Sanity check ofps_add_nacc() node ix %d eperr %f > cell eperr %f\n",n->ix,eperr,cp->eperr);
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("ofps_add_nacc cell eperr failed");
#endif
}
}
#endif /* SANITY_CHECK_CLOSEST */
}
/* Remove a node from the spatial acceleration grid */
static void
ofps_rem_nacc(ofps *s, node *n) {
if (n->ix < 0)
return;
if (n->pn != NULL) { /* If is on acceleration list, remove it */
*n->pn = n->n;
if (n->n != NULL)
n->n->pn = n->pn;
}
n->pn = NULL;
n->n = NULL;
}
/* Add a vertex to the spatial acceleration grid */
static void
ofps_add_vacc(ofps *s, vtx *vx) {
int pci;
acell *cp;
/* Normal spatial acceleration grid */
pci = ofps_point2cell(s, vx->v, vx->p);
cp = &s->grid[pci];
vx->n = cp->vhead;
if (cp->vhead != NULL)
cp->vhead->pn = &vx->n;
cp->vhead = vx;
vx->pn = &cp->vhead;
vx->pci = pci;
#ifdef DEBUG
printf("Adding vertex no %d to spatial accelleration grid in cell %d\n",vx->no,pci);
#endif
#ifdef SANITY_CHECK_CLOSEST
if (s->agrid_init) {
int e, di = s->di;
double eperr;
double p[MXPD], v[MXPD];
/* Check that the eperr to the center of the cell */
/* is less than the worst case for that cell */
/* Clip point in case it lies outside the grid, */
/* and would give an excessive eperr */
for (e = 0; e < di; e++) {
p[e] = vx->p[e];
if (p[e] < 0.0)
p[e] = 0.0;
else if (p[e] > 1.0)
p[e] = 1.0;
}
ofps_cc_percept(s, v, p);
eperr = ofps_comp_eperr(s, NULL, cp->cv, cp->cp, v, p, 0);
if (eperr > cp->eperr) {
//printf("~1 Cell ix %d co %s center %s (%s), vtx at %s (%s) clipped to %s (%s)\n",pci,pco(s->di,cp->co),ppos(di,cp->cp),ppos(di,cp->cv),ppos(di,vx->p),ppos(di,vx->v),ppos(di,p),ppos(di,v));
warning("Sanity check ofps_add_vacc() vtx no %d eperr %f > cell eperr %f",vx->no,eperr,cp->eperr);
printf("Sanity check ofps_add_vacc() vtx no %d eperr %f > cell eperr %f\n",vx->no,eperr,cp->eperr);
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("ofps_add_vacc cell eperr failed");
#endif
}
}
#endif /* SANITY_CHECK_CLOSEST */
}
/* Add a vertex to the seeding groups */
static void
ofps_add_vseed(ofps *s, vtx *vx) {
double oog;
int pci;
acell *cp;
#ifdef DEBUG
printf("Adding vertex no %d to sorted binary tree\n",vx->no);
#endif
/* Add the vertex to the sorted binary trees */
if ((aat_ainsert(s->vtreep, (void *)vx)) == 0)
error("aat_ainsert vertex malloc failed");
/* Out of gamut vertexes are not candidates for seeds */
if ((oog = ofps_oog(s, vx->p)) > COINTOL) {
vx->used = 1;
//printf("Setting used on vtx no %d, used %d, eserr %f, vm %s nsp %d oog by %e\n",vx->no,vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp, oog);
}
if (vx->used == 0) {
#ifdef INDEP_SURFACE
/* Only pick full dimensional visible vertexes for seeding group, */
/* since only they have a full-d error value. */
if (sm_andtest(s, &s->sc[0].a_sm, &vx->vm) != 0) {
#endif
//printf("Adding (3) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",vx->no,vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp);
if ((aat_ainsert(s->vtrees[vx->nsp], (void *)vx)) == 0)
error("aat_ainsert vertex malloc failed");
#ifdef INDEP_SURFACE
} else {
//printf("Not adding (2) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",vx->no,vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp);
}
#endif
}
//else printf("Not adding (3) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",vx->no,vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp);
}
/* Remove a vertex from the seeding groups */
static void
ofps_rem_vseed(ofps *s, vtx *vx) {
#ifdef DEBUG
printf("Removing vertex no %d from sorted binary tree\n",vx->no);
#endif
/* Remove the vertex from the sorted binary tree */
if ((aat_aerase(s->vtreep, (void *)vx)) == 0)
error("aat_aerase vertex failed to find vertex no %d (3)", vx->no);
if (vx->used == 0) {
#ifdef INDEP_SURFACE
/* Only pick full dimensional visible vertexes for seeding group, */
/* since only they have a full-d error value. */
if (sm_andtest(s, &s->sc[0].a_sm, &vx->vm) != 0) {
#endif
//printf("Removing (4) vtx no %d, 0x%x, used %d, eserr %f, vm %s nsp %d\n",vx->no,vx, vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp);
if ((aat_aerase(s->vtrees[vx->nsp], (void *)vx)) == 0)
error("aat_aerase vertex failed to find vertex no %d (4)", vx->no);
#ifdef INDEP_SURFACE
} else {
//printf("Not removing (1) vtx no %d, 0x%x, used %d, eserr %f, vm %s nsp %d\n",vx->no,vx,vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp);
}
#endif
}
// else printf("Not removing (2) vtx no %d, 0x%x, used %d, eserr %f, vm %s nsp %d\n",vx->no,vx,vx->used,vx->eserr,psm(s,&vx->vm),vx->nsp);
}
/* Remove a vertex from the spatial acceleration grid */
static void
ofps_rem_vacc(ofps *s, vtx *vx) {
/* Remove from spatial acceleration grid */
if (vx->pn != NULL) { /* If is on acceleration list, remove it */
*vx->pn = vx->n;
if (vx->n != NULL)
vx->n->pn = vx->pn;
}
vx->pn = NULL;
vx->n = NULL;
}
/* Clear the spatial acceleration grid */
static void
ofps_reset_acc(ofps *s) {
int i;
for (i = 0; i < s->nig; i++) {
acell *cp = &s->_grid[i];
if (cp->gflag != BOUND_GFLAG)
cp->gflag = 0;
cp->head = NULL;
cp->vhead = NULL;
}
s->gflag = 0;
}
/* --------------------------------------------------- */
/* Creat a randomized order list of pointers to the fixed points */
static void
ofps_setup_fixed(
ofps *s,
fxpos *fxlist, /* List of existing fixed points */
int fxno /* Number in fixed list */
) {
int e, di = s->di;
int i, j;
s->fnp = 0;
if (fxno == 0)
return;
/* Allocate a list of pointers sufficient for all the fixed points */
if ((s->ufx = (fxpos **)calloc(sizeof(fxpos *), fxno)) == NULL)
error ("ofps: malloc failed on pointers to fixed list");
/* Add each fixed point to the list */
for (i = 0; i < fxno; i++) {
/* Clip the fixed point */
ofps_clip_point7(s, fxlist[i].p, fxlist[i].p);
/* Comute perceptual attributes */
s->percept(s->od, fxlist[i].v, fxlist[i].p);
/* Skip any duplicate points, or Voronoi will get confused.. */
for (j = 0; j < s->fnp; j++) {
for (e = 0; e < di; e++) {
if (fabs(s->ufx[j]->p[e] - fxlist[i].p[e]) > 1e-5)
break; /* Not a match */
}
if (e >= di)
break; /* Is a match */
}
if (j < s->fnp)
continue; /* Skip adding this point */
s->ufx[s->fnp++] = &fxlist[i];
}
/* Randomly shuffle the fixed points */
for (i = 0; i < s->fnp; i++) {
fxpos *tp;
j = i_rand(0, s->fnp-1);
/* Swap the pointers */
tp = s->ufx[i];
s->ufx[i] = s->ufx[j];
s->ufx[j] = tp;
}
s->tinp -= (fxno - s->fnp);
}
/* Seed the object with any fixed points */
/* (I think this is only used if ofps is used to check the stats */
/* on all the points. ) */
/* Return NZ on failure */
static int
ofps_add_fixed(
ofps *s
) {
int e, di = s->di;
int i, j, ii;
/* Add fixed points if there are any */
if (s->fnp == 0)
return 0;
if (s->verb)
printf("Adding %d unique fixed points\n",s->fnp);
for (i = 0; i < s->fnp; i++) {
node *p = s->n[i]; /* Destination for point */
/* Make sure that fixed point is within our gamut */
ofps_clip_point(s, s->ufx[i]->p, s->ufx[i]->p);
for (e = 0; e < di; e++) { /* copy device and perceptual coords */
p->op[e] = p->p[e] = s->ufx[i]->p[e];
p->v[e] = s->ufx[i]->v[e];
}
/* Count gamut surface planes it lies on */
det_node_gsurf(s, p, p->p);
p->fx = 1; /* is a fixed point */
/* Compute the Voronoi for it, and inc s->np */
if (add_node2voronoi(s, s->np, 1)) {
/* In theory we could try adding points in a different order, */
/* by resetting the voronoi, shuffling all the fixedpoints */
/* and re-adding them again. */
warning("Adding a fixed point failed to hit any vertexes, and no points to swap with!");
return 1;
}
if (s->verb)
printf("%cAdded fixed %d/%d",cr_char,i,s->fnp); fflush(stdout);
#ifdef DUMP_STRUCTURE
printf("Done node %d\n",s->np);
dump_node_vtxs(s, 0);
printf("=========================================================================\n");
#endif
#ifdef DUMP_PLOT_SEED
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 0, -1); /* Device, No wait, verticies */
#endif /* DUMP_PLOT_SEED */
}
return 0;
}
/* Seed the object with any fixed and movable incremental farthest points. */
/* (We are only called if there is at leaset one movable point) */
static void
ofps_seed(ofps *s) {
int e, di = s->di;
int ii, i, j, k, fc;
double rerr;
int needfirst = 0; /* Need a special first seed point (seems better without this ?) */
int dofixed = 0; /* Do a fixed point next */
int abortonfail = 0; /* Abort on failing to add fixed points (isn't always good ?) */
int nsp = 0; /* Number of surface points */
aat_atrav_t *aat_tr;
if (s->verb)
printf("\n");
if ((aat_tr = aat_atnew()) == NULL)
error("aat_atnew returned NULL");
if (s->verb) {
printf("There are %d unique fixed points to add (%d total fixed points)\n",s->fnp, s->fxno);
printf("There are %d far spread points to add\n",s->tinp - s->fnp);
}
if (!needfirst && s->fnp > 1) { /* There are fixed points to add */
dofixed = s->fnp > 2 ? 2 : 1;
}
/* Seed all the points. */
/* (i is the node we're creating, j is the verbose interval count, */
/* fc is the count of the fixed points added, ii is the movable count) */
for (fc = j = i = ii = 0; i < s->tinp; i++, j++) {
node *p = s->n[i]; /* New node */
double spref_mult;
/* Compute current surface preference weighting */
if (s->tinp - s->fnp <= 1) /* Prevent divide by zero */
spref_mult = 0.0;
else
spref_mult = ii/(s->tinp - s->fnp - 1.0);
spref_mult = (1.0 - spref_mult) * s->ssurfpref + spref_mult * s->esurfpref;
if (needfirst) { /* No initial fixed points, so seed the first */
/* point as a special. */
double min[MXPD], max[MXPD];
p->fx = 0;
/* If there are no fixed points in the bulk, make the */
/* first point such a point, to avoid pathology. */
for (e = 0; e < di; e++)
p->p[e] = s->imin[e] + (s->imax[e] - s->imin[e]) * 1.0/4.141592654;
/* Clip the new location */
ofps_clip_point8(s, p->p, p->p);
s->percept(s->od, p->v, p->p);
#ifdef DEBUG
printf("Creating first seed point (moveable %d out of %d)\n",i+1,s->tinp);
#endif
} else if (dofixed) { /* Setup to add a fixed point */
#ifdef DEBUG
printf("Adding fixed point %d out of %d\n",fc+1,s->fnp);
#endif
/* (Fixed points are already clipped and have perceptual value) */
for (e = 0; e < di; e++) { /* copy device and perceptual coords */
p->p[e] = s->ufx[fc]->p[e];
p->v[e] = s->ufx[fc]->v[e];
}
p->fx = 1; /* is a fixed point */
/* Count gamut surface planes it lies on */
if (det_node_gsurf(s, p, p->p) != 0)
nsp++;
} else { /* Setup to add a movable point */
int k;
int sf = 0;
double spref_mult;
double mx;
vtx *vx, *bvx;
double spweight[MXPD+1]; /* Surface preference weight table */
double bspweight; /* Biggest weight */
#ifdef DEBUG
printf("Adding movable point %d out of %d\n",i+1,s->tinp);
#endif
p->fx = 0;
/* Compute current surface preference weighting */
if (s->tinp - s->fnp <= 1) /* Prevent divide by zero */
spref_mult = 0.0;
else
spref_mult = ii/(s->tinp - s->fnp - 1.0);
spref_mult = (1.0 - spref_mult) * s->ssurfpref + spref_mult * s->esurfpref;
/* Until we use the next vertex, keep looking for a movable point */
for (;;) {
#ifdef NEVER /* DEBUG: Show the contents of each list */
printf("All vertex list:\n");
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
printf(" Vtx %d, used %d, eserr %f, weserr %f, vm %s\n",vx->no,vx->used,vx->eserr,vx->eserr * spweight[vx->nsp],psm(s,&vx->vm));
}
for (e = 0; e <= (di+1); e++) {
printf("Sorted tree vertex list for nsp %d :\n",e);
for (vx = aat_atlast(aat_tr, s->vtrees[e]); vx != NULL; vx = aat_atprev(aat_tr)) {
printf(" Vtx %d, used %d, eserr %f, weserr %f, vm %s\n",vx->no,vx->used,vx->eserr,vx->eserr * spweight[vx->nsp],psm(s,&vx->vm));
}
}
#endif
/* Compute the surface weighting multiplier for */
/* each possible nsp + 1 */
spweight[0] = 1.0;
for (e = 1; e <= di; e++)
spweight[e] = spweight[e-1] * spref_mult;
/* Locate the Voronoi vertex with the greatest distance to a sampling points */
for (mx = -1.0, bvx = NULL, e = 0; e <= (di+1); e++) {
double weserr;
/* Get largest eserr vertex for this nsp */
if ((vx = aat_atlast(aat_tr, s->vtrees[e])) == NULL)
continue;
weserr = vx->eserr * spweight[vx->nsp];
//printf("~1 considering vertex no %d, eserr %f, weserr %f\n",vx->no,vx->eserr,weserr);
if (weserr > mx) {
mx = weserr;
bvx = vx;
}
}
//if (bvx != NULL) printf("~1 got vertex no %d, eserr %f, weserr %f\n",bvx->no,bvx->eserr,mx);
#ifdef SANITY_CHECK_SEED
/* Do exaustive search of candidate vertexes */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
double tweserr;
tweserr = vx->eserr * spweight[vx->nsp];
if (vx->used == 0 && tweserr > (mx + 10.0 * NUMTOL)
#ifdef INDEP_SURFACE
/* Only pick full dimensional visible vertexes, */
/* since only they have a full-d error value. */
&& sm_andtest(s, &s->sc[0].a_sm, &vx->vm) != 0
#endif
) {
warning("!!!!!! Sanity: Didn't pick largest eperr vtx no %d %f, picked %d %f instead !!!!!!",vx->no,tweserr,bvx->no,mx);
printf("!!!!!! Sanity: Didn't pick largest eperr vtx no %d %f, picked %d %f instead !!!!!!\n",vx->no,tweserr,bvx->no,mx);
mx = tweserr;
bvx = vx;
}
}
#endif /* SANITY_CHECK_SEED */
if (bvx == NULL) { /* We've failed to find a movable point */
/* This could be because there are fixed points */
/* at all candidate locations. */
if (fc < s->fnp) { /* Use a fixed point */
/* (Fixed points are already clipped and have perceptual value) */
for (e = 0; e < di; e++) { /* copy device and perceptual coords */
p->p[e] = s->ufx[fc]->p[e];
p->v[e] = s->ufx[fc]->v[e];
}
p->fx = 1; /* is a fixed point */
/* Count gamut surface planes it lies on */
if (det_node_gsurf(s, p, p->p) != 0)
nsp++;
break; /* Go and use this point */
}
error("ofps: assert, there are no vertexes to choose in initial seed\n");
}
#ifdef DEBUG
printf("Picking vertex no %d at %s with weserr %f, mask 0x%x\n",bvx->no,ppos(di,bvx->p),mx,bvx->pmask);
#endif
/* Don't pick the vertex again */
bvx->used = 1;
//printf("Removing (4) vtx no %d, used %d, eserr %f, vm %s nsp %d\n",bvx->no,bvx->used,bvx->eserr,psm(s,&bvx->vm),bvx->nsp);
if ((aat_aerase(s->vtrees[bvx->nsp], (void *)bvx)) == 0)
error("aat_aerase vertex failed to find vertex no %d (5)", bvx->no);
/* Add the new node */
for (e = 0; e < di; e++)
p->p[e] = bvx->p[e];
/* Count gamut surface planes it lies on */
if (det_node_gsurf(s, p, p->p) != 0)
nsp++;
#ifdef RANDOM_PERTERB
/* Compute radius of closest real node to vertex */
rerr = 1.0;
for (k = 0; k <= di; k++) {
double rads;
int ix = bvx->nix[k];
node *np;
if (ix < 0)
break; /* Done */
np = s->n[ix];
for (rads = 0.0, e = 0; e < di; e++) {
double tt = bvx->p[e] - np->p[e];
rads += tt * tt;
}
if (rads < rerr)
rerr = rads;
}
rerr = s->lperterb * sqrt(rerr);
if (rerr < 0.001)
rerr = 0.001;
/* Add a random offset to the position, and retry */
/* if it collides with an existing node or future fixed point */
for (k = 0; k < 20; k++) {
int pci; /* Point list index */
acell *cp; /* Acceleration cell */
node *p1;
for (e = 0; e < di; e++)
p->p[e] = bvx->p[e] + d_rand(-rerr, rerr);
/* Confine node to planes vertex was on, */
/* bit not if we're having trouble avoiding collissions */
if (bvx->nsp > 0)
confineto_gsurf(s, p->p, p->sp, p->nsp);
ofps_clip_point9(s, p->p, p->p);
s->percept(s->od, p->v, p->p);
pci = ofps_point2cell(s, p->v, p->p); /* Grid index of cell of interest */
if ((cp = &s->grid[pci]) == NULL)
break; /* Nothing in cell */
for (p1 = cp->head; p1 != NULL; p1 = p1->n) {
for (e = 0; e < di; e++) {
if (fabs(p->v[e] - p1->v[e]) > COINTOL)
break; /* Not cooincident */
}
if (e >= di) { /* Cooincident */
#ifdef DEBUG
printf("Random offset node ix %d at %s collides with ix %d at %s - retry random %d\n",p->ix,ppos(di,p->p),p1->ix,ppos(di,p1->p),k);
#endif
break; /* Retry */
}
}
if (p1 != NULL) /* Coincident */
continue;
/* Check movable point against fixed points that are yet to be added */
/* (~~~ Ideally we should use an accelleration structure to check */
/* for cooincidence rather than doing an exaustive search. ~~~) */
if (fc < s->fnp) { /* There are more fixed points to add */
int f;
for (f = fc; f < s->fnp; f++) {
for (e = 0; e < di; e++) {
if (fabs(p->p[e] - s->ufx[f]->p[e]) > COINTOL)
break; /* Not cooincident */
}
if (e >= di) { /* Cooincident */
#ifdef DEBUG
printf("Movable node ix %d at %s collides with fixed point %d at %s - retry movable %d\n",p->ix,ppos(di,p->p),f,ppos(di,s->ufx[f]->p),k);
#endif
break;
}
}
if (f >= s->fnp)
break; /* movable point is not cooincident */
} else {
break; /* Not cooincident, so OK */
}
}
if (k >= 20) {
/* This can happen if we didn't pick the absolute largest weserr, */
/* and the vertex we ended up with is being confined to the same */
/* location as an existing vertex by the planes is on. */
/* (Why does it have an weperr > 0.0 then ????) */
/* Give up on this point and chose another one. */
continue;
// error("ofps_seed: Assert, was unable to joggle cooincindent point");
}
#else /* !RANDOM_PERTERB */
/* Confine node to planes vertex was on */
if (bvx->nsp > 0)
confineto_gsurf(s, p->p, p->sp, p->nsp);
ofps_clip_point9(s, p->p, p->p);
s->percept(s->od, p->v, p->p);
#endif /* !RANDOM_PERTERB */
/* Added this movable point, so chosen the next point */
break;
} /* keep looking for a movable point */
}
/* We now have a first/fixed/moevable point to add */
/* hack test */
//p->p[0] = d_rand(0.0, 1.0);
//p->p[1] = d_rand(0.0, 1.0);
//ofps_cc_percept(s, p->v, p->p);
/* Establish original position */
for (e = 0; e < di; e++)
p->op[e] = p->p[e];
/* Compute the Voronoi for it, and inc s->np */
/* Fail if we get a position fail */
if (add_node2voronoi(s, i, dofixed && abortonfail)) {
if (dofixed) {
/* Pospone adding this vertex */
if ((s->fnp - fc) >= (s->tinp - i - 1)) { /* No room for moveable points */
// error("Adding fixed point failed to hit any vertexes or posn. failed");
abortonfail = 0;
} else
dofixed = 0;
}
if (needfirst) {
/* Hmm. The first seed point has failed. What should we do ? */
error("Adding first seed point failed to hit any vertexes or posn. failed");
}
/* Skip this point */
--i;
--j;
continue;
}
/* Suceeded in adding the point */
if (p->fx) { /* Fixed point was added */
fc++;
if (dofixed > 0) /* May not have been triggered by dofixed */
dofixed--;
if ((s->fnp - fc) >= (s->tinp - i - 1)) { /* No room for moveable points */
dofixed = s->fnp - fc; /* Do all the fixed */
}
} else { /* Movable point */
ii++;
if (fc < s->fnp) { /* There are more fixed points to add */
dofixed = s->fnp - fc;
/* Add fixed 2 at a time to try and minimize the disruption */
/* of the movable point edge priority */
if (dofixed > 2)
dofixed = 2;
}
}
if (s->verb && (j == 11 || i == (s->tinp-1))) {
printf("%cAdded %d/%d",cr_char,s->np,s->tinp); fflush(stdout);
j = 0;
}
#ifdef DUMP_STRUCTURE
printf("Done node %d\n",i);
dump_node_vtxs(s, 0);
printf("=========================================================================\n");
#endif
#ifdef DUMP_PLOT_SEED
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 0, -1); /* Device, No wait, verticies */
#endif /* DUMP_PLOT_SEED */
needfirst = 0; /* Must have done first */
}
//printf("Number of gamut surface points = %d\n",nsp);
aat_atdelete(aat_tr);
if (s->verb)
printf("\n");
}
/* Recreate the Voronoi diagram with the current point positions */
static void
ofps_redo_voronoi(
ofps *s
) {
vtx *vx, *nvx;
int i, j, k, e, di = s->di;
/* Retry if we get a failure to add a point */
for (k = 0; k < NINSERTTRIES; k++) {
/* (~9 should think about smoothing the pre-conditioning lookup */
/* if the number of tries is high. Add this to rspl.) */
/* Clear the voronoi nodes */
node_clear(s, s->n[-2 * di - 2]);
for (j = -s->gnp; j < s->np; j++)
node_clear(s, s->n[j]);
/* Delete the voronoi verticies */
for (vx = s->uvtx; vx != NULL; vx = nvx) {
nvx = vx->link;
del_vtx(s, vx);
}
s->uvtx = NULL;
/* Clear out the spatial acceleration grid */
ofps_reset_acc(s);
if (s->nv != 0)
warning("ofps: Assert, clear didn't leave us with 0 vertexes");
if (s->umid != NULL)
warning("ofps: Assert, clear didn't empty used midpoint list");
if (aat_asize(s->vtreep) != 0)
warning("ofps: Assert, clear didn't empty vertex tree");
for (e = 0; e <= (di+1); e++) {
if (aat_asize(s->vtrees[e]) != 0)
warning("ofps: Assert, clear didn't empty vertex tree");
}
/* Set number of points in voronoi to zero */
s->np = 0;
/* Initialse the empty veronoi etc. */
ofps_binit(s);
s->posfailstp = 0;
/* Add all points in again. */
for (i = 0 ;i < s->tinp; i++) { /* Same order as before */
/* Compute the Voronoi for it (will add it to spatial accelleration grid) */
/* and increment s->np */
if (add_node2voronoi(s, i, 0)) {
/* Hmm. Shuffle and retry the whole thing. */
shuffle_node_order(s);
break;
}
/* If it's not going well, re-shuffle and abort too */
if (i > 10 && s->posfailstp/(1.0+i) > 0.2) {
//printf("~1 after node %d, posfailes = %d, prop %f\n",i,s->posfailstp, s->posfailstp/(1.0+i));
/* Hmm. Shuffle and and retry the whole thing. */
if (s->verb > 1)
warning("Too many nodes are failing to be inserted - reshuffling and re-starting\n");
shuffle_node_order(s);
break;
}
#ifdef DUMP_STRUCTURE
printf("Done node %d\n",i);
dump_node_vtxs(s, 0);
// ofps_re_create_node_node_vtx_lists(s);
// if ((s->optit+1) >= 4)
// { char buf[200]; sprintf(buf, "Itteration %d node ix %d",s->optit+1,s->np-1); dump_node_vtxs2(s, buf); }
printf("=========================================================================\n");
#endif
#ifdef DUMP_PLOT_RESEED
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 0, -1); /* Device, No wait, verticies */
#endif /* DUMP_PLOT_RESEED */
}
if (i >= s->tinp) {
#ifdef DEBUG
if (k > 1) printf("Took %d retries\n",k-1);
#endif /* DEBUG */
break;
}
/* Retry the whole thing */
}
if (k >= NINSERTTRIES)
error("Failed to re-seed the veronoi after %d tries - too many node insertion failures ?",NINSERTTRIES);
}
/* ----------------------------------------------------------- */
/* Ideas for improving the accelleration:
When there is no SUBD, then it is possible that the node
neighbourhood net (if it is kept up to date during seeding)
could be used to locate the closest node and then vertex.
(It can't be used for fixup, because the voronoi properties
aren't true during fixup.)
Starting at at the first node found using the spiral structure,
check all it's neigbours and if it's neighbour is closer to
the target, switch to it. If no neighbour is closer,
then that is the closest node.
The closest vertex is then connected to the closest node ?
*/
#undef DEBUG_FCLOSE
/* Given a node, locate all vertexes that it hits. */
/* s->flag is assumed to be relevant for the given node. */
/* Any hit vertexes are added to the s->nxh list. */
/* s->vvchecks and s->nvcheckhits will be updated. */
/* Return nz if vertexs were hit */
/* (This only returns visible vertexes.) */
static int ofps_findhit_vtxs(ofps *s, node *nn) {
int e, di = s->di;
int i, j;
int pci; /* Point cell index */
acell *cp;
vtx *vx;
double beperr, eperr;
acell *slist = NULL, *sliste = NULL; /* Next to search list */
int hit = 0;
if (nn->ix < 0)
error("ofps_findhit_vtxs given gamut boundary node ix %d",nn->ix);
#ifdef DEBUG
if (s->agrid_init == 0)
error("ofps_findhit_vtxs() called before agrid_init");
#endif
#ifdef DEBUG_FCLOSE
printf("\nLocating a hit vtx to node at p = %s, v = %s\n", ppos(di,nn->p), ppos(di,nn->v));
#endif
/* Determine the largest eperr of any vertex */
{
aat_atrav_t *aat_tr;
beperr = 1e300;
if ((aat_tr = aat_atnew()) == NULL)
error("aat_atnew returned NULL");
/* Find the largest vertex eperr visible to the node */
for (vx = aat_atlast(aat_tr, s->vtreep); vx != NULL; vx = aat_atprev(aat_tr)) {
#ifdef INDEP_SURFACE
if (sm_vtx_node(s, vx, nn) == 0)
continue;
#endif /* INDEP_SURFACE */
beperr = vx->eperr;
break;
}
aat_atdelete(aat_tr);
#ifdef DEBUG_FCLOSE
printf("Largest eperr of any vertex = %f\n", beperr);
// fprintf(stderr,"Largest eperr of any vertex = %f\n", beperr);
#endif
}
s->nvfschd += s->nv; /* Number of vertexes in a full search */
s->naccsrch++; /* Number of searches */
/* Do a breadth first seed search for any hit vertexes, or until */
/* we run out of cells that could possibly be hits. */
/* Locate a starting cell using the grid */
pci = ofps_point2cell(s, nn->v, nn->p); /* Grid index of cell of interest */
cp = &s->grid[pci];
s->gflag++; /* cell touched flag */
/* Put the starting cell on the search list */
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",cp - s->grid, pco(di,cp->co));
#endif
#ifdef NEVER
if (sliste == NULL) { /* First in empty list */
slist = cp;
} else {
sliste->slist = cp; /* Add to end of list */
}
sliste = cp;
cp->slist = NULL;
#else
cp->slist = slist;
slist = cp;
#endif
cp->gflag = s->gflag; /* Cell is on list to be searched */
/* until we run out of cells to search */
for (;slist != NULL;) {
acell *ncp;
/* For each cell in the search list, check it and recursion. */
for (cp = slist, slist = sliste = NULL; cp != NULL; cp = ncp) {
double ceperr;
ncp = cp->slist;
#ifdef DEBUG_FCLOSE
printf("Checking cell ix %d co %s\n",cp - s->grid,pco(di,cp->co));
#endif
/* Compute the smallest eperr possible in this cell, by computing the */
/* eperr of the cell center to the node minus the estimated */
/* largest eperr of any point within the cell to the center. */
ceperr = ofps_comp_eperr(s, NULL, cp->v, cp->p, nn->v, nn->p, nn->nsp);
eperr = ceperr - cp->eperr;
//printf("~1 ceperr %f, cp->eperr %f, eperr %f, beperr %f\n",ceperr,cp->eperr,eperr,beperr);
/* If smallest possible eperr is larger than largest vertexe eperr */
if (eperr > beperr) {
//printf("~1 skipping cell\n");
#ifdef SANITY_CHECK_CLOSEST
/* Check all nodees in the cell anyway */
for (vx = cp->vhead; vx != NULL; vx = vx->n) {
int par = 0;
if (vx->cflag == s->flag)
continue;
#ifdef INDEP_SURFACE
if (sm_vtx_node(s, vx, nn) == 0)
continue;
#endif /* INDEP_SURFACE */
if (nn->ixm & vx->nix[MXPD+2]) { /* Is in nixm */
for (e = 0; e <= di; e++) { /* Do exact check */
if (nn->ix == vx->nix[e])
break;
}
if (e <= di)
par = 1;
}
eperr = ofps_comp_eperr7(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
if (!par && (vx->eperr - eperr) > 0.0) {
//printf("~1 Node ix %d at %s (%s)\n Cell ix %d co %s center %s (%s),\n vtx no %d at %s (%s)\n",nn->ix, ppos(di,nn->p),ppos(di,nn->v),cp - s->grid,pco(s->di,cp->co),ppos(di,cp->cp),ppos(di,cp->cv),vx->no, ppos(di,vx->p),ppos(di,vx->v));
warning("Sanity check ofps_findhit_vtxs() cell skip failed, hit on vtx no %d, eperr %f < vx->eperr %f, cell ix %d eperr %f, est min eperr %f",vx->no,eperr,vx->eperr,cp - s->grid,ceperr,ceperr - cp->eperr);
printf("Sanity check ofps_findhit_vtxs() cell skip failed, hit on vtx no %d, eperr %f < vx->eperr %f, cell ix %d eperr %f, est min eperr %f\n",vx->no,eperr,vx->eperr,cp - s->grid,ceperr,ceperr - cp->eperr);
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("findclosest node cell skip failed");
#endif
}
}
#endif /* SANITY_CHECK_CLOSEST */
continue; /* Cell is not worth searching */
}
/* Search the cell */
s->ncellssch++;
/* For vertexes in this cell */
for (vx = cp->vhead; vx != NULL; vx = vx->n) {
#ifdef DEBUG
printf("Checking vtx no %d\n",vx->no);
#endif
/* If the vertex has already been checked */
if (vx->cflag == s->flag)
continue;
if (vx->ofake) /* ofake vertexes can't be hit */
continue;
#ifdef INDEP_SURFACE
/* Only check for hit if the vertex is visible to the node */
if (sm_vtx_node(s, vx, nn) == 0) {
# ifdef DEBUG
printf("Vertex no %d xmask 0x%x vm %s isn't visible to ix %d pmask 0x%x a_sm %s\n",vx->no,vx->cmask,psm(s,&vx->vm),nn->ix,nn->pmask,psm(s,&s->sc[nn->pmask].a_sm));
# endif /* DEBUG */
continue;
}
#endif /* INDEP_SURFACE */
vx->add = 0;
vx->del = 0;
vx->par = 0;
s->vvchecks++; /* Checking a vertex */
/* Check if node is already parent to this vertex. */
/* This only happens during fixups if the reposition fails and we */
/* retain the vertex with the deleted vertex location (not currently */
/* done), or by slim numerical margine, so ignore such hits. */
/* We treat a parent as a hit node for the purposes of recursion, */
/* and add it to a special list used to complete the vertex net. */
if (nn->ixm & vx->nix[MXPD+2]) { /* Is in nixm */
for (e = 0; e <= di; e++) { /* Do exact check */
if (nn->ix == vx->nix[e])
break;
}
if (e <= di) {
#ifdef DEBUG
printf("Vertex no %d has already got node ix %d\n",vx->no,nn->ix);
#endif
vx->par = 1;
}
}
/* nba_eperr is assumed to be valid if vx->cflag == s->flag */
vx->nba_eperr = ofps_comp_eperr7(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
#ifdef DEBUG
printf("Computing nba_eperr of %f for vtx no %d\n",vx->nba_eperr, vx->no);
#endif
/* See if the vertex eperr will be improved */
if (!vx->par && (vx->eperr - vx->nba_eperr) > 0.0) {
s->nvcheckhits++;
hit = 1;
vx->del = 1; /* Mark for deletion */
vx->nxh = s->nxh; /* Add vertex to list */
s->nxh = vx;
vx->hflag = s->flag;
#ifdef DEBUG
printf("Vertex error improvement hit by %f (%f < %f)\n",vx->eperr-vx->nba_eperr,vx->nba_eperr,vx->eperr);
if (vx->par) {
printf("Vertex no %d hit by its own parent ix %d\n",vx->no, nn->ix);
warning("Vertex no %d hit by its own parent ix %d",vx->no, nn->ix);
}
#endif
}
#ifdef DEBUG
else { /* If worse */
printf("Vertex error not hit by %f (%f < %f)\n",vx->eperr-vx->nba_eperr,vx->nba_eperr,vx->eperr);
}
#endif
vx->cflag = s->flag;
} /* Next vertex in cell */
/* Put all this cells neighbours on the search list */
/* (This is probably the critical inner loop. If ->acnl was */
/* scaled by sizeof(acell), then the implicit multiply could */
/* be avoided) */
for (j = 0; j < s->nacnl; j++) {
acell *nc = cp + s->acnl[j];
if (nc->gflag >= s->gflag)
continue;
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",nc - s->grid, pco(di,nc->co));
#endif
#ifdef NEVER
if (sliste == NULL) { /* First in empty list */
slist = nc;
} else {
sliste->slist = nc; /* Add to end of list */
}
sliste = nc;
nc->slist = NULL;
#else
nc->slist = slist;
slist = nc;
#endif
nc->gflag = s->gflag; /* Cell is on list to be searched */
}
} /* Next cell in current list */
//printf("~1 don that search list\n");
} /* Next list */
//printf("~1 no more search lists\n");
return hit;
}
#ifdef DEBUG_FCLOSE
#undef DEBUG_FCLOSE
#endif
#undef DEBUG_FCLOSE
/* Given a vertex, locate the smallest eperr node. */
/* Return NULL if none, and if ceperr is not NULL, set it to the */
/* eperr to the returned node. */
/* (This only returns visible nodes.) */
static node *ofps_findclosest_node(ofps *s, double *ceperr, vtx *vx) {
int e, di = s->di;
int i, j;
int pci; /* Point cell index */
acell *cp;
double eperr, beperr = 1e300; /* eperr of closest node */
node *bno = NULL; /* Closest node */
acell *slist = NULL, *sliste = NULL; /* Next to search list */
#ifdef DEBUG
if (s->agrid_init == 0)
error("ofps_findclosest_node() called befor agrid_init");
#endif
#ifdef DEBUG_FCLOSE
printf("\nLocating closest node to vtx at p = %s, v = %s\n", ppos(di,vx->p), ppos(di,vx->v));
#endif
s->nnfschd += s->np; /* Number of nodes in a full search */
s->naccsrch++; /* Number of searches */
/* Do a breadth first seed search for any better nodees, or until */
/* we run out of cells that could improve on the current best. */
/* Locate a starting cell using the grid */
pci = ofps_point2cell(s, vx->v, vx->p); /* Grid index of cell of interest */
cp = &s->grid[pci];
s->gflag++; /* cell touched flag */
/* Put the starting cell on the search list */
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",cp - s->grid, pco(di,cp->co));
#endif
#ifdef NEVER
if (sliste == NULL) { /* First in empty list */
slist = cp;
} else {
sliste->slist = cp; /* Add to end of list */
}
sliste = cp;
cp->slist = NULL;
#else
cp->slist = slist; /* Add it to start of list */
slist = cp;
#endif
cp->gflag = s->gflag; /* Cell is on list to be searched */
/* until we run out of cells to search */
for (;slist != NULL;) {
acell *ncp;
/* For each cell in the search list, check it and recursion. */
for (cp = slist, slist = sliste = NULL; cp != NULL; cp = ncp) {
double ceperr;
ncp = cp->slist;
#ifdef DEBUG_FCLOSE
printf("Checking cell ix %d co %s\n",cp - s->grid,pco(di,cp->co));
#endif
/* Compute the eperr of the cell center to the vtx minus the estimated */
/* largest eperr of any point within the cell to the center. */
ceperr = ofps_comp_eperr(s, NULL, cp->v, cp->p, vx->v, vx->p, vx->nsp);
eperr = ceperr - cp->eperr;
/* If the cell is worth searching */
if (eperr < beperr) {
node *no;
/* Search the cell */
s->ncellssch++;
for (no = cp->head; no != NULL; no = no->n) {
#ifdef INDEP_SURFACE
/* Check if this node is visible to this vtx */
if (sm_vtx_node(s, vx, no) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the node to the new vtx */
eperr = ofps_comp_eperr(s, NULL, no->v, no->p, vx->v, vx->p, vx->nsp);
if (eperr < beperr) {
bno = no;
beperr = eperr;
#ifdef DEBUG_FCLOSE
printf("Improved to node ix %d eperr\n",bno->ix,beperr);
#endif
}
}
/* Put all this cells neighbours on the search list */
/* (This is probably the critical ivxer loop. If ->acnl was */
/* scaled by sizeof(acell), then the implicit multiply could */
/* be avoided) */
for (j = 0; j < s->nacnl; j++) {
acell *nc = cp + s->acnl[j];
if (nc->gflag >= s->gflag)
continue;
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",nc - s->grid, pco(di,nc->co));
#endif
#ifdef NEVER
if (sliste == NULL) { /* First in empty list */
slist = nc;
} else {
sliste->slist = nc; /* Add to end of list */
}
sliste = nc;
nc->slist = NULL;
#else
nc->slist = slist; /* Add it to start of list */
slist = nc;
#endif
nc->gflag = s->gflag; /* Cell is on list to be searched */
}
}
#ifdef SANITY_CHECK_CLOSEST
/* Check all nodees in the cell anyway */
else {
double teperr;
node *no;
for (no = cp->head; no != NULL; no = no->n) {
#ifdef INDEP_SURFACE
/* Check if this node is visible to this vtx */
if (sm_vtx_node(s, vx, no) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the node to the new vtx */
teperr = ofps_comp_eperr(s, NULL, no->v, no->p, vx->v, vx->p, vx->nsp);
if (teperr < beperr) {
warning("Sanity check ofps_findclosest_node() cell skip failed, estimated %f from cellc eperr %f - cell eperr %f, found %f from node ix %d",eperr,ceperr,cp->eperr,teperr,no->ix);
printf("Sanity check ofps_findclosest_node() cell skip failed, estimated %f from cellc eperr %f - cell eperr %f, found %f from node ix %d\n",eperr,ceperr,cp->eperr,teperr,no->ix);
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("findclosest node cell skip failed");
#endif
}
}
}
#endif /* SANITY_CHECK_CLOSEST */
} /* Next cell in current list */
#ifdef DEBUG_FCLOSE
printf("Finished ivxer loop because p 0x%x = NULL\n",cp);
#endif
} /* Next list */
#ifdef DEBUG_FCLOSE
printf("Finished outer loop because slist 0x%x = NULL\n",slist);
#endif
#ifdef DEBUG_FCLOSE
if (bno == NULL)
printf("Failed to find a closest node");
else
printf("Returning best node ix %d, eperr %f\n",bno->ix,beperr);
#endif
#ifdef SANITY_CHECK_CLOSEST
/* Use exaustive search */
{
double ch_beperr = 1e300; /* Device distance squared of closest vertex */
node *ch_bno = NULL;
for (i = 0; i < (s->np-1); i++) {
node *nn = s->n[i];
double eperr;
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the node and the vertex */
eperr = ofps_comp_eperr(s, NULL, nn->v, nn->p, vx->v, vx->p, vx->nsp);
if (eperr < ch_beperr) {
ch_bno = nn;
ch_beperr = eperr;
}
}
if (ch_bno != NULL && ch_beperr + 1e-3 < beperr) {
if (bno == NULL) {
warning("Sanity check ofps_findclosest_node() failed,\n found none, should be ix %d dist %f",ch_bno->ix,ch_beperr);
printf("Sanity check ofps_findclosest_node() failed,\n found none, should be ix %d dist %f\n",ch_bno->ix,ch_beperr);
} else {
warning("Sanity check ofps_findclosest_node() failed,\n found ix %d dist %f, should be ix %d dist %f",bno->ix,beperr,ch_bno->ix,ch_beperr);
printf("Sanity check ofps_findclosest_node() failed,\n found ix %d dist %f, should be ix %d dist %f\n",bno->ix,beperr,ch_bno->ix,ch_beperr);
}
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("findclosest node failed");
#endif
}
}
#endif
if (bno != NULL && ceperr != NULL)
*ceperr = beperr;
return bno;
}
/* ----------------------------------------------------------- */
#ifdef NEVER /* No longer used */
/* Given a node, locate the smallest eperr vertex. */
/* Return NULL if none, and if ceperr is not NULL, set it to the */
/* eperr to the returned vertex. */
/* (This only returns visible vertexes.) */
static vtx *ofps_findclosest_vtx(ofps *s, double *ceperr, node *nn) {
int e, di = s->di;
int i, j;
int pci; /* Point cell index */
acell *cp;
double eperr, beperr = 1e300; /* eperr of closest vertex */
vtx *bvx = NULL; /* Closest vertex */
acell *slist = NULL, *sliste = NULL; /* Next to search list */
#ifdef DEBUG
if (s->agrid_init == 0)
error("ofps_findclosest_vtx() called befor agrid_init");
#endif
#ifdef DEBUG_FCLOSE
printf("\nLocating closest vtx to node at p = %s, v = %s\n", ppos(di,nn->p), ppos(di,nn->v));
#endif
s->nvfschd += s->nv; /* Number of vertexes in a full search */
s->naccsrch++; /* Number of searches */
/* Do a breadth first seed search for any better vertexes, or until */
/* we run out of cells that could improve on the current best. */
/* Locate a starting cell using the grid */
pci = ofps_point2cell(s, nn->v, nn->p); /* Grid index of cell of interest */
cp = &s->grid[pci];
s->gflag++; /* cell touched flag */
/* Put the starting cell on the search list */
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",cp - s->grid, pco(di,cp->co));
#endif
if (sliste == NULL) { /* First in empty list */
slist = cp;
} else {
sliste->slist = cp; /* Add to end of list */
}
sliste = cp;
cp->slist = NULL;
cp->gflag = s->gflag; /* Cell is on list to be searched */
/* until we run out of cells to search */
for (;slist != NULL;) {
acell *ncp;
/* For each cell in the search list, check it and recursion. */
for (cp = slist, slist = sliste = NULL; cp != NULL; cp = ncp) {
double ceperr;
ncp = cp->slist;
#ifdef DEBUG_FCLOSE
printf("Checking cell ix %d co %s\n",cp - s->grid,pco(di,cp->co));
#endif
/* Compute the eperr of the cell center to the node minus the estimated */
/* largest eperr of any point within the cell to the center. */
ceperr = ofps_comp_eperr(s, NULL, cp->v, cp->p, nn->v, nn->p, nn->nsp);
eperr = ceperr - cp->eperr;
/* If the cell is worth searching */
if (eperr < beperr) {
vtx *vx;
/* Search the cell */
s->ncellssch++;
for (vx = cp->vhead; vx != NULL; vx = vx->n) {
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the vertex to the new node */
eperr = ofps_comp_eperr(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
if (eperr < beperr) {
bvx = vx;
beperr = eperr;
#ifdef DEBUG_FCLOSE
printf("Improved to vtx no %d eperr\n",bvx->no,beperr);
#endif
}
}
/* Put all this cells neighbours on the search list */
/* (This is probably the critical inner loop. If ->acnl was */
/* scaled by sizeof(acell), then the implicit multiply could */
/* be avoided) */
for (j = 0; j < s->nacnl; j++) {
acell *nc = cp + s->acnl[j];
if (nc->gflag >= s->gflag)
continue;
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",nc - s->grid, pco(di,nc->co));
#endif
if (sliste == NULL) { /* First in empty list */
slist = nc;
} else {
sliste->slist = nc; /* Add to end of list */
}
sliste = nc;
nc->slist = NULL;
nc->gflag = s->gflag; /* Cell is on list to be searched */
}
}
#ifdef SANITY_CHECK_CLOSEST
/* Check all vertexes in the cell anyway */
else {
double teperr;
vtx *vx;
for (vx = cp->vhead; vx != NULL; vx = vx->n) {
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the vertex to the new node */
teperr = ofps_comp_eperr(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
if (teperr < beperr) {
warning("Sanity check ofps_findclosest_vtx() cell skip failed, estimated %f from cellc eperr %f - cell eperr %f, found %f from vtx no %d",eperr,ceperr,cp->eperr,teperr,vx->no);
printf("Sanity check ofps_findclosest_vtx() cell skip failed, estimated %f from cellc eperr %f - cell eperr %f, found %f from vtx no %d\n",eperr,ceperr,cp->eperr,teperr,vx->no);
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("findclosest vertex cell skip failed");
#endif
}
}
}
#endif /* SANITY_CHECK_CLOSEST */
} /* Next cell in current list */
#ifdef DEBUG_FCLOSE
printf("Finished inner loop because p 0x%x = NULL\n",cp);
#endif
} /* Next list */
#ifdef DEBUG_FCLOSE
printf("Finished outer loop because slist 0x%x = NULL\n",slist);
#endif
#ifdef DEBUG_FCLOSE
if (bvx == NULL)
printf("Failed to find a closest vertex");
else
printf("Returning best vtx no %d, eperr %f\n",bvx->no,beperr);
#endif
#ifdef SANITY_CHECK_CLOSEST
/* Use exaustive search */
{
double ch_beperr = 1e300; /* Device distance squared of closest vertex */
vtx *vx, *ch_bvx = NULL;
for (vx = s->uvtx; vx != NULL; vx = vx->link) { /* Check all vertexes */
double eperr;
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the vertex to the new node */
eperr = ofps_comp_eperr(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
if (eperr < ch_beperr) {
ch_bvx = vx;
ch_beperr = eperr;
}
}
if (ch_bvx != NULL && ch_beperr + 1e-3 < beperr) {
if (bvx == NULL) {
warning("Sanity check ofps_findclosest_vtx() failed,\n found none, should be no %d dist %f",ch_bvx->no,ch_beperr);
printf("Sanity check ofps_findclosest_vtx() failed,\n found none, should be no %d dist %f\n",ch_bvx->no,ch_beperr);
} else {
warning("Sanity check ofps_findclosest_vtx() failed,\n found no %d dist %f, should be no %d dist %f",bvx->no,beperr,ch_bvx->no,ch_beperr);
printf("Sanity check ofps_findclosest_vtx() failed,\n found no %d dist %f, should be no %d dist %f\n",bvx->no,beperr,ch_bvx->no,ch_beperr);
}
#ifdef SANITY_CHECK_CLOSEST_FATAL
error("findclosest vertex failed");
#endif
}
}
#endif
if (bvx != NULL && ceperr != NULL)
*ceperr = beperr;
return bvx;
}
/* Given a node, locate a vertex that it hits. */
/* Return NULL if none, and if ceperr is not NULL, set it to the */
/* eperr to the returned vertex. */
/* (This only returns visible vertexes.) */
static vtx *ofps_findhit_vtx(ofps *s, double *ceperr, node *nn) {
int e, di = s->di;
int i, j;
int pci; /* Point cell index */
acell *cp;
double eperr, beperr = 1e300; /* eperr of closest vertex */
vtx *bvx = NULL; /* Closest vertex */
acell *slist = NULL, *sliste = NULL; /* Next to search list */
#ifdef DEBUG
if (s->agrid_init == 0)
error("ofps_findhit_vtx() called befor agrid_init");
#endif
#ifdef DEBUG_FCLOSE
printf("\nLocating a hit vtx to node at p = %s, v = %s\n", ppos(di,nn->p), ppos(di,nn->v));
#endif
s->nvfschd += s->nv; /* Number of vertexes in a full search */
s->naccsrch++; /* Number of searches */
/* Do a breadth first seed search for any hit vertexes, or until */
/* we run out of cells that could improve on the current best. */
/* Locate a starting cell using the grid */
pci = ofps_point2cell(s, nn->v, nn->p); /* Grid index of cell of interest */
cp = &s->grid[pci];
s->gflag++; /* cell touched flag */
/* Put the starting cell on the search list */
for (j = 0; j < s->nacnl; j++) {
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",cp - s->grid, pco(di,cp->co));
#endif
if (sliste == NULL) { /* First in empty list */
slist = cp;
} else {
sliste->slist = cp; /* Add to end of list */
}
sliste = cp;
cp->slist = NULL;
cp->gflag = s->gflag; /* Cell is on list to be searched */
}
/* until we run out of cells to search */
for (;slist != NULL;) {
acell *ncp;
/* For each cell in the search list, check it and recursion. */
for (cp = slist, slist = sliste = NULL; cp != NULL; cp = ncp) {
ncp = cp->slist;
#ifdef DEBUG_FCLOSE
printf("Checking cell ix %d co %s\n",cp - s->grid,pco(di,cp->co));
#endif
/* If the cell is worth searching */
if (1) {
vtx *vx;
/* Search the cell */
s->ncellssch++;
for (vx = cp->vhead; vx != NULL; vx = vx->n) {
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
/* Compute the eperr between the vertex to the new node */
eperr = ofps_comp_eperr(s, NULL, vx->v, vx->p, nn->v, nn->p, nn->nsp);
if (eperr < vx->eperr) {
bvx = vx;
beperr = eperr;
#ifdef DEBUG_FCLOSE
printf("Found hit vtx no %d eperr\n",bvx->no,beperr);
#endif
break;
}
}
if (vx != NULL)
break;
/* Put all this cells neighbours on the search list */
/* (This is probably the critical inner loop. If ->acnl was */
/* scaled by sizeof(acell), then the implicit multiply could */
/* be avoided) */
for (j = 0; j < s->nacnl; j++) {
acell *nc = cp + s->acnl[j];
if (nc->gflag >= s->gflag)
continue;
#ifdef DEBUG_FCLOSE
printf("Adding cell ix %d co %s to slist\n",nc - s->grid, pco(di,nc->co));
#endif
if (sliste == NULL) { /* First in empty list */
slist = nc;
} else {
sliste->slist = nc; /* Add to end of list */
}
sliste = nc;
nc->slist = NULL;
nc->gflag = s->gflag; /* Cell is on list to be searched */
}
}
} /* Next cell in current list */
} /* Next list */
#ifdef DEBUG_FCLOSE
if (bvx == NULL)
printf("Failed to find a hit vertex");
else
printf("Returning hit vtx no %d, eperr %f\n",bvx->no,beperr);
#endif
if (bvx != NULL && ceperr != NULL)
*ceperr = beperr;
return bvx;
}
#ifdef DEBUG_FCLOSE
#undef DEBUG_FCLOSE
#endif
#endif /* NEVER */
/* ----------------------------------------------------------- */
/* Re-position the vertexes given the current point positions, */
/* and fixup the veronoi. */
static void
ofps_repos_and_fix_voronoi(
ofps *s
) {
int e, di = s->di;
int i, j, k;
node *nds[MXPD+1]; /* Real nodes of vertex */
int ii; /* Number of real nodes */
double ee[MXPD+1]; /* Per node estimated error */
vtx *vx;
node *nn, *pp;
int nfuxups, l_nfuxups; /* Count of fixups */
int csllow; /* Count since last low */
int mxcsllow = 5; /* Threshold to give up */
#ifdef DEBUG
printf("Repositioning vertexes\n");
#endif
/* Re-position the vertexes to match optimized node positions */
s->fchl = NULL;
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
nodecomb nc;
if (vx->ifake || vx->ofake)
continue;
vx->p_eperr = vx->eperr;
/* Pointers to real nodes. */
for (ii = e = 0; e <= di; e++) {
if (vx->nix[e] >= 0)
nds[ii++] = s->n[vx->nix[e]];
if (vx->nix[e] < -s->nbp)
error("ofps_repos_and_fix_voronoi() got fake node no %d comb %s fake %d",vx->no,pcomb(di,vx->nix),vx->ofake);
}
/* Compute the current eperr at the vertex given the repositioned nodes, */
/* to set acceptance threshold for repositioned vertex. */
ofps_pn_eperr(s, NULL, ee, vx->v, vx->p, nds, ii);
nc.ceperr = ofps_eperr2(ee, ii);
/* Setup to re-position the vertex */
memset((void *)&nc, 0, sizeof(nodecomb));
for (e = 0; e < di; e++) {
nc.nix[e] = vx->nix[e];
nc.p[e] = vx->p[e];
nc.v[e] = vx->v[e];
}
nc.nix[e] = vx->nix[e];
#ifdef DEBUG
printf("Repositioning vertex no %d nodes %s at %s, ceperr %f\n",vx->no,pcomb(di,vx->nix),ppos(di,vx->p),nc.ceperr);
#endif
/* We're about to change the position and eperr: */
ofps_rem_vacc(s, vx);
ofps_rem_vseed(s, vx);
if (position_vtx(s, &nc, 1, 1, 0) == 2) {
/* Just leave it where it was. Perhaps fixups will delete it */
if (s->verb > 1)
warning("re_position_vtx failed for vtx no %d at %s",vx->no,ppos(di,vx->p));
} else {
//printf("~1 moved from %s to %s\n",ppos(di,vx->p),ppos(di,nc.p));
for (e = 0; e < di; e++) {
vx->p[e] = nc.p[e];
vx->v[e] = nc.v[e];
}
vx->eperr = nc.eperr;
vx->eserr = nc.eserr;
}
/* Count the number of gamut surfaces the vertex falls on */
det_vtx_gsurf(s, vx);
/* We've changed the position and eperr: */
ofps_add_vacc(s, vx);
ofps_add_vseed(s, vx);
/* Add all vertexes to the "to be checked" list */
vx->fchl = s->fchl; /* Add vertex to the "to be checked" list */
if (s->fchl != NULL)
s->fchl->pfchl = &vx->fchl;
s->fchl = vx;
vx->pfchl = &s->fchl;
vx->fflag = s->fflag;
vx->fupcount = 0;
vx->fuptol = NUMTOL;
}
#ifdef DUMP_PLOT_BEFORFIXUP
printf("Before applying fixups:\n");
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 0, -1); /* Device, No wait, verticies */
#endif /* DUMP_PLOT_BEFORFIXUP */
/* Now fixup the veroni. */
#ifdef DEBUG
printf("Doing fixups:\n");
#endif
/* We loop until the check list is empty */
l_nfuxups = 1e9;
csllow = 0;
while (s->fchl != NULL && csllow < mxcsllow) {
vtx *nvx;
s->fflag++; /* Fixup round flag */
s->nsvtxs = 0;
nfuxups = 0;
#ifdef DEBUG
printf("\nFixup round %d%s\n",s->fflag, s->fchl == NULL ? "" : " fchl != NULL");
#endif
/* out of gamut, or whether the closest node to it */
/* is not one of its parent nodes. */
for (vx = s->fchl; vx != NULL; vx = nvx) {
double ceperr; /* eperr to closest node */
int hit = 0;
/* For each vertex on the check list, check if it is */
nvx = vx->fchl;
#ifdef DEBUG
printf("Checking vtx no %d, fuptol %e\n",vx->no,vx->fuptol);
#endif
vx->hnode = NULL;
nn = NULL;
/* Check if the vertex position is clipped, */
/* and add fake boundary node if it is */
/* For all the gamut boundary planes: */
for (i = 0; i < s->nbp; i++) {
pleq *vp = &s->gpeqs[i];
double v;
nn = s->n[-1-i];
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, nn) == 0) {
continue;
}
#endif /* INDEP_SURFACE */
for (v = vp->pe[di], e = 0; e < di; e++)
v += vp->pe[e] * vx->p[e];
if (v > vx->fuptol) {
#ifdef NEVER
/* This is an optimization: */
/* Check whether nn is already a parent of the node */
for (e = 0; e <= di; e++) {
if (nn->ix == vx->nix[e])
break;
}
if (e <= di) {
continue; /* It is */
}
#endif
/* Add all the vertexes parent nodes to the nearest nodes "add" list */
#ifdef DEBUG
printf("Vertex no %d hit by boundary node ix %d by %e\n",vx->no,nn->ix,v);
#endif
hit = 1;
if (vx->hnode == NULL) {
vx->hnode = nn;
vx->hitmarg = 50.0 * v;
if (s->nsvtxs >= s->_nsvtxs) {
s->_nsvtxs = 2 * s->_nsvtxs + 5;
if ((s->svtxs = (vtx **)realloc(s->svtxs, sizeof(vtx *) * s->_nsvtxs)) == NULL)
error("ofps: malloc failed on svtxs%d", s->_nsvtxs);
}
s->svtxs[s->nsvtxs]= vx;
vx->psvtxs = &s->svtxs[s->nsvtxs];
s->nsvtxs++;
} else if (50.0 * v > vx->hitmarg) {
vx->hnode = nn;
vx->hitmarg = 50.0 * v;
}
#ifdef DEBUG
printf("Added vtx no %d to node %d for fixup\n",vx->no,nn->ix);
#endif
}
}
/* Or locate the nearest node to the vertex. */
/* (This only returns visible nodes) */
if ((nn = ofps_findclosest_node(s, &ceperr, vx)) != NULL) {
double errimp = vx->eperr - ceperr;
/* See if it is closer than the parent nodes */
if (errimp >= vx->fuptol) { /* It is */
/* Add the vertexe to the "to be fixed" list */
#ifdef DEBUG
printf("Vertex no %d hit by node ix %d by %e\n",vx->no,nn->ix,errimp);
#endif
hit = 1;
if (vx->hnode == NULL) {
vx->hnode = nn;
vx->hitmarg = errimp;
if (s->nsvtxs >= s->_nsvtxs) {
s->_nsvtxs = 2 * s->_nsvtxs + 5;
if ((s->svtxs = (vtx **)realloc(s->svtxs, sizeof(vtx *) * s->_nsvtxs)) == NULL)
error("ofps: malloc failed on svtxs%d", s->_nsvtxs);
}
s->svtxs[s->nsvtxs]= vx;
vx->psvtxs = &s->svtxs[s->nsvtxs];
s->nsvtxs++;
} else if (errimp > vx->hitmarg) {
vx->hnode = nn;
vx->hitmarg = errimp;
}
#ifdef DEBUG
printf("Added node %d to vtx no %d fixup\n",nn->ix, vx->no);
#endif
}
}
next_vtx:;
if (hit) {
vx->fupcount++;
vx->fuptol *= 2.0;
nfuxups++;
}
/* Remove this vertex from the check list */
if (vx->pfchl != NULL) { /* If is on fixup check list, remove it */
*vx->pfchl = vx->fchl;
if (vx->fchl != NULL)
vx->fchl->pfchl = vx->pfchl;
}
vx->pfchl = NULL;
vx->fchl = NULL;
}
if (s->fchl != NULL)
error("Check list should be empty!");
if (nfuxups < l_nfuxups) {
l_nfuxups = nfuxups;
csllow = 0;
} else {
csllow++;
}
#ifdef DEBUG
printf("\nAbout to fixup %d marked vertexes\n",nfuxups);
#endif
/* Smallest error to largest seems best, */
/* probably because the closer nodes cut off the */
/* further ones, reducing the number of redundant create/deletes */
#define HEAP_COMPARE(A,B) ((A)->hitmarg < (B)->hitmarg)
HEAPSORT(vtx *, s->svtxs, s->nsvtxs);
#undef HEAP_COMPARE
/* Fixup the back references after the sort */
for (i = 0; i < s->nsvtxs; i++) {
vx = s->svtxs[i];
vx->psvtxs = &s->svtxs[i];
}
/* For each vertex on the "to be fixed" list, */
/* search for hits by the node starting at that vertex, */
/* and recursively locate all the hit vertexes. */
for (i = 0; i < s->nsvtxs; i++) {
if ((vx = s->svtxs[i]) == NULL) {
continue; /* Vertex got deleted by a previous fix */
}
nn = vx->hnode;
s->svtxs[i] = NULL;
vx->psvtxs = NULL;
s->nvcheckhits = 0; /* Count number of vertexes hit by recursive check. */
s->batch = NULL; /* Nothing in pending delete list */
s->nup = NULL; /* Nothing in nodes to be updated list */
s->flag++; /* Marker flag for adding this node */
s->nxh = NULL; /* Nothing in nodes hit list */
#ifdef DEBUG
printf("\nFixing up node ix %d starting at vx no %d\n",nn->ix,vx->no);
// fprintf(stderr,"Fixing up node ix %d starting at vx no %d\n",nn->ix,vx->no);
#endif
/* Recursively search for all vertexes hit by the new node */
/* Note that we don't care that this only finds connected hits, */
/* since there should be a separate s->svtxs[] entry for a hit by this */
/* node on a disconnected region. */
ofps_check_vtx(s, nn, vx, 100000, 0);
#ifdef DEBUG
printf("Fixing up node ix %d, %d vertexes hit by it\n",nn->ix,s->nvcheckhits);
#endif
/* Number of nodes that would be checked by exaustive search */
s->vvpchecks += s->nv;
/* Now re-add the node to the veronoi */
if (add_to_vsurf(s, nn, 1, 0) > 0) {
s->add_hit++;
#ifdef DUMP_PLOT_EACHFIXUP
printf("After adding node ix %d at %s to vurf\n",nn->ix,ppos(di,nn->p));
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 0, -1); /* Device, No wait, verticies */
#endif /* DUMP_PLOT_EACHFIXUP */
} else {
#ifdef DUMP_PLOT_EACHFIXUP
printf("Adding node ix %d at %s to vurf was miss\n",nn->ix,ppos(di,nn->p));
#endif /* DUMP_PLOT_EACHFIXUP */
s->fadd_mis++;
}
}
s->nsvtxs = 0;
} /* Loop until there are no more vertexes to check */
#ifdef DEBUG
printf("Done fixups s->fchl 0x%x == NULL or csllow %d >= %d\n",s->fchl,csllow,mxcsllow);
#endif
#ifdef SANITY_CHECK_FIXUP
/* Check that no node other than a parent is closer to any vertex */
if (check_vertex_closest_node(s)) {
#ifdef SANITY_CHECK_FIXUP_FATAL
error("!!!!!! Sanity: Fixup didn't work");
#endif /* SANITY_CHECK_FIXUP_FATAL */
}
#endif /* SANITY_CHECK_FIXUP */
#ifdef DEBUG
printf("Applied fixups\n");
#endif
}
/* --------------------------------------------------- */
/* After seeding or re-positioning, create the node */
/* neighbour node and vertex lists. */
/* (Invalidates and deletes any midpoints) */
static void ofps_re_create_node_node_vtx_lists(ofps *s) {
int i, e, di = s->di;
vtx *vx;
/* for each node, clear its vertex list */
for (i = -s->gnp; i < s->np; i++) {
node *p = s->n[i];
p->nvv = 0;
}
/* For each vertex, add it to each of its parent nodes */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
for (e = 0; e <= di; e++) {
node *p = s->n[vx->nix[e]];
node_add_vertex(s, p, vx);
}
}
/* For each node, recompute its neighbourhood nodes */
for (i = -s->gnp; i < s->np; i++) {
node *p = s->n[i];
node_recomp_nvn(s, p); /* Recompute the nodes associated vertex nodes */
}
}
/* --------------------------------------------------- */
/* Midpoints */
/* Finding midpoint location code using dnsqe() */
/* Context for callback */
typedef struct {
ofps *s;
node *nds[2]; /* List of nodes */
} mopt_cx;
/* calculate the functions at x[] */
int dnsq_mid_solver( /* Return < 0 on abort */
void *fdata, /* Opaque data pointer */
int n, /* Dimenstionality */
double *x, /* Multivariate input values */
double *fvec, /* Multivariate output values */
int iflag /* Flag set to 0 to trigger debug output */
) {
mopt_cx *cx = (mopt_cx *)fdata;
ofps *s = cx->s;
int e, di = s->di;
double pos[MXPD], sv[MXPD];
double cee[2], teperr;
//printf("~1 dnsq_solver got %d nodes and %d planes\n",cx->nn,cx->nsp);
/* Compute pos as interpolation between node 0 and 1 */
for (e = 0; e < di; e++)
pos[e] = cx->nds[0]->p[e] * (1.0 - x[0]) + cx->nds[1]->p[e] * x[0];
ofps_cc_percept(s, sv, pos);
/* Get eperr */
cee[0] = ofps_comp_eperr8(s, NULL, sv, pos, cx->nds[0]->v, cx->nds[0]->p, cx->nds[0]->nsp);
cee[1] = ofps_comp_eperr8(s, NULL, sv, pos, cx->nds[1]->v, cx->nds[1]->p, cx->nds[1]->nsp);
//printf("~1 error = %f, %f", cee[0], cee[1]);
teperr = 0.5 * (cee[0] + cee[1]);
fvec[0] = teperr - cee[0];
// printf("dnsq_mid_solver returning %f from %f\n",fvec[0],x[0]);
return 0;
}
/* Create or re-create all the midpoints, given the vertexes are done. */
static void
ofps_create_mids(ofps *s) {
int e, di = s->di;
int i, j, k;
double rerr;
int nsp = 0; /* Number of surface points */
double dnsqtol = 1e-6; /* Solution tollerance to aim for */
vopt_cx cx;
double fvec[1];
int rv;
cx.s = s;
//printf("~1 creating mid points\n");
/* Clear any existing midpoints */
for (i = 0; i < s->tinp; i++) {
node *p = s->n[i];
if (p->ix < 0)
break; /* Done when we get to gamut boundary nodes */
for (j = 0; j < p->nvn; j++) {
if (p->mm[j] != NULL) {
del_mid(s, p->mm[j]);
p->mm[j] = NULL;
}
}
}
/* For each node, make sure it and each neighbor node have a shared midpoint */
for (i = 0; i < s->tinp; i++) {
node *p = s->n[i];
if (p->ix < 0)
break; /* Done when we get to gamut boundary nodes */
/* For each neighbor node, create midpoint */
for (j = 0; j < p->nvn; j++) {
mid *mp;
node *p2;
double ee[2];
if (p->vn[j] < 0 || p->mm[j] != NULL)
continue; /* Gamut boundary or already got a midpoint */
/* Create a midpoint between node p->ix and p->vn[j] */
p2 = s->n[p->vn[j]];
mp = new_mid(s);
mp->refc++;
p->mm[j] = mp;
for (k = 0; k < p2->nvn; k++) {
if (p2->vn[k] == p->ix) {
p2->mm[k] = mp;
mp->refc++;
break;
}
}
mp->nix[0] = p->ix;
mp->nix[1] = p->vn[j];
//printf("~1 creating midpoint %d between nodes %d %d\n",mp->no,p->ix,p->vn[j]);
cx.nds[0] = p;
cx.nds[1] = p2;
mp->np = 0.5;
/* Locate mid point */
if ((rv = dnsqe((void *)&cx, dnsq_mid_solver, NULL, 1, &mp->np,
0.2, fvec, 0.0, dnsqtol, 0, 0)) != 1 && rv != 3) {
error("ofps: Locating midpoint failed with %d",rv);
}
for (e = 0; e < di; e++)
mp->p[e] = p->p[e] * (1.0 - mp->np) + p2->p[e] * mp->np;
ofps_cc_percept(s, mp->v, mp->p);
/* Compute the eperr's for midpoint */
ofps_pn_eperr(s, mp->ce, ee, mp->v, mp->p, cx.nds, 2);
mp->eperr = ofps_eperr2(ee, 2);
mp->eserr = ofps_eserr2(mp->ce, ee, 2);
//printf("~1 location %s (%s) eperr %f\n",ppos(di,mp->p), ppos(di,mp->v), mp->eperr);
}
}
}
/* --------------------------------------------------- */
/* Statistics: Compute serr stats. */
static void ofps_stats(ofps *s) {
int e, di = s->di;
int i, j;
double acnt;
vtx *vx;
mid *mp;
//printf("~1 stats called\n");
s->mn = 1e80;
s->mx = -1e80;
s->av = 0.0;
acnt = 0.0;
/* Vertex stats */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
double es;
if (vx->ghost) /* Skip a ghost (coincident) vertex */
continue;
#ifdef INDEP_SURFACE
/* Ignore vertexes that aren't full dimension. */
if (sm_andtest(s, &s->sc[0].a_sm, &vx->vm) == 0)
continue;
#endif
es = vx->eserr;
if (es >= 0.0 && es < s->mn)
s->mn = es;
if (es > s->mx)
s->mx = es;
s->av += es;
acnt++;
}
s->av /= acnt;
/* Midpoint/node stats */
for (s->smns = 1e6, mp = s->umid; mp != NULL; mp = mp->link) {
if (mp->nix[0] < 0 || mp->nix[1] < 0
|| mp->eserr < 0.0)
continue; /* Skip fake points */
if (mp->eserr < s->smns) {
s->smns = mp->eserr;
}
}
s->smns *= 2.0; /* Error distance between nodes is double error to midpoint */
}
/* --------------------------------------------------- */
/* Support accessing the list of generated sample points */
/* Reset the read index */
static void
ofps_reset(ofps *s) {
s->rix = 0;
}
/* Read the next non-fixed point value */
/* Return nz if no more */
static int
ofps_read(ofps *s, double *p, double *v) {
int e;
/* Advance to next non-fixed point */
while(s->rix < s->np && s->n[s->rix]->fx)
s->rix++;
if (s->rix >= s->np)
return 1;
/* Return point info to caller */
for (e = 0; e < s->di; e++) {
if (p != NULL)
p[e] = s->n[s->rix]->p[e];
if (v != NULL)
v[e] = s->n[s->rix]->v[e];
}
s->rix++;
return 0;
}
/* --------------------------------------------------- */
/* Compute more optimum location for node amongst the surrounding */
/* verticies. The result is put in ->np[] and ->nv[]. */
/* The main aim is to minimize the maximum eserr of any vertex, */
/* but moving away from low midpoint eserr's improves the */
/* convergence rate and improves the eveness of the result. */
static void comp_opt(ofps *s, int poi, double oshoot, double sep_weight) {
node *pp; /* Node in question */
int e, di = s->di;
double radsq = -1.0; /* Span/radius squared */
double rad;
double sum;
int i;
int bi = 0, bj = 0;
pp = s->n[poi]; /* Node in question */
/* Move towards vertex with highest eserr approach */
if (pp->nvv > 0) {
double aerr1, werr1, berr1, cnt1; /* Average, worst, best eserr from vertexes */
int weix1, beix1; /* Worst, best error vertex index */
double ov1[MXPD]; /* Optimization vector towards largest vertex error */
double aerr2, werr2, berr2, cnt2; /* Average, worst, best eserr from midpoints */
int weix2, beix2; /* Worst, best error midpoint index */
double ov2[MXPD]; /* Optimization vector away from smallest midpoint error */
//printf("\n --------------------------------\n");
//printf("~1 Optimizing ix %d, %f %f\n",poi,pp->p[0],pp->p[1]);
/* Compute the average and locate the largest error from verticies */
for (aerr1 = cnt1 = 0.0, werr1 = -1.0, berr1 = 1e80, i = 0; i < pp->nvv; i++) {
vtx *vp = pp->vv[i];
#ifdef INDEP_SURFACE
/* Ingnore vertexes that are not visible to this node. */
if (sm_vtx_node(s, vp, pp) == 0) {
continue;
}
#endif
aerr1 += vp->eserr;
cnt1++;
//printf("~1 Vertex no %d at %f %f serr = %f\n",vp->no,vp->p[0],vp->p[1],vp->eserr);
if (vp->eserr > werr1) {
werr1 = vp->eserr;
weix1 = i;
}
if (vp->eserr < berr1) {
berr1 = vp->eserr;
beix1 = i;
}
}
if (cnt1 > 0.0 && werr1 > NUMTOL && berr1 > NUMTOL) {
double wbf, bbf;
double towards = 0.8; /* Amount to weight vector towards from closest */
/* Compute a blend factor that takes the current */
/* location towards the worst vertex error and */
/* away from the best */
aerr1 /= cnt1;
wbf = towards * (werr1 - aerr1)/werr1;
bbf = (1.0 - towards) * (aerr1 - berr1)/berr1;
// wbf = towards * (werr1 - aerr1)/aerr1;
// bbf = (1.0 - towards) * (aerr1 - berr1)/aerr1;
for (e = 0; e < di; e++)
ov1[e] = wbf * (pp->vv[weix1]->p[e] - pp->p[e])
+ bbf * (pp->p[e] - pp->vv[beix1]->p[e]);
//printf("~1 moved %f %f towards vtx no %d at %f %f\n",ov1[0],ov1[2],pp->vv[weix1]->no,pp->vv[weix1]->p[0],pp->vv[weix1]->p[1]);
} else {
for (e = 0; e < di; e++)
ov1[e] = 0.0;
}
/* Compute the average and locate the smallest error from midpoints */
for (aerr2 = cnt2 = 0.0, werr2 = 1e80, berr2 = -1.0, i = 0; i < pp->nvn; i++) {
mid *mp = pp->mm[i];
node *on = s->n[pp->vn[i]]; /* Other node involved */
if (mp == NULL || mp->nix[0] < 0 || mp->nix[1] < 0)
continue; /* Must be a fake gamut boundary node */
#ifdef INDEP_SURFACE
/* Ingnore nodes of higher dimension */
if ((pp->pmask & on->pmask) != pp->pmask) {
continue;
}
#endif
aerr2 += mp->eserr;
cnt2++;
//printf("~1 plane no %d from node ix %d serr = %f\n",mp->no,on->ix,mp->eserr);
if (mp->eserr < werr2) {
werr2 = mp->eserr;
weix2 = i;
}
if (mp->eserr > berr2) {
berr2 = mp->eserr;
beix2 = i;
}
}
if (cnt2 > 0.0 && werr2 > NUMTOL && berr2 > NUMTOL) {
double wbf, bbf;
double away = 0.8; /* Amount to weight vector away from closest */
/* Compute a blend factor that takes the current */
/* location away from the worst plane error */
aerr2 /= cnt2;
wbf = away * (aerr2 - werr2)/werr2;
bbf = (1.0 - away) * (berr2 - aerr2)/berr2;
// wbf = away * (aerr2 - werr2)/aerr2;
// bbf = (1.0 - away) * (berr2 - aerr2)/aerr2;
for (e = 0; e < di; e++)
ov2[e] = wbf * (pp->p[e] - pp->mm[weix2]->p[e])
+ bbf * (pp->mm[beix2]->p[e] - pp->p[e]);
//printf("~1 moved %f %f away from node ix %d at %f %f\n",ov2[0],ov2[1],pp->vn[weix2],pp->mm[weix2]->p[0],pp->mm[weix2]->p[1]);
} else {
for (e = 0; e < di; e++)
ov2[e] = 0.0;
}
//printf("~1 ov1 = %f %f, ov2 = %f %f, sep weight %f\n",ov1[0], ov1[1], ov2[0], ov2[1], sep_weight);
/* Move the node by the sum of the two vectors */
for (e = 0; e < di; e++)
pp->np[e] = pp->p[e] + (1.0 - sep_weight) * ov1[e] + sep_weight * ov2[e];
//printf("~1 moved node %d by %f %f\n",pp->ix, (1.0 - sep_weight) * ov1[0] + sep_weight * ov2[0],(1.0 - sep_weight) * ov1[1] + sep_weight * ov2[1]);
}
//printf("~1 check moved by %f %f\n",pp->np[0] - pp->p[0], pp->np[1] - pp->p[1]);
/* Apply overshoot/damping */
for (e = 0; e < di; e++)
pp->np[e] = pp->p[e] + (pp->np[e] - pp->p[e]) * oshoot;
//printf("~1 after overshoot of %f got %f %f\n",oshoot,pp->np[0],pp->np[1]);
/* Clip the new location */
ofps_clip_point10(s, pp->np, pp->np);
#if defined(KEEP_SURFACE) || defined(INDEP_SURFACE)
if (pp->nsp > 0) {
confineto_gsurf(s, pp->np, pp->sp, pp->nsp);
}
#endif
/* Update perceptual */
s->percept(s->od, pp->nv, pp->np); /* Was clipped above */
/* Compute how far the point has moved */
/* (?? maybe should change this to change in average or max eserr ??) */
for (sum = 0.0, e = 0; e < di; e++) {
double tt = pp->np[e] - pp->p[e];
sum += tt * tt;
//printf("~1 total motion = %f\n",sqrt(sum));
}
if (sum > s->mxmvsq) /* Track maximum movement */
s->mxmvsq = sum;
}
static void
ofps_optimize(
ofps *s
) {
int maxits;
int transitters;
double transpow;
double oshoot, ioshoot, foshoot;
double sepw, isepw, fsepw;
double stoptol;
int e, di = s->di;
int i, j;
/* Default is "Good" */
maxits = OPT_MAXITS;
transitters = OPT_TRANS_ITTERS;
transpow = OPT_TRANS_POW;
ioshoot = OPT_INITIAL_OVERSHOOT;
foshoot = OPT_FINAL_OVERSHOOT;
isepw = OPT_INITIAL_SEP_WEIGHT;
fsepw = OPT_FINAL_SEP_WEIGHT;
stoptol = OPT_STOP_TOL;
#ifdef OPT_MAXITS_2
/* Option is "Fast" */
if (s->good == 0) {
maxits = OPT_MAXITS_2;
transitters = OPT_TRANS_ITTERS_2;
transpow = OPT_TRANS_POW_2;
ioshoot = OPT_INITIAL_OVERSHOOT_2;
foshoot = OPT_FINAL_OVERSHOOT_2;
isepw = OPT_INITIAL_SEP_WEIGHT_2;
fsepw = OPT_FINAL_SEP_WEIGHT_2;
stoptol = OPT_STOP_TOL_2;
}
#endif /* OPT_MAXITS_2 */
oshoot = ioshoot;
for (s->optit = 0; s->optit < maxits; s->optit++) { /* Up to maximum number of itterations */
vtx *vx;
double bf = 1.0;
int nvxhits;
double hratio, thresh;
int doinc = 0;
s->mxmvsq = 0.0;
if (s->optit < transitters)
bf = s->optit/(double)transitters;
bf = pow(bf, transpow);
oshoot = (1.0 - bf) * ioshoot + bf * foshoot;
sepw = (1.0 - bf) * isepw + bf * fsepw;
/* Compute optimized node positions */
for (i = 0; i < s->tinp; i++) {
if (s->n[i]->fx)
continue; /* Ignore fixed points */
comp_opt(s, i, oshoot, sepw);
}
/* Then update their positions to the optimized ones */
for (i = 0; i < s->tinp; i++) {
node *pp = s->n[i];
if (pp->fx)
continue; /* Ignore fixed points */
ofps_rem_nacc(s, pp); /* Remove from spatial accelleration grid */
for (e = 0; e < di; e++) {
pp->op[e] = pp->p[e]; /* Record previous position */
pp->p[e] = pp->np[e]; /* Move to optimized location */
pp->v[e] = pp->nv[e];
}
ofps_add_nacc(s, pp); /* Add to spatial acceleration grid */
}
/* Make sure that the optimized nodes don't accidentaly collide */
for (i = 0; i < s->tinp; i++) {
node *pp = s->n[i];
if (pp->fx)
continue; /* Ignore fixed points */
for (j = 0; j < 20; j++) { /* Retry until not cooincident */
int pci; /* Point list index */
acell *cp; /* Acceleration cell */
node *p1;
pci = ofps_point2cell(s, pp->v, pp->p); /* Grid index of cell of interest */
cp = &s->grid[pci];
for (p1 = cp->head; p1 != NULL; p1 = p1->n) {
if (p1 == pp)
continue;
for (e = 0; e < di; e++) {
if (fabs(pp->p[e] - p1->p[e]) > COINTOL)
break; /* Not cooincident */
}
if (e >= di) { /* Cooincident */
#ifdef DEBUG
printf("Optimized node ix %d at %s collides with ix %d at %s - joggling it %d\n",pp->ix,ppos(di,pp->p),p1->ix,ppos(di,p1->p),i);
warning("Optimized node ix %d at %s collides with ix %d at %s - joggling it %d",pp->ix,ppos(di,pp->p),p1->ix,ppos(di,p1->p),i);
#endif
ofps_rem_nacc(s, pp); /* Remove from spatial accelleration grid */
/* Joggle it's position */
for (e = 0; e < di; e++) {
if (pp->p[e] < 0.5)
pp->p[e] += d_rand(0.0, 1e-4);
else
pp->p[e] -= d_rand(0.0, 1e-4);
}
/* Ignore confine planes. Next itter should fix it anyway ? */
ofps_clip_point10(s, pp->p, pp->p);
/* Update perceptual (was clipped above) */
s->percept(s->od, pp->v, pp->p);
break;
}
}
if (p1 == NULL)
break;
}
if (j >= 20)
error("ofps_optimize: Assert, was unable to joggle cooincindent point");
}
/* Ideally the fixup method should create and delete fewer vertexes */
/* than reseeding, hence always be faster, but in practice this doesn't */
/* seem to be so. Perhaps this is because the fixups are being */
/* done in a far from optimal order ? What this means is that often */
/* for big movements reseeding will be faster. To get the best of both, */
/* we try and estimate when the fixup method will break even with */
/* reseeding, and switch over. */
/* Estimate how many vertexes will be hit by the move */
nvxhits = ofps_quick_check_hits(s);
/* Decide which way to go */
thresh = 1.0/(di * di);
hratio = nvxhits/(double)s->nv;
//printf("~1 quick check of vertex hits = %d, ratio %f, threshold %f\n",nvxhits,hratio,thresh);
/* Hmm. Re-seed seems to sometimes be slower than expected for > 3D, */
/* so don't use it. */
if (hratio < thresh && di < 4) {
doinc = 1;
}
#ifdef FORCE_RESEED /* Force reseed after itteration */
doinc = 0;
#else
# ifdef FORCE_INCREMENTAL /* Force incremental update after itteration */
doinc = 1;
# endif
#endif
/* Incrementally update veronoi */
if (doinc) {
if (s->verb)
printf("Fixing up veronoi\n");
/* Re-position the vertexes, and fixup the veronoi */
ofps_repos_and_fix_voronoi(s);
/* Reseed the veronoi */
} else {
if (s->verb)
printf("Re-seeding\n");
/* remove nodes from the spatial acceleration grid. */
for (i = 0; i < s->tinp; i++) {
node *pp = s->n[i];
ofps_rem_nacc(s, pp); /* Remove from spatial accelleration grid */
}
/* And recompute veronoi, and add to spatial accelleration grid. */
ofps_redo_voronoi(s);
}
ofps_re_create_node_node_vtx_lists(s);
ofps_create_mids(s);
ofps_stats(s);
if (s->verb) {
printf("It %d: Maxmv = %f, MinPoint = %.3f, Min = %.3f, Avg. = %.3f, Max = %.3f, %.1f secs.\n",s->optit+1,sqrt(s->mxmvsq),s->smns,s->mn,s->av,s->mx,(msec_time() - s->l_mstime) / 1000.0);
#ifdef STATS
printf("Current vtx %d, created %d, deleted %d, positioned %d\n", s->nv,s->nvtxcreated - s->l_nvtxcreated,s->nvtxdeleted - s->l_nvtxdeleted, s->positions - s->l_positions);
s->l_positions = s->positions;
s->l_nvtxcreated = s->nvtxcreated;
s->l_nvtxdeleted = s->nvtxdeleted;
#endif
s->l_mstime = msec_time();
}
#ifdef DUMP_STRUCTURE
dump_node_vtxs(s, 1);
// { char buf[200]; sprintf(buf, "After itteration %d",s->optit+1); dump_node_vtxs2(s, buf); }
printf("=========================================================================\n");
#endif
#ifdef DUMP_PLOT
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 1, -1); /* Device, wait, verticies */
#endif /* DUMP_PLOT */
#ifdef SANITY_RESEED_AFTER_FIXUPS
/* For debugging, replace the incremental fixed up veronoi with */
/* a from scratch one. */
if (s->verb)
printf("Re-seeding after fixup:\n");
/* Save the current incremental vertexes */
save_ivertexes(s);
ofps_redo_voronoi(s);
ofps_re_create_node_node_vtx_lists(s);
ofps_create_mids(s);
ofps_stats(s);
if (s->verb) {
printf("It %d: Maxmv = %f, MinPoint = %.3f, Min = %.3f, Avg. = %.3f, Max = %.3f, %.1f secs.\n",s->optit+1,sqrt(s->mxmvsq),s->smns,s->mn,s->av,s->mx,(msec_time() - s->l_mstime) / 1000.0);
#ifdef STATS
printf("Current vtx %d, created %d, deleted %d, positioned %d\n", s->nvtxcreated - s->l_nvtxcreated,s->nvtxdeleted - s->l_nvtxdeleted, s->positions - s->l_positions);
s->l_positions = s->positions;
s->l_nvtxcreated = s->nvtxcreated;
s->l_nvtxdeleted = s->nvtxdeleted;
#endif
s->l_mstime = msec_time();
}
/* Check that no node other than a parent is closer to any vertex */
if (check_vertex_closest_node(s)) {
warning("Verify that re-seed leaves only parents closest to vertexes failed");
}
/* Check the incremental vertexes against the re-seeded vertexes */
if (check_vertexes(s)) {
warning("Verify of incremental vertexes failed!");
printf("Verify of incremental vertexes failed!\n");
} else {
warning("Verify of incremental vertexes succeeded!");
}
#ifdef DUMP_STRUCTURE
dump_node_vtxs(s, 1);
#endif
#ifdef DUMP_PLOT
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 1, -1); /* Device, wait, verticies */
#endif /* DUMP_PLOT */
#endif /* SANITY_RESEED_AFTER_FIXUPS */
if (sqrt(s->mxmvsq) < stoptol)
break;
}
}
/* ------------------------------------------------------------------------ */
/* Main object creation/destruction */
/* Destroy ourselves */
static void
ofps_del(ofps *s) {
int i, e, di = s->di;
if (s->ufx != NULL)
free(s->ufx);
/* Free our nodes */
for (i = 0; i < s->np; i++) {
node_free(s, s->n[i]);
}
s->n -= s->gnp; /* Fixup offset */
free(s->n);
free(s->_n);
/* Any free vertexes */
while (s->fvtx != NULL) {
vtx *p = s->fvtx;
s->fvtx = p->link;
free(p);
}
/* Any other allocations */
s->sob->del(s->sob);
if (s->combs != NULL) {
for (i = 0; i < s->_ncombs; i++) {
if (s->combs[i].v1 != NULL)
free(s->combs[i].v1);
if (s->combs[i].v2 != NULL)
free(s->combs[i].v2);
}
free(s->combs);
}
if (s->sc)
free(s->sc);
if (s->svtxs != NULL)
free(s->svtxs);
if (s->_grid != NULL)
free(s->_grid);
if (s->acnl != NULL)
free(s->acnl);
if (s->vtreep != NULL)
aat_adelete(s->vtreep);
for (e = 0; e <= (di+1); e++) {
if (s->vtrees[e] != NULL)
aat_adelete(s->vtrees[e]);
}
if (s->pcache != NULL)
s->pcache->del(s->pcache);
free(s);
}
/* Constructor */
ofps *new_ofps(
int verb, /* Verbosity level, 1 = progress, 2 = warnings */
int di, /* Dimensionality of device space */
double ilimit, /* Ink limit (sum of device coords max) */
int tinp, /* Total number of points to generate, including fixed */
int good, /* 0 = fast, 1 = good */
double dadaptation, /* Degree of adaptation to device characteristic 0.0 - 1.0 */
double devd_wght, /* Device space weighting (if dad < 0) */
double perc_wght, /* Perceptual space weighting (if dad < 0) */
double curv_wght, /* Curvature weighting (if dad < 0) */
fxpos *fxlist, /* List of existing fixed points (may be NULL) */
int fxno, /* Number of existing fixes points */
void (*percept)(void *od, double *out, double *in), /* Perceptual lookup func. */
void *od /* context for Perceptual function */
) {
return new_ofps_ex(verb, di, ilimit, NULL, NULL, tinp, good,
dadaptation, devd_wght, perc_wght, curv_wght,
fxlist, fxno, percept, od, 0, -1);
}
/* Extended constructor */
ofps *new_ofps_ex(
int verb, /* Verbosity level, 1 = progress, 2 = warnings */
int di, /* Dimensionality of device space */
double ilimit, /* Total ink limit (sum of device coords max) */
double *imin, /* Ink limit - limit on min of p[], usually >= 0.0 (may be NULL) */
double *imax, /* Ink limit - limit on min of p[], usually <= 1.0 (may be NULL) */
int tinp, /* Total number of points to generate, including fixed */
int good, /* 0 = fast, 1 = good */
double dadaptation, /* Degree of adaptation to device characteristic 0.0 - 1.0 */
double devd_wght, /* Device space weighting (if dad < 0) */
double perc_wght, /* Perceptual space weighting (if dad < 0) */
double curv_wght, /* Curvature weighting (if dad < 0) */
fxpos *fxlist, /* List of existing fixed points (may be NULL) */
int fxno, /* Number of existing fixes points */
void (*percept)(void *od, double *out, double *in), /* Perceptual lookup func. */
void *od, /* context for Perceptual function */
int ntostop, /* Debug - number of points until diagnostic stop */
int nopstop /* Debug - number of optimizations until diagnostic stop, -1 = not */
) {
int i, e;
ofps *s;
long stime,ttime;
stime = clock();
if ((s = (ofps *)calloc(sizeof(ofps), 1)) == NULL)
error ("ofps: malloc failed on new ofps");
if (di > MXPD)
error ("ofps: Can't handle di %d",di);
s->verb = verb;
s->ntostop = ntostop;
s->nopstop = nopstop;
if ((s->sob = new_sobol(di)) == NULL)
error ("ofps: new_sobol %d failed", di);
if (s->verb)
printf("Degree of adaptation: %.3f\n", dadaptation);
/* Set internal values explicitly */
if (dadaptation < 0.0) {
s->devd_wght = devd_wght;
s->perc_wght = perc_wght;
s->curv_wght = curv_wght;
/* Set values implicitly with adapation level */
} else {
if (dadaptation > 1.0)
dadaptation = 1.0;
/* Convert to internal numbers */
s->perc_wght = ADAPT_PERCWGHT * dadaptation;
s->curv_wght = ADAPT_CURVWGHT * dadaptation * dadaptation;
s->devd_wght = 1.0 - s->perc_wght;
}
if (s->verb)
printf("Adaptation weights: Device = %.3f, Perceptual = %.3f, Curvature = %.3f\n",
s->devd_wght,s->perc_wght,s->curv_wght);
s->di = di;
if (tinp < fxno) /* Make sure we return at least the fixed points */
tinp = fxno;
s->fxno = fxno; /* Number of fixed points provided */
s->tinp = tinp; /* Target total number of points */
/* Hack to workaround pathalogical case. At ilimit == di-2.0, we get > 32 bits */
/* of mask for CMYK */
if (di >= 3
&& ilimit >= (di-2.0 - 2 * ILIMITEPS)
&& ilimit <= (di-2.0 + 2 * ILIMITEPS))
ilimit = di-2.0 - 2 * ILIMITEPS;
/* Hack to workaround pathalogical case. At ilimit == 100% we get a failure */
/* to add any variable steps */
if (ilimit > 0.9999 && ilimit < 1.0001)
ilimit = 0.9999;
s->ilimit = ilimit;
for (e = 0; e < di; e++) {
if (imin != NULL)
s->imin[e] = imin[e];
else
s->imin[e] = 0.0;
if (imax != NULL)
s->imax[e] = imax[e];
else
s->imax[e] = 1.0;
}
/* Compute an approximate half expected sample point spacing, */
/* and setup seeding acceleration grid. */
{
double vol = 1.0;
double eprange;
for (e = 0; e < di; e++)
vol *= s->imax[e] - s->imin[e];
vol /= tinp; /* Approx vol per point */
vol = pow(vol, 1.0/di); /* Distance per point */
s->surftol = SURFTOL * vol;
//printf("~1 surftol = %f\n",s->surftol);
}
#ifdef STANDALONE_TEST
/* If no perceptual function given, use default */
if (percept == NULL) {
s->percept = default_ofps_to_percept;
s->od = s;
} else {
s->percept = percept;
s->od = od;
}
#else
/* If no perceptual function given, use default */
//warning("~1 new_ofps_ex() forcing default perceptual function");
if (percept == NULL) {
s->percept = default_ofps_to_percept;
s->od = s;
} else {
s->percept = percept;
s->od = od;
}
#endif
s->good = good; /* Fast/Good flag */
s->lperterb = PERTERB_AMOUNT;
#ifdef OPT_MAXITS_2
if (s->good == 0)
s->lperterb = PERTERB_AMOUNT_2;
#endif
s->ssurfpref = INITIAL_SURFACE_PREF;
s->esurfpref = FINAL_SURFACE_PREF;
/* Init method pointers */
s->reset = ofps_reset;
s->read = ofps_read;
s->stats = ofps_stats;
s->del = ofps_del;
s->gnp = 2 * di + 1 + 2; /* Gamut boundary + inside/outside fake points */
/* -1 to -2di-1 are fake boundary nodes indexes, */
/* with -2di-1 being the ink limit boundary. */
/* -2di-2 is the fake inside node. */
/* -2di-3 is the fake outside node. */
/* Allocate the space for the target number of points */
if ((s->_n = (node *)calloc(sizeof(node), s->gnp + s->tinp)) == NULL)
error ("ofps: malloc failed on sample nodes");
if ((s->n = (node **)calloc(sizeof(node *), s->gnp + s->tinp)) == NULL)
error ("ofps: malloc failed on sample nodes");
s->n += s->gnp; /* Allow -ve index for fake points */
for (i = -s->gnp; i < s->tinp; i++) {
int bitp;
s->n[i] = &s->_n[i + s->gnp];
s->n[i]->ix = i;
bitp = 31 & (i + (i >> 4) + (i >> 8) + (i >> 12));
s->n[i]->ixm = (1 << bitp);
}
s->np = s->fnp = 0;
#ifdef STATS
/* Save current counts to report stats after a pass */
s->l_positions = s->positions;
s->l_nvtxcreated = s->nvtxcreated;
s->l_nvtxdeleted = s->nvtxdeleted;
#endif
s->l_mstime = msec_time();
/* Setup the eperr sorted trees */
if ((s->vtreep = aat_anew(vtx_aat_cmp_eperr)) == NULL)
error("Allocating aat tree failed");
/* One sorted tree per number of surface planes */
for (e = 0; e <= (di+1); e++) {
if ((s->vtrees[e] = aat_anew(vtx_aat_cmp_eserr)) == NULL)
error("Allocating aat tree failed");
}
#ifdef CACHE_PERCEPTUAL
ofps_init_pcache(s);
# endif /* CACHE_PERCEPTUAL */
/* Setup spatial acceleration grid */
ofps_init_acc1(s);
/* Initialse the empty veronoi etc. */
ofps_binit(s);
/* Setup spatial acceleration grid (2) */
ofps_init_acc2(s);
/* Setup the fixed points */
ofps_setup_fixed(s, fxlist, fxno);
if (fxno > 0 && tinp <= fxno) { /* There are no moveable points to create */
/* Add the fixed points */
if (ofps_add_fixed(s)) {
s->del(s);
return NULL;
}
if (s->verb && fxno > 0) {
ofps_stats(s);
printf("After fixed points: MinPoint = %.3f, Min = %.3f, Avg. = %.3f, Max = %.3f\n",s->smns,s->mn,s->av,s->mx);
}
}
if (tinp > fxno) { /* There are movable points to create */
/* Add the fixed points and create the moveable points */
ofps_seed(s);
ofps_re_create_node_node_vtx_lists(s);
ofps_create_mids(s);
ofps_stats(s);
if (s->verb) {
printf("After seeding points: MinPoint = %.3f, Min = %.3f, Avg. = %.3f, Max = %.3f, %.1f secs\n",s->smns,s->mn,s->av,s->mx,(msec_time() - s->l_mstime) / 1000.0);
#ifdef STATS
printf("Current vtx %d, created %d, deleted %d, positioned %d\n", s->nv,s->nvtxcreated - s->l_nvtxcreated,s->nvtxdeleted - s->l_nvtxdeleted, s->positions - s->l_positions);
s->l_positions = s->positions;
s->l_nvtxcreated = s->nvtxcreated;
s->l_nvtxdeleted = s->nvtxdeleted;
#endif
s->l_mstime = msec_time();
}
# ifdef DUMP_STRUCTURE
printf("After seeding:\n");
dump_node_vtxs(s, 1);
// dump_node_vtxs2(s, "After seeding");
#else /* !DUMP_STRUCTURE */
#ifdef SANITY_CHECK_CONSISTENCY
sanity_check(s, 1);
#endif
#endif /* !DUMP_STRUCTURE */
#ifdef DUMP_PLOT
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 1, -1); /* Device, No wait, no verticies */
#endif /* DUMP_PLOT */
#ifdef DOOPT
/* Do the optimization */
ofps_optimize(s);
#endif /* DOOPT */
# ifdef DUMP_STRUCTURE
printf("After optimization:\n");
dump_node_vtxs(s, 1);
// dump_node_vtxs2(s, "After optimization");
#else /* !DUMP_STRUCTURE */
#ifdef SANITY_CHECK_CONSISTENCY
sanity_check(s, 1);
#endif
#endif /* !DUMP_STRUCTURE */
ofps_stats(s);
if (s->verb)
printf("After optimization: MinPoint = %.3f, Min = %.3f, Avg. = %.3f, Max = %.3f\n",s->smns, s->mn,s->av,s->mx);
#ifdef DUMP_PLOT
dump_image(s, PERC_PLOT, DO_WAIT, DUMP_VTX, DUMP_PLA, 1, -1); /* Device, wait, verticies */
#endif /* DUMP_PLOT */
}
ofps_reset(s); /* Reset read index */
#if defined(DEBUG) || defined(STATS)
{
vtx *vx;
int novtx = 0;
int totvtxverts = 0;
int maxvtxverts = 0;
vtx **svtxs; /* Sorted vertexes by number of vertexes */
ttime = clock() - stime;
printf("Execution time = %f seconds\n",ttime/(double)CLOCKS_PER_SEC);
/* Look at the vertexes */
for (novtx = 0, vx = s->uvtx; vx != NULL; vx = vx->link, novtx++)
;
if ((svtxs = (vtx **)malloc(sizeof(vtx *) * novtx)) == NULL)
error ("ofps: malloc failed on vertex pointer list");
/* Look at the vertexes */
for (novtx = 0, vx = s->uvtx; vx != NULL; vx = vx->link, novtx++) {
svtxs[novtx] = vx;
totvtxverts += vx->nnv;
if (vx->nnv > maxvtxverts)
maxvtxverts = vx->nnv;
}
#define HEAP_COMPARE(A,B) ((A)->nnv > (B)->nnv)
HEAPSORT(vtx *, svtxs, novtx);
#undef HEAP_COMPARE
// printf("Top 20 vertexes per vertex:\n");
// for (i = 0; i < 20 && i < novtx; i++) {
// printf(" Vtx no %d, no vtxs = %d\n",svtxs[i]->no,svtxs[i]->nnv);
// }
fprintf(stderr,"Average vertexes per vertex %.1f, max %d\n",totvtxverts/(double)novtx,maxvtxverts);
fprintf(stderr,"Average hit vertexes per add %.1f\n",s->nhitv/(double)s->nsurfadds,s->maxhitv);
fprintf(stderr,"Total number of vertex = %d\n",novtx);
fprintf(stderr,"Total vertex positions = %d\n",s->positions);
fprintf(stderr,"Total dnsqs = %d\n",s->dnsqs);
fprintf(stderr,"Total function calls = %d\n",s->funccount);
fprintf(stderr,"Average dnsqs/position = %.2f\n",s->dnsqs/(double)s->positions);
fprintf(stderr,"Average function calls/dnsq = %.1f\n",s->funccount/(double)s->dnsqs);
fprintf(stderr,"Maximum function calls/dnsq = %d\n",s->maxfunc);
fprintf(stderr,"Average function calls/successful dnsq = %.2f\n",s->sucfunc/(double)s->sucdnsq);
fprintf(stderr,"Average function calls/position = %.1f\n",s->funccount/(double)s->positions);
fprintf(stderr,"Maximum tries for dnsq sucess %d\n",s->maxretries);
fprintf(stderr,"Number of position_vtx failures %d\n",s->posfails);
fprintf(stderr,"Vertex hit check efficiency = %.1f%%\n",100.0 * (1.0 - s->vvchecks/(double)s->vvpchecks));
fprintf(stderr,"Average accell cells searched = %.2f\n",s->ncellssch/(double)s->naccsrch);
fprintf(stderr,"add_to_vsurf hit rate = %.1f%%\n",100.0 * s->add_hit/(s->add_hit + s->add_mis));
#ifdef DOOPT
fprintf(stderr,"fixup add_to_vsurf hit rate = %.1f%%\n",100.0 * s->fadd_hit/(s->fadd_hit + s->fadd_mis));
fprintf(stderr,"Vertex closest search efficiency = %.1f%%\n",100.0 * (1.0 - s->nvschd/(double)s->nvfschd));
fprintf(stderr,"Node closest search efficiency = %.1f%%\n",100.0 * (1.0 - s->nnschd/(double)s->nnfschd));
#endif
free(svtxs);
}
#endif
return s;
}
/* =================================================== */
#ifdef STANDALONE_TEST
/* Graphics Gems curve */
static double gcurve(double vv, double g) {
if (g >= 0.0) {
vv = vv/(g - g * vv + 1.0);
} else {
vv = (vv - g * vv)/(1.0 - g * vv);
}
return vv;
}
static void sa_percept(void *od, double *p, double *d) {
double dd[2];
/* Default linear */
p[0] = 100.0 * (dd[0] = d[0]);
p[1] = 100.0 * (dd[1] = d[1]);
/* Normal non-linear test */
// p[0] = 100.0 * gcurve(dd[0], -8.0);
// p[1] = 100.0 * gcurve(dd[1], 4.0);
/* More extreme non-linear test */
p[0] = 100.0 * gcurve(dd[0], -16.0);
p[1] = 100.0 * gcurve(dd[1], 8.0);
/* An X break point to test curvature weighting */
// if (dd[0] < 0.5)
// p[0] = 100.0 * 0.6 * dd[0];
// else
// p[0] = 100.0 * (0.3 + 1.4 * (dd[0] - 0.5));
// p[1] = 100.0 * dd[1];
// if (dd[0] < 0.0)
// dd[0] = 0.0;
// if (dd[1] < 0.0)
// dd[1] = 0.0;
// p[0] = 100.0 * pow(dd[0], 0.5);
// p[1] = 100.0 * pow(dd[1], 1.0);
// p[1] = 0.8 * p[1] + 0.2 * p[0];
/* One that causes dnsq failures due to ACCELL failure */
// p[0] = gcurve(dd[0], -4.0);
// p[1] = gcurve(dd[1], 2.0);
// p[0] = 100.0 * gcurve(0.6 * p[0] + 0.4 * p[1], 2.0);
// p[1] = 100.0 * gcurve(0.1 * p[1] + 0.9 * p[1], -4.0);
// p[0] = 100.0 * dd[0] * dd[0];
// p[1] = 100.0 * dd[1] * dd[1];
}
int
main(argc,argv)
int argc;
char *argv[];
{
int npoints = 55;
int ntostop = 0;
int nopstop = 0;
ofps *s;
fxpos fx[4]; /* Any fixed points */
int nfx = 0;
error_program = argv[0];
printf("Standalone test of ofps, args are: no. of points, default %d, points to skip before diag. plots, optim passes to skip\n",npoints);
if (argc > 1)
npoints = atoi(argv[1]);
if (argc > 2)
ntostop = atoi(argv[2]);
if (argc > 3)
nopstop = atoi(argv[3]);
fx[0].p[0] = 0.5;
fx[0].p[1] = 0.5;
fx[1].p[0] = 0.145722;
fx[1].p[1] = 0.0;
fx[2].p[0] = 1.0;
fx[2].p[1] = 0.104414;
nfx = 0;
/* Create the required points */
s = new_ofps_ex(1, 2, 1.5, NULL, NULL, npoints, 1,
// s = new_ofps_ex(1, 2, 2.5, NULL, NULL, npoints, 1,
SA_ADAPT, SA_DEVD_MULT, SA_PERC_MULT, SA_INTERP_MULT,
fx, nfx, sa_percept, (void *)NULL, ntostop, nopstop);
#ifdef DUMP_PLOT
printf("Device plot (with verts):\n");
dump_image(s, 0, DO_WAIT, 1, DUMP_PLA, 1, -1);
printf("Device plot:\n");
dump_image(s, 0, DO_WAIT, 0, 0, 1, -1);
printf("Perceptual plot (with verts):\n");
dump_image(s, 1, DO_WAIT, 1, DUMP_PLA, 1, -1);
printf("Perceptual plot:\n");
dump_image(s, 1, DO_WAIT, 0, 0, 1, -1);
#endif /* DUMP_PLOT */
s->del(s);
return 0;
}
#endif /* STANDALONE_TEST */
#define WIDTH 400 /* Raster size for debug plots */
#define HEIGHT 400
/* Utility - return a string containing the di coord */
static char *pco(int di, int *co) {
static char buf[5][200];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
for (e = 0; e < di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%d", co[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Utility - return a string containing the di vector */
static char *ppos(int di, double *p) {
static char buf[5][200];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
for (e = 0; e < di; e++) {
double val = p[e];
/* Make -0.00000000 turn into 0.000 for cosmetics */
if (val < 0.0 && val >-1e-9)
val = 0.0;
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%f", val); bp += strlen(bp);
}
return buf[ix];
}
/* Utility - return a string containing the di+1 combination */
static char *pcomb(int di, int *n) {
static char buf[5][200];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
for (e = 0; e <= di; e++) {
if (e > 0)
*bp++ = ' ';
sprintf(bp, "%d", n[e]); bp += strlen(bp);
}
return buf[ix];
}
/* Utility - return a string containing the eperr/eserr value */
static char *peperr(double eperr) {
static char buf[5][200];
static int ix = 0;
int e;
char *bp;
if (++ix >= 5)
ix = 0;
bp = buf[ix];
if (eperr >= 1e50)
sprintf(bp,"%s", "Big");
else
sprintf(bp,"%f",eperr);
return buf[ix];
}
/* --------------------------------------------------------------- */
#if defined(DEBUG) || defined(DUMP_PLOT_SEED) || defined(DUMP_PLOT)
/* Dump the current point positions to a plot window file */
static void
dump_image(
ofps *s,
int pcp, /* Do perceptual plot */
int dwt, /* Do wait for a key */
int dvx, /* Dump voronoi verticies and mid points */
int dpla, /* Dump node planes */
int ferr, /* Show final error rather than seeding error */
int noi /* -1 for general state, node of interest for particular */
) {
int i, j, k, e, di = s->di;
double minx, miny, maxx, maxy;
static double *x1a = NULL; /* Previous sample locations */
static double *y1a = NULL;
static double *x2a = NULL; /* Current sample locations */
static double *y2a = NULL;
static char *_ntext, **ntext;
static int _n3 = 0; /* Current Voronoi verticies */
static double *x3a = NULL;
static double *y3a = NULL;
static plot_col *mcols = NULL;
static char *_mtext, **mtext;
int n3;
static double *x4a = NULL; /* plane vectors */
static double *y4a = NULL;
static double *x5a = NULL;
static double *y5a = NULL;
static plot_col *ocols = NULL;
static int _o4 = 0;
int o4;
if (pcp != 0) { /* Perceptual range */
vtx *vx;
minx = miny = 1e60;
maxx = maxy = -1e60;
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
double v[MXPD];
if (vx->v[0] < minx)
minx = vx->v[0];
if (vx->v[1] < miny)
miny = vx->v[1];
if (vx->v[0] > maxx)
maxx = vx->v[0];
if (vx->v[1] > maxy)
maxy = vx->v[1];
}
} else {
minx = 0.0; /* Assume */
miny = 0.0;
maxx = 1.0;
maxy = 1.0;
}
#ifdef NEVER
/* Expand the range a little */
minx -= 0.1 * (maxx - minx);
maxx += 0.1/1.1 * (maxx - minx);
miny -= 0.1 * (maxy - miny);
maxy += 0.1/1.1 * (maxy - miny);
#endif
if (x1a == NULL) {
if ((x1a = (double *)malloc(s->tinp * sizeof(double))) == NULL)
error ("ofps: malloc failed x1a");
if ((y1a = (double *)malloc(s->tinp * sizeof(double))) == NULL)
error ("ofps: malloc failed ya1");
if ((x2a = (double *)malloc(s->tinp * sizeof(double))) == NULL)
error ("ofps: malloc failed x2a");
if ((y2a = (double *)malloc(s->tinp * sizeof(double))) == NULL)
error ("ofps: malloc failed y2a");
if ((_ntext = (char *)malloc(s->tinp * 10 * sizeof(char))) == NULL)
error ("ofps: malloc failed _ntext");
if ((ntext = (char **)malloc(s->tinp * sizeof(char *))) == NULL)
error ("ofps: malloc failed ntext");
for (i = 0; i < s->tinp; i++)
ntext[i] = _ntext + i * 10;
}
/* Add sample node location */
for (i = 0; i < s->np; i++) {
node *p = s->n[i];
if (pcp != 0) {
double ov[MXPD];
ofps_cc_percept(s, ov, p->op);
x1a[i] = ov[0];
y1a[i] = ov[1];
x2a[i] = p->v[0];
y2a[i] = p->v[1];
} else {
x1a[i] = p->op[0];
y1a[i] = p->op[1];
x2a[i] = p->p[0];
y2a[i] = p->p[1];
}
sprintf(ntext[i],"%d",p->ix);
// sprintf(ntext[i],"",p->ix);
}
if (dvx) {
vtx *vx;
mid *mp;
node *p = NULL;
// double rgb0[3] = { 0.0, 0.5, 0.5 }; /* "cool" */
// double rgb1[3] = { 1.0, 0.5, 0.0 }; /* "warm" */
double rgb0[3] = { 0.0, 1.0, 0.0 }; /* "cool" */
double rgb1[3] = { 1.0, 0.0, 0.5 }; /* "warm" */
double mine, maxe; /* Min and max vertex eserr */
if (noi >= 0)
p = s->n[noi];
if (x3a == NULL) { /* Initial allocation */
_n3 = s->np * 4;
if ((x3a = (double *)malloc(_n3 * sizeof(double))) == NULL)
error ("ofps: malloc failed x3a");
if ((y3a = (double *)malloc(_n3 * sizeof(double))) == NULL)
error ("ofps: malloc failed y3a");
if ((mcols = (plot_col *)malloc(_n3 * sizeof(plot_col))) == NULL)
error ("ofps: malloc failed mcols");
if ((_mtext = (char *)malloc(_n3 * 10 * sizeof(char))) == NULL)
error ("ofps: malloc failed _mtext");
if ((mtext = (char **)malloc(_n3 * sizeof(char *))) == NULL)
error ("ofps: malloc failed mtext");
for (i = 0; i < _n3; i++)
mtext[i] = _mtext + i * 10;
}
/* Compute min & max serr for each vertex */
mine = 1e6;
maxe = -1e6;
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
if (vx->ghost)
continue;
if (vx->eserr > maxe)
maxe = vx->eserr;
if (vx->eserr > NUMTOL && vx->eserr < mine)
mine = vx->eserr;
}
if ((maxe - mine) < 10.0)
maxe = mine + 1.0;
/* Add mid points */
for (n3 = 0, mp = s->umid; mp != NULL; mp = mp->link, n3++) {
if (n3 >= _n3) { /* need more space */
_n3 = 2 * _n3 + 5;
if ((x3a = (double *)realloc(x3a, _n3 * sizeof(double))) == NULL)
error ("ofps: realloc failed x3a %d",_n3);
if ((y3a = (double *)realloc(y3a, _n3 * sizeof(double))) == NULL)
error ("ofps: realloc failed y3a");
if ((mcols = (plot_col *)realloc(mcols, _n3 * sizeof(plot_col))) == NULL)
error ("ofps: realloc failed mcols");
if ((_mtext = (char *)realloc(_mtext, _n3 * 10 * sizeof(char))) == NULL)
error ("ofps: realloc failed _mtext");
if ((mtext = (char **)realloc(mtext, _n3 * sizeof(char *))) == NULL)
error ("ofps: realloc failed mtest");
for (i = 0; i < _n3; i++)
mtext[i] = _mtext + i * 10;
}
if (pcp != 0) {
x3a[n3] = mp->v[0];
y3a[n3] = mp->v[1];
} else {
x3a[n3] = mp->p[0];
y3a[n3] = mp->p[1];
}
/* Show mid points in grey */
mcols[n3].rgb[0] = 0.85;
mcols[n3].rgb[1] = 0.85;
mcols[n3].rgb[2] = 0.85;
sprintf(mtext[n3],"%s","");
sprintf(mtext[n3],"%d",mp->no);
// sprintf(mtext[n3],"%d",(int)(mp->eserr + 0.5));
}
/* Add Voronoi verticies */
for (vx = s->uvtx; vx != NULL; vx = vx->link, n3++) {
if (n3 >= _n3) { /* need more space */
_n3 = _n3 * 2 + 5;
if ((x3a = (double *)realloc(x3a, _n3 * sizeof(double))) == NULL)
error ("ofps: realloc failed x3a %d",_n3);
if ((y3a = (double *)realloc(y3a, _n3 * sizeof(double))) == NULL)
error ("ofps: realloc failed y3a");
if ((mcols = (plot_col *)realloc(mcols, _n3 * sizeof(plot_col))) == NULL)
error ("ofps: realloc failed mcols");
if ((_mtext = (char *)realloc(_mtext, _n3 * 10 * sizeof(char))) == NULL)
error ("ofps: realloc failed _mtext");
if ((mtext = (char **)realloc(mtext, _n3 * sizeof(char *))) == NULL)
error ("ofps: realloc failed mtext");
for (i = 0; i < _n3; i++)
mtext[i] = _mtext + i * 10;
}
if (pcp != 0) {
x3a[n3] = vx->v[0];
y3a[n3] = vx->v[1];
} else {
x3a[n3] = vx->p[0];
y3a[n3] = vx->p[1];
}
/* Show the vertexes as warm to cold, depending on their eserr */
if (p == NULL) {
double bf;
bf = (vx->eserr - mine)/(maxe - mine);
if (bf < 0.0)
bf = 0.0;
if (bf > 1.0)
bf = 1.0;
for (e = 0; e < 3; e++)
mcols[n3].rgb[e] = bf * rgb1[e] + (1.0 - bf) * rgb0[e];
//printf("~1 serr = %f, color = %f %f %f\n",vx->eserr, mcols[n3].rgb[0], mcols[n3].rgb[1], mcols[n3].rgb[2]);
// sprintf(mtext[n3],"");
// sprintf(mtext[n3],"%d",(int)(vx->eserr + 0.5));
#ifndef NEVER /* Vertex no */
sprintf(mtext[n3],"%d",vx->no);
#endif
#ifdef NEVER /* Vertex no and eserr */
if (vx->eserr >= 1e50)
sprintf(mtext[n3],"%d:Big",vx->no);
else
sprintf(mtext[n3],"%d:%d",vx->no,(int)(vx->eserr + 0.5));
#endif
#ifdef NEVER /* eserr */
if (vx->eserr >= 1e50)
sprintf(mtext[n3],"Big");
else
sprintf(mtext[n3],"%d",(int)(vx->eserr + 0.5));
#endif
/* Highlight the vertcies of interest */
} else {
for (j = 0; j < p->nvv; j++) {
if (p->vv[j] == vx)
break;
}
if (j < p->nvv) { /* Vertex associated with node of interest */
mcols[n3].rgb[0] = 0.1;
mcols[n3].rgb[1] = 0.9;
mcols[n3].rgb[2] = 0.9;
sprintf(mtext[n3],"%d",(int)(vx->eserr + 0.5));
} else {
mcols[n3].rgb[0] = 0.82; /* default color */
mcols[n3].rgb[1] = 0.59;
mcols[n3].rgb[2] = 0.0;
sprintf(mtext[n3],"%s","");
}
}
}
#ifdef DUMP_EPERR /* Create .tiff of eperr */
if (s->np >= s->ntostop) {
unsigned char pa[WIDTH * 3];
char *name = "ofps.tif";
int width = WIDTH;
int height = HEIGHT;
int x, y;
TIFF *tif;
double pos[MXPD], vpos[MXPD];
double rgb_low[3] = { 0.0, 1.0, 0.0 }; /* "low error" */
double rgb_high[3] = { 1.0, 0.0, 0.0 }; /* "high error" */
if ((tif = TIFFOpen(name, "w")) == NULL) {
fprintf(stderr,"Failed to open output TIFF file '%s'\n",name);
exit (-1);
}
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
mine = 0.0;
for (y = 0; y < height; y++) {
pos[1] = 1.0 - y/(height-1.0);
/* Fill in pa[] with colors for this line */
for (x = 0; x < width; x++) {
double ss;
unsigned char *dp;
double bf;
double beserr, eserr;
dp = pa + x * 3;
pos[0] = x/(width-1.0);
dp[0] = dp[1] = dp[2] = 0;
//printf("~1 doing %d %d pos %f %f\n",x,y,pos[0],pos[1]);
/* Lookup perceptual value at sample point location */
ofps_cc_percept(s, vpos, pos);
/* See if the sample is in gamut */
for (ss = 0.0, e = 0; e < s->di; e++) {
if (pos[e] < s->imin[e]
|| pos[e] > s->imax[e])
break;
ss += pos[e];
}
if (e < s->di || ss > (s->ilimit + ILIMITEPS)) {
//printf("~1 out of gamut\n");
continue;
}
/* We determine the eserr by evaluating eserr for */
/* every node, and keeping the smallest. */
/* (This could be speeded up by using nearest search function) */
beserr = 1e80;
for (i = 0; i < s->np; i++) {
node *np = s->n[i];
eserr = ofps_comp_eperr9(s, NULL, vpos, pos, np->v, np->p, np->nsp);
if (eserr < beserr)
beserr = eserr;
}
bf = (beserr - mine)/(maxe - mine);
//printf("~1 beserr = %f, bf = %f\n",beserr,bf);
if (bf < 0.0)
bf = 0.0;
if (bf > 1.0)
bf = 1.0;
for (e = 0; e < 3; e++)
dp[e] = (int)(255.0 * (bf * rgb_high[e] + (1.0 - bf) * rgb_low[e]) + 0.5);
}
if (TIFFWriteScanline(tif, (tdata_t)pa, y, 0) < 0) {
fprintf(stderr,"WriteScanline Failed at line %d\n",y);
exit (-1);
}
}
(void) TIFFClose(tif);
}
#endif /* DUMP_EPERR */
}
/* Show veronoi planes by plotting the vertex network */
if (dpla) {
vtx *vx1, *vx2;
if (x4a == NULL) {
_o4 = s->tinp;
if ((x4a = (double *)malloc(_o4 * sizeof(double))) == NULL)
error ("ofps: malloc %d failed",_o4);
if ((y4a = (double *)malloc(_o4 * sizeof(double))) == NULL)
error ("ofps: malloc %d failed",_o4);
if ((x5a = (double *)malloc(_o4 * sizeof(double))) == NULL)
error ("ofps: malloc %d failed",_o4);
if ((y5a = (double *)malloc(_o4 * sizeof(double))) == NULL)
error ("ofps: malloc %d failed",_o4);
if ((ocols = (plot_col *)malloc(_o4 * sizeof(plot_col))) == NULL)
error ("ofps: malloc %d failed",_o4);
}
/* Add normal planes then subd planes, so that subd are always on top */
o4 = 0;
#ifdef INDEP_SURFACE
for (k = 0; k < 2; k++) /* Do two passes */
#else
for (k = 0; k < 1; k++)
#endif
{
/* Add node planes */
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
/* Don't plot faces involving the fake inside or outside node */
for (e = 0; e <= di; e++) {
if (vx1->nix[e] < -s->nbp)
break;
}
if (e <= di)
continue;
for (j = 0; j < vx1->nnv; j++) {
vx2 = vx1->nv[j];
/* Don't plot faces involving the fake inside or outside node */
for (e = 0; e <= di; e++) {
if (vx2->nix[e] < -s->nbp)
break;
}
if (e <= di)
continue;
#ifdef INDEP_SURFACE
if (sm_andtest(s, &vx1->vm, &s->sc[0].a_sm) == 0
|| sm_andtest(s, &vx2->vm, &s->sc[0].a_sm) == 0) { /* Subd plane */
if (k == 0)
continue; /* Doing non-zubd pass */
} else {
if (k == 1)
continue; /* Doing subd pass */
}
#endif
if (o4 >= _o4) { /* need more space */
_o4 *= 2;
if ((x4a = (double *)realloc(x4a, _o4 * sizeof(double))) == NULL)
error ("ofps: realloc x4a %d failed", _o4);
if ((y4a = (double *)realloc(y4a, _o4 * sizeof(double))) == NULL)
error ("ofps: realloc y4a %d failed", _o4);
if ((x5a = (double *)realloc(x5a, _o4 * sizeof(double))) == NULL)
error ("ofps: realloc x5a %d failed", _o4);
if ((y5a = (double *)realloc(y5a, _o4 * sizeof(double))) == NULL)
error ("ofps: realloc y5a %d failed", _o4);
if ((ocols = (plot_col *)realloc(ocols, _o4 * sizeof(plot_col))) == NULL)
error ("ofps: realloc y5a %d failed", _o4);
}
if (pcp != 0) {
x4a[o4] = vx1->v[0];
y4a[o4] = vx1->v[1];
x5a[o4] = vx2->v[0];
y5a[o4] = vx2->v[1];
} else {
x4a[o4] = vx1->p[0];
y4a[o4] = vx1->p[1];
x5a[o4] = vx2->p[0];
y5a[o4] = vx2->p[1];
}
#ifdef INDEP_SURFACE
/* Show the sub dimension outline in apricot */
if (k == 1) {
ocols[o4].rgb[0] = 1.0; /* Apricot */
ocols[o4].rgb[1] = 0.52;
ocols[o4].rgb[2] = 0.57;
} else
#endif
{
ocols[o4].rgb[0] = 0.5; /* Light Blue */
ocols[o4].rgb[1] = 0.9;
ocols[o4].rgb[2] = 0.9;
}
o4++;
}
}
}
}
if ((s->nopstop >= 0 && s->optit < s->nopstop) || s->np < s->ntostop)
dwt = 0;
/* Plot the vectors */
do_plot_vec2(minx, maxx, miny, maxy,
x1a, y1a, x2a, y2a, ntext, s->np, dwt,
x3a, y3a, mcols, mtext, dvx ? n3 : 0,
x4a, y4a, x5a, y5a, ocols, dpla ? o4 : 0);
}
#endif /* DEBUG || DUMP_PLOT */
/* ------------------------------------------------------------------- */
#ifdef SANITY_RESEED_AFTER_FIXUPS
/* Save the current used vertexes to the i_uvtx list, */
/* so that they can be verified against the re-seeded vertexes */
static void save_ivertexes(ofps *s) {
vtx *vx, *nvx;
s->i_uvtx = NULL;
for (vx = s->uvtx; vx != NULL; vx = nvx) {
nvx = vx->link;
/* Remove the vertex from used and other lists */
del_vtx1(s, vx);
/* Add it to the i_uvtx list */
vx->link = s->i_uvtx;
s->i_uvtx = vx;
}
}
/* Check the incremental vertexes against the re-seeded vertexes */
static int check_vertexes(ofps *s) {
int i, j, e, k, di = s->di;
vtx *v1, *v2;
int fail = 0;
printf("Verifying incremental vertexes against re-seeded:\n");
/* For each reference (re-seeded) vertex */
for (v1 = s->uvtx; v1 != NULL; v1 = v1->link) {
/* Locate the equivalent incremental vertex */
for (v2 = s->i_uvtx; v2 != NULL; v2 = v2->link) {
for (e = 0; e <= di; e++) {
if (v1->nix[e] != v2->nix[e])
break;
}
if (e > di)
break; /* Found it */
}
if (v2 == NULL) {
printf("Missing vertex no %d comb %s\n",v1->no,pcomb(di,v1->nix));
fail = 1;
continue;
}
/* Check the vertex location */
for (e = 0; e < di; e++) {
if (fabs(v1->p[e] - v2->p[e]) > 1e-5) {
break;
}
}
if (e < di) {
printf("Vertex no %d (%d) comb %s in different location %s, should be %s\n",v1->no,v2->no,pcomb(di,v1->nix),ppos(di,v2->p),ppos(di,v1->p));
fail = 1;
}
/* Check the eserr */
if (fabs(v1->eserr - v2->eserr) > 1e-3) {
printf("Vertex no %d (%d) comb %s has different eserr %f, should be %f\n",v1->no,v2->no,pcomb(di,v1->nix),v2->eserr,v1->eserr);
fail = 1;
}
/* Check setmask */
if (!_sm_equal(s, &v1->vm, &v2->vm)) {
printf("Vertex no %d (%d) comb %s has different vm %s, should be %s\n",v1->no,v2->no,pcomb(di,v1->nix),psm(s,&v2->vm),psm(s,&v1->vm));
fail = 1;
}
/* Check that the vertex nets are the same */
for (i = 0; i < v1->nnv; i++) {
vtx *vv1 = v1->nv[i];
for (j = 0; j < v2->nnv; j++) {
vtx *vv2 = v2->nv[j];
for (e = 0; e <= di; e++) {
if (vv1->nix[e] != vv2->nix[e])
break;
}
if (e > di)
break; /* Found it */
}
if (j >= v2->nnv) {
printf("Vertex no %d comb %s, i_ missing neighbour no %d comb %s\n",v1->no,pcomb(di,v1->nix),vv1->no,pcomb(di,vv1->nix));
fail = 1;
}
}
for (j = 0; j < v2->nnv; j++) {
vtx *vv2 = v2->nv[j];
for (i = 0; i < v1->nnv; i++) {
vtx *vv1 = v1->nv[i];
for (e = 0; e <= di; e++) {
if (vv1->nix[e] != vv2->nix[e])
break;
}
if (e > di)
break; /* Found it */
}
if (i >= v1->nnv) {
printf("Vertex no %d comb %s, i_ extra neighbour no (%d) comb %s\n",v1->no,pcomb(di,v1->nix),vv2->no,pcomb(di,vv2->nix));
fail = 1;
}
}
}
/* For each incremental vertex, check that there is a corresponding re-seeded vertex */
for (v2 = s->i_uvtx; v2 != NULL; v2 = v2->link) {
for (v1 = s->uvtx; v1 != NULL; v1 = v1->link) {
for (e = 0; e <= di; e++) {
if (v1->nix[e] != v2->nix[e])
break;
}
if (e > di)
break; /* Found it */
}
if (v1 == NULL) {
printf("Extra vertex no (%d) comb %s\n",v2->no,pcomb(di,v2->nix));
fail = 1;
}
}
if (fail)
printf("Failed to verify incremental vertexes against re-seeded:\n");
else
printf("Successfully verified incremental vertexes against re-seeded\n");
return fail;
}
#endif /* SANITY_RESEED_AFTER_FIXUPS */
/* ------------------------------------------------------------------- */
/* Do an exaustive, very slow check for missing vertexes */
/*
This may be really, really, really slow.
For every possible combination of di+1 nodes,
locate the corresponding vertex. If it is
locatable, check that no other node is closer to it.
If it meets these conditions, then check that it is in the veronoi surface.
*/
static void check_for_missing_vertexes(ofps *s) {
int e, di = s->di;
vtx *vx;
COMBO(co, MXPD+1, di+1, s->np + s->nbp); /* di-1 out of neighbor nodes combination counter */
nodecomb vv;
int lsc = -100;
int isok = 1;
printf("Doing exaustive check for missing vertexes:\n");
/* Mark all the vertexes so that we can tell if any are missed. */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
vx->sch = 0;
}
CB_INIT(co);
while (!CB_DONE(co)) {
int rl = 0;
memset((void *)&vv, 0, sizeof(nodecomb));
for (e = 0; e <= di; e++) {
vv.nix[e] = co[e] - s->nbp;
if (vv.nix[e] >= 0)
rl = 1;
}
if (rl == 0)
goto next_comb; /* No real nodes */
vv.vv = NULL;
vv.ceperr = 1e100;
sort_nix(s, vv.nix);
printf("Comb %s\n",pcomb(di, vv.nix));
if (lsc != vv.nix[di]) {
fprintf(stderr,"digit %d\n",vv.nix[di]);
lsc = vv.nix[di];
}
if (position_vtx(s, &vv, 0, 0, 0) == 0) {
int ix;
double eperr;
node *nn;
printf(" Located at %s (%s), eperr %f\n",ppos(di,vv.p),ppos(di,vv.v),vv.eperr);
/* Check that the point is not out of gamut */
if (ofps_in_dev_gamut(s, vv.p, NULL) < -s->surftol) {
printf(" vertex is out of gamut\n");
goto not_valid;
}
/* Check that no other vertex is closer */
for (ix = 0; ix < s->np; ix++) {
for (e = 0; e <= di; e++) {
if (vv.nix[e] == ix)
break;
}
if (e <= di)
continue; /* Is a parent */
nn = s->n[ix];
eperr = ofps_comp_eperr(s, NULL, nn->v, nn->p, vv.v, vv.p, 0);
printf(" eperr to ix %d is %f\n",nn->ix,eperr);
if (eperr < vv.eperr) {
printf("vertex is closer to node ix %d\n",nn->ix);
break;
}
}
if (ix >= s->np) {
printf("Point %s is valid\n",pcomb(di,vv.nix));
/* see if we've created it */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
for (e = 0; e <= di; e++) {
if (vx->nix[e] != vv.nix[e])
break;
}
if (e > di)
break; /* Found it */
}
if (vx == NULL) {
printf("Can't find vertex %s at %s (%s)\n",pcomb(di,vv.nix),ppos(di,vv.p),ppos(di,vv.v));
fprintf(stderr,"Can't find vertex %s at %s (%s)\n",pcomb(di,vv.nix),ppos(di,vv.p),ppos(di,vv.v));
isok = 0;
} else {
vx->sch = 1;
printf("Found vertex no %d nix %s at %s (%s) OK\n",vx->no,pcomb(di,vv.nix),ppos(di,vv.p),ppos(di,vv.v));
fprintf(stderr,"Found vertex no %d nix %s at %s (%s) OK\n",vx->no,pcomb(di,vv.nix),ppos(di,vv.p),ppos(di,vv.v));
}
}
not_valid:;
} else {
printf(" Failed to locate %s\n",pcomb(di,vv.nix));
}
next_comb:;
CB_INC(co);
}
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
if (vx->sch == 0) {
for (e = 0; e <= di; e++) {
if (vx->nix[e] < -s->nbp) /* involves inside or outside fake point */
break;
}
if (e <= di)
continue; /* Ignore */
printf("Extra vertex no %d nix %s at %s (%s) OK\n",vx->no,pcomb(di,vx->nix),ppos(di,vx->p),ppos(di,vx->v));
fprintf(stderr,"Extra vertex no %d nix %s at %s (%s) OK\n",vx->no,pcomb(di,vx->nix),ppos(di,vx->p),ppos(di,vx->v));
isok = 0;
}
}
if (isok) {
printf("Check for missing veftexes is OK\n");
fprintf(stderr,"Check for missing veftexes is OK\n");
} else {
printf("Check for missing veftexes FAILED\n");
fprintf(stderr,"Check for missing veftexes FAILED\n");
}
}
/* ------------------------------------------------------------------- */
/* Check the veronoi to check that no node other than the parent */
/* node is closer to any vertex. */
/* return nz if there is a problem */
static int check_vertex_closest_node(ofps *s) {
int i, e, di = s->di;
node *nn, *pp;
vtx *vx;
/* Check that no node other than a parent is closer to any vertex */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
double ceperr;
if (vx->ofake)
continue;
/* Check if the vertex position is clipped by a gamut boundary. */
for (i = 0; i < s->nbp; i++) {
pleq *vp = &s->gpeqs[i];
double v;
pp = s->n[-1-i];
#ifdef INDEP_SURFACE
/* Check if this vertex is visible to this node */
if (sm_vtx_node(s, vx, pp) == 0) {
continue; /* It's hidden */
}
#endif /* INDEP_SURFACE */
for (v = vp->pe[di], e = 0; e < di; e++)
v += vp->pe[e] * vx->p[e];
if (v > 2.0 * NUMTOL) {
/* Check whether pp is already a parent of the node */
for (e = 0; e <= di; e++) {
if (pp->ix == vx->nix[e])
break;
}
if (e <= di)
continue; /* It is */
#ifdef DEBUG
printf("Vertex %d parents %s is clipped by boundary node %d by %e\n", vx->no,pcomb(di,vx->nix),pp->ix,v);
#endif
warning("Vertex %d parents %s is clipped by boundary node %d by %e", vx->no,pcomb(di,vx->nix),pp->ix,v);
return 1;
}
}
/* locate the nearest node to the vertex */
if ((nn = ofps_findclosest_node(s, &ceperr, vx)) == NULL)
continue;
/* See if it is closer than the parent nodes */
if ((vx->eperr - ceperr) < 2.0 * NUMTOL)
continue; /* No it's not */
/* Check whether nn is already a parent of the node */
for (e = 0; e <= di; e++) {
if (nn->ix == vx->nix[e])
break;
}
if (e <= di)
continue; /* A parent */
#ifdef DEBUG
printf("Vertex %d is closer to %d (%f) than parent nodes %s (%f) by %e\n",vx->no,nn->ix,ceperr,pcomb(di,vx->nix),vx->eperr, ceperr - vx->eperr);
#endif
warning("Vertex %d is closer to %d (%f) than parent nodes %s (%f) by %e",vx->no,nn->ix,ceperr,pcomb(di,vx->nix),vx->eperr, ceperr - vx->eperr);
return 1;
}
fflush(stdout);
return 0;
}
/* ------------------------------------------------------------------- */
#if defined(DEBUG) || defined(DUMP_PLOT) || defined (SANITY_CHECK_CONSISTENCY) || defined(DUMP_STRUCTURE)
/* Do some sanity checking on the points */
static void
sanity_check(
ofps *s,
int check_nodelists /* nz to check node lists */
) {
int i, j, k, e, di = s->di;
vtx *vx1, *vx2;
int fail = 0; /* 0 = pass, 1 = soft fail, 2 = hard fail */
#ifdef DEBUG
printf("Running sanity check...\n");
#endif
/* See if any of the sample nodes are near the same location */
for (i = 0; i < (s->np-1); i++) {
node *p1 = s->n[i];
for (j = i+1; j < s->np; j++) {
node *p2 = s->n[j];
double rad;
for (rad = 0.0, e = 0; e < di; e++) {
double tt = p1->p[e] - p2->p[e];
rad += tt * tt;
}
rad = sqrt(rad);
if (rad < 1e-5) {
#ifdef DEBUG
printf("Nodes ix %d and ix %d are at %s and %s\n", i,j,ppos(di,p1->p),ppos(di,p2->p));
#endif
fail = 2;
}
}
}
/* See if any of the vertexes have the same node combinations */
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link)
vx1->sch = 0;
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
if (vx1->sch)
continue;
for (vx2 = vx1->link; vx2 != NULL; vx2 = vx2->link) {
if (vx2->sch)
continue;
for (e = 0; e <= di; e++) {
if (vx1->nix[e] != vx2->nix[e])
break;
}
if (e > di) {
vx1->sch = vx2->sch = 1; /* Don't do these again */
#ifdef DEBUG
printf("Vertex ix %d and ix %d have same nix %s\n", vx1->no,vx2->no,pcomb(di,vx1->nix));
#endif
fail = 2;
}
}
}
/* See if any of the vertexes are at the same location */
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link)
vx1->sch = 0;
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
if (vx1->sch)
continue;
for (vx2 = vx1->link; vx2 != NULL; vx2 = vx2->link) {
double rad;
if (vx2->sch)
continue;
for (rad = 0.0, e = 0; e < di; e++) {
double tt = vx1->p[e] - vx2->p[e];
rad += tt * tt;
}
rad = sqrt(rad);
if (rad < 1e-10) {
vx1->sch = vx2->sch = 1; /* Don't do these again */
#ifdef DEBUG
printf("Vertex no %d nix %s vm %s and no %d nix %s vm %s are at %s and %s", vx1->no,pcomb(di,vx1->nix),psm(s,&vx1->vm),vx2->no,pcomb(di,vx2->nix),psm(s,&vx2->vm),ppos(di,vx1->p),ppos(di,vx2->p));
if (fabs(vx1->eperr - vx2->eperr) > 1e-5)
printf(" and errs %f %f\n",vx1->eperr,vx2->eperr);
else
printf("\n");
#endif
/* See if the two vertexes are both visible to each other */
if (sm_vtx_vtx(s, vx1, vx2) != 0) {
fail = 2;
}
}
}
}
/* See if any of the nodes and vertexes are near the same location */
for (i = 0; i < s->np; i++) {
node *p1 = s->n[i];
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
double rad;
for (rad = 0.0, e = 0; e < di; e++) {
double tt = p1->p[e] - vx1->p[e];
rad += tt * tt;
}
rad = sqrt(rad);
if (rad < 1e-5) {
#ifdef DEBUG
printf("Node ix %d and Vertex no %d are at %s and %s%s", i,vx1->no,ppos(di,p1->p),ppos(di,vx1->p),vx1->ghost ? " (ghost)" : "");
if (vx1->eperr > 1e-5)
printf(" and err %f\n",vx1->eperr);
else
printf("\n");
#endif
if (vx1->ghost == 0)
fail = 1;
}
}
}
/* Check every node appears in at least one vertex */
for (i = 0; i < s->np; i++) { /* For all nodes */
node *p1 = s->n[i];
vtx *vx;
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
for (e = 0; e <= di; e++) {
if (vx->nix[e] == p1->ix)
break; /* yes */
}
if (e <= di)
break; /* yes */
}
if (vx == NULL) {
#ifdef DEBUG
printf("Node ix %d has no vertexes that refer to it\n", p1->ix);
#endif
fail = 2;
}
}
if (check_nodelists) {
/* See if any vertexes do not appear in their constituent nodes */
/* vertex list, or whether verexes nodes don't appear in neighbour list. */
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
for (e = 0; e <= di; e++) {
int ix = vx1->nix[e];
node *pp = s->n[ix];
for (j = 0; j < pp->nvv; j++) {
if (pp->vv[j] == vx1)
break;
}
if (j >= pp->nvv) {
#ifdef DEBUG
printf("Vertex no %d nix %s doesn't appear in node ix %d\n", vx1->no,pcomb(di,vx1->nix),pp->ix);
#endif
fail = 2;
}
}
}
}
/* Check that every vertex of a node contains that node. */
for (i = 0; i < s->np; i++) { /* For all nodes */
node *p1 = s->n[i];
for (j = 0; j < p1->nvv; j++) { /* For all its vertexes */
vtx *vx = p1->vv[j];
for (e = 0; e <= di; e++) { /* All vertexes parent nodes */
int ix = vx->nix[e];
node *pp = s->n[ix];
if (ix == p1->ix)
break;
}
if (e > di) {
#ifdef DEBUG
printf("Node ix %d has vtx no %d nix %s that doesn't contain node\n", p1->ix, vx->no,pcomb(di,vx->nix));
#endif
fail = 2;
}
}
}
if (check_nodelists) {
/* Check that a node contains as neighbours all the parent */
/* nodes of its vertexes */
for (i = 0; i < s->np; i++) { /* For all nodes */
node *p1 = s->n[i];
for (j = 0; j < p1->nvv; j++) { /* For all its vertexes */
vtx *vx = p1->vv[j];
for (e = 0; e <= di; e++) {
int ix = vx->nix[e];
node *pp = s->n[ix];
if (ix == p1->ix)
continue; /* Neighbours don't include self */
for (k = 0; k < p1->nvn; k++) {
if (p1->vn[k] == ix)
break;
}
if (k >= p1->nvn) {
#ifdef DEBUG
printf("Node ix %d has vtx no %d nix %s where neighbour ix %d is missing\n", p1->ix, vx->no,pcomb(di,vx->nix),ix);
#endif
fail = 2;
}
}
}
}
}
/* Check that the vertex net is correct */
{
int ff, f, e, di = s->di;
vtx *vx1, *vx2;
int nnv = 0;
int _nnv = 0;
struct _vtx **nv = NULL;
/* Do a brute force search to locate all this vertexes net neighbours */
for (vx1 = s->uvtx; vx1 != NULL; vx1 = vx1->link) {
nnv = 0; /* Clear the current list */
/* Search all other vertexes for neighbours */
for (vx2 = s->uvtx; vx2 != NULL; vx2 = vx2->link) {
int aa, bb, cc; /* Probable hit check */
int nnm, nmix;
if (vx1 == vx2)
continue;
#ifdef NEVER /* vertex net needs all neighbours ? */
#ifdef INDEP_SURFACE
if (sm_vtx_vtx(s, vx1, vx2) == 0)
continue;
#endif /* INDEP_SURFACE */
#endif
/* Use the nixm to quickly check if all but one parent node matches */
aa = vx1->nix[MXPD+2]; /* nixm */
bb = vx2->nix[MXPD+2]; /* nixm */
if ((aa & bb) == 0 || (cc = aa & ~bb, (cc & (cc-1)) != 0))
continue; /* It's certainly not */
/* Do an exact check of all except one node match */
for (nnm = ff = e = 0; e <= di; e++) {
for (f = ff; f <= di; f++) {
if (vx1->nix[e] == vx2->nix[f]) {
ff = f; /* Start from here next time */
break;
}
if (vx1->nix[e] > vx2->nix[f]) /* No point in looking further */
f = di;
}
if (f > di) { /* Didn't match */
if (++nnm > 1)
break;
nmix = e;
}
}
if (e <= di)
continue; /* No match */
if (nnm == 0) {
error("ofps: two vertexes have the same nodes !\n"
"no %d at %s nix %s\nno %d at %s nix %s",
vx1->no,ppos(di,vx1->p),pcomb(di,vx1->nix),
vx2->no,ppos(di,vx2->p),pcomb(di,vx2->nix));
}
if (nnv >= _nnv) {
_nnv = 2 * _nnv + 1;
if ((nv = (vtx **)realloc(nv, sizeof(vtx *) * _nnv)) == NULL)
error("ofps: realloc failed on node vertex pointers");
}
nv[nnv++] = vx2;
}
/* Now check that the vertex nets match */
for (i = 0; i < nnv; i++) {
for (j = 0; j < vx1->nnv; j++) {
if (nv[i] == vx1->nv[j])
break;
}
if (j >= vx1->nnv) {
printf("Vtx no %d is missing vtx no %d from net\n",vx1->no,nv[i]->no);
fail = 2;
}
}
for (j = 0; j < vx1->nnv; j++) {
for (i = 0; i < nnv; i++) {
if (nv[i] == vx1->nv[j])
break;
}
if (i >= nnv) {
printf("Vtx no %d has extra vtx no %d in net\n",vx1->no,vx1->nv[j]->no);
fail = 2;
}
}
}
}
if (fail) {
if (fail == 1)
warning("Internal consistency check failed (soft)");
else
warning("Internal consistency check failed");
#ifdef DEBUG
if (fail == 1)
printf("Internal consistency check failed (soft)\n");
else
printf("Internal consistency check failed\n");
fflush(stdout);
#endif
#ifdef SANITY_CHECK_CONSISTENCY_FATAL
if (fail == 2)
error("Internal consistency check failed");
#endif
}
#ifdef SANITY_CHECK_EXAUSTIVE_SEARCH_FOR_VERTEXES
check_for_missing_vertexes(s);
#endif
}
#endif /* SANITY_CHECK_CONSISTENCY */
#if defined(DEBUG) || defined(DUMP_STRUCTURE)
/* ------------------------------------------------------------------- */
/* Dump the node & vertex relationship */
static void
dump_node_vtxs(
ofps *s,
int check_nodelists
) {
int i, j, e, di = s->di;
vtx *vx;
printf("\n");
printf("Dumping current state...\n");
/* Dump node information */
for (i = -s->gnp; i < s->np; i++) {
node *p1 = s->n[i];
printf("Node ix %d, pos %s, mask 0x%x, asm %s\n",p1->ix,ppos(di, p1->p),p1->pmask,psm(s,&s->sc[p1->pmask].a_sm));
}
printf("\n");
/* Dump vertex information */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
if (vx->ofake == 0)
printf("Vertex no %d, pmask 0x%x, cmask 0x%x, vm %s\n pos %s, nix %s, eperr = %s, eserr = %s\n",vx->no,vx->pmask,vx->cmask,psm(s,&vx->vm), ppos(di, vx->p), pcomb(di,vx->nix), peperr(vx->eperr), peperr(vx->eserr));
else
printf("Vertex no %d, pmask 0x%x, cmask 0x%x, vm %s, nix %s, eperr = %s, eserr = %s (ofake)\n",vx->no,vx->pmask,vx->cmask,psm(s,&vx->vm), pcomb(di,vx->nix), peperr(vx->eperr), peperr(vx->eserr));
}
printf("\n");
/* Dump vertex and associated vertex information */
for (vx = s->uvtx; vx != NULL; vx = vx->link) {
printf("Vertex no %d has Vtx net:",vx->no);
for (j = 0; j < vx->nnv; j++) {
vtx *vx2 = vx->nv[j];
printf(" %d",vx2->no);
}
printf("\n");
}
printf("\n");
/* Dump node and associated vertex information */
for (i = -s->nbp; i < s->np; i++) {
node *p1 = s->n[i];
printf("Node ix %d, pos %s, mask 0x%x, a_sm %s:\n",p1->ix,ppos(di, p1->p),p1->pmask,psm(s,&s->sc[p1->pmask].a_sm));
for (j = 0; j < p1->nvv; j++) {
vtx *vx = p1->vv[j];
if (vx->ofake == 0)
printf(" Vtx no %d pmask 0x%x cmask 0x%x vm %s pos %s nix %s eserr %s\n",vx->no,vx->pmask,vx->cmask,psm(s,&vx->vm),ppos(di, vx->p), pcomb(di, vx->nix), peperr(vx->eserr));
else
printf(" Vtx no %d pmask 0x%x cmask 0x%x vm %s nix %s eserr %s (ofake)\n",vx->no,vx->pmask,vx->cmask,psm(s,&vx->vm),pcomb(di,vx->nix), peperr(vx->eserr));
}
for (j = 0; j < p1->nvn; j++) {
int ix = p1->vn[j];
if (ix >= 0) {
node *n1 = s->n[ix];
printf(" Assoc. node ix %d pos %s\n",ix,ppos(di, n1->p));
} else {
printf(" Assoc. node ix %d\n",ix);
}
}
}
printf("\n");
sanity_check(s, check_nodelists);
fflush(stdout);
}
#ifdef NEVER
/* Special dump the node & vertex relationship, */
/* for comparing with "good" output. */
/* Deal with vertexe order and numbering. */
/* Note that the "new" "bad" code needs ofake vertexes */
/* to work, wheras the "old" "good" code doesn't, so */
/* skip reporting ofake vetexes. */
static void
dump_node_vtxs2(
ofps *s,
char *com
) {
int i, j, e, di = s->di;
vtx *vx;
FILE *fp;
static int cc = 0;
int showofake = 1;
vtx **vlist;
if (cc == 0) {
if ((fp = fopen("bad.log","w")) == NULL)
error("Unable to open file '%s'\n","bad.log");
cc = 1;
} else {
if ((fp = fopen("bad.log","a")) == NULL)
error("Unable to open file '%s'\n","bad.log");
}
fprintf(fp,"\n");
fprintf(fp,"Dumping current state (%s) ...\n",com);
/* Dump node information */
for (i = -s->gnp; i < s->np; i++) {
node *p1 = s->n[i];
fprintf(fp,"Node ix %d, pos %s, mask 0x%x, asm %s\n",p1->ix,ppos(di, p1->p),p1->pmask,psm(s,&s->sc[p1->pmask].a_sm));
}
fprintf(fp,"\n");
/* Sort the vertexes by their nix */
{
int scl = s->gnp + s->np;
int nv;
int nused;
for (nused = 0, vx = s->uvtx; vx != NULL; vx = vx->link)
nused++;
if (nused != s->nv) error("s->nv %d doesn't match uvtx list %d",s->nv,nused);
//printf("~1 number of vertexes = %d\n",s->nv);
if ((vlist = (vtx **)malloc(sizeof(vtx *) * s->nv)) == NULL)
error ("ofps: malloc failed on sorted vertex list");
for (i = nv = 0, vx = s->uvtx; vx != NULL; vx = vx->link, i++) {
if (!showofake && vx->ofake)
continue;
vlist[nv++] = vx;
/* Convert nix into sort index */
vx->sch = 0;
for (e = 0; e <= di; e++) {
vx->sch = scl * vx->sch + (vx->nix[e] + s->gnp);
}
//printf("~1 nix %s ix %d\n",pcomb(di,vx->nix),vx->sch);
//fflush(stdout);
}
/* Sort */
#define HEAP_COMPARE(A,B) ((A)->sch < (B)->sch)
HEAPSORT(vtx *, vlist, nv);
#undef HEAP_COMPARE
for (i = 0; i < nv; i++) {
vx = vlist[i];
vx->sch = i; /* Sorted index */
}
/* Dump vertex information */
for (i = 0; i < nv; i++) {
vx = vlist[i];
fprintf(fp,"Vertex no %d, pmask 0x%x, cmask 0x%x, vm %s\n pos %s, nix %s, eserr = %s\n",vx->sch,vx->pmask,vx->cmask,psm(s,&vx->vm), ppos(di, vx->p), pcomb(di,vx->nix), peperr(vx->eserr));
}
fprintf(fp,"\n");
free(vlist);
}
/* Dump node and associated vertex information */
for (i = -s->nbp; i < s->np; i++) {
node *p1 = s->n[i];
vtx **vv;
int nvv;
int *vn;
fprintf(fp,"Node ix %d, pos %s, mask 0x%x, a_sm %s\n",p1->ix,ppos(di, p1->p),p1->pmask,psm(s,&s->sc[p1->pmask].a_sm));
/* Display the vertexes in order */
if ((vv = (vtx **)malloc(sizeof(vtx *) * p1->nvv)) == NULL)
error ("ofps: malloc failed on sorted vertex list");
for (nvv = j = 0; j < p1->nvv; j++) {
if (!showofake && p1->vv[j]->ofake)
continue;
vv[nvv++] = p1->vv[j];
}
#define HEAP_COMPARE(A,B) ((A)->sch < (B)->sch)
HEAPSORT(vtx *, vv, nvv);
#undef HEAP_COMPARE
for (j = 0; j < nvv; j++) {
vtx *vx = vv[j];
fprintf(fp," Vtx no %d pmask 0x%x cmask 0x%x vm %s pos %s nix %s eserr %s\n",vx->sch,vx->pmask,vx->cmask,psm(s,&vx->vm),ppos(di, vx->p), pcomb(di, vx->nix), peperr(vx->eserr));
}
free(vv);
/* Sort the nodes to be in order */
if ((vn = (int *)malloc(sizeof(int) * p1->nvn)) == NULL)
error ("ofps: malloc failed on sorted vertex list");
for (j = 0; j < p1->nvn; j++)
vn[j] = p1->vn[j];
#define HEAP_COMPARE(A,B) ((A) < (B))
HEAPSORT(int, vn, p1->nvn);
#undef HEAP_COMPARE
for (j = 0; j < p1->nvn; j++) {
int ix = vn[j];
if (ix >= 0) {
node *n1 = s->n[ix];
fprintf(fp," Assoc. node ix %d pos %s\n",ix,ppos(di, n1->p));
} else {
fprintf(fp," Assoc. node ix %d\n",ix);
}
}
free(vn);
}
printf("\n");
fflush(fp);
fclose(fp);
}
#endif /* NEVER */
#endif /* DEBUG || DUMP_PLOT || DUMP_STRUCTURE */
/* --------------------------------------------------------------- */
#ifdef DUMP_FERR /* Create .tiff of dnsq function error */
/* Draw a line in the output diagnostic raster */
static int
show_line(
ofps *s, /* ofps object */
int x1, int y1, int x2, int y2, /* line start and end points */
unsigned char rgb[3], /* Color */
unsigned char *base, /* Raster base of line */
int pitch,
int width,
int height
) {
unsigned char *pp;
int ow = width, oh = height; /* width and height of raster for clipping */
int dx, dy; /* Line deltas */
int adx, ady; /* Absolute deltas */
int e, k1, k2; /* Error and axial/diagonal error change values */
int m1,m2; /* axial/diagonal coordinate change values */
int ll; /* Line length */
/* Do a crude clip */
if (x1 < 0)
x1 = 0;
if (x1 >= ow)
x1 = ow-1;
if (x2 < 0)
x2 = 0;
if (x2 >= ow)
x2 = ow-1;
if (y1 < 0)
y1 = 0;
if (y1 >= oh)
y1 = oh-1;
if (y2 < 0)
y2 = 0;
if (y2 >= oh)
y2 = oh-1;
/* calculate the standard constants */
dx = x2 - x1;
dy = y2 - y1;
if(dx < 0) {
m1 = -3; /* x is going backwards */
adx = -dx; /* make this absolute */
} else {
m1 = 3; /* x is going forwards */
adx = dx;
}
e = 0;
if(dy < 0) {
m2 = -pitch; /* y is going upwards (decreasing) */
ady = -dy; /* make this absolute */
e = -1; /* make lines retraceable */
} else {
m2 = pitch; /* y is going downwards (increasing) */
ady = dy;
}
/* m1 has been set to x increment, m2 to y increment */
m2 += m1; /* make m2 the diagonal address increment */
/* and m1 the x axial inrement */
if(adx > ady) { /* x is driven */
ll = adx;
k1 = 2 * ady;
k2 = 2 * (ady - adx);
e += k1 - adx;
} else {
ll = ady;
k1 = 2 * adx;
k2 = 2 * (adx - ady);
e += k1 - ady;
m1 = m2 - m1; /* Make m1 the y increment */
}
/* Start pixel of line */
pp = base + y1 * pitch + 3 * x1;
ll++; /* Draw start and end point */
while( ll > 0) {
while(e < 0 && ll > 0) {
pp[0] = rgb[0];
pp[1] = rgb[1];
pp[2] = rgb[2];
pp += m1;
e += k1;
ll--;
}
while(e >= 0 && ll > 0) {
pp[0] = rgb[0];
pp[1] = rgb[1];
pp[2] = rgb[2];
pp += m2;
e += k2;
ll--;
}
}
return 0;
}
/* Dump a TIFF of the dnsq function values for a given point/plane set. */
static void
dump_dnsqe(
ofps *s,
char *fname,
int *nix,
vopt_cx *cx
) {
int i, j, e, di = s->di;
unsigned char *base, *pa, col[2][3];
int width = WIDTH;
int height = HEIGHT;
int pitch = width * 3;
int x, y;
TIFF *tif;
double pos[MXPD], fval[MXPD];
double angle, mag;
printf("Dumping dnsqe error for combination %s\n",pcomb(di,nix));
if ((tif = TIFFOpen(fname, "w")) == NULL) {
fprintf(stderr,"Failed to open output TIFF file '%s'\n",fname);
exit (-1);
}
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
/* allocate a raster */
if ((base = (unsigned char *)malloc(sizeof(unsigned char) * height * pitch)) == NULL)
error ("ofps: malloc failed on diagnostic raster");
for (y = 0; y < height; y++) {
pos[1] = 1.0 - y/(height-1.0);
pos[1] = 1.4 * pos[1] - 0.2;
pa = base + y * pitch;
/* Fill in pa[] with colors for this line */
for (x = 0; x < width; x++) {
double ss;
unsigned char *dp;
double beserr, eserr;
double bf, rgb[3];
double oog = 1.0;
double escale = 10.0; /* Error value scaling */
dp = pa + x * 3;
pos[0] = x/(width-1.0);
pos[0] = 1.4 * pos[0] - 0.2;
dp[0] = dp[1] = dp[2] = 255;
//printf("~1 doing %d %d pos %f %f\n",x,y,pos[0],pos[1]);
/* Se if the sample is in gamut */
for (ss = 0.0, e = 0; e < s->di; e++) {
if (pos[e] < s->imin[e]
|| pos[e] > s->imax[e])
break;
ss += pos[e];
}
if (e < s->di || ss > (s->ilimit + ILIMITEPS)) {
oog = 0.7; /* Show gamut boundary */
}
#ifdef NEVER /* Test colors out */
fval[0] = pos[0] * 2.0 * escale - escale;
fval[1] = pos[1] * 2.0 * escale - escale;
#else
/* Lookup the function value here */
dnsq_solver(cx, di, pos, fval, 0);
#endif
/* Turn the two values into colors. */
for (e = 0; e < di; e++) {
fval[e] = (fval[e] / escale);
if (fval[e] >= 0.0)
fval[e] = pow(fval[e], 0.5);
else
fval[e] = -pow(-fval[e], 0.5);
}
/* Convert to angle and magnitude */
angle = 180.0/3.1415926 * atan2(fval[0], fval[1]);
if (angle < 0.0)
angle += 360.0;
else if (angle > 360.0)
angle -= 360.0;
mag = sqrt(fval[0] * fval[0] + fval[1] * fval[1]);
if (mag > 1.0)
mag = 1.0;
rgb[0] = rgb[1] = rgb[1] = 0.0;
if (angle < 120.0) { /* red to green */
bf = angle / 120.0;
rgb[0] = 1.0 - bf;
rgb[1] = bf;
rgb[2] = 0.0;
} else if (angle < 240.0) { /* green to blue */
bf = (angle - 120.0) / 120.0;
rgb[0] = 0.0;
rgb[1] = 1.0 - bf;
rgb[2] = bf;
} else { /* blue to red */
bf = (angle - 240.0) / 120.0;
rgb[0] = bf;
rgb[1] = 0.0;
rgb[2] = 1.0 - bf;
}
/* Scale to black with magnitude */
for (e = 0; e < 3; e++) {
rgb[e] = 1.0 - rgb[e];
rgb[e] = (1.0 - mag) * 0.0 + mag * rgb[e];
}
for (e = 0; e < 3; e++)
dp[e] = (int)(255.0 * oog * rgb[e] + 0.5);
}
}
/* Show the path the dnsq sampled */
col[0][0] = col[0][1] = 255, col[0][2] = 128;
col[1][0] = 128, col[1][1] = col[1][2] = 255;
for (i = 0; i < (cx->nl-1); i++) {
//printf("~1 line %d: %f %f -> %f %f\n",i, cx->clist[i].p[0], cx->clist[i].p[1], cx->clist[i+1].p[0], cx->clist[i+1].p[1]);
show_line(s,
(int)(((cx->clist[i].p[0] + 0.2) / 1.4) * (width - 1.0) + 0.5),
(int)((1.0 - ((cx->clist[i].p[1] + 0.2) / 1.4)) * (height - 1.0) + 0.5),
(int)(((cx->clist[i+1].p[0] + 0.2) / 1.4) * (width - 1.0) + 0.5),
(int)((1.0 - ((cx->clist[i+1].p[1] + 0.2) / 1.4)) * (height - 1.0) + 0.5),
col[i & 1], base, pitch, width, height);
}
/* Write the raster out */
for (y = 0; y < height; y++) {
pa = base + y * pitch;
if (TIFFWriteScanline(tif, (tdata_t)pa, y, 0) < 0) {
fprintf(stderr,"WriteScanline Failed at line %d\n",y);
exit (-1);
}
}
(void) TIFFClose(tif);
free(base);
}
#endif /* DUMP_FERR */
/* --------------------------------------------------------------- */
#ifdef NEVER
/* Compute an aproximate bounding shere, and use */
/* the center of it as the start point. */
double radsq = -1.0; /* Span/radius squared */
double rad;
double sum;
int i, j;
int bi = 0, bj = 0;
/* Find the two vectors that are farthest apart. Brute force search */
/* Also track the device position for the points used to define the shere */
for (i = 0; i < (ii-1); i++) {
for (j = i+1; j < ii; j++) {
for (sum = 0.0, e = 0; e < di; e++) {
double tt = cx.nds[i]->p[e] - cx.nds[j]->p[e];
sum += tt * tt;
}
if (sum > radsq) {
radsq = sum;
bi = i;
bj = j;
}
}
}
/* Set initial bounding sphere */
for (e = 0; e < di; e++)
atp[e] = 0.5 * (cx.nds[bi]->p[e] + cx.nds[bj]->p[e]);
radsq /= 4.0; /* diam^2 -> rad^2 */
rad = sqrt(radsq);
/* Go though all the points again, expanding sphere if necessary */
for (i = 0; i < ii; i++) {
if (i == bi || i == bj)
continue;
/* Compute distance squared of vertex to bounding sphere center */
for (sum = 0.0, e = 0; e < di; e++) {
double tt = cx.nds[i]->p[e] - atp[e];
sum += tt * tt;
}
if (sum > radsq) {
double tt;
sum = sqrt(sum) + 1e-10; /* Radius to point */
rad = 0.5 * (rad + sum);
radsq = rad * rad;
tt = sum - rad;
for (e = 0; e < di; e++)
atp[e] = (rad * atp[e] + tt * cx.nds[i]->p[e])/sum;
}
}
/* Given two sample point indexes, compute the plane between them. */
/* (This will fail with a divide by zero error if two points are coincident) */
static void comp_pleq(ofps *s, pleq *vp, int ix1, int ix2) {
node *p0 = s->n[ix1], *p1 = s->n[ix2];
int e, di = s->di;
double cp[MXPD];
double sum = 0.0;
/* Compute plane normal from ix1 to ix2 */
for (e = 0; e < di; e++) {
double tt = p1->p[e] - p0->p[e];
vp->pe[e] = tt;
sum += tt * tt;
}
sum = sqrt(sum);
/* Normalise it */
for (e = 0; e < di; e++)
vp->pe[e] /= sum;
/* Compute mid point */
for (e = 0; e < di; e++)
cp[e] = 0.5 * (p1->p[e] + p0->p[e]);
/* Compute the plane equation constant */
for (vp->pe[di] = 0.0, e = 0; e < di; e++)
vp->pe[di] -= vp->pe[e] * cp[e];
}
#endif // NEVER
|