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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkSelectPolyData.h"
#include "vtkCellData.h"
#include "vtkDijkstraGraphGeodesicPath.h"
#include "vtkExecutive.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkLine.h"
#include "vtkPointData.h"
#include "vtkPointLocator.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkTriangleFilter.h"
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkSelectPolyData);
vtkCxxSetObjectMacro(vtkSelectPolyData, Loop, vtkPoints);
// Description:
// Instantiate object with InsideOut turned off.
vtkSelectPolyData::vtkSelectPolyData()
: SelectionScalarsArrayName(nullptr)
{
this->GenerateSelectionScalars = 0;
this->SetSelectionScalarsArrayName("Selection");
this->InsideOut = 0;
this->EdgeSearchMode = VTK_GREEDY_EDGE_SEARCH;
this->Loop = nullptr;
this->SelectionMode = VTK_INSIDE_SMALLEST_REGION;
this->ClosestPoint[0] = this->ClosestPoint[1] = this->ClosestPoint[2] = 0.0;
this->GenerateUnselectedOutput = 0;
this->SetNumberOfOutputPorts(3);
vtkNew<vtkPolyData> output2;
this->GetExecutive()->SetOutputData(1, output2);
vtkNew<vtkPolyData> output3;
this->GetExecutive()->SetOutputData(2, output3);
}
//------------------------------------------------------------------------------
vtkSelectPolyData::~vtkSelectPolyData()
{
this->SetSelectionScalarsArrayName(nullptr);
if (this->Loop)
{
this->Loop->Delete();
}
}
//------------------------------------------------------------------------------
vtkPolyData* vtkSelectPolyData::GetUnselectedOutput()
{
if (this->GetNumberOfOutputPorts() < 2)
{
return nullptr;
}
return vtkPolyData::SafeDownCast(this->GetExecutive()->GetOutputData(1));
}
//------------------------------------------------------------------------------
vtkAlgorithmOutput* vtkSelectPolyData::GetUnselectedOutputPort()
{
return this->GetOutputPort(1);
}
//------------------------------------------------------------------------------
vtkPolyData* vtkSelectPolyData::GetSelectionEdges()
{
if (this->GetNumberOfOutputPorts() < 3)
{
return nullptr;
}
return vtkPolyData::SafeDownCast(this->GetExecutive()->GetOutputData(2));
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::GreedyEdgeSearch(vtkPolyData* mesh, vtkIdList* edgePointIds)
{
vtkIdType numLoopPts = this->Loop->GetNumberOfPoints();
// First thing to do is find the closest mesh points to the loop
// points. This creates a list of mesh point ids corresponding to the loop point positions.
vtkNew<vtkIdList> loopIds;
loopIds->SetNumberOfIds(numLoopPts);
vtkPoints* inPts = mesh->GetPoints();
vtkIdType numPts = mesh->GetNumberOfPoints();
for (vtkIdType loopPointId = 0; loopPointId < numLoopPts; loopPointId++)
{
if (this->CheckAbort())
{
break;
}
double xLoop[3];
this->Loop->GetPoint(loopPointId, xLoop);
vtkIdType closestMeshPointId = 0;
double closestDist2 = VTK_DOUBLE_MAX;
for (vtkIdType meshPointId = 0; meshPointId < numPts; meshPointId++)
{
double x[3];
inPts->GetPoint(meshPointId, x);
double dist2 = vtkMath::Distance2BetweenPoints(x, xLoop);
if (dist2 < closestDist2)
{
closestMeshPointId = meshPointId;
closestDist2 = dist2;
}
} // for all input points
loopIds->SetId(loopPointId, closestMeshPointId);
} // for all loop points
edgePointIds->InsertNextId(loopIds->GetId(0));
// Now that we've got point ids, we build the loop. Start with the
// first two points in the loop (which define a line), and find the
// mesh edge that is directed along the line, and whose
// end point is closest to the line. Continue until loop closes in on
// itself.
vtkNew<vtkIdList> neighbors;
neighbors->Allocate(10000);
for (vtkIdType loopPointIndex = 0; loopPointIndex < numLoopPts; loopPointIndex++)
{
if (this->CheckAbort())
{
break;
}
vtkIdType currentId = loopIds->GetId(loopPointIndex);
vtkIdType nextId = loopIds->GetId((loopPointIndex + 1) % numLoopPts);
vtkIdType prevId = (-1);
double x[3];
double x0[3];
double x1[3];
double vec[3];
inPts->GetPoint(currentId, x);
inPts->GetPoint(currentId, x0);
inPts->GetPoint(nextId, x1);
for (int j = 0; j < 3; j++)
{
vec[j] = x1[j] - x0[j];
}
// track edge
for (vtkIdType id = currentId; id != nextId;)
{
vtkSelectPolyData::GetPointNeighbors(mesh, id, neighbors); // points connected by edge
vtkIdType numNei = neighbors->GetNumberOfIds();
vtkIdType closest = -1;
double closestDist2 = VTK_DOUBLE_MAX;
for (vtkIdType j = 0; j < numNei; j++)
{
vtkIdType neiId = neighbors->GetId(j);
if (neiId == nextId)
{
closest = neiId;
break;
}
else
{
double neiX[3];
inPts->GetPoint(neiId, neiX);
double dir[3];
for (vtkIdType k = 0; k < 3; k++)
{
dir[k] = neiX[k] - x[k];
}
if (neiId != prevId && vtkMath::Dot(dir, vec) > 0.0) // candidate
{
double dist2 = vtkLine::DistanceToLine(neiX, x0, x1);
if (dist2 < closestDist2)
{
closest = neiId;
closestDist2 = dist2;
}
} // in direction of line
}
} // for all neighbors
if (closest < 0)
{
vtkErrorMacro(<< "Can't follow edge. Set EdgeSearchMode to Dijkstra to avoid this error.");
edgePointIds->Initialize(); // clear edge list to indicate error
return;
}
else
{
edgePointIds->InsertNextId(closest);
prevId = id;
id = closest;
inPts->GetPoint(id, x);
}
} // for tracking edge
} // for all edges of loop
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::DijkstraEdgeSearch(vtkPolyData* mesh, vtkIdList* edgePointIds)
{
vtkNew<vtkDijkstraGraphGeodesicPath> edgeSearchFilter;
edgeSearchFilter->StopWhenEndReachedOn();
edgeSearchFilter->SetInputData(mesh);
vtkNew<vtkPointLocator> pointLocator;
pointLocator->SetDataSet(mesh);
vtkPoints* inPts = mesh->GetPoints();
vtkIdType numLoopPts = this->Loop->GetNumberOfPoints();
vtkIdType currentId = 0;
double xLoop[3];
this->Loop->GetPoint(0, xLoop);
vtkIdType nextId = pointLocator->FindClosestPoint(xLoop);
for (vtkIdType i = 0; i < numLoopPts; i++)
{
if (this->CheckAbort())
{
break;
}
currentId = nextId;
this->Loop->GetPoint((i + 1) % numLoopPts, xLoop);
nextId = pointLocator->FindClosestPoint(xLoop);
edgeSearchFilter->SetStartVertex(currentId);
edgeSearchFilter->SetEndVertex(nextId);
edgeSearchFilter->Update();
vtkPolyData* outputPath = edgeSearchFilter->GetOutput();
double x0[3];
inPts->GetPoint(currentId, x0);
for (int j = outputPath->GetNumberOfPoints() - 1; j >= 0; --j)
{
double x[3];
outputPath->GetPoint(j, x);
double dist2 = vtkMath::Distance2BetweenPoints(x, x0);
if (dist2 > 0.0)
{
// Find point ID to add in the input mesh to remember the next edge point
edgePointIds->InsertNextId(pointLocator->FindClosestPoint(x));
for (int k = 0; k < 3; ++k)
{
// Remember last added point so that it does not get added twice
x0[k] = x[k];
}
}
}
}
}
//------------------------------------------------------------------------------
int vtkSelectPolyData::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
// get the input and output objects
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkPolyData* input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkPolyData* output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Initialize and check data
vtkDebugMacro(<< "Selecting data...");
this->GetUnselectedOutput()->Initialize();
this->GetSelectionEdges()->Initialize();
// CHeck if inputs are valid
if (input->GetNumberOfPoints() < 1)
{
vtkErrorMacro("Input contains no points");
return 1;
}
vtkIdType numLoopPts;
if (this->Loop == nullptr || (numLoopPts = this->Loop->GetNumberOfPoints()) < 3)
{
vtkErrorMacro("Please define a loop with at least three points");
return 1;
}
// Convert to triangle mesh. All further computations are done on the triangulated mesh.
vtkSmartPointer<vtkPolyData> triMesh;
{
vtkNew<vtkTriangleFilter> tf;
tf->SetInputData(input);
tf->PassLinesOff();
tf->PassVertsOff();
tf->SetContainerAlgorithm(this);
tf->Update();
triMesh = tf->GetOutput();
}
vtkCellArray* inPolys = triMesh->GetPolys();
if (inPolys->GetNumberOfCells() < 1)
{
vtkErrorMacro("This filter operates on surface primitives");
return 1;
}
// Create a mesh that only contains points and polys
// (probably to avoid potential interference of other cell types)
// and links are computed (so that neighbors can be retrieved).
vtkNew<vtkPolyData> mesh;
vtkPoints* inPts = triMesh->GetPoints();
mesh->SetPoints(inPts);
mesh->SetPolys(inPolys);
mesh->BuildLinks(); // to do neighborhood searching
vtkIdType numCells = mesh->GetNumberOfCells();
// Get a list of point IDs of the mesh that forms a continuous closed loop
vtkNew<vtkIdList> edgePointIds;
edgePointIds->Allocate(numLoopPts * 10, 1000);
switch (this->EdgeSearchMode)
{
case VTK_GREEDY_EDGE_SEARCH:
this->GreedyEdgeSearch(mesh, edgePointIds);
break;
case VTK_DIJKSTRA_EDGE_SEARCH:
this->DijkstraEdgeSearch(triMesh, edgePointIds);
break;
default:
vtkErrorMacro("Unknown edge search mode: " << this->EdgeSearchMode);
}
if (edgePointIds->GetNumberOfIds() == 0 || this->CheckAbort())
{
return 1;
}
// Save the found edge list into SelectionEdges polydata
vtkIdType numMeshLoopPts = edgePointIds->GetNumberOfIds();
vtkNew<vtkCellArray> selectionEdges;
selectionEdges->AllocateEstimate(1, numMeshLoopPts);
selectionEdges->InsertNextCell(numMeshLoopPts);
for (vtkIdType i = 0; i < numMeshLoopPts; i++)
{
selectionEdges->InsertCellPoint(edgePointIds->GetId(i));
}
this->GetSelectionEdges()->SetPoints(inPts);
this->GetSelectionEdges()->SetLines(selectionEdges);
// Store distance from edge in point and cell marks and
// get ID of the cell that is farthest from the loop.
vtkNew<vtkIntArray> pointMarks;
vtkNew<vtkIntArray> cellMarks;
vtkIdType cellIdInSelectedRegion =
this->ComputeTopologicalDistance(mesh, edgePointIds, pointMarks, cellMarks);
// If the region that is closest to a specific point needs to be extracted then get
// a cell that is closes to that position.
if (this->SelectionMode == VTK_INSIDE_CLOSEST_POINT_REGION)
{
// find closest point and use as a seed
cellIdInSelectedRegion = this->GetClosestCellId(mesh, pointMarks);
}
// Set point and cell mark values in the selected region to -1.
// We'll end up having >0 values outside the selected region, -1 inside.
this->FillMarksInRegion(mesh, edgePointIds, pointMarks, cellMarks, cellIdInSelectedRegion);
// Invert mark value if we want to get the smallest region.
if (this->SelectionMode == VTK_INSIDE_SMALLEST_REGION)
{
for (vtkIdType i = 0; i < numCells; i++)
{
int markValue = cellMarks->GetValue(i);
cellMarks->SetValue(i, -markValue);
}
vtkIdType numPts = pointMarks->GetNumberOfValues();
for (vtkIdType i = 0; i < numPts; i++)
{
int markValue = pointMarks->GetValue(i);
pointMarks->SetValue(i, -markValue);
}
}
// Write filter output.
vtkPointData* inPD = triMesh->GetPointData();
vtkCellData* inCD = triMesh->GetCellData();
if (this->GenerateSelectionScalars)
{
// Write distance from contour as scalars to the output mesh.
// This can be used for example for later cliipping the mesh with vtkClipPolyData.
this->SetSelectionScalarsToOutput(inPD, inCD, mesh, edgePointIds, pointMarks, output);
}
else
{
// crop the input mesh to the selected region
this->SetClippedResultToOutput(inPD, inCD, mesh, cellMarks, output);
}
return 1;
}
//------------------------------------------------------------------------------
vtkIdType vtkSelectPolyData::ComputeTopologicalDistance(
vtkPolyData* mesh, vtkIdList* edgePointIds, vtkIntArray* pointMarks, vtkIntArray* cellMarks)
{
vtkIdType numPts = mesh->GetNumberOfPoints();
vtkIdType numCells = mesh->GetNumberOfCells();
// Next, prepare to mark off inside/outside and on boundary of loop.
// Mark the boundary of the loop using point marks. Also initialize
// the advancing front (used to mark traversal/compute scalars).
// Prepare to compute the advancing front
// Mark all points and cells as unvisited
cellMarks->SetNumberOfValues(numCells);
const int unvisited = VTK_INT_MAX;
for (vtkIdType i = 0; i < numCells; i++)
{
cellMarks->SetValue(i, unvisited);
}
pointMarks->SetNumberOfValues(numPts);
for (vtkIdType i = 0; i < numPts; i++)
{
pointMarks->SetValue(i, unvisited);
}
// Current and next front contain point IDs
vtkSmartPointer<vtkIdList> currentFront = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> nextFront = vtkSmartPointer<vtkIdList>::New();
vtkIdType numMeshLoopPts = edgePointIds->GetNumberOfIds();
for (vtkIdType i = 0; i < numMeshLoopPts; i++)
{
vtkIdType id = edgePointIds->GetId(i);
pointMarks->SetValue(id, 0); // marks the start of the front
currentFront->InsertNextId(id);
}
// Traverse the front as long as we can. We're basically computing a
// topological distance.
int maxFrontValue = 0;
vtkIdType maxFrontCell = (-1);
int currentFrontValue = 1;
vtkIdType numPtsInFront = 0;
while ((numPtsInFront = currentFront->GetNumberOfIds()))
{
// Process all triangles around the current front points
for (vtkIdType i = 0; i < numPtsInFront; i++)
{
vtkIdType id = currentFront->GetId(i);
vtkIdType* cells;
vtkIdType ncells;
mesh->GetPointCells(id, ncells, cells);
for (vtkIdType j = 0; j < ncells; j++)
{
id = cells[j];
if (cellMarks->GetValue(id) != unvisited)
{
// the cell is already visited
continue;
}
// This cell has not been visited yet
if (currentFrontValue > maxFrontValue)
{
// update maximum distance
maxFrontCell = id;
}
cellMarks->SetValue(id, currentFrontValue);
// Add all unvisited points of this triangle to the front
const vtkIdType* pts;
vtkIdType npts;
mesh->GetCellPoints(id, npts, pts);
for (vtkIdType k = 0; k < npts; k++)
{
if (pointMarks->GetValue(pts[k]) == unvisited)
{
pointMarks->SetValue(pts[k], 1);
nextFront->InsertNextId(pts[k]);
}
}
}
}
// All points in the current front has been processed, start a new iteration
currentFrontValue++;
// Swap currentFront and nextFront
vtkSmartPointer<vtkIdList> tmpFront = currentFront;
currentFront = nextFront;
nextFront = tmpFront;
nextFront->Reset();
}
return maxFrontCell;
}
//------------------------------------------------------------------------------
vtkIdType vtkSelectPolyData::GetClosestCellId(vtkPolyData* mesh, vtkIntArray* pointMarks)
{
vtkPoints* inPts = mesh->GetPoints();
vtkIdType numPts = inPts->GetNumberOfPoints();
vtkIdType closestCellId = -1;
double closestDist2 = VTK_DOUBLE_MAX;
vtkIdType closestPointId = -1;
for (vtkIdType pointId = 0; pointId < numPts; pointId++)
{
double x[3];
inPts->GetPoint(pointId, x);
double dist2 = vtkMath::Distance2BetweenPoints(x, this->ClosestPoint);
// get closest point not on the boundary
if (dist2 < closestDist2 && pointMarks->GetValue(pointId) != 0)
{
closestPointId = pointId;
closestDist2 = dist2;
}
}
if (closestPointId >= 0)
{
vtkIdType ncells;
vtkIdType* cells;
mesh->GetPointCells(closestPointId, ncells, cells);
if (ncells > 0)
{
closestCellId = cells[0];
}
}
return closestCellId;
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::FillMarksInRegion(vtkPolyData* mesh, vtkIdList* edgePointIds,
vtkIntArray* pointMarks, vtkIntArray* cellMarks, vtkIdType cellIdInSelectedRegion)
{
// We do the fill as a moving front. This is an alternative to recursion. The
// fill negates one region of the mesh on one side of the loop.
// In contrast to ComputeTopologicalDistance, current and next front
// in this method contain cell IDs.
vtkSmartPointer<vtkIdList> currentFront = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> nextFront = vtkSmartPointer<vtkIdList>::New();
currentFront->InsertNextId(cellIdInSelectedRegion);
// Initialize the front with the received cell ID
const int fillValue = -1;
const int boundaryValue = 0;
cellMarks->SetValue(cellIdInSelectedRegion, fillValue);
vtkNew<vtkIdList> neighbors;
neighbors->Allocate(10000);
vtkIdType numCellsInFront;
while ((numCellsInFront = currentFront->GetNumberOfIds()) > 0)
{
// Iterate through all the triangles and visit all the neighbor triangles
for (vtkIdType i = 0; i < numCellsInFront; i++)
{
vtkIdType id = currentFront->GetId(i);
const vtkIdType* pts;
vtkIdType npts;
mesh->GetCellPoints(id, npts, pts);
for (vtkIdType j = 0; j < 3; j++)
{
vtkIdType cellPointId1 = pts[j];
vtkIdType cellPointId2 = pts[(j + 1) % 3];
int cellPointValue1 = pointMarks->GetValue(cellPointId1);
int cellPointValue2 = pointMarks->GetValue(cellPointId2);
if (cellPointValue1 != boundaryValue)
{
pointMarks->SetValue(cellPointId1, fillValue);
}
if (cellPointValue1 == boundaryValue && cellPointValue2 == boundaryValue)
{
// This may be a boundary edge or just an edge that connects two boundary points.
// Do a full search in the boundary edge list to find out.
if (vtkSelectPolyData::IsBoundaryEdge(cellPointId1, cellPointId2, edgePointIds))
{
// cannot cross boundary
continue;
}
}
// add neighbors of this edge to the advancing front
mesh->GetCellEdgeNeighbors(id, cellPointId1, cellPointId2, neighbors);
vtkIdType numNei = neighbors->GetNumberOfIds();
for (vtkIdType k = 0; k < numNei; k++)
{
vtkIdType neiId = neighbors->GetId(k);
int val = cellMarks->GetValue(neiId);
if (val == fillValue)
{
// already processed
continue;
}
cellMarks->SetValue(neiId, fillValue);
nextFront->InsertNextId(neiId);
}
} // for all edges of cell
} // all cells in front
// Swap currentFront and nextFront
vtkSmartPointer<vtkIdList> tmpFront = currentFront;
currentFront = nextFront;
nextFront = tmpFront;
nextFront->Reset();
} // while still advancing
}
//------------------------------------------------------------------------------
bool vtkSelectPolyData::IsBoundaryEdge(
vtkIdType pointId1, vtkIdType pointId2, vtkIdList* edgePointIds)
{
vtkIdType numMeshLoopPts = edgePointIds->GetNumberOfIds();
for (vtkIdType edgePointIndex = 0; edgePointIndex < numMeshLoopPts; ++edgePointIndex)
{
vtkIdType edgePointId = edgePointIds->GetId(edgePointIndex);
if (edgePointId == pointId1)
{
vtkIdType wrappedEdgePointIndex = (edgePointIndex + 1) % numMeshLoopPts;
vtkIdType edgePointIdAfter = edgePointIds->GetId(wrappedEdgePointIndex);
if (edgePointIdAfter == pointId2)
{
// boundary edge
return true;
}
// Conceptually (edgePointIndex - 1) % numMeshLoopPts, but avoiding negatives.
wrappedEdgePointIndex = (edgePointIndex > 0) ? (edgePointIndex - 1) : (numMeshLoopPts - 1);
vtkIdType edgePointIdBefore = edgePointIds->GetId(wrappedEdgePointIndex);
if (edgePointIdBefore == pointId2)
{
// boundary edge
return true;
}
}
if (edgePointId == pointId2)
{
vtkIdType wrappedEdgePointIndex = (edgePointIndex + 1) % numMeshLoopPts;
vtkIdType edgePointIdAfter = edgePointIds->GetId(wrappedEdgePointIndex);
if (edgePointIdAfter == pointId1)
{
// boundary edge
return true;
}
// Conceptually (edgePointIndex - 1) % numMeshLoopPts, but avoiding negatives.
wrappedEdgePointIndex = (edgePointIndex > 0) ? (edgePointIndex - 1) : (numMeshLoopPts - 1);
vtkIdType edgePointIdBefore = edgePointIds->GetId(wrappedEdgePointIndex);
if (edgePointIdBefore == pointId1)
{
// boundary edge
return true;
}
}
}
// not a boundary edge
return false;
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::SetSelectionScalarsToOutput(vtkPointData* originalPointData,
vtkCellData* originalCellData, vtkPolyData* mesh, vtkIdList* edgePointIds,
vtkIntArray* pointMarks, vtkPolyData* output)
{
vtkPoints* inPts = mesh->GetPoints();
vtkIdType numPts = inPts->GetNumberOfPoints();
vtkNew<vtkFloatArray> selectionScalars;
selectionScalars->SetName(this->SelectionScalarsArrayName);
selectionScalars->SetNumberOfTuples(numPts);
// "Boundary" here refers to a polyline that connects the loop point positions.
// Compute signed distance to loop for non-boundary points.
vtkIdType numLoopPts = this->Loop->GetNumberOfPoints();
for (vtkIdType pointId = 0; pointId < numPts; pointId++)
{
if (this->CheckAbort())
{
break;
}
if (pointMarks->GetValue(pointId) == 0)
{
// boundary point, we'll deal with these later
continue;
}
// Not an edge point.
double x[3];
inPts->GetPoint(pointId, x);
double closestDist2 = VTK_DOUBLE_MAX;
for (vtkIdType i = 0; i < numLoopPts; i++)
{
double x0[3];
double x1[3];
this->Loop->GetPoint(i, x0);
this->Loop->GetPoint((i + 1) % numLoopPts, x1);
double t;
double xLoop[3];
double dist2 = vtkLine::DistanceToLine(x, x0, x1, t, xLoop);
if (dist2 < closestDist2)
{
closestDist2 = dist2;
}
}
// Set signed distance
double closestDist = 0.0;
if (pointMarks->GetValue(pointId) < 0)
{
closestDist = -sqrt(closestDist2);
}
else
{
closestDist = sqrt(closestDist2);
}
selectionScalars->SetComponent(pointId, 0, closestDist);
}
// Compute signed distance to loop for boundary points.
vtkIdType numMeshLoopPts = edgePointIds->GetNumberOfIds();
vtkNew<vtkIdList> neighbors;
neighbors->Allocate(10000);
for (vtkIdType edgePointIndex = 0; edgePointIndex < numMeshLoopPts; edgePointIndex++)
{
if (this->CheckAbort())
{
break;
}
vtkIdType edgePointId = edgePointIds->GetId(edgePointIndex);
double x[3];
inPts->GetPoint(edgePointId, x);
// Find the boundary line segment closest to this point
double closestPointOnBoundaryPos[3] = { 0.0, 0.0,
0.0 }; // closest position of edgePoint on the boundary
double closestDist2 = VTK_DOUBLE_MAX; // distance from closest boundary
{
for (vtkIdType loopPointId = 0; loopPointId < numLoopPts; loopPointId++)
{
double x0[3];
double x1[3];
this->Loop->GetPoint(loopPointId, x0);
this->Loop->GetPoint((loopPointId + 1) % numLoopPts, x1);
double t;
double xLoop[3]; // closest position on the boundary
double dist2 = vtkLine::DistanceToLine(x, x0, x1, t, xLoop);
if (dist2 < closestDist2)
{
closestDist2 = dist2;
closestPointOnBoundaryPos[0] = xLoop[0];
closestPointOnBoundaryPos[1] = xLoop[1];
closestPointOnBoundaryPos[2] = xLoop[2];
}
}
}
// Find neighbor farthest from the boundary (inside/outside information
// is the most reliable for this neighbor).
vtkIdType farthestNeighborPointId = 0;
{
vtkSelectPolyData::GetPointNeighbors(mesh, edgePointId, neighbors);
vtkIdType numNei = neighbors->GetNumberOfIds();
double maxDist = 0.0;
for (vtkIdType i = 0; i < numNei; i++)
{
vtkIdType neiId = neighbors->GetId(i);
if (pointMarks->GetValue(neiId) != 0) // find the furthest away
{
double dist = fabs(selectionScalars->GetComponent(neiId, 0));
if (dist > maxDist)
{
farthestNeighborPointId = neiId;
maxDist = dist;
}
}
}
}
// First compute distance assuming that x is on the same side of the boundary as the farthest
// neighbor.
double dist = sqrt(closestDist2);
if (pointMarks->GetValue(farthestNeighborPointId) < 0)
{
dist = -dist;
}
// If x and the farthest neighbor are actually different sides of the boundary then invert the
// signed distance value.
double farthestNeighborPointPos[3];
inPts->GetPoint(farthestNeighborPointId, farthestNeighborPointPos);
if (vtkMath::Distance2BetweenPoints(farthestNeighborPointPos, x) >
vtkMath::Distance2BetweenPoints(farthestNeighborPointPos, closestPointOnBoundaryPos))
{
// x is on the other side of the boundary
dist = -dist;
}
selectionScalars->SetComponent(edgePointId, 0, dist);
} // for all boundary points
output->CopyStructure(mesh); // pass geometry/topology unchanged
vtkPointData* outPD = output->GetPointData();
outPD->CopyAllOn();
outPD->PassData(originalPointData);
int idx = outPD->AddArray(selectionScalars);
outPD->SetActiveAttribute(idx, vtkDataSetAttributes::SCALARS);
vtkCellData* outCD = output->GetCellData();
outCD->PassData(originalCellData);
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::SetClippedResultToOutput(vtkPointData* originalPointData,
vtkCellData* originalCellData, vtkPolyData* mesh, vtkIntArray* cellMarks, vtkPolyData* output)
{
vtkCellData* outCD = output->GetCellData();
outCD->CopyAllOn(vtkDataSetAttributes::COPYTUPLE);
outCD->CopyAllocate(originalCellData);
// spit out all the negative cells
vtkNew<vtkCellArray> newPolys;
vtkIdType numCells = mesh->GetNumberOfCells();
newPolys->AllocateEstimate(numCells / 2, 3);
vtkIdType newID = 0;
for (vtkIdType i = 0; i < numCells; i++)
{
if (this->CheckAbort())
{
break;
}
if ((cellMarks->GetValue(i) < 0) || (cellMarks->GetValue(i) > 0 && this->InsideOut))
{
const vtkIdType* pts;
vtkIdType npts;
mesh->GetCellPoints(i, npts, pts);
newID = newPolys->InsertNextCell(npts, pts);
outCD->CopyData(originalCellData, i, newID);
}
}
vtkPoints* inPts = mesh->GetPoints();
output->SetPoints(inPts);
output->SetPolys(newPolys);
vtkPointData* outPD = output->GetPointData();
outPD->PassData(originalPointData);
if (this->GenerateUnselectedOutput)
{
vtkCellData* unCD = this->GetUnselectedOutput()->GetCellData();
unCD->CopyAllOn(vtkDataSetAttributes::COPYTUPLE);
unCD->CopyAllocate(originalCellData);
vtkNew<vtkCellArray> unPolys;
unPolys->AllocateEstimate(numCells / 2, 3);
for (vtkIdType i = 0; i < numCells; i++)
{
if (this->CheckAbort())
{
break;
}
if ((cellMarks->GetValue(i) >= 0) || this->InsideOut)
{
const vtkIdType* pts;
vtkIdType npts;
mesh->GetCellPoints(i, npts, pts);
newID = unPolys->InsertNextCell(npts, pts);
unCD->CopyData(originalCellData, i, newID);
}
}
this->GetUnselectedOutput()->SetPoints(inPts);
this->GetUnselectedOutput()->SetPolys(unPolys);
this->GetUnselectedOutput()->GetPointData()->PassData(originalPointData);
}
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::GetPointNeighbors(vtkPolyData* mesh, vtkIdType ptId, vtkIdList* nei)
{
nei->Reset();
vtkIdType ncells;
vtkIdType* cells;
mesh->GetPointCells(ptId, ncells, cells);
for (vtkIdType i = 0; i < ncells; i++)
{
const vtkIdType* pts;
vtkIdType npts;
mesh->GetCellPoints(cells[i], npts, pts);
for (vtkIdType j = 0; j < 3; j++)
{
if (pts[j] != ptId)
{
nei->InsertUniqueId(pts[j]);
}
}
}
}
//------------------------------------------------------------------------------
vtkMTimeType vtkSelectPolyData::GetMTime()
{
vtkMTimeType mTime = this->Superclass::GetMTime();
vtkMTimeType time;
if (this->Loop != nullptr)
{
time = this->Loop->GetMTime();
mTime = (time > mTime ? time : mTime);
}
return mTime;
}
//------------------------------------------------------------------------------
void vtkSelectPolyData::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent
<< "Generate Unselected Output: " << (this->GenerateUnselectedOutput ? "On\n" : "Off\n");
os << indent << "Inside Mode: ";
os << this->GetSelectionModeAsString() << "\n";
os << indent << "Closest Point: (" << this->ClosestPoint[0] << ", " << this->ClosestPoint[1]
<< ", " << this->ClosestPoint[2] << ")\n";
os << indent
<< "Generate Selection Scalars: " << (this->GenerateSelectionScalars ? "On\n" : "Off\n");
if (this->GenerateSelectionScalars)
{
os << indent << "Selection Scalars array name: " << this->SelectionScalarsArrayName << "\n";
}
os << indent << "Inside Out: " << (this->InsideOut ? "On\n" : "Off\n");
os << indent << "Edge Search Mode: ";
os << this->GetEdgeSearchModeAsString() << "\n";
if (this->Loop)
{
os << indent << "Loop of " << this->Loop->GetNumberOfPoints() << "points defined\n";
}
else
{
os << indent << "Loop not defined\n";
}
}
VTK_ABI_NAMESPACE_END
|