File size: 41,256 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 | // 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 <vector>
#include <Base/Matrix.h>
#include <Base/Sequencer.h>
#include "Algorithm.h"
#include "Approximation.h"
#include "Evaluation.h"
#include "Functional.h"
#include "Grid.h"
#include "Iterator.h"
#include "TopoAlgorithm.h"
using namespace MeshCore;
MeshOrientationVisitor::MeshOrientationVisitor() = default;
bool MeshOrientationVisitor::Visit(
const MeshFacet& rclFacet,
const MeshFacet& rclFrom,
FacetIndex ulFInd,
unsigned long ulLevel
)
{
(void)ulFInd;
(void)ulLevel;
if (!rclFrom.HasSameOrientation(rclFacet)) {
_nonuniformOrientation = true;
return false;
}
return true;
}
bool MeshOrientationVisitor::HasNonUnifomOrientedFacets() const
{
return _nonuniformOrientation;
}
MeshOrientationCollector::MeshOrientationCollector(
std::vector<FacetIndex>& aulIndices,
std::vector<FacetIndex>& aulComplement
)
: _aulIndices(aulIndices)
, _aulComplement(aulComplement)
{}
bool MeshOrientationCollector::Visit(
const MeshFacet& rclFacet,
const MeshFacet& rclFrom,
FacetIndex ulFInd,
unsigned long ulLevel
)
{
(void)ulLevel;
// different orientation of rclFacet and rclFrom
if (!rclFacet.HasSameOrientation(rclFrom)) {
// is not marked as false oriented
if (!rclFrom.IsFlag(MeshFacet::TMP0)) {
// mark this facet as false oriented
rclFacet.SetFlag(MeshFacet::TMP0);
_aulIndices.push_back(ulFInd);
}
else {
_aulComplement.push_back(ulFInd);
}
}
else {
// same orientation but if the neighbour rclFrom is false oriented
// then rclFrom is also false oriented
if (rclFrom.IsFlag(MeshFacet::TMP0)) {
// mark this facet as false oriented
rclFacet.SetFlag(MeshFacet::TMP0);
_aulIndices.push_back(ulFInd);
}
else {
_aulComplement.push_back(ulFInd);
}
}
return true;
}
MeshSameOrientationCollector::MeshSameOrientationCollector(std::vector<FacetIndex>& aulIndices)
: _aulIndices(aulIndices)
{}
bool MeshSameOrientationCollector::Visit(
const MeshFacet& rclFacet,
const MeshFacet& rclFrom,
FacetIndex ulFInd,
unsigned long ulLevel
)
{
// different orientation of rclFacet and rclFrom
(void)ulLevel;
if (rclFacet.HasSameOrientation(rclFrom)) {
_aulIndices.push_back(ulFInd);
}
return true;
}
// ----------------------------------------------------
MeshEvalOrientation::MeshEvalOrientation(const MeshKernel& rclM)
: MeshEvaluation(rclM)
{}
bool MeshEvalOrientation::Evaluate()
{
const MeshFacetArray& rFAry = _rclMesh.GetFacets();
MeshFacetArray::_TConstIterator iBeg = rFAry.begin();
MeshFacetArray::_TConstIterator iEnd = rFAry.end();
for (auto it = iBeg; it != iEnd; ++it) {
for (int i = 0; i < 3; i++) {
if (it->_aulNeighbours[i] != FACET_INDEX_MAX) {
const MeshFacet& rclFacet = iBeg[it->_aulNeighbours[i]];
for (int j = 0; j < 3; j++) {
if (it->_aulPoints[i] == rclFacet._aulPoints[j]) {
if ((it->_aulPoints[(i + 1) % 3] == rclFacet._aulPoints[(j + 1) % 3])
|| (it->_aulPoints[(i + 2) % 3] == rclFacet._aulPoints[(j + 2) % 3])) {
return false; // adjacent face with wrong orientation
}
}
}
}
}
}
return true;
}
unsigned long MeshEvalOrientation::HasFalsePositives(const std::vector<FacetIndex>& inds) const
{
// All faces with wrong orientation (i.e. adjacent faces with a normal flip and their
// neighbours) build a segment and are marked as TMP0. Now we check all border faces of the
// segments with their correct neighbours if there was really a normal flip. If there is no
// normal flip we have a false positive. False-positives can occur if the mesh structure has
// some defects which let the region-grow algorithm fail to detect the faces with wrong
// orientation.
const MeshFacetArray& rFAry = _rclMesh.GetFacets();
MeshFacetArray::_TConstIterator iBeg = rFAry.begin();
for (FacetIndex it : inds) {
const MeshFacet& f = iBeg[it];
for (FacetIndex nbIndex : f._aulNeighbours) {
if (nbIndex != FACET_INDEX_MAX) {
const MeshFacet& n = iBeg[nbIndex];
if (f.IsFlag(MeshFacet::TMP0) && !n.IsFlag(MeshFacet::TMP0)) {
for (int j = 0; j < 3; j++) {
if (f.HasSameOrientation(n)) {
// adjacent face with same orientation => false positive
return nbIndex;
}
}
}
}
}
}
return FACET_INDEX_MAX;
}
std::vector<FacetIndex> MeshEvalOrientation::GetIndices() const
{
FacetIndex ulStartFacet {}, ulVisited {};
if (_rclMesh.CountFacets() == 0) {
return {};
}
// reset VISIT flags
MeshAlgorithm cAlg(_rclMesh);
cAlg.ResetFacetFlag(MeshFacet::VISIT);
cAlg.ResetFacetFlag(MeshFacet::TMP0);
const MeshFacetArray& rFAry = _rclMesh.GetFacets();
MeshFacetArray::_TConstIterator iTri = rFAry.begin();
MeshFacetArray::_TConstIterator iBeg = rFAry.begin();
MeshFacetArray::_TConstIterator iEnd = rFAry.end();
ulStartFacet = 0;
std::vector<FacetIndex> uIndices, uComplement;
MeshOrientationCollector clHarmonizer(uIndices, uComplement);
while (ulStartFacet != FACET_INDEX_MAX) {
unsigned long wrongFacets = uIndices.size();
uComplement.clear();
uComplement.push_back(ulStartFacet);
ulVisited = _rclMesh.VisitNeighbourFacets(clHarmonizer, ulStartFacet) + 1;
// In the currently visited component we have found less than 40% as correct
// oriented and the rest as false oriented. So, we decide that it should be the other
// way round and swap the indices of this component.
if (uComplement.size() < static_cast<unsigned long>(0.4F * static_cast<float>(ulVisited))) {
uIndices.erase(uIndices.begin() + wrongFacets, uIndices.end());
uIndices.insert(uIndices.end(), uComplement.begin(), uComplement.end());
}
// if the mesh consists of several topologic independent components
// We can search from position 'iTri' on because all elements _before_ are already visited
// what we know from the previous iteration.
MeshIsNotFlag<MeshFacet> flag;
iTri = std::find_if(iTri, iEnd, [flag](const MeshFacet& f) {
return flag(f, MeshFacet::VISIT);
});
if (iTri < iEnd) {
ulStartFacet = iTri - iBeg;
}
else {
ulStartFacet = FACET_INDEX_MAX;
}
}
// in some very rare cases where we have some strange artifacts in the mesh structure
// we get false-positives. If we find some we check all 'invalid' faces again
cAlg.ResetFacetFlag(MeshFacet::TMP0);
cAlg.SetFacetsFlag(uIndices, MeshFacet::TMP0);
ulStartFacet = HasFalsePositives(uIndices);
while (ulStartFacet != FACET_INDEX_MAX) {
cAlg.ResetFacetsFlag(uIndices, MeshFacet::VISIT);
std::vector<FacetIndex> falsePos;
MeshSameOrientationCollector coll(falsePos);
_rclMesh.VisitNeighbourFacets(coll, ulStartFacet);
std::sort(uIndices.begin(), uIndices.end());
std::sort(falsePos.begin(), falsePos.end());
std::vector<FacetIndex> diff;
std::back_insert_iterator<std::vector<FacetIndex>> biit(diff);
std::set_difference(uIndices.begin(), uIndices.end(), falsePos.begin(), falsePos.end(), biit);
uIndices = diff;
cAlg.ResetFacetFlag(MeshFacet::TMP0);
cAlg.SetFacetsFlag(uIndices, MeshFacet::TMP0);
FacetIndex current = ulStartFacet;
ulStartFacet = HasFalsePositives(uIndices);
if (current == ulStartFacet) {
break; // avoid an endless loop
}
}
return uIndices;
}
MeshFixOrientation::MeshFixOrientation(MeshKernel& rclM)
: MeshValidation(rclM)
{}
bool MeshFixOrientation::Fixup()
{
MeshTopoAlgorithm(_rclMesh).HarmonizeNormals();
return MeshEvalOrientation(_rclMesh).Evaluate();
}
// ----------------------------------------------------
MeshEvalSolid::MeshEvalSolid(const MeshKernel& rclM)
: MeshEvaluation(rclM)
{}
bool MeshEvalSolid::Evaluate()
{
std::vector<MeshGeomEdge> edges;
_rclMesh.GetEdges(edges);
for (const auto& it : edges) {
if (it._bBorder) {
return false;
}
}
return true;
}
// ----------------------------------------------------
namespace MeshCore
{
struct Edge_Index
{
PointIndex p0, p1;
FacetIndex f;
};
struct Edge_Less
{
bool operator()(const Edge_Index& x, const Edge_Index& y) const
{
if (x.p0 < y.p0) {
return true;
}
if (x.p0 > y.p0) {
return false;
}
if (x.p1 < y.p1) {
return true;
}
if (x.p1 > y.p1) {
return false;
}
return false;
}
};
} // namespace MeshCore
bool MeshEvalTopology::Evaluate()
{
// Using and sorting a vector seems to be faster and more memory-efficient
// than a map.
const MeshFacetArray& rclFAry = _rclMesh.GetFacets();
std::vector<Edge_Index> edges;
edges.reserve(3 * rclFAry.size());
// build up an array of edges
MeshFacetArray::_TConstIterator pI;
Base::SequencerLauncher seq("Checking topology...", rclFAry.size());
for (pI = rclFAry.begin(); pI != rclFAry.end(); ++pI) {
for (int i = 0; i < 3; i++) {
Edge_Index item {};
item.p0 = std::min<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.p1 = std::max<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.f = pI - rclFAry.begin();
edges.push_back(item);
}
seq.next();
}
// sort the edges
std::sort(edges.begin(), edges.end(), Edge_Less());
// search for non-manifold edges
PointIndex p0 = POINT_INDEX_MAX, p1 = POINT_INDEX_MAX;
nonManifoldList.clear();
nonManifoldFacets.clear();
int count = 0;
std::vector<FacetIndex> facets;
std::vector<Edge_Index>::iterator pE;
for (pE = edges.begin(); pE != edges.end(); ++pE) {
if (p0 == pE->p0 && p1 == pE->p1) {
count++;
facets.push_back(pE->f);
}
else {
if (count > 2) {
// Edge that is shared by more than 2 facets
nonManifoldList.emplace_back(p0, p1);
nonManifoldFacets.push_back(facets);
}
p0 = pE->p0;
p1 = pE->p1;
facets.clear();
facets.push_back(pE->f);
count = 1;
}
}
return nonManifoldList.empty();
}
// generate indexed edge list which tangents non-manifolds
void MeshEvalTopology::GetFacetManifolds(std::vector<FacetIndex>& raclFacetIndList) const
{
raclFacetIndList.clear();
const MeshFacetArray& rclFAry = _rclMesh.GetFacets();
MeshFacetArray::_TConstIterator pI;
for (pI = rclFAry.begin(); pI != rclFAry.end(); ++pI) {
for (int i = 0; i < 3; i++) {
PointIndex ulPt0 = std::min<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
PointIndex ulPt1 = std::max<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
std::pair<PointIndex, PointIndex> edge = std::make_pair(ulPt0, ulPt1);
if (std::ranges::find(nonManifoldList, edge) != nonManifoldList.end()) {
raclFacetIndList.push_back(pI - rclFAry.begin());
}
}
}
}
unsigned long MeshEvalTopology::CountManifolds() const
{
return nonManifoldList.size();
}
bool MeshFixTopology::Fixup()
{
#if 0
MeshEvalTopology eval(_rclMesh);
if (!eval.Evaluate()) {
eval.GetFacetManifolds(deletedFaces);
// remove duplicates
std::sort(deletedFaces.begin(), deletedFaces.end());
deletedFaces.erase(std::unique(deletedFaces.begin(), deletedFaces.end()), deletedFaces.end());
_rclMesh.DeleteFacets(deletedFaces);
}
#else
const MeshFacetArray& rFaces = _rclMesh.GetFacets();
deletedFaces.reserve(3 * nonManifoldList.size()); // allocate some memory
for (const auto& it : nonManifoldList) {
std::vector<FacetIndex> non_mf;
non_mf.reserve(it.size());
for (FacetIndex jt : it) {
// facet is only connected with one edge and there causes a non-manifold
unsigned short numOpenEdges = rFaces[jt].CountOpenEdges();
if (numOpenEdges == 2) {
non_mf.push_back(jt);
}
else if (rFaces[jt].IsDegenerated()) {
non_mf.push_back(jt);
}
}
// are we able to repair the non-manifold edge by not removing all facets?
if (it.size() - non_mf.size() == 2) {
deletedFaces.insert(deletedFaces.end(), non_mf.begin(), non_mf.end());
}
else {
deletedFaces.insert(deletedFaces.end(), it.begin(), it.end());
}
}
if (!deletedFaces.empty()) {
// remove duplicates
std::sort(deletedFaces.begin(), deletedFaces.end());
deletedFaces.erase(std::unique(deletedFaces.begin(), deletedFaces.end()), deletedFaces.end());
_rclMesh.DeleteFacets(deletedFaces);
_rclMesh.RebuildNeighbours();
}
#endif
return true;
}
// ---------------------------------------------------------
bool MeshEvalPointManifolds::Evaluate()
{
this->nonManifoldPoints.clear();
this->facetsOfNonManifoldPoints.clear();
MeshCore::MeshRefPointToPoints vv_it(_rclMesh);
MeshCore::MeshRefPointToFacets vf_it(_rclMesh);
unsigned long ctPoints = _rclMesh.CountPoints();
for (PointIndex index = 0; index < ctPoints; index++) {
// get the local neighbourhood of the point
const std::set<FacetIndex>& nf = vf_it[index];
const std::set<PointIndex>& np = vv_it[index];
std::set<unsigned long>::size_type sp {}, sf {};
sp = np.size();
sf = nf.size();
// for an inner point the number of adjacent points is equal to the number of shared faces
// for a boundary point the number of adjacent points is higher by one than the number of
// shared faces for a non-manifold point the number of adjacent points is higher by more
// than one than the number of shared faces
if (sp > sf + 1) {
nonManifoldPoints.push_back(index);
std::vector<FacetIndex> faces;
faces.insert(faces.end(), nf.begin(), nf.end());
this->facetsOfNonManifoldPoints.push_back(faces);
}
}
return this->nonManifoldPoints.empty();
}
void MeshEvalPointManifolds::GetFacetIndices(std::vector<FacetIndex>& facets) const
{
std::list<std::vector<FacetIndex>>::const_iterator it;
for (it = facetsOfNonManifoldPoints.begin(); it != facetsOfNonManifoldPoints.end(); ++it) {
facets.insert(facets.end(), it->begin(), it->end());
}
if (!facets.empty()) {
// remove duplicates
std::sort(facets.begin(), facets.end());
facets.erase(std::unique(facets.begin(), facets.end()), facets.end());
}
}
// ---------------------------------------------------------
bool MeshEvalSingleFacet::Evaluate()
{
// get all non-manifolds
(void)MeshEvalTopology::Evaluate();
/*
// for each (multiple) single linked facet there should
// exist two valid facets sharing the same edge
// so make facet 1 neighbour of facet 2 and vice versa
const std::vector<MeshFacet>& rclFAry = _rclMesh.GetFacets();
std::vector<MeshFacet>::const_iterator pI;
std::vector<std::list<unsigned long> > aclMf = _aclManifoldList;
_aclManifoldList.clear();
std::map<std::pair<unsigned long, unsigned long>, std::list<unsigned long> > aclHits;
std::map<std::pair<unsigned long, unsigned long>, std::list<unsigned long> >::iterator pEdge;
// search for single links (a non-manifold edge and two open edges)
//
//
// build edge <=> facet map
for (pI = rclFAry.begin(); pI != rclFAry.end(); ++pI)
{
for (int i = 0; i < 3; i++)
{
unsigned long ulPt0 = std::min<unsigned long>(pI->_aulPoints[i], pI->_aulPoints[(i+1)%3]);
unsigned long ulPt1 = std::max<unsigned long>(pI->_aulPoints[i], pI->_aulPoints[(i+1)%3]);
aclHits[std::pair<unsigned long, unsigned long>(ulPt0, ulPt1)].push_front(pI -
rclFAry.begin());
}
}
// now search for single links
for (std::vector<std::list<unsigned long> >::const_iterator pMF = aclMf.begin(); pMF !=
aclMf.end(); ++pMF)
{
std::list<unsigned long> aulManifolds;
for (std::list<unsigned long>::const_iterator pF = pMF->begin(); pF != pMF->end(); ++pF)
{
const MeshFacet& rclF = rclFAry[*pF];
unsigned long ulCtNeighbours=0;
for (int i = 0; i < 3; i++)
{
unsigned long ulPt0 = std::min<unsigned long>(rclF._aulPoints[i],
rclF._aulPoints[(i+1)%3]); unsigned long ulPt1 = std::max<unsigned long>(rclF._aulPoints[i],
rclF._aulPoints[(i+1)%3]); std::pair<unsigned long, unsigned long> clEdge(ulPt0, ulPt1);
// number of facets sharing this edge
ulCtNeighbours += aclHits[clEdge].size();
}
// single linked found
if (ulCtNeighbours == pMF->size() + 2)
aulManifolds.push_front(*pF);
}
if ( aulManifolds.size() > 0 )
_aclManifoldList.push_back(aulManifolds);
}
*/
return (nonManifoldList.empty());
}
bool MeshFixSingleFacet::Fixup()
{
std::vector<FacetIndex> aulInvalids;
for (const auto& it : _raclManifoldList) {
for (FacetIndex it2 : it) {
aulInvalids.push_back(it2);
}
}
_rclMesh.DeleteFacets(aulInvalids);
return true;
}
// ----------------------------------------------------------------
bool MeshEvalSelfIntersection::Evaluate()
{
// Contains bounding boxes for every facet
std::vector<Base::BoundBox3f> boxes;
// Splits the mesh using grid for speeding up the calculation
MeshFacetGrid cMeshFacetGrid(_rclMesh);
const MeshFacetArray& rFaces = _rclMesh.GetFacets();
MeshGridIterator clGridIter(cMeshFacetGrid);
unsigned long ulGridX {}, ulGridY {}, ulGridZ {};
cMeshFacetGrid.GetCtGrids(ulGridX, ulGridY, ulGridZ);
MeshFacetIterator cMFI(_rclMesh);
for (cMFI.Begin(); cMFI.More(); cMFI.Next()) {
boxes.push_back((*cMFI).GetBoundBox());
}
// Calculates the intersections
Base::SequencerLauncher seq("Checking for self-intersections...", ulGridX * ulGridY * ulGridZ);
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
// Get the facet indices, belonging to the current grid unit
std::vector<FacetIndex> aulGridElements;
clGridIter.GetElements(aulGridElements);
seq.next();
if (aulGridElements.empty()) {
continue;
}
MeshGeomFacet facet1, facet2;
Base::Vector3f pt1, pt2;
for (auto it = aulGridElements.begin(); it != aulGridElements.end(); ++it) {
const Base::BoundBox3f& box1 = boxes[*it];
cMFI.Set(*it);
facet1 = *cMFI;
const MeshFacet& rface1 = rFaces[*it];
for (auto jt = it; jt != aulGridElements.end(); ++jt) {
if (jt == it) { // the identical facet
continue;
}
// If the facets share a common vertex we do not check for self-intersections
// because they could but usually do not intersect each other and the algorithm
// below would detect false-positives, otherwise
const MeshFacet& rface2 = rFaces[*jt];
if (rface1._aulPoints[0] == rface2._aulPoints[0]
|| rface1._aulPoints[0] == rface2._aulPoints[1]
|| rface1._aulPoints[0] == rface2._aulPoints[2]) {
continue; // ignore facets sharing a common vertex
}
if (rface1._aulPoints[1] == rface2._aulPoints[0]
|| rface1._aulPoints[1] == rface2._aulPoints[1]
|| rface1._aulPoints[1] == rface2._aulPoints[2]) {
continue; // ignore facets sharing a common vertex
}
if (rface1._aulPoints[2] == rface2._aulPoints[0]
|| rface1._aulPoints[2] == rface2._aulPoints[1]
|| rface1._aulPoints[2] == rface2._aulPoints[2]) {
continue; // ignore facets sharing a common vertex
}
const Base::BoundBox3f& box2 = boxes[*jt];
if (box1 && box2) {
cMFI.Set(*jt);
facet2 = *cMFI;
int ret = facet1.IntersectWithFacet(facet2, pt1, pt2);
if (ret == 2) {
// abort after the first detected self-intersection
return false;
}
}
}
}
}
return true;
}
void MeshEvalSelfIntersection::GetIntersections(
const std::vector<std::pair<FacetIndex, FacetIndex>>& indices,
std::vector<std::pair<Base::Vector3f, Base::Vector3f>>& intersection
) const
{
intersection.reserve(indices.size());
MeshFacetIterator cMF1(_rclMesh);
MeshFacetIterator cMF2(_rclMesh);
Base::Vector3f pt1, pt2;
std::vector<std::pair<FacetIndex, FacetIndex>>::const_iterator it;
for (it = indices.begin(); it != indices.end(); ++it) {
cMF1.Set(it->first);
cMF2.Set(it->second);
Base::BoundBox3f box1 = cMF1->GetBoundBox();
Base::BoundBox3f box2 = cMF2->GetBoundBox();
if (box1 && box2) {
int ret = cMF1->IntersectWithFacet(*cMF2, pt1, pt2);
if (ret == 2) {
intersection.emplace_back(pt1, pt2);
}
}
}
}
void MeshEvalSelfIntersection::GetIntersections(
std::vector<std::pair<FacetIndex, FacetIndex>>& intersection
) const
{
// Contains bounding boxes for every facet
std::vector<Base::BoundBox3f> boxes;
// intersection.clear();
// Splits the mesh using grid for speeding up the calculation
MeshFacetGrid cMeshFacetGrid(_rclMesh);
const MeshFacetArray& rFaces = _rclMesh.GetFacets();
MeshGridIterator clGridIter(cMeshFacetGrid);
unsigned long ulGridX {}, ulGridY {}, ulGridZ {};
cMeshFacetGrid.GetCtGrids(ulGridX, ulGridY, ulGridZ);
MeshFacetIterator cMFI(_rclMesh);
for (cMFI.Begin(); cMFI.More(); cMFI.Next()) {
boxes.push_back((*cMFI).GetBoundBox());
}
// Calculates the intersections
Base::SequencerLauncher seq("Checking for self-intersections...", ulGridX * ulGridY * ulGridZ);
for (clGridIter.Init(); clGridIter.More(); clGridIter.Next()) {
// Get the facet indices, belonging to the current grid unit
std::vector<FacetIndex> aulGridElements;
clGridIter.GetElements(aulGridElements);
seq.next(true);
if (aulGridElements.empty()) {
continue;
}
MeshGeomFacet facet1, facet2;
Base::Vector3f pt1, pt2;
for (auto it = aulGridElements.begin(); it != aulGridElements.end(); ++it) {
const Base::BoundBox3f& box1 = boxes[*it];
cMFI.Set(*it);
facet1 = *cMFI;
const MeshFacet& rface1 = rFaces[*it];
for (auto jt = it; jt != aulGridElements.end(); ++jt) {
if (jt == it) { // the identical facet
continue;
}
// If the facets share a common vertex we do not check for self-intersections
// because they could but usually do not intersect each other and the algorithm
// below would detect false-positives, otherwise
const MeshFacet& rface2 = rFaces[*jt];
if (rface1._aulPoints[0] == rface2._aulPoints[0]
|| rface1._aulPoints[0] == rface2._aulPoints[1]
|| rface1._aulPoints[0] == rface2._aulPoints[2]) {
continue; // ignore facets sharing a common vertex
}
if (rface1._aulPoints[1] == rface2._aulPoints[0]
|| rface1._aulPoints[1] == rface2._aulPoints[1]
|| rface1._aulPoints[1] == rface2._aulPoints[2]) {
continue; // ignore facets sharing a common vertex
}
if (rface1._aulPoints[2] == rface2._aulPoints[0]
|| rface1._aulPoints[2] == rface2._aulPoints[1]
|| rface1._aulPoints[2] == rface2._aulPoints[2]) {
continue; // ignore facets sharing a common vertex
}
const Base::BoundBox3f& box2 = boxes[*jt];
if (box1 && box2) {
cMFI.Set(*jt);
facet2 = *cMFI;
int ret = facet1.IntersectWithFacet(facet2, pt1, pt2);
if (ret == 2) {
intersection.emplace_back(*it, *jt);
}
}
}
}
}
}
std::vector<FacetIndex> MeshFixSelfIntersection::GetFacets() const
{
std::vector<FacetIndex> indices;
const MeshFacetArray& rFaces = _rclMesh.GetFacets();
for (const auto& it : selfIntersectons) {
unsigned short numOpenEdges1 = rFaces[it.first].CountOpenEdges();
unsigned short numOpenEdges2 = rFaces[it.second].CountOpenEdges();
// often we have only single or border facets that intersect other facets
// in this case remove only these facets and keep the other one
if (numOpenEdges1 == 0 && numOpenEdges2 > 0) {
indices.push_back(it.second);
}
else if (numOpenEdges1 > 0 && numOpenEdges2 == 0) {
indices.push_back(it.first);
}
else {
indices.push_back(it.first);
indices.push_back(it.second);
}
}
// remove duplicates
std::sort(indices.begin(), indices.end());
indices.erase(std::unique(indices.begin(), indices.end()), indices.end());
return indices;
}
bool MeshFixSelfIntersection::Fixup()
{
_rclMesh.DeleteFacets(GetFacets());
return true;
}
// ----------------------------------------------------------------
bool MeshEvalNeighbourhood::Evaluate()
{
// Note: If more than two facets are attached to the edge then we have a
// non-manifold edge here.
// This means that the neighbourhood cannot be valid, for sure. But we just
// want to check whether the neighbourhood is valid for topologic correctly
// edges and thus we ignore this case.
// Non-manifolds are an own category of errors and are handled by the class
// MeshEvalTopology.
//
// Using and sorting a vector seems to be faster and more memory-efficient
// than a map.
const MeshFacetArray& rclFAry = _rclMesh.GetFacets();
std::vector<Edge_Index> edges;
edges.reserve(3 * rclFAry.size());
// build up an array of edges
MeshFacetArray::_TConstIterator pI;
Base::SequencerLauncher seq("Checking indices...", rclFAry.size());
for (pI = rclFAry.begin(); pI != rclFAry.end(); ++pI) {
for (int i = 0; i < 3; i++) {
Edge_Index item {};
item.p0 = std::min<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.p1 = std::max<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.f = pI - rclFAry.begin();
edges.push_back(item);
}
seq.next();
}
// sort the edges
std::sort(edges.begin(), edges.end(), Edge_Less());
PointIndex p0 = POINT_INDEX_MAX, p1 = POINT_INDEX_MAX;
PointIndex f0 = FACET_INDEX_MAX, f1 = FACET_INDEX_MAX;
int count = 0;
std::vector<Edge_Index>::iterator pE;
for (pE = edges.begin(); pE != edges.end(); ++pE) {
if (p0 == pE->p0 && p1 == pE->p1) {
f1 = pE->f;
count++;
}
else {
// we handle only the cases for 1 and 2, for all higher
// values we have a non-manifold that is ignored here
if (count == 2) {
const MeshFacet& rFace0 = rclFAry[f0];
const MeshFacet& rFace1 = rclFAry[f1];
unsigned short side0 = rFace0.Side(p0, p1);
unsigned short side1 = rFace1.Side(p0, p1);
// Check whether rFace0 and rFace1 reference each other as
// neighbours
if (rFace0._aulNeighbours[side0] != f1 || rFace1._aulNeighbours[side1] != f0) {
return false;
}
}
else if (count == 1) {
const MeshFacet& rFace = rclFAry[f0];
unsigned short side = rFace.Side(p0, p1);
// should be "open edge" but isn't marked as such
if (rFace._aulNeighbours[side] != FACET_INDEX_MAX) {
return false;
}
}
p0 = pE->p0;
p1 = pE->p1;
f0 = pE->f;
count = 1;
}
}
return true;
}
std::vector<FacetIndex> MeshEvalNeighbourhood::GetIndices() const
{
std::vector<FacetIndex> inds;
const MeshFacetArray& rclFAry = _rclMesh.GetFacets();
std::vector<Edge_Index> edges;
edges.reserve(3 * rclFAry.size());
// build up an array of edges
MeshFacetArray::_TConstIterator pI;
Base::SequencerLauncher seq("Checking indices...", rclFAry.size());
for (pI = rclFAry.begin(); pI != rclFAry.end(); ++pI) {
for (int i = 0; i < 3; i++) {
Edge_Index item {};
item.p0 = std::min<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.p1 = std::max<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.f = pI - rclFAry.begin();
edges.push_back(item);
}
seq.next();
}
// sort the edges
std::sort(edges.begin(), edges.end(), Edge_Less());
PointIndex p0 = POINT_INDEX_MAX, p1 = POINT_INDEX_MAX;
PointIndex f0 = FACET_INDEX_MAX, f1 = FACET_INDEX_MAX;
int count = 0;
std::vector<Edge_Index>::iterator pE;
for (pE = edges.begin(); pE != edges.end(); ++pE) {
if (p0 == pE->p0 && p1 == pE->p1) {
f1 = pE->f;
count++;
}
else {
// we handle only the cases for 1 and 2, for all higher
// values we have a non-manifold that is ignored here
if (count == 2) {
const MeshFacet& rFace0 = rclFAry[f0];
const MeshFacet& rFace1 = rclFAry[f1];
unsigned short side0 = rFace0.Side(p0, p1);
unsigned short side1 = rFace1.Side(p0, p1);
// Check whether rFace0 and rFace1 reference each other as
// neighbours
if (rFace0._aulNeighbours[side0] != f1 || rFace1._aulNeighbours[side1] != f0) {
inds.push_back(f0);
inds.push_back(f1);
}
}
else if (count == 1) {
const MeshFacet& rFace = rclFAry[f0];
unsigned short side = rFace.Side(p0, p1);
// should be "open edge" but isn't marked as such
if (rFace._aulNeighbours[side] != FACET_INDEX_MAX) {
inds.push_back(f0);
}
}
p0 = pE->p0;
p1 = pE->p1;
f0 = pE->f;
count = 1;
}
}
// remove duplicates
std::sort(inds.begin(), inds.end());
inds.erase(std::unique(inds.begin(), inds.end()), inds.end());
return inds;
}
bool MeshFixNeighbourhood::Fixup()
{
_rclMesh.RebuildNeighbours();
return true;
}
void MeshKernel::RebuildNeighbours(FacetIndex index)
{
std::vector<Edge_Index> edges;
edges.reserve(3 * (this->_aclFacetArray.size() - index));
// build up an array of edges
auto pB = this->_aclFacetArray.begin();
for (auto pI = pB + index; pI != this->_aclFacetArray.end(); ++pI) {
for (int i = 0; i < 3; i++) {
Edge_Index item {};
item.p0 = std::min<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.p1 = std::max<PointIndex>(pI->_aulPoints[i], pI->_aulPoints[(i + 1) % 3]);
item.f = pI - pB;
edges.push_back(item);
}
}
// sort the edges
// std::sort(edges.begin(), edges.end(), Edge_Less());
int threads = int(std::thread::hardware_concurrency());
MeshCore::parallel_sort(edges.begin(), edges.end(), Edge_Less(), threads);
PointIndex p0 = POINT_INDEX_MAX, p1 = POINT_INDEX_MAX;
PointIndex f0 = FACET_INDEX_MAX, f1 = FACET_INDEX_MAX;
int count = 0;
std::vector<Edge_Index>::iterator pE;
for (pE = edges.begin(); pE != edges.end(); ++pE) {
if (p0 == pE->p0 && p1 == pE->p1) {
f1 = pE->f;
count++;
}
else {
// we handle only the cases for 1 and 2, for all higher
// values we have a non-manifold that is ignored here
if (count == 2) {
MeshFacet& rFace0 = this->_aclFacetArray[f0];
MeshFacet& rFace1 = this->_aclFacetArray[f1];
unsigned short side0 = rFace0.Side(p0, p1);
unsigned short side1 = rFace1.Side(p0, p1);
rFace0._aulNeighbours[side0] = f1;
rFace1._aulNeighbours[side1] = f0;
}
else if (count == 1) {
MeshFacet& rFace = this->_aclFacetArray[f0];
unsigned short side = rFace.Side(p0, p1);
rFace._aulNeighbours[side] = FACET_INDEX_MAX;
}
p0 = pE->p0;
p1 = pE->p1;
f0 = pE->f;
count = 1;
}
}
// we handle only the cases for 1 and 2, for all higher
// values we have a non-manifold that is ignored here
if (count == 2) {
MeshFacet& rFace0 = this->_aclFacetArray[f0];
MeshFacet& rFace1 = this->_aclFacetArray[f1];
unsigned short side0 = rFace0.Side(p0, p1);
unsigned short side1 = rFace1.Side(p0, p1);
rFace0._aulNeighbours[side0] = f1;
rFace1._aulNeighbours[side1] = f0;
}
else if (count == 1) {
MeshFacet& rFace = this->_aclFacetArray[f0];
unsigned short side = rFace.Side(p0, p1);
rFace._aulNeighbours[side] = FACET_INDEX_MAX;
}
}
void MeshKernel::RebuildNeighbours()
{
// complete rebuild
RebuildNeighbours(0);
}
// ----------------------------------------------------------------
MeshEigensystem::MeshEigensystem(const MeshKernel& rclB)
: MeshEvaluation(rclB)
, _cU(1.0F, 0.0F, 0.0F)
, _cV(0.0F, 1.0F, 0.0F)
, _cW(0.0F, 0.0F, 1.0F)
{
// use the values of world coordinates as default
Base::BoundBox3f box = _rclMesh.GetBoundBox();
_fU = box.LengthX();
_fV = box.LengthY();
_fW = box.LengthZ();
}
Base::Matrix4D MeshEigensystem::Transform() const
{
// x,y,c ... vectors
// R,Q ... matrices (R is orthonormal so its transposed(=inverse) is equal to Q)
//
// from local (x) to world (y,c) coordinates we have the equation
// y = R * x + c
// <==>
// x = Q * y - Q * c
Base::Matrix4D clTMat;
// rotation part
clTMat[0][0] = double(_cU.x);
clTMat[0][1] = double(_cU.y);
clTMat[0][2] = double(_cU.z);
clTMat[0][3] = 0.0;
clTMat[1][0] = double(_cV.x);
clTMat[1][1] = double(_cV.y);
clTMat[1][2] = double(_cV.z);
clTMat[1][3] = 0.0;
clTMat[2][0] = double(_cW.x);
clTMat[2][1] = double(_cW.y);
clTMat[2][2] = double(_cW.z);
clTMat[2][3] = 0.0;
clTMat[3][0] = 0.0;
clTMat[3][1] = 0.0;
clTMat[3][2] = 0.0;
clTMat[3][3] = 1.0;
Base::Vector3f c(_cC);
c = clTMat * c;
// translation part
clTMat[0][3] = double(-c.x);
clTMat[1][3] = double(-c.y);
clTMat[2][3] = double(-c.z);
return clTMat;
}
bool MeshEigensystem::Evaluate()
{
CalculateLocalSystem();
float xmin = 0.0F, xmax = 0.0F, ymin = 0.0F, ymax = 0.0F, zmin = 0.0F, zmax = 0.0F;
Base::Vector3f clVect, clProj;
float fH {};
const MeshPointArray& aclPoints = _rclMesh.GetPoints();
for (const auto& it : aclPoints) {
// u-direction
clVect = it - _cC;
clProj.ProjectToLine(clVect, _cU);
clVect = clVect + clProj;
fH = clVect.Length();
// point vectors in the same direction ?
if ((clVect * _cU) < 0.0F) {
fH = -fH;
}
xmax = std::max<float>(xmax, fH);
xmin = std::min<float>(xmin, fH);
// v-direction
clVect = it - _cC;
clProj.ProjectToLine(clVect, _cV);
clVect = clVect + clProj;
fH = clVect.Length();
// point vectors in the same direction ?
if ((clVect * _cV) < 0.0F) {
fH = -fH;
}
ymax = std::max<float>(ymax, fH);
ymin = std::min<float>(ymin, fH);
// w-direction
clVect = it - _cC;
clProj.ProjectToLine(clVect, _cW);
clVect = clVect + clProj;
fH = clVect.Length();
// point vectors in the same direction ?
if ((clVect * _cW) < 0.0F) {
fH = -fH;
}
zmax = std::max<float>(zmax, fH);
zmin = std::min<float>(zmin, fH);
}
_fU = xmax - xmin;
_fV = ymax - ymin;
_fW = zmax - zmin;
return false; // to call Fixup() if needed
}
Base::Vector3f MeshEigensystem::GetBoundings() const
{
return Base::Vector3f(_fU, _fV, _fW);
}
void MeshEigensystem::CalculateLocalSystem()
{
// at least one facet is needed
if (_rclMesh.CountFacets() < 1) {
return; // cannot continue calculation
}
const MeshPointArray& aclPoints = _rclMesh.GetPoints();
MeshPointArray::_TConstIterator it;
PlaneFit planeFit;
for (it = aclPoints.begin(); it != aclPoints.end(); ++it) {
planeFit.AddPoint(*it);
}
planeFit.Fit();
_cC = planeFit.GetBase();
_cU = planeFit.GetDirU();
_cV = planeFit.GetDirV();
_cW = planeFit.GetNormal();
// set the sign for the vectors
float fSumU {0.0F}, fSumV {0.0F}, fSumW {0.0F};
for (it = aclPoints.begin(); it != aclPoints.end(); ++it) {
float fU = _cU * (*it - _cC);
float fV = _cV * (*it - _cC);
float fW = _cW * (*it - _cC);
fSumU += (fU > 0 ? fU * fU : -fU * fU);
fSumV += (fV > 0 ? fV * fV : -fV * fV);
fSumW += (fW > 0 ? fW * fW : -fW * fW);
}
// avoid ambiguities concerning directions
if (fSumU < 0.0F) {
_cU *= -1.0F;
}
if (fSumV < 0.0F) {
_cV *= -1.0F;
}
if (fSumW < 0.0F) {
_cW *= -1.0F;
}
if ((_cU % _cV) * _cW < 0.0F) {
_cW = -_cW; // make a right-handed system
}
}
|