File size: 74,210 Bytes
985c397 | 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 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2005 Imetric 3D GmbH *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <algorithm>
#include <limits>
#include <Base/Console.h>
#include <Base/Sequencer.h>
#include "Algorithm.h"
#include "Approximation.h"
#include "Elements.h"
#include "Grid.h"
#include "Iterator.h"
#include "Triangulation.h"
using namespace MeshCore;
using Base::BoundBox2d;
using Base::BoundBox3f;
using Base::Polygon2d;
bool MeshAlgorithm::IsVertexVisible(
const Base::Vector3f& rcVertex,
const Base::Vector3f& rcView,
const MeshFacetGrid& rclGrid
) const
{
const float fMaxDistance = 0.001F;
Base::Vector3f cDirection = rcVertex - rcView;
float fDistance = cDirection.Length();
Base::Vector3f cIntsct;
FacetIndex uInd {};
// search for the nearest facet to rcView in direction to rcVertex
if (NearestFacetOnRay(rcView, cDirection, /*1.2f*fDistance,*/ rclGrid, cIntsct, uInd)) {
// now check if the facet overlays the point
float fLen = Base::Distance(rcView, cIntsct);
if (fLen < fDistance) {
// is it the same point?
if (Base::Distance(rcVertex, cIntsct) > fMaxDistance) {
// ok facet overlays the vertex
return false;
}
}
}
return true; // no facet between the two points
}
bool MeshAlgorithm::NearestFacetOnRay(
const Base::Vector3f& rclPt,
const Base::Vector3f& rclDir,
Base::Vector3f& rclRes,
FacetIndex& rulFacet
) const
{
return NearestFacetOnRay(rclPt, rclDir, Mathf::PI, rclRes, rulFacet);
}
bool MeshAlgorithm::NearestFacetOnRay(
const Base::Vector3f& rclPt,
const Base::Vector3f& rclDir,
float fMaxAngle,
Base::Vector3f& rclRes,
FacetIndex& rulFacet
) const
{
Base::Vector3f clProj;
Base::Vector3f clRes;
bool bSol = false;
FacetIndex ulInd = 0;
// slow execution with no grid
MeshFacetIterator clFIter(_rclMesh);
for (clFIter.Init(); clFIter.More(); clFIter.Next()) {
if (clFIter->Foraminate(rclPt, rclDir, clRes, fMaxAngle)) {
if (!bSol) {
// first solution
bSol = true;
clProj = clRes;
ulInd = clFIter.Position();
}
else {
// is closer to the point
if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) {
clProj = clRes;
ulInd = clFIter.Position();
}
}
}
}
if (bSol) {
rclRes = clProj;
rulFacet = ulInd;
}
return bSol;
}
bool MeshAlgorithm::NearestFacetOnRay(
const Base::Vector3f& rclPt,
const Base::Vector3f& rclDir,
const MeshFacetGrid& rclGrid,
Base::Vector3f& rclRes,
FacetIndex& rulFacet
) const
{
std::vector<FacetIndex> aulFacets;
MeshGridIterator clGridIter(rclGrid);
if (clGridIter.InitOnRay(rclPt, rclDir, aulFacets)) {
if (!RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet)) {
aulFacets.clear();
while (clGridIter.NextOnRay(aulFacets)) {
if (RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet)) {
return true;
}
}
}
else {
return true;
}
}
return false;
}
bool MeshAlgorithm::NearestFacetOnRay(
const Base::Vector3f& rclPt,
const Base::Vector3f& rclDir,
float fMaxSearchArea,
const MeshFacetGrid& rclGrid,
Base::Vector3f& rclRes,
FacetIndex& rulFacet
) const
{
const float fMaxAngle = 1.75F;
std::vector<FacetIndex> aulFacets;
MeshGridIterator clGridIter(rclGrid);
if (clGridIter.InitOnRay(rclPt, rclDir, fMaxSearchArea, aulFacets)) {
if (!RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet, fMaxAngle)) {
aulFacets.clear();
while (clGridIter.NextOnRay(aulFacets)) {
if (RayNearestField(rclPt, rclDir, aulFacets, rclRes, rulFacet, fMaxAngle)) {
return true;
}
}
}
else {
return true;
}
}
return false;
}
bool MeshAlgorithm::NearestFacetOnRay(
const Base::Vector3f& rclPt,
const Base::Vector3f& rclDir,
const std::vector<FacetIndex>& raulFacets,
Base::Vector3f& rclRes,
FacetIndex& rulFacet
) const
{
Base::Vector3f clProj;
Base::Vector3f clRes;
bool bSol = false;
FacetIndex ulInd = 0;
for (FacetIndex index : raulFacets) {
MeshGeomFacet rclSFacet = _rclMesh.GetFacet(index);
if (rclSFacet.Foraminate(rclPt, rclDir, clRes)) {
if (!bSol) { // first solution
bSol = true;
clProj = clRes;
ulInd = index;
}
else { // is closer to the point
if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) {
clProj = clRes;
ulInd = index;
}
}
}
}
if (bSol) {
rclRes = clProj;
rulFacet = ulInd;
}
return bSol;
}
bool MeshAlgorithm::RayNearestField(
const Base::Vector3f& rclPt,
const Base::Vector3f& rclDir,
const std::vector<FacetIndex>& raulFacets,
Base::Vector3f& rclRes,
FacetIndex& rulFacet,
float /*fMaxAngle*/
) const
{
Base::Vector3f clProj, clRes;
bool bSol = false;
FacetIndex ulInd = 0;
for (FacetIndex index : raulFacets) {
if (_rclMesh.GetFacet(index).Foraminate(rclPt, rclDir, clRes /*, fMaxAngle*/)) {
if (!bSol) { // first solution
bSol = true;
clProj = clRes;
ulInd = index;
}
else { // is closer to the point
if ((clRes - rclPt).Length() < (clProj - rclPt).Length()) {
clProj = clRes;
ulInd = index;
}
}
}
}
if (bSol) {
rclRes = clProj;
rulFacet = ulInd;
}
return bSol;
}
bool MeshAlgorithm::FirstFacetToVertex(
const Base::Vector3f& rPt,
float fMaxDistance,
const MeshFacetGrid& rGrid,
FacetIndex& uIndex
) const
{
const float fEps = 0.001F;
bool found = false;
std::vector<FacetIndex> facets;
// get the facets of the grid the point lies into
rGrid.GetElements(rPt, facets);
// Check all facets inside the grid if the point is part of it
for (FacetIndex facet : facets) {
MeshGeomFacet cFacet = this->_rclMesh.GetFacet(facet);
if (cFacet.IsPointOfFace(rPt, fMaxDistance)) {
found = true;
uIndex = facet;
break;
}
// if not then check the distance to the border of the triangle
Base::Vector3f res;
float fDist {};
unsigned short uSide {};
cFacet.ProjectPointToPlane(rPt, res);
cFacet.NearestEdgeToPoint(res, fDist, uSide);
if (fDist < fEps) {
found = true;
uIndex = facet;
break;
}
}
return found;
}
float MeshAlgorithm::GetAverageEdgeLength() const
{
float fLen = 0.0F;
MeshFacetIterator cF(_rclMesh);
for (cF.Init(); cF.More(); cF.Next()) {
for (int i = 0; i < 3; i++) {
fLen += Base::Distance(cF->_aclPoints[i], cF->_aclPoints[(i + 1) % 3]);
}
}
fLen = fLen / (3.0F * _rclMesh.CountFacets());
return fLen;
}
float MeshAlgorithm::GetMinimumEdgeLength() const
{
float fLen = std::numeric_limits<float>::max();
MeshFacetIterator cF(_rclMesh);
for (cF.Init(); cF.More(); cF.Next()) {
for (int i = 0; i < 3; i++) {
fLen = std::min(fLen, Base::Distance(cF->_aclPoints[i], cF->_aclPoints[(i + 1) % 3]));
}
}
return fLen;
}
float MeshAlgorithm::GetMaximumEdgeLength() const
{
float fLen = 0.0F;
MeshFacetIterator cF(_rclMesh);
for (cF.Init(); cF.More(); cF.Next()) {
for (int i = 0; i < 3; i++) {
fLen = std::max(fLen, Base::Distance(cF->_aclPoints[i], cF->_aclPoints[(i + 1) % 3]));
}
}
return fLen;
}
Base::Vector3f MeshAlgorithm::GetGravityPoint() const
{
Base::Vector3f center;
MeshPointIterator cP(_rclMesh);
for (cP.Init(); cP.More(); cP.Next()) {
center += *cP;
}
return center / static_cast<float>(_rclMesh.CountPoints());
}
void MeshAlgorithm::GetMeshBorders(std::list<std::vector<Base::Vector3f>>& rclBorders) const
{
std::vector<FacetIndex> aulAllFacets(_rclMesh.CountFacets());
FacetIndex k = 0;
for (FacetIndex& index : aulAllFacets) {
index = k++;
}
GetFacetBorders(aulAllFacets, rclBorders);
}
void MeshAlgorithm::GetMeshBorders(std::list<std::vector<PointIndex>>& rclBorders) const
{
std::vector<FacetIndex> aulAllFacets(_rclMesh.CountFacets());
FacetIndex k = 0;
for (FacetIndex& index : aulAllFacets) {
index = k++;
}
GetFacetBorders(aulAllFacets, rclBorders, true);
}
void MeshAlgorithm::GetFacetBorders(
const std::vector<FacetIndex>& raulInd,
std::list<std::vector<Base::Vector3f>>& rclBorders
) const
{
const MeshPointArray& rclPAry = _rclMesh._aclPointArray;
std::list<std::vector<PointIndex>> aulBorders;
GetFacetBorders(raulInd, aulBorders, true);
for (const auto& border : aulBorders) {
std::vector<Base::Vector3f> boundary;
boundary.reserve(border.size());
for (PointIndex jt : border) {
boundary.push_back(rclPAry[jt]);
}
rclBorders.push_back(boundary);
}
}
void MeshAlgorithm::GetFacetBorders(
const std::vector<FacetIndex>& raulInd,
std::list<std::vector<PointIndex>>& rclBorders,
bool ignoreOrientation
) const
{
const MeshFacetArray& rclFAry = _rclMesh._aclFacetArray;
// mark all facets that are in the indices list
ResetFacetFlag(MeshFacet::VISIT);
for (FacetIndex it : raulInd) {
rclFAry[it].SetFlag(MeshFacet::VISIT);
}
// collect all boundary edges (unsorted)
std::list<std::pair<PointIndex, PointIndex>> aclEdges;
for (FacetIndex it : raulInd) {
const MeshFacet& rclFacet = rclFAry[it];
for (unsigned short i = 0; i < 3; i++) {
FacetIndex ulNB = rclFacet._aulNeighbours[i];
if (ulNB != FACET_INDEX_MAX) {
if (rclFAry[ulNB].IsFlag(MeshFacet::VISIT)) {
continue;
}
}
aclEdges.push_back(rclFacet.GetEdge(i));
}
}
if (aclEdges.empty()) {
return; // no borders found (=> solid)
}
// search for edges in the unsorted list
PointIndex ulFirst {}, ulLast {};
std::list<PointIndex> clBorder;
ulFirst = aclEdges.begin()->first;
ulLast = aclEdges.begin()->second;
aclEdges.erase(aclEdges.begin());
clBorder.push_back(ulFirst);
clBorder.push_back(ulLast);
while (!aclEdges.empty()) {
// get adjacent edge
std::list<std::pair<PointIndex, PointIndex>>::iterator pEI;
for (pEI = aclEdges.begin(); pEI != aclEdges.end(); ++pEI) {
if (pEI->first == ulLast) {
ulLast = pEI->second;
clBorder.push_back(ulLast);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
if (pEI->second == ulFirst) {
ulFirst = pEI->first;
clBorder.push_front(ulFirst);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
// Note: Using this might result into boundaries with wrong orientation.
// But if the mesh has some facets with wrong orientation we might get
// broken boundary curves.
if (pEI->second == ulLast && ignoreOrientation) {
ulLast = pEI->first;
clBorder.push_back(ulLast);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
if (pEI->first == ulFirst && ignoreOrientation) {
ulFirst = pEI->second;
clBorder.push_front(ulFirst);
aclEdges.erase(pEI);
pEI = aclEdges.begin();
break;
}
}
// Note: Calling erase on list iterators doesn't force a re-allocation and
// thus doesn't invalidate the iterator itself, only the referenced object
if ((pEI == aclEdges.end()) || aclEdges.empty() || (ulLast == ulFirst)) {
// no further edge found or closed polyline, respectively
rclBorders.emplace_back(clBorder.begin(), clBorder.end());
clBorder.clear();
if (!aclEdges.empty()) {
// start new boundary
ulFirst = aclEdges.begin()->first;
ulLast = aclEdges.begin()->second;
aclEdges.erase(aclEdges.begin());
clBorder.push_back(ulFirst);
clBorder.push_back(ulLast);
}
}
}
}
void MeshAlgorithm::GetFacetBorder(FacetIndex uFacet, std::list<PointIndex>& rBorder) const
{
const MeshFacetArray& rFAry = _rclMesh._aclFacetArray;
std::list<std::pair<PointIndex, PointIndex>> openEdges;
if (uFacet >= rFAry.size()) {
return;
}
// add the open edge to the beginning of the list
auto face = rFAry.begin() + uFacet;
for (unsigned short i = 0; i < 3; i++) {
if (face->_aulNeighbours[i] == FACET_INDEX_MAX) {
openEdges.push_back(face->GetEdge(i));
}
}
if (openEdges.empty()) {
return; // facet is not a border facet
}
for (auto it = rFAry.begin(); it != rFAry.end(); ++it) {
if (it == face) {
continue;
}
for (unsigned short i = 0; i < 3; i++) {
if (it->_aulNeighbours[i] == FACET_INDEX_MAX) {
openEdges.push_back(it->GetEdge(i));
}
}
}
SplitBoundaryFromOpenEdges(openEdges, rBorder);
}
void MeshAlgorithm::GetFacetsBorders(
const std::vector<FacetIndex>& uFacets,
std::list<std::vector<PointIndex>>& rBorders
) const
{
ResetFacetFlag(MeshFacet::TMP0);
SetFacetsFlag(uFacets, MeshFacet::TMP0);
ResetPointFlag(MeshPoint::TMP0);
const MeshFacetArray& rFAry = _rclMesh._aclFacetArray;
const MeshPointArray& rPAry = _rclMesh._aclPointArray;
std::list<std::pair<PointIndex, PointIndex>> openEdges;
// add the open edge to the beginning of the list
for (auto it : uFacets) {
const MeshFacet& face = rFAry[it];
for (int i = 0; i < 3; i++) {
if (face._aulNeighbours[i] == FACET_INDEX_MAX) {
std::pair<PointIndex, PointIndex> openEdge = face.GetEdge(i);
openEdges.push_back(openEdge);
// mark all points of open edges of the given facets
rPAry[openEdge.first].SetFlag(MeshPoint::TMP0);
rPAry[openEdge.second].SetFlag(MeshPoint::TMP0);
}
}
}
if (openEdges.empty()) {
return; // none of the facets are border facets
}
for (const auto& it : rFAry) {
if (it.IsFlag(MeshFacet::TMP0)) {
continue;
}
for (int i = 0; i < 3; i++) {
if (it._aulNeighbours[i] == FACET_INDEX_MAX) {
openEdges.push_back(it.GetEdge(i));
}
}
}
// if the first element is not an edge of "uFacets" then give up
while (!openEdges.empty()) {
PointIndex first = openEdges.begin()->first;
PointIndex second = openEdges.begin()->second;
if (!rPAry[first].IsFlag(MeshPoint::TMP0)) {
break;
}
if (!rPAry[second].IsFlag(MeshPoint::TMP0)) {
break;
}
std::list<PointIndex> boundary;
SplitBoundaryFromOpenEdges(openEdges, boundary);
rBorders.emplace_back(boundary.begin(), boundary.end());
}
}
void MeshAlgorithm::SplitBoundaryFromOpenEdges(
std::list<std::pair<PointIndex, PointIndex>>& openEdges,
std::list<PointIndex>& boundary
) const
{
// Start with the edge that is associated to uFacet
if (openEdges.empty()) {
return;
}
PointIndex ulFirst = openEdges.begin()->first;
PointIndex ulLast = openEdges.begin()->second;
openEdges.erase(openEdges.begin());
boundary.push_back(ulFirst);
boundary.push_back(ulLast);
while (ulLast != ulFirst) {
// find adjacent edge
std::list<std::pair<PointIndex, PointIndex>>::iterator pEI;
for (pEI = openEdges.begin(); pEI != openEdges.end(); ++pEI) {
if (pEI->first == ulLast) {
ulLast = pEI->second;
boundary.push_back(ulLast);
openEdges.erase(pEI);
pEI = openEdges.begin();
break;
}
if (pEI->second == ulFirst) {
ulFirst = pEI->first;
boundary.push_front(ulFirst);
openEdges.erase(pEI);
pEI = openEdges.begin();
break;
}
}
// cannot close the border
if (pEI == openEdges.end()) {
break;
}
}
}
void MeshAlgorithm::SplitBoundaryLoops(std::list<std::vector<PointIndex>>& aBorders)
{
// Count the number of open edges for each point
std::map<PointIndex, int> openPointDegree;
for (const auto& jt : _rclMesh._aclFacetArray) {
for (int i = 0; i < 3; i++) {
if (jt._aulNeighbours[i] == FACET_INDEX_MAX) {
openPointDegree[jt._aulPoints[i]]++;
openPointDegree[jt._aulPoints[(i + 1) % 3]]++;
}
}
}
// go through all boundaries and split them if needed
std::list<std::vector<PointIndex>> aSplitBorders;
for (const auto& aBorder : aBorders) {
bool split = false;
for (auto jt : aBorder) {
// two (or more) boundaries meet in one non-manifold point
if (openPointDegree[jt] > 2) {
split = true;
break;
}
}
if (!split) {
aSplitBorders.push_back(aBorder);
}
else {
SplitBoundaryLoops(aBorder, aSplitBorders);
}
}
aBorders = aSplitBorders;
}
void MeshAlgorithm::SplitBoundaryLoops(
const std::vector<PointIndex>& rBound,
std::list<std::vector<PointIndex>>& aBorders
)
{
std::map<PointIndex, int> aPtDegree;
std::vector<PointIndex> cBound;
for (PointIndex it : rBound) {
int deg = (aPtDegree[it]++);
if (deg > 0) {
for (auto jt = cBound.begin(); jt != cBound.end(); ++jt) {
if (*jt == it) {
std::vector<PointIndex> cBoundLoop;
cBoundLoop.insert(cBoundLoop.end(), jt, cBound.end());
cBoundLoop.push_back(it);
cBound.erase(jt, cBound.end());
aBorders.push_back(cBoundLoop);
(aPtDegree[it]--);
break;
}
}
}
cBound.push_back(it);
}
}
bool MeshAlgorithm::FillupHole(
const std::vector<PointIndex>& boundary,
AbstractPolygonTriangulator& cTria,
MeshFacetArray& rFaces,
MeshPointArray& rPoints,
int level,
const MeshRefPointToFacets* pP2FStructure
) const
{
if (boundary.front() == boundary.back()) {
// first and last vertex are identical
if (boundary.size() < 4) {
return false; // something strange
}
}
else if (boundary.size() < 3) {
return false; // something strange
}
// Get a facet as reference coordinate system
MeshGeomFacet rTriangle;
MeshFacet rFace;
PointIndex refPoint0 = *(boundary.begin());
PointIndex refPoint1 = *(boundary.begin() + 1);
if (pP2FStructure) {
const std::set<FacetIndex>& ring1 = (*pP2FStructure)[refPoint0];
const std::set<FacetIndex>& ring2 = (*pP2FStructure)[refPoint1];
std::vector<FacetIndex> f_int;
std::set_intersection(
ring1.begin(),
ring1.end(),
ring2.begin(),
ring2.end(),
std::back_insert_iterator<std::vector<FacetIndex>>(f_int)
);
if (f_int.size() != 1) {
return false; // error, this must be an open edge!
}
rFace = _rclMesh._aclFacetArray[f_int.front()];
rTriangle = _rclMesh.GetFacet(rFace);
}
else {
bool ready = false;
for (auto it = _rclMesh._aclFacetArray.begin(); it != _rclMesh._aclFacetArray.end(); ++it) {
for (int i = 0; i < 3; i++) {
if (((it->_aulPoints[i] == refPoint0) && (it->_aulPoints[(i + 1) % 3] == refPoint1))
|| ((it->_aulPoints[i] == refPoint1)
&& (it->_aulPoints[(i + 1) % 3] == refPoint0))) {
rFace = *it;
rTriangle = _rclMesh.GetFacet(*it);
ready = true;
break;
}
}
if (ready) {
break;
}
}
}
// add points to the polygon
std::vector<Base::Vector3f> polygon;
for (PointIndex jt : boundary) {
polygon.push_back(_rclMesh._aclPointArray[jt]);
rPoints.push_back(_rclMesh._aclPointArray[jt]);
}
// remove the last added point if it is duplicated
std::vector<PointIndex> bounds = boundary;
if (boundary.front() == boundary.back()) {
bounds.pop_back();
polygon.pop_back();
rPoints.pop_back();
}
// There is no easy way to check whether the boundary is interior (a hole) or exterior before
// performing the triangulation. Afterwards we can compare the normals of the created triangles
// with the z-direction of our local coordinate system. If the scalar product is positive it was
// a hole, otherwise not.
cTria.SetPolygon(polygon);
cTria.SetIndices(bounds);
std::vector<Base::Vector3f> surf_pts = cTria.GetPolygon();
if (pP2FStructure && level > 0) {
std::set<PointIndex> index = pP2FStructure->NeighbourPoints(boundary, level);
for (PointIndex it : index) {
Base::Vector3f pt(_rclMesh._aclPointArray[it]);
surf_pts.push_back(pt);
}
}
if (cTria.TriangulatePolygon()) {
// if we have enough points then we fit a surface through the points and project
// the added points onto this surface
cTria.PostProcessing(surf_pts);
// get the facets and add the additional points to the array
rFaces.insert(rFaces.end(), cTria.GetFacets().begin(), cTria.GetFacets().end());
std::vector<Base::Vector3f> newVertices = cTria.AddedPoints();
for (const auto& vertex : newVertices) {
rPoints.push_back(vertex);
}
// Unfortunately, some algorithms do not care about the orientation of the polygon so we
// cannot rely on the normal criterion to decide whether it's a hole or not.
//
std::vector<MeshFacet> faces = cTria.GetFacets();
// Special case handling for a hole with three edges: the resulting facet might be
// coincident with the reference facet
if (faces.size() == 1) {
MeshFacet first = faces.front();
if (cTria.NeedsReindexing()) {
first._aulPoints[0] = boundary[first._aulPoints[0]];
first._aulPoints[1] = boundary[first._aulPoints[1]];
first._aulPoints[2] = boundary[first._aulPoints[2]];
}
if (first.IsEqual(rFace)) {
rFaces.clear();
rPoints.clear();
cTria.Discard();
return false;
}
}
constexpr auto max = std::numeric_limits<unsigned short>::max();
// Get the new neighbour to our reference facet
MeshFacet facet;
unsigned short ref_side = rFace.Side(refPoint0, refPoint1);
unsigned short tri_side = max;
if (cTria.NeedsReindexing()) {
// the referenced indices of the polyline
refPoint0 = 0;
refPoint1 = 1;
}
if (ref_side < max) {
for (const auto& face : faces) {
tri_side = face.Side(refPoint0, refPoint1);
if (tri_side < max) {
facet = face;
break;
}
}
}
// in case the reference facet has not an open edge print a log message
if (ref_side == max || tri_side == max) {
Base::Console().log(
"MeshAlgorithm::FillupHole: Expected open edge for facet <%d, %d, %d>\n",
rFace._aulPoints[0],
rFace._aulPoints[1],
rFace._aulPoints[2]
);
rFaces.clear();
rPoints.clear();
cTria.Discard();
return false;
}
#if 1
MeshGeomFacet triangle;
triangle = cTria.GetTriangle(rPoints, facet);
TriangulationVerifier* verifier = cTria.GetVerifier();
if (!verifier) {
return true;
}
// Now we have two adjacent triangles which we check for overlaps.
// Therefore we build a separation plane that must separate the two diametrically opposed
// points.
Base::Vector3f planeNormal = rTriangle.GetNormal()
% (rTriangle._aclPoints[(ref_side + 1) % 3] - rTriangle._aclPoints[ref_side]);
Base::Vector3f planeBase = rTriangle._aclPoints[ref_side % 3];
Base::Vector3f ref_point = rTriangle._aclPoints[(ref_side + 2) % 3];
Base::Vector3f tri_point = triangle._aclPoints[(tri_side + 2) % 3];
if (!verifier->Accept(planeNormal, planeBase, ref_point, tri_point)) {
rFaces.clear();
rPoints.clear();
cTria.Discard();
return false;
}
// we know to have filled a polygon, now check for the orientation
if (verifier->MustFlip(triangle.GetNormal(), rTriangle.GetNormal())) {
for (auto& rFace : rFaces) {
rFace.FlipNormal();
}
}
#endif
return true;
}
return false;
}
void MeshAlgorithm::SetFacetsProperty(
const std::vector<FacetIndex>& raulInds,
const std::vector<unsigned long>& raulProps
) const
{
if (raulInds.size() != raulProps.size()) {
return;
}
auto iP = raulProps.begin();
for (auto i = raulInds.begin(); i != raulInds.end(); ++i, ++iP) {
_rclMesh._aclFacetArray[*i].SetProperty(*iP);
}
}
void MeshAlgorithm::SetFacetsFlag(const std::vector<FacetIndex>& raulInds, MeshFacet::TFlagType tF) const
{
for (FacetIndex it : raulInds) {
_rclMesh._aclFacetArray[it].SetFlag(tF);
}
}
void MeshAlgorithm::SetPointsFlag(const std::vector<FacetIndex>& raulInds, MeshPoint::TFlagType tF) const
{
for (PointIndex it : raulInds) {
_rclMesh._aclPointArray[it].SetFlag(tF);
}
}
void MeshAlgorithm::GetFacetsFlag(std::vector<FacetIndex>& raulInds, MeshFacet::TFlagType tF) const
{
raulInds.reserve(raulInds.size() + CountFacetFlag(tF));
MeshFacetArray::_TConstIterator beg = _rclMesh._aclFacetArray.begin();
MeshFacetArray::_TConstIterator end = _rclMesh._aclFacetArray.end();
for (auto it = beg; it != end; ++it) {
if (it->IsFlag(tF)) {
raulInds.push_back(it - beg);
}
}
}
void MeshAlgorithm::GetPointsFlag(std::vector<PointIndex>& raulInds, MeshPoint::TFlagType tF) const
{
raulInds.reserve(raulInds.size() + CountPointFlag(tF));
MeshPointArray::_TConstIterator beg = _rclMesh._aclPointArray.begin();
MeshPointArray::_TConstIterator end = _rclMesh._aclPointArray.end();
for (auto it = beg; it != end; ++it) {
if (it->IsFlag(tF)) {
raulInds.push_back(it - beg);
}
}
}
void MeshAlgorithm::ResetFacetsFlag(const std::vector<FacetIndex>& raulInds, MeshFacet::TFlagType tF) const
{
for (FacetIndex it : raulInds) {
_rclMesh._aclFacetArray[it].ResetFlag(tF);
}
}
void MeshAlgorithm::ResetPointsFlag(const std::vector<FacetIndex>& raulInds, MeshPoint::TFlagType tF) const
{
for (PointIndex it : raulInds) {
_rclMesh._aclPointArray[it].ResetFlag(tF);
}
}
void MeshAlgorithm::SetFacetFlag(MeshFacet::TFlagType tF) const
{
_rclMesh._aclFacetArray.SetFlag(tF);
}
void MeshAlgorithm::SetPointFlag(MeshPoint::TFlagType tF) const
{
_rclMesh._aclPointArray.SetFlag(tF);
}
void MeshAlgorithm::ResetFacetFlag(MeshFacet::TFlagType tF) const
{
_rclMesh._aclFacetArray.ResetFlag(tF);
}
void MeshAlgorithm::ResetPointFlag(MeshPoint::TFlagType tF) const
{
_rclMesh._aclPointArray.ResetFlag(tF);
}
unsigned long MeshAlgorithm::CountFacetFlag(MeshFacet::TFlagType tF) const
{
MeshIsFlag<MeshFacet> flag;
return std::count_if(
_rclMesh._aclFacetArray.begin(),
_rclMesh._aclFacetArray.end(),
[flag, tF](const MeshFacet& f) { return flag(f, tF); }
);
}
unsigned long MeshAlgorithm::CountPointFlag(MeshPoint::TFlagType tF) const
{
MeshIsFlag<MeshPoint> flag;
return std::count_if(
_rclMesh._aclPointArray.begin(),
_rclMesh._aclPointArray.end(),
[flag, tF](const MeshPoint& f) { return flag(f, tF); }
);
}
void MeshAlgorithm::GetFacetsFromToolMesh(
const MeshKernel& rToolMesh,
const Base::Vector3f& rcDir,
std::vector<FacetIndex>& raclCutted
) const
{
MeshFacetIterator cFIt(_rclMesh);
MeshFacetIterator cTIt(rToolMesh);
BoundBox3f cBB = rToolMesh.GetBoundBox();
Base::SequencerLauncher seq("Check facets...", _rclMesh.CountFacets());
// check all facets
Base::Vector3f tmp;
for (cFIt.Init(); cFIt.More(); cFIt.Next()) {
// check each point of each facet
for (const auto& pnt : cFIt->_aclPoints) {
// at least the point must be inside the bounding box of the tool mesh
if (cBB.IsInBox(pnt)) {
// should not cause performance problems since the tool mesh is usually rather
// lightweight
int ct = 0;
for (cTIt.Init(); cTIt.More(); cTIt.Next()) {
if (cTIt->IsPointOfFace(pnt, MeshPoint::epsilon())) {
ct = 1;
break; // the point lies on the tool mesh
}
if (cTIt->Foraminate(pnt, rcDir, tmp)) {
// check if the intersection point lies in direction rcDir of the considered
// point
if ((tmp - pnt) * rcDir > 0) {
ct++;
}
}
}
// odd number => point is inside the tool mesh
if (ct % 2 == 1) {
raclCutted.push_back(cFIt.Position());
break;
}
}
}
seq.next();
}
}
void MeshAlgorithm::GetFacetsFromToolMesh(
const MeshKernel& rToolMesh,
const Base::Vector3f& rcDir,
const MeshFacetGrid& rGrid,
std::vector<FacetIndex>& raclCutted
) const
{
// iterator over grid structure
MeshGridIterator clGridIter(rGrid);
BoundBox3f cBB = rToolMesh.GetBoundBox();
Base::Vector3f tmp;
MeshFacetIterator cFIt(_rclMesh);
MeshFacetIterator cTIt(rToolMesh);
MeshAlgorithm cToolAlg(rToolMesh);
// To speed up the algorithm we use the grid built up from the associated mesh. For each grid
// element we check whether it lies completely inside or outside the toolmesh or even intersects
// with the toolmesh. So we can reduce the number of facets with further tests dramatically.
// If the grid box is outside the toolmesh all the facets inside can be skipped. If the grid
// box is inside the toolmesh all facets are stored with no further tests because they must
// also lie inside the toolmesh. Finally, if the grid box intersects with the toolmesh we must
// also check for each whether it intersects with the toolmesh as well.
std::vector<FacetIndex> aulInds;
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
int ret = cToolAlg.Surround(clGridIter.GetBoundBox(), rcDir);
// the box is completely inside the toolmesh
if (ret == 1) {
// these facets can be removed without more checks
clGridIter.GetElements(raclCutted);
}
// the box intersects with toolmesh
else if (ret == 0) {
// these facets must be tested for intersections with the toolmesh
clGridIter.GetElements(aulInds);
}
// the box is outside the toolmesh but this could still mean that the triangles
// inside the grid intersect with the toolmesh
else if (ret == -1) {
// these facets must be tested for intersections with the toolmesh
clGridIter.GetElements(aulInds);
}
}
// remove duplicates
std::sort(aulInds.begin(), aulInds.end());
aulInds.erase(std::unique(aulInds.begin(), aulInds.end()), aulInds.end());
std::sort(raclCutted.begin(), raclCutted.end());
raclCutted.erase(std::unique(raclCutted.begin(), raclCutted.end()), raclCutted.end());
Base::SequencerLauncher seq("Check facets...", aulInds.size());
// check all facets
for (FacetIndex it : aulInds) {
cFIt.Set(it);
// check each point of each facet
for (auto point : cFIt->_aclPoints) {
// at least the point must be inside the bounding box of the tool mesh
if (cBB.IsInBox(point)) {
// should not cause performance problems since the tool mesh is usually rather
// lightweight
int ct = 0;
for (cTIt.Init(); cTIt.More(); cTIt.Next()) {
if (cTIt->IsPointOfFace(point, MeshPoint::epsilon())) {
ct = 1;
break; // the point lies on the tool mesh
}
if (cTIt->Foraminate(point, rcDir, tmp)) {
// check if the intersection point lies in direction rcDir of the considered
// point
if ((tmp - point) * rcDir > 0) {
ct++;
}
}
}
// odd number => point is inside the tool mesh
if (ct % 2 == 1) {
raclCutted.push_back(cFIt.Position());
break;
}
}
}
seq.next();
}
// remove duplicates
std::sort(raclCutted.begin(), raclCutted.end());
raclCutted.erase(std::unique(raclCutted.begin(), raclCutted.end()), raclCutted.end());
}
int MeshAlgorithm::Surround(const Base::BoundBox3f& rBox, const Base::Vector3f& rcDir)
{
Base::Vector3f pt1, pt2, tmp;
const BoundBox3f& cBB = _rclMesh.GetBoundBox();
// at least both boxes intersect
if (cBB && rBox) {
// check for intersections with the actual mesh
Base::Vector3f cCorner[8] = {
Base::Vector3f(rBox.MinX, rBox.MinY, rBox.MinZ),
Base::Vector3f(rBox.MaxX, rBox.MinY, rBox.MinZ),
Base::Vector3f(rBox.MaxX, rBox.MaxY, rBox.MinZ),
Base::Vector3f(rBox.MinX, rBox.MaxY, rBox.MinZ),
Base::Vector3f(rBox.MinX, rBox.MinY, rBox.MaxZ),
Base::Vector3f(rBox.MaxX, rBox.MinY, rBox.MaxZ),
Base::Vector3f(rBox.MaxX, rBox.MaxY, rBox.MaxZ),
Base::Vector3f(rBox.MinX, rBox.MaxY, rBox.MaxZ)
};
MeshFacetIterator cTIt(_rclMesh);
// triangulation of the box
int triangles[36] = {0, 1, 2, 0, 2, 3, 0, 1, 5, 0, 5, 4, 0, 4, 7, 0, 7, 3,
6, 7, 4, 6, 4, 5, 6, 2, 3, 6, 3, 7, 6, 1, 2, 6, 5, 1};
std::vector<MeshGeomFacet> cFacet(12);
int id = 0;
for (size_t ii = 0; ii < 12; ii++) {
cFacet[ii]._aclPoints[0] = cCorner[triangles[id++]];
cFacet[ii]._aclPoints[1] = cCorner[triangles[id++]];
cFacet[ii]._aclPoints[2] = cCorner[triangles[id++]];
}
// check for intersections of the box with the mesh
for (const auto& it : cFacet) {
for (cTIt.Init(); cTIt.More(); cTIt.Next()) {
int ret = cTIt->IntersectWithFacet(it, pt1, pt2);
// the box intersects the mesh?
if (ret != 0) {
return 0; // => no more investigations required
}
}
}
// Now we know that the box doesn't intersect with the mesh. This means that either the box
// is completely inside or outside the mesh. To check this we test one point of the box
// whether it is inside or outside.
int ct = 0;
for (cTIt.Init(); cTIt.More(); cTIt.Next()) {
if (cTIt->IsPointOfFace(cCorner[0], MeshPoint::epsilon())) {
ct = 1;
break; // the point lies on the tool mesh
}
if (cTIt->Foraminate(cCorner[0], rcDir, tmp)) {
// check if the intersection point lies in direction rcDir of the considered point
if ((tmp - cCorner[0]) * rcDir > 0) {
ct++;
}
}
}
// odd number => point (i.e. the box) is inside the mesh, even number => point is outside
// the mesh
return (ct % 2 == 1) ? 1 : -1;
}
// no intersection the box is outside the mesh
return -1;
}
void MeshAlgorithm::CheckFacets(
const MeshFacetGrid& rclGrid,
const Base::ViewProjMethod* pclProj,
const Base::Polygon2d& rclPoly,
bool bInner,
std::vector<FacetIndex>& raulFacets
) const
{
std::vector<FacetIndex>::iterator it;
MeshFacetIterator clIter(_rclMesh, 0);
Base::Vector3f clPt2d;
Base::Vector3f clGravityOfFacet;
bool bNoPointInside {};
// Cache current view projection matrix since calls to Coin's projection are expensive
Base::ViewProjMatrix fixedProj(pclProj->getComposedProjectionMatrix());
// Precompute the polygon's bounding box
Base::BoundBox2d clPolyBBox = rclPoly.CalcBoundBox();
// if true use grid on mesh to speed up search
if (bInner) {
BoundBox3f clBBox3d;
BoundBox2d clViewBBox;
std::vector<FacetIndex> aulAllElements;
// iterator for the bounding box grids
MeshGridIterator clGridIter(rclGrid);
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
clBBox3d = clGridIter.GetBoundBox();
clViewBBox = clBBox3d.ProjectBox(&fixedProj);
if (clViewBBox.Intersect(clPolyBBox)) {
// collect all elements in aulAllElements
clGridIter.GetElements(aulAllElements);
}
}
// remove duplicates
std::sort(aulAllElements.begin(), aulAllElements.end());
aulAllElements.erase(
std::unique(aulAllElements.begin(), aulAllElements.end()),
aulAllElements.end()
);
Base::SequencerLauncher seq("Check facets", aulAllElements.size());
for (it = aulAllElements.begin(); it != aulAllElements.end(); ++it) {
bNoPointInside = true;
clGravityOfFacet.Set(0.0F, 0.0F, 0.0F);
MeshGeomFacet rclFacet = _rclMesh.GetFacet(*it);
for (const auto& pnt : rclFacet._aclPoints) {
clPt2d = fixedProj(pnt);
clGravityOfFacet += clPt2d;
if (clPolyBBox.Contains(Base::Vector2d(clPt2d.x, clPt2d.y))
&& rclPoly.Contains(Base::Vector2d(clPt2d.x, clPt2d.y))) {
raulFacets.push_back(*it);
bNoPointInside = false;
break;
}
}
// if no facet point is inside the polygon then check also the gravity
if (bNoPointInside) {
clGravityOfFacet *= 1.0F / 3.0F;
if (clPolyBBox.Contains(Base::Vector2d(clGravityOfFacet.x, clGravityOfFacet.y))
&& rclPoly.Contains(Base::Vector2d(clGravityOfFacet.x, clGravityOfFacet.y))) {
raulFacets.push_back(*it);
}
}
seq.next();
}
}
// When cutting triangles outside then go through all elements
else {
Base::SequencerLauncher seq("Check facets", _rclMesh.CountFacets());
for (clIter.Init(); clIter.More(); clIter.Next()) {
for (const auto& pnt : clIter->_aclPoints) {
clPt2d = fixedProj(pnt);
if ((clPolyBBox.Contains(Base::Vector2d(clPt2d.x, clPt2d.y))
&& !rclPoly.Contains(Base::Vector2d(clPt2d.x, clPt2d.y)))) {
raulFacets.push_back(clIter.Position());
break;
}
}
seq.next();
}
}
}
void MeshAlgorithm::CheckFacets(
const Base::ViewProjMethod* pclProj,
const Base::Polygon2d& rclPoly,
bool bInner,
std::vector<FacetIndex>& raulFacets
) const
{
const MeshPointArray& p = _rclMesh.GetPoints();
const MeshFacetArray& f = _rclMesh.GetFacets();
Base::Vector3f pt2d;
// Use a bounding box to reduce number of call to Polygon::Contains
Base::BoundBox2d bb = rclPoly.CalcBoundBox();
// Precompute the screen projection matrix as Coin's projection function is expensive
Base::ViewProjMatrix fixedProj(pclProj->getComposedProjectionMatrix());
FacetIndex index = 0;
for (auto it = f.begin(); it != f.end(); ++it, ++index) {
for (PointIndex ptIndex : it->_aulPoints) {
pt2d = fixedProj(p[ptIndex]);
// First check whether the point is in the bounding box of the polygon
if ((bb.Contains(Base::Vector2d(pt2d.x, pt2d.y))
&& rclPoly.Contains(Base::Vector2d(pt2d.x, pt2d.y)))
^ !bInner) {
raulFacets.push_back(index);
break;
}
}
}
}
float MeshAlgorithm::Surface() const
{
float fTotal = 0.0F;
MeshFacetIterator clFIter(_rclMesh);
for (clFIter.Init(); clFIter.More(); clFIter.Next()) {
fTotal += clFIter->Area();
}
return fTotal;
}
void MeshAlgorithm::SubSampleByDist(float fDist, std::vector<Base::Vector3f>& rclPoints) const
{
rclPoints.clear();
MeshFacetIterator clFIter(_rclMesh);
for (clFIter.Init(); clFIter.More(); clFIter.Next()) {
size_t k = rclPoints.size();
clFIter->SubSample(fDist, rclPoints);
if (rclPoints.size() == k) {
rclPoints.push_back(clFIter->GetGravityPoint()); // min. add middle point
}
}
}
void MeshAlgorithm::SubSampleAllPoints(std::vector<Base::Vector3f>& rclPoints) const
{
rclPoints.clear();
// Add all Points
//
MeshPointIterator clPIter(_rclMesh);
for (clPIter.Init(); clPIter.More(); clPIter.Next()) {
rclPoints.push_back(*clPIter);
}
}
void MeshAlgorithm::SubSampleByCount(unsigned long ulCtPoints, std::vector<Base::Vector3f>& rclPoints) const
{
float fDist = float(std::sqrt(Surface() / float(ulCtPoints)));
SubSampleByDist(fDist, rclPoints);
}
void MeshAlgorithm::SearchFacetsFromPolyline(
const std::vector<Base::Vector3f>& rclPolyline,
float fRadius,
const MeshFacetGrid& rclGrid,
std::vector<FacetIndex>& rclResultFacetsIndices
) const
{
rclResultFacetsIndices.clear();
if (rclPolyline.size() < 3) {
return; // no polygon defined
}
std::set<FacetIndex> aclFacets;
for (auto pV = rclPolyline.begin(); pV < (rclPolyline.end() - 1); ++pV) {
const Base::Vector3f &rclP0 = *pV, &rclP1 = *(pV + 1);
// BB eines Polyline-Segments
BoundBox3f clSegmBB(rclP0.x, rclP0.y, rclP0.z, rclP0.x, rclP0.y, rclP0.z);
clSegmBB.Add(rclP1);
clSegmBB.Enlarge(fRadius); // BB um Suchradius vergroessern
std::vector<FacetIndex> aclBBFacets;
unsigned long k = rclGrid.Inside(clSegmBB, aclBBFacets, false);
for (unsigned long i = 0; i < k; i++) {
if (_rclMesh.GetFacet(aclBBFacets[i]).DistanceToLineSegment(rclP0, rclP1) < fRadius) {
aclFacets.insert(aclBBFacets[i]);
}
}
}
rclResultFacetsIndices.insert(rclResultFacetsIndices.begin(), aclFacets.begin(), aclFacets.end());
}
void MeshAlgorithm::CutBorderFacets(std::vector<FacetIndex>& raclFacetIndices, unsigned short usLevel) const
{
std::vector<FacetIndex> aclToDelete;
CheckBorderFacets(raclFacetIndices, aclToDelete, usLevel);
// alle gefunden "Rand"-Facetsindizes" aus dem Array loeschen
std::vector<FacetIndex> aclResult;
std::set<FacetIndex> aclTmp(aclToDelete.begin(), aclToDelete.end());
for (FacetIndex facetIndex : raclFacetIndices) {
if (aclTmp.find(facetIndex) == aclTmp.end()) {
aclResult.push_back(facetIndex);
}
}
raclFacetIndices = aclResult;
}
unsigned long MeshAlgorithm::CountBorderEdges() const
{
unsigned long cnt = 0;
const MeshFacetArray& rclFAry = _rclMesh._aclFacetArray;
auto end = rclFAry.end();
for (auto it = rclFAry.begin(); it != end; ++it) {
for (FacetIndex facetIndex : it->_aulNeighbours) {
if (facetIndex == FACET_INDEX_MAX) {
cnt++;
}
}
}
return cnt;
}
void MeshAlgorithm::CheckBorderFacets(
const std::vector<FacetIndex>& raclFacetIndices,
std::vector<FacetIndex>& raclResultIndices,
unsigned short usLevel
) const
{
ResetFacetFlag(MeshFacet::TMP0);
SetFacetsFlag(raclFacetIndices, MeshFacet::TMP0);
const MeshFacetArray& rclFAry = _rclMesh._aclFacetArray;
for (unsigned short usL = 0; usL < usLevel; usL++) {
for (FacetIndex facetIndex : raclFacetIndices) {
for (FacetIndex ulNB : rclFAry[facetIndex]._aulNeighbours) {
if (ulNB == FACET_INDEX_MAX) {
raclResultIndices.push_back(facetIndex);
rclFAry[facetIndex].ResetFlag(MeshFacet::TMP0);
continue;
}
if (!rclFAry[ulNB].IsFlag(MeshFacet::TMP0)) {
raclResultIndices.push_back(facetIndex);
rclFAry[facetIndex].ResetFlag(MeshFacet::TMP0);
continue;
}
}
}
}
}
void MeshAlgorithm::GetBorderPoints(
const std::vector<FacetIndex>& raclFacetIndices,
std::set<PointIndex>& raclResultPointsIndices
) const
{
ResetFacetFlag(MeshFacet::TMP0);
SetFacetsFlag(raclFacetIndices, MeshFacet::TMP0);
const MeshFacetArray& rclFAry = _rclMesh._aclFacetArray;
for (FacetIndex facetIndex : raclFacetIndices) {
for (int i = 0; i < 3; i++) {
const MeshFacet& rclFacet = rclFAry[facetIndex];
FacetIndex ulNB = rclFacet._aulNeighbours[i];
if (ulNB == FACET_INDEX_MAX) {
raclResultPointsIndices.insert(rclFacet._aulPoints[i]);
raclResultPointsIndices.insert(rclFacet._aulPoints[(i + 1) % 3]);
continue;
}
if (!rclFAry[ulNB].IsFlag(MeshFacet::TMP0)) {
raclResultPointsIndices.insert(rclFacet._aulPoints[i]);
raclResultPointsIndices.insert(rclFacet._aulPoints[(i + 1) % 3]);
continue;
}
}
}
}
bool MeshAlgorithm::NearestPointFromPoint(
const Base::Vector3f& rclPt,
FacetIndex& rclResFacetIndex,
Base::Vector3f& rclResPoint
) const
{
if (_rclMesh.CountFacets() == 0) {
return false;
}
// calc each facet
float fMinDist = std::numeric_limits<float>::max();
FacetIndex ulInd = FACET_INDEX_MAX;
MeshFacetIterator pF(_rclMesh);
for (pF.Init(); pF.More(); pF.Next()) {
float fDist = pF->DistanceToPoint(rclPt);
if (fDist < fMinDist) {
fMinDist = fDist;
ulInd = pF.Position();
}
}
MeshGeomFacet rclSFacet = _rclMesh.GetFacet(ulInd);
rclSFacet.DistanceToPoint(rclPt, rclResPoint);
rclResFacetIndex = ulInd;
return true;
}
bool MeshAlgorithm::NearestPointFromPoint(
const Base::Vector3f& rclPt,
const MeshFacetGrid& rclGrid,
FacetIndex& rclResFacetIndex,
Base::Vector3f& rclResPoint
) const
{
FacetIndex ulInd = rclGrid.SearchNearestFromPoint(rclPt);
if (ulInd == FACET_INDEX_MAX) {
return false;
}
MeshGeomFacet rclSFacet = _rclMesh.GetFacet(ulInd);
rclSFacet.DistanceToPoint(rclPt, rclResPoint);
rclResFacetIndex = ulInd;
return true;
}
bool MeshAlgorithm::NearestPointFromPoint(
const Base::Vector3f& rclPt,
const MeshFacetGrid& rclGrid,
float fMaxSearchArea,
FacetIndex& rclResFacetIndex,
Base::Vector3f& rclResPoint
) const
{
FacetIndex ulInd = rclGrid.SearchNearestFromPoint(rclPt, fMaxSearchArea);
if (ulInd == FACET_INDEX_MAX) {
return false; // no facets inside BoundingBox
}
MeshGeomFacet rclSFacet = _rclMesh.GetFacet(ulInd);
rclSFacet.DistanceToPoint(rclPt, rclResPoint);
rclResFacetIndex = ulInd;
return true;
}
bool MeshAlgorithm::CutWithPlane(
const Base::Vector3f& clBase,
const Base::Vector3f& clNormal,
const MeshFacetGrid& rclGrid,
std::list<std::vector<Base::Vector3f>>& rclResult,
float fMinEps,
bool bConnectPolygons
) const
{
std::vector<FacetIndex> aulFacets;
// Search grid
MeshGridIterator clGridIter(rclGrid);
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
// if Gridvoxel intersects the plane: pick up all facets for cutting
if (clGridIter.GetBoundBox().IsCutPlane(clBase, clNormal)) {
clGridIter.GetElements(aulFacets);
}
}
// remove multiple triangles
std::sort(aulFacets.begin(), aulFacets.end());
aulFacets.erase(std::unique(aulFacets.begin(), aulFacets.end()), aulFacets.end());
// intersect all facets with plane
std::list<std::pair<Base::Vector3f, Base::Vector3f>> clTempPoly; // Field with intersection lines
// (unsorted, not chained)
for (FacetIndex facetIndex : aulFacets) {
Base::Vector3f clE1, clE2;
const MeshGeomFacet clF(_rclMesh.GetFacet(facetIndex));
// Cut the facet and store the cutting path
if (clF.IntersectWithPlane(clBase, clNormal, clE1, clE2)) {
clTempPoly.emplace_back(clE1, clE2);
}
}
if (bConnectPolygons) {
// std::list<std::pair<Base::Vector3f, Base::Vector3f> > rclTempLines;
std::list<std::pair<Base::Vector3f, Base::Vector3f>> rclResultLines(
clTempPoly.begin(),
clTempPoly.end()
);
std::list<std::vector<Base::Vector3f>> tempList;
ConnectLines(clTempPoly, tempList, fMinEps);
ConnectPolygons(tempList, clTempPoly);
for (auto& iter : clTempPoly) {
rclResultLines.push_front(iter);
}
return ConnectLines(rclResultLines, rclResult, fMinEps);
}
return ConnectLines(clTempPoly, rclResult, fMinEps);
}
bool MeshAlgorithm::ConnectLines(
std::list<std::pair<Base::Vector3f, Base::Vector3f>>& rclLines,
std::list<std::vector<Base::Vector3f>>& rclPolylines,
float fMinEps
) const
{
using TCIter = std::list<std::pair<Base::Vector3f, Base::Vector3f>>::iterator;
// square search radius
// const float fMinEps = 1.0e-2f; // := 10 micrometer distance
fMinEps = fMinEps * fMinEps;
// remove all lines whose distance is smaller than epsilon
std::list<TCIter> _clToDelete;
float fToDelDist = fMinEps / 10.0F;
for (TCIter pF = rclLines.begin(); pF != rclLines.end(); ++pF) {
if (Base::DistanceP2(pF->first, pF->second) < fToDelDist) {
_clToDelete.push_back(pF);
}
}
for (auto& pI : _clToDelete) {
rclLines.erase(pI);
}
while (!rclLines.empty()) {
TCIter pF;
// new polyline
std::list<Base::Vector3f> clPoly;
// add first line and delete from the list
Base::Vector3f clFront = rclLines.begin()->first; // current start point of the polyline
Base::Vector3f clEnd = rclLines.begin()->second; // current end point of the polyline
clPoly.push_back(clFront);
clPoly.push_back(clEnd);
rclLines.erase(rclLines.begin());
// search for the next line on the begin/end of the polyline and add it
TCIter pFront, pEnd;
bool bFoundLine {};
do {
float fFrontMin = fMinEps, fEndMin = fMinEps;
bool bFrontFirst = false, bEndFirst = false;
pFront = rclLines.end();
pEnd = rclLines.end();
bFoundLine = false;
for (pF = rclLines.begin(); pF != rclLines.end(); ++pF) {
if (Base::DistanceP2(clFront, pF->first) < fFrontMin) {
fFrontMin = Base::DistanceP2(clFront, pF->first);
pFront = pF;
bFrontFirst = true;
}
else if (Base::DistanceP2(clEnd, pF->first) < fEndMin) {
fEndMin = Base::DistanceP2(clEnd, pF->first);
pEnd = pF;
bEndFirst = true;
}
else if (Base::DistanceP2(clFront, pF->second) < fFrontMin) {
fFrontMin = Base::DistanceP2(clFront, pF->second);
pFront = pF;
bFrontFirst = false;
}
else if (Base::DistanceP2(clEnd, pF->second) < fEndMin) {
fEndMin = Base::DistanceP2(clEnd, pF->second);
pEnd = pF;
bEndFirst = false;
}
}
if (pFront != rclLines.end()) {
bFoundLine = true;
if (bFrontFirst) {
clPoly.push_front(pFront->second);
clFront = pFront->second;
}
else {
clPoly.push_front(pFront->first);
clFront = pFront->first;
}
rclLines.erase(pFront);
}
if (pEnd != rclLines.end()) {
bFoundLine = true;
if (bEndFirst) {
clPoly.push_back(pEnd->second);
clEnd = pEnd->second;
}
else {
clPoly.push_back(pEnd->first);
clEnd = pEnd->first;
}
rclLines.erase(pEnd);
}
} while (bFoundLine);
rclPolylines.emplace_back(clPoly.begin(), clPoly.end());
}
// remove all polylines with too few length
using TPIter = std::list<std::vector<Base::Vector3f>>::iterator;
std::list<TPIter> _clPolyToDelete;
for (TPIter pJ = rclPolylines.begin(); pJ != rclPolylines.end(); ++pJ) {
if (pJ->size() == 2) { // only one line segment
if (Base::DistanceP2(*pJ->begin(), *(pJ->begin() + 1)) <= fMinEps) {
_clPolyToDelete.push_back(pJ);
}
}
}
for (auto& pK : _clPolyToDelete) {
rclPolylines.erase(pK);
}
return true;
}
bool MeshAlgorithm::ConnectPolygons(
std::list<std::vector<Base::Vector3f>>& clPolyList,
std::list<std::pair<Base::Vector3f, Base::Vector3f>>& rclLines
) const
{
for (auto OutIter = clPolyList.begin(); OutIter != clPolyList.end(); ++OutIter) {
if (OutIter->empty()) {
continue;
}
std::pair<Base::Vector3f, Base::Vector3f> currentSort;
float fDist = Base::Distance(OutIter->front(), OutIter->back());
currentSort.first = OutIter->front();
currentSort.second = OutIter->back();
for (auto InnerIter = clPolyList.begin(); InnerIter != clPolyList.end(); ++InnerIter) {
if (OutIter == InnerIter) {
continue;
}
if (Base::Distance(OutIter->front(), InnerIter->front()) < fDist) {
currentSort.second = InnerIter->front();
fDist = Base::Distance(OutIter->front(), InnerIter->front());
}
if (Base::Distance(OutIter->front(), InnerIter->back()) < fDist) {
currentSort.second = InnerIter->back();
fDist = Base::Distance(OutIter->front(), InnerIter->back());
}
}
rclLines.push_front(currentSort);
}
return true;
}
void MeshAlgorithm::GetFacetsFromPlane(
const MeshFacetGrid& rclGrid,
const Base::Vector3f& clNormal,
float d,
const Base::Vector3f& rclLeft,
const Base::Vector3f& rclRight,
std::vector<FacetIndex>& rclRes
) const
{
std::vector<FacetIndex> aulFacets;
Base::Vector3f clBase = d * clNormal;
Base::Vector3f clPtNormal(rclLeft - rclRight);
clPtNormal.Normalize();
// search grid
MeshGridIterator clGridIter(rclGrid);
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
// add facets from grid if the plane if cut the grid-voxel
if (clGridIter.GetBoundBox().IsCutPlane(clBase, clNormal)) {
clGridIter.GetElements(aulFacets);
}
}
// testing facet against planes
for (FacetIndex facetIndex : aulFacets) {
MeshGeomFacet clSFacet = _rclMesh.GetFacet(facetIndex);
if (clSFacet.IntersectWithPlane(clBase, clNormal)) {
bool bInner = false;
for (int i = 0; (i < 3) && !bInner; i++) {
Base::Vector3f clPt = clSFacet._aclPoints[i];
if ((clPt.DistanceToPlane(rclLeft, clPtNormal) <= 0.0F)
&& (clPt.DistanceToPlane(rclRight, clPtNormal) >= 0.0F)) {
bInner = true;
}
}
if (bInner) {
rclRes.push_back(facetIndex);
}
}
}
}
void MeshAlgorithm::PointsFromFacetsIndices(
const std::vector<FacetIndex>& rvecIndices,
std::vector<Base::Vector3f>& rvecPoints
) const
{
const MeshFacetArray& rclFAry = _rclMesh._aclFacetArray;
const MeshPointArray& rclPAry = _rclMesh._aclPointArray;
std::set<PointIndex> setPoints;
for (FacetIndex facetIndex : rvecIndices) {
for (PointIndex pointIndex : rclFAry[facetIndex]._aulPoints) {
setPoints.insert(pointIndex);
}
}
rvecPoints.clear();
for (PointIndex pointIndex : setPoints) {
rvecPoints.push_back(rclPAry[pointIndex]);
}
}
bool MeshAlgorithm::Distance(
const Base::Vector3f& rclPt,
FacetIndex ulFacetIdx,
float fMaxDistance,
float& rfDistance
) const
{
const MeshFacetArray& rclFAry = _rclMesh._aclFacetArray;
const MeshPointArray& rclPAry = _rclMesh._aclPointArray;
const PointIndex* pulIdx = rclFAry[ulFacetIdx]._aulPoints;
BoundBox3f clBB;
clBB.Add(rclPAry[*(pulIdx++)]);
clBB.Add(rclPAry[*(pulIdx++)]);
clBB.Add(rclPAry[*pulIdx]);
clBB.Enlarge(fMaxDistance);
if (!clBB.IsInBox(rclPt)) {
return false;
}
rfDistance = _rclMesh.GetFacet(ulFacetIdx).DistanceToPoint(rclPt);
return rfDistance < fMaxDistance;
}
float MeshAlgorithm::CalculateMinimumGridLength(
float fLength,
const Base::BoundBox3f& rBBox,
unsigned long maxElements
) const
{
// Max. limit of grid elements
float fMaxGridElements = static_cast<float>(maxElements);
// estimate the minimum allowed grid length
float fMinGridLen = static_cast<float>(
std::pow((rBBox.LengthX() * rBBox.LengthY() * rBBox.LengthZ() / fMaxGridElements), 0.3333F)
);
return std::max<float>(fMinGridLen, fLength);
}
// ----------------------------------------------------
void MeshRefPointToFacets::Rebuild()
{
_map.clear();
const MeshPointArray& rPoints = _rclMesh.GetPoints();
const MeshFacetArray& rFacets = _rclMesh.GetFacets();
_map.resize(rPoints.size());
auto pFBegin = rFacets.begin();
for (auto pFIter = rFacets.begin(); pFIter != rFacets.end(); ++pFIter) {
_map[pFIter->_aulPoints[0]].insert(pFIter - pFBegin);
_map[pFIter->_aulPoints[1]].insert(pFIter - pFBegin);
_map[pFIter->_aulPoints[2]].insert(pFIter - pFBegin);
}
}
Base::Vector3f MeshRefPointToFacets::GetNormal(PointIndex pos) const
{
const std::set<FacetIndex>& n = _map[pos];
Base::Vector3f normal;
MeshGeomFacet f;
for (FacetIndex it : n) {
f = _rclMesh.GetFacet(it);
normal += f.Area() * f.GetNormal();
}
normal.Normalize();
return normal;
}
std::set<PointIndex> MeshRefPointToFacets::NeighbourPoints(
const std::vector<PointIndex>& pt,
int level
) const
{
std::set<PointIndex> cp, nb, lp;
cp.insert(pt.begin(), pt.end());
lp.insert(pt.begin(), pt.end());
auto f_it = _rclMesh.GetFacets().begin();
for (int i = 0; i < level; i++) {
std::set<PointIndex> cur;
for (PointIndex it : lp) {
const std::set<FacetIndex>& ft = (*this)[it];
for (FacetIndex jt : ft) {
for (PointIndex index : f_it[jt]._aulPoints) {
if (cp.find(index) == cp.end() && nb.find(index) == nb.end()) {
nb.insert(index);
cur.insert(index);
}
}
}
}
lp = cur;
if (lp.empty()) {
break;
}
}
return nb;
}
std::set<PointIndex> MeshRefPointToFacets::NeighbourPoints(PointIndex pos) const
{
std::set<PointIndex> p;
const std::set<FacetIndex>& vf = _map[pos];
for (FacetIndex it : vf) {
PointIndex p1 {}, p2 {}, p3 {};
_rclMesh.GetFacetPoints(it, p1, p2, p3);
if (p1 != pos) {
p.insert(p1);
}
if (p2 != pos) {
p.insert(p2);
}
if (p3 != pos) {
p.insert(p3);
}
}
return p;
}
void MeshRefPointToFacets::Neighbours(FacetIndex ulFacetInd, float fMaxDist, MeshCollector& collect) const
{
std::set<FacetIndex> visited;
Base::Vector3f clCenter = _rclMesh.GetFacet(ulFacetInd).GetGravityPoint();
const MeshFacetArray& rFacets = _rclMesh.GetFacets();
SearchNeighbours(rFacets, ulFacetInd, clCenter, fMaxDist * fMaxDist, visited, collect);
}
void MeshRefPointToFacets::SearchNeighbours(
const MeshFacetArray& rFacets,
FacetIndex index,
const Base::Vector3f& rclCenter,
float fMaxDist2,
std::set<FacetIndex>& visited,
MeshCollector& collect
) const
{
if (visited.find(index) != visited.end()) {
return;
}
const MeshFacet& face = rFacets[index];
if (Base::DistanceP2(rclCenter, _rclMesh.GetFacet(face).GetGravityPoint()) > fMaxDist2) {
return;
}
visited.insert(index);
collect.Append(_rclMesh, index);
for (PointIndex ptIndex : face._aulPoints) {
const std::set<FacetIndex>& f = (*this)[ptIndex];
for (FacetIndex j : f) {
SearchNeighbours(rFacets, j, rclCenter, fMaxDist2, visited, collect);
}
}
}
MeshFacetArray::_TConstIterator MeshRefPointToFacets::GetFacet(FacetIndex index) const
{
return _rclMesh.GetFacets().begin() + index;
}
const std::set<FacetIndex>& MeshRefPointToFacets::operator[](PointIndex pos) const
{
return _map[pos];
}
std::vector<FacetIndex> MeshRefPointToFacets::GetIndices(PointIndex pos1, PointIndex pos2) const
{
std::vector<FacetIndex> intersection;
std::back_insert_iterator<std::vector<FacetIndex>> result(intersection);
const std::set<FacetIndex>& set1 = _map[pos1];
const std::set<FacetIndex>& set2 = _map[pos2];
std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), result);
return intersection;
}
std::vector<FacetIndex> MeshRefPointToFacets::GetIndices(
PointIndex pos1,
PointIndex pos2,
PointIndex pos3
) const
{
std::vector<FacetIndex> intersection;
std::back_insert_iterator<std::vector<FacetIndex>> result(intersection);
std::vector<FacetIndex> set1 = GetIndices(pos1, pos2);
const std::set<FacetIndex>& set2 = _map[pos3];
std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), result);
return intersection;
}
void MeshRefPointToFacets::AddNeighbour(PointIndex pos, FacetIndex facet)
{
_map[pos].insert(facet);
}
void MeshRefPointToFacets::RemoveNeighbour(PointIndex pos, FacetIndex facet)
{
_map[pos].erase(facet);
}
void MeshRefPointToFacets::RemoveFacet(FacetIndex facetIndex)
{
PointIndex p0 {}, p1 {}, p2 {};
_rclMesh.GetFacetPoints(facetIndex, p0, p1, p2);
_map[p0].erase(facetIndex);
_map[p1].erase(facetIndex);
_map[p2].erase(facetIndex);
}
//----------------------------------------------------------------------------
void MeshRefFacetToFacets::Rebuild()
{
_map.clear();
const MeshFacetArray& rFacets = _rclMesh.GetFacets();
_map.resize(rFacets.size());
MeshRefPointToFacets vertexFace(_rclMesh);
auto pFBegin = rFacets.begin();
for (auto pFIter = pFBegin; pFIter != rFacets.end(); ++pFIter) {
for (PointIndex ptIndex : pFIter->_aulPoints) {
const std::set<FacetIndex>& faces = vertexFace[ptIndex];
for (FacetIndex face : faces) {
_map[pFIter - pFBegin].insert(face);
}
}
}
}
const std::set<FacetIndex>& MeshRefFacetToFacets::operator[](FacetIndex pos) const
{
return _map[pos];
}
std::vector<FacetIndex> MeshRefFacetToFacets::GetIndices(FacetIndex pos1, FacetIndex pos2) const
{
std::vector<FacetIndex> intersection;
std::back_insert_iterator<std::vector<FacetIndex>> result(intersection);
const std::set<FacetIndex>& set1 = _map[pos1];
const std::set<FacetIndex>& set2 = _map[pos2];
std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), result);
return intersection;
}
//----------------------------------------------------------------------------
void MeshRefPointToPoints::Rebuild()
{
_map.clear();
const MeshPointArray& rPoints = _rclMesh.GetPoints();
_map.resize(rPoints.size());
const MeshFacetArray& rFacets = _rclMesh.GetFacets();
for (const auto& rFacet : rFacets) {
PointIndex ulP0 = rFacet._aulPoints[0];
PointIndex ulP1 = rFacet._aulPoints[1];
PointIndex ulP2 = rFacet._aulPoints[2];
_map[ulP0].insert(ulP1);
_map[ulP0].insert(ulP2);
_map[ulP1].insert(ulP0);
_map[ulP1].insert(ulP2);
_map[ulP2].insert(ulP0);
_map[ulP2].insert(ulP1);
}
}
Base::Vector3f MeshRefPointToPoints::GetNormal(PointIndex pos) const
{
const MeshPointArray& rPoints = _rclMesh.GetPoints();
MeshCore::PlaneFit pf;
pf.AddPoint(rPoints[pos]);
MeshCore::MeshPoint center = rPoints[pos];
const std::set<PointIndex>& cv = _map[pos];
for (PointIndex cv_it : cv) {
pf.AddPoint(rPoints[cv_it]);
center += rPoints[cv_it];
}
pf.Fit();
Base::Vector3f normal = pf.GetNormal();
normal.Normalize();
return normal;
}
float MeshRefPointToPoints::GetAverageEdgeLength(PointIndex index) const
{
const MeshPointArray& rPoints = _rclMesh.GetPoints();
float len = 0.0F;
const std::set<PointIndex>& n = (*this)[index];
const Base::Vector3f& p = rPoints[index];
for (PointIndex it : n) {
len += Base::Distance(p, rPoints[it]);
}
return (len / n.size());
}
const std::set<PointIndex>& MeshRefPointToPoints::operator[](PointIndex pos) const
{
return _map[pos];
}
void MeshRefPointToPoints::AddNeighbour(PointIndex pos, PointIndex facet)
{
_map[pos].insert(facet);
}
void MeshRefPointToPoints::RemoveNeighbour(PointIndex pos, PointIndex facet)
{
_map[pos].erase(facet);
}
//----------------------------------------------------------------------------
void MeshRefEdgeToFacets::Rebuild()
{
_map.clear();
const MeshFacetArray& rFacets = _rclMesh.GetFacets();
FacetIndex index = 0;
for (auto it = rFacets.begin(); it != rFacets.end(); ++it, ++index) {
for (int i = 0; i < 3; i++) {
MeshEdge e;
e.first = it->_aulPoints[i];
e.second = it->_aulPoints[(i + 1) % 3];
auto jt = _map.find(e);
if (jt == _map.end()) {
_map[e].first = index;
_map[e].second = FACET_INDEX_MAX;
}
else {
_map[e].second = index;
}
}
}
}
const std::pair<FacetIndex, FacetIndex>& MeshRefEdgeToFacets::operator[](const MeshEdge& edge) const
{
return _map.find(edge)->second;
}
//----------------------------------------------------------------------------
void MeshRefNormalToPoints::Rebuild()
{
_norm.clear();
const MeshPointArray& rPoints = _rclMesh.GetPoints();
_norm.resize(rPoints.size());
const MeshFacetArray& rFacets = _rclMesh.GetFacets();
for (const auto& rFacet : rFacets) {
const MeshPoint& p0 = rPoints[rFacet._aulPoints[0]];
const MeshPoint& p1 = rPoints[rFacet._aulPoints[1]];
const MeshPoint& p2 = rPoints[rFacet._aulPoints[2]];
float l2p01 = Base::DistanceP2(p0, p1);
float l2p12 = Base::DistanceP2(p1, p2);
float l2p20 = Base::DistanceP2(p2, p0);
Base::Vector3f facenormal = _rclMesh.GetFacet(rFacet).GetNormal();
_norm[rFacet._aulPoints[0]] += facenormal * (1.0F / (l2p01 * l2p20));
_norm[rFacet._aulPoints[1]] += facenormal * (1.0F / (l2p12 * l2p01));
_norm[rFacet._aulPoints[2]] += facenormal * (1.0F / (l2p20 * l2p12));
}
for (auto& it : _norm) {
it.Normalize();
}
}
const Base::Vector3f& MeshRefNormalToPoints::operator[](PointIndex pos) const
{
return _norm[pos];
}
|