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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
/** @file vtkLoopBooleanPolyDataFilter.cxx
* @brief This is the filter to perform boolean operations
* @author Adam Updegrove
* @author updega2@gmail.com
*/
#include "vtkLoopBooleanPolyDataFilter.h"
#include "vtkAppendPolyData.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCleanPolyData.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkGenericCell.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntersectionPolyDataFilter.h"
#include "vtkMath.h"
#include "vtkMergeCells.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointLocator.h"
#include "vtkPolyDataNormals.h"
#include "vtkSmartPointer.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkTriangle.h"
#include "vtkUnstructuredGrid.h"
#include <iostream>
#include <list>
#include <sstream>
#include <string>
//------------------------------------------------------------------------------
// Helper typedefs and data structures.
VTK_ABI_NAMESPACE_BEGIN
namespace
{
struct simLine
{
vtkIdType id;
vtkIdType pt1;
vtkIdType pt2;
};
struct simLoop
{
std::list<simLine> cells;
vtkIdType startPt;
vtkIdType endPt;
int loopType;
};
}
// Implementation function
class vtkLoopBooleanPolyDataFilter::Impl
{
public:
Impl();
virtual ~Impl();
void Initialize();
void SetCheckArrays();
void SetBoundaryArrays();
void ResetCheckArrays();
void GetBooleanRegions(int inputIndex, std::vector<simLoop>* loops);
void DetermineIntersection(std::vector<simLoop>* loops);
void PerformBoolean(vtkPolyData* output, int booleanOperation);
void ThresholdRegions(vtkPolyData** surfaces);
protected:
int RunLoopFind(vtkIdType interPt, vtkIdType nextCell, bool* usedPt, simLoop* loop);
int RunLoopTest(vtkIdType interPt, vtkIdType nextCell, simLoop* loop, bool* usedPt);
int GetCellOrientation(vtkPolyData* pd, vtkIdType cellId, vtkIdType p0, vtkIdType p1, int index);
int FindRegion(int inputIndex, int fillnumber, int start, int fill);
int FindRegionTipToe(int inputIndex, int fillnumber, int fill);
public:
int IntersectionCase;
vtkPolyData* Mesh[2];
vtkPolyData* IntersectionLines;
vtkIntArray* BoundaryPointArray[2];
vtkIntArray* BoundaryCellArray[2];
vtkIntArray* BooleanArray[2];
vtkIntArray* NewCellIds[2];
vtkIdType* Checked[2];
vtkIdType* CheckedCarefully[2];
vtkIdType* PointMapper[2];
vtkIdType* ReversePointMapper[2];
vtkIdList* CheckCells;
vtkIdList* CheckCells2;
vtkIdList* CheckCellsCareful;
vtkIdList* CheckCellsCareful2;
// Pointer to overarching filter
vtkLoopBooleanPolyDataFilter* ParentFilter;
};
vtkLoopBooleanPolyDataFilter::Impl::Impl()
: CheckCells(nullptr)
, CheckCells2(nullptr)
, CheckCellsCareful(nullptr)
, CheckCellsCareful2(nullptr)
{
for (int i = 0; i < 2; i++)
{
this->Mesh[i] = vtkPolyData::New();
this->BooleanArray[i] = vtkIntArray::New();
this->BoundaryPointArray[i] = vtkIntArray::New();
this->BoundaryCellArray[i] = vtkIntArray::New();
this->NewCellIds[i] = vtkIntArray::New();
this->Checked[i] = nullptr;
this->CheckedCarefully[i] = nullptr;
this->PointMapper[i] = nullptr;
this->ReversePointMapper[i] = nullptr;
}
this->IntersectionLines = vtkPolyData::New();
this->CheckCells = vtkIdList::New();
this->CheckCells2 = vtkIdList::New();
this->CheckCellsCareful = vtkIdList::New();
this->CheckCellsCareful2 = vtkIdList::New();
// Intersection Case:
// 0 -> Only hard closed intersection loops
// 1 -> At least one soft closed intersection loop
// 2 -> At least one open intersection loop
this->IntersectionCase = 0;
}
vtkLoopBooleanPolyDataFilter::Impl::~Impl()
{
for (int i = 0; i < 2; i++)
{
this->Mesh[i]->Delete();
this->BooleanArray[i]->Delete();
this->BoundaryPointArray[i]->Delete();
this->BoundaryCellArray[i]->Delete();
this->NewCellIds[i]->Delete();
delete[] this->Checked[i];
delete[] this->CheckedCarefully[i];
delete[] this->PointMapper[i];
delete[] this->ReversePointMapper[i];
}
this->IntersectionLines->Delete();
this->CheckCells->Delete();
this->CheckCells2->Delete();
this->CheckCellsCareful->Delete();
this->CheckCellsCareful2->Delete();
}
// Flood fill algorithm to find region of mesh separated by intersection lines
int vtkLoopBooleanPolyDataFilter::Impl::FindRegion(
int inputIndex, int fillnumber, int start, int fill)
{
vtkDebugWithObjectMacro(this->ParentFilter, << "Finding region with fill " << fillnumber
<< " of mesh " << inputIndex << " with cellID "
<< this->CheckCells->GetId(0));
// Id List to store neighbor cells for each set of nodes and a cell
vtkSmartPointer<vtkIdList> neighbors = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> tmp = vtkSmartPointer<vtkIdList>::New();
vtkIdType numCheckCells;
// Get neighboring cell for each pair of points in current cell
// While there are still cells to be checked, find neighbor cells
while ((numCheckCells = this->CheckCells->GetNumberOfIds()) > 0)
{
for (int c = 0; c < numCheckCells; c++)
{
vtkIdType cellId = this->CheckCells->GetId(c);
// Get the three points of the cell
const vtkIdType* pts = nullptr;
vtkIdType npts = 0;
this->Mesh[inputIndex]->GetCellPoints(cellId, npts, pts);
if (this->Checked[inputIndex][cellId] == 0)
{
// Mark cell as checked and insert the fillnumber value to cell
if (fill)
{
this->BooleanArray[inputIndex]->InsertValue(cellId, fillnumber);
}
this->Checked[inputIndex][cellId] = 1;
for (int i = 0; i < npts; i++)
{
vtkIdType p1 = pts[i];
// Get the cells attached to each point
this->Mesh[inputIndex]->GetPointCells(p1, neighbors);
vtkIdType numNeighbors = neighbors->GetNumberOfIds();
// For each neighboring cell
for (int j = 0; j < numNeighbors; j++)
{
// If this cell is close to a boundary
if (this->BoundaryCellArray[inputIndex]->GetValue(neighbors->GetId(j)))
{
// If this cell hasn't been checked already
if (this->CheckedCarefully[inputIndex][neighbors->GetId(j)] == 0)
{
// Add this cell to the careful check cells list and run
// the region finding tip toe code
this->CheckCellsCareful->InsertNextId(neighbors->GetId(j));
if (fill)
{
this->FindRegionTipToe(inputIndex, fillnumber, 1);
}
else
{
this->FindRegionTipToe(inputIndex, fillnumber, 0);
}
this->CheckCellsCareful->Reset();
this->CheckCellsCareful2->Reset();
}
}
// Cell needs to be added to check list
else
{
this->CheckCells2->InsertNextId(neighbors->GetId(j));
}
}
}
}
// This statement is for if the start cell is a boundary cell
else if (this->CheckedCarefully[inputIndex][cellId] == 0 && start)
{
start = 0;
this->CheckCells->Reset();
this->CheckCellsCareful->InsertNextId(cellId);
if (fill)
{
this->FindRegionTipToe(inputIndex, fillnumber, 1);
}
else
{
this->FindRegionTipToe(inputIndex, fillnumber, 0);
}
}
}
// Swap the current check list to the full check list and continue
tmp = this->CheckCells;
this->CheckCells = this->CheckCells2;
this->CheckCells2 = tmp;
tmp->Reset();
}
return 1;
}
// This is the slow version of the flood fill algorithm that is initiated
// when we get close to a boundary to ensure we don't cross the line
int vtkLoopBooleanPolyDataFilter::Impl::FindRegionTipToe(int inputIndex, int fillnumber, int fill)
{
// Id List to store neighbor cells for each set of nodes and a cell
vtkSmartPointer<vtkIdList> tmp = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> neighborIds = vtkSmartPointer<vtkIdList>::New();
vtkIdType numCheckCells;
// Get neighboring cell for each pair of points in current cell
// While there are still cells to be checked
while ((numCheckCells = this->CheckCellsCareful->GetNumberOfIds()) > 0)
{
for (int c = 0; c < numCheckCells; c++)
{
neighborIds->Reset();
vtkIdType cellId = this->CheckCellsCareful->GetId(c);
// Get the three points of the cell
const vtkIdType* pts = nullptr;
vtkIdType npts = 0;
this->Mesh[inputIndex]->GetCellPoints(cellId, npts, pts);
// Update this cell to have been checked carefully and assign it
// with the fillnumber scalar
if (this->CheckedCarefully[inputIndex][cellId] == 0)
{
if (fill)
{
this->BooleanArray[inputIndex]->InsertValue(cellId, fillnumber);
}
this->CheckedCarefully[inputIndex][cellId] = 1;
// For each edge of the cell
vtkDebugWithObjectMacro(this->ParentFilter, << "Checking edges of cell " << cellId);
for (int i = 0; i < npts; i++)
{
vtkIdType p1 = pts[i];
vtkIdType p2 = pts[(i + 1) % (npts)];
vtkSmartPointer<vtkIdList> neighbors = vtkSmartPointer<vtkIdList>::New();
// Initial check to make sure the cell is in fact a face cell
this->Mesh[inputIndex]->GetCellEdgeNeighbors(cellId, p1, p2, neighbors);
vtkIdType numNeighbors = neighbors->GetNumberOfIds();
// Check to make sure it is an outside surface cell,
// i.e. one neighbor
if (numNeighbors == 1)
{
int count = 0;
// Check to see if cell is on the boundary,
// if it is get adjacent lines
if (this->BoundaryPointArray[inputIndex]->GetValue(p1) == 1)
{
count++;
}
if (this->BoundaryPointArray[inputIndex]->GetValue(p2) == 1)
{
count++;
}
vtkIdType neighbor = neighbors->GetId(0);
// if cell is not on the boundary, add new cell to check list
if (count < 2)
{
neighborIds->InsertNextId(neighbor);
}
// if cell is on boundary, check to make sure it isn't
// false positive; don't add to check list. This is done by
// getting the boundary lines attached to each point, then
// intersecting the two lists. If the result is zero, then this
// is a false positive
else
{
// Variables for the boundary cells adjacent to the boundary point
vtkSmartPointer<vtkIdList> bLinesOne = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> bLinesTwo = vtkSmartPointer<vtkIdList>::New();
vtkIdType bPt1 = PointMapper[inputIndex][p1];
this->IntersectionLines->GetPointCells(bPt1, bLinesOne);
vtkIdType bPt2 = PointMapper[inputIndex][p2];
this->IntersectionLines->GetPointCells(bPt2, bLinesTwo);
bLinesOne->IntersectWith(bLinesTwo);
// Cell is false positive. Add to check list.
if (bLinesOne->GetNumberOfIds() == 0)
{
vtkDebugWithObjectMacro(this->ParentFilter, << "False positive! " << neighbor);
neighborIds->InsertNextId(neighbor);
}
else
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "I have not been added because false");
}
}
}
else
{
vtkDebugWithObjectMacro(this->ParentFilter, << "NumNei is not 1");
vtkDebugWithObjectMacro(this->ParentFilter, << "Number of Neighbors " << numNeighbors);
vtkDebugWithObjectMacro(this->ParentFilter, << "Cell is " << cellId);
for (int k = 0; k < numNeighbors; k++)
{
vtkDebugWithObjectMacro(this->ParentFilter, << "Id!!! " << neighbors->GetId(k));
}
}
}
vtkIdType numIds = neighborIds->GetNumberOfIds();
if (numIds > 0)
{
// Add all Ids in current list to global list of Ids
for (int k = 0; k < numIds; k++)
{
vtkIdType neighborId = neighborIds->GetId(k);
if (this->CheckedCarefully[inputIndex][neighborId] == 0)
{
this->CheckCellsCareful2->InsertNextId(neighborId);
}
else if (this->Checked[inputIndex][neighborId] == 0)
{
this->CheckCells2->InsertNextId(neighborId);
}
}
}
}
}
// Add current list of checked cells to the full list and continue
tmp = this->CheckCellsCareful;
this->CheckCellsCareful = this->CheckCellsCareful2;
this->CheckCellsCareful2 = tmp;
tmp->Reset();
}
return 1;
}
void vtkLoopBooleanPolyDataFilter::Impl::Initialize()
{
for (int i = 0; i < 2; i++)
{
if (this->Mesh[i]->GetNumberOfPoints() == 0 || this->Mesh[i]->GetNumberOfCells() == 0)
{
vtkGenericWarningMacro(<< "Mesh has zero points or cells and "
<< "cannot run filter");
return;
}
// Get the number of Polys for scalar allocation
int numPolys = this->Mesh[i]->GetNumberOfPolys();
int numPts = this->Mesh[i]->GetNumberOfPoints();
int numLinePts = this->IntersectionLines->GetNumberOfPoints();
// Allocate space for each Boundary Array and the fill array
this->BoundaryPointArray[i]->SetNumberOfTuples(numPts);
this->BoundaryCellArray[i]->SetNumberOfTuples(numPolys);
this->BooleanArray[i]->SetNumberOfTuples(numPolys);
this->Checked[i] = new vtkIdType[numPolys];
this->CheckedCarefully[i] = new vtkIdType[numPolys];
this->PointMapper[i] = new vtkIdType[numPolys];
this->ReversePointMapper[i] = new vtkIdType[numLinePts];
for (int j = 0; j < numPts; j++)
{
this->BoundaryPointArray[i]->InsertValue(j, 0);
}
for (int j = 0; j < numPolys; j++)
{
this->BoundaryCellArray[i]->InsertValue(j, 0);
this->BooleanArray[i]->InsertValue(j, 0);
this->Checked[i][j] = 0;
this->CheckedCarefully[i][j] = 0;
this->PointMapper[i][j] = -1;
}
for (int j = 0; j < numLinePts; j++)
{
this->ReversePointMapper[i][j] = -1;
}
}
this->NewCellIds[0]->DeepCopy(this->IntersectionLines->GetCellData()->GetArray("NewCell0ID"));
this->NewCellIds[1]->DeepCopy(this->IntersectionLines->GetCellData()->GetArray("NewCell1ID"));
this->BooleanArray[0]->SetName("BooleanRegion");
this->BooleanArray[1]->SetName("BooleanRegion");
this->Mesh[0]->GetCellData()->AddArray(this->BooleanArray[0]);
this->Mesh[0]->GetCellData()->SetActiveScalars("BooleanRegion");
this->Mesh[1]->GetCellData()->AddArray(this->BooleanArray[1]);
this->Mesh[1]->GetCellData()->SetActiveScalars("BooleanRegion");
this->BoundaryCellArray[0]->SetName("BoundaryCells");
this->BoundaryCellArray[1]->SetName("BoundaryCells");
this->Mesh[0]->GetCellData()->AddArray(this->BoundaryCellArray[0]);
this->Mesh[0]->GetCellData()->SetActiveScalars("BoundaryCells");
this->Mesh[1]->GetCellData()->AddArray(this->BoundaryCellArray[1]);
this->Mesh[1]->GetCellData()->SetActiveScalars("BoundaryCells");
this->BoundaryPointArray[0]->SetName("BoundaryPoints");
this->BoundaryPointArray[1]->SetName("BoundaryPoints");
this->Mesh[0]->GetPointData()->AddArray(this->BoundaryPointArray[0]);
this->Mesh[0]->GetPointData()->SetActiveScalars("BoundaryPoints");
this->Mesh[1]->GetPointData()->AddArray(this->BoundaryPointArray[1]);
this->Mesh[1]->GetPointData()->SetActiveScalars("BoundaryPoints");
}
// Function to find the regions on each input separated by the intersection
// lines
void vtkLoopBooleanPolyDataFilter::Impl::GetBooleanRegions(
int inputIndex, std::vector<simLoop>* loops)
{
vtkSmartPointer<vtkPolyData> tmpPolyData = vtkSmartPointer<vtkPolyData>::New();
tmpPolyData->DeepCopy(this->Mesh[inputIndex]);
tmpPolyData->BuildLinks();
std::vector<simLoop>::iterator loopit;
std::list<simLine>::iterator cellit;
// For each intersection loop
for (loopit = loops->begin(); loopit != loops->end(); ++loopit)
{
std::list<simLine> loopcells = (loopit)->cells;
// Go through each cell in the loop
for (cellit = loopcells.begin(); cellit != loopcells.end(); ++cellit)
{
simLine nextLine;
nextLine = *cellit;
vtkIdType nextCell = nextLine.id;
vtkIdType p1 = nextLine.pt1;
vtkIdType p2 = nextLine.pt2;
vtkIdType outputCellId0 = this->NewCellIds[inputIndex]->GetComponent(nextCell, 0);
vtkIdType outputCellId1 = this->NewCellIds[inputIndex]->GetComponent(nextCell, 1);
// If the cell has not been given an orientation from the flood fill
// algorithm and it has an id from vtkIntersectionPolyDataFilter
if (outputCellId0 != -1)
{
// If the cell hasn't been touched
if (this->CheckedCarefully[inputIndex][outputCellId0] == 0)
{
int sign1 = this->GetCellOrientation(tmpPolyData, outputCellId0, p1, p2, inputIndex);
// If cell orientation is found
if (sign1 != 0)
{
this->CheckCells->InsertNextId(outputCellId0);
this->FindRegion(inputIndex, sign1, 1, 1);
this->CheckCells->Reset();
this->CheckCells2->Reset();
this->CheckCellsCareful->Reset();
this->CheckCellsCareful2->Reset();
}
}
}
// Check cell on other side of intersection line
if (outputCellId1 != -1)
{
// If the cell hasn't been touched
if (this->CheckedCarefully[inputIndex][outputCellId1] == 0)
{
int sign2 = this->GetCellOrientation(tmpPolyData, outputCellId1, p1, p2, inputIndex);
// If cell orientation is found
if (sign2 != 0)
{
this->CheckCells->InsertNextId(outputCellId1);
this->FindRegion(inputIndex, sign2, 1, 1);
this->CheckCells->Reset();
this->CheckCells2->Reset();
this->CheckCellsCareful->Reset();
this->CheckCellsCareful2->Reset();
}
}
}
}
}
}
// Get Cell orientation so we know which value to flood fill a region with
int vtkLoopBooleanPolyDataFilter::Impl::GetCellOrientation(
vtkPolyData* pd, vtkIdType cellId, vtkIdType p0, vtkIdType p1, int index)
{
vtkDebugWithObjectMacro(this->ParentFilter, << "CellId: " << cellId);
vtkIdType npts;
const vtkIdType* pts;
pd->BuildLinks();
pd->GetCellPoints(cellId, npts, pts);
// pt0Id and pt1Id are from intersectionLines PolyData and I am trying
// to compare these to the point ids in pd.
vtkIdType cellPtId0 = this->ReversePointMapper[index][p0];
vtkIdType cellPtId1 = this->ReversePointMapper[index][p1];
double points[3][3];
vtkIdType cellPtId2 = 0;
for (int j = 0; j < npts; j++)
{
pd->GetPoint(pts[j], points[j]);
if (cellPtId0 != pts[j] && cellPtId1 != pts[j])
{
cellPtId2 = pts[j];
}
}
vtkSmartPointer<vtkPoints> cellPts = vtkSmartPointer<vtkPoints>::New();
cellPts->InsertNextPoint(pd->GetPoint(cellPtId0));
cellPts->InsertNextPoint(pd->GetPoint(cellPtId1));
cellPts->InsertNextPoint(pd->GetPoint(cellPtId2));
vtkSmartPointer<vtkPolyData> cellPD = vtkSmartPointer<vtkPolyData>::New();
cellPD->SetPoints(cellPts);
vtkSmartPointer<vtkCellArray> cellLines = vtkSmartPointer<vtkCellArray>::New();
for (int j = 0; j < npts; j++)
{
int spot1 = j;
int spot2 = (j + 1) % 3;
cellLines->InsertNextCell(2);
cellLines->InsertCellPoint(spot1);
cellLines->InsertCellPoint(spot2);
}
cellPD->SetLines(cellLines);
// Set up a transform that will rotate the points to the
// XY-plane (normal aligned with z-axis).
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
double zaxis[3] = { 0.0, 0.0, 1.0 };
double rotationAxis[3], normal[3], center[3], rotationAngle;
vtkTriangle::ComputeNormal(points[0], points[1], points[2], normal);
double dotZAxis = vtkMath::Dot(normal, zaxis);
if (fabs(1.0 - dotZAxis) < 1e-6)
{
// Aligned with z-axis
rotationAxis[0] = 1.0;
rotationAxis[1] = 0.0;
rotationAxis[2] = 0.0;
rotationAngle = 0.0;
}
else if (fabs(1.0 + dotZAxis) < 1e-6)
{
// Co-linear with z-axis, but reversed sense.
// Aligned with z-axis
rotationAxis[0] = 1.0;
rotationAxis[1] = 0.0;
rotationAxis[2] = 0.0;
rotationAngle = 180.0;
}
else
{
// The general case
vtkMath::Cross(normal, zaxis, rotationAxis);
vtkMath::Normalize(rotationAxis);
rotationAngle = vtkMath::DegreesFromRadians(acos(vtkMath::Dot(zaxis, normal)));
}
transform->PreMultiply();
transform->Identity();
vtkDebugWithObjectMacro(this->ParentFilter, << "ROTATION ANGLE " << rotationAngle);
transform->RotateWXYZ(rotationAngle, rotationAxis[0], rotationAxis[1], rotationAxis[2]);
vtkTriangle::TriangleCenter(points[0], points[1], points[2], center);
transform->Translate(-center[0], -center[1], -center[2]);
vtkSmartPointer<vtkTransformPolyDataFilter> transformer =
vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transformer->SetInputData(cellPD);
transformer->SetTransform(transform);
transformer->SetContainerAlgorithm(this->ParentFilter);
transformer->Update();
vtkSmartPointer<vtkPolyData> transPD = vtkSmartPointer<vtkPolyData>::New();
transPD = transformer->GetOutput();
transPD->BuildLinks();
double area = 0;
double tedgept1[3];
double tedgept2[3];
vtkIdType newpt;
for (newpt = 0; newpt < transPD->GetNumberOfPoints() - 1; newpt++)
{
transPD->GetPoint(newpt, tedgept1);
transPD->GetPoint(newpt + 1, tedgept2);
area = area + (tedgept1[0] * tedgept2[1]) - (tedgept2[0] * tedgept1[1]);
}
transPD->GetPoint(newpt, tedgept1);
transPD->GetPoint(0, tedgept2);
area = area + (tedgept1[0] * tedgept2[1]) - (tedgept2[0] * tedgept1[1]);
int value = 0;
double tolerance = 1e-6;
if (area < 0 && fabs(area) > tolerance)
{
value = -1;
}
else if (area > 0 && fabs(area) > tolerance)
{
value = 1;
}
else
{
vtkDebugWithObjectMacro(this->ParentFilter, << "Line pts are " << p0 << " and " << p1);
vtkDebugWithObjectMacro(
this->ParentFilter, << "PD pts are " << cellPtId0 << " and " << cellPtId1);
value = 0;
}
return value;
}
// Reset the find region arrays to test another region
void vtkLoopBooleanPolyDataFilter::Impl::ResetCheckArrays()
{
for (int i = 0; i < 2; i++)
{
int numPolys = this->Mesh[i]->GetNumberOfCells();
for (vtkIdType cellId = 0; cellId < numPolys; cellId++)
{
if (this->BoundaryCellArray[i]->GetValue(cellId) == 1)
{
this->Checked[i][cellId] = 1;
this->CheckedCarefully[i][cellId] = 0;
}
else
{
this->Checked[i][cellId] = 0;
this->CheckedCarefully[i][cellId] = 1;
}
}
}
}
// Set the boundary arrays on the mesh
void vtkLoopBooleanPolyDataFilter::Impl::SetBoundaryArrays()
{
// Variables used in the function
// Point locator to find points on mesh that are the points on the boundary
// lines
vtkSmartPointer<vtkPointLocator> pointLocator1 = vtkSmartPointer<vtkPointLocator>::New();
vtkSmartPointer<vtkPointLocator> pointLocator2 = vtkSmartPointer<vtkPointLocator>::New();
pointLocator1->SetDataSet(this->Mesh[0]);
pointLocator1->BuildLocator();
pointLocator2->SetDataSet(this->Mesh[1]);
pointLocator2->BuildLocator();
int numPoints = this->IntersectionLines->GetNumberOfPoints();
for (vtkIdType pointId = 0; pointId < numPoints; pointId++)
{
double pt[3];
this->IntersectionLines->GetPoint(pointId, pt);
// Find point on mesh
vtkIdType bp1 = pointLocator1->FindClosestPoint(pt);
this->PointMapper[0][bp1] = pointId;
this->ReversePointMapper[0][pointId] = bp1;
this->BoundaryPointArray[0]->InsertValue(bp1, 1);
vtkSmartPointer<vtkIdList> bpCellIds1 = vtkSmartPointer<vtkIdList>::New();
this->Mesh[0]->GetPointCells(bp1, bpCellIds1);
// Set the point mapping array
// Assign each cell attached to this point as a boundary cell
for (int i = 0; i < bpCellIds1->GetNumberOfIds(); i++)
{
this->BoundaryCellArray[0]->InsertValue(bpCellIds1->GetId(i), 1);
this->Checked[0][bpCellIds1->GetId(i)] = 1;
}
vtkIdType bp2 = pointLocator2->FindClosestPoint(pt);
this->PointMapper[1][bp2] = pointId;
this->ReversePointMapper[1][pointId] = bp2;
this->BoundaryPointArray[1]->InsertValue(bp2, 1);
vtkSmartPointer<vtkIdList> bpCellIds2 = vtkSmartPointer<vtkIdList>::New();
this->Mesh[1]->GetPointCells(bp2, bpCellIds2);
// Set the point mapping array
// Assign each cell attached to this point as a boundary cell
for (int i = 0; i < bpCellIds2->GetNumberOfIds(); i++)
{
this->BoundaryCellArray[1]->InsertValue(bpCellIds2->GetId(i), 1);
this->Checked[1][bpCellIds2->GetId(i)] = 1;
}
}
}
// Set the original finding region check arrays
void vtkLoopBooleanPolyDataFilter::Impl::SetCheckArrays()
{
for (int i = 0; i < 2; i++)
{
// Get the number of Polys for scalar allocation
int numPolys = this->Mesh[i]->GetNumberOfPolys();
for (int j = 0; j < numPolys; j++)
{
if (this->Checked[i][j] == 0)
{
this->CheckedCarefully[i][j] = 1;
}
else
{
this->CheckedCarefully[i][j] = 0;
}
}
}
}
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkLoopBooleanPolyDataFilter);
//------------------------------------------------------------------------------
vtkLoopBooleanPolyDataFilter::vtkLoopBooleanPolyDataFilter()
{
this->Operation = VTK_UNION;
this->SetNumberOfInputPorts(2);
this->SetNumberOfOutputPorts(2);
this->NoIntersectionOutput = 1;
this->NumberOfIntersectionPoints = 0;
this->NumberOfIntersectionLines = 0;
this->Status = 1;
this->Tolerance = 1e-6;
}
//------------------------------------------------------------------------------
vtkLoopBooleanPolyDataFilter::~vtkLoopBooleanPolyDataFilter() = default;
//------------------------------------------------------------------------------
int vtkLoopBooleanPolyDataFilter::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
vtkInformation* inInfo0 = inputVector[0]->GetInformationObject(0);
vtkInformation* inInfo1 = inputVector[1]->GetInformationObject(0);
vtkInformation* outInfo0 = outputVector->GetInformationObject(0);
vtkInformation* outInfo1 = outputVector->GetInformationObject(1);
if (!inInfo0 || !inInfo1 || !outInfo0 || !outInfo1)
{
this->Status = 0;
return 0;
}
vtkPolyData* input0 = vtkPolyData::SafeDownCast(inInfo0->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData* input1 = vtkPolyData::SafeDownCast(inInfo1->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData* outputSurface =
vtkPolyData::SafeDownCast(outInfo0->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData* outputIntersection =
vtkPolyData::SafeDownCast(outInfo1->Get(vtkDataObject::DATA_OBJECT()));
if (!input0 || !input1 || !outputSurface || !outputIntersection)
{
this->Status = 0;
return 0;
}
// Get intersected versions
vtkSmartPointer<vtkIntersectionPolyDataFilter> polydataIntersection =
vtkSmartPointer<vtkIntersectionPolyDataFilter>::New();
polydataIntersection->SetInputConnection(0, this->GetInputConnection(0, 0));
polydataIntersection->SetInputConnection(1, this->GetInputConnection(1, 0));
polydataIntersection->SplitFirstOutputOn();
polydataIntersection->SplitSecondOutputOn();
polydataIntersection->SetTolerance(this->Tolerance);
polydataIntersection->SetContainerAlgorithm(this);
polydataIntersection->Update();
if (this->CheckAbort() || polydataIntersection->GetStatus() != 1)
{
this->Status = 0;
return 0;
}
this->NumberOfIntersectionPoints = polydataIntersection->GetNumberOfIntersectionPoints();
this->NumberOfIntersectionLines = polydataIntersection->GetNumberOfIntersectionLines();
vtkDebugMacro(<< "Intersection is Done!!!");
vtkLoopBooleanPolyDataFilter::Impl* impl = new vtkLoopBooleanPolyDataFilter::Impl();
impl->ParentFilter = this;
impl->Mesh[0]->DeepCopy(polydataIntersection->GetOutput(1));
impl->Mesh[0]->BuildLinks();
impl->Mesh[1]->DeepCopy(polydataIntersection->GetOutput(2));
impl->Mesh[1]->BuildLinks();
impl->IntersectionLines->ShallowCopy(polydataIntersection->GetOutput(0));
if (this->NumberOfIntersectionPoints == 0 || this->NumberOfIntersectionLines == 0)
{
vtkWarningMacro(<< "No intersections!");
if (this->CheckAbort() || this->NoIntersectionOutput == 0)
{
delete impl;
return 1;
}
else
{
for (int i = 0; i < 2; i++)
{
// Get the number of Polys for scalar allocation
int numPolys = impl->Mesh[i]->GetNumberOfPolys();
int numPts = impl->Mesh[i]->GetNumberOfPoints();
for (int j = 0; j < numPts; j++)
{
impl->BoundaryPointArray[i]->InsertValue(j, 0);
}
for (int j = 0; j < numPolys; j++)
{
impl->BoundaryCellArray[i]->InsertValue(j, 0);
}
impl->BoundaryCellArray[i]->SetName("BoundaryCells");
impl->Mesh[i]->GetCellData()->AddArray(impl->BoundaryCellArray[i]);
impl->Mesh[i]->GetCellData()->SetActiveScalars("BoundaryCells");
impl->BoundaryPointArray[i]->SetName("BoundaryPoints");
impl->Mesh[i]->GetPointData()->AddArray(impl->BoundaryPointArray[i]);
impl->Mesh[i]->GetPointData()->SetActiveScalars("BoundaryPoints");
}
if (this->NoIntersectionOutput == 1)
{
vtkDebugMacro(<< "Only returning first surface");
outputSurface->DeepCopy(impl->Mesh[0]);
}
else if (this->NoIntersectionOutput == 2)
{
vtkDebugMacro(<< "Only returning second surface");
outputSurface->DeepCopy(impl->Mesh[1]);
}
else
{
vtkDebugMacro(<< "Keeping both");
vtkSmartPointer<vtkAppendPolyData> appender = vtkSmartPointer<vtkAppendPolyData>::New();
appender->AddInputData(impl->Mesh[0]);
appender->AddInputData(impl->Mesh[1]);
appender->SetContainerAlgorithm(this);
appender->Update();
outputSurface->DeepCopy(appender->GetOutput());
}
delete impl;
return 1;
}
}
double badtri1[2], badtri2[2];
double freeedge1[2], freeedge2[2];
impl->Mesh[0]->GetCellData()->GetArray("BadTriangle")->GetRange(badtri1, 0);
impl->Mesh[0]->GetCellData()->GetArray("FreeEdge")->GetRange(freeedge1, 0);
impl->Mesh[1]->GetCellData()->GetArray("BadTriangle")->GetRange(badtri2, 0);
impl->Mesh[1]->GetCellData()->GetArray("FreeEdge")->GetRange(freeedge2, 0);
// Set the check and boundary arrays for region finding
vtkDebugMacro(<< "Initializing");
impl->Initialize();
vtkDebugMacro(<< "Setting Bound Arrays");
impl->SetBoundaryArrays();
vtkDebugMacro(<< "Setting Check Arrays");
impl->SetCheckArrays();
// Determine the intersection type and obtain the intersection loops
// to give to Boolean Region finding
vtkDebugMacro(<< "Determining Intersection Type");
std::vector<simLoop> loops;
impl->DetermineIntersection(&loops);
// Get the regions bounded by the intersection lines and give correct
// orientation
impl->GetBooleanRegions(0, &loops);
vtkDebugMacro(<< "DONE WITH 1");
impl->GetBooleanRegions(1, &loops);
vtkDebugMacro(<< "DONE WITH 2");
// Combine certain orientations based on the operation desired
impl->PerformBoolean(outputSurface, this->Operation);
// Number of bad triangles and free edges (Should be zero for watertight,
// manifold surfaces!
vtkDebugMacro(<< "SURFACE 1 BAD TRI MIN: " << badtri1[0] << " MAX: " << badtri1[1]);
vtkDebugMacro(<< "SURFACE 1 FREE EDGE MIN: " << freeedge1[0] << " MAX: " << freeedge1[1]);
vtkDebugMacro(<< "SURFACE 2 BAD TRI MIN: " << badtri2[0] << " MAX: " << badtri2[1]);
vtkDebugMacro(<< "SURFACE 2 FREE EDGE MIN: " << freeedge2[0] << " MAX: " << freeedge2[1]);
double fullbadtri[2], fullfreeedge[2], dummy[2];
vtkIntersectionPolyDataFilter::CleanAndCheckSurface(outputSurface, dummy, this->Tolerance);
outputSurface->GetCellData()->GetArray("BadTriangle")->GetRange(fullbadtri, 0);
outputSurface->GetCellData()->GetArray("FreeEdge")->GetRange(fullfreeedge, 0);
// Add Normals
vtkSmartPointer<vtkPolyDataNormals> normaler = vtkSmartPointer<vtkPolyDataNormals>::New();
normaler->SetInputData(outputSurface);
normaler->AutoOrientNormalsOn();
normaler->SetContainerAlgorithm(this);
normaler->Update();
outputSurface->DeepCopy(normaler->GetOutput());
vtkDebugMacro(<< "FULL SURFACE BAD TRI MIN: " << fullbadtri[0]);
vtkDebugMacro(<< " MAX: " << fullbadtri[1]);
vtkDebugMacro(<< "FULL SURFACE FREE EDGE MIN: " << fullfreeedge[0]);
vtkDebugMacro(<< " MAX: " << fullfreeedge[1]);
delete impl;
return 1;
}
//------------------------------------------------------------------------------
void vtkLoopBooleanPolyDataFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Operation: ";
switch (this->Operation)
{
case VTK_UNION:
os << "UNION";
break;
case VTK_INTERSECTION:
os << "INTERSECTION";
break;
case VTK_DIFFERENCE:
os << "DIFFERENCE";
break;
}
os << "\n";
os << indent << "No Intersection Output: " << this->NoIntersectionOutput << "\n";
os << indent << "Tolerance: " << this->Tolerance << "\n";
os << indent << "NumberOfIntersectionPoints: " << this->NumberOfIntersectionPoints << "\n";
os << indent << "NumberOfIntersectionLines: " << this->NumberOfIntersectionLines << "\n";
}
//------------------------------------------------------------------------------
int vtkLoopBooleanPolyDataFilter::FillInputPortInformation(int port, vtkInformation* info)
{
if (!this->Superclass::FillInputPortInformation(port, info))
{
return 0;
}
if (port == 0)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
}
else if (port == 1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
info->Set(vtkAlgorithm::INPUT_IS_OPTIONAL(), 0);
}
return 1;
}
//------------------------------------------------------------------------------
// Determine type of intersection
void vtkLoopBooleanPolyDataFilter::Impl::DetermineIntersection(std::vector<simLoop>* loops)
{
int numInterPts = this->IntersectionLines->GetNumberOfPoints();
bool* usedPt;
usedPt = new bool[numInterPts];
for (vtkIdType interPt = 0; interPt < numInterPts; interPt++)
{
usedPt[interPt] = false;
}
for (vtkIdType interPt = 0; interPt < numInterPts; interPt++)
{
if (!usedPt[interPt])
{
simLoop newloop;
vtkSmartPointer<vtkIdList> cellIds = vtkSmartPointer<vtkIdList>::New();
this->IntersectionLines->GetPointCells(interPt, cellIds);
if (cellIds->GetNumberOfIds() > 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Cells is greater than 2 for first point " << interPt);
}
else if (cellIds->GetNumberOfIds() < 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Cells is less than 2 for point " << interPt);
}
vtkIdType nextCell = cellIds->GetId(0);
// Run through intersection lines to get loops!
newloop.startPt = interPt;
int caseId = 0;
caseId = this->RunLoopFind(interPt, nextCell, usedPt, &newloop);
if (caseId != -1)
{
// If the intersection loop is open
if (this->IntersectionCase == 2)
{
vtkIdType nextPt = caseId;
vtkDebugWithObjectMacro(this->ParentFilter, << "End point of open loop is " << nextPt);
newloop.endPt = nextPt;
newloop.loopType = 2;
nextCell = cellIds->GetId(1);
vtkIdType newId = this->RunLoopFind(interPt, nextCell, usedPt, &newloop);
newloop.startPt = newId;
// Save start and end point in custom data structure for loop
}
else
{
newloop.loopType = 1;
}
}
usedPt[interPt] = true;
loops->push_back(newloop);
}
}
vtkDebugWithObjectMacro(this->ParentFilter, << "Number Of Loops: " << loops->size());
delete[] usedPt;
}
// Function for if the intersection is soft closed or open
int vtkLoopBooleanPolyDataFilter::Impl::RunLoopFind(
vtkIdType interPt, vtkIdType nextCell, bool* usedPt, simLoop* loop)
{
vtkIdType prevPt = interPt;
vtkIdType nextPt = interPt;
vtkSmartPointer<vtkIdList> pointIds = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> cellIds = vtkSmartPointer<vtkIdList>::New();
IntersectionLines->GetCellPoints(nextCell, pointIds);
if (pointIds->GetNumberOfIds() > 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Points is greater than 2 for first cell " << nextCell);
}
else if (pointIds->GetNumberOfIds() < 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Points is less than 2 for first cell " << nextCell);
}
if (pointIds->GetId(0) == nextPt)
{
nextPt = pointIds->GetId(1);
}
else
{
nextPt = pointIds->GetId(0);
}
simLine newline;
newline.pt1 = prevPt;
newline.pt2 = nextPt;
newline.id = nextCell;
loop->cells.push_back(newline);
usedPt[nextPt] = true;
while (nextPt != loop->cells.front().pt1)
{
IntersectionLines->GetPointCells(nextPt, cellIds);
if (cellIds->GetNumberOfIds() > 2)
{
IntersectionCase = 1;
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Cells is greater than 2 for point " << nextPt);
usedPt[nextPt] = false;
nextCell = this->RunLoopTest(nextPt, nextCell, loop, usedPt);
if (nextCell == -1)
{
break;
}
vtkDebugWithObjectMacro(this->ParentFilter, << "Next cell is " << nextCell);
}
else if (cellIds->GetNumberOfIds() < 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Cells is less than 2 for point " << nextPt);
IntersectionCase = 2;
return nextPt;
}
else
{
if (cellIds->GetId(0) == nextCell)
{
nextCell = cellIds->GetId(1);
}
else
{
nextCell = cellIds->GetId(0);
}
}
IntersectionLines->GetCellPoints(nextCell, pointIds);
if (pointIds->GetNumberOfIds() > 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Points is greater than 2 for cell " << nextCell);
}
else if (pointIds->GetNumberOfIds() < 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Points is less than 2 for first cell " << nextCell);
}
prevPt = nextPt;
if (pointIds->GetId(0) == nextPt)
{
nextPt = pointIds->GetId(1);
}
else
{
nextPt = pointIds->GetId(0);
}
usedPt[nextPt] = true;
simLine newestline;
newestline.pt1 = prevPt;
newestline.pt2 = nextPt;
newestline.id = nextCell;
loop->cells.push_back(newestline);
}
loop->endPt = nextPt;
loop->loopType = 0;
vtkDebugWithObjectMacro(this->ParentFilter, << "Start and End Point are " << nextPt);
return -1;
}
// Tests an orientation in a specified region
int vtkLoopBooleanPolyDataFilter::Impl::RunLoopTest(
vtkIdType interPt, vtkIdType nextCell, simLoop* loop, bool* usedPt)
{
// This test is only if the intersection has soft closed loops
vtkDebugWithObjectMacro(this->ParentFilter, << "Running Loop Test to find right loop");
vtkIdType stopCell = nextCell;
vtkIdType prevPt = interPt;
vtkIdType nextPt = interPt;
vtkSmartPointer<vtkIdList> pointIds = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkPolyData> tmpPolyData = vtkSmartPointer<vtkPolyData>::New();
int input = 0;
tmpPolyData->DeepCopy(this->Mesh[input]);
tmpPolyData->BuildLinks();
std::list<simLine>::iterator cellit;
vtkSmartPointer<vtkIdList> cellIds = vtkSmartPointer<vtkIdList>::New();
IntersectionLines->GetPointCells(nextPt, cellIds);
vtkDebugWithObjectMacro(this->ParentFilter,
<< "Number of cells should be more than two!! " << cellIds->GetNumberOfIds());
for (vtkIdType i = 0; i < cellIds->GetNumberOfIds(); i++)
{
int numRegionsFound = 0;
vtkIdType cellId = cellIds->GetId(i);
vtkDebugWithObjectMacro(this->ParentFilter, << "Testing cell " << cellId);
IntersectionLines->GetCellPoints(cellId, pointIds);
if (pointIds->GetNumberOfIds() > 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Points is greater than 2 for first cell " << nextCell);
}
else if (pointIds->GetNumberOfIds() < 2)
{
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number Of Points is less than 2 for first cell " << nextCell);
}
if (pointIds->GetId(0) == interPt)
{
nextPt = pointIds->GetId(1);
}
else
{
nextPt = pointIds->GetId(0);
}
if (usedPt[nextPt])
{
vtkDebugWithObjectMacro(this->ParentFilter, << "Bad One");
}
if (cellId != stopCell && !usedPt[nextPt])
{
simLine newline;
newline.id = cellId;
newline.pt1 = prevPt;
newline.pt2 = nextPt;
loop->cells.push_back(newline);
vtkDebugWithObjectMacro(this->ParentFilter, << "Cell id is: " << cellId);
for (cellit = loop->cells.begin(); cellit != loop->cells.end(); ++cellit)
{
simLine nextLine;
nextLine = *cellit;
nextCell = nextLine.id;
vtkIdType p1 = nextLine.pt1;
vtkIdType p2 = nextLine.pt2;
vtkDebugWithObjectMacro(this->ParentFilter, << "Line cell is " << nextCell);
vtkIdType outputCellId0 = this->NewCellIds[input]->GetComponent(nextCell, 0);
vtkIdType outputCellId1 = this->NewCellIds[input]->GetComponent(nextCell, 1);
if (outputCellId0 != -1)
{
if (this->CheckedCarefully[input][outputCellId0] == 0)
{
int sign1 = this->GetCellOrientation(tmpPolyData, outputCellId0, p1, p2, input);
if (sign1 == -1)
{
numRegionsFound++;
this->CheckCells->InsertNextId(outputCellId0);
this->FindRegion(input, sign1, 1, 0);
this->CheckCells->Reset();
this->CheckCells2->Reset();
this->CheckCellsCareful->Reset();
this->CheckCellsCareful2->Reset();
}
}
}
if (outputCellId1 != -1)
{
if (this->CheckedCarefully[input][outputCellId1] == 0)
{
int sign2 = this->GetCellOrientation(tmpPolyData, outputCellId1, p1, p2, input);
if (sign2 == -1)
{
numRegionsFound++;
this->CheckCells->InsertNextId(outputCellId1);
this->FindRegion(input, sign2, 1, 0);
this->CheckCells->Reset();
this->CheckCells2->Reset();
this->CheckCellsCareful->Reset();
this->CheckCellsCareful2->Reset();
}
}
}
}
loop->cells.pop_back();
this->ResetCheckArrays();
vtkDebugWithObjectMacro(
this->ParentFilter, << "Number of Regions Found: " << numRegionsFound);
if (numRegionsFound == 1)
{
vtkDebugWithObjectMacro(this->ParentFilter, << "Legitimate Loop found");
return cellId;
}
}
}
vtkDebugWithObjectMacro(this->ParentFilter, << "Start and End Point are " << nextPt);
return -1;
}
// Combine the correct regions for output boolean
void vtkLoopBooleanPolyDataFilter::Impl::PerformBoolean(vtkPolyData* output, int booleanOperation)
{
// vtkSmartPointer<vtkThreshold> thresholder =
// vtkSmartPointer<vtkThreshold>::New();
// vtkSmartPointer<vtkDataSetSurfaceFilter> surfacer =
// vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
vtkPolyData* surfaces[4];
for (int i = 0; i < 4; i++)
{
surfaces[i] = vtkPolyData::New();
}
this->ThresholdRegions(surfaces);
// thresholder->SetInputData(this->Mesh[0]);
// thresholder->SetInputArrayToProcess(0, 0, 0, 1, "BooleanRegion");
// thresholder->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
// thresholder->SetLowerThreshold(-1.0);
// thresholder->SetUpperThreshold(-1.0);
// thresholder->Update();
// surfacer->SetInputData(thresholder->GetOutput());
// surfacer->Update();
// surfaces[0]->DeepCopy(surfacer->GetOutput());
// thresholder->SetInputData(this->Mesh[0]);
// thresholder->SetInputArrayToProcess(0, 0, 0, 1, "BooleanRegion");
// thresholder->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
// thresholder->SetLowerThreshold(1.0);
// thresholder->SetUpperThreshold(1.0);
// thresholder->Update();
// surfacer->SetInputData(thresholder->GetOutput());
// surfacer->Update();
// surfaces[1]->DeepCopy(surfacer->GetOutput());
// thresholder->SetInputData(this->Mesh[1]);
// thresholder->SetInputArrayToProcess(0, 0, 0, 1, "BooleanRegion");
// thresholder->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
// thresholder->SetLowerThreshold(1.0);
// thresholder->SetUpperThreshold(1.0);
// thresholder->Update();
// surfacer->SetInputData(thresholder->GetOutput());
// surfacer->Update();
// surfaces[2]->DeepCopy(surfacer->GetOutput());
// thresholder->SetInputData(this->Mesh[1]);
// thresholder->SetInputArrayToProcess(0, 0, 0, 1, "BooleanRegion");
// thresholder->SetThresholdFunction(vtkThreshold::THRESHOLD_BETWEEN);
// thresholder->SetLowerThreshold(-1.0);
// thresholder->SetUpperThreshold(-1.0);
// thresholder->Update();
// surfacer->SetInputData(thresholder->GetOutput());
// surfacer->Update();
// surfaces[3]->DeepCopy(surfacer->GetOutput());
vtkSmartPointer<vtkAppendPolyData> appender = vtkSmartPointer<vtkAppendPolyData>::New();
// If open intersection case, make sure correct region is taken
if (this->IntersectionCase == 2)
{
vtkSmartPointer<vtkPolyData> tmp = vtkSmartPointer<vtkPolyData>::New();
int numCells[4];
std::list<vtkIdType> nocellregion;
for (int i = 0; i < 4; i++)
{
numCells[i] = surfaces[i]->GetNumberOfCells();
if (numCells[i] == 0)
{
nocellregion.push_back(i);
}
}
if (!nocellregion.empty())
{
if (nocellregion.front() == 0)
{
tmp->DeepCopy(surfaces[1]);
surfaces[1]->DeepCopy(surfaces[0]);
surfaces[0]->DeepCopy(tmp);
}
if (nocellregion.back() == 2)
{
tmp->DeepCopy(surfaces[3]);
surfaces[3]->DeepCopy(surfaces[2]);
surfaces[2]->DeepCopy(tmp);
}
}
}
if (booleanOperation == 0)
{
appender->AddInputData(surfaces[0]);
appender->AddInputData(surfaces[2]);
}
if (booleanOperation == 1)
{
appender->AddInputData(surfaces[1]);
appender->AddInputData(surfaces[3]);
}
if (booleanOperation == 2)
{
appender->AddInputData(surfaces[0]);
appender->AddInputData(surfaces[3]);
}
appender->SetContainerAlgorithm(this->ParentFilter);
appender->Update();
output->DeepCopy(appender->GetOutput());
for (int i = 0; i < 4; i++)
{
surfaces[i]->Delete();
}
}
void vtkLoopBooleanPolyDataFilter::Impl::ThresholdRegions(vtkPolyData** surfaces)
{
vtkPoints* points[4];
vtkCellArray* cells[4];
vtkIntArray* boundaryPoints[4];
vtkIntArray* boundaryCells[4];
vtkIntArray* booleanCells[4];
for (int i = 0; i < 4; i++)
{
points[i] = vtkPoints::New();
cells[i] = vtkCellArray::New();
boundaryPoints[i] = vtkIntArray::New();
boundaryCells[i] = vtkIntArray::New();
booleanCells[i] = vtkIntArray::New();
}
for (int i = 0; i < 2; i++)
{
int numCells = this->Mesh[i]->GetNumberOfCells();
for (int j = 0; j < numCells; j++)
{
int value = this->BooleanArray[i]->GetValue(j);
vtkIdType npts;
const vtkIdType* pts;
this->Mesh[i]->GetCellPoints(j, npts, pts);
if (value < 0)
{
vtkSmartPointer<vtkIdList> newPointIds = vtkSmartPointer<vtkIdList>::New();
newPointIds->SetNumberOfIds(3);
for (int k = 0; k < npts; k++)
{
double pt[3];
this->Mesh[i]->GetPoint(pts[k], pt);
vtkIdType newId = points[3 * i]->InsertNextPoint(pt);
newPointIds->SetId(k, newId);
boundaryPoints[3 * i]->InsertValue(newId, this->BoundaryPointArray[i]->GetValue(pts[k]));
}
vtkIdType cellId = cells[3 * i]->InsertNextCell(newPointIds);
boundaryCells[3 * i]->InsertValue(cellId, this->BoundaryCellArray[i]->GetValue(j));
booleanCells[3 * i]->InsertValue(cellId, this->BooleanArray[i]->GetValue(j));
}
if (value > 0)
{
vtkSmartPointer<vtkIdList> newPointIds = vtkSmartPointer<vtkIdList>::New();
newPointIds->SetNumberOfIds(3);
for (int k = 0; k < npts; k++)
{
double pt[3];
this->Mesh[i]->GetPoint(pts[k], pt);
vtkIdType newId = points[i + 1]->InsertNextPoint(pt);
newPointIds->SetId(k, newId);
boundaryPoints[i + 1]->InsertValue(newId, this->BoundaryPointArray[i]->GetValue(pts[k]));
}
vtkIdType cellId = cells[i + 1]->InsertNextCell(newPointIds);
boundaryCells[i + 1]->InsertValue(cellId, this->BoundaryCellArray[i]->GetValue(j));
booleanCells[i + 1]->InsertValue(cellId, this->BooleanArray[i]->GetValue(j));
}
}
}
for (int i = 0; i < 4; i++)
{
surfaces[i]->SetPoints(points[i]);
surfaces[i]->SetPolys(cells[i]);
surfaces[i]->BuildLinks();
boundaryPoints[i]->SetName("BoundaryPoints");
surfaces[i]->GetPointData()->AddArray(boundaryPoints[i]);
boundaryCells[i]->SetName("BoundaryCells");
surfaces[i]->GetCellData()->AddArray(boundaryCells[i]);
booleanCells[i]->SetName("BooleanRegion");
surfaces[i]->GetCellData()->AddArray(booleanCells[i]);
points[i]->Delete();
cells[i]->Delete();
boundaryPoints[i]->Delete();
boundaryCells[i]->Delete();
booleanCells[i]->Delete();
}
}
VTK_ABI_NAMESPACE_END
|