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
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: CloudCompare project #
//# #
//##########################################################################
//Qt
#include <QColorDialog>
#include <QElapsedTimer>
#include <QInputDialog>
#include <QMessageBox>
#include <QPushButton>
//CCLib
#include <NormalDistribution.h>
#include <ScalarFieldTools.h>
#include <StatisticalTestingTools.h>
#include <WeibullDistribution.h>
//qCC_db
#include "ccColorScalesManager.h"
#include "ccFacet.h"
#include "ccGenericPrimitive.h"
#include "ccOctreeProxy.h"
#include "ccPointCloud.h"
#include "ccPointCloudInterpolator.h"
#include "ccPolyline.h"
#include "ccSensor.h"
//qCC_gl
#include "ccGuiParameters.h"
//common
#include <ccPickOneElementDlg.h>
//Local
#include "ccAskTwoDoubleValuesDlg.h"
#include "ccColorGradientDlg.h"
#include "ccColorLevelsDlg.h"
#include "ccComputeOctreeDlg.h"
#include "ccExportCoordToSFDlg.h"
#include "ccInterpolationDlg.h"
#include "ccItemSelectionDlg.h"
#include "ccNormalComputationDlg.h"
#include "ccOrderChoiceDlg.h"
#include "ccProgressDialog.h"
#include "ccScalarFieldArithmeticsDlg.h"
#include "ccScalarFieldFromColorDlg.h"
#include "ccStatisticalTestDlg.h"
#include "ccCommon.h"
#include "ccConsole.h"
#include "ccEntityAction.h"
#include "ccHistogramWindow.h"
#include "ccLibAlgorithms.h"
#include "ccUtils.h"
// This is included only for temporarily removing an object from the tree.
// TODO figure out a cleaner way to do this without having to include all of mainwindow.h
#include "mainwindow.h"
namespace ccEntityAction
{
static QString GetFirstAvailableSFName(const ccPointCloud* cloud, const QString& baseName)
{
if (cloud == nullptr)
{
Q_ASSERT(false);
return QString();
}
QString name = baseName;
int tries = 0;
while (cloud->getScalarFieldIndexByName(qPrintable(name)) >= 0 || tries > 99)
name = QString("%1 #%2").arg(baseName).arg(++tries);
if (tries > 99)
return QString();
return name;
}
//////////
// Colours
bool setColor(ccHObject::Container selectedEntities, bool colorize, QWidget *parent)
{
QColor colour = QColorDialog::getColor(Qt::white, parent);
if (!colour.isValid())
return false;
while (!selectedEntities.empty())
{
ccHObject* ent = selectedEntities.back();
selectedEntities.pop_back();
if (ent->isA(CC_TYPES::HIERARCHY_OBJECT))
{
//automatically parse a group's children set
for (unsigned i = 0; i < ent->getChildrenNumber(); ++i)
selectedEntities.push_back(ent->getChild(i));
}
else if (ent->isA(CC_TYPES::POINT_CLOUD) || ent->isA(CC_TYPES::MESH))
{
ccPointCloud* cloud = nullptr;
if (ent->isA(CC_TYPES::POINT_CLOUD))
{
cloud = static_cast<ccPointCloud*>(ent);
}
else
{
ccMesh* mesh = static_cast<ccMesh*>(ent);
ccGenericPointCloud* vertices = mesh->getAssociatedCloud();
if ( !vertices
|| !vertices->isA(CC_TYPES::POINT_CLOUD)
|| (vertices->isLocked() && !mesh->isAncestorOf(vertices)) )
{
ccLog::Warning(QString("[SetColor] Can't set color for mesh '%1' (vertices are not accessible)").arg(ent->getName()));
continue;
}
cloud = static_cast<ccPointCloud*>(vertices);
}
if (colorize)
{
cloud->colorize(static_cast<float>(colour.redF()),
static_cast<float>(colour.greenF()),
static_cast<float>(colour.blueF()) );
}
else
{
cloud->setRGBColor( ccColor::FromQColor(colour) );
}
cloud->showColors(true);
cloud->showSF(false); //just in case
cloud->prepareDisplayForRefresh();
if (ent != cloud)
{
ent->showColors(true);
}
else if (cloud->getParent() && cloud->getParent()->isKindOf(CC_TYPES::MESH))
{
cloud->getParent()->showColors(true);
cloud->getParent()->showSF(false); //just in case
}
}
else if (ent->isKindOf(CC_TYPES::PRIMITIVE))
{
ccGenericPrimitive* prim = ccHObjectCaster::ToPrimitive(ent);
ccColor::Rgb col( static_cast<ColorCompType>(colour.red()),
static_cast<ColorCompType>(colour.green()),
static_cast<ColorCompType>(colour.blue()) );
prim->setColor(col);
ent->showColors(true);
ent->showSF(false); //just in case
ent->prepareDisplayForRefresh();
}
else if (ent->isA(CC_TYPES::POLY_LINE))
{
ccPolyline* poly = ccHObjectCaster::ToPolyline(ent);
poly->setColor(ccColor::FromQColor(colour));
ent->showColors(true);
ent->showSF(false); //just in case
ent->prepareDisplayForRefresh();
}
else if (ent->isA(CC_TYPES::FACET))
{
ccFacet* facet = ccHObjectCaster::ToFacet(ent);
facet->setColor(ccColor::FromQColor(colour));
ent->showColors(true);
ent->showSF(false); //just in case
ent->prepareDisplayForRefresh();
}
else
{
ccLog::Warning(QString("[SetColor] Can't change color of entity '%1'").arg(ent->getName()));
}
}
return true;
}
bool rgbToGreyScale(const ccHObject::Container &selectedEntities)
{
for (ccHObject* ent : selectedEntities)
{
bool lockedVertices = false;
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent, &lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (cloud && cloud->isA(CC_TYPES::POINT_CLOUD))
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
if (pc->hasColors())
{
pc->convertRGBToGreyScale();
pc->showColors(true);
pc->showSF(false); //just in case
pc->prepareDisplayForRefresh();
}
}
}
return true;
}
bool setColorGradient(const ccHObject::Container &selectedEntities, QWidget *parent)
{
ccColorGradientDlg dlg(parent);
if (!dlg.exec())
return false;
unsigned char dim = dlg.getDimension();
ccColorGradientDlg::GradientType ramp = dlg.getType();
ccColorScale::Shared colorScale(nullptr);
if (ramp == ccColorGradientDlg::Default)
{
colorScale = ccColorScalesManager::GetDefaultScale();
}
else if (ramp == ccColorGradientDlg::TwoColors)
{
colorScale = ccColorScale::Create("Temp scale");
QColor first,second;
dlg.getColors(first,second);
colorScale->insert(ccColorScaleElement(0.0, first), false);
colorScale->insert(ccColorScaleElement(1.0, second), true);
}
Q_ASSERT(colorScale || ramp == ccColorGradientDlg::Banding);
const double frequency = dlg.getBandingFrequency();
for (ccHObject* ent : selectedEntities)
{
bool lockedVertices = false;
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent,&lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (cloud && cloud->isA(CC_TYPES::POINT_CLOUD)) // TODO
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
bool success = false;
if (ramp == ccColorGradientDlg::Banding)
success = pc->setRGBColorByBanding(dim, frequency);
else
success = pc->setRGBColorByHeight(dim, colorScale);
if (success)
{
ent->showColors(true);
ent->showSF(false); //just in case
ent->prepareDisplayForRefresh();
}
}
}
return true;
}
bool changeColorLevels(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.size() != 1)
{
ccConsole::Error("Select one and only one colored cloud or mesh!");
return false;
}
bool lockedVertices;
ccPointCloud* pointCloud = ccHObjectCaster::ToPointCloud(selectedEntities[0], &lockedVertices);
if (!pointCloud || lockedVertices)
{
if (lockedVertices && pointCloud)
ccUtils::DisplayLockedVerticesWarning(pointCloud->getName(), true);
return false;
}
if (!pointCloud->hasColors())
{
ccConsole::Error("Selected entity has no colors!");
return false;
}
ccColorLevelsDlg dlg(parent, pointCloud);
dlg.exec();
return true;
}
//! Interpolate colors from on entity and transfer them to another one
bool interpolateColors(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.size() != 2)
{
ccConsole::Error("Select 2 entities (clouds or meshes)!");
return false;
}
ccHObject* ent1 = selectedEntities[0];
ccHObject* ent2 = selectedEntities[1];
ccGenericPointCloud* cloud1 = ccHObjectCaster::ToGenericPointCloud(ent1);
ccGenericPointCloud* cloud2 = ccHObjectCaster::ToGenericPointCloud(ent2);
if (!cloud1 || !cloud2)
{
ccConsole::Error("Select 2 entities (clouds or meshes)!");
return false;
}
if (!cloud1->hasColors() && !cloud2->hasColors())
{
ccConsole::Error("None of the selected entities has per-point or per-vertex colors!");
return false;
}
else if (cloud1->hasColors() && cloud2->hasColors())
{
ccConsole::Error("Both entities have colors! Remove the colors on the entity you wish to import the colors to!");
return false;
}
ccGenericPointCloud* source = cloud1;
ccGenericPointCloud* dest = cloud2;
if ( cloud2->hasColors())
{
std::swap(source, dest);
std::swap(cloud1, cloud2);
std::swap(ent1, ent2);
}
if (!dest->isA(CC_TYPES::POINT_CLOUD))
{
ccConsole::Error("Destination cloud (or vertices) must be a real point cloud!");
return false;
}
ccProgressDialog pDlg(true, parent);
if (static_cast<ccPointCloud*>(dest)->interpolateColorsFrom(source, &pDlg))
{
ent2->showColors(true);
ent2->showSF(false); //just in case
}
else
{
ccConsole::Error("An error occurred! (see console)");
}
ent2->prepareDisplayForRefresh_recursive();
return true;
}
//! Interpolate scalar fields from on entity and transfer them to another one
bool interpolateSFs(const ccHObject::Container &selectedEntities, ccMainAppInterface* app)
{
if (selectedEntities.size() != 2)
{
ccConsole::Error("Select 2 entities (clouds or meshes)!");
return false;
}
ccHObject* ent1 = selectedEntities[0];
ccHObject* ent2 = selectedEntities[1];
ccPointCloud* cloud1 = ccHObjectCaster::ToPointCloud(ent1);
ccPointCloud* cloud2 = ccHObjectCaster::ToPointCloud(ent2);
if (!cloud1 || !cloud2)
{
ccConsole::Error("Select 2 entities (clouds or meshes)!");
return false;
}
if (!cloud1->hasScalarFields() && !cloud2->hasScalarFields())
{
ccConsole::Error("None of the selected entities has per-point or per-vertex colors!");
return false;
}
else if (cloud1->hasScalarFields() && cloud2->hasScalarFields())
{
//ask the user to chose which will be the 'source' cloud
ccOrderChoiceDlg ocDlg(cloud1, "Source", cloud2, "Destination", app);
if (!ocDlg.exec())
{
//process cancelled by the user
return false;
}
if (cloud1 != ocDlg.getFirstEntity())
{
std::swap(cloud1, cloud2);
}
}
else if (cloud2->hasScalarFields())
{
std::swap(cloud1, cloud2);
}
ccPointCloud* source = cloud1;
ccPointCloud* dest = cloud2;
//show the list of scalar fields available on the source point cloud
std::vector<int> sfIndexes;
try
{
unsigned sfCount = source->getNumberOfScalarFields();
if (sfCount == 1)
{
sfIndexes.push_back(0);
}
else if (sfCount > 1)
{
ccItemSelectionDlg isDlg(true, app->getMainWindow(), "entity");
QStringList scalarFields;
{
for (unsigned i = 0; i < sfCount; ++i)
{
scalarFields << source->getScalarFieldName(i);
}
}
isDlg.setItems(scalarFields, 0);
if (!isDlg.exec())
{
//cancelled by the user
return false;
}
isDlg.getSelectedIndexes(sfIndexes);
if (sfIndexes.empty())
{
ccConsole::Error("No scalar field was selected");
return false;
}
}
else
{
assert(false);
}
}
catch (const std::bad_alloc&)
{
ccConsole::Error("Not enough memory");
return false;
}
//semi-persistent parameters
static ccPointCloudInterpolator::Parameters::Method s_interpMethod = ccPointCloudInterpolator::Parameters::RADIUS;
static ccPointCloudInterpolator::Parameters::Algo s_interpAlgo = ccPointCloudInterpolator::Parameters::NORMAL_DIST;
static int s_interpKNN = 6;
ccInterpolationDlg iDlg(app->getMainWindow());
iDlg.setInterpolationMethod(s_interpMethod);
iDlg.setInterpolationAlgorithm(s_interpAlgo);
iDlg.knnSpinBox->setValue(s_interpKNN);
iDlg.radiusDoubleSpinBox->setValue(dest->getOwnBB().getDiagNormd() / 100);
if (!iDlg.exec())
{
//process cancelled by the user
return false;
}
//setup parameters
ccPointCloudInterpolator::Parameters params;
params.method = s_interpMethod = iDlg.getInterpolationMethod();
params.algo = s_interpAlgo = iDlg.getInterpolationAlgorithm();
params.knn = s_interpKNN = iDlg.knnSpinBox->value();
params.radius = iDlg.radiusDoubleSpinBox->value();
params.sigma = iDlg.kernelDoubleSpinBox->value();
ccProgressDialog pDlg(true, app->getMainWindow());
unsigned sfCountBefore = dest->getNumberOfScalarFields();
if (ccPointCloudInterpolator::InterpolateScalarFieldsFrom(dest, source, sfIndexes, params, &pDlg))
{
dest->setCurrentDisplayedScalarField(static_cast<int>(std::min(sfCountBefore + 1, dest->getNumberOfScalarFields())) - 1);
dest->showSF(true);
}
else
{
ccConsole::Error("An error occurred! (see console)");
}
dest->prepareDisplayForRefresh_recursive();
return true;
}
bool convertTextureToColor(const ccHObject::Container& selectedEntities, QWidget *parent)
{
for (ccHObject* ent : selectedEntities)
{
if (ent->isA(CC_TYPES::MESH)/*|| ent->isKindOf(CC_TYPES::PRIMITIVE)*/) //TODO
{
ccMesh* mesh = ccHObjectCaster::ToMesh(ent);
Q_ASSERT(mesh);
if (!mesh->hasMaterials())
{
ccLog::Warning(QString("[convertTextureToColor] Mesh '%1' has no material/texture!").arg(mesh->getName()));
continue;
}
else
{
if ( mesh->hasColors()
&& QMessageBox::warning( parent,
"Mesh already has colors",
QString("Mesh '%1' already has colors! Overwrite them?").arg(mesh->getName()),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No) != QMessageBox::Yes)
{
continue;
}
//ColorCompType C[3]={MAX_COLOR_COMP,MAX_COLOR_COMP,MAX_COLOR_COMP};
//mesh->getColorFromMaterial(triIndex,*P,C,withRGB);
//cloud->addRGBColor(C);
if (mesh->convertMaterialsToVertexColors())
{
mesh->showColors(true);
mesh->showSF(false); //just in case
mesh->showMaterials(false);
mesh->prepareDisplayForRefresh_recursive();
}
else
{
ccLog::Warning(QString("[convertTextureToColor] Failed to convert texture on mesh '%1'!").arg(mesh->getName()));
}
}
}
}
return true;
}
bool enhanceRGBWithIntensities(const ccHObject::Container& selectedEntities, QWidget *parent)
{
QString defaultSFName("Intensity");
bool useCustomIntensityRange = false;
static double s_minI = 0.0, s_maxI = 1.0;
if (QMessageBox::question(parent, "Intensity range", "Do you want to define the theoretical intensity range (yes)\nor use the actual one (no)?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
{
ccAskTwoDoubleValuesDlg atdvDlg("Min", "Max", -1000000.0, 1000000.0, s_minI, s_maxI, 3, "Theroetical intensity", parent);
if (!atdvDlg.exec())
{
//process cancelled by the user
return false;
}
s_minI = atdvDlg.doubleSpinBox1->value();
s_maxI = atdvDlg.doubleSpinBox2->value();
useCustomIntensityRange = true;
}
for (ccHObject* ent : selectedEntities)
{
bool lockedVertices = false;
ccPointCloud* pc = ccHObjectCaster::ToPointCloud(ent, &lockedVertices);
if (!pc || lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (!pc->hasColors())
{
ccLog::Warning(QString("[enhanceRGBWithIntensities] Entity '%1' has no RGB color!").arg(ent->getName()));
continue;
}
if (!pc->hasScalarFields())
{
ccLog::Warning(QString("[enhanceRGBWithIntensities] Entity '%1' has no scalar field!").arg(ent->getName()));
continue;
}
int sfIdx = -1;
if (pc->getNumberOfScalarFields() > 1)
{
//does the previously selected SF works?
if (!defaultSFName.isEmpty())
{
//if it's valid, we'll keep this SF!
sfIdx = pc->getScalarFieldIndexByName(qPrintable(defaultSFName));
}
if (sfIdx < 0)
{
//let the user choose the right scalar field
ccPickOneElementDlg poeDlg("Intensity scalar field", "Choose scalar field", parent);
for (unsigned i = 0; i < pc->getNumberOfScalarFields(); ++i)
{
CCLib::ScalarField* sf = pc->getScalarField(i);
assert(sf);
QString sfName(sf->getName());
poeDlg.addElement(sfName);
if (sfIdx < 0 && sfName.contains("intensity", Qt::CaseInsensitive))
{
sfIdx = static_cast<int>(i);
}
}
poeDlg.setDefaultIndex(std::max(0, sfIdx));
if (!poeDlg.exec())
{
//process cancelled by the user
return false;
}
sfIdx = poeDlg.getSelectedIndex();
defaultSFName = pc->getScalarField(sfIdx)->getName();
}
}
else
{
sfIdx = 0;
}
assert(sfIdx >= 0);
if (pc->enhanceRGBWithIntensitySF(sfIdx, useCustomIntensityRange, s_minI, s_maxI))
{
ent->prepareDisplayForRefresh();
ent->showColors(true);
ent->showSF(false);
}
else
{
ccLog::Warning(QString("[enhanceRGBWithIntensities] Failed to apply the process on entity '%1'!").arg(ent->getName()));
}
}
return true;
}
//////////
// Scalar Fields
bool sfGaussianFilter(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.empty())
return false;
double sigma = ccLibAlgorithms::GetDefaultCloudKernelSize(selectedEntities);
if (sigma < 0.0)
{
ccConsole::Error("No eligible point cloud in selection!");
return false;
}
bool ok = false;
sigma = QInputDialog::getDouble(parent,
"Gaussian filter",
"sigma:",
sigma,
DBL_MIN,
1.0e9,
8,
&ok);
if (!ok)
return false;
ccProgressDialog pDlg(true, parent);
pDlg.setAutoClose(false);
for (ccHObject* ent : selectedEntities)
{
bool lockedVertices = false;
ccPointCloud* pc = ccHObjectCaster::ToPointCloud(ent, &lockedVertices);
if (!pc || lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
//la methode est activee sur le champ scalaire affiche
CCLib::ScalarField* sf = pc->getCurrentDisplayedScalarField();
if (sf != nullptr)
{
//on met en lecture (OUT) le champ scalaire actuellement affiche
int outSfIdx = pc->getCurrentDisplayedScalarFieldIndex();
Q_ASSERT(outSfIdx >= 0);
pc->setCurrentOutScalarField(outSfIdx);
CCLib::ScalarField* outSF = pc->getCurrentOutScalarField();
Q_ASSERT(sf != nullptr);
QString sfName = QString("%1.smooth(%2)").arg(outSF->getName()).arg(sigma);
int sfIdx = pc->getScalarFieldIndexByName(qPrintable(sfName));
if (sfIdx < 0)
sfIdx = pc->addScalarField(qPrintable(sfName)); //output SF has same type as input SF
if (sfIdx >= 0)
pc->setCurrentInScalarField(sfIdx);
else
{
ccConsole::Error(QString("Failed to create scalar field for cloud '%1' (not enough memory?)").arg(pc->getName()));
continue;
}
ccOctree::Shared octree = pc->getOctree();
if (!octree)
{
octree = pc->computeOctree(&pDlg);
if (!octree)
{
ccConsole::Error(QString("Couldn't compute octree for cloud '%1'!").arg(pc->getName()));
continue;
}
}
if (octree)
{
QElapsedTimer eTimer;
eTimer.start();
CCLib::ScalarFieldTools::applyScalarFieldGaussianFilter(static_cast<PointCoordinateType>(sigma),
pc,
-1,
&pDlg,
octree.data());
ccConsole::Print("[GaussianFilter] Timing: %3.2f s.", static_cast<double>(eTimer.elapsed()) / 1000.0);
pc->setCurrentDisplayedScalarField(sfIdx);
pc->showSF(sfIdx >= 0);
sf = pc->getCurrentDisplayedScalarField();
if (sf)
sf->computeMinAndMax();
pc->prepareDisplayForRefresh_recursive();
}
else
{
ccConsole::Error(QString("Failed to compute entity [%1] octree! (not enough memory?)").arg(pc->getName()));
}
}
else
{
ccConsole::Warning(QString("Entity [%1] has no active scalar field!").arg(pc->getName()));
}
}
return true;
}
bool sfBilateralFilter(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.empty())
return false;
double sigma = ccLibAlgorithms::GetDefaultCloudKernelSize(selectedEntities);
if (sigma < 0.0)
{
ccConsole::Error("No eligible point cloud in selection!");
return false;
}
//estimate a good value for scalar field sigma, based on the first cloud
//and its displayed scalar field
ccPointCloud* pc_test = ccHObjectCaster::ToPointCloud(selectedEntities[0]);
CCLib::ScalarField* sf_test = pc_test->getCurrentDisplayedScalarField();
ScalarType range = sf_test->getMax() - sf_test->getMin();
double scalarFieldSigma = range / 4; // using 1/4 of total range
ccAskTwoDoubleValuesDlg dlg("Spatial sigma",
"Scalar sigma",
DBL_MIN,
1.0e9,
sigma,
scalarFieldSigma,
8,
nullptr,
parent);
dlg.doubleSpinBox1->setStatusTip("3*sigma = 98% attenuation");
dlg.doubleSpinBox2->setStatusTip("Scalar field's sigma controls how much the filter behaves as a Gaussian Filter\n sigma at +inf uses the whole range of scalars ");
if (!dlg.exec())
return false;
//get values
sigma = dlg.doubleSpinBox1->value();
scalarFieldSigma = dlg.doubleSpinBox2->value();
ccProgressDialog pDlg(true, parent);
pDlg.setAutoClose(false);
for (ccHObject* ent : selectedEntities)
{
bool lockedVertices = false;
ccPointCloud* pc = ccHObjectCaster::ToPointCloud(ent, &lockedVertices);
if (!pc || lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
//the algorithm will use the currently displayed SF
CCLib::ScalarField* sf = pc->getCurrentDisplayedScalarField();
if (sf != nullptr)
{
//we set the displayed SF as "OUT" SF
int outSfIdx = pc->getCurrentDisplayedScalarFieldIndex();
Q_ASSERT(outSfIdx >= 0);
pc->setCurrentOutScalarField(outSfIdx);
CCLib::ScalarField* outSF = pc->getCurrentOutScalarField();
Q_ASSERT(outSF != nullptr);
QString sfName = QString("%1.bilsmooth(%2,%3)").arg(outSF->getName()).arg(sigma).arg(scalarFieldSigma);
int sfIdx = pc->getScalarFieldIndexByName(qPrintable(sfName));
if (sfIdx < 0)
sfIdx = pc->addScalarField(qPrintable(sfName)); //output SF has same type as input SF
if (sfIdx >= 0)
pc->setCurrentInScalarField(sfIdx);
else
{
ccConsole::Error(QString("Failed to create scalar field for cloud '%1' (not enough memory?)").arg(pc->getName()));
continue;
}
ccOctree::Shared octree = pc->getOctree();
if (!octree)
{
octree = pc->computeOctree(&pDlg);
if (!octree)
{
ccConsole::Error(QString("Couldn't compute octree for cloud '%1'!").arg(pc->getName()));
continue;
}
}
Q_ASSERT(octree != nullptr);
{
QElapsedTimer eTimer;
eTimer.start();
CCLib::ScalarFieldTools::applyScalarFieldGaussianFilter(static_cast<PointCoordinateType>(sigma),
pc,
static_cast<PointCoordinateType>(scalarFieldSigma),
&pDlg,
octree.data());
ccConsole::Print("[BilateralFilter] Timing: %3.2f s.", eTimer.elapsed() / 1000.0);
pc->setCurrentDisplayedScalarField(sfIdx);
pc->showSF(sfIdx >= 0);
sf = pc->getCurrentDisplayedScalarField();
if (sf)
sf->computeMinAndMax();
pc->prepareDisplayForRefresh_recursive();
}
}
else
{
ccConsole::Warning(QString("Entity [%1] has no active scalar field!").arg(pc->getName()));
}
}
return true;
}
bool sfConvertToRGB(const ccHObject::Container &selectedEntities, QWidget *parent)
{
//we first ask the user if the SF colors should be mixed with existing colors
bool mixWithExistingColors = false;
QMessageBox::StandardButton answer = QMessageBox::warning( parent,
"Scalar Field to RGB",
"Mix with existing colors (if any)?",
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::Yes );
if (answer == QMessageBox::Yes)
mixWithExistingColors = true;
else if (answer == QMessageBox::Cancel)
return false;
for (ccHObject* ent : selectedEntities)
{
ccGenericPointCloud* cloud = nullptr;
bool lockedVertices = false;
cloud = ccHObjectCaster::ToPointCloud(ent, &lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (cloud != nullptr) //TODO
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
//if there is no displayed SF --> nothing to do!
if (pc->getCurrentDisplayedScalarField())
{
if (pc->setRGBColorWithCurrentScalarField(mixWithExistingColors))
{
ent->showColors(true);
ent->showSF(false); //just in case
}
}
cloud->prepareDisplayForRefresh_recursive();
}
}
return true;
}
bool sfConvertToRandomRGB(const ccHObject::Container &selectedEntities, QWidget *parent)
{
static int s_randomColorsNumber = 256;
bool ok = false;
s_randomColorsNumber = QInputDialog::getInt(parent,
"Random colors",
"Number of random colors (will be regularly sampled over the SF interval):",
s_randomColorsNumber,
2,
INT_MAX,
16,
&ok);
if (!ok)
return false;
Q_ASSERT(s_randomColorsNumber > 1);
ColorsTableType* randomColors = new ColorsTableType;
if (!randomColors->reserveSafe(static_cast<unsigned>(s_randomColorsNumber)))
{
ccConsole::Error("Not enough memory!");
return false;
}
//generate random colors
for (int i = 0; i < s_randomColorsNumber; ++i)
{
ccColor::Rgb col = ccColor::Generator::Random();
randomColors->addElement(col);
}
//apply random colors
for (ccHObject* ent : selectedEntities)
{
ccGenericPointCloud* cloud = nullptr;
bool lockedVertices = false;
cloud = ccHObjectCaster::ToPointCloud(ent, &lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (cloud != nullptr) //TODO
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
ccScalarField* sf = pc->getCurrentDisplayedScalarField();
//if there is no displayed SF --> nothing to do!
if (sf && sf->currentSize() >= pc->size())
{
if (!pc->resizeTheRGBTable(false))
{
ccConsole::Error("Not enough memory!");
break;
}
else
{
ScalarType minSF = sf->getMin();
ScalarType maxSF = sf->getMax();
ScalarType step = (maxSF - minSF) / (s_randomColorsNumber - 1);
if (step == 0)
step = static_cast<ScalarType>(1.0);
for (unsigned i = 0; i < pc->size(); ++i)
{
ScalarType val = sf->getValue(i);
unsigned colIndex = static_cast<unsigned>((val - minSF) / step);
if (colIndex == s_randomColorsNumber)
--colIndex;
pc->setPointColor(i, randomColors->getValue(colIndex));
}
pc->showColors(true);
pc->showSF(false); //just in case
}
}
cloud->prepareDisplayForRefresh_recursive();
}
}
return true;
}
bool sfRename(const ccHObject::Container &selectedEntities, QWidget *parent)
{
for (ccHObject* ent : selectedEntities)
{
ccGenericPointCloud* cloud = ccHObjectCaster::ToPointCloud(ent);
if (cloud != nullptr) //TODO
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
ccScalarField* sf = pc->getCurrentDisplayedScalarField();
//if there is no displayed SF --> nothing to do!
if (sf == nullptr)
{
ccConsole::Warning(QString("Cloud %1 has no displayed scalar field!").arg(pc->getName()));
}
else
{
const char* sfName = sf->getName();
bool ok = false;
QString newName = QInputDialog::getText(parent,
"SF name",
"name:",
QLineEdit::Normal,
QString(sfName ? sfName : "unknown"),
&ok);
if (ok)
sf->setName(qPrintable(newName));
}
}
}
return true;
}
bool sfAddIdField(const ccHObject::Container &selectedEntities)
{
for (ccHObject* ent : selectedEntities)
{
ccGenericPointCloud* cloud = ccHObjectCaster::ToPointCloud(ent);
if (cloud != nullptr) //TODO
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
int sfIdx = pc->getScalarFieldIndexByName(CC_DEFAULT_ID_SF_NAME);
if (sfIdx < 0)
sfIdx = pc->addScalarField(CC_DEFAULT_ID_SF_NAME);
if (sfIdx < 0)
{
ccLog::Warning("Not enough memory!");
return false;
}
CCLib::ScalarField* sf = pc->getScalarField(sfIdx);
Q_ASSERT(sf->currentSize() == pc->size());
for (unsigned j=0 ; j<cloud->size(); j++)
{
ScalarType idValue = static_cast<ScalarType>(j);
sf->setValue(j, idValue);
}
sf->computeMinAndMax();
pc->setCurrentDisplayedScalarField(sfIdx);
pc->showSF(true);
pc->prepareDisplayForRefresh();
}
}
return true;
}
bool sfSetAsCoord(const ccHObject::Container &selectedEntities, QWidget *parent)
{
ccExportCoordToSFDlg ectsDlg(parent);
ectsDlg.warningLabel->setVisible(false);
ectsDlg.setWindowTitle("Export SF to coordinate(s)");
if (!ectsDlg.exec())
return false;
bool exportDim[3] = { ectsDlg.exportX(), ectsDlg.exportY(), ectsDlg.exportZ() };
if (!exportDim[0] && !exportDim[1] && !exportDim[2]) //nothing to do?!
return false;
//for each selected cloud (or vertices set)
for (ccHObject* ent : selectedEntities)
{
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent);
if (cloud && cloud->isA(CC_TYPES::POINT_CLOUD))
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
ccScalarField* sf = pc->getCurrentDisplayedScalarField();
if (sf != nullptr)
{
unsigned ptsCount = pc->size();
bool hasDefaultValueForNaN = false;
ScalarType defaultValueForNaN = sf->getMin();
for (unsigned i = 0; i < ptsCount; ++i)
{
ScalarType s = sf->getValue(i);
//handle NaN values
if (!CCLib::ScalarField::ValidValue(s))
{
if (!hasDefaultValueForNaN)
{
bool ok = false;
double out = QInputDialog::getDouble( parent,
"SF --> coordinate",
"Enter the coordinate equivalent for NaN values:",
defaultValueForNaN,
-1.0e9,
1.0e9,
6,
&ok);
if (ok)
defaultValueForNaN = static_cast<ScalarType>(out);
else
ccLog::Warning("[SetSFAsCoord] By default the coordinate equivalent for NaN values will be the minimum SF value");
hasDefaultValueForNaN = true;
}
s = defaultValueForNaN;
}
CCVector3* P = const_cast<CCVector3*>(pc->getPoint(i));
//test each dimension
if (exportDim[0])
P->x = s;
if (exportDim[1])
P->y = s;
if (exportDim[2])
P->z = s;
}
pc->invalidateBoundingBox();
}
}
}
return true;
}
bool exportCoordToSF(const ccHObject::Container &selectedEntities, QWidget* parent)
{
ccExportCoordToSFDlg ectsDlg(parent);
if (!ectsDlg.exec())
{
return false;
}
bool exportDims[3] = { ectsDlg.exportX(),
ectsDlg.exportY(),
ectsDlg.exportZ() };
if (!exportDims[0] && !exportDims[1] && !exportDims[2]) //nothing to do?!
{
return false;
}
//for each selected cloud (or vertices set)
for (ccHObject* entity : selectedEntities)
{
ccPointCloud* pc = ccHObjectCaster::ToPointCloud(entity);
if (pc == nullptr)
{
// TODO do something with error?
continue;
}
if (!pc->exportCoordToSF(exportDims))
{
ccLog::Error("The process failed!");
return true; //true because we want the UI to be updated anyway
}
if (entity != pc)
{
entity->showSF(true); //for meshes
}
entity->prepareDisplayForRefresh_recursive();
}
return true;
}
bool sfArithmetic(const ccHObject::Container &selectedEntities, QWidget *parent)
{
Q_ASSERT(!selectedEntities.empty());
ccHObject* entity = selectedEntities[0];
bool lockedVertices;
ccPointCloud* cloud = ccHObjectCaster::ToPointCloud(entity,&lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(entity->getName(),true);
return false;
}
if (cloud == nullptr)
{
return false;
}
ccScalarFieldArithmeticsDlg sfaDlg(cloud,parent);
if (!sfaDlg.exec())
{
return false;
}
if (!sfaDlg.apply(cloud))
{
ccConsole::Error("An error occurred (see Console for more details)");
}
cloud->showSF(true);
cloud->prepareDisplayForRefresh_recursive();
return true;
}
bool sfFromColor(const ccHObject::Container &selectedEntities, QWidget *parent)
{
//candidates
std::unordered_set<ccPointCloud*> clouds;
for (ccHObject* ent : selectedEntities)
{
ccPointCloud* cloud = ccHObjectCaster::ToPointCloud(ent);
if (cloud && ent->hasColors()) //only for clouds (or vertices)
clouds.insert( cloud );
}
if (clouds.empty())
return false;
ccScalarFieldFromColorDlg dialog(parent);
if (!dialog.exec())
return false;
const bool exportR = dialog.getRStatus();
const bool exportG = dialog.getGStatus();
const bool exportB = dialog.getBStatus();
const bool exportC = dialog.getCompositeStatus();
for (const auto cloud : clouds)
{
std::vector<ccScalarField*> fields(4);
fields[0] = (exportR ? new ccScalarField(qPrintable(GetFirstAvailableSFName(cloud,"R"))) : nullptr);
fields[1] = (exportG ? new ccScalarField(qPrintable(GetFirstAvailableSFName(cloud,"G"))) : nullptr);
fields[2] = (exportB ? new ccScalarField(qPrintable(GetFirstAvailableSFName(cloud,"B"))) : nullptr);
fields[3] = (exportC ? new ccScalarField(qPrintable(GetFirstAvailableSFName(cloud,"Composite"))) : nullptr);
//try to instantiate memory for each field
unsigned count = cloud->size();
for (ccScalarField*& sf : fields)
{
if (sf && !sf->reserveSafe(count))
{
ccLog::Warning(QString("[sfFromColor] Not enough memory to instantiate SF '%1' on cloud '%2'").arg(sf->getName(), cloud->getName()));
sf->release();
sf = nullptr;
}
}
//export points
for (unsigned j = 0; j < cloud->size(); ++j)
{
const ccColor::Rgb& rgb = cloud->getPointColor(j);
if (fields[0])
fields[0]->addElement(rgb.r);
if (fields[1])
fields[1]->addElement(rgb.g);
if (fields[2])
fields[2]->addElement(rgb.b);
if (fields[3])
fields[3]->addElement(static_cast<ScalarType>(rgb.r + rgb.g + rgb.b) / 3);
}
QString fieldsStr;
for (ccScalarField*& sf : fields)
{
if (sf == nullptr)
continue;
sf->computeMinAndMax();
int sfIdx = cloud->getScalarFieldIndexByName(sf->getName());
if (sfIdx >= 0)
cloud->deleteScalarField(sfIdx);
sfIdx = cloud->addScalarField(sf);
Q_ASSERT(sfIdx >= 0);
if (sfIdx >= 0)
{
cloud->setCurrentDisplayedScalarField(sfIdx);
cloud->showSF(true);
cloud->prepareDisplayForRefresh();
//mesh vertices?
if (cloud->getParent() && cloud->getParent()->isKindOf(CC_TYPES::MESH))
{
cloud->getParent()->showSF(true);
cloud->getParent()->prepareDisplayForRefresh();
}
if (!fieldsStr.isEmpty())
fieldsStr.append(", ");
fieldsStr.append(sf->getName());
}
else
{
ccConsole::Warning(QString("[sfFromColor] Failed to add scalar field '%1' to cloud '%2'?!").arg(sf->getName(), cloud->getName()));
sf->release();
sf = nullptr;
}
}
if (!fieldsStr.isEmpty())
ccLog::Print(QString("[sfFromColor] New scalar fields (%1) added to '%2'").arg(fieldsStr, cloud->getName()));
}
return true;
}
bool processMeshSF(const ccHObject::Container &selectedEntities, ccMesh::MESH_SCALAR_FIELD_PROCESS process, QWidget *parent)
{
for (ccHObject* ent : selectedEntities)
{
if (ent->isKindOf(CC_TYPES::MESH) || ent->isKindOf(CC_TYPES::PRIMITIVE)) //TODO
{
ccMesh* mesh = ccHObjectCaster::ToMesh(ent);
if (mesh == nullptr)
continue;
ccGenericPointCloud* cloud = mesh->getAssociatedCloud();
if (cloud == nullptr)
continue;
if (cloud->isA(CC_TYPES::POINT_CLOUD)) //TODO
{
ccPointCloud* pc = static_cast<ccPointCloud*>(cloud);
//on active le champ scalaire actuellement affiche
int sfIdx = pc->getCurrentDisplayedScalarFieldIndex();
if (sfIdx >= 0)
{
pc->setCurrentScalarField(sfIdx);
mesh->processScalarField(process);
pc->getCurrentInScalarField()->computeMinAndMax();
mesh->prepareDisplayForRefresh_recursive();
}
else
{
ccConsole::Warning(QString("Mesh [%1] vertices have no activated scalar field!").arg(mesh->getName()));
}
}
}
}
return true;
}
//////////
// Normals
bool computeNormals(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.empty())
{
ccConsole::Error("Select at least one point cloud");
return false;
}
//look for clouds and meshes
std::vector<ccPointCloud*> clouds;
bool withScanGrid = false;
bool withSensor = false;
std::vector<ccMesh*> meshes;
PointCoordinateType defaultRadius = 0;
try
{
for (const auto entity : selectedEntities)
{
if (entity->isA(CC_TYPES::POINT_CLOUD))
{
ccPointCloud* cloud = static_cast<ccPointCloud*>(entity);
clouds.push_back(cloud);
if (cloud->gridCount() > 0)
{
withScanGrid = true;
}
for (unsigned i = 0; i < cloud->getChildrenNumber(); ++i)
{
if (cloud->hasSensor())
{
withSensor = true;
}
}
if (defaultRadius == 0)
{
//default radius
defaultRadius = ccNormalVectors::GuessNaiveRadius(cloud);
}
}
else if (entity->isKindOf(CC_TYPES::MESH))
{
if (entity->isA(CC_TYPES::MESH))
{
ccMesh* mesh = ccHObjectCaster::ToMesh(entity);
meshes.push_back(mesh);
}
else
{
ccConsole::Error(QString("Can't compute normals on sub-meshes! Select the parent mesh instead"));
return false;
}
}
}
}
catch (const std::bad_alloc&)
{
ccConsole::Error("Not enough memory!");
return false;
}
//compute normals for each selected cloud
if (!clouds.empty())
{
static CC_LOCAL_MODEL_TYPES s_lastModelType = LS;
static ccNormalVectors::Orientation s_lastNormalOrientation = ccNormalVectors::UNDEFINED;
static int s_lastMSTNeighborCount = 6;
static double s_lastMinGridAngle_deg = 1.0;
ccNormalComputationDlg ncDlg(withScanGrid, withSensor, parent);
ncDlg.setLocalModel(s_lastModelType);
ncDlg.setRadius(defaultRadius);
ncDlg.setPreferredOrientation(s_lastNormalOrientation);
ncDlg.setMSTNeighborCount(s_lastMSTNeighborCount);
ncDlg.setMinGridAngle_deg(s_lastMinGridAngle_deg);
if (clouds.size() == 1)
{
ncDlg.setCloud(clouds.front());
}
if (!ncDlg.exec())
return false;
//normals computation
CC_LOCAL_MODEL_TYPES model = s_lastModelType = ncDlg.getLocalModel();
bool useGridStructure = withScanGrid && ncDlg.useScanGridsForComputation();
defaultRadius = ncDlg.getRadius();
double minGridAngle_deg = s_lastMinGridAngle_deg = ncDlg.getMinGridAngle_deg();
//normals orientation
bool orientNormals = ncDlg.orientNormals();
bool orientNormalsWithGrids = withScanGrid && ncDlg.useScanGridsForOrientation();
bool orientNormalsWithSensors = withSensor && ncDlg.useSensorsForOrientation();
ccNormalVectors::Orientation preferredOrientation = s_lastNormalOrientation = ncDlg.getPreferredOrientation();
bool orientNormalsMST = ncDlg.useMSTOrientation();
int mstNeighbors = s_lastMSTNeighborCount = ncDlg.getMSTNeighborCount();
ccProgressDialog pDlg(true, parent);
pDlg.setAutoClose(false);
size_t errors = 0;
for (auto cloud : clouds)
{
Q_ASSERT(cloud != nullptr);
bool result = false;
bool normalsAlreadyOriented = false;
if (useGridStructure && cloud->gridCount())
{
#if 0
ccPointCloud* newCloud = new ccPointCloud("temp");
newCloud->reserve(cloud->size());
for (size_t gi=0; gi<cloud->gridCount(); ++gi)
{
const ccPointCloud::Grid::Shared& scanGrid = cloud->grid(gi);
if (scanGrid && scanGrid->indexes.empty())
{
//empty grid, we skip it
continue;
}
ccGLMatrixd toSensor = scanGrid->sensorPosition.inverse();
const int* _indexGrid = scanGrid->indexes.data();
for (int j = 0; j < static_cast<int>(scanGrid->h); ++j)
{
for (int i = 0; i < static_cast<int>(scanGrid->w); ++i, ++_indexGrid)
{
if (*_indexGrid >= 0)
{
unsigned pointIndex = static_cast<unsigned>(*_indexGrid);
const CCVector3* P = cloud->getPoint(pointIndex);
CCVector3 Q = toSensor * (*P);
newCloud->addPoint(Q);
}
}
}
addToDB(newCloud);
}
#endif
//compute normals with the associated scan grid(s)
normalsAlreadyOriented = true;
result = cloud->computeNormalsWithGrids(minGridAngle_deg, &pDlg);
}
else
{
//compute normals with the octree
normalsAlreadyOriented = orientNormals && (preferredOrientation != ccNormalVectors::UNDEFINED);
result = cloud->computeNormalsWithOctree(model, orientNormals ? preferredOrientation : ccNormalVectors::UNDEFINED, defaultRadius, &pDlg);
}
//do we need to orient the normals? (this may have been already done if 'orientNormalsForThisCloud' is true)
if (result && orientNormals && !normalsAlreadyOriented)
{
if (cloud->gridCount() && orientNormalsWithGrids)
{
//we can still use the grid structure(s) to orient the normals!
result = cloud->orientNormalsWithGrids();
}
else if (cloud->hasSensor() && orientNormalsWithSensors)
{
result = false;
// RJ: TODO: the issue here is that a cloud can have multiple sensors.
// As the association to sensor is not explicit in CC, given a cloud
// some points can belong to one sensor and some others can belongs to others sensors.
// so it's why here grid orientation has precedence over sensor orientation because in this
// case association is more explicit.
// Here we take the first valid viewpoint for now even if it's not a really good...
CCVector3 sensorPosition;
for (size_t i = 0; i < cloud->getChildrenNumber(); ++i)
{
ccHObject* child = cloud->getChild(static_cast<unsigned>(i));
if (child && child->isKindOf(CC_TYPES::SENSOR))
{
ccSensor* sensor = ccHObjectCaster::ToSensor(child);
if (sensor->getActiveAbsoluteCenter(sensorPosition))
{
result = cloud->orientNormalsTowardViewPoint(sensorPosition, &pDlg);
break;
}
}
}
}
else if (orientNormalsMST)
{
//use Minimum Spanning Tree to resolve normals direction
result = cloud->orientNormalsWithMST(mstNeighbors, &pDlg);
}
}
if (!result)
{
++errors;
}
cloud->prepareDisplayForRefresh();
}
if (errors != 0)
{
if (errors < clouds.size())
ccConsole::Error("Failed to compute or orient the normals on some clouds! (see console)");
else
ccConsole::Error("Failed to compute or orient the normals! (see console)");
}
}
//compute normals for each selected mesh
if (!meshes.empty())
{
QMessageBox question( QMessageBox::Question,
"Mesh normals",
"Compute per-vertex normals (smooth) or per-triangle (faceted)?",
QMessageBox::NoButton,
parent);
QPushButton* perVertexButton = question.addButton("Per-vertex", QMessageBox::YesRole);
QPushButton* perTriangleButton = question.addButton("Per-triangle", QMessageBox::NoRole);
question.exec();
bool computePerVertexNormals = (question.clickedButton() == perVertexButton);
for (auto mesh : meshes)
{
Q_ASSERT(mesh != nullptr);
//we remove temporarily the mesh as its normals may be removed (and they can be a child object)
MainWindow* instance = dynamic_cast<MainWindow*>(parent);
MainWindow::ccHObjectContext objContext;
if (instance)
objContext = instance->removeObjectTemporarilyFromDBTree(mesh);
mesh->clearTriNormals();
mesh->showNormals(false);
bool result = mesh->computeNormals(computePerVertexNormals);
if (instance)
instance->putObjectBackIntoDBTree(mesh,objContext);
if (!result)
{
ccConsole::Error(QString("Failed to compute normals on mesh '%1'").arg(mesh->getName()));
continue;
}
mesh->prepareDisplayForRefresh_recursive();
}
}
return true;
}
bool invertNormals(const ccHObject::Container &selectedEntities)
{
for (ccHObject* ent : selectedEntities)
{
bool lockedVertices;
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent, &lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (cloud && cloud->isA(CC_TYPES::POINT_CLOUD)) // TODO
{
ccPointCloud* ccCloud = static_cast<ccPointCloud*>(cloud);
if (ccCloud->hasNormals())
{
ccCloud->invertNormals();
ccCloud->showNormals(true);
ccCloud->prepareDisplayForRefresh_recursive();
}
}
}
return true;
}
bool orientNormalsFM(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.empty())
{
ccConsole::Error("Select at least one point cloud");
return false;
}
bool ok = false;
const int s_defaultLevel = 6;
int value = QInputDialog::getInt( parent,
"Orient normals (FM)", "Octree level",
s_defaultLevel,
1, CCLib::DgmOctree::MAX_OCTREE_LEVEL,
1,
&ok);
if (!ok)
return false;
Q_ASSERT(value >= 0 && value <= 255);
unsigned char level = static_cast<unsigned char>(value);
ccProgressDialog pDlg(false, parent);
pDlg.setAutoClose(false);
size_t errors = 0;
for (ccHObject* entity : selectedEntities)
{
if (!entity->isA(CC_TYPES::POINT_CLOUD))
continue;
ccPointCloud* cloud = static_cast<ccPointCloud*>(entity);
if (!cloud->hasNormals())
{
ccConsole::Warning(QString("Cloud '%1' has no normals!").arg(cloud->getName()));
continue;
}
//orient normals with Fast Marching
if (cloud->orientNormalsWithFM(level, &pDlg))
{
cloud->prepareDisplayForRefresh();
}
else
{
++errors;
}
}
if (errors)
{
ccConsole::Error(QString("Process failed (check console)"));
}
else
{
ccLog::Warning("Normals have been oriented: you may still have to globally invert the cloud normals however (Edit > Normals > Invert).");
}
return true;
}
bool orientNormalsMST(const ccHObject::Container &selectedEntities, QWidget *parent)
{
if (selectedEntities.empty())
{
ccConsole::Error("Select at least one point cloud");
return false;
}
bool ok = false;
static unsigned s_defaultKNN = 6;
unsigned kNN = static_cast<unsigned>(QInputDialog::getInt( parent,
"Neighborhood size", "Neighbors",
s_defaultKNN ,
1, 1000,
1,
&ok));
if (!ok)
return false;
s_defaultKNN = kNN;
ccProgressDialog pDlg(true, parent);
pDlg.setAutoClose(false);
size_t errors = 0;
for (ccHObject* entity : selectedEntities)
{
if (!entity->isA(CC_TYPES::POINT_CLOUD))
continue;
ccPointCloud* cloud = static_cast<ccPointCloud*>(entity);
if (!cloud->hasNormals())
{
ccConsole::Warning(QString("Cloud '%1' has no normals!").arg(cloud->getName()));
continue;
}
//use Minimum Spanning Tree to resolve normals direction
if (cloud->orientNormalsWithMST(kNN, &pDlg))
{
cloud->prepareDisplayForRefresh();
}
else
{
ccConsole::Warning(QString("Process failed on cloud '%1'").arg(cloud->getName()));
++errors;
}
}
if (errors)
{
ccConsole::Error(QString("Process failed (check console)"));
}
else
{
ccLog::Warning("Normals have been oriented: you may still have to globally invert the cloud normals however (Edit > Normals > Invert).");
}
return true;
}
bool convertNormalsTo(const ccHObject::Container &selectedEntities, NORMAL_CONVERSION_DEST dest)
{
unsigned errorCount = 0;
size_t selNum = selectedEntities.size();
for (size_t i = 0; i < selNum; ++i)
{
ccHObject* ent = selectedEntities[i];
bool lockedVertices = false;
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent, &lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selNum == 1);
continue;
}
if (cloud && cloud->isA(CC_TYPES::POINT_CLOUD)) // TODO
{
ccPointCloud* ccCloud = static_cast<ccPointCloud*>(cloud);
if (ccCloud->hasNormals())
{
bool success = true;
switch(dest)
{
case NORMAL_CONVERSION_DEST::HSV_COLORS:
{
success = ccCloud->convertNormalToRGB();
if (success)
{
ccCloud->showSF(false);
ccCloud->showNormals(false);
ccCloud->showColors(true);
}
}
break;
case NORMAL_CONVERSION_DEST::DIP_DIR_SFS:
{
//get/create 'dip' scalar field
int dipSFIndex = ccCloud->getScalarFieldIndexByName(CC_DEFAULT_DIP_SF_NAME);
if (dipSFIndex < 0)
dipSFIndex = ccCloud->addScalarField(CC_DEFAULT_DIP_SF_NAME);
if (dipSFIndex < 0)
{
ccLog::Warning("[ccEntityAction::convertNormalsTo] Not enough memory!");
success = false;
break;
}
//get/create 'dip direction' scalar field
int dipDirSFIndex = ccCloud->getScalarFieldIndexByName(CC_DEFAULT_DIP_DIR_SF_NAME);
if (dipDirSFIndex < 0)
dipDirSFIndex = ccCloud->addScalarField(CC_DEFAULT_DIP_DIR_SF_NAME);
if (dipDirSFIndex < 0)
{
ccCloud->deleteScalarField(dipSFIndex);
ccLog::Warning("[ccEntityAction::convertNormalsTo] Not enough memory!");
success = false;
break;
}
ccScalarField* dipSF = static_cast<ccScalarField*>(ccCloud->getScalarField(dipSFIndex));
ccScalarField* dipDirSF = static_cast<ccScalarField*>(ccCloud->getScalarField(dipDirSFIndex));
Q_ASSERT(dipSF && dipDirSF);
success = ccCloud->convertNormalToDipDirSFs(dipSF, dipDirSF);
if (success)
{
//apply default 360 degrees color scale!
ccColorScale::Shared dipScale = ccColorScalesManager::GetDefaultScale(ccColorScalesManager::DIP_BRYW);
ccColorScale::Shared dipDirScale = ccColorScalesManager::GetDefaultScale(ccColorScalesManager::DIP_DIR_REPEAT);
dipSF->setColorScale(dipScale);
dipDirSF->setColorScale(dipDirScale);
ccCloud->setCurrentDisplayedScalarField(dipDirSFIndex); //dip dir. seems more interesting by default
ccCloud->showSF(true);
}
else
{
ccCloud->deleteScalarField(dipSFIndex);
ccCloud->deleteScalarField(dipDirSFIndex);
}
}
break;
default:
Q_ASSERT(false);
ccLog::Warning("[ccEntityAction::convertNormalsTo] Internal error: unhandled destination!");
success = false;
i = selNum; //no need to process the selected entities anymore!
break;
}
if (success)
{
ccCloud->prepareDisplayForRefresh_recursive();
}
else
{
++errorCount;
}
}
}
}
//errors should have been sent to console as warnings
if (errorCount)
{
ccConsole::Error("Error(s) occurred! (see console)");
}
return true;
}
//////////
// Octree
bool computeOctree(const ccHObject::Container &selectedEntities, QWidget *parent)
{
ccBBox bbox;
std::unordered_set<ccGenericPointCloud*> clouds;
PointCoordinateType maxBoxSize = -1;
for (ccHObject* ent : selectedEntities)
{
//specific test for locked vertices
bool lockedVertices = false;
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent, &lockedVertices);
if (cloud == nullptr)
{
continue;
}
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
clouds.insert(cloud);
//we look for the biggest box so as to define the "minimum cell size"
const ccBBox thisBBox = cloud->getOwnBB();
if (thisBBox.isValid())
{
CCVector3 dd = thisBBox.maxCorner() - thisBBox.minCorner();
PointCoordinateType maxd = std::max(dd.x, std::max(dd.y, dd.z));
if (maxBoxSize < 0.0 || maxd > maxBoxSize)
maxBoxSize = maxd;
}
bbox += thisBBox;
}
if (clouds.empty() || maxBoxSize < 0.0)
{
ccLog::Warning("[doActionComputeOctree] No eligible entities in selection!");
return false;
}
//min(cellSize) = max(dim)/2^N with N = max subidivision level
const double minCellSize = static_cast<double>(maxBoxSize) / (1 << ccOctree::MAX_OCTREE_LEVEL);
ccComputeOctreeDlg coDlg(bbox, minCellSize, parent);
if (!coDlg.exec())
return false;
ccProgressDialog pDlg(true, parent);
pDlg.setAutoClose(false);
//if we must use a custom bounding box, we update 'bbox'
if (coDlg.getMode() == ccComputeOctreeDlg::CUSTOM_BBOX)
bbox = coDlg.getCustomBBox();
for (const auto cloud : clouds)
{
//we temporarily detach entity, as it may undergo
//"severe" modifications (octree deletion, etc.) --> see ccPointCloud::computeOctree
MainWindow* instance = dynamic_cast<MainWindow*>(parent);
MainWindow::ccHObjectContext objContext;
if (instance)
objContext = instance->removeObjectTemporarilyFromDBTree(cloud);
//computation
QElapsedTimer eTimer;
eTimer.start();
ccOctree::Shared octree(nullptr);
switch (coDlg.getMode())
{
case ccComputeOctreeDlg::DEFAULT:
octree = cloud->computeOctree(&pDlg);
break;
case ccComputeOctreeDlg::MIN_CELL_SIZE:
case ccComputeOctreeDlg::CUSTOM_BBOX:
{
//for a cell-size based custom box, we must update it for each cloud!
if (coDlg.getMode() == ccComputeOctreeDlg::MIN_CELL_SIZE)
{
double cellSize = coDlg.getMinCellSize();
PointCoordinateType halfBoxWidth = static_cast<PointCoordinateType>(cellSize * (1 << ccOctree::MAX_OCTREE_LEVEL) / 2.0);
CCVector3 C = cloud->getOwnBB().getCenter();
bbox = ccBBox( C - CCVector3(halfBoxWidth, halfBoxWidth, halfBoxWidth),
C + CCVector3(halfBoxWidth, halfBoxWidth, halfBoxWidth));
}
cloud->deleteOctree();
octree = ccOctree::Shared(new ccOctree(cloud));
if (octree->build(bbox.minCorner(), bbox.maxCorner(), nullptr, nullptr, &pDlg) > 0)
{
ccOctreeProxy* proxy = new ccOctreeProxy(octree);
proxy->setDisplay(cloud->getDisplay());
cloud->addChild(proxy);
}
else
{
octree.clear();
}
}
break;
default:
Q_ASSERT(false);
return false;
}
qint64 elapsedTime_ms = eTimer.elapsed();
//put object back in tree
if (instance)
instance->putObjectBackIntoDBTree(cloud, objContext);
if (octree)
{
ccConsole::Print("[doActionComputeOctree] Timing: %2.3f s", static_cast<double>(elapsedTime_ms) / 1000.0);
cloud->setEnabled(true); //for mesh vertices!
ccOctreeProxy* proxy = cloud->getOctreeProxy();
assert(proxy);
proxy->setVisible(true);
proxy->prepareDisplayForRefresh();
}
else
{
ccConsole::Warning(QString("Octree computation on cloud '%1' failed!").arg(cloud->getName()));
}
}
return true;
}
//////////
// Properties
bool clearProperty(ccHObject::Container selectedEntities, CLEAR_PROPERTY property, QWidget *parent)
{
for (ccHObject* ent : selectedEntities)
{
//specific case: clear normals on a mesh
if (property == CLEAR_PROPERTY::NORMALS && ( ent->isA(CC_TYPES::MESH) /*|| ent->isKindOf(CC_TYPES::PRIMITIVE)*/ )) //TODO
{
ccMesh* mesh = ccHObjectCaster::ToMesh(ent);
if (!mesh)
{
assert(false);
continue;
}
if (mesh->hasTriNormals())
{
mesh->showNormals(false);
MainWindow* instance = dynamic_cast<MainWindow*>(parent);
MainWindow::ccHObjectContext objContext;
if (instance)
objContext = instance->removeObjectTemporarilyFromDBTree(mesh);
mesh->clearTriNormals();
if (instance)
instance->putObjectBackIntoDBTree(mesh,objContext);
ent->prepareDisplayForRefresh();
continue;
}
else if (mesh->hasNormals()) //per-vertex normals?
{
if (mesh->getParent()
&& (mesh->getParent()->isA(CC_TYPES::MESH)/*|| mesh->getParent()->isKindOf(CC_TYPES::PRIMITIVE)*/) //TODO
&& ccHObjectCaster::ToMesh(mesh->getParent())->getAssociatedCloud() == mesh->getAssociatedCloud())
{
ccLog::Warning("[doActionClearNormals] Can't remove per-vertex normals on a sub mesh!");
}
else //mesh is alone, we can freely remove normals
{
if (mesh->getAssociatedCloud() && mesh->getAssociatedCloud()->isA(CC_TYPES::POINT_CLOUD))
{
mesh->showNormals(false);
static_cast<ccPointCloud*>(mesh->getAssociatedCloud())->unallocateNorms();
mesh->prepareDisplayForRefresh();
continue;
}
}
}
}
bool lockedVertices;
ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(ent,&lockedVertices);
if (lockedVertices)
{
ccUtils::DisplayLockedVerticesWarning(ent->getName(), selectedEntities.size() == 1);
continue;
}
if (cloud && cloud->isA(CC_TYPES::POINT_CLOUD)) // TODO
{
auto pointCloud = static_cast<ccPointCloud*>(cloud);
switch (property)
{
case CLEAR_PROPERTY::COLORS:
if (cloud->hasColors())
{
pointCloud->unallocateColors();
ent->prepareDisplayForRefresh();
}
break;
case CLEAR_PROPERTY::NORMALS:
if (cloud->hasNormals())
{
pointCloud->unallocateNorms();
ent->prepareDisplayForRefresh();
}
break;
case CLEAR_PROPERTY::CURRENT_SCALAR_FIELD:
if (cloud->hasDisplayedScalarField())
{
pointCloud->deleteScalarField( pointCloud->getCurrentDisplayedScalarFieldIndex() );
ent->prepareDisplayForRefresh();
}
break;
case CLEAR_PROPERTY::ALL_SCALAR_FIELDS:
if (cloud->hasScalarFields())
{
pointCloud->deleteAllScalarFields();
ent->prepareDisplayForRefresh();
}
break;
}
}
}
return true;
}
bool toggleProperty(const ccHObject::Container &selectedEntities, TOGGLE_PROPERTY property)
{
ccHObject baseEntities;
ConvertToGroup(selectedEntities, baseEntities, ccHObject::DP_NONE);
for (unsigned i=0; i<baseEntities.getChildrenNumber(); ++i)
{
ccHObject* child = baseEntities.getChild(i);
switch(property)
{
case TOGGLE_PROPERTY::ACTIVE:
child->toggleActivation/*_recursive*/();
break;
case TOGGLE_PROPERTY::VISIBLE:
child->toggleVisibility_recursive();
break;
case TOGGLE_PROPERTY::COLOR:
child->toggleColors_recursive();
break;
case TOGGLE_PROPERTY::NORMALS:
child->toggleNormals_recursive();
break;
case TOGGLE_PROPERTY::SCALAR_FIELD:
child->toggleSF_recursive();
break;
case TOGGLE_PROPERTY::MATERIAL:
child->toggleMaterials_recursive();
break;
case TOGGLE_PROPERTY::NAME:
child->toggleShowName_recursive();
break;
default:
Q_ASSERT(false);
return false;
}
child->prepareDisplayForRefresh_recursive();
}
return true;
}
//////////
// Stats
bool statisticalTest(const ccHObject::Container &selectedEntities, QWidget *parent)
{
ccPickOneElementDlg poeDlg("Distribution","Choose distribution",parent);
poeDlg.addElement("Gauss");
poeDlg.addElement("Weibull");
poeDlg.setDefaultIndex(0);
if (!poeDlg.exec())
{
return false;
}
int distribIndex = poeDlg.getSelectedIndex();
ccStatisticalTestDlg* sDlg = nullptr;
switch (distribIndex)
{
case 0: //Gauss
sDlg = new ccStatisticalTestDlg("mu","sigma",QString(),"Local Statistical Test (Gauss)",parent);
break;
case 1: //Weibull
sDlg = new ccStatisticalTestDlg("a","b","shift","Local Statistical Test (Weibull)",parent);
break;
default:
ccConsole::Error("Invalid distribution!");
return false;
}
if (!sDlg->exec())
{
sDlg->deleteLater();
return false;
}
//build up corresponding distribution
CCLib::GenericDistribution* distrib = nullptr;
{
ScalarType a = static_cast<ScalarType>(sDlg->getParam1());
ScalarType b = static_cast<ScalarType>(sDlg->getParam2());
ScalarType c = static_cast<ScalarType>(sDlg->getParam3());
switch (distribIndex)
{
case 0: //Gauss
{
CCLib::NormalDistribution* N = new CCLib::NormalDistribution();
N->setParameters(a,b*b); //warning: we input sigma2 here (not sigma)
distrib = static_cast<CCLib::GenericDistribution*>(N);
break;
}
case 1: //Weibull
CCLib::WeibullDistribution* W = new CCLib::WeibullDistribution();
W->setParameters(a,b,c);
distrib = static_cast<CCLib::GenericDistribution*>(W);
break;
}
}
const double pChi2 = sDlg->getProba();
const int nn = sDlg->getNeighborsNumber();
ccProgressDialog pDlg(true, parent);
pDlg.setAutoClose(false);
for (ccHObject* ent : selectedEntities)
{
ccPointCloud* pc = ccHObjectCaster::ToPointCloud(ent);
if (pc == nullptr)
{
// TODO handle error?
continue;
}
//we apply method on currently displayed SF
ccScalarField* inSF = pc->getCurrentDisplayedScalarField();
if (inSF == nullptr)
{
// TODO handle error?
continue;
}
Q_ASSERT(inSF->capacity() != 0);
//force SF as 'OUT' field (in case of)
const int outSfIdx = pc->getCurrentDisplayedScalarFieldIndex();
pc->setCurrentOutScalarField(outSfIdx);
//force Chi2 Distances field as 'IN' field (create it by the way if necessary)
int chi2SfIdx = pc->getScalarFieldIndexByName(CC_CHI2_DISTANCES_DEFAULT_SF_NAME);
if (chi2SfIdx < 0)
chi2SfIdx = pc->addScalarField(CC_CHI2_DISTANCES_DEFAULT_SF_NAME);
if (chi2SfIdx < 0)
{
ccConsole::Error("Couldn't allocate a new scalar field for computing chi2 distances! Try to free some memory ...");
break;
}
pc->setCurrentInScalarField(chi2SfIdx);
//compute octree if necessary
ccOctree::Shared theOctree = pc->getOctree();
if (!theOctree)
{
theOctree = pc->computeOctree(&pDlg);
if (!theOctree)
{
ccConsole::Error(QString("Couldn't compute octree for cloud '%1'!").arg(pc->getName()));
break;
}
}
QElapsedTimer eTimer;
eTimer.start();
double chi2dist = CCLib::StatisticalTestingTools::testCloudWithStatisticalModel(distrib, pc, nn, pChi2, &pDlg, theOctree.data());
ccConsole::Print("[Chi2 Test] Timing: %3.2f ms.", eTimer.elapsed() / 1000.0);
ccConsole::Print("[Chi2 Test] %s test result = %f", distrib->getName(), chi2dist);
//we set the theoretical Chi2 distance limit as the minimum displayed SF value so that all points below are grayed
{
ccScalarField* chi2SF = static_cast<ccScalarField*>(pc->getCurrentInScalarField());
Q_ASSERT(chi2SF);
chi2SF->computeMinAndMax();
chi2dist *= chi2dist;
chi2SF->setMinDisplayed(static_cast<ScalarType>(chi2dist));
chi2SF->setSymmetricalScale(false);
chi2SF->setSaturationStart(static_cast<ScalarType>(chi2dist));
//chi2SF->setSaturationStop(chi2dist);
pc->setCurrentDisplayedScalarField(chi2SfIdx);
pc->showSF(true);
pc->prepareDisplayForRefresh_recursive();
}
}
delete distrib;
distrib = nullptr;
sDlg->deleteLater();
return true;
}
bool computeStatParams(const ccHObject::Container &selectedEntities, QWidget *parent)
{
ccPickOneElementDlg pDlg("Distribution", "Distribution Fitting", parent);
pDlg.addElement("Gauss");
pDlg.addElement("Weibull");
pDlg.setDefaultIndex(0);
if (!pDlg.exec())
return false;
CCLib::GenericDistribution* distrib = nullptr;
{
switch (pDlg.getSelectedIndex())
{
case 0: //GAUSS
distrib = new CCLib::NormalDistribution();
break;
case 1: //WEIBULL
distrib = new CCLib::WeibullDistribution();
break;
default:
Q_ASSERT(false);
return false;
}
}
Q_ASSERT(distrib != nullptr);
for (ccHObject* ent : selectedEntities)
{
ccPointCloud* pc = ccHObjectCaster::ToPointCloud(ent);
if (pc == nullptr)
{
// TODO report error?
continue;
}
//we apply method on currently displayed SF
ccScalarField* sf = pc->getCurrentDisplayedScalarField();
if (sf == nullptr)
{
// TODO report error?
continue;
}
Q_ASSERT(!sf->empty());
if (sf && distrib->computeParameters(*sf))
{
QString description;
const unsigned precision = ccGui::Parameters().displayedNumPrecision;
switch (pDlg.getSelectedIndex())
{
case 0: //GAUSS
{
CCLib::NormalDistribution* normal = static_cast<CCLib::NormalDistribution*>(distrib);
description = QString("mean = %1 / std.dev. = %2").arg(normal->getMu(), 0, 'f', precision).arg(sqrt(normal->getSigma2()), 0, 'f', precision);
}
break;
case 1: //WEIBULL
{
CCLib::WeibullDistribution* weibull = static_cast<CCLib::WeibullDistribution*>(distrib);
ScalarType a, b;
weibull->getParameters(a, b);
description = QString("a = %1 / b = %2 / shift = %3").arg(a, 0, 'f', precision).arg(b, 0, 'f', precision).arg(weibull->getValueShift(), 0, 'f', precision);
ccLog::Print(QString("[Distribution fitting] Additional Weibull distrib. parameters: mode = %1 / skewness = %2").arg(weibull->computeMode()).arg(weibull->computeSkewness()));
}
break;
default:
{
Q_ASSERT(false);
return false;
}
}
description.prepend(QString("%1: ").arg(distrib->getName()));
ccConsole::Print(QString("[Distribution fitting] %1").arg(description));
const unsigned numberOfClasses = static_cast<unsigned>(ceil(sqrt(static_cast<double>(pc->size()))));
std::vector<unsigned> histo;
std::vector<double> npis;
try
{
histo.resize(numberOfClasses, 0);
npis.resize(numberOfClasses, 0.0);
}
catch (const std::bad_alloc&)
{
ccConsole::Warning("[Distribution fitting] Not enough memory!");
continue;
}
//compute the Chi2 distance
{
unsigned finalNumberOfClasses = 0;
const double chi2dist = CCLib::StatisticalTestingTools::computeAdaptativeChi2Dist(distrib, pc, 0, finalNumberOfClasses, false, nullptr, nullptr, histo.data(), npis.data());
if (chi2dist >= 0.0)
{
ccConsole::Print("[Distribution fitting] %s: Chi2 Distance = %f", distrib->getName(), chi2dist);
}
else
{
ccConsole::Warning("[Distribution fitting] Failed to compute Chi2 distance?!");
continue;
}
}
//compute RMS
{
unsigned n = pc->size();
double squareSum = 0;
unsigned counter = 0;
for (unsigned i = 0; i < n; ++i)
{
ScalarType v = pc->getPointScalarValue(i);
if (CCLib::ScalarField::ValidValue(v))
{
squareSum += static_cast<double>(v) * v;
++counter;
}
}
if (counter != 0)
{
double rms = sqrt(squareSum / counter);
ccConsole::Print(QString("Scalar field RMS = %1").arg(rms));
}
}
//show histogram
ccHistogramWindowDlg* hDlg = new ccHistogramWindowDlg(parent);
hDlg->setWindowTitle("[Distribution fitting]");
ccHistogramWindow* histogram = hDlg->window();
histogram->fromBinArray(histo, sf->getMin(), sf->getMax());
histo.clear();
histogram->setCurveValues(npis);
npis.clear();
histogram->setTitle(description);
histogram->setColorScheme(ccHistogramWindow::USE_CUSTOM_COLOR_SCALE);
histogram->setColorScale(sf->getColorScale());
histogram->setAxisLabels(sf->getName(), "Count");
histogram->refresh();
hDlg->show();
}
else
{
ccConsole::Warning(QString("[Entity: %1]-[SF: %2] Couldn't compute distribution parameters!").arg(pc->getName(), sf->getName()));
}
}
delete distrib;
distrib = nullptr;
return true;
}
}
|