115 vtkInformation *vtkNotUsed(request),
116 vtkInformationVector **inputVector,
117 vtkInformationVector *outputVector)
119 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
120 vtkInformation *outInfo = outputVector->GetInformationObject(0);
122 vtkPolyData *input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
123 vtkPolyData *output = vtkPolyData::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
127 output->CopyStructure(input);
128 output->GetPointData()->PassData(input->GetPointData());
129 output->GetCellData()->PassData(input->GetCellData());
138 vtkErrorMacro(<<
"Both BoundaryLabelsArrayName and BoundaryPointOrderArrayName have to be set.");
143 vtkIdType numberOfPoints = input->GetNumberOfPoints();
145 vtkNew<vtkIntArray> labelsArray;
147 labelsArray->SetNumberOfTuples(numberOfPoints);
148 labelsArray->FillComponent(0,
static_cast<double>(invalidLabel));
150 vtkNew<vtkIntArray> orderArray;
152 orderArray->SetNumberOfTuples(numberOfPoints);
153 orderArray->FillComponent(0,
static_cast<double>(invalidLabel));
155 vtkNew<vtkvmtkPolyDataBoundaryExtractor> boundaryExtractor;
156 boundaryExtractor->SetInputData(input);
157 boundaryExtractor->Update();
158 vtkPolyData* boundaries = boundaryExtractor->GetOutput();
159 vtkDataArray* boundaryPointIds = boundaries->GetPointData()->GetScalars();
162 std::vector<std::vector<vtkIdType> > rings;
163 for (vtkIdType boundaryIndex=0; boundaryIndex<boundaries->GetNumberOfCells(); boundaryIndex++)
165 vtkPolyLine* boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryIndex));
166 if (!boundary || !boundaryPointIds)
170 vtkIdType numberOfBoundaryPoints = boundary->GetNumberOfPoints();
171 std::vector<vtkIdType> ring;
172 ring.reserve(numberOfBoundaryPoints);
173 for (vtkIdType j=0; j<numberOfBoundaryPoints; j++)
175 ring.push_back(
static_cast<vtkIdType
>(vtkMath::Round(
176 boundaryPointIds->GetComponent(boundary->GetPointId(j),0))));
178 rings.push_back(ring);
181 vtkIdType numberOfBoundaries =
static_cast<vtkIdType
>(rings.size());
182 std::vector<vtkIdType> labels(rings.size(),invalidLabel);
190 vtkIdType numberOfPlanes = 0;
194 numberOfPlanes = this->
PlaneOrigins->GetNumberOfPoints();
198 numberOfPlanes = this->
PlaneNormals->GetNumberOfTuples();
201 std::vector<vtkIdType> planeLabels(numberOfPlanes,invalidLabel);
202 std::vector<bool> planeMatched(numberOfPlanes,
false);
203 for (vtkIdType planeIndex=0; planeIndex<numberOfPlanes; planeIndex++)
207 if (planeLabels[planeIndex] > highestLabelInUse)
209 highestLabelInUse = planeLabels[planeIndex];
217 for (vtkIdType planeIndex=0; planeIndex<numberOfPlanes; planeIndex++)
221 vtkIdType nearestBoundary = -1;
222 double nearestDistance = 0.0;
223 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
225 double boundaryNearestDistance, boundaryFurthestDistance;
226 DistancesFromPoint(input,rings[boundaryIndex],origin,boundaryNearestDistance,boundaryFurthestDistance);
234 if (nearestBoundary == -1 || boundaryNearestDistance < nearestDistance)
236 nearestBoundary = boundaryIndex;
237 nearestDistance = boundaryNearestDistance;
240 if (nearestBoundary == -1)
246 if (labels[nearestBoundary] != invalidLabel)
250 labels[nearestBoundary] = planeLabels[planeIndex];
251 planeMatched[planeIndex] =
true;
259 vtkErrorMacro(<<
"MaximumDistanceFromPlane has to be positive in the OnPlane mode; no boundary was matched to a plane.");
264 vtkErrorMacro(<<
"PlaneNormals has to be set in the OnPlane mode; no boundary was matched to a plane.");
272 for (vtkIdType planeIndex=0; planeIndex<numberOfPlanes; planeIndex++)
274 double origin[3], normal[3];
277 if (vtkMath::Normalize(normal) == 0.0)
282 vtkIdType nearestBoundary = -1;
283 double nearestDistance = 0.0;
284 vtkIdType numberOfQualifyingBoundaries = 0;
285 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
289 if (labels[boundaryIndex] != invalidLabel)
296 double boundaryNearestDistance, boundaryFurthestDistance;
297 DistancesFromPoint(input,rings[boundaryIndex],origin,boundaryNearestDistance,boundaryFurthestDistance);
307 for (
size_t j=0; j<rings[boundaryIndex].size(); j++)
309 double boundaryPoint[3];
310 input->GetPoint(rings[boundaryIndex][j],boundaryPoint);
312 for (
int k=0; k<3; k++)
314 offset[k] = boundaryPoint[k] - origin[k];
326 numberOfQualifyingBoundaries++;
327 if (nearestBoundary == -1 || boundaryNearestDistance < nearestDistance)
329 nearestBoundary = boundaryIndex;
330 nearestDistance = boundaryNearestDistance;
334 if (nearestBoundary == -1)
338 if (numberOfQualifyingBoundaries > 1)
342 vtkWarningMacro(<<numberOfQualifyingBoundaries<<
" boundaries lie in the plane of the one labelled "
343 <<planeLabels[planeIndex]<<
"; the nearest to its origin was taken.");
345 labels[nearestBoundary] = planeLabels[planeIndex];
346 planeMatched[planeIndex] =
true;
353 if (existingLabels && existingLabels->GetNumberOfComponents() == 1
354 && existingLabels->GetNumberOfTuples() == numberOfPoints)
356 for (vtkIdType pointId=0; pointId<numberOfPoints; pointId++)
358 vtkIdType label =
static_cast<vtkIdType
>(vtkMath::Round(existingLabels->GetComponent(pointId,0)));
359 if (label > highestLabelInUse)
361 highestLabelInUse = label;
368 std::map<vtkIdType,std::pair<vtkIdType,vtkIdType> > bestClaimByLabel;
369 std::vector<vtkIdType> claimedLabel(rings.size(),invalidLabel);
370 std::vector<vtkIdType> claimCount(rings.size(),0);
371 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
373 std::map<vtkIdType,vtkIdType> counts;
374 for (
size_t j=0; j<rings[boundaryIndex].size(); j++)
376 vtkIdType label =
static_cast<vtkIdType
>(vtkMath::Round(
377 existingLabels->GetComponent(rings[boundaryIndex][j],0)));
383 vtkIdType ringSize =
static_cast<vtkIdType
>(rings[boundaryIndex].size());
384 for (std::map<vtkIdType,vtkIdType>::iterator count=counts.begin(); count!=counts.end(); ++count)
386 if (count->second * 2 > ringSize)
388 claimedLabel[boundaryIndex] = count->first;
389 claimCount[boundaryIndex] = count->second;
393 if (claimedLabel[boundaryIndex] == invalidLabel)
399 std::map<vtkIdType,std::pair<vtkIdType,vtkIdType> >::iterator best =
400 bestClaimByLabel.find(claimedLabel[boundaryIndex]);
401 if (best == bestClaimByLabel.end())
403 bestClaimByLabel[claimedLabel[boundaryIndex]] =
404 std::make_pair(claimCount[boundaryIndex],boundaryIndex);
408 vtkWarningMacro(<<
"More than one boundary carries the label "<<claimedLabel[boundaryIndex]
409 <<
"; it stays with the one holding it most widely and the others are relabeled.");
410 if (claimCount[boundaryIndex] > best->second.first)
412 best->second = std::make_pair(claimCount[boundaryIndex],boundaryIndex);
416 for (std::map<vtkIdType,std::pair<vtkIdType,vtkIdType> >::iterator best=bestClaimByLabel.begin();
417 best!=bestClaimByLabel.end(); ++best)
419 labels[best->second.second] = best->first;
427 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
433 if (labels[boundaryIndex] > highestLabelInUse)
435 highestLabelInUse = labels[boundaryIndex];
442 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
444 if (labels[boundaryIndex] > highestLabelInUse)
446 highestLabelInUse = labels[boundaryIndex];
449 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
451 if (labels[boundaryIndex] == invalidLabel)
453 labels[boundaryIndex] = ++highestLabelInUse;
462 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
464 for (
size_t j=0; j<rings[boundaryIndex].size(); j++)
466 labelsArray->SetValue(rings[boundaryIndex][j],
static_cast<int>(labels[boundaryIndex]));
467 orderArray->SetValue(rings[boundaryIndex][j],
static_cast<int>(j));
472 for (vtkIdType planeIndex=0; planeIndex<numberOfPlanes; planeIndex++)
474 if (!planeMatched[planeIndex])
482 output->GetPointData()->AddArray(labelsArray);
483 output->GetPointData()->AddArray(orderArray);
490 vtkIdType numberOfBoundaries = boundaries->GetNumberOfCells();
491 if (numberOfBoundaries < 2)
498 std::vector<std::vector<double> > barycenters(numberOfBoundaries,std::vector<double>(3,0.0));
499 std::vector<double> meanRadii(numberOfBoundaries,0.0);
500 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
502 vtkPolyLine* boundary = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryIndex));
507 double barycenter[3];
509 for (
int j=0; j<3; j++)
511 barycenters[boundaryIndex][j] = barycenter[j];
518 std::vector<bool> paired(numberOfBoundaries,
false);
519 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
521 if (paired[boundaryIndex])
525 vtkIdType closestIndex = -1;
526 double closestDistance2 = 0.0;
527 for (vtkIdType candidateIndex=boundaryIndex+1; candidateIndex<numberOfBoundaries; candidateIndex++)
529 if (paired[candidateIndex])
533 double distance2 = vtkMath::Distance2BetweenPoints(&barycenters[boundaryIndex][0],&barycenters[candidateIndex][0]);
534 if (closestIndex == -1 || distance2 < closestDistance2)
536 closestIndex = candidateIndex;
537 closestDistance2 = distance2;
540 if (closestIndex == -1)
543 vtkWarningMacro(<<
"Annular labeling: the boundary labelled "<<labels[boundaryIndex]
544 <<
" has no partner, so it keeps its label and no outer label is derived from it.");
548 paired[boundaryIndex] =
true;
549 paired[closestIndex] =
true;
551 vtkIdType innerIndex = boundaryIndex;
552 vtkIdType outerIndex = closestIndex;
553 if (meanRadii[outerIndex] < meanRadii[innerIndex])
555 std::swap(innerIndex,outerIndex);