/usr/include/openvdb/tools/LevelSetUtil.h is in libopenvdb-dev 5.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2012-2017 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.
//
///////////////////////////////////////////////////////////////////////////
/// @file tools/LevelSetUtil.h
///
/// @brief Miscellaneous utility methods that operate primarily
/// or exclusively on level set grids.
///
/// @author Mihai Alden
#ifndef OPENVDB_TOOLS_LEVEL_SET_UTIL_HAS_BEEN_INCLUDED
#define OPENVDB_TOOLS_LEVEL_SET_UTIL_HAS_BEEN_INCLUDED
#include "MeshToVolume.h" // for traceExteriorBoundaries
#include "SignedFloodFill.h" // for signedFloodFillWithValues
#include <openvdb/Types.h>
#include <openvdb/Grid.h>
#include <boost/scoped_array.hpp>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
#include <tbb/parallel_sort.h>
#include <limits>
namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace tools {
// MS Visual C++ requires this extra level of indirection in order to compile
// THIS MUST EXIST IN AN UNNAMED NAMESPACE IN ORDER TO COMPILE ON WINDOWS
namespace {
template<typename GridType>
inline typename GridType::ValueType lsutilGridMax()
{
return std::numeric_limits<typename GridType::ValueType>::max();
}
template<typename GridType>
inline typename GridType::ValueType lsutilGridZero()
{
return zeroVal<typename GridType::ValueType>();
}
} // unnamed namespace
////////////////////////////////////////
/// @brief Threaded method to convert a sparse level set/SDF into a sparse fog volume
///
/// @details For a level set, the active and negative-valued interior half of the
/// narrow band becomes a linear ramp from 0 to 1; the inactive interior becomes
/// active with a constant value of 1; and the exterior, including the background
/// and the active exterior half of the narrow band, becomes inactive with a constant
/// value of 0. The interior, though active, remains sparse.
/// @details For a generic SDF, a specified cutoff distance determines the width
/// of the ramp, but otherwise the result is the same as for a level set.
///
/// @param grid level set/SDF grid to transform
/// @param cutoffDistance optional world space cutoff distance for the ramp
/// (automatically clamped if greater than the interior
/// narrow band width)
template<class GridType>
inline void
sdfToFogVolume(
GridType& grid,
typename GridType::ValueType cutoffDistance = lsutilGridMax<GridType>());
/// @brief Threaded method to construct a boolean mask that represents interior regions
/// in a signed distance field.
///
/// @return A shared pointer to either a boolean grid or tree with the same tree
/// configuration and potentially transform as the input @c volume and whose active
/// and @c true values correspond to the interior of the input signed distance field.
///
/// @param volume Signed distance field / level set volume.
/// @param isovalue Threshold below which values are considered part of the
/// interior region.
template<class GridOrTreeType>
inline typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr
sdfInteriorMask(
const GridOrTreeType& volume,
typename GridOrTreeType::ValueType isovalue = lsutilGridZero<GridOrTreeType>());
/// @brief Extracts the interior regions of a signed distance field and topologically enclosed
/// (watertight) regions of value greater than the @a isovalue (cavities) that can arise
/// as the result of CSG union operations between different shapes where at least one of
/// the shapes has a concavity that is capped.
///
/// For example the enclosed region of a capped bottle would include the walls and
/// the interior cavity.
///
/// @return A shared pointer to either a boolean grid or tree with the same tree configuration
/// and potentially transform as the input @c volume and whose active and @c true values
/// correspond to the interior and enclosed regions in the input signed distance field.
///
/// @param volume Signed distance field / level set volume.
/// @param isovalue Threshold below which values are considered part of the interior region.
/// @param fillMask Optional boolean tree, when provided enclosed cavity regions that are not
/// completely filled by this mask are ignored.
///
/// For instance if the fill mask does not completely fill the bottle in the
/// previous example only the walls and cap are returned and the interior
/// cavity will be ignored.
template<typename GridOrTreeType>
inline typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr
extractEnclosedRegion(
const GridOrTreeType& volume,
typename GridOrTreeType::ValueType isovalue = lsutilGridZero<GridOrTreeType>(),
const typename TreeAdapter<GridOrTreeType>::TreeType::template ValueConverter<bool>::Type*
fillMask = nullptr);
/// @brief Return a mask of the voxels that intersect the implicit surface with
/// the given @a isovalue.
///
/// @param volume Signed distance field / level set volume.
/// @param isovalue The crossing point that is considered the surface.
template<typename GridOrTreeType>
inline typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr
extractIsosurfaceMask(const GridOrTreeType& volume, typename GridOrTreeType::ValueType isovalue);
/// @brief Return a mask for each connected component of the given grid's active voxels.
///
/// @param volume Input grid or tree
/// @param masks Output set of disjoint active topology masks sorted in descending order
/// based on the active voxel count.
template<typename GridOrTreeType>
inline void
extractActiveVoxelSegmentMasks(const GridOrTreeType& volume,
std::vector<typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr>& masks);
/// @brief Separates disjoint active topology components into distinct grids or trees.
///
/// @details Supports volumes with active tiles.
///
/// @param volume Input grid or tree
/// @param segments Output set of disjoint active topology components sorted in
/// descending order based on the active voxel count.
template<typename GridOrTreeType>
inline void
segmentActiveVoxels(const GridOrTreeType& volume,
std::vector<typename GridOrTreeType::Ptr>& segments);
/// @brief Separates disjoint SDF surfaces into distinct grids or trees.
///
/// @details Supports asymmetric interior / exterior narrowband widths and
/// SDF volumes with dense interior regions.
///
/// @param volume Input signed distance field / level set volume
/// @param segments Output set of disjoint SDF surfaces found in @a volume sorted in
/// descending order based on the surface intersecting voxel count.
template<typename GridOrTreeType>
inline void
segmentSDF(const GridOrTreeType& volume, std::vector<typename GridOrTreeType::Ptr>& segments);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Internal utility objects and implementation details
namespace level_set_util_internal {
template<typename LeafNodeType>
struct MaskInteriorVoxels {
using ValueType = typename LeafNodeType::ValueType;
using BoolLeafNodeType = tree::LeafNode<bool, LeafNodeType::LOG2DIM>;
MaskInteriorVoxels(
ValueType isovalue, const LeafNodeType ** nodes, BoolLeafNodeType ** maskNodes)
: mNodes(nodes), mMaskNodes(maskNodes), mIsovalue(isovalue)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
BoolLeafNodeType * maskNodePt = nullptr;
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
mMaskNodes[n] = nullptr;
const LeafNodeType& node = *mNodes[n];
if (!maskNodePt) {
maskNodePt = new BoolLeafNodeType(node.origin(), false);
} else {
maskNodePt->setOrigin(node.origin());
}
const ValueType* values = &node.getValue(0);
for (Index i = 0; i < LeafNodeType::SIZE; ++i) {
if (values[i] < mIsovalue) maskNodePt->setValueOn(i, true);
}
if (maskNodePt->onVoxelCount() > 0) {
mMaskNodes[n] = maskNodePt;
maskNodePt = nullptr;
}
}
if (maskNodePt) delete maskNodePt;
}
LeafNodeType const * const * const mNodes;
BoolLeafNodeType ** const mMaskNodes;
ValueType const mIsovalue;
}; // MaskInteriorVoxels
template<typename TreeType, typename InternalNodeType>
struct MaskInteriorTiles {
using ValueType = typename TreeType::ValueType;
MaskInteriorTiles(ValueType isovalue, const TreeType& tree, InternalNodeType ** maskNodes)
: mTree(&tree), mMaskNodes(maskNodes), mIsovalue(isovalue) { }
void operator()(const tbb::blocked_range<size_t>& range) const {
tree::ValueAccessor<const TreeType> acc(*mTree);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
typename InternalNodeType::ValueAllIter it = mMaskNodes[n]->beginValueAll();
for (; it; ++it) {
if (acc.getValue(it.getCoord()) < mIsovalue) {
it.setValue(true);
it.setValueOn(true);
}
}
}
}
TreeType const * const mTree;
InternalNodeType ** const mMaskNodes;
ValueType const mIsovalue;
}; // MaskInteriorTiles
template<typename TreeType>
struct PopulateTree {
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
PopulateTree(TreeType& tree, LeafNodeType** leafnodes,
const size_t * nodexIndexMap, ValueType background)
: mNewTree(background)
, mTreePt(&tree)
, mNodes(leafnodes)
, mNodeIndexMap(nodexIndexMap)
{
}
PopulateTree(PopulateTree& rhs, tbb::split)
: mNewTree(rhs.mNewTree.background())
, mTreePt(&mNewTree)
, mNodes(rhs.mNodes)
, mNodeIndexMap(rhs.mNodeIndexMap)
{
}
void operator()(const tbb::blocked_range<size_t>& range) {
tree::ValueAccessor<TreeType> acc(*mTreePt);
if (mNodeIndexMap) {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
for (size_t i = mNodeIndexMap[n], I = mNodeIndexMap[n + 1]; i < I; ++i) {
if (mNodes[i] != nullptr) acc.addLeaf(mNodes[i]);
}
}
} else {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
acc.addLeaf(mNodes[n]);
}
}
}
void join(PopulateTree& rhs) { mTreePt->merge(*rhs.mTreePt); }
private:
TreeType mNewTree;
TreeType * const mTreePt;
LeafNodeType ** const mNodes;
size_t const * const mNodeIndexMap;
}; // PopulateTree
/// @brief Negative active values are set @c 0, everything else is set to @c 1.
template<typename LeafNodeType>
struct LabelBoundaryVoxels {
using ValueType = typename LeafNodeType::ValueType;
using CharLeafNodeType = tree::LeafNode<char, LeafNodeType::LOG2DIM>;
LabelBoundaryVoxels(
ValueType isovalue, const LeafNodeType ** nodes, CharLeafNodeType ** maskNodes)
: mNodes(nodes), mMaskNodes(maskNodes), mIsovalue(isovalue)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
CharLeafNodeType * maskNodePt = nullptr;
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
mMaskNodes[n] = nullptr;
const LeafNodeType& node = *mNodes[n];
if (!maskNodePt) {
maskNodePt = new CharLeafNodeType(node.origin(), 1);
} else {
maskNodePt->setOrigin(node.origin());
}
typename LeafNodeType::ValueOnCIter it;
for (it = node.cbeginValueOn(); it; ++it) {
maskNodePt->setValueOn(it.pos(), ((*it - mIsovalue) < 0.0) ? 0 : 1);
}
if (maskNodePt->onVoxelCount() > 0) {
mMaskNodes[n] = maskNodePt;
maskNodePt = nullptr;
}
}
if (maskNodePt) delete maskNodePt;
}
LeafNodeType const * const * const mNodes;
CharLeafNodeType ** const mMaskNodes;
ValueType const mIsovalue;
}; // LabelBoundaryVoxels
template<typename LeafNodeType>
struct FlipRegionSign {
using ValueType = typename LeafNodeType::ValueType;
FlipRegionSign(LeafNodeType ** nodes) : mNodes(nodes) { }
void operator()(const tbb::blocked_range<size_t>& range) const {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
ValueType* values = const_cast<ValueType*>(&mNodes[n]->getValue(0));
for (Index i = 0; i < LeafNodeType::SIZE; ++i) {
values[i] = values[i] < 0 ? 1 : -1;
}
}
}
LeafNodeType ** const mNodes;
}; // FlipRegionSign
template<typename LeafNodeType>
struct FindMinVoxelValue {
using ValueType = typename LeafNodeType::ValueType;
FindMinVoxelValue(LeafNodeType const * const * const leafnodes)
: minValue(std::numeric_limits<ValueType>::max())
, mNodes(leafnodes)
{
}
FindMinVoxelValue(FindMinVoxelValue& rhs, tbb::split)
: minValue(std::numeric_limits<ValueType>::max())
, mNodes(rhs.mNodes)
{
}
void operator()(const tbb::blocked_range<size_t>& range) {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
const ValueType* data = mNodes[n]->buffer().data();
for (Index i = 0; i < LeafNodeType::SIZE; ++i) {
minValue = std::min(minValue, data[i]);
}
}
}
void join(FindMinVoxelValue& rhs) { minValue = std::min(minValue, rhs.minValue); }
ValueType minValue;
LeafNodeType const * const * const mNodes;
}; // FindMinVoxelValue
template<typename InternalNodeType>
struct FindMinTileValue {
using ValueType = typename InternalNodeType::ValueType;
FindMinTileValue(InternalNodeType const * const * const nodes)
: minValue(std::numeric_limits<ValueType>::max())
, mNodes(nodes)
{
}
FindMinTileValue(FindMinTileValue& rhs, tbb::split)
: minValue(std::numeric_limits<ValueType>::max())
, mNodes(rhs.mNodes)
{
}
void operator()(const tbb::blocked_range<size_t>& range) {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
typename InternalNodeType::ValueAllCIter it = mNodes[n]->beginValueAll();
for (; it; ++it) {
minValue = std::min(minValue, *it);
}
}
}
void join(FindMinTileValue& rhs) { minValue = std::min(minValue, rhs.minValue); }
ValueType minValue;
InternalNodeType const * const * const mNodes;
}; // FindMinTileValue
template<typename LeafNodeType>
struct SDFVoxelsToFogVolume {
using ValueType = typename LeafNodeType::ValueType;
SDFVoxelsToFogVolume(LeafNodeType ** nodes, ValueType cutoffDistance)
: mNodes(nodes), mWeight(ValueType(1.0) / cutoffDistance)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
LeafNodeType& node = *mNodes[n];
node.setValuesOff();
ValueType* values = node.buffer().data();
for (Index i = 0; i < LeafNodeType::SIZE; ++i) {
values[i] = values[i] > ValueType(0.0) ? ValueType(0.0) : values[i] * mWeight;
if (values[i] > ValueType(0.0)) node.setValueOn(i);
}
if (node.onVoxelCount() == 0) {
delete mNodes[n];
mNodes[n] = nullptr;
}
}
}
LeafNodeType ** const mNodes;
ValueType const mWeight;
}; // SDFVoxelsToFogVolume
template<typename TreeType, typename InternalNodeType>
struct SDFTilesToFogVolume {
SDFTilesToFogVolume(const TreeType& tree, InternalNodeType ** nodes)
: mTree(&tree), mNodes(nodes) { }
void operator()(const tbb::blocked_range<size_t>& range) const {
using ValueType = typename TreeType::ValueType;
tree::ValueAccessor<const TreeType> acc(*mTree);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
typename InternalNodeType::ValueAllIter it = mNodes[n]->beginValueAll();
for (; it; ++it) {
if (acc.getValue(it.getCoord()) < ValueType(0.0)) {
it.setValue(ValueType(1.0));
it.setValueOn(true);
}
}
}
}
TreeType const * const mTree;
InternalNodeType ** const mNodes;
}; // SDFTilesToFogVolume
template<typename TreeType>
struct FillMaskBoundary {
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
FillMaskBoundary(const TreeType& tree, ValueType isovalue, const BoolTreeType& fillMask,
const BoolLeafNodeType ** fillNodes, BoolLeafNodeType ** newNodes)
: mTree(&tree)
, mFillMask(&fillMask)
, mFillNodes(fillNodes)
, mNewNodes(newNodes)
, mIsovalue(isovalue)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
tree::ValueAccessor<const BoolTreeType> maskAcc(*mFillMask);
tree::ValueAccessor<const TreeType> distAcc(*mTree);
boost::scoped_array<char> valueMask(new char[BoolLeafNodeType::SIZE]);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
mNewNodes[n] = nullptr;
const BoolLeafNodeType& node = *mFillNodes[n];
const Coord& origin = node.origin();
const bool denseNode = node.isDense();
// possible early out if the fill mask is dense
if (denseNode) {
int denseNeighbors = 0;
const BoolLeafNodeType* neighborNode =
maskAcc.probeConstLeaf(origin.offsetBy(-1, 0, 0));
if (neighborNode && neighborNode->isDense()) ++denseNeighbors;
neighborNode = maskAcc.probeConstLeaf(origin.offsetBy(BoolLeafNodeType::DIM, 0, 0));
if (neighborNode && neighborNode->isDense()) ++denseNeighbors;
neighborNode = maskAcc.probeConstLeaf(origin.offsetBy(0, -1, 0));
if (neighborNode && neighborNode->isDense()) ++denseNeighbors;
neighborNode = maskAcc.probeConstLeaf(origin.offsetBy(0, BoolLeafNodeType::DIM, 0));
if (neighborNode && neighborNode->isDense()) ++denseNeighbors;
neighborNode = maskAcc.probeConstLeaf(origin.offsetBy(0, 0, -1));
if (neighborNode && neighborNode->isDense()) ++denseNeighbors;
neighborNode = maskAcc.probeConstLeaf(origin.offsetBy(0, 0, BoolLeafNodeType::DIM));
if (neighborNode && neighborNode->isDense()) ++denseNeighbors;
if (denseNeighbors == 6) continue;
}
// rest value mask
memset(valueMask.get(), 0, sizeof(char) * BoolLeafNodeType::SIZE);
const typename TreeType::LeafNodeType* distNode = distAcc.probeConstLeaf(origin);
// check internal voxel neighbors
bool earlyTermination = false;
if (!denseNode) {
if (distNode) {
evalInternalNeighborsP(valueMask.get(), node, *distNode);
evalInternalNeighborsN(valueMask.get(), node, *distNode);
} else if (distAcc.getValue(origin) > mIsovalue) {
earlyTermination = evalInternalNeighborsP(valueMask.get(), node);
if (!earlyTermination) {
earlyTermination = evalInternalNeighborsN(valueMask.get(), node);
}
}
}
// check external voxel neighbors
if (!earlyTermination) {
evalExternalNeighborsX<true>(valueMask.get(), node, maskAcc, distAcc);
evalExternalNeighborsX<false>(valueMask.get(), node, maskAcc, distAcc);
evalExternalNeighborsY<true>(valueMask.get(), node, maskAcc, distAcc);
evalExternalNeighborsY<false>(valueMask.get(), node, maskAcc, distAcc);
evalExternalNeighborsZ<true>(valueMask.get(), node, maskAcc, distAcc);
evalExternalNeighborsZ<false>(valueMask.get(), node, maskAcc, distAcc);
}
// Export marked boundary voxels.
int numBoundaryValues = 0;
for (Index i = 0, I = BoolLeafNodeType::SIZE; i < I; ++i) {
numBoundaryValues += valueMask[i] == 1;
}
if (numBoundaryValues > 0) {
mNewNodes[n] = new BoolLeafNodeType(origin, false);
for (Index i = 0, I = BoolLeafNodeType::SIZE; i < I; ++i) {
if (valueMask[i] == 1) mNewNodes[n]->setValueOn(i);
}
}
}
}
private:
// Check internal voxel neighbors in positive {x, y, z} directions.
void evalInternalNeighborsP(char* valueMask, const BoolLeafNodeType& node,
const LeafNodeType& distNode) const
{
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM - 1; ++z) {
const Index pos = yPos + z;
if (valueMask[pos] != 0 || !node.isValueOn(pos)) continue;
if (!node.isValueOn(pos + 1) && distNode.getValue(pos + 1) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM - 1; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (valueMask[pos] != 0 || !node.isValueOn(pos)) continue;
if (!node.isValueOn(pos + BoolLeafNodeType::DIM) &&
distNode.getValue(pos + BoolLeafNodeType::DIM) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
for (Index x = 0; x < BoolLeafNodeType::DIM - 1; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (valueMask[pos] != 0 || !node.isValueOn(pos)) continue;
if (!node.isValueOn(pos + BoolLeafNodeType::DIM * BoolLeafNodeType::DIM) &&
(distNode.getValue(pos + BoolLeafNodeType::DIM * BoolLeafNodeType::DIM)
> mIsovalue))
{
valueMask[pos] = 1;
}
}
}
}
}
bool evalInternalNeighborsP(char* valueMask, const BoolLeafNodeType& node) const {
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM - 1; ++z) {
const Index pos = yPos + z;
if (node.isValueOn(pos) && !node.isValueOn(pos + 1)) {
valueMask[pos] = 1;
return true;
}
}
}
}
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM - 1; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (node.isValueOn(pos) && !node.isValueOn(pos + BoolLeafNodeType::DIM)) {
valueMask[pos] = 1;
return true;
}
}
}
}
for (Index x = 0; x < BoolLeafNodeType::DIM - 1; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (node.isValueOn(pos) &&
!node.isValueOn(pos + BoolLeafNodeType::DIM * BoolLeafNodeType::DIM)) {
valueMask[pos] = 1;
return true;
}
}
}
}
return false;
}
// Check internal voxel neighbors in negative {x, y, z} directions.
void evalInternalNeighborsN(char* valueMask, const BoolLeafNodeType& node,
const LeafNodeType& distNode) const
{
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 1; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (valueMask[pos] != 0 || !node.isValueOn(pos)) continue;
if (!node.isValueOn(pos - 1) && distNode.getValue(pos - 1) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 1; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (valueMask[pos] != 0 || !node.isValueOn(pos)) continue;
if (!node.isValueOn(pos - BoolLeafNodeType::DIM) &&
distNode.getValue(pos - BoolLeafNodeType::DIM) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
for (Index x = 1; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (valueMask[pos] != 0 || !node.isValueOn(pos)) continue;
if (!node.isValueOn(pos - BoolLeafNodeType::DIM * BoolLeafNodeType::DIM) &&
(distNode.getValue(pos - BoolLeafNodeType::DIM * BoolLeafNodeType::DIM)
> mIsovalue))
{
valueMask[pos] = 1;
}
}
}
}
}
bool evalInternalNeighborsN(char* valueMask, const BoolLeafNodeType& node) const {
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 1; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (node.isValueOn(pos) && !node.isValueOn(pos - 1)) {
valueMask[pos] = 1;
return true;
}
}
}
}
for (Index x = 0; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 1; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (node.isValueOn(pos) && !node.isValueOn(pos - BoolLeafNodeType::DIM)) {
valueMask[pos] = 1;
return true;
}
}
}
}
for (Index x = 1; x < BoolLeafNodeType::DIM; ++x) {
const Index xPos = x << (2 * BoolLeafNodeType::LOG2DIM);
for (Index y = 0; y < BoolLeafNodeType::DIM; ++y) {
const Index yPos = xPos + (y << BoolLeafNodeType::LOG2DIM);
for (Index z = 0; z < BoolLeafNodeType::DIM; ++z) {
const Index pos = yPos + z;
if (node.isValueOn(pos) &&
!node.isValueOn(pos - BoolLeafNodeType::DIM * BoolLeafNodeType::DIM)) {
valueMask[pos] = 1;
return true;
}
}
}
}
return false;
}
// Check external voxel neighbors
// If UpWind is true check the X+ oriented node face, else the X- oriented face.
template<bool UpWind>
void evalExternalNeighborsX(char* valueMask, const BoolLeafNodeType& node,
const tree::ValueAccessor<const BoolTreeType>& maskAcc,
const tree::ValueAccessor<const TreeType>& distAcc) const {
const Coord& origin = node.origin();
Coord ijk(0, 0, 0), nijk;
int step = -1;
if (UpWind) {
step = 1;
ijk[0] = int(BoolLeafNodeType::DIM) - 1;
}
const Index xPos = ijk[0] << (2 * int(BoolLeafNodeType::LOG2DIM));
for (ijk[1] = 0; ijk[1] < int(BoolLeafNodeType::DIM); ++ijk[1]) {
const Index yPos = xPos + (ijk[1] << int(BoolLeafNodeType::LOG2DIM));
for (ijk[2] = 0; ijk[2] < int(BoolLeafNodeType::DIM); ++ijk[2]) {
const Index pos = yPos + ijk[2];
if (valueMask[pos] == 0 && node.isValueOn(pos)) {
nijk = origin + ijk.offsetBy(step, 0, 0);
if (!maskAcc.isValueOn(nijk) && distAcc.getValue(nijk) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
}
// If UpWind is true check the Y+ oriented node face, else the Y- oriented face.
template<bool UpWind>
void evalExternalNeighborsY(char* valueMask, const BoolLeafNodeType& node,
const tree::ValueAccessor<const BoolTreeType>& maskAcc,
const tree::ValueAccessor<const TreeType>& distAcc) const {
const Coord& origin = node.origin();
Coord ijk(0, 0, 0), nijk;
int step = -1;
if (UpWind) {
step = 1;
ijk[1] = int(BoolLeafNodeType::DIM) - 1;
}
const Index yPos = ijk[1] << int(BoolLeafNodeType::LOG2DIM);
for (ijk[0] = 0; ijk[0] < int(BoolLeafNodeType::DIM); ++ijk[0]) {
const Index xPos = yPos + (ijk[0] << (2 * int(BoolLeafNodeType::LOG2DIM)));
for (ijk[2] = 0; ijk[2] < int(BoolLeafNodeType::DIM); ++ijk[2]) {
const Index pos = xPos + ijk[2];
if (valueMask[pos] == 0 && node.isValueOn(pos)) {
nijk = origin + ijk.offsetBy(0, step, 0);
if (!maskAcc.isValueOn(nijk) && distAcc.getValue(nijk) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
}
// If UpWind is true check the Z+ oriented node face, else the Z- oriented face.
template<bool UpWind>
void evalExternalNeighborsZ(char* valueMask, const BoolLeafNodeType& node,
const tree::ValueAccessor<const BoolTreeType>& maskAcc,
const tree::ValueAccessor<const TreeType>& distAcc) const {
const Coord& origin = node.origin();
Coord ijk(0, 0, 0), nijk;
int step = -1;
if (UpWind) {
step = 1;
ijk[2] = int(BoolLeafNodeType::DIM) - 1;
}
for (ijk[0] = 0; ijk[0] < int(BoolLeafNodeType::DIM); ++ijk[0]) {
const Index xPos = ijk[0] << (2 * int(BoolLeafNodeType::LOG2DIM));
for (ijk[1] = 0; ijk[1] < int(BoolLeafNodeType::DIM); ++ijk[1]) {
const Index pos = ijk[2] + xPos + (ijk[1] << int(BoolLeafNodeType::LOG2DIM));
if (valueMask[pos] == 0 && node.isValueOn(pos)) {
nijk = origin + ijk.offsetBy(0, 0, step);
if (!maskAcc.isValueOn(nijk) && distAcc.getValue(nijk) > mIsovalue) {
valueMask[pos] = 1;
}
}
}
}
}
//////////
TreeType const * const mTree;
BoolTreeType const * const mFillMask;
BoolLeafNodeType const * const * const mFillNodes;
BoolLeafNodeType ** const mNewNodes;
ValueType const mIsovalue;
}; // FillMaskBoundary
/// @brief Constructs a memory light char tree that represents the exterior region with @c +1
/// and the interior regions with @c -1.
template <class TreeType>
inline typename TreeType::template ValueConverter<char>::Type::Ptr
computeEnclosedRegionMask(const TreeType& tree, typename TreeType::ValueType isovalue,
const typename TreeType::template ValueConverter<bool>::Type* fillMask)
{
using LeafNodeType = typename TreeType::LeafNodeType;
using RootNodeType = typename TreeType::RootNodeType;
using NodeChainType = typename RootNodeType::NodeChainType;
using InternalNodeType = typename boost::mpl::at<NodeChainType, boost::mpl::int_<1>>::type;
using CharTreeType = typename TreeType::template ValueConverter<char>::Type;
using CharLeafNodeType = typename CharTreeType::LeafNodeType;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
const TreeType* treePt = &tree;
size_t numLeafNodes = 0, numInternalNodes = 0;
std::vector<const LeafNodeType*> nodes;
std::vector<size_t> leafnodeCount;
{
// compute the prefix sum of the leafnode count in each internal node.
std::vector<const InternalNodeType*> internalNodes;
treePt->getNodes(internalNodes);
numInternalNodes = internalNodes.size();
leafnodeCount.push_back(0);
for (size_t n = 0; n < numInternalNodes; ++n) {
leafnodeCount.push_back(leafnodeCount.back() + internalNodes[n]->leafCount());
}
numLeafNodes = leafnodeCount.back();
// extract all leafnodes
nodes.reserve(numLeafNodes);
for (size_t n = 0; n < numInternalNodes; ++n) {
internalNodes[n]->getNodes(nodes);
}
}
// create mask leafnodes
boost::scoped_array<CharLeafNodeType*> maskNodes(new CharLeafNodeType*[numLeafNodes]);
tbb::parallel_for(tbb::blocked_range<size_t>(0, numLeafNodes),
LabelBoundaryVoxels<LeafNodeType>(isovalue, &nodes[0], maskNodes.get()));
// create mask grid
typename CharTreeType::Ptr maskTree(new CharTreeType(1));
PopulateTree<CharTreeType> populate(*maskTree, maskNodes.get(), &leafnodeCount[0], 1);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, numInternalNodes), populate);
// optionally evaluate the fill mask
std::vector<CharLeafNodeType*> extraMaskNodes;
if (fillMask) {
std::vector<const BoolLeafNodeType*> fillMaskNodes;
fillMask->getNodes(fillMaskNodes);
boost::scoped_array<BoolLeafNodeType*> boundaryMaskNodes(
new BoolLeafNodeType*[fillMaskNodes.size()]);
tbb::parallel_for(tbb::blocked_range<size_t>(0, fillMaskNodes.size()),
FillMaskBoundary<TreeType>(tree, isovalue, *fillMask, &fillMaskNodes[0],
boundaryMaskNodes.get()));
tree::ValueAccessor<CharTreeType> maskAcc(*maskTree);
for (size_t n = 0, N = fillMaskNodes.size(); n < N; ++n) {
if (boundaryMaskNodes[n] == nullptr) continue;
const BoolLeafNodeType& boundaryNode = *boundaryMaskNodes[n];
const Coord& origin = boundaryNode.origin();
CharLeafNodeType* maskNodePt = maskAcc.probeLeaf(origin);
if (!maskNodePt) {
maskNodePt = maskAcc.touchLeaf(origin);
extraMaskNodes.push_back(maskNodePt);
}
char* data = maskNodePt->buffer().data();
typename BoolLeafNodeType::ValueOnCIter it = boundaryNode.cbeginValueOn();
for (; it; ++it) {
if (data[it.pos()] != 0) data[it.pos()] = -1;
}
delete boundaryMaskNodes[n];
}
}
// eliminate enclosed regions
tools::traceExteriorBoundaries(*maskTree);
// flip voxel sign to negative inside and positive outside.
tbb::parallel_for(tbb::blocked_range<size_t>(0, numLeafNodes),
FlipRegionSign<CharLeafNodeType>(maskNodes.get()));
if (!extraMaskNodes.empty()) {
tbb::parallel_for(tbb::blocked_range<size_t>(0, extraMaskNodes.size()),
FlipRegionSign<CharLeafNodeType>(&extraMaskNodes[0]));
}
// propagate sign information into tile region
tools::signedFloodFill(*maskTree);
return maskTree;
} // computeEnclosedRegionMask()
template <class TreeType>
inline typename TreeType::template ValueConverter<bool>::Type::Ptr
computeInteriorMask(const TreeType& tree, typename TreeType::ValueType iso)
{
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
using RootNodeType = typename TreeType::RootNodeType;
using NodeChainType = typename RootNodeType::NodeChainType;
using InternalNodeType = typename boost::mpl::at<NodeChainType, boost::mpl::int_<1> >::type;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
using BoolRootNodeType = typename BoolTreeType::RootNodeType;
using BoolNodeChainType = typename BoolRootNodeType::NodeChainType;
using BoolInternalNodeType =
typename boost::mpl::at<BoolNodeChainType, boost::mpl::int_<1>>::type;
/////
// Clamp the isovalue to the level set's background value minus epsilon.
// (In a valid narrow-band level set, all voxels, including background voxels,
// have values less than or equal to the background value, so an isovalue
// greater than or equal to the background value would produce a mask with
// effectively infinite extent.)
iso = std::min(iso,
static_cast<ValueType>(tree.background() - math::Tolerance<ValueType>::value()));
size_t numLeafNodes = 0, numInternalNodes = 0;
std::vector<const LeafNodeType*> nodes;
std::vector<size_t> leafnodeCount;
{
// compute the prefix sum of the leafnode count in each internal node.
std::vector<const InternalNodeType*> internalNodes;
tree.getNodes(internalNodes);
numInternalNodes = internalNodes.size();
leafnodeCount.push_back(0);
for (size_t n = 0; n < numInternalNodes; ++n) {
leafnodeCount.push_back(leafnodeCount.back() + internalNodes[n]->leafCount());
}
numLeafNodes = leafnodeCount.back();
// extract all leafnodes
nodes.reserve(numLeafNodes);
for (size_t n = 0; n < numInternalNodes; ++n) {
internalNodes[n]->getNodes(nodes);
}
}
// create mask leafnodes
boost::scoped_array<BoolLeafNodeType*> maskNodes(new BoolLeafNodeType*[numLeafNodes]);
tbb::parallel_for(tbb::blocked_range<size_t>(0, numLeafNodes),
MaskInteriorVoxels<LeafNodeType>(iso, &nodes[0], maskNodes.get()));
// create mask grid
typename BoolTreeType::Ptr maskTree(new BoolTreeType(false));
PopulateTree<BoolTreeType> populate(*maskTree, maskNodes.get(), &leafnodeCount[0], false);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, numInternalNodes), populate);
// evaluate tile values
std::vector<BoolInternalNodeType*> internalMaskNodes;
maskTree->getNodes(internalMaskNodes);
tbb::parallel_for(tbb::blocked_range<size_t>(0, internalMaskNodes.size()),
MaskInteriorTiles<TreeType, BoolInternalNodeType>(iso, tree, &internalMaskNodes[0]));
tree::ValueAccessor<const TreeType> acc(tree);
typename BoolTreeType::ValueAllIter it(*maskTree);
it.setMaxDepth(BoolTreeType::ValueAllIter::LEAF_DEPTH - 2);
for ( ; it; ++it) {
if (acc.getValue(it.getCoord()) < iso) {
it.setValue(true);
it.setActiveState(true);
}
}
return maskTree;
} // computeInteriorMask()
template<typename InputTreeType>
struct MaskIsovalueCrossingVoxels
{
using InputValueType = typename InputTreeType::ValueType;
using InputLeafNodeType = typename InputTreeType::LeafNodeType;
using BoolTreeType = typename InputTreeType::template ValueConverter<bool>::Type;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
MaskIsovalueCrossingVoxels(
const InputTreeType& inputTree,
const std::vector<const InputLeafNodeType*>& inputLeafNodes,
BoolTreeType& maskTree,
InputValueType iso)
: mInputAccessor(inputTree)
, mInputNodes(!inputLeafNodes.empty() ? &inputLeafNodes.front() : nullptr)
, mMaskTree(false)
, mMaskAccessor(maskTree)
, mIsovalue(iso)
{
}
MaskIsovalueCrossingVoxels(MaskIsovalueCrossingVoxels& rhs, tbb::split)
: mInputAccessor(rhs.mInputAccessor.tree())
, mInputNodes(rhs.mInputNodes)
, mMaskTree(false)
, mMaskAccessor(mMaskTree)
, mIsovalue(rhs.mIsovalue)
{
}
void operator()(const tbb::blocked_range<size_t>& range) {
const InputValueType iso = mIsovalue;
Coord ijk(0, 0, 0);
BoolLeafNodeType* maskNodePt = nullptr;
for (size_t n = range.begin(); mInputNodes && (n != range.end()); ++n) {
const InputLeafNodeType& node = *mInputNodes[n];
if (!maskNodePt) maskNodePt = new BoolLeafNodeType(node.origin(), false);
else maskNodePt->setOrigin(node.origin());
bool collectedData = false;
for (typename InputLeafNodeType::ValueOnCIter it = node.cbeginValueOn(); it; ++it) {
bool isUnder = *it < iso;
ijk = it.getCoord();
++ijk[2];
bool signChange = isUnder != (mInputAccessor.getValue(ijk) < iso); // +z edge
--ijk[2];
if (!signChange) {
--ijk[2];
signChange = isUnder != (mInputAccessor.getValue(ijk) < iso); // -z edge
++ijk[2];
}
if (!signChange) {
++ijk[1];
signChange = isUnder != (mInputAccessor.getValue(ijk) < iso); // +y edge
--ijk[1];
}
if (!signChange) {
--ijk[1];
signChange = isUnder != (mInputAccessor.getValue(ijk) < iso); // -y edge
++ijk[1];
}
if (!signChange) {
++ijk[0];
signChange = isUnder != (mInputAccessor.getValue(ijk) < iso); // +x edge
--ijk[0];
}
if (!signChange) {
--ijk[0];
signChange = isUnder != (mInputAccessor.getValue(ijk) < iso); // -x edge
++ijk[0];
}
if (signChange) {
collectedData = true;
maskNodePt->setValueOn(it.pos(), true);
}
}
if (collectedData) {
mMaskAccessor.addLeaf(maskNodePt);
maskNodePt = nullptr;
}
}
if (maskNodePt) delete maskNodePt;
}
void join(MaskIsovalueCrossingVoxels& rhs) {
mMaskAccessor.tree().merge(rhs.mMaskAccessor.tree());
}
private:
tree::ValueAccessor<const InputTreeType> mInputAccessor;
InputLeafNodeType const * const * const mInputNodes;
BoolTreeType mMaskTree;
tree::ValueAccessor<BoolTreeType> mMaskAccessor;
InputValueType mIsovalue;
}; // MaskIsovalueCrossingVoxels
////////////////////////////////////////
template<typename NodeType>
struct NodeMaskSegment
{
using Ptr = SharedPtr<NodeMaskSegment>;
using NodeMaskType = typename NodeType::NodeMaskType;
NodeMaskSegment() : connections(), mask(false), origin(0,0,0), visited(false) {}
std::vector<NodeMaskSegment*> connections;
NodeMaskType mask;
Coord origin;
bool visited;
}; // struct NodeMaskSegment
template<typename NodeType>
inline void
nodeMaskSegmentation(const NodeType& node,
std::vector<typename NodeMaskSegment<NodeType>::Ptr>& segments)
{
using NodeMaskType = typename NodeType::NodeMaskType;
using NodeMaskSegmentType = NodeMaskSegment<NodeType>;
using NodeMaskSegmentTypePtr = typename NodeMaskSegmentType::Ptr;
NodeMaskType nodeMask(node.getValueMask());
std::deque<Index> indexList;
while (!nodeMask.isOff()) {
NodeMaskSegmentTypePtr segment(new NodeMaskSegmentType());
segment->origin = node.origin();
NodeMaskType& mask = segment->mask;
indexList.push_back(nodeMask.findFirstOn());
nodeMask.setOff(indexList.back()); // mark as visited
Coord ijk(0, 0, 0);
while (!indexList.empty()) {
const Index pos = indexList.back();
indexList.pop_back();
if (mask.isOn(pos)) continue;
mask.setOn(pos);
ijk = NodeType::offsetToLocalCoord(pos);
Index npos = pos - 1;
if (ijk[2] != 0 && nodeMask.isOn(npos)) {
nodeMask.setOff(npos);
indexList.push_back(npos);
}
npos = pos + 1;
if (ijk[2] != (NodeType::DIM - 1) && nodeMask.isOn(npos)) {
nodeMask.setOff(npos);
indexList.push_back(npos);
}
npos = pos - NodeType::DIM;
if (ijk[1] != 0 && nodeMask.isOn(npos)) {
nodeMask.setOff(npos);
indexList.push_back(npos);
}
npos = pos + NodeType::DIM;
if (ijk[1] != (NodeType::DIM - 1) && nodeMask.isOn(npos)) {
nodeMask.setOff(npos);
indexList.push_back(npos);
}
npos = pos - NodeType::DIM * NodeType::DIM;
if (ijk[0] != 0 && nodeMask.isOn(npos)) {
nodeMask.setOff(npos);
indexList.push_back(npos);
}
npos = pos + NodeType::DIM * NodeType::DIM;
if (ijk[0] != (NodeType::DIM - 1) && nodeMask.isOn(npos)) {
nodeMask.setOff(npos);
indexList.push_back(npos);
}
}
segments.push_back(segment);
}
}
template<typename NodeType>
struct SegmentNodeMask
{
using NodeMaskSegmentType = NodeMaskSegment<NodeType>;
using NodeMaskSegmentTypePtr = typename NodeMaskSegmentType::Ptr;
using NodeMaskSegmentVector = typename std::vector<NodeMaskSegmentTypePtr>;
SegmentNodeMask(std::vector<NodeType*>& nodes, NodeMaskSegmentVector* nodeMaskArray)
: mNodes(!nodes.empty() ? &nodes.front() : nullptr)
, mNodeMaskArray(nodeMaskArray)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
NodeType& node = *mNodes[n];
nodeMaskSegmentation(node, mNodeMaskArray[n]);
// hack origin data to store array offset
Coord& origin = const_cast<Coord&>(node.origin());
origin[0] = static_cast<int>(n);
}
}
NodeType * const * const mNodes;
NodeMaskSegmentVector * const mNodeMaskArray;
}; // struct SegmentNodeMask
template<typename TreeType, typename NodeType>
struct ConnectNodeMaskSegments
{
using NodeMaskType = typename NodeType::NodeMaskType;
using NodeMaskSegmentType = NodeMaskSegment<NodeType>;
using NodeMaskSegmentTypePtr = typename NodeMaskSegmentType::Ptr;
using NodeMaskSegmentVector = typename std::vector<NodeMaskSegmentTypePtr>;
ConnectNodeMaskSegments(const TreeType& tree, NodeMaskSegmentVector* nodeMaskArray)
: mTree(&tree)
, mNodeMaskArray(nodeMaskArray)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
tree::ValueAccessor<const TreeType> acc(*mTree);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
NodeMaskSegmentVector& segments = mNodeMaskArray[n];
if (segments.empty()) continue;
std::vector<std::set<NodeMaskSegmentType*> > connections(segments.size());
Coord ijk = segments[0]->origin;
const NodeType* node = acc.template probeConstNode<NodeType>(ijk);
if (!node) continue;
// get neighbour nodes
ijk[2] += NodeType::DIM;
const NodeType* nodeZUp = acc.template probeConstNode<NodeType>(ijk);
ijk[2] -= (NodeType::DIM + NodeType::DIM);
const NodeType* nodeZDown = acc.template probeConstNode<NodeType>(ijk);
ijk[2] += NodeType::DIM;
ijk[1] += NodeType::DIM;
const NodeType* nodeYUp = acc.template probeConstNode<NodeType>(ijk);
ijk[1] -= (NodeType::DIM + NodeType::DIM);
const NodeType* nodeYDown = acc.template probeConstNode<NodeType>(ijk);
ijk[1] += NodeType::DIM;
ijk[0] += NodeType::DIM;
const NodeType* nodeXUp = acc.template probeConstNode<NodeType>(ijk);
ijk[0] -= (NodeType::DIM + NodeType::DIM);
const NodeType* nodeXDown = acc.template probeConstNode<NodeType>(ijk);
ijk[0] += NodeType::DIM;
const Index startPos = node->getValueMask().findFirstOn();
for (Index pos = startPos; pos < NodeMaskType::SIZE; ++pos) {
if (!node->isValueOn(pos)) continue;
ijk = NodeType::offsetToLocalCoord(pos);
#ifdef _MSC_FULL_VER
#if _MSC_FULL_VER >= 190000000 && _MSC_FULL_VER < 190024210
// Visual Studio 2015 had a codegen bug that wasn't fixed until Update 3
volatile Index npos = 0;
#else
Index npos = 0;
#endif
#else
Index npos = 0;
#endif
if (ijk[2] == 0) {
npos = pos + (NodeType::DIM - 1);
if (nodeZDown && nodeZDown->isValueOn(npos)) {
NodeMaskSegmentType* nsegment =
findNodeMaskSegment(mNodeMaskArray[getNodeOffset(*nodeZDown)], npos);
const Index idx = findNodeMaskSegmentIndex(segments, pos);
connections[idx].insert(nsegment);
}
} else if (ijk[2] == (NodeType::DIM - 1)) {
npos = pos - (NodeType::DIM - 1);
if (nodeZUp && nodeZUp->isValueOn(npos)) {
NodeMaskSegmentType* nsegment =
findNodeMaskSegment(mNodeMaskArray[getNodeOffset(*nodeZUp)], npos);
const Index idx = findNodeMaskSegmentIndex(segments, pos);
connections[idx].insert(nsegment);
}
}
if (ijk[1] == 0) {
npos = pos + (NodeType::DIM - 1) * NodeType::DIM;
if (nodeYDown && nodeYDown->isValueOn(npos)) {
NodeMaskSegmentType* nsegment =
findNodeMaskSegment(mNodeMaskArray[getNodeOffset(*nodeYDown)], npos);
const Index idx = findNodeMaskSegmentIndex(segments, pos);
connections[idx].insert(nsegment);
}
} else if (ijk[1] == (NodeType::DIM - 1)) {
npos = pos - (NodeType::DIM - 1) * NodeType::DIM;
if (nodeYUp && nodeYUp->isValueOn(npos)) {
NodeMaskSegmentType* nsegment =
findNodeMaskSegment(mNodeMaskArray[getNodeOffset(*nodeYUp)], npos);
const Index idx = findNodeMaskSegmentIndex(segments, pos);
connections[idx].insert(nsegment);
}
}
if (ijk[0] == 0) {
npos = pos + (NodeType::DIM - 1) * NodeType::DIM * NodeType::DIM;
if (nodeXDown && nodeXDown->isValueOn(npos)) {
NodeMaskSegmentType* nsegment =
findNodeMaskSegment(mNodeMaskArray[getNodeOffset(*nodeXDown)], npos);
const Index idx = findNodeMaskSegmentIndex(segments, pos);
connections[idx].insert(nsegment);
}
} else if (ijk[0] == (NodeType::DIM - 1)) {
npos = pos - (NodeType::DIM - 1) * NodeType::DIM * NodeType::DIM;
if (nodeXUp && nodeXUp->isValueOn(npos)) {
NodeMaskSegmentType* nsegment =
findNodeMaskSegment(mNodeMaskArray[getNodeOffset(*nodeXUp)], npos);
const Index idx = findNodeMaskSegmentIndex(segments, pos);
connections[idx].insert(nsegment);
}
}
}
for (size_t i = 0, I = connections.size(); i < I; ++i) {
typename std::set<NodeMaskSegmentType*>::iterator
it = connections[i].begin(), end = connections[i].end();
std::vector<NodeMaskSegmentType*>& segmentConnections = segments[i]->connections;
segmentConnections.reserve(connections.size());
for (; it != end; ++it) {
segmentConnections.push_back(*it);
}
}
} // end range loop
}
private:
static inline size_t getNodeOffset(const NodeType& node) {
return static_cast<size_t>(node.origin()[0]);
}
static inline NodeMaskSegmentType*
findNodeMaskSegment(NodeMaskSegmentVector& segments, Index pos)
{
NodeMaskSegmentType* segment = nullptr;
for (size_t n = 0, N = segments.size(); n < N; ++n) {
if (segments[n]->mask.isOn(pos)) {
segment = segments[n].get();
break;
}
}
return segment;
}
static inline Index
findNodeMaskSegmentIndex(NodeMaskSegmentVector& segments, Index pos)
{
for (Index n = 0, N = Index(segments.size()); n < N; ++n) {
if (segments[n]->mask.isOn(pos)) return n;
}
return Index(-1);
}
TreeType const * const mTree;
NodeMaskSegmentVector * const mNodeMaskArray;
}; // struct ConnectNodeMaskSegments
template<typename TreeType>
struct MaskSegmentGroup
{
using LeafNodeType = typename TreeType::LeafNodeType;
using TreeTypePtr = typename TreeType::Ptr;
using NodeMaskSegmentType = NodeMaskSegment<LeafNodeType>;
MaskSegmentGroup(const std::vector<NodeMaskSegmentType*>& segments)
: mSegments(!segments.empty() ? &segments.front() : nullptr)
, mTree(new TreeType(false))
{
}
MaskSegmentGroup(const MaskSegmentGroup& rhs, tbb::split)
: mSegments(rhs.mSegments)
, mTree(new TreeType(false))
{
}
TreeTypePtr& mask() { return mTree; }
void join(MaskSegmentGroup& rhs) { mTree->merge(*rhs.mTree); }
void operator()(const tbb::blocked_range<size_t>& range) {
tree::ValueAccessor<TreeType> acc(*mTree);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
NodeMaskSegmentType& segment = *mSegments[n];
LeafNodeType* node = acc.touchLeaf(segment.origin);
node->getValueMask() |= segment.mask;
}
}
private:
NodeMaskSegmentType * const * const mSegments;
TreeTypePtr mTree;
}; // struct MaskSegmentGroup
////////////////////////////////////////
template<typename TreeType>
struct ExpandLeafNodeRegion
{
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
using NodeMaskType = typename LeafNodeType::NodeMaskType;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
/////
ExpandLeafNodeRegion(const TreeType& distTree, BoolTreeType& maskTree,
std::vector<BoolLeafNodeType*>& maskNodes)
: mDistTree(&distTree)
, mMaskTree(&maskTree)
, mMaskNodes(!maskNodes.empty() ? &maskNodes.front() : nullptr)
, mNewMaskTree(false)
{
}
ExpandLeafNodeRegion(const ExpandLeafNodeRegion& rhs, tbb::split)
: mDistTree(rhs.mDistTree)
, mMaskTree(rhs.mMaskTree)
, mMaskNodes(rhs.mMaskNodes)
, mNewMaskTree(false)
{
}
BoolTreeType& newMaskTree() { return mNewMaskTree; }
void join(ExpandLeafNodeRegion& rhs) { mNewMaskTree.merge(rhs.mNewMaskTree); }
void operator()(const tbb::blocked_range<size_t>& range) {
using NodeType = LeafNodeType;
tree::ValueAccessor<const TreeType> distAcc(*mDistTree);
tree::ValueAccessor<const BoolTreeType> maskAcc(*mMaskTree);
tree::ValueAccessor<BoolTreeType> newMaskAcc(mNewMaskTree);
NodeMaskType maskZUp, maskZDown, maskYUp, maskYDown, maskXUp, maskXDown;
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
BoolLeafNodeType& maskNode = *mMaskNodes[n];
if (maskNode.isEmpty()) continue;
Coord ijk = maskNode.origin(), nijk;
const LeafNodeType* distNode = distAcc.probeConstLeaf(ijk);
if (!distNode) continue;
const ValueType *dataZUp = nullptr, *dataZDown = nullptr,
*dataYUp = nullptr, *dataYDown = nullptr,
*dataXUp = nullptr, *dataXDown = nullptr;
ijk[2] += NodeType::DIM;
getData(ijk, distAcc, maskAcc, maskZUp, dataZUp);
ijk[2] -= (NodeType::DIM + NodeType::DIM);
getData(ijk, distAcc, maskAcc, maskZDown, dataZDown);
ijk[2] += NodeType::DIM;
ijk[1] += NodeType::DIM;
getData(ijk, distAcc, maskAcc, maskYUp, dataYUp);
ijk[1] -= (NodeType::DIM + NodeType::DIM);
getData(ijk, distAcc, maskAcc, maskYDown, dataYDown);
ijk[1] += NodeType::DIM;
ijk[0] += NodeType::DIM;
getData(ijk, distAcc, maskAcc, maskXUp, dataXUp);
ijk[0] -= (NodeType::DIM + NodeType::DIM);
getData(ijk, distAcc, maskAcc, maskXDown, dataXDown);
ijk[0] += NodeType::DIM;
for (typename BoolLeafNodeType::ValueOnIter it = maskNode.beginValueOn(); it; ++it) {
const Index pos = it.pos();
const ValueType val = std::abs(distNode->getValue(pos));
ijk = BoolLeafNodeType::offsetToLocalCoord(pos);
nijk = ijk + maskNode.origin();
if (dataZUp && ijk[2] == (BoolLeafNodeType::DIM - 1)) {
const Index npos = pos - (NodeType::DIM - 1);
if (maskZUp.isOn(npos) && std::abs(dataZUp[npos]) > val) {
newMaskAcc.setValueOn(nijk.offsetBy(0, 0, 1));
}
} else if (dataZDown && ijk[2] == 0) {
const Index npos = pos + (NodeType::DIM - 1);
if (maskZDown.isOn(npos) && std::abs(dataZDown[npos]) > val) {
newMaskAcc.setValueOn(nijk.offsetBy(0, 0, -1));
}
}
if (dataYUp && ijk[1] == (BoolLeafNodeType::DIM - 1)) {
const Index npos = pos - (NodeType::DIM - 1) * NodeType::DIM;
if (maskYUp.isOn(npos) && std::abs(dataYUp[npos]) > val) {
newMaskAcc.setValueOn(nijk.offsetBy(0, 1, 0));
}
} else if (dataYDown && ijk[1] == 0) {
const Index npos = pos + (NodeType::DIM - 1) * NodeType::DIM;
if (maskYDown.isOn(npos) && std::abs(dataYDown[npos]) > val) {
newMaskAcc.setValueOn(nijk.offsetBy(0, -1, 0));
}
}
if (dataXUp && ijk[0] == (BoolLeafNodeType::DIM - 1)) {
const Index npos = pos - (NodeType::DIM - 1) * NodeType::DIM * NodeType::DIM;
if (maskXUp.isOn(npos) && std::abs(dataXUp[npos]) > val) {
newMaskAcc.setValueOn(nijk.offsetBy(1, 0, 0));
}
} else if (dataXDown && ijk[0] == 0) {
const Index npos = pos + (NodeType::DIM - 1) * NodeType::DIM * NodeType::DIM;
if (maskXDown.isOn(npos) && std::abs(dataXDown[npos]) > val) {
newMaskAcc.setValueOn(nijk.offsetBy(-1, 0, 0));
}
}
} // end value on loop
} // end range loop
}
private:
static inline void
getData(const Coord& ijk, tree::ValueAccessor<const TreeType>& distAcc,
tree::ValueAccessor<const BoolTreeType>& maskAcc, NodeMaskType& mask,
const ValueType*& data)
{
const LeafNodeType* node = distAcc.probeConstLeaf(ijk);
if (node) {
data = node->buffer().data();
mask = node->getValueMask();
const BoolLeafNodeType* maskNodePt = maskAcc.probeConstLeaf(ijk);
if (maskNodePt) mask -= maskNodePt->getValueMask();
}
}
TreeType const * const mDistTree;
BoolTreeType * const mMaskTree;
BoolLeafNodeType ** const mMaskNodes;
BoolTreeType mNewMaskTree;
}; // struct ExpandLeafNodeRegion
template<typename TreeType>
struct FillLeafNodeVoxels
{
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
using NodeMaskType = typename LeafNodeType::NodeMaskType;
using BoolLeafNodeType = tree::LeafNode<bool, LeafNodeType::LOG2DIM>;
FillLeafNodeVoxels(const TreeType& tree, std::vector<BoolLeafNodeType*>& maskNodes)
: mTree(&tree), mMaskNodes(!maskNodes.empty() ? &maskNodes.front() : nullptr)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
tree::ValueAccessor<const TreeType> distAcc(*mTree);
std::vector<Index> indexList;
indexList.reserve(NodeMaskType::SIZE);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
BoolLeafNodeType& maskNode = *mMaskNodes[n];
const LeafNodeType * distNode = distAcc.probeConstLeaf(maskNode.origin());
if (!distNode) continue;
NodeMaskType mask(distNode->getValueMask());
NodeMaskType& narrowbandMask = maskNode.getValueMask();
for (Index pos = narrowbandMask.findFirstOn(); pos < NodeMaskType::SIZE; ++pos) {
if (narrowbandMask.isOn(pos)) indexList.push_back(pos);
}
mask -= narrowbandMask; // bitwise difference
narrowbandMask.setOff();
const ValueType* data = distNode->buffer().data();
Coord ijk(0, 0, 0);
while (!indexList.empty()) {
const Index pos = indexList.back();
indexList.pop_back();
if (narrowbandMask.isOn(pos)) continue;
narrowbandMask.setOn(pos);
const ValueType dist = std::abs(data[pos]);
ijk = LeafNodeType::offsetToLocalCoord(pos);
Index npos = pos - 1;
if (ijk[2] != 0 && mask.isOn(npos) && std::abs(data[npos]) > dist) {
mask.setOff(npos);
indexList.push_back(npos);
}
npos = pos + 1;
if ((ijk[2] != (LeafNodeType::DIM - 1)) && mask.isOn(npos)
&& std::abs(data[npos]) > dist)
{
mask.setOff(npos);
indexList.push_back(npos);
}
npos = pos - LeafNodeType::DIM;
if (ijk[1] != 0 && mask.isOn(npos) && std::abs(data[npos]) > dist) {
mask.setOff(npos);
indexList.push_back(npos);
}
npos = pos + LeafNodeType::DIM;
if ((ijk[1] != (LeafNodeType::DIM - 1)) && mask.isOn(npos)
&& std::abs(data[npos]) > dist)
{
mask.setOff(npos);
indexList.push_back(npos);
}
npos = pos - LeafNodeType::DIM * LeafNodeType::DIM;
if (ijk[0] != 0 && mask.isOn(npos) && std::abs(data[npos]) > dist) {
mask.setOff(npos);
indexList.push_back(npos);
}
npos = pos + LeafNodeType::DIM * LeafNodeType::DIM;
if ((ijk[0] != (LeafNodeType::DIM - 1)) && mask.isOn(npos)
&& std::abs(data[npos]) > dist)
{
mask.setOff(npos);
indexList.push_back(npos);
}
} // end flood fill loop
} // end range loop
}
TreeType const * const mTree;
BoolLeafNodeType ** const mMaskNodes;
}; // FillLeafNodeVoxels
template<typename TreeType>
struct ExpandNarrowbandMask
{
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
using BoolTreeTypePtr = typename BoolTreeType::Ptr;
ExpandNarrowbandMask(const TreeType& tree, std::vector<BoolTreeTypePtr>& segments)
: mTree(&tree), mSegments(!segments.empty() ? &segments.front() : nullptr)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
const TreeType& distTree = *mTree;
std::vector<BoolLeafNodeType*> nodes;
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
BoolTreeType& narrowBandMask = *mSegments[n];
BoolTreeType candidateMask(narrowBandMask, false, TopologyCopy());
while (true) {
nodes.clear();
candidateMask.getNodes(nodes);
if (nodes.empty()) break;
const tbb::blocked_range<size_t> nodeRange(0, nodes.size());
tbb::parallel_for(nodeRange, FillLeafNodeVoxels<TreeType>(distTree, nodes));
narrowBandMask.topologyUnion(candidateMask);
ExpandLeafNodeRegion<TreeType> op(distTree, narrowBandMask, nodes);
tbb::parallel_reduce(nodeRange, op);
if (op.newMaskTree().empty()) break;
candidateMask.clear();
candidateMask.merge(op.newMaskTree());
} // end expand loop
} // end range loop
}
TreeType const * const mTree;
BoolTreeTypePtr * const mSegments;
}; // ExpandNarrowbandMask
template<typename TreeType>
struct FloodFillSign
{
using TreeTypePtr = typename TreeType::Ptr;
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
using RootNodeType = typename TreeType::RootNodeType;
using NodeChainType = typename RootNodeType::NodeChainType;
using InternalNodeType = typename boost::mpl::at<NodeChainType, boost::mpl::int_<1> >::type;
FloodFillSign(const TreeType& tree, std::vector<TreeTypePtr>& segments)
: mTree(&tree)
, mSegments(!segments.empty() ? &segments.front() : nullptr)
, mMinValue(ValueType(0.0))
{
ValueType minSDFValue = std::numeric_limits<ValueType>::max();
{
std::vector<const InternalNodeType*> nodes;
tree.getNodes(nodes);
if (!nodes.empty()) {
FindMinTileValue<InternalNodeType> minOp(&nodes[0]);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), minOp);
minSDFValue = std::min(minSDFValue, minOp.minValue);
}
}
if (minSDFValue > ValueType(0.0)) {
std::vector<const LeafNodeType*> nodes;
tree.getNodes(nodes);
if (!nodes.empty()) {
FindMinVoxelValue<LeafNodeType> minOp(&nodes[0]);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), minOp);
minSDFValue = std::min(minSDFValue, minOp.minValue);
}
}
mMinValue = minSDFValue;
}
void operator()(const tbb::blocked_range<size_t>& range) const {
const ValueType interiorValue = -std::abs(mMinValue);
const ValueType exteriorValue = std::abs(mTree->background());
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
tools::signedFloodFillWithValues(*mSegments[n], exteriorValue, interiorValue);
}
}
private:
TreeType const * const mTree;
TreeTypePtr * const mSegments;
ValueType mMinValue;
}; // FloodFillSign
template<typename TreeType>
struct MaskedCopy
{
using TreeTypePtr = typename TreeType::Ptr;
using ValueType = typename TreeType::ValueType;
using LeafNodeType = typename TreeType::LeafNodeType;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolTreeTypePtr = typename BoolTreeType::Ptr;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
MaskedCopy(const TreeType& tree, std::vector<TreeTypePtr>& segments,
std::vector<BoolTreeTypePtr>& masks)
: mTree(&tree)
, mSegments(!segments.empty() ? &segments.front() : nullptr)
, mMasks(!masks.empty() ? &masks.front() : nullptr)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
std::vector<const BoolLeafNodeType*> nodes;
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
const BoolTreeType& mask = *mMasks[n];
nodes.clear();
mask.getNodes(nodes);
Copy op(*mTree, nodes);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), op);
mSegments[n] = op.outputTree();
}
}
private:
struct Copy {
Copy(const TreeType& inputTree, std::vector<const BoolLeafNodeType*>& maskNodes)
: mInputTree(&inputTree)
, mMaskNodes(!maskNodes.empty() ? &maskNodes.front() : nullptr)
, mOutputTreePtr(new TreeType(inputTree.background()))
{
}
Copy(const Copy& rhs, tbb::split)
: mInputTree(rhs.mInputTree)
, mMaskNodes(rhs.mMaskNodes)
, mOutputTreePtr(new TreeType(mInputTree->background()))
{
}
TreeTypePtr& outputTree() { return mOutputTreePtr; }
void join(Copy& rhs) { mOutputTreePtr->merge(*rhs.mOutputTreePtr); }
void operator()(const tbb::blocked_range<size_t>& range) {
tree::ValueAccessor<const TreeType> inputAcc(*mInputTree);
tree::ValueAccessor<TreeType> outputAcc(*mOutputTreePtr);
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
const BoolLeafNodeType& maskNode = *mMaskNodes[n];
if (maskNode.isEmpty()) continue;
const Coord& ijk = maskNode.origin();
const LeafNodeType* inputNode = inputAcc.probeConstLeaf(ijk);
if (inputNode) {
LeafNodeType* outputNode = outputAcc.touchLeaf(ijk);
for (typename BoolLeafNodeType::ValueOnCIter it = maskNode.cbeginValueOn();
it; ++it)
{
const Index idx = it.pos();
outputNode->setValueOn(idx, inputNode->getValue(idx));
}
} else {
const int valueDepth = inputAcc.getValueDepth(ijk);
if (valueDepth >= 0) {
outputAcc.addTile(TreeType::RootNodeType::LEVEL - valueDepth,
ijk, inputAcc.getValue(ijk), true);
}
}
}
}
private:
TreeType const * const mInputTree;
BoolLeafNodeType const * const * const mMaskNodes;
TreeTypePtr mOutputTreePtr;
}; // struct Copy
TreeType const * const mTree;
TreeTypePtr * const mSegments;
BoolTreeTypePtr * const mMasks;
}; // MaskedCopy
////////////////////////////////////////
template<typename VolumePtrType>
struct ComputeActiveVoxelCount
{
ComputeActiveVoxelCount(std::vector<VolumePtrType>& segments, size_t *countArray)
: mSegments(!segments.empty() ? &segments.front() : nullptr)
, mCountArray(countArray)
{
}
void operator()(const tbb::blocked_range<size_t>& range) const {
for (size_t n = range.begin(), N = range.end(); n < N; ++n) {
mCountArray[n] = mSegments[n]->activeVoxelCount();
}
}
VolumePtrType * const mSegments;
size_t * const mCountArray;
};
struct GreaterCount
{
GreaterCount(const size_t *countArray) : mCountArray(countArray) {}
inline bool operator() (const size_t& lhs, const size_t& rhs) const
{
return (mCountArray[lhs] > mCountArray[rhs]);
}
size_t const * const mCountArray;
};
////////////////////////////////////////
template<typename TreeType>
struct GridOrTreeConstructor
{
using TreeTypePtr = typename TreeType::Ptr;
using BoolTreePtrType = typename TreeType::template ValueConverter<bool>::Type::Ptr;
static BoolTreePtrType constructMask(const TreeType&, BoolTreePtrType& maskTree)
{ return maskTree; }
static TreeTypePtr construct(const TreeType&, TreeTypePtr& tree) { return tree; }
};
template<typename TreeType>
struct GridOrTreeConstructor<Grid<TreeType> >
{
using GridType = Grid<TreeType>;
using GridTypePtr = typename Grid<TreeType>::Ptr;
using TreeTypePtr = typename TreeType::Ptr;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolTreePtrType = typename BoolTreeType::Ptr;
using BoolGridType = Grid<BoolTreeType>;
using BoolGridPtrType = typename BoolGridType::Ptr;
static BoolGridPtrType constructMask(const GridType& grid, BoolTreePtrType& maskTree) {
BoolGridPtrType maskGrid(BoolGridType::create(maskTree));
maskGrid->setTransform(grid.transform().copy());
return maskGrid;
}
static GridTypePtr construct(const GridType& grid, TreeTypePtr& maskTree) {
GridTypePtr maskGrid(GridType::create(maskTree));
maskGrid->setTransform(grid.transform().copy());
maskGrid->insertMeta(grid);
return maskGrid;
}
};
} // namespace level_set_util_internal
////////////////////////////////////////
template <class GridType>
inline void
sdfToFogVolume(GridType& grid, typename GridType::ValueType cutoffDistance)
{
using ValueType = typename GridType::ValueType;
using TreeType = typename GridType::TreeType;
using LeafNodeType = typename TreeType::LeafNodeType;
using RootNodeType = typename TreeType::RootNodeType;
using NodeChainType = typename RootNodeType::NodeChainType;
using InternalNodeType = typename boost::mpl::at<NodeChainType, boost::mpl::int_<1>>::type;
//////////
TreeType& tree = grid.tree();
size_t numLeafNodes = 0, numInternalNodes = 0;
std::vector<LeafNodeType*> nodes;
std::vector<size_t> leafnodeCount;
{
// Compute the prefix sum of the leafnode count in each internal node.
std::vector<InternalNodeType*> internalNodes;
tree.getNodes(internalNodes);
numInternalNodes = internalNodes.size();
leafnodeCount.push_back(0);
for (size_t n = 0; n < numInternalNodes; ++n) {
leafnodeCount.push_back(leafnodeCount.back() + internalNodes[n]->leafCount());
}
numLeafNodes = leafnodeCount.back();
// Steal all leafnodes (Removes them from the tree and transfers ownership.)
nodes.reserve(numLeafNodes);
for (size_t n = 0; n < numInternalNodes; ++n) {
internalNodes[n]->stealNodes(nodes, tree.background(), false);
}
// Clamp cutoffDistance to min sdf value
ValueType minSDFValue = std::numeric_limits<ValueType>::max();
{
level_set_util_internal::FindMinTileValue<InternalNodeType> minOp(&internalNodes[0]);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, internalNodes.size()), minOp);
minSDFValue = std::min(minSDFValue, minOp.minValue);
}
if (minSDFValue > ValueType(0.0)) {
level_set_util_internal::FindMinVoxelValue<LeafNodeType> minOp(&nodes[0]);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), minOp);
minSDFValue = std::min(minSDFValue, minOp.minValue);
}
cutoffDistance = -std::abs(cutoffDistance);
cutoffDistance = minSDFValue > cutoffDistance ? minSDFValue : cutoffDistance;
}
// Transform voxel values and delete leafnodes that are uniformly zero after the transformation.
// (Positive values are set to zero with inactive state and negative values are remapped
// from zero to one with active state.)
tbb::parallel_for(tbb::blocked_range<size_t>(0, nodes.size()),
level_set_util_internal::SDFVoxelsToFogVolume<LeafNodeType>(&nodes[0], cutoffDistance));
// Populate a new tree with the remaining leafnodes
typename TreeType::Ptr newTree(new TreeType(ValueType(0.0)));
level_set_util_internal::PopulateTree<TreeType> populate(
*newTree, &nodes[0], &leafnodeCount[0], 0);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, numInternalNodes), populate);
// Transform tile values (Negative valued tiles are set to 1.0 with active state.)
std::vector<InternalNodeType*> internalNodes;
newTree->getNodes(internalNodes);
tbb::parallel_for(tbb::blocked_range<size_t>(0, internalNodes.size()),
level_set_util_internal::SDFTilesToFogVolume<TreeType, InternalNodeType>(
tree, &internalNodes[0]));
{
tree::ValueAccessor<const TreeType> acc(tree);
typename TreeType::ValueAllIter it(*newTree);
it.setMaxDepth(TreeType::ValueAllIter::LEAF_DEPTH - 2);
for ( ; it; ++it) {
if (acc.getValue(it.getCoord()) < ValueType(0.0)) {
it.setValue(ValueType(1.0));
it.setActiveState(true);
}
}
}
// Insert missing root level tiles. (The new tree is constructed from the remaining leafnodes
// and will therefore not contain any root level tiles that may exist in the original tree.)
{
typename TreeType::ValueAllIter it(tree);
it.setMaxDepth(TreeType::ValueAllIter::ROOT_DEPTH);
for ( ; it; ++it) {
if (it.getValue() < ValueType(0.0)) {
newTree->addTile(TreeType::ValueAllIter::ROOT_LEVEL, it.getCoord(),
ValueType(1.0), true);
}
}
}
grid.setTree(newTree);
grid.setGridClass(GRID_FOG_VOLUME);
}
////////////////////////////////////////
template <class GridOrTreeType>
inline typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr
sdfInteriorMask(const GridOrTreeType& volume, typename GridOrTreeType::ValueType isovalue)
{
using TreeType = typename TreeAdapter<GridOrTreeType>::TreeType;
const TreeType& tree = TreeAdapter<GridOrTreeType>::tree(volume);
using BoolTreePtrType = typename TreeType::template ValueConverter<bool>::Type::Ptr;
BoolTreePtrType mask = level_set_util_internal::computeInteriorMask(tree, isovalue);
return level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::constructMask(
volume, mask);
}
template<typename GridOrTreeType>
inline typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr
extractEnclosedRegion(const GridOrTreeType& volume,
typename GridOrTreeType::ValueType isovalue,
const typename TreeAdapter<GridOrTreeType>::TreeType::template ValueConverter<bool>::Type*
fillMask)
{
using TreeType = typename TreeAdapter<GridOrTreeType>::TreeType;
const TreeType& tree = TreeAdapter<GridOrTreeType>::tree(volume);
using CharTreePtrType = typename TreeType::template ValueConverter<char>::Type::Ptr;
CharTreePtrType regionMask = level_set_util_internal::computeEnclosedRegionMask(
tree, isovalue, fillMask);
using BoolTreePtrType = typename TreeType::template ValueConverter<bool>::Type::Ptr;
BoolTreePtrType mask = level_set_util_internal::computeInteriorMask(*regionMask, 0);
return level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::constructMask(
volume, mask);
}
////////////////////////////////////////
template<typename GridOrTreeType>
inline typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr
extractIsosurfaceMask(const GridOrTreeType& volume, typename GridOrTreeType::ValueType isovalue)
{
using TreeType = typename TreeAdapter<GridOrTreeType>::TreeType;
const TreeType& tree = TreeAdapter<GridOrTreeType>::tree(volume);
std::vector<const typename TreeType::LeafNodeType*> nodes;
tree.getNodes(nodes);
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
typename BoolTreeType::Ptr mask(new BoolTreeType(false));
level_set_util_internal::MaskIsovalueCrossingVoxels<TreeType> op(tree, nodes, *mask, isovalue);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, nodes.size()), op);
return level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::constructMask(
volume, mask);
}
////////////////////////////////////////
template<typename GridOrTreeType>
inline void
extractActiveVoxelSegmentMasks(const GridOrTreeType& volume,
std::vector<typename GridOrTreeType::template ValueConverter<bool>::Type::Ptr>& masks)
{
using TreeType = typename TreeAdapter<GridOrTreeType>::TreeType;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolTreePtrType = typename BoolTreeType::Ptr;
using BoolLeafNodeType = typename BoolTreeType::LeafNodeType;
using NodeMaskSegmentType = level_set_util_internal::NodeMaskSegment<BoolLeafNodeType>;
using NodeMaskSegmentPtrType = typename NodeMaskSegmentType::Ptr;
using NodeMaskSegmentPtrVector = typename std::vector<NodeMaskSegmentPtrType>;
using NodeMaskSegmentRawPtrVector = typename std::vector<NodeMaskSegmentType*>;
/////
const TreeType& tree = TreeAdapter<GridOrTreeType>::tree(volume);
BoolTreeType topologyMask(tree, false, TopologyCopy());
if (topologyMask.hasActiveTiles()) {
topologyMask.voxelizeActiveTiles();
}
std::vector<BoolLeafNodeType*> leafnodes;
topologyMask.getNodes(leafnodes);
if (leafnodes.empty()) return;
// 1. Split node masks into disjoint segments
// Note: The LeafNode origin coord is modified to record the 'leafnodes' array offset.
boost::scoped_array<NodeMaskSegmentPtrVector> nodeSegmentArray(
new NodeMaskSegmentPtrVector[leafnodes.size()]);
tbb::parallel_for(tbb::blocked_range<size_t>(0, leafnodes.size()),
level_set_util_internal::SegmentNodeMask<BoolLeafNodeType>(
leafnodes, nodeSegmentArray.get()));
// 2. Compute segment connectivity
tbb::parallel_for(tbb::blocked_range<size_t>(0, leafnodes.size()),
level_set_util_internal::ConnectNodeMaskSegments<BoolTreeType, BoolLeafNodeType>(
topologyMask, nodeSegmentArray.get()));
topologyMask.clear();
size_t nodeSegmentCount = 0;
for (size_t n = 0, N = leafnodes.size(); n < N; ++n) {
nodeSegmentCount += nodeSegmentArray[n].size();
}
// 3. Group connected segments
std::deque<NodeMaskSegmentRawPtrVector> nodeSegmentGroups;
NodeMaskSegmentType* nextSegment = nodeSegmentArray[0][0].get();
while (nextSegment) {
nodeSegmentGroups.push_back(NodeMaskSegmentRawPtrVector());
std::vector<NodeMaskSegmentType*>& segmentGroup = nodeSegmentGroups.back();
segmentGroup.reserve(nodeSegmentCount);
std::deque<NodeMaskSegmentType*> segmentQueue;
segmentQueue.push_back(nextSegment);
nextSegment = nullptr;
while (!segmentQueue.empty()) {
NodeMaskSegmentType* segment = segmentQueue.back();
segmentQueue.pop_back();
if (segment->visited) continue;
segment->visited = true;
segmentGroup.push_back(segment);
// queue connected segments
std::vector<NodeMaskSegmentType*>& connections = segment->connections;
for (size_t n = 0, N = connections.size(); n < N; ++n) {
if (!connections[n]->visited) segmentQueue.push_back(connections[n]);
}
}
// find first unvisited segment
for (size_t n = 0, N = leafnodes.size(); n < N; ++n) {
NodeMaskSegmentPtrVector& nodeSegments = nodeSegmentArray[n];
for (size_t i = 0, I = nodeSegments.size(); i < I; ++i) {
if (!nodeSegments[i]->visited) nextSegment = nodeSegments[i].get();
}
}
}
// 4. Mask segment groups
if (nodeSegmentGroups.size() == 1) {
BoolTreePtrType mask(new BoolTreeType(tree, false, TopologyCopy()));
if (mask->hasActiveTiles()) {
mask->voxelizeActiveTiles();
}
masks.push_back(
level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::constructMask(
volume, mask));
} else if (nodeSegmentGroups.size() > 1) {
for (size_t n = 0, N = nodeSegmentGroups.size(); n < N; ++n) {
NodeMaskSegmentRawPtrVector& segmentGroup = nodeSegmentGroups[n];
level_set_util_internal::MaskSegmentGroup<BoolTreeType> op(segmentGroup);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, segmentGroup.size()), op);
masks.push_back(
level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::constructMask(
volume, op.mask()));
}
}
// 5. Sort segments in descending order based on the active voxel count.
if (masks.size() > 1) {
const size_t segmentCount = masks.size();
boost::scoped_array<size_t> segmentOrderArray(new size_t[segmentCount]);
boost::scoped_array<size_t> voxelCountArray(new size_t[segmentCount]);
for (size_t n = 0; n < segmentCount; ++n) {
segmentOrderArray[n] = n;
}
tbb::parallel_for(tbb::blocked_range<size_t>(0, segmentCount),
level_set_util_internal::ComputeActiveVoxelCount<BoolTreePtrType>(
masks, voxelCountArray.get()));
size_t *begin = segmentOrderArray.get();
tbb::parallel_sort(begin, begin + masks.size(), level_set_util_internal::GreaterCount(
voxelCountArray.get()));
std::vector<BoolTreePtrType> orderedMasks;
orderedMasks.reserve(masks.size());
for (size_t n = 0; n < segmentCount; ++n) {
orderedMasks.push_back(masks[segmentOrderArray[n]]);
}
masks.swap(orderedMasks);
}
} // extractActiveVoxelSegmentMasks()
template<typename GridOrTreeType>
inline void
segmentActiveVoxels(const GridOrTreeType& volume,
std::vector<typename GridOrTreeType::Ptr>& segments)
{
using TreeType = typename TreeAdapter<GridOrTreeType>::TreeType;
using TreePtrType = typename TreeType::Ptr;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolTreePtrType = typename BoolTreeType::Ptr;
const TreeType& inputTree = TreeAdapter<GridOrTreeType>::tree(volume);
// 1. Segment active topology mask
std::vector<BoolTreePtrType> maskSegmentArray;
extractActiveVoxelSegmentMasks(inputTree, maskSegmentArray);
const size_t numSegments = maskSegmentArray.size();
if (numSegments < 2) {
// single segment early-out
TreePtrType segment(new TreeType(inputTree));
segments.push_back(
level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::construct(
volume, segment));
return;
}
const tbb::blocked_range<size_t> segmentRange(0, numSegments);
// 2. Export segments
std::vector<TreePtrType> outputSegmentArray(numSegments);
tbb::parallel_for(segmentRange,
level_set_util_internal::MaskedCopy<TreeType>(inputTree, outputSegmentArray,
maskSegmentArray));
for (size_t n = 0, N = numSegments; n < N; ++n) {
segments.push_back(
level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::construct(
volume, outputSegmentArray[n]));
}
}
template<typename GridOrTreeType>
inline void
segmentSDF(const GridOrTreeType& volume, std::vector<typename GridOrTreeType::Ptr>& segments)
{
using TreeType = typename TreeAdapter<GridOrTreeType>::TreeType;
using TreePtrType = typename TreeType::Ptr;
using BoolTreeType = typename TreeType::template ValueConverter<bool>::Type;
using BoolTreePtrType = typename BoolTreeType::Ptr;
const TreeType& inputTree = TreeAdapter<GridOrTreeType>::tree(volume);
// 1. Mask zero crossing voxels
BoolTreePtrType mask = extractIsosurfaceMask(inputTree, lsutilGridZero<GridOrTreeType>());
// 2. Segment the zero crossing mask
std::vector<BoolTreePtrType> maskSegmentArray;
extractActiveVoxelSegmentMasks(*mask, maskSegmentArray);
const size_t numSegments = maskSegmentArray.size();
if (numSegments < 2) {
// single segment early-out
TreePtrType segment(new TreeType(inputTree));
segments.push_back(
level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::construct(
volume, segment));
return;
}
const tbb::blocked_range<size_t> segmentRange(0, numSegments);
// 3. Expand zero crossing mask to capture sdf narrow band
tbb::parallel_for(segmentRange,
level_set_util_internal::ExpandNarrowbandMask<TreeType>(inputTree, maskSegmentArray));
// 4. Export sdf segments
std::vector<TreePtrType> outputSegmentArray(numSegments);
tbb::parallel_for(segmentRange, level_set_util_internal::MaskedCopy<TreeType>(
inputTree, outputSegmentArray, maskSegmentArray));
tbb::parallel_for(segmentRange,
level_set_util_internal::FloodFillSign<TreeType>(inputTree, outputSegmentArray));
for (size_t n = 0, N = numSegments; n < N; ++n) {
segments.push_back(
level_set_util_internal::GridOrTreeConstructor<GridOrTreeType>::construct(
volume, outputSegmentArray[n]));
}
}
} // namespace tools
} // namespace OPENVDB_VERSION_NAME
} // namespace openvdb
#endif // OPENVDB_TOOLS_LEVEL_SET_UTIL_HAS_BEEN_INCLUDED
// Copyright (c) 2012-2017 DreamWorks Animation LLC
// All rights reserved. This software is distributed under the
// Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
|