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
|
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2018 DreamWorks Animation LLC
//
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
//
// Redistributions of source code must retain the above copyright
// and license notice and the following restrictions and disclaimer.
//
// * Neither the name of DreamWorks Animation nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
// LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
//
///////////////////////////////////////////////////////////////////////////
#include <cstdio> // for remove()
#include <fstream>
#include <sstream>
#include <cppunit/extensions/HelperMacros.h>
#include <openvdb/Exceptions.h>
#include <openvdb/Types.h>
#include <openvdb/math/Transform.h>
#include <openvdb/tools/ValueTransformer.h> // for tools::setValueOnMin(), et al.
#include <openvdb/tree/LeafNode.h>
#include <openvdb/io/Compression.h> // for io::RealToHalf
#include <openvdb/math/Math.h> // for Abs()
#include <openvdb/openvdb.h>
#include <openvdb/util/CpuTimer.h>
#include <openvdb/tools/LevelSetSphere.h>
#include <openvdb/tools/Prune.h>
#include <openvdb/tools/ChangeBackground.h>
#include <openvdb/tools/SignedFloodFill.h>
#include "util.h" // for unittest_util::makeSphere()
#define ASSERT_DOUBLES_EXACTLY_EQUAL(expected, actual) \
CPPUNIT_ASSERT_DOUBLES_EQUAL((expected), (actual), /*tolerance=*/0.0);
using ValueType = float;
using LeafNodeType = openvdb::tree::LeafNode<ValueType,3>;
using InternalNodeType1 = openvdb::tree::InternalNode<LeafNodeType,4>;
using InternalNodeType2 = openvdb::tree::InternalNode<InternalNodeType1,5>;
using RootNodeType = openvdb::tree::RootNode<InternalNodeType2>;
class TestTree: public CppUnit::TestFixture
{
public:
void setUp() override { openvdb::initialize(); }
void tearDown() override { openvdb::uninitialize(); }
CPPUNIT_TEST_SUITE(TestTree);
CPPUNIT_TEST(testChangeBackground);
CPPUNIT_TEST(testHalf);
CPPUNIT_TEST(testValues);
CPPUNIT_TEST(testSetValue);
CPPUNIT_TEST(testSetValueOnly);
CPPUNIT_TEST(testEvalMinMax);
CPPUNIT_TEST(testResize);
CPPUNIT_TEST(testHasSameTopology);
CPPUNIT_TEST(testTopologyCopy);
CPPUNIT_TEST(testIterators);
CPPUNIT_TEST(testIO);
CPPUNIT_TEST(testNegativeIndexing);
CPPUNIT_TEST(testDeepCopy);
CPPUNIT_TEST(testMerge);
CPPUNIT_TEST(testVoxelizeActiveTiles);
CPPUNIT_TEST(testTopologyUnion);
CPPUNIT_TEST(testTopologyIntersection);
CPPUNIT_TEST(testTopologyDifference);
CPPUNIT_TEST(testFill);
CPPUNIT_TEST(testSignedFloodFill);
CPPUNIT_TEST(testPruneInactive);
CPPUNIT_TEST(testPruneLevelSet);
CPPUNIT_TEST(testTouchLeaf);
CPPUNIT_TEST(testProbeLeaf);
CPPUNIT_TEST(testAddLeaf);
CPPUNIT_TEST(testAddTile);
CPPUNIT_TEST(testGetNodes);
CPPUNIT_TEST(testStealNodes);
CPPUNIT_TEST(testProcessBBox);
CPPUNIT_TEST(testStealNode);
CPPUNIT_TEST_SUITE_END();
void testChangeBackground();
void testHalf();
void testValues();
void testSetValue();
void testSetValueOnly();
void testEvalMinMax();
void testResize();
void testHasSameTopology();
void testTopologyCopy();
void testIterators();
void testIO();
void testNegativeIndexing();
void testDeepCopy();
void testMerge();
void testVoxelizeActiveTiles();
void testTopologyUnion();
void testTopologyIntersection();
void testTopologyDifference();
void testFill();
void testSignedFloodFill();
void testPruneLevelSet();
void testPruneInactive();
void testTouchLeaf();
void testProbeLeaf();
void testAddLeaf();
void testAddTile();
void testGetNodes();
void testStealNodes();
void testProcessBBox();
void testStealNode();
private:
template<typename TreeType> void testWriteHalf();
template<typename TreeType> void doTestMerge(openvdb::MergePolicy);
template<typename TreeTypeA, typename TreeTypeB> void doTestTopologyDifference();
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestTree);
void
TestTree::testChangeBackground()
{
const int dim = 128;
const openvdb::Vec3f center(0.35f, 0.35f, 0.35f);
const float
radius = 0.15f,
voxelSize = 1.0f / (dim-1),
halfWidth = 4,
gamma = halfWidth * voxelSize;
using GridT = openvdb::FloatGrid;
const openvdb::Coord inside(int(center[0]*dim), int(center[1]*dim), int(center[2]*dim));
const openvdb::Coord outside(dim);
{//changeBackground
GridT::Ptr grid = openvdb::tools::createLevelSetSphere<GridT>(
radius, center, voxelSize, halfWidth);
openvdb::FloatTree& tree = grid->tree();
CPPUNIT_ASSERT(grid->tree().isValueOff(outside));
ASSERT_DOUBLES_EXACTLY_EQUAL( gamma, tree.getValue(outside));
CPPUNIT_ASSERT(tree.isValueOff(inside));
ASSERT_DOUBLES_EXACTLY_EQUAL(-gamma, tree.getValue(inside));
const float background = gamma*3.43f;
openvdb::tools::changeBackground(tree, background);
CPPUNIT_ASSERT(grid->tree().isValueOff(outside));
ASSERT_DOUBLES_EXACTLY_EQUAL( background, tree.getValue(outside));
CPPUNIT_ASSERT(tree.isValueOff(inside));
ASSERT_DOUBLES_EXACTLY_EQUAL(-background, tree.getValue(inside));
}
{//changeLevelSetBackground
GridT::Ptr grid = openvdb::tools::createLevelSetSphere<GridT>(
radius, center, voxelSize, halfWidth);
openvdb::FloatTree& tree = grid->tree();
CPPUNIT_ASSERT(grid->tree().isValueOff(outside));
ASSERT_DOUBLES_EXACTLY_EQUAL( gamma, tree.getValue(outside));
CPPUNIT_ASSERT(tree.isValueOff(inside));
ASSERT_DOUBLES_EXACTLY_EQUAL(-gamma, tree.getValue(inside));
const float v1 = gamma*3.43f, v2 = -gamma*6.457f;
openvdb::tools::changeAsymmetricLevelSetBackground(tree, v1, v2);
CPPUNIT_ASSERT(grid->tree().isValueOff(outside));
ASSERT_DOUBLES_EXACTLY_EQUAL( v1, tree.getValue(outside));
CPPUNIT_ASSERT(tree.isValueOff(inside));
ASSERT_DOUBLES_EXACTLY_EQUAL( v2, tree.getValue(inside));
}
}
void
TestTree::testHalf()
{
testWriteHalf<openvdb::FloatTree>();
testWriteHalf<openvdb::DoubleTree>();
testWriteHalf<openvdb::Vec2STree>();
testWriteHalf<openvdb::Vec2DTree>();
testWriteHalf<openvdb::Vec3STree>();
testWriteHalf<openvdb::Vec3DTree>();
// Verify that non-floating-point grids are saved correctly.
testWriteHalf<openvdb::BoolTree>();
testWriteHalf<openvdb::Int32Tree>();
testWriteHalf<openvdb::Int64Tree>();
}
template<class TreeType>
void
TestTree::testWriteHalf()
{
using GridType = openvdb::Grid<TreeType>;
using ValueT = typename TreeType::ValueType;
ValueT background(5);
GridType grid(background);
unittest_util::makeSphere<GridType>(openvdb::Coord(64, 64, 64),
openvdb::Vec3f(35, 30, 40),
/*radius=*/10, grid,
/*dx=*/1.0f, unittest_util::SPHERE_DENSE);
CPPUNIT_ASSERT(!grid.tree().empty());
// Write grid blocks in both float and half formats.
std::ostringstream outFull(std::ios_base::binary);
grid.setSaveFloatAsHalf(false);
grid.writeBuffers(outFull);
outFull.flush();
const size_t fullBytes = outFull.str().size();
CPPUNIT_ASSERT_MESSAGE("wrote empty full float buffers", fullBytes > 0);
std::ostringstream outHalf(std::ios_base::binary);
grid.setSaveFloatAsHalf(true);
grid.writeBuffers(outHalf);
outHalf.flush();
const size_t halfBytes = outHalf.str().size();
CPPUNIT_ASSERT_MESSAGE("wrote empty half float buffers", halfBytes > 0);
if (openvdb::io::RealToHalf<ValueT>::isReal) {
// Verify that the half float file is "significantly smaller" than the full float file.
std::ostringstream ostr;
ostr << "half float buffers not significantly smaller than full float ("
<< halfBytes << " vs. " << fullBytes << " bytes)";
CPPUNIT_ASSERT_MESSAGE(ostr.str(), halfBytes < size_t(0.75 * fullBytes));
} else {
// For non-real data types, "half float" and "full float" file sizes should be the same.
CPPUNIT_ASSERT_MESSAGE("full float and half float file sizes differ for data of type "
+ std::string(openvdb::typeNameAsString<ValueT>()), halfBytes == fullBytes);
}
// Read back the half float data (converting back to full float in the process),
// then write it out again in half float format. Verify that the resulting file
// is identical to the original half float file.
{
openvdb::Grid<TreeType> gridCopy(grid);
gridCopy.setSaveFloatAsHalf(true);
std::istringstream is(outHalf.str(), std::ios_base::binary);
// Since the input stream doesn't include a VDB header with file format version info,
// tag the input stream explicitly with the current version number.
openvdb::io::setCurrentVersion(is);
gridCopy.readBuffers(is);
std::ostringstream outDiff(std::ios_base::binary);
gridCopy.writeBuffers(outDiff);
outDiff.flush();
CPPUNIT_ASSERT_MESSAGE("half-from-full and half-from-half buffers differ",
outHalf.str() == outDiff.str());
}
}
void
TestTree::testValues()
{
ValueType background=5.0f;
{
const openvdb::Coord c0(5,10,20), c1(50000,20000,30000);
RootNodeType root_node(background);
const float v0=0.234f, v1=4.5678f;
CPPUNIT_ASSERT(root_node.empty());
ASSERT_DOUBLES_EXACTLY_EQUAL(root_node.getValue(c0), background);
ASSERT_DOUBLES_EXACTLY_EQUAL(root_node.getValue(c1), background);
root_node.setValueOn(c0, v0);
root_node.setValueOn(c1, v1);
ASSERT_DOUBLES_EXACTLY_EQUAL(v0,root_node.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(v1,root_node.getValue(c1));
int count=0;
for (int i =0; i<256; ++i) {
for (int j=0; j<256; ++j) {
for (int k=0; k<256; ++k) {
if (root_node.getValue(openvdb::Coord(i,j,k))<1.0f) ++count;
}
}
}
CPPUNIT_ASSERT(count == 1);
}
{
const openvdb::Coord min(-30,-25,-60), max(60,80,100);
const openvdb::Coord c0(-5,-10,-20), c1(50,20,90), c2(59,67,89);
const float v0=0.234f, v1=4.5678f, v2=-5.673f;
RootNodeType root_node(background);
CPPUNIT_ASSERT(root_node.empty());
ASSERT_DOUBLES_EXACTLY_EQUAL(background,root_node.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(background,root_node.getValue(c1));
ASSERT_DOUBLES_EXACTLY_EQUAL(background,root_node.getValue(c2));
root_node.setValueOn(c0, v0);
root_node.setValueOn(c1, v1);
root_node.setValueOn(c2, v2);
ASSERT_DOUBLES_EXACTLY_EQUAL(v0,root_node.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(v1,root_node.getValue(c1));
ASSERT_DOUBLES_EXACTLY_EQUAL(v2,root_node.getValue(c2));
int count=0;
for (int i =min[0]; i<max[0]; ++i) {
for (int j=min[1]; j<max[1]; ++j) {
for (int k=min[2]; k<max[2]; ++k) {
if (root_node.getValue(openvdb::Coord(i,j,k))<1.0f) ++count;
}
}
}
CPPUNIT_ASSERT(count == 2);
}
}
void
TestTree::testSetValue()
{
const float background = 5.0f;
openvdb::FloatTree tree(background);
const openvdb::Coord c0( 5, 10, 20), c1(-5,-10,-20);
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL(-1, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL(-1, tree.getValueDepth(c1));
CPPUNIT_ASSERT(tree.isValueOff(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
tree.setValue(c0, 10.0);
ASSERT_DOUBLES_EXACTLY_EQUAL(10.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL(-1, tree.getValueDepth(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(openvdb::Coord(7, 10, 20)));
CPPUNIT_ASSERT_EQUAL( 2, tree.getValueDepth(openvdb::Coord(8, 10, 20)));
CPPUNIT_ASSERT(tree.isValueOn(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
tree.setValue(c1, 20.0);
ASSERT_DOUBLES_EXACTLY_EQUAL(10.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(20.0, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c1));
CPPUNIT_ASSERT(tree.isValueOn(c0));
CPPUNIT_ASSERT(tree.isValueOn(c1));
struct Local {
static inline void minOp(float& f, bool& b) { f = std::min(f, 15.f); b = true; }
static inline void maxOp(float& f, bool& b) { f = std::max(f, 12.f); b = true; }
static inline void sumOp(float& f, bool& b) { f += /*background=*/5.f; b = true; }
};
openvdb::tools::setValueOnMin(tree, c0, 15.0);
tree.modifyValueAndActiveState(c1, Local::minOp);
ASSERT_DOUBLES_EXACTLY_EQUAL(10.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(15.0, tree.getValue(c1));
openvdb::tools::setValueOnMax(tree, c0, 12.0);
tree.modifyValueAndActiveState(c1, Local::maxOp);
ASSERT_DOUBLES_EXACTLY_EQUAL(12.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(15.0, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL(2, int(tree.activeVoxelCount()));
float minVal = -999.0, maxVal = -999.0;
tree.evalMinMax(minVal, maxVal);
ASSERT_DOUBLES_EXACTLY_EQUAL(12.0, minVal);
ASSERT_DOUBLES_EXACTLY_EQUAL(15.0, maxVal);
tree.setValueOff(c0, background);
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(15.0, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL(1, int(tree.activeVoxelCount()));
openvdb::tools::setValueOnSum(tree, c0, background);
tree.modifyValueAndActiveState(c1, Local::sumOp);
ASSERT_DOUBLES_EXACTLY_EQUAL(2*background, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(15.0+background, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL(2, int(tree.activeVoxelCount()));
// Test the extremes of the coordinate range
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(openvdb::Coord::min()));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(openvdb::Coord::max()));
//std::cerr << "min=" << openvdb::Coord::min() << " max= " << openvdb::Coord::max() << "\n";
tree.setValue(openvdb::Coord::min(), 1.0f);
tree.setValue(openvdb::Coord::max(), 2.0f);
ASSERT_DOUBLES_EXACTLY_EQUAL(1.0f, tree.getValue(openvdb::Coord::min()));
ASSERT_DOUBLES_EXACTLY_EQUAL(2.0f, tree.getValue(openvdb::Coord::max()));
}
void
TestTree::testSetValueOnly()
{
const float background = 5.0f;
openvdb::FloatTree tree(background);
const openvdb::Coord c0( 5, 10, 20), c1(-5,-10,-20);
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL(-1, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL(-1, tree.getValueDepth(c1));
CPPUNIT_ASSERT(tree.isValueOff(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
tree.setValueOnly(c0, 10.0);
ASSERT_DOUBLES_EXACTLY_EQUAL(10.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL(-1, tree.getValueDepth(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(openvdb::Coord(7, 10, 20)));
CPPUNIT_ASSERT_EQUAL( 2, tree.getValueDepth(openvdb::Coord(8, 10, 20)));
CPPUNIT_ASSERT(tree.isValueOff(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
tree.setValueOnly(c1, 20.0);
ASSERT_DOUBLES_EXACTLY_EQUAL(10.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(20.0, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c1));
CPPUNIT_ASSERT(tree.isValueOff(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
tree.setValue(c0, 30.0);
ASSERT_DOUBLES_EXACTLY_EQUAL(30.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(20.0, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c1));
CPPUNIT_ASSERT(tree.isValueOn(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
tree.setValueOnly(c0, 40.0);
ASSERT_DOUBLES_EXACTLY_EQUAL(40.0, tree.getValue(c0));
ASSERT_DOUBLES_EXACTLY_EQUAL(20.0, tree.getValue(c1));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c0));
CPPUNIT_ASSERT_EQUAL( 3, tree.getValueDepth(c1));
CPPUNIT_ASSERT(tree.isValueOn(c0));
CPPUNIT_ASSERT(tree.isValueOff(c1));
CPPUNIT_ASSERT_EQUAL(1, int(tree.activeVoxelCount()));
}
namespace {
/// Helper function to test openvdb::tree::Tree::evalMinMax() for various tree types
template<typename TreeT>
void
evalMinMaxTest()
{
using ValueT = typename TreeT::ValueType;
struct Local {
static bool isEqual(const ValueT& a, const ValueT& b) {
using namespace openvdb; // for operator>()
return !(math::Abs(a - b) > zeroVal<ValueT>());
}
};
const ValueT
zero = openvdb::zeroVal<ValueT>(),
minusTwo = zero + (-2),
plusTwo = zero + 2,
five = zero + 5;
TreeT tree(/*background=*/five);
// No set voxels (defaults to min = max = zero)
ValueT minVal = five, maxVal = five;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT(Local::isEqual(minVal, zero));
CPPUNIT_ASSERT(Local::isEqual(maxVal, zero));
// Only one set voxel
tree.setValue(openvdb::Coord(0, 0, 0), minusTwo);
minVal = maxVal = five;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT(Local::isEqual(minVal, minusTwo));
CPPUNIT_ASSERT(Local::isEqual(maxVal, minusTwo));
// Multiple set voxels, single value
tree.setValue(openvdb::Coord(10, 10, 10), minusTwo);
minVal = maxVal = five;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT(Local::isEqual(minVal, minusTwo));
CPPUNIT_ASSERT(Local::isEqual(maxVal, minusTwo));
// Multiple set voxels, multiple values
tree.setValue(openvdb::Coord(10, 10, 10), plusTwo);
tree.setValue(openvdb::Coord(-10, -10, -10), zero);
minVal = maxVal = five;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT(Local::isEqual(minVal, minusTwo));
CPPUNIT_ASSERT(Local::isEqual(maxVal, plusTwo));
}
/// Specialization for boolean trees
template<>
void
evalMinMaxTest<openvdb::BoolTree>()
{
openvdb::BoolTree tree(/*background=*/false);
// No set voxels (defaults to min = max = zero)
bool minVal = true, maxVal = false;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(false, minVal);
CPPUNIT_ASSERT_EQUAL(false, maxVal);
// Only one set voxel
tree.setValue(openvdb::Coord(0, 0, 0), true);
minVal = maxVal = false;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(true, minVal);
CPPUNIT_ASSERT_EQUAL(true, maxVal);
// Multiple set voxels, single value
tree.setValue(openvdb::Coord(-10, -10, -10), true);
minVal = maxVal = false;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(true, minVal);
CPPUNIT_ASSERT_EQUAL(true, maxVal);
// Multiple set voxels, multiple values
tree.setValue(openvdb::Coord(10, 10, 10), false);
minVal = true; maxVal = false;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(false, minVal);
CPPUNIT_ASSERT_EQUAL(true, maxVal);
}
/// Specialization for string trees
template<>
void
evalMinMaxTest<openvdb::StringTree>()
{
const std::string
echidna("echidna"), loris("loris"), pangolin("pangolin");
openvdb::StringTree tree(/*background=*/loris);
// No set voxels (defaults to min = max = zero)
std::string minVal, maxVal;
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(std::string(), minVal);
CPPUNIT_ASSERT_EQUAL(std::string(), maxVal);
// Only one set voxel
tree.setValue(openvdb::Coord(0, 0, 0), pangolin);
minVal.clear(); maxVal.clear();
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(pangolin, minVal);
CPPUNIT_ASSERT_EQUAL(pangolin, maxVal);
// Multiple set voxels, single value
tree.setValue(openvdb::Coord(-10, -10, -10), pangolin);
minVal.clear(); maxVal.clear();
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(pangolin, minVal);
CPPUNIT_ASSERT_EQUAL(pangolin, maxVal);
// Multiple set voxels, multiple values
tree.setValue(openvdb::Coord(10, 10, 10), echidna);
minVal.clear(); maxVal.clear();
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(echidna, minVal);
CPPUNIT_ASSERT_EQUAL(pangolin, maxVal);
}
/// Specialization for Coord trees
template<>
void
evalMinMaxTest<openvdb::Coord>()
{
using CoordTree = openvdb::tree::Tree4<openvdb::Coord,5,4,3>::Type;
const openvdb::Coord backg(5,4,-6), a(5,4,-7), b(5,5,-6);
CoordTree tree(backg);
// No set voxels (defaults to min = max = zero)
openvdb::Coord minVal=openvdb::Coord::max(), maxVal=openvdb::Coord::min();
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(openvdb::Coord(0), minVal);
CPPUNIT_ASSERT_EQUAL(openvdb::Coord(0), maxVal);
// Only one set voxel
tree.setValue(openvdb::Coord(0, 0, 0), a);
minVal=openvdb::Coord::max();
maxVal=openvdb::Coord::min();
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(a, minVal);
CPPUNIT_ASSERT_EQUAL(a, maxVal);
// Multiple set voxels
tree.setValue(openvdb::Coord(-10, -10, -10), b);
minVal=openvdb::Coord::max();
maxVal=openvdb::Coord::min();
tree.evalMinMax(minVal, maxVal);
CPPUNIT_ASSERT_EQUAL(a, minVal);
CPPUNIT_ASSERT_EQUAL(b, maxVal);
}
} // unnamed namespace
void
TestTree::testEvalMinMax()
{
evalMinMaxTest<openvdb::BoolTree>();
evalMinMaxTest<openvdb::FloatTree>();
evalMinMaxTest<openvdb::Int32Tree>();
evalMinMaxTest<openvdb::Vec3STree>();
evalMinMaxTest<openvdb::Vec2ITree>();
evalMinMaxTest<openvdb::StringTree>();
evalMinMaxTest<openvdb::Coord>();
}
void
TestTree::testResize()
{
ValueType background=5.0f;
//use this when resize is implemented
RootNodeType root_node(background);
CPPUNIT_ASSERT(root_node.getLevel()==3);
ASSERT_DOUBLES_EXACTLY_EQUAL(background, root_node.getValue(openvdb::Coord(5,10,20)));
//fprintf(stdout,"Root grid dim=(%i,%i,%i)\n",
// root_node.getGridDim(0), root_node.getGridDim(1), root_node.getGridDim(2));
root_node.setValueOn(openvdb::Coord(5,10,20),0.234f);
ASSERT_DOUBLES_EXACTLY_EQUAL(root_node.getValue(openvdb::Coord(5,10,20)) , 0.234f);
root_node.setValueOn(openvdb::Coord(500,200,300),4.5678f);
ASSERT_DOUBLES_EXACTLY_EQUAL(root_node.getValue(openvdb::Coord(500,200,300)) , 4.5678f);
{
ValueType sum=0.0f;
for (RootNodeType::ChildOnIter root_iter = root_node.beginChildOn();
root_iter.test(); ++root_iter)
{
for (InternalNodeType2::ChildOnIter internal_iter2 = root_iter->beginChildOn();
internal_iter2.test(); ++internal_iter2)
{
for (InternalNodeType1::ChildOnIter internal_iter1 =
internal_iter2->beginChildOn(); internal_iter1.test(); ++internal_iter1)
{
for (LeafNodeType::ValueOnIter block_iter =
internal_iter1->beginValueOn(); block_iter.test(); ++block_iter)
{
sum += *block_iter;
}
}
}
}
ASSERT_DOUBLES_EXACTLY_EQUAL(sum, (0.234f + 4.5678f));
}
CPPUNIT_ASSERT(root_node.getLevel()==3);
ASSERT_DOUBLES_EXACTLY_EQUAL(background, root_node.getValue(openvdb::Coord(5,11,20)));
{
ValueType sum=0.0f;
for (RootNodeType::ChildOnIter root_iter = root_node.beginChildOn();
root_iter.test(); ++root_iter)
{
for (InternalNodeType2::ChildOnIter internal_iter2 = root_iter->beginChildOn();
internal_iter2.test(); ++internal_iter2)
{
for (InternalNodeType1::ChildOnIter internal_iter1 =
internal_iter2->beginChildOn(); internal_iter1.test(); ++internal_iter1)
{
for (LeafNodeType::ValueOnIter block_iter =
internal_iter1->beginValueOn(); block_iter.test(); ++block_iter)
{
sum += *block_iter;
}
}
}
}
ASSERT_DOUBLES_EXACTLY_EQUAL(sum, (0.234f + 4.5678f));
}
}
void
TestTree::testHasSameTopology()
{
// Test using trees of the same type.
{
const float background1=5.0f;
openvdb::FloatTree tree1(background1);
const float background2=6.0f;
openvdb::FloatTree tree2(background2);
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
tree1.setValue(openvdb::Coord(-10,40,845),3.456f);
CPPUNIT_ASSERT(!tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(!tree2.hasSameTopology(tree1));
tree2.setValue(openvdb::Coord(-10,40,845),-3.456f);
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
tree1.setValue(openvdb::Coord(1,-500,-8), 1.0f);
CPPUNIT_ASSERT(!tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(!tree2.hasSameTopology(tree1));
tree2.setValue(openvdb::Coord(1,-500,-8),1.0f);
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
}
// Test using trees of different types.
{
const float background1=5.0f;
openvdb::FloatTree tree1(background1);
const openvdb::Vec3f background2(1.0f,3.4f,6.0f);
openvdb::Vec3fTree tree2(background2);
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
tree1.setValue(openvdb::Coord(-10,40,845),3.456f);
CPPUNIT_ASSERT(!tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(!tree2.hasSameTopology(tree1));
tree2.setValue(openvdb::Coord(-10,40,845),openvdb::Vec3f(1.0f,2.0f,-3.0f));
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
tree1.setValue(openvdb::Coord(1,-500,-8), 1.0f);
CPPUNIT_ASSERT(!tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(!tree2.hasSameTopology(tree1));
tree2.setValue(openvdb::Coord(1,-500,-8),openvdb::Vec3f(1.0f,2.0f,-3.0f));
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
}
}
void
TestTree::testTopologyCopy()
{
// Test using trees of the same type.
{
const float background1=5.0f;
openvdb::FloatTree tree1(background1);
tree1.setValue(openvdb::Coord(-10,40,845),3.456f);
tree1.setValue(openvdb::Coord(1,-50,-8), 1.0f);
const float background2=6.0f, setValue2=3.0f;
openvdb::FloatTree tree2(tree1,background2,setValue2,openvdb::TopologyCopy());
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
ASSERT_DOUBLES_EXACTLY_EQUAL(background2, tree2.getValue(openvdb::Coord(1,2,3)));
ASSERT_DOUBLES_EXACTLY_EQUAL(setValue2, tree2.getValue(openvdb::Coord(-10,40,845)));
ASSERT_DOUBLES_EXACTLY_EQUAL(setValue2, tree2.getValue(openvdb::Coord(1,-50,-8)));
tree1.setValue(openvdb::Coord(1,-500,-8), 1.0f);
CPPUNIT_ASSERT(!tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(!tree2.hasSameTopology(tree1));
tree2.setValue(openvdb::Coord(1,-500,-8),1.0f);
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
}
// Test using trees of different types.
{
const openvdb::Vec3f background1(1.0f,3.4f,6.0f);
openvdb::Vec3fTree tree1(background1);
tree1.setValue(openvdb::Coord(-10,40,845),openvdb::Vec3f(3.456f,-2.3f,5.6f));
tree1.setValue(openvdb::Coord(1,-50,-8), openvdb::Vec3f(1.0f,3.0f,4.5f));
const float background2=6.0f, setValue2=3.0f;
openvdb::FloatTree tree2(tree1,background2,setValue2,openvdb::TopologyCopy());
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
ASSERT_DOUBLES_EXACTLY_EQUAL(background2, tree2.getValue(openvdb::Coord(1,2,3)));
ASSERT_DOUBLES_EXACTLY_EQUAL(setValue2, tree2.getValue(openvdb::Coord(-10,40,845)));
ASSERT_DOUBLES_EXACTLY_EQUAL(setValue2, tree2.getValue(openvdb::Coord(1,-50,-8)));
tree1.setValue(openvdb::Coord(1,-500,-8), openvdb::Vec3f(1.0f,0.0f,-3.0f));
CPPUNIT_ASSERT(!tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(!tree2.hasSameTopology(tree1));
tree2.setValue(openvdb::Coord(1,-500,-8), 1.0f);
CPPUNIT_ASSERT(tree1.hasSameTopology(tree2));
CPPUNIT_ASSERT(tree2.hasSameTopology(tree1));
}
}
void
TestTree::testIterators()
{
ValueType background=5.0f;
RootNodeType root_node(background);
root_node.setValueOn(openvdb::Coord(5,10,20),0.234f);
root_node.setValueOn(openvdb::Coord(50000,20000,30000),4.5678f);
{
ValueType sum=0.0f;
for (RootNodeType::ChildOnIter root_iter = root_node.beginChildOn();
root_iter.test(); ++root_iter)
{
for (InternalNodeType2::ChildOnIter internal_iter2 = root_iter->beginChildOn();
internal_iter2.test(); ++internal_iter2)
{
for (InternalNodeType1::ChildOnIter internal_iter1 =
internal_iter2->beginChildOn(); internal_iter1.test(); ++internal_iter1)
{
for (LeafNodeType::ValueOnIter block_iter =
internal_iter1->beginValueOn(); block_iter.test(); ++block_iter)
{
sum += *block_iter;
}
}
}
}
ASSERT_DOUBLES_EXACTLY_EQUAL((0.234f + 4.5678f), sum);
}
{
// As above, but using dense iterators.
ValueType sum = 0.0f, val = 0.0f;
for (RootNodeType::ChildAllIter rootIter = root_node.beginChildAll();
rootIter.test(); ++rootIter)
{
if (!rootIter.isChildNode()) continue;
for (InternalNodeType2::ChildAllIter internalIter2 =
rootIter.probeChild(val)->beginChildAll(); internalIter2; ++internalIter2)
{
if (!internalIter2.isChildNode()) continue;
for (InternalNodeType1::ChildAllIter internalIter1 =
internalIter2.probeChild(val)->beginChildAll(); internalIter1; ++internalIter1)
{
if (!internalIter1.isChildNode()) continue;
for (LeafNodeType::ValueOnIter leafIter =
internalIter1.probeChild(val)->beginValueOn(); leafIter; ++leafIter)
{
sum += *leafIter;
}
}
}
}
ASSERT_DOUBLES_EXACTLY_EQUAL((0.234f + 4.5678f), sum);
}
{
ValueType v_sum=0.0f;
openvdb::Coord xyz0, xyz1, xyz2, xyz3, xyzSum(0, 0, 0);
for (RootNodeType::ChildOnIter root_iter = root_node.beginChildOn();
root_iter.test(); ++root_iter)
{
root_iter.getCoord(xyz3);
for (InternalNodeType2::ChildOnIter internal_iter2 = root_iter->beginChildOn();
internal_iter2.test(); ++internal_iter2)
{
internal_iter2.getCoord(xyz2);
xyz2 = xyz2 - internal_iter2.parent().origin();
for (InternalNodeType1::ChildOnIter internal_iter1 =
internal_iter2->beginChildOn(); internal_iter1.test(); ++internal_iter1)
{
internal_iter1.getCoord(xyz1);
xyz1 = xyz1 - internal_iter1.parent().origin();
for (LeafNodeType::ValueOnIter block_iter =
internal_iter1->beginValueOn(); block_iter.test(); ++block_iter)
{
block_iter.getCoord(xyz0);
xyz0 = xyz0 - block_iter.parent().origin();
v_sum += *block_iter;
xyzSum = xyzSum + xyz0 + xyz1 + xyz2 + xyz3;
}
}
}
}
ASSERT_DOUBLES_EXACTLY_EQUAL((0.234f + 4.5678f), v_sum);
CPPUNIT_ASSERT_EQUAL(openvdb::Coord(5 + 50000, 10 + 20000, 20 + 30000), xyzSum);
}
}
void
TestTree::testIO()
{
const char* filename = "testIO.dbg";
openvdb::SharedPtr<const char> scopedFile(filename, ::remove);
{
ValueType background=5.0f;
RootNodeType root_node(background);
root_node.setValueOn(openvdb::Coord(5,10,20),0.234f);
root_node.setValueOn(openvdb::Coord(50000,20000,30000),4.5678f);
std::ofstream os(filename, std::ios_base::binary);
root_node.writeTopology(os);
root_node.writeBuffers(os);
CPPUNIT_ASSERT(!os.fail());
}
{
ValueType background=2.0f;
RootNodeType root_node(background);
ASSERT_DOUBLES_EXACTLY_EQUAL(background, root_node.getValue(openvdb::Coord(5,10,20)));
{
std::ifstream is(filename, std::ios_base::binary);
// Since the test file doesn't include a VDB header with file format version info,
// tag the input stream explicitly with the current version number.
openvdb::io::setCurrentVersion(is);
root_node.readTopology(is);
root_node.readBuffers(is);
CPPUNIT_ASSERT(!is.fail());
}
ASSERT_DOUBLES_EXACTLY_EQUAL(0.234f, root_node.getValue(openvdb::Coord(5,10,20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(5.0f, root_node.getValue(openvdb::Coord(5,11,20)));
ValueType sum=0.0f;
for (RootNodeType::ChildOnIter root_iter = root_node.beginChildOn();
root_iter.test(); ++root_iter)
{
for (InternalNodeType2::ChildOnIter internal_iter2 = root_iter->beginChildOn();
internal_iter2.test(); ++internal_iter2)
{
for (InternalNodeType1::ChildOnIter internal_iter1 =
internal_iter2->beginChildOn(); internal_iter1.test(); ++internal_iter1)
{
for (LeafNodeType::ValueOnIter block_iter =
internal_iter1->beginValueOn(); block_iter.test(); ++block_iter)
{
sum += *block_iter;
}
}
}
}
ASSERT_DOUBLES_EXACTLY_EQUAL(sum, (0.234f + 4.5678f));
}
}
void
TestTree::testNegativeIndexing()
{
ValueType background=5.0f;
openvdb::FloatTree tree(background);
CPPUNIT_ASSERT(tree.empty());
ASSERT_DOUBLES_EXACTLY_EQUAL(tree.getValue(openvdb::Coord(5,-10,20)), background);
ASSERT_DOUBLES_EXACTLY_EQUAL(tree.getValue(openvdb::Coord(-5000,2000,3000)), background);
tree.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree.setValue(openvdb::Coord(-5,-10, 20),0.4f);
tree.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
ASSERT_DOUBLES_EXACTLY_EQUAL(0.0f, tree.getValue(openvdb::Coord( 5, 10, 20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.1f, tree.getValue(openvdb::Coord(-5, 10, 20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.2f, tree.getValue(openvdb::Coord( 5,-10, 20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.3f, tree.getValue(openvdb::Coord( 5, 10,-20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.4f, tree.getValue(openvdb::Coord(-5,-10, 20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.5f, tree.getValue(openvdb::Coord(-5, 10,-20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.6f, tree.getValue(openvdb::Coord( 5,-10,-20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(0.7f, tree.getValue(openvdb::Coord(-5,-10,-20)));
ASSERT_DOUBLES_EXACTLY_EQUAL(4.5678f, tree.getValue(openvdb::Coord(-5000, 2000,-3000)));
ASSERT_DOUBLES_EXACTLY_EQUAL(4.5678f, tree.getValue(openvdb::Coord( 5000,-2000,-3000)));
ASSERT_DOUBLES_EXACTLY_EQUAL(4.5678f, tree.getValue(openvdb::Coord(-5000,-2000, 3000)));
int count=0;
for (int i =-25; i<25; ++i) {
for (int j=-25; j<25; ++j) {
for (int k=-25; k<25; ++k) {
if (tree.getValue(openvdb::Coord(i,j,k))<1.0f) {
//fprintf(stderr,"(%i,%i,%i)=%f\n",i,j,k,tree.getValue(openvdb::Coord(i,j,k)));
++count;
}
}
}
}
CPPUNIT_ASSERT(count == 8);
int count2 = 0;
openvdb::Coord xyz;
for (openvdb::FloatTree::ValueOnCIter iter = tree.cbeginValueOn(); iter; ++iter) {
++count2;
xyz = iter.getCoord();
//std::cerr << xyz << " = " << *iter << "\n";
}
CPPUNIT_ASSERT(count2 == 11);
CPPUNIT_ASSERT(tree.activeVoxelCount() == 11);
{
count2 = 0;
for (openvdb::FloatTree::ValueOnCIter iter = tree.cbeginValueOn(); iter; ++iter) {
++count2;
xyz = iter.getCoord();
//std::cerr << xyz << " = " << *iter << "\n";
}
CPPUNIT_ASSERT(count2 == 11);
CPPUNIT_ASSERT(tree.activeVoxelCount() == 11);
}
}
void
TestTree::testDeepCopy()
{
// set up a tree
const float fillValue1=5.0f;
openvdb::FloatTree tree1(fillValue1);
tree1.setValue(openvdb::Coord(-10,40,845), 3.456f);
tree1.setValue(openvdb::Coord(1,-50,-8), 1.0f);
// make a deep copy of the tree
openvdb::TreeBase::Ptr newTree = tree1.copy();
// cast down to the concrete type to query values
openvdb::FloatTree *pTree2 = dynamic_cast<openvdb::FloatTree *>(newTree.get());
// compare topology
CPPUNIT_ASSERT(tree1.hasSameTopology(*pTree2));
CPPUNIT_ASSERT(pTree2->hasSameTopology(tree1));
// trees should be equal
ASSERT_DOUBLES_EXACTLY_EQUAL(fillValue1, pTree2->getValue(openvdb::Coord(1,2,3)));
ASSERT_DOUBLES_EXACTLY_EQUAL(3.456f, pTree2->getValue(openvdb::Coord(-10,40,845)));
ASSERT_DOUBLES_EXACTLY_EQUAL(1.0f, pTree2->getValue(openvdb::Coord(1,-50,-8)));
// change 1 value in tree2
openvdb::Coord changeCoord(1, -500, -8);
pTree2->setValue(changeCoord, 1.0f);
// topology should no longer match
CPPUNIT_ASSERT(!tree1.hasSameTopology(*pTree2));
CPPUNIT_ASSERT(!pTree2->hasSameTopology(tree1));
// query changed value and make sure it's different between trees
ASSERT_DOUBLES_EXACTLY_EQUAL(fillValue1, tree1.getValue(changeCoord));
ASSERT_DOUBLES_EXACTLY_EQUAL(1.0f, pTree2->getValue(changeCoord));
}
void
TestTree::testMerge()
{
ValueType background=5.0f;
openvdb::FloatTree tree0(background), tree1(background), tree2(background);
CPPUNIT_ASSERT(tree2.empty());
tree0.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree0.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree0.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree0.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree1.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree1.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree1.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree1.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree0.setValue(openvdb::Coord(-5,-10, 20),0.4f);
tree0.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree0.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree0.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree0.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree0.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree0.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
tree2.setValue(openvdb::Coord(-5,-10, 20),0.4f);
tree2.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree2.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree2.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree2.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
CPPUNIT_ASSERT(tree0.leafCount()!=tree1.leafCount());
CPPUNIT_ASSERT(tree0.leafCount()!=tree2.leafCount());
CPPUNIT_ASSERT(!tree2.empty());
tree1.merge(tree2, openvdb::MERGE_ACTIVE_STATES);
CPPUNIT_ASSERT(tree2.empty());
CPPUNIT_ASSERT(tree0.leafCount()==tree1.leafCount());
CPPUNIT_ASSERT(tree0.nonLeafCount()==tree1.nonLeafCount());
CPPUNIT_ASSERT(tree0.activeLeafVoxelCount()==tree1.activeLeafVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveLeafVoxelCount()==tree1.inactiveLeafVoxelCount());
CPPUNIT_ASSERT(tree0.activeVoxelCount()==tree1.activeVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveVoxelCount()==tree1.inactiveVoxelCount());
for (openvdb::FloatTree::ValueOnCIter iter0 = tree0.cbeginValueOn(); iter0; ++iter0) {
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter0,tree1.getValue(iter0.getCoord()));
}
// Test active tile support.
{
using namespace openvdb;
FloatTree treeA(/*background*/0.0), treeB(/*background*/0.0);
treeA.fill(CoordBBox(Coord(16,16,16), Coord(31,31,31)), /*value*/1.0);
treeB.fill(CoordBBox(Coord(0,0,0), Coord(15,15,15)), /*value*/1.0);
CPPUNIT_ASSERT_EQUAL(4096, int(treeA.activeVoxelCount()));
CPPUNIT_ASSERT_EQUAL(4096, int(treeB.activeVoxelCount()));
treeA.merge(treeB, MERGE_ACTIVE_STATES);
CPPUNIT_ASSERT_EQUAL(8192, int(treeA.activeVoxelCount()));
CPPUNIT_ASSERT_EQUAL(0, int(treeB.activeVoxelCount()));
}
doTestMerge<openvdb::FloatTree>(openvdb::MERGE_NODES);
doTestMerge<openvdb::FloatTree>(openvdb::MERGE_ACTIVE_STATES);
doTestMerge<openvdb::FloatTree>(openvdb::MERGE_ACTIVE_STATES_AND_NODES);
doTestMerge<openvdb::BoolTree>(openvdb::MERGE_NODES);
doTestMerge<openvdb::BoolTree>(openvdb::MERGE_ACTIVE_STATES);
doTestMerge<openvdb::BoolTree>(openvdb::MERGE_ACTIVE_STATES_AND_NODES);
}
template<typename TreeType>
void
TestTree::doTestMerge(openvdb::MergePolicy policy)
{
using namespace openvdb;
TreeType treeA, treeB;
using RootT = typename TreeType::RootNodeType;
using LeafT = typename TreeType::LeafNodeType;
const typename TreeType::ValueType val(1);
const int
depth = static_cast<int>(treeA.treeDepth()),
leafDim = static_cast<int>(LeafT::dim()),
leafSize = static_cast<int>(LeafT::size());
// Coords that are in a different top-level branch than (0, 0, 0)
const Coord pos(static_cast<int>(RootT::getChildDim()));
treeA.setValueOff(pos, val);
treeA.setValueOff(-pos, val);
treeB.setValueOff(Coord(0), val);
treeB.fill(CoordBBox(pos, pos.offsetBy(leafDim - 1)), val, /*active=*/true);
treeB.setValueOn(-pos, val);
// treeA treeB .
// .
// R R .
// / \ /|\ .
// I I I I I .
// / \ / | \ .
// I I I I I .
// / \ / | on x SIZE .
// L L L L .
// off off on off .
CPPUNIT_ASSERT_EQUAL(0, int(treeA.activeVoxelCount()));
CPPUNIT_ASSERT_EQUAL(leafSize + 1, int(treeB.activeVoxelCount()));
CPPUNIT_ASSERT_EQUAL(2, int(treeA.leafCount()));
CPPUNIT_ASSERT_EQUAL(2, int(treeB.leafCount()));
CPPUNIT_ASSERT_EQUAL(2*(depth-2)+1, int(treeA.nonLeafCount())); // 2 branches (II+II+R)
CPPUNIT_ASSERT_EQUAL(3*(depth-2)+1, int(treeB.nonLeafCount())); // 3 branches (II+II+II+R)
treeA.merge(treeB, policy);
// MERGE_NODES MERGE_ACTIVE_STATES MERGE_ACTIVE_STATES_AND_NODES .
// .
// R R R .
// /|\ /|\ /|\ .
// I I I I I I I I I .
// / | \ / | \ / | \ .
// I I I I I I I I I .
// / | \ / | on x SIZE / | \ .
// L L L L L L L L .
// off off off on off on off on x SIZE .
switch (policy) {
case MERGE_NODES:
CPPUNIT_ASSERT_EQUAL(0, int(treeA.activeVoxelCount()));
CPPUNIT_ASSERT_EQUAL(2 + 1, int(treeA.leafCount())); // 1 leaf node stolen from B
CPPUNIT_ASSERT_EQUAL(3*(depth-2)+1, int(treeA.nonLeafCount())); // 3 branches (II+II+II+R)
break;
case MERGE_ACTIVE_STATES:
CPPUNIT_ASSERT_EQUAL(2, int(treeA.leafCount())); // 1 leaf stolen, 1 replaced with tile
CPPUNIT_ASSERT_EQUAL(3*(depth-2)+1, int(treeA.nonLeafCount())); // 3 branches (II+II+II+R)
CPPUNIT_ASSERT_EQUAL(leafSize + 1, int(treeA.activeVoxelCount()));
break;
case MERGE_ACTIVE_STATES_AND_NODES:
CPPUNIT_ASSERT_EQUAL(2 + 1, int(treeA.leafCount())); // 1 leaf node stolen from B
CPPUNIT_ASSERT_EQUAL(3*(depth-2)+1, int(treeA.nonLeafCount())); // 3 branches (II+II+II+R)
CPPUNIT_ASSERT_EQUAL(leafSize + 1, int(treeA.activeVoxelCount()));
break;
}
CPPUNIT_ASSERT(treeB.empty());
}
void
TestTree::testVoxelizeActiveTiles()
{
using openvdb::CoordBBox;
using openvdb::Coord;
// Use a small custom tree so we don't run out of memory when
// tiles are converted to dense leafs :)
using MyTree = openvdb::tree::Tree4<float,2, 2, 2>::Type;
float background=5.0f;
const Coord xyz[] = {Coord(-1,-2,-3),Coord( 1, 2, 3)};
//check two leaf nodes and two tiles at each level 1, 2 and 3
const int tile_size[4]={0, 1<<2, 1<<(2*2), 1<<(3*2)};
// serial version
for (int level=0; level<=3; ++level) {
MyTree tree(background);
CPPUNIT_ASSERT_EQUAL(-1,tree.getValueDepth(xyz[0]));
CPPUNIT_ASSERT_EQUAL(-1,tree.getValueDepth(xyz[1]));
if (level==0) {
tree.setValue(xyz[0], 1.0f);
tree.setValue(xyz[1], 1.0f);
} else {
const int n = tile_size[level];
tree.fill(CoordBBox::createCube(Coord(-n,-n,-n), n), 1.0f, true);
tree.fill(CoordBBox::createCube(Coord( 0, 0, 0), n), 1.0f, true);
}
CPPUNIT_ASSERT_EQUAL(3-level,tree.getValueDepth(xyz[0]));
CPPUNIT_ASSERT_EQUAL(3-level,tree.getValueDepth(xyz[1]));
tree.voxelizeActiveTiles(false);
CPPUNIT_ASSERT_EQUAL(3 ,tree.getValueDepth(xyz[0]));
CPPUNIT_ASSERT_EQUAL(3 ,tree.getValueDepth(xyz[1]));
}
// multi-threaded version
for (int level=0; level<=3; ++level) {
MyTree tree(background);
CPPUNIT_ASSERT_EQUAL(-1,tree.getValueDepth(xyz[0]));
CPPUNIT_ASSERT_EQUAL(-1,tree.getValueDepth(xyz[1]));
if (level==0) {
tree.setValue(xyz[0], 1.0f);
tree.setValue(xyz[1], 1.0f);
} else {
const int n = tile_size[level];
tree.fill(CoordBBox::createCube(Coord(-n,-n,-n), n), 1.0f, true);
tree.fill(CoordBBox::createCube(Coord( 0, 0, 0), n), 1.0f, true);
}
CPPUNIT_ASSERT_EQUAL(3-level,tree.getValueDepth(xyz[0]));
CPPUNIT_ASSERT_EQUAL(3-level,tree.getValueDepth(xyz[1]));
tree.voxelizeActiveTiles(true);
CPPUNIT_ASSERT_EQUAL(3 ,tree.getValueDepth(xyz[0]));
CPPUNIT_ASSERT_EQUAL(3 ,tree.getValueDepth(xyz[1]));
}
#if 0
const CoordBBox bbox(openvdb::Coord(-30,-50,-30), openvdb::Coord(530,610,623));
{// benchmark serial
MyTree tree(background);
tree.sparseFill( bbox, 1.0f, /*state*/true);
openvdb::util::CpuTimer timer("\nserial voxelizeActiveTiles");
tree.voxelizeActiveTiles(/*threaded*/false);
timer.stop();
}
{// benchmark parallel
MyTree tree(background);
tree.sparseFill( bbox, 1.0f, /*state*/true);
openvdb::util::CpuTimer timer("\nparallel voxelizeActiveTiles");
tree.voxelizeActiveTiles(/*threaded*/true);
timer.stop();
}
#endif
}
void
TestTree::testTopologyUnion()
{
{//super simple test with only two active values
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 2.0f);
openvdb::FloatTree tree2(tree1);
tree1.topologyUnion(tree0);
for (openvdb::FloatTree::ValueOnCIter iter = tree0.cbeginValueOn(); iter; ++iter) {
CPPUNIT_ASSERT(tree1.isValueOn(iter.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree2.cbeginValueOn(); iter; ++iter) {
CPPUNIT_ASSERT(tree1.isValueOn(iter.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1.cbeginValueOn(); iter; ++iter) {
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree2.getValue(iter.getCoord()));
}
}
{// test using setValue
ValueType background=5.0f;
openvdb::FloatTree tree0(background), tree1(background), tree2(background);
CPPUNIT_ASSERT(tree2.empty());
// tree0 = tree1.topologyUnion(tree2)
tree0.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree0.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree0.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree0.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree1.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree1.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree1.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree1.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree0.setValue(openvdb::Coord(-5,-10, 20),background);
tree0.setValue(openvdb::Coord(-5, 10,-20),background);
tree0.setValue(openvdb::Coord( 5,-10,-20),background);
tree0.setValue(openvdb::Coord(-5,-10,-20),background);
tree0.setValue(openvdb::Coord(-5000, 2000,-3000),background);
tree0.setValue(openvdb::Coord( 5000,-2000,-3000),background);
tree0.setValue(openvdb::Coord(-5000,-2000, 3000),background);
tree2.setValue(openvdb::Coord(-5,-10, 20),0.4f);
tree2.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree2.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree2.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree2.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
// tree3 has the same topology as tree2 but a different value type
const openvdb::Vec3f background2(1.0f,3.4f,6.0f), vec_val(3.1f,5.3f,-9.5f);
openvdb::Vec3fTree tree3(background2);
for (openvdb::FloatTree::ValueOnCIter iter2 = tree2.cbeginValueOn(); iter2; ++iter2) {
tree3.setValue(iter2.getCoord(), vec_val);
}
CPPUNIT_ASSERT(tree0.leafCount()!=tree1.leafCount());
CPPUNIT_ASSERT(tree0.leafCount()!=tree2.leafCount());
CPPUNIT_ASSERT(tree0.leafCount()!=tree3.leafCount());
CPPUNIT_ASSERT(!tree2.empty());
CPPUNIT_ASSERT(!tree3.empty());
openvdb::FloatTree tree1_copy(tree1);
//tree1.topologyUnion(tree2);//should make tree1 = tree0
tree1.topologyUnion(tree3);//should make tree1 = tree0
CPPUNIT_ASSERT(tree0.leafCount()==tree1.leafCount());
CPPUNIT_ASSERT(tree0.nonLeafCount()==tree1.nonLeafCount());
CPPUNIT_ASSERT(tree0.activeLeafVoxelCount()==tree1.activeLeafVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveLeafVoxelCount()==tree1.inactiveLeafVoxelCount());
CPPUNIT_ASSERT(tree0.activeVoxelCount()==tree1.activeVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveVoxelCount()==tree1.inactiveVoxelCount());
CPPUNIT_ASSERT(tree1.hasSameTopology(tree0));
CPPUNIT_ASSERT(tree0.hasSameTopology(tree1));
for (openvdb::FloatTree::ValueOnCIter iter2 = tree2.cbeginValueOn(); iter2; ++iter2) {
CPPUNIT_ASSERT(tree1.isValueOn(iter2.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter1 = tree1.cbeginValueOn(); iter1; ++iter1) {
CPPUNIT_ASSERT(tree0.isValueOn(iter1.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter0 = tree0.cbeginValueOn(); iter0; ++iter0) {
CPPUNIT_ASSERT(tree1.isValueOn(iter0.getCoord()));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter0,tree1.getValue(iter0.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1_copy.cbeginValueOn(); iter; ++iter) {
CPPUNIT_ASSERT(tree1.isValueOn(iter.getCoord()));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree1.getValue(iter.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree3.isValueOn(p) || tree1_copy.isValueOn(p));
}
}
{
ValueType background=5.0f;
openvdb::FloatTree tree0(background), tree1(background), tree2(background);
CPPUNIT_ASSERT(tree2.empty());
// tree0 = tree1.topologyUnion(tree2)
tree0.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree0.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree0.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree0.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree1.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree1.setValue(openvdb::Coord(-5, 10, 20),0.1f);
tree1.setValue(openvdb::Coord( 5,-10, 20),0.2f);
tree1.setValue(openvdb::Coord( 5, 10,-20),0.3f);
tree0.setValue(openvdb::Coord(-5,-10, 20),background);
tree0.setValue(openvdb::Coord(-5, 10,-20),background);
tree0.setValue(openvdb::Coord( 5,-10,-20),background);
tree0.setValue(openvdb::Coord(-5,-10,-20),background);
tree0.setValue(openvdb::Coord(-5000, 2000,-3000),background);
tree0.setValue(openvdb::Coord( 5000,-2000,-3000),background);
tree0.setValue(openvdb::Coord(-5000,-2000, 3000),background);
tree2.setValue(openvdb::Coord(-5,-10, 20),0.4f);
tree2.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree2.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree2.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree2.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
// tree3 has the same topology as tree2 but a different value type
const openvdb::Vec3f background2(1.0f,3.4f,6.0f), vec_val(3.1f,5.3f,-9.5f);
openvdb::Vec3fTree tree3(background2);
for (openvdb::FloatTree::ValueOnCIter iter2 = tree2.cbeginValueOn(); iter2; ++iter2) {
tree3.setValue(iter2.getCoord(), vec_val);
}
openvdb::FloatTree tree4(tree1);//tree4 = tree1
openvdb::FloatTree tree5(tree1);//tree5 = tree1
tree1.topologyUnion(tree3);//should make tree1 = tree0
CPPUNIT_ASSERT(tree1.hasSameTopology(tree0));
for (openvdb::Vec3fTree::ValueOnCIter iter3 = tree3.cbeginValueOn(); iter3; ++iter3) {
tree4.setValueOn(iter3.getCoord());
const openvdb::Coord p = iter3.getCoord();
ASSERT_DOUBLES_EXACTLY_EQUAL(tree1.getValue(p),tree5.getValue(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(tree4.getValue(p),tree5.getValue(p));
}
CPPUNIT_ASSERT(tree4.hasSameTopology(tree0));
for (openvdb::FloatTree::ValueOnCIter iter4 = tree4.cbeginValueOn(); iter4; ++iter4) {
const openvdb::Coord p = iter4.getCoord();
ASSERT_DOUBLES_EXACTLY_EQUAL(tree0.getValue(p),tree5.getValue(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(tree1.getValue(p),tree5.getValue(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(tree4.getValue(p),tree5.getValue(p));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree3.isValueOn(p) || tree4.isValueOn(p));
}
}
{// test overlapping spheres
const float background=5.0f, R0=10.0f, R1=5.6f;
const openvdb::Vec3f C0(35.0f, 30.0f, 40.0f), C1(22.3f, 30.5f, 31.0f);
const openvdb::Coord dim(32, 32, 32);
openvdb::FloatGrid grid0(background);
openvdb::FloatGrid grid1(background);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C0, R0, grid0,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C1, R1, grid1,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
openvdb::FloatTree& tree0 = grid0.tree();
openvdb::FloatTree& tree1 = grid1.tree();
openvdb::FloatTree tree0_copy(tree0);
tree0.topologyUnion(tree1);
const openvdb::Index64 n0 = tree0_copy.activeVoxelCount();
const openvdb::Index64 n = tree0.activeVoxelCount();
const openvdb::Index64 n1 = tree1.activeVoxelCount();
//fprintf(stderr,"Union of spheres: n=%i, n0=%i n1=%i n0+n1=%i\n",n,n0,n1, n0+n1);
CPPUNIT_ASSERT( n > n0 );
CPPUNIT_ASSERT( n > n1 );
CPPUNIT_ASSERT( n < n0 + n1 );
for (openvdb::FloatTree::ValueOnCIter iter = tree1.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree0.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(tree0.getValue(p), tree0_copy.getValue(p));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree0_copy.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree0.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(tree0.getValue(p), *iter);
}
}
{// test union of a leaf and a tile
if (openvdb::FloatTree::DEPTH > 2) {
const int leafLevel = openvdb::FloatTree::DEPTH - 1;
const int tileLevel = leafLevel - 1;
const openvdb::Coord xyz(0);
openvdb::FloatTree tree0;
tree0.addTile(tileLevel, xyz, /*value=*/0, /*activeState=*/true);
CPPUNIT_ASSERT(tree0.isValueOn(xyz));
openvdb::FloatTree tree1;
tree1.touchLeaf(xyz)->setValuesOn();
CPPUNIT_ASSERT(tree1.isValueOn(xyz));
tree0.topologyUnion(tree1);
CPPUNIT_ASSERT(tree0.isValueOn(xyz));
CPPUNIT_ASSERT_EQUAL(tree0.getValueDepth(xyz), leafLevel);
}
}
}// testTopologyUnion
void
TestTree::testTopologyIntersection()
{
{//no overlapping voxels
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 2.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(1), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(1), tree1.activeVoxelCount());
tree1.topologyIntersection(tree0);
CPPUNIT_ASSERT_EQUAL(tree1.activeVoxelCount(), openvdb::Index64(0));
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(tree1.empty());
}
{//two overlapping voxels
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 2.0f);
tree1.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree0.activeVoxelCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(2), tree1.activeVoxelCount() );
tree1.topologyIntersection(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(!tree1.empty());
}
{//4 overlapping voxels
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree0.setValue(openvdb::Coord( 400, 30, 20), 2.0f);
tree0.setValue(openvdb::Coord( 8, 11, 11), 3.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree0.leafCount() );
tree1.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree1.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 6.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree1.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree1.leafCount() );
tree1.topologyIntersection(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(3), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(2), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(!tree1.empty());
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(2), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(2), tree1.activeVoxelCount() );
}
{//passive tile
const ValueType background=0.0f;
const openvdb::Index64 dim = openvdb::FloatTree::RootNodeType::ChildNodeType::DIM;
openvdb::FloatTree tree0(background), tree1(background);
tree0.fill(openvdb::CoordBBox(openvdb::Coord(0),openvdb::Coord(dim-1)),2.0f, false);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(0), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(0), tree0.leafCount() );
tree1.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree1.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree1.setValue(openvdb::Coord( dim, 11, 11), 6.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree1.activeVoxelCount());
tree1.topologyIntersection(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(0), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(0), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(tree1.empty());
}
{//active tile
const ValueType background=0.0f;
const openvdb::Index64 dim = openvdb::FloatTree::RootNodeType::ChildNodeType::DIM;
openvdb::FloatTree tree0(background), tree1(background);
tree1.fill(openvdb::CoordBBox(openvdb::Coord(0),openvdb::Coord(dim-1)),2.0f, true);
CPPUNIT_ASSERT_EQUAL(dim*dim*dim, tree1.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(0), tree1.leafCount() );
tree0.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree0.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree0.setValue(openvdb::Coord( dim, 11, 11), 6.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree0.leafCount() );
tree1.topologyIntersection(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(2), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(2), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(!tree1.empty());
}
{// use tree with different voxel type
ValueType background=5.0f;
openvdb::FloatTree tree0(background), tree1(background), tree2(background);
CPPUNIT_ASSERT(tree2.empty());
// tree0 = tree1.topologyIntersection(tree2)
tree0.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree0.setValue(openvdb::Coord(-5, 10,-20),0.1f);
tree0.setValue(openvdb::Coord( 5,-10,-20),0.2f);
tree0.setValue(openvdb::Coord(-5,-10,-20),0.3f);
tree1.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree1.setValue(openvdb::Coord(-5, 10,-20),0.1f);
tree1.setValue(openvdb::Coord( 5,-10,-20),0.2f);
tree1.setValue(openvdb::Coord(-5,-10,-20),0.3f);
tree2.setValue(openvdb::Coord( 5, 10, 20),0.4f);
tree2.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree2.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree2.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree2.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
openvdb::FloatTree tree1_copy(tree1);
// tree3 has the same topology as tree2 but a different value type
const openvdb::Vec3f background2(1.0f,3.4f,6.0f), vec_val(3.1f,5.3f,-9.5f);
openvdb::Vec3fTree tree3(background2);
for (openvdb::FloatTree::ValueOnCIter iter = tree2.cbeginValueOn(); iter; ++iter) {
tree3.setValue(iter.getCoord(), vec_val);
}
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(4), tree0.leafCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(4), tree1.leafCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(7), tree2.leafCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(7), tree3.leafCount());
//tree1.topologyInterection(tree2);//should make tree1 = tree0
tree1.topologyIntersection(tree3);//should make tree1 = tree0
CPPUNIT_ASSERT(tree0.leafCount()==tree1.leafCount());
CPPUNIT_ASSERT(tree0.nonLeafCount()==tree1.nonLeafCount());
CPPUNIT_ASSERT(tree0.activeLeafVoxelCount()==tree1.activeLeafVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveLeafVoxelCount()==tree1.inactiveLeafVoxelCount());
CPPUNIT_ASSERT(tree0.activeVoxelCount()==tree1.activeVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveVoxelCount()==tree1.inactiveVoxelCount());
CPPUNIT_ASSERT(tree1.hasSameTopology(tree0));
CPPUNIT_ASSERT(tree0.hasSameTopology(tree1));
for (openvdb::FloatTree::ValueOnCIter iter = tree0.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree1.isValueOn(p));
CPPUNIT_ASSERT(tree2.isValueOn(p));
CPPUNIT_ASSERT(tree3.isValueOn(p));
CPPUNIT_ASSERT(tree1_copy.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree1.getValue(p));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1_copy.cbeginValueOn(); iter; ++iter) {
CPPUNIT_ASSERT(tree1.isValueOn(iter.getCoord()));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree1.getValue(iter.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree0.isValueOn(p));
CPPUNIT_ASSERT(tree2.isValueOn(p));
CPPUNIT_ASSERT(tree3.isValueOn(p));
CPPUNIT_ASSERT(tree1_copy.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree0.getValue(p));
}
}
{// test overlapping spheres
const float background=5.0f, R0=10.0f, R1=5.6f;
const openvdb::Vec3f C0(35.0f, 30.0f, 40.0f), C1(22.3f, 30.5f, 31.0f);
const openvdb::Coord dim(32, 32, 32);
openvdb::FloatGrid grid0(background);
openvdb::FloatGrid grid1(background);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C0, R0, grid0,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C1, R1, grid1,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
openvdb::FloatTree& tree0 = grid0.tree();
openvdb::FloatTree& tree1 = grid1.tree();
openvdb::FloatTree tree0_copy(tree0);
tree0.topologyIntersection(tree1);
const openvdb::Index64 n0 = tree0_copy.activeVoxelCount();
const openvdb::Index64 n = tree0.activeVoxelCount();
const openvdb::Index64 n1 = tree1.activeVoxelCount();
//fprintf(stderr,"Intersection of spheres: n=%i, n0=%i n1=%i n0+n1=%i\n",n,n0,n1, n0+n1);
CPPUNIT_ASSERT( n < n0 );
CPPUNIT_ASSERT( n < n1 );
for (openvdb::FloatTree::ValueOnCIter iter = tree0.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree1.isValueOn(p));
CPPUNIT_ASSERT(tree0_copy.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter, tree0_copy.getValue(p));
}
}
{// Test based on boolean grids
openvdb::CoordBBox bigRegion(openvdb::Coord(-9), openvdb::Coord(10));
openvdb::CoordBBox smallRegion(openvdb::Coord( 1), openvdb::Coord(10));
openvdb::BoolGrid::Ptr gridBig = openvdb::BoolGrid::create(false);
gridBig->fill(bigRegion, true/*value*/, true /*make active*/);
CPPUNIT_ASSERT_EQUAL(8, int(gridBig->tree().activeTileCount()));
CPPUNIT_ASSERT_EQUAL((20 * 20 * 20), int(gridBig->activeVoxelCount()));
openvdb::BoolGrid::Ptr gridSmall = openvdb::BoolGrid::create(false);
gridSmall->fill(smallRegion, true/*value*/, true /*make active*/);
CPPUNIT_ASSERT_EQUAL(0, int(gridSmall->tree().activeTileCount()));
CPPUNIT_ASSERT_EQUAL((10 * 10 * 10), int(gridSmall->activeVoxelCount()));
// change the topology of gridBig by intersecting with gridSmall
gridBig->topologyIntersection(*gridSmall);
// Should be unchanged
CPPUNIT_ASSERT_EQUAL(0, int(gridSmall->tree().activeTileCount()));
CPPUNIT_ASSERT_EQUAL((10 * 10 * 10), int(gridSmall->activeVoxelCount()));
// In this case the interesection should be exactly "small"
CPPUNIT_ASSERT_EQUAL(0, int(gridBig->tree().activeTileCount()));
CPPUNIT_ASSERT_EQUAL((10 * 10 * 10), int(gridBig->activeVoxelCount()));
}
}// testTopologyIntersection
void
TestTree::testTopologyDifference()
{
{//no overlapping voxels
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 2.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(1), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(1), tree1.activeVoxelCount());
tree1.topologyDifference(tree0);
CPPUNIT_ASSERT_EQUAL(tree1.activeVoxelCount(), openvdb::Index64(1));
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(!tree1.empty());
}
{//two overlapping voxels
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 2.0f);
tree1.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree0.activeVoxelCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(2), tree1.activeVoxelCount() );
CPPUNIT_ASSERT( tree0.isValueOn(openvdb::Coord( 500, 300, 200)));
CPPUNIT_ASSERT( tree1.isValueOn(openvdb::Coord( 500, 300, 200)));
CPPUNIT_ASSERT( tree1.isValueOn(openvdb::Coord( 8, 11, 11)));
tree1.topologyDifference(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree1.activeVoxelCount() );
CPPUNIT_ASSERT( tree0.isValueOn(openvdb::Coord( 500, 300, 200)));
CPPUNIT_ASSERT(!tree1.isValueOn(openvdb::Coord( 500, 300, 200)));
CPPUNIT_ASSERT( tree1.isValueOn(openvdb::Coord( 8, 11, 11)));
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(!tree1.empty());
}
{//4 overlapping voxels
const ValueType background=0.0f;
openvdb::FloatTree tree0(background), tree1(background);
tree0.setValue(openvdb::Coord( 500, 300, 200), 1.0f);
tree0.setValue(openvdb::Coord( 400, 30, 20), 2.0f);
tree0.setValue(openvdb::Coord( 8, 11, 11), 3.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree0.leafCount() );
tree1.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree1.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree1.setValue(openvdb::Coord( 8, 11, 11), 6.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree1.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree1.leafCount() );
tree1.topologyDifference(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(3), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT(!tree1.empty());
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(1), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree1.activeVoxelCount() );
}
{//passive tile
const ValueType background=0.0f;
const openvdb::Index64 dim = openvdb::FloatTree::RootNodeType::ChildNodeType::DIM;
openvdb::FloatTree tree0(background), tree1(background);
tree0.fill(openvdb::CoordBBox(openvdb::Coord(0),openvdb::Coord(dim-1)),2.0f, false);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(0), tree0.activeVoxelCount());
CPPUNIT_ASSERT(!tree0.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(0), tree0.root().onTileCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(0), tree0.leafCount() );
tree1.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree1.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree1.setValue(openvdb::Coord( dim, 11, 11), 6.0f);
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree1.activeVoxelCount());
CPPUNIT_ASSERT(!tree1.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree1.leafCount() );
tree1.topologyDifference(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(3), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(3), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(3), tree1.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(3), tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
}
{//active tile
const ValueType background=0.0f;
const openvdb::Index64 dim = openvdb::FloatTree::RootNodeType::ChildNodeType::DIM;
openvdb::FloatTree tree0(background), tree1(background);
tree1.fill(openvdb::CoordBBox(openvdb::Coord(0),openvdb::Coord(dim-1)),2.0f, true);
CPPUNIT_ASSERT_EQUAL(dim*dim*dim, tree1.activeVoxelCount());
CPPUNIT_ASSERT(tree1.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(1), tree1.root().onTileCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(0), tree0.leafCount() );
tree0.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree0.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree0.setValue(openvdb::Coord( int(dim), 11, 11), 6.0f);
CPPUNIT_ASSERT(!tree0.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree0.leafCount() );
CPPUNIT_ASSERT( tree0.isValueOn(openvdb::Coord( int(dim), 11, 11)));
CPPUNIT_ASSERT(!tree1.isValueOn(openvdb::Coord( int(dim), 11, 11)));
tree1.topologyDifference(tree0);
CPPUNIT_ASSERT(tree1.root().onTileCount() > 1);
CPPUNIT_ASSERT_EQUAL( dim*dim*dim - 2, tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
openvdb::tools::pruneInactive(tree1);
CPPUNIT_ASSERT_EQUAL( dim*dim*dim - 2, tree1.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
}
{//active tile
const ValueType background=0.0f;
const openvdb::Index64 dim = openvdb::FloatTree::RootNodeType::ChildNodeType::DIM;
openvdb::FloatTree tree0(background), tree1(background);
tree1.fill(openvdb::CoordBBox(openvdb::Coord(0),openvdb::Coord(dim-1)),2.0f, true);
CPPUNIT_ASSERT_EQUAL(dim*dim*dim, tree1.activeVoxelCount());
CPPUNIT_ASSERT(tree1.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(1), tree1.root().onTileCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(0), tree0.leafCount() );
tree0.setValue(openvdb::Coord( 500, 301, 200), 4.0f);
tree0.setValue(openvdb::Coord( 400, 30, 20), 5.0f);
tree0.setValue(openvdb::Coord( dim, 11, 11), 6.0f);
CPPUNIT_ASSERT(!tree0.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(3), tree0.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(3), tree0.leafCount() );
tree0.topologyDifference(tree1);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(1), tree0.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree0.activeVoxelCount() );
CPPUNIT_ASSERT(!tree0.empty());
openvdb::tools::pruneInactive(tree0);
CPPUNIT_ASSERT_EQUAL( openvdb::Index32(1), tree0.leafCount() );
CPPUNIT_ASSERT_EQUAL( openvdb::Index64(1), tree0.activeVoxelCount() );
CPPUNIT_ASSERT(!tree1.empty());
}
{// use tree with different voxel type
ValueType background=5.0f;
openvdb::FloatTree tree0(background), tree1(background), tree2(background);
CPPUNIT_ASSERT(tree2.empty());
// tree0 = tree1.topologyIntersection(tree2)
tree0.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree0.setValue(openvdb::Coord(-5, 10,-20),0.1f);
tree0.setValue(openvdb::Coord( 5,-10,-20),0.2f);
tree0.setValue(openvdb::Coord(-5,-10,-20),0.3f);
tree1.setValue(openvdb::Coord( 5, 10, 20),0.0f);
tree1.setValue(openvdb::Coord(-5, 10,-20),0.1f);
tree1.setValue(openvdb::Coord( 5,-10,-20),0.2f);
tree1.setValue(openvdb::Coord(-5,-10,-20),0.3f);
tree2.setValue(openvdb::Coord( 5, 10, 20),0.4f);
tree2.setValue(openvdb::Coord(-5, 10,-20),0.5f);
tree2.setValue(openvdb::Coord( 5,-10,-20),0.6f);
tree2.setValue(openvdb::Coord(-5,-10,-20),0.7f);
tree2.setValue(openvdb::Coord(-5000, 2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord( 5000,-2000,-3000),4.5678f);
tree2.setValue(openvdb::Coord(-5000,-2000, 3000),4.5678f);
openvdb::FloatTree tree1_copy(tree1);
// tree3 has the same topology as tree2 but a different value type
const openvdb::Vec3f background2(1.0f,3.4f,6.0f), vec_val(3.1f,5.3f,-9.5f);
openvdb::Vec3fTree tree3(background2);
for (openvdb::FloatTree::ValueOnCIter iter = tree2.cbeginValueOn(); iter; ++iter) {
tree3.setValue(iter.getCoord(), vec_val);
}
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(4), tree0.leafCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(4), tree1.leafCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(7), tree2.leafCount());
CPPUNIT_ASSERT_EQUAL(openvdb::Index32(7), tree3.leafCount());
//tree1.topologyInterection(tree2);//should make tree1 = tree0
tree1.topologyIntersection(tree3);//should make tree1 = tree0
CPPUNIT_ASSERT(tree0.leafCount()==tree1.leafCount());
CPPUNIT_ASSERT(tree0.nonLeafCount()==tree1.nonLeafCount());
CPPUNIT_ASSERT(tree0.activeLeafVoxelCount()==tree1.activeLeafVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveLeafVoxelCount()==tree1.inactiveLeafVoxelCount());
CPPUNIT_ASSERT(tree0.activeVoxelCount()==tree1.activeVoxelCount());
CPPUNIT_ASSERT(tree0.inactiveVoxelCount()==tree1.inactiveVoxelCount());
CPPUNIT_ASSERT(tree1.hasSameTopology(tree0));
CPPUNIT_ASSERT(tree0.hasSameTopology(tree1));
for (openvdb::FloatTree::ValueOnCIter iter = tree0.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree1.isValueOn(p));
CPPUNIT_ASSERT(tree2.isValueOn(p));
CPPUNIT_ASSERT(tree3.isValueOn(p));
CPPUNIT_ASSERT(tree1_copy.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree1.getValue(p));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1_copy.cbeginValueOn(); iter; ++iter) {
CPPUNIT_ASSERT(tree1.isValueOn(iter.getCoord()));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree1.getValue(iter.getCoord()));
}
for (openvdb::FloatTree::ValueOnCIter iter = tree1.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree0.isValueOn(p));
CPPUNIT_ASSERT(tree2.isValueOn(p));
CPPUNIT_ASSERT(tree3.isValueOn(p));
CPPUNIT_ASSERT(tree1_copy.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter,tree0.getValue(p));
}
}
{// test overlapping spheres
const float background=5.0f, R0=10.0f, R1=5.6f;
const openvdb::Vec3f C0(35.0f, 30.0f, 40.0f), C1(22.3f, 30.5f, 31.0f);
const openvdb::Coord dim(32, 32, 32);
openvdb::FloatGrid grid0(background);
openvdb::FloatGrid grid1(background);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C0, R0, grid0,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C1, R1, grid1,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
openvdb::FloatTree& tree0 = grid0.tree();
openvdb::FloatTree& tree1 = grid1.tree();
openvdb::FloatTree tree0_copy(tree0);
tree0.topologyDifference(tree1);
const openvdb::Index64 n0 = tree0_copy.activeVoxelCount();
const openvdb::Index64 n = tree0.activeVoxelCount();
CPPUNIT_ASSERT( n < n0 );
for (openvdb::FloatTree::ValueOnCIter iter = tree0.cbeginValueOn(); iter; ++iter) {
const openvdb::Coord p = iter.getCoord();
CPPUNIT_ASSERT(tree1.isValueOff(p));
CPPUNIT_ASSERT(tree0_copy.isValueOn(p));
ASSERT_DOUBLES_EXACTLY_EQUAL(*iter, tree0_copy.getValue(p));
}
}
} // testTopologyDifference
////////////////////////////////////////
void
TestTree::testFill()
{
// Use a custom tree configuration to ensure we flood-fill at all levels!
using LeafT = openvdb::tree::LeafNode<float,2>;//4^3
using InternalT = openvdb::tree::InternalNode<LeafT,2>;//4^3
using RootT = openvdb::tree::RootNode<InternalT>;// child nodes are 16^3
using TreeT = openvdb::tree::Tree<RootT>;
const float outside = 2.0f, inside = -outside;
const openvdb::CoordBBox
bbox{openvdb::Coord{-3, -50, 30}, openvdb::Coord{13, 11, 323}},
otherBBox{openvdb::Coord{400, 401, 402}, openvdb::Coord{600}};
{// sparse fill
openvdb::Grid<TreeT>::Ptr grid = openvdb::Grid<TreeT>::create(outside);
TreeT& tree = grid->tree();
CPPUNIT_ASSERT(!tree.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(0), tree.activeVoxelCount());
for (openvdb::CoordBBox::Iterator<true> ijk(bbox); ijk; ++ijk) {
ASSERT_DOUBLES_EXACTLY_EQUAL(outside, tree.getValue(*ijk));
}
tree.sparseFill(bbox, inside, /*active=*/true);
CPPUNIT_ASSERT(tree.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(bbox.volume()), tree.activeVoxelCount());
for (openvdb::CoordBBox::Iterator<true> ijk(bbox); ijk; ++ijk) {
ASSERT_DOUBLES_EXACTLY_EQUAL(inside, tree.getValue(*ijk));
}
}
{// dense fill
openvdb::Grid<TreeT>::Ptr grid = openvdb::Grid<TreeT>::create(outside);
TreeT& tree = grid->tree();
CPPUNIT_ASSERT(!tree.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(openvdb::Index64(0), tree.activeVoxelCount());
for (openvdb::CoordBBox::Iterator<true> ijk(bbox); ijk; ++ijk) {
ASSERT_DOUBLES_EXACTLY_EQUAL(outside, tree.getValue(*ijk));
}
// Add some active tiles.
tree.sparseFill(otherBBox, inside, /*active=*/true);
CPPUNIT_ASSERT(tree.hasActiveTiles());
CPPUNIT_ASSERT_EQUAL(otherBBox.volume(), tree.activeVoxelCount());
tree.denseFill(bbox, inside, /*active=*/true);
// In OpenVDB 4.0.0 and earlier, denseFill() densified active tiles
// throughout the tree. Verify that it no longer does that.
CPPUNIT_ASSERT(tree.hasActiveTiles()); // i.e., otherBBox
CPPUNIT_ASSERT_EQUAL(bbox.volume() + otherBBox.volume(), tree.activeVoxelCount());
for (openvdb::CoordBBox::Iterator<true> ijk(bbox); ijk; ++ijk) {
ASSERT_DOUBLES_EXACTLY_EQUAL(inside, tree.getValue(*ijk));
}
tree.clear();
CPPUNIT_ASSERT(!tree.hasActiveTiles());
tree.sparseFill(otherBBox, inside, /*active=*/true);
CPPUNIT_ASSERT(tree.hasActiveTiles());
tree.denseFill(bbox, inside, /*active=*/false);
CPPUNIT_ASSERT(tree.hasActiveTiles()); // i.e., otherBBox
CPPUNIT_ASSERT_EQUAL(otherBBox.volume(), tree.activeVoxelCount());
// In OpenVDB 4.0.0 and earlier, denseFill() filled sparsely if given
// an inactive fill value. Verify that it now fills densely.
const int leafDepth = int(tree.treeDepth()) - 1;
for (openvdb::CoordBBox::Iterator<true> ijk(bbox); ijk; ++ijk) {
CPPUNIT_ASSERT_EQUAL(leafDepth, tree.getValueDepth(*ijk));
ASSERT_DOUBLES_EXACTLY_EQUAL(inside, tree.getValue(*ijk));
}
}
}// testFill
void
TestTree::testSignedFloodFill()
{
// Use a custom tree configuration to ensure we flood-fill at all levels!
using LeafT = openvdb::tree::LeafNode<float,2>;//4^3
using InternalT = openvdb::tree::InternalNode<LeafT,2>;//4^3
using RootT = openvdb::tree::RootNode<InternalT>;// child nodes are 16^3
using TreeT = openvdb::tree::Tree<RootT>;
const float outside = 2.0f, inside = -outside, radius = 20.0f;
{//first test flood filling of a leaf node
const LeafT::ValueType fill0=5, fill1=-fill0;
openvdb::tools::SignedFloodFillOp<TreeT> sff(fill0, fill1);
int D = LeafT::dim(), C=D/2;
openvdb::Coord origin(0,0,0), left(0,0,C-1), right(0,0,C);
LeafT leaf(origin,fill0);
for (int i=0; i<D; ++i) {
left[0]=right[0]=i;
for (int j=0; j<D; ++j) {
left[1]=right[1]=j;
leaf.setValueOn(left,fill0);
leaf.setValueOn(right,fill1);
}
}
const openvdb::Coord first(0,0,0), last(D-1,D-1,D-1);
CPPUNIT_ASSERT(!leaf.isValueOn(first));
CPPUNIT_ASSERT(!leaf.isValueOn(last));
CPPUNIT_ASSERT_EQUAL(fill0, leaf.getValue(first));
CPPUNIT_ASSERT_EQUAL(fill0, leaf.getValue(last));
sff(leaf);
CPPUNIT_ASSERT(!leaf.isValueOn(first));
CPPUNIT_ASSERT(!leaf.isValueOn(last));
CPPUNIT_ASSERT_EQUAL(fill0, leaf.getValue(first));
CPPUNIT_ASSERT_EQUAL(fill1, leaf.getValue(last));
}
openvdb::Grid<TreeT>::Ptr grid = openvdb::Grid<TreeT>::create(outside);
TreeT& tree = grid->tree();
const RootT& root = tree.root();
const openvdb::Coord dim(3*16, 3*16, 3*16);
const openvdb::Coord C(16+8,16+8,16+8);
CPPUNIT_ASSERT(!tree.isValueOn(C));
CPPUNIT_ASSERT(root.getTableSize()==0);
//make narrow band of sphere without setting sign for the background values!
openvdb::Grid<TreeT>::Accessor acc = grid->getAccessor();
const openvdb::Vec3f center(static_cast<float>(C[0]),
static_cast<float>(C[1]),
static_cast<float>(C[2]));
openvdb::Coord xyz;
for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) {
for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) {
for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) {
const openvdb::Vec3R p = grid->transform().indexToWorld(xyz);
const float dist = float((p-center).length() - radius);
if (fabs(dist) > outside) continue;
acc.setValue(xyz, dist);
}
}
}
// Check narrow band with incorrect background
const size_t size_before = root.getTableSize();
CPPUNIT_ASSERT(size_before>0);
CPPUNIT_ASSERT(!tree.isValueOn(C));
ASSERT_DOUBLES_EXACTLY_EQUAL(outside,tree.getValue(C));
for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) {
for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) {
for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) {
const openvdb::Vec3R p = grid->transform().indexToWorld(xyz);
const float dist = float((p-center).length() - radius);
const float val = acc.getValue(xyz);
if (dist < inside) {
ASSERT_DOUBLES_EXACTLY_EQUAL( val, outside);
} else if (dist>outside) {
ASSERT_DOUBLES_EXACTLY_EQUAL( val, outside);
} else {
ASSERT_DOUBLES_EXACTLY_EQUAL( val, dist );
}
}
}
}
CPPUNIT_ASSERT(tree.getValueDepth(C) == -1);//i.e. background value
openvdb::tools::signedFloodFill(tree);
CPPUNIT_ASSERT(tree.getValueDepth(C) == 0);//added inside tile to root
// Check narrow band with correct background
for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) {
for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) {
for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) {
const openvdb::Vec3R p = grid->transform().indexToWorld(xyz);
const float dist = float((p-center).length() - radius);
const float val = acc.getValue(xyz);
if (dist < inside) {
ASSERT_DOUBLES_EXACTLY_EQUAL( val, inside);
} else if (dist>outside) {
ASSERT_DOUBLES_EXACTLY_EQUAL( val, outside);
} else {
ASSERT_DOUBLES_EXACTLY_EQUAL( val, dist );
}
}
}
}
CPPUNIT_ASSERT(root.getTableSize()>size_before);//added inside root tiles
CPPUNIT_ASSERT(!tree.isValueOn(C));
ASSERT_DOUBLES_EXACTLY_EQUAL(inside,tree.getValue(C));
}
void
TestTree::testPruneInactive()
{
using openvdb::Coord;
using openvdb::Index32;
using openvdb::Index64;
const float background = 5.0;
openvdb::FloatTree tree(background);
// Verify that the newly-constructed tree is empty and that pruning it has no effect.
CPPUNIT_ASSERT(tree.empty());
openvdb::tools::prune(tree);
CPPUNIT_ASSERT(tree.empty());
openvdb::tools::pruneInactive(tree);
CPPUNIT_ASSERT(tree.empty());
// Set some active values.
tree.setValue(Coord(-5, 10, 20), 0.1f);
tree.setValue(Coord(-5,-10, 20), 0.4f);
tree.setValue(Coord(-5, 10,-20), 0.5f);
tree.setValue(Coord(-5,-10,-20), 0.7f);
tree.setValue(Coord( 5, 10, 20), 0.0f);
tree.setValue(Coord( 5,-10, 20), 0.2f);
tree.setValue(Coord( 5,-10,-20), 0.6f);
tree.setValue(Coord( 5, 10,-20), 0.3f);
// Verify that the tree has the expected numbers of active voxels and leaf nodes.
CPPUNIT_ASSERT_EQUAL(Index64(8), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(8), tree.leafCount());
// Verify that prune() has no effect, since the values are all different.
openvdb::tools::prune(tree);
CPPUNIT_ASSERT_EQUAL(Index64(8), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(8), tree.leafCount());
// Verify that pruneInactive() has no effect, since the values are active.
openvdb::tools::pruneInactive(tree);
CPPUNIT_ASSERT_EQUAL(Index64(8), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(8), tree.leafCount());
// Make some of the active values inactive, without changing their values.
tree.setValueOff(Coord(-5, 10, 20));
tree.setValueOff(Coord(-5,-10, 20));
tree.setValueOff(Coord(-5, 10,-20));
tree.setValueOff(Coord(-5,-10,-20));
CPPUNIT_ASSERT_EQUAL(Index64(4), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(8), tree.leafCount());
// Verify that prune() has no effect, since the values are still different.
openvdb::tools::prune(tree);
CPPUNIT_ASSERT_EQUAL(Index64(4), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(8), tree.leafCount());
// Verify that pruneInactive() prunes the nodes containing only inactive voxels.
openvdb::tools::pruneInactive(tree);
CPPUNIT_ASSERT_EQUAL(Index64(4), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(4), tree.leafCount());
// Make all of the active values inactive, without changing their values.
tree.setValueOff(Coord( 5, 10, 20));
tree.setValueOff(Coord( 5,-10, 20));
tree.setValueOff(Coord( 5,-10,-20));
tree.setValueOff(Coord( 5, 10,-20));
CPPUNIT_ASSERT_EQUAL(Index64(0), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(4), tree.leafCount());
// Verify that prune() has no effect, since the values are still different.
openvdb::tools::prune(tree);
CPPUNIT_ASSERT_EQUAL(Index64(0), tree.activeVoxelCount());
CPPUNIT_ASSERT_EQUAL(Index32(4), tree.leafCount());
// Verify that pruneInactive() prunes all of the remaining leaf nodes.
openvdb::tools::pruneInactive(tree);
CPPUNIT_ASSERT(tree.empty());
}
void
TestTree::testPruneLevelSet()
{
const float background=10.0f, R=5.6f;
const openvdb::Vec3f C(12.3f, 15.5f, 10.0f);
const openvdb::Coord dim(32, 32, 32);
openvdb::FloatGrid grid(background);
unittest_util::makeSphere<openvdb::FloatGrid>(dim, C, R, grid,
1.0f, unittest_util::SPHERE_SPARSE_NARROW_BAND);
openvdb::FloatTree& tree = grid.tree();
openvdb::Index64 count = 0;
openvdb::Coord xyz;
for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) {
for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) {
for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) {
if (fabs(tree.getValue(xyz))<background) ++count;
}
}
}
const openvdb::Index32 leafCount = tree.leafCount();
CPPUNIT_ASSERT_EQUAL(tree.activeVoxelCount(), count);
CPPUNIT_ASSERT_EQUAL(tree.activeLeafVoxelCount(), count);
openvdb::Index64 removed = 0;
const float new_width = background - 9.0f;
// This version is fast since it only visits voxel and avoids
// random access to set the voxels off.
using VoxelOnIter = openvdb::FloatTree::LeafNodeType::ValueOnIter;
for (openvdb::FloatTree::LeafIter lIter = tree.beginLeaf(); lIter; ++lIter) {
for (VoxelOnIter vIter = lIter->beginValueOn(); vIter; ++vIter) {
if (fabs(*vIter)<new_width) continue;
lIter->setValueOff(vIter.pos(), *vIter > 0.0f ? background : -background);
++removed;
}
}
// The following version is slower since it employs
// FloatTree::ValueOnIter that visits both tiles and voxels and
// also uses random acceess to set the voxels off.
/*
for (openvdb::FloatTree::ValueOnIter i = tree.beginValueOn(); i; ++i) {
if (fabs(*i)<new_width) continue;
tree.setValueOff(i.getCoord(), *i > 0.0f ? background : -background);
++removed2;
}
*/
CPPUNIT_ASSERT_EQUAL(leafCount, tree.leafCount());
//std::cerr << "Leaf count=" << tree.leafCount() << std::endl;
CPPUNIT_ASSERT_EQUAL(tree.activeVoxelCount(), count-removed);
CPPUNIT_ASSERT_EQUAL(tree.activeLeafVoxelCount(), count-removed);
openvdb::tools::pruneLevelSet(tree);
CPPUNIT_ASSERT(tree.leafCount() < leafCount);
//std::cerr << "Leaf count=" << tree.leafCount() << std::endl;
CPPUNIT_ASSERT_EQUAL(tree.activeVoxelCount(), count-removed);
CPPUNIT_ASSERT_EQUAL(tree.activeLeafVoxelCount(), count-removed);
openvdb::FloatTree::ValueOnCIter i = tree.cbeginValueOn();
for (; i; ++i) CPPUNIT_ASSERT( *i < new_width);
for (xyz[0]=0; xyz[0]<dim[0]; ++xyz[0]) {
for (xyz[1]=0; xyz[1]<dim[1]; ++xyz[1]) {
for (xyz[2]=0; xyz[2]<dim[2]; ++xyz[2]) {
const float val = tree.getValue(xyz);
if (fabs(val)<new_width)
CPPUNIT_ASSERT(tree.isValueOn(xyz));
else if (val < 0.0f) {
CPPUNIT_ASSERT(tree.isValueOff(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL( -background, val );
} else {
CPPUNIT_ASSERT(tree.isValueOff(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL( background, val );
}
}
}
}
}
void
TestTree::testTouchLeaf()
{
const float background=10.0f;
const openvdb::Coord xyz(-20,30,10);
{// test tree
openvdb::FloatTree::Ptr tree(new openvdb::FloatTree(background));
CPPUNIT_ASSERT_EQUAL(-1, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree->leafCount()));
CPPUNIT_ASSERT(tree->touchLeaf(xyz) != nullptr);
CPPUNIT_ASSERT_EQUAL( 3, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree->leafCount()));
CPPUNIT_ASSERT(!tree->isValueOn(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, tree->getValue(xyz));
}
{// test accessor
openvdb::FloatTree::Ptr tree(new openvdb::FloatTree(background));
openvdb::tree::ValueAccessor<openvdb::FloatTree> acc(*tree);
CPPUNIT_ASSERT_EQUAL(-1, acc.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree->leafCount()));
CPPUNIT_ASSERT(acc.touchLeaf(xyz) != nullptr);
CPPUNIT_ASSERT_EQUAL( 3, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree->leafCount()));
CPPUNIT_ASSERT(!acc.isValueOn(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL(background, acc.getValue(xyz));
}
}
void
TestTree::testProbeLeaf()
{
const float background=10.0f, value = 2.0f;
const openvdb::Coord xyz(-20,30,10);
{// test Tree::probeLeaf
openvdb::FloatTree::Ptr tree(new openvdb::FloatTree(background));
CPPUNIT_ASSERT_EQUAL(-1, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree->leafCount()));
CPPUNIT_ASSERT(tree->probeLeaf(xyz) == nullptr);
CPPUNIT_ASSERT_EQUAL(-1, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree->leafCount()));
tree->setValue(xyz, value);
CPPUNIT_ASSERT_EQUAL( 3, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree->leafCount()));
CPPUNIT_ASSERT(tree->probeLeaf(xyz) != nullptr);
CPPUNIT_ASSERT_EQUAL( 3, tree->getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree->leafCount()));
CPPUNIT_ASSERT(tree->isValueOn(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL(value, tree->getValue(xyz));
}
{// test Tree::probeConstLeaf
const openvdb::FloatTree tree1(background);
CPPUNIT_ASSERT_EQUAL(-1, tree1.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree1.leafCount()));
CPPUNIT_ASSERT(tree1.probeConstLeaf(xyz) == nullptr);
CPPUNIT_ASSERT_EQUAL(-1, tree1.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree1.leafCount()));
openvdb::FloatTree tmp(tree1);
tmp.setValue(xyz, value);
const openvdb::FloatTree tree2(tmp);
CPPUNIT_ASSERT_EQUAL( 3, tree2.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree2.leafCount()));
CPPUNIT_ASSERT(tree2.probeConstLeaf(xyz) != nullptr);
CPPUNIT_ASSERT_EQUAL( 3, tree2.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree2.leafCount()));
CPPUNIT_ASSERT(tree2.isValueOn(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL(value, tree2.getValue(xyz));
}
{// test ValueAccessor::probeLeaf
openvdb::FloatTree::Ptr tree(new openvdb::FloatTree(background));
openvdb::tree::ValueAccessor<openvdb::FloatTree> acc(*tree);
CPPUNIT_ASSERT_EQUAL(-1, acc.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree->leafCount()));
CPPUNIT_ASSERT(acc.probeLeaf(xyz) == nullptr);
CPPUNIT_ASSERT_EQUAL(-1, acc.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree->leafCount()));
acc.setValue(xyz, value);
CPPUNIT_ASSERT_EQUAL( 3, acc.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree->leafCount()));
CPPUNIT_ASSERT(acc.probeLeaf(xyz) != nullptr);
CPPUNIT_ASSERT_EQUAL( 3, acc.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree->leafCount()));
CPPUNIT_ASSERT(acc.isValueOn(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL(value, acc.getValue(xyz));
}
{// test ValueAccessor::probeConstLeaf
const openvdb::FloatTree tree1(background);
openvdb::tree::ValueAccessor<const openvdb::FloatTree> acc1(tree1);
CPPUNIT_ASSERT_EQUAL(-1, acc1.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree1.leafCount()));
CPPUNIT_ASSERT(acc1.probeConstLeaf(xyz) == nullptr);
CPPUNIT_ASSERT_EQUAL(-1, acc1.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 0, int(tree1.leafCount()));
openvdb::FloatTree tmp(tree1);
tmp.setValue(xyz, value);
const openvdb::FloatTree tree2(tmp);
openvdb::tree::ValueAccessor<const openvdb::FloatTree> acc2(tree2);
CPPUNIT_ASSERT_EQUAL( 3, acc2.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree2.leafCount()));
CPPUNIT_ASSERT(acc2.probeConstLeaf(xyz) != nullptr);
CPPUNIT_ASSERT_EQUAL( 3, acc2.getValueDepth(xyz));
CPPUNIT_ASSERT_EQUAL( 1, int(tree2.leafCount()));
CPPUNIT_ASSERT(acc2.isValueOn(xyz));
ASSERT_DOUBLES_EXACTLY_EQUAL(value, acc2.getValue(xyz));
}
}
void
TestTree::testAddLeaf()
{
using namespace openvdb;
using LeafT = FloatTree::LeafNodeType;
const Coord ijk(100);
FloatGrid grid;
FloatTree& tree = grid.tree();
tree.setValue(ijk, 5.0);
const LeafT* oldLeaf = tree.probeLeaf(ijk);
CPPUNIT_ASSERT(oldLeaf != nullptr);
ASSERT_DOUBLES_EXACTLY_EQUAL(5.0, oldLeaf->getValue(ijk));
LeafT* newLeaf = new LeafT;
newLeaf->setOrigin(oldLeaf->origin());
newLeaf->fill(3.0);
tree.addLeaf(newLeaf);
CPPUNIT_ASSERT_EQUAL(newLeaf, tree.probeLeaf(ijk));
ASSERT_DOUBLES_EXACTLY_EQUAL(3.0, tree.getValue(ijk));
}
void
TestTree::testAddTile()
{
using namespace openvdb;
const Coord ijk(100);
FloatGrid grid;
FloatTree& tree = grid.tree();
tree.setValue(ijk, 5.0);
CPPUNIT_ASSERT(tree.probeLeaf(ijk) != nullptr);
const Index lvl = FloatTree::DEPTH >> 1;
OPENVDB_NO_UNREACHABLE_CODE_WARNING_BEGIN
if (lvl > 0) tree.addTile(lvl,ijk, 3.0, /*active=*/true);
else tree.addTile(1,ijk, 3.0, /*active=*/true);
OPENVDB_NO_UNREACHABLE_CODE_WARNING_END
CPPUNIT_ASSERT(tree.probeLeaf(ijk) == nullptr);
ASSERT_DOUBLES_EXACTLY_EQUAL(3.0, tree.getValue(ijk));
}
struct BBoxOp
{
std::vector<openvdb::CoordBBox> bbox;
std::vector<openvdb::Index> level;
// This method is required by Tree::visitActiveBBox
// Since it will return false if LEVEL==0 it will never descent to
// the active voxels. In other words the smallest BBoxes
// correspond to LeafNodes or active tiles at LEVEL=1
template<openvdb::Index LEVEL>
inline bool descent() { return LEVEL>0; }
// This method is required by Tree::visitActiveBBox
template<openvdb::Index LEVEL>
inline void operator()(const openvdb::CoordBBox &_bbox) {
bbox.push_back(_bbox);
level.push_back(LEVEL);
}
};
void
TestTree::testProcessBBox()
{
using openvdb::Coord;
using openvdb::CoordBBox;
//check two leaf nodes and two tiles at each level 1, 2 and 3
const int size[4]={1<<3, 1<<3, 1<<(3+4), 1<<(3+4+5)};
for (int level=0; level<=3; ++level) {
openvdb::FloatTree tree;
const int n = size[level];
const CoordBBox bbox[]={CoordBBox::createCube(Coord(-n,-n,-n), n),
CoordBBox::createCube(Coord( 0, 0, 0), n)};
if (level==0) {
tree.setValue(Coord(-1,-2,-3), 1.0f);
tree.setValue(Coord( 1, 2, 3), 1.0f);
} else {
tree.fill(bbox[0], 1.0f, true);
tree.fill(bbox[1], 1.0f, true);
}
BBoxOp op;
tree.visitActiveBBox(op);
CPPUNIT_ASSERT_EQUAL(2, int(op.bbox.size()));
for (int i=0; i<2; ++i) {
//std::cerr <<"\nLevel="<<level<<" op.bbox["<<i<<"]="<<op.bbox[i]
// <<" op.level["<<i<<"]= "<<op.level[i]<<std::endl;
CPPUNIT_ASSERT_EQUAL(level,int(op.level[i]));
CPPUNIT_ASSERT(op.bbox[i] == bbox[i]);
}
}
}
void
TestTree::testGetNodes()
{
//unittest_util::CpuTimer timer;
using openvdb::CoordBBox;
using openvdb::Coord;
using openvdb::Vec3f;
using openvdb::FloatGrid;
using openvdb::FloatTree;
const Vec3f center(0.35f, 0.35f, 0.35f);
const float radius = 0.15f;
const int dim = 128, half_width = 5;
const float voxel_size = 1.0f/dim;
FloatGrid::Ptr grid = FloatGrid::create(/*background=*/half_width*voxel_size);
FloatTree& tree = grid->tree();
grid->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/voxel_size));
unittest_util::makeSphere<FloatGrid>(
Coord(dim), center, radius, *grid, unittest_util::SPHERE_SPARSE_NARROW_BAND);
const size_t leafCount = tree.leafCount();
const size_t voxelCount = tree.activeVoxelCount();
{//testing Tree::getNodes() with std::vector<T*>
std::vector<openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<T*> and Tree::getNodes()");
tree.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() with std::vector<const T*>
std::vector<const openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<const T*> and Tree::getNodes()");
tree.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() const with std::vector<const T*>
std::vector<const openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<const T*> and Tree::getNodes() const");
const FloatTree& tmp = tree;
tmp.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() with std::vector<T*> and std::vector::reserve
std::vector<openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<T*>, std::vector::reserve and Tree::getNodes");
array.reserve(tree.leafCount());
tree.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() with std::deque<T*>
std::deque<const openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::deque<T*> and Tree::getNodes");
tree.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() with std::deque<T*>
std::deque<const openvdb::FloatTree::RootNodeType::ChildNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::deque<T*> and Tree::getNodes");
tree.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(size_t(1), array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
}
{//testing Tree::getNodes() with std::deque<T*>
std::deque<const openvdb::FloatTree::RootNodeType::ChildNodeType::ChildNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::deque<T*> and Tree::getNodes");
tree.getNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(size_t(1), array.size());
CPPUNIT_ASSERT_EQUAL(leafCount, size_t(tree.leafCount()));
}
/*
{//testing Tree::getNodes() with std::deque<T*> where T is not part of the tree configuration
using NodeT = openvdb::tree::LeafNode<float, 5>;
std::deque<const NodeT*> array;
tree.getNodes(array);//should NOT compile since NodeT is not part of the FloatTree configuration
}
{//testing Tree::getNodes() const with std::deque<T*> where T is not part of the tree configuration
using NodeT = openvdb::tree::LeafNode<float, 5>;
std::deque<const NodeT*> array;
const FloatTree& tmp = tree;
tmp.getNodes(array);//should NOT compile since NodeT is not part of the FloatTree configuration
}
*/
}// testGetNodes
void
TestTree::testStealNodes()
{
//unittest_util::CpuTimer timer;
using openvdb::CoordBBox;
using openvdb::Coord;
using openvdb::Vec3f;
using openvdb::FloatGrid;
using openvdb::FloatTree;
const Vec3f center(0.35f, 0.35f, 0.35f);
const float radius = 0.15f;
const int dim = 128, half_width = 5;
const float voxel_size = 1.0f/dim;
FloatGrid::Ptr grid = FloatGrid::create(/*background=*/half_width*voxel_size);
const FloatTree& tree = grid->tree();
grid->setTransform(openvdb::math::Transform::createLinearTransform(/*voxel size=*/voxel_size));
unittest_util::makeSphere<FloatGrid>(
Coord(dim), center, radius, *grid, unittest_util::SPHERE_SPARSE_NARROW_BAND);
const size_t leafCount = tree.leafCount();
const size_t voxelCount = tree.activeVoxelCount();
{//testing Tree::stealNodes() with std::vector<T*>
FloatTree tree2 = tree;
std::vector<openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<T*> and Tree::stealNodes()");
tree2.stealNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::stealNodes() with std::vector<const T*>
FloatTree tree2 = tree;
std::vector<const openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<const T*> and Tree::stealNodes()");
tree2.stealNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::stealNodes() const with std::vector<const T*>
FloatTree tree2 = tree;
std::vector<const openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<const T*> and Tree::stealNodes() const");
tree2.stealNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::stealNodes() with std::vector<T*> and std::vector::reserve
FloatTree tree2 = tree;
std::vector<openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::vector<T*>, std::vector::reserve and Tree::stealNodes");
array.reserve(tree2.leafCount());
tree2.stealNodes(array, 0.0f, false);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() with std::deque<T*>
FloatTree tree2 = tree;
std::deque<const openvdb::FloatTree::LeafNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::deque<T*> and Tree::stealNodes");
tree2.stealNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(leafCount, array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
size_t sum = 0;
for (size_t i=0; i<array.size(); ++i) sum += array[i]->onVoxelCount();
CPPUNIT_ASSERT_EQUAL(voxelCount, sum);
}
{//testing Tree::getNodes() with std::deque<T*>
FloatTree tree2 = tree;
std::deque<const openvdb::FloatTree::RootNodeType::ChildNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::deque<T*> and Tree::stealNodes");
tree2.stealNodes(array, 0.0f, true);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(size_t(1), array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
}
{//testing Tree::getNodes() with std::deque<T*>
FloatTree tree2 = tree;
std::deque<const openvdb::FloatTree::RootNodeType::ChildNodeType::ChildNodeType*> array;
CPPUNIT_ASSERT_EQUAL(size_t(0), array.size());
//timer.start("\nstd::deque<T*> and Tree::stealNodes");
tree2.stealNodes(array);
//timer.stop();
CPPUNIT_ASSERT_EQUAL(size_t(1), array.size());
CPPUNIT_ASSERT_EQUAL(size_t(0), size_t(tree2.leafCount()));
}
/*
{//testing Tree::stealNodes() with std::deque<T*> where T is not part of the tree configuration
FloatTree tree2 = tree;
using NodeT = openvdb::tree::LeafNode<float, 5>;
std::deque<const NodeT*> array;
//should NOT compile since NodeT is not part of the FloatTree configuration
tree2.stealNodes(array, 0.0f, true);
}
*/
}// testStealNodes
void
TestTree::testStealNode()
{
using openvdb::Index;
using openvdb::FloatTree;
const float background=0.0f, value = 5.6f, epsilon=0.000001f;
const openvdb::Coord xyz(-23,42,70);
{// stal a LeafNode
using NodeT = FloatTree::LeafNodeType;
CPPUNIT_ASSERT_EQUAL(Index(0), NodeT::getLevel());
FloatTree tree(background);
CPPUNIT_ASSERT_EQUAL(Index(0), tree.leafCount());
CPPUNIT_ASSERT(!tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(background, tree.getValue(xyz), epsilon);
CPPUNIT_ASSERT(tree.root().stealNode<NodeT>(xyz, value, false) == nullptr);
tree.setValue(xyz, value);
CPPUNIT_ASSERT_EQUAL(Index(1), tree.leafCount());
CPPUNIT_ASSERT(tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(value, tree.getValue(xyz), epsilon);
NodeT* node = tree.root().stealNode<NodeT>(xyz, background, false);
CPPUNIT_ASSERT(node != nullptr);
CPPUNIT_ASSERT_EQUAL(Index(0), tree.leafCount());
CPPUNIT_ASSERT(!tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(background, tree.getValue(xyz), epsilon);
CPPUNIT_ASSERT(tree.root().stealNode<NodeT>(xyz, value, false) == nullptr);
CPPUNIT_ASSERT_DOUBLES_EQUAL(value, node->getValue(xyz), epsilon);
CPPUNIT_ASSERT(node->isValueOn(xyz));
delete node;
}
{// steal a bottom InternalNode
using NodeT = FloatTree::RootNodeType::ChildNodeType::ChildNodeType;
CPPUNIT_ASSERT_EQUAL(Index(1), NodeT::getLevel());
FloatTree tree(background);
CPPUNIT_ASSERT_EQUAL(Index(0), tree.leafCount());
CPPUNIT_ASSERT(!tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(background, tree.getValue(xyz), epsilon);
CPPUNIT_ASSERT(tree.root().stealNode<NodeT>(xyz, value, false) == nullptr);
tree.setValue(xyz, value);
CPPUNIT_ASSERT_EQUAL(Index(1), tree.leafCount());
CPPUNIT_ASSERT(tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(value, tree.getValue(xyz), epsilon);
NodeT* node = tree.root().stealNode<NodeT>(xyz, background, false);
CPPUNIT_ASSERT(node != nullptr);
CPPUNIT_ASSERT_EQUAL(Index(0), tree.leafCount());
CPPUNIT_ASSERT(!tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(background, tree.getValue(xyz), epsilon);
CPPUNIT_ASSERT(tree.root().stealNode<NodeT>(xyz, value, false) == nullptr);
CPPUNIT_ASSERT_DOUBLES_EQUAL(value, node->getValue(xyz), epsilon);
CPPUNIT_ASSERT(node->isValueOn(xyz));
delete node;
}
{// steal a top InternalNode
using NodeT = FloatTree::RootNodeType::ChildNodeType;
CPPUNIT_ASSERT_EQUAL(Index(2), NodeT::getLevel());
FloatTree tree(background);
CPPUNIT_ASSERT_EQUAL(Index(0), tree.leafCount());
CPPUNIT_ASSERT(!tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(background, tree.getValue(xyz), epsilon);
CPPUNIT_ASSERT(tree.root().stealNode<NodeT>(xyz, value, false) == nullptr);
tree.setValue(xyz, value);
CPPUNIT_ASSERT_EQUAL(Index(1), tree.leafCount());
CPPUNIT_ASSERT(tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(value, tree.getValue(xyz), epsilon);
NodeT* node = tree.root().stealNode<NodeT>(xyz, background, false);
CPPUNIT_ASSERT(node != nullptr);
CPPUNIT_ASSERT_EQUAL(Index(0), tree.leafCount());
CPPUNIT_ASSERT(!tree.isValueOn(xyz));
CPPUNIT_ASSERT_DOUBLES_EQUAL(background, tree.getValue(xyz), epsilon);
CPPUNIT_ASSERT(tree.root().stealNode<NodeT>(xyz, value, false) == nullptr);
CPPUNIT_ASSERT_DOUBLES_EQUAL(value, node->getValue(xyz), epsilon);
CPPUNIT_ASSERT(node->isValueOn(xyz));
delete node;
}
}
// Copyright (c) 2012-2018 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|