210 vtkInformation *vtkNotUsed(request),
211 vtkInformationVector **inputVector,
212 vtkInformationVector *outputVector)
215 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
216 vtkInformation *outInfo = outputVector->GetInformationObject(0);
218 vtkPolyData *input = vtkPolyData::SafeDownCast(
219 inInfo->Get(vtkDataObject::DATA_OBJECT()));
220 vtkPolyData *output = vtkPolyData::SafeDownCast(
221 outInfo->Get(vtkDataObject::DATA_OBJECT()));
224 if ( input->GetNumberOfPoints() < 6 )
226 vtkErrorMacro(<<
"Error: too few input points.");
232 vtkSmartPointer<vtkPoints> newPoints = vtkSmartPointer<vtkPoints>::New();
233 newPoints->DeepCopy(input->GetPoints());
234 vtkSmartPointer<vtkCellArray> newPolys = vtkSmartPointer<vtkCellArray>::New();
235 newPolys->DeepCopy(input->GetPolys());
239 vtkSmartPointer<vtkIntArray> cellEntityIdsArray;
242 cellEntityIdsArray = vtkSmartPointer<vtkIntArray>::New();
243 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
244 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
246 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
250 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
256 info(
"Finding boundaries of input surface...");
260 vtkNew<vtkPolyData> boundariesData;
261 vtkNew<vtkIdList> boundaryLabels;
264 vtkPolyData* boundaries = boundariesData;
266 int numberOfBoundaries = boundaries->GetNumberOfCells();
267 std::cout <<
"Found " << numberOfBoundaries <<
" boundaries." << std::endl;
270 info(
"Computing boundary barycenters...");
271 vtkSmartPointer<vtkPoints> barycenters = vtkSmartPointer<vtkPoints>::New();
272 barycenters->SetNumberOfPoints(numberOfBoundaries);
273 for (
int i=0; i<numberOfBoundaries; i++)
275 double barycenter[3];
277 barycenters->SetPoint(i, barycenter);
281 info(
"Pairing boundaries...");
283 || (!this->BoundaryIds && numberOfBoundaries % 2) )
285 vtkErrorMacro(<<
"Error: the number of boundaries must be even.");
289 vtkNew<vtkIdList> boundaryIndicesToUse;
290 vtkIdList* boundaryIndexFilter = this->
BoundaryIds;
293 for (vtkIdType boundaryIndex=0; boundaryIndex<numberOfBoundaries; boundaryIndex++)
295 if (this->
BoundaryIds->IsId(boundaryLabels->GetId(boundaryIndex)) != -1)
297 boundaryIndicesToUse->InsertNextId(boundaryIndex);
300 boundaryIndexFilter = boundaryIndicesToUse;
305 for (
size_t pairingCount=0; pairingCount<boundaryPairs.size(); ++pairingCount)
307 vtkIdType i0 = boundaryPairs[pairingCount].first;
308 vtkIdType i1 = boundaryPairs[pairingCount].second;
311 info(
"Finding boundary point ids...");
312 vtkIdTypeArray * boundaryPointIdsArray = vtkIdTypeArray::SafeDownCast(boundaries->GetPointData()->GetScalars());
315 vtkPolyLine* boundary0 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i0));
316 vtkIdType numberOfBoundaryPoints0 = boundary0->GetNumberOfPoints();
317 if (numberOfBoundaryPoints0 < 8)
319 vtkErrorMacro(<<
"Error: Assuming at least 8 boundary points!");
321 vtkSmartPointer<vtkIdList> boundaryPointIds0 = vtkSmartPointer<vtkIdList>::New();
322 boundaryPointIds0->SetNumberOfIds(numberOfBoundaryPoints0);
323 for (
int j=0; j<numberOfBoundaryPoints0; j++)
326 boundaryPointIds0->SetId(j, boundaryPointIdsArray->GetValue(boundary0->GetPointId(j)));
330 vtkPolyLine* boundary1 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i1));
331 vtkIdType numberOfBoundaryPoints1 = boundary1->GetNumberOfPoints();
332 if (numberOfBoundaryPoints1 < 8)
334 vtkErrorMacro(<<
"Error: Assuming at least 8 boundary points!");
336 vtkSmartPointer<vtkIdList> boundaryPointIds1 = vtkSmartPointer<vtkIdList>::New();
337 boundaryPointIds1->SetNumberOfIds(numberOfBoundaryPoints1);
338 for (
int j=0; j<numberOfBoundaryPoints1; j++)
341 boundaryPointIds1->SetId(j, boundaryPointIdsArray->GetValue(boundary1->GetPointId(j)));
345 info(
"Finding closest vertices...");
347 vtkIdType i0start = starts.first;
348 vtkIdType i1start = starts.second;
352 int i0forward = (i0start + 1) % numberOfBoundaryPoints0;
353 int i1forward = (i1start + 1) % numberOfBoundaryPoints1;
355 double barycenter[3];
356 double startingPoint[3];
357 double pointForward[3];
359 barycenters->GetPoint(i0, barycenter);
360 input->GetPoint(boundaryPointIds0->GetId(i0start), startingPoint);
361 input->GetPoint(boundaryPointIds0->GetId(i0forward), pointForward);
362 cross_diff(startingPoint, pointForward, barycenter, cross[0]);
364 barycenters->GetPoint(i1, barycenter);
365 input->GetPoint(boundaryPointIds1->GetId(i1start), startingPoint);
366 input->GetPoint(boundaryPointIds1->GetId(i1forward), pointForward);
367 cross_diff(startingPoint, pointForward, barycenter, cross[1]);
370 bool backwards = vtkMath::Dot(cross[0], cross[1]) < 0.0;
371 int dir1 = backwards ? dir0: -dir0;
374 double nudgeFactor = 0.01;
375 i0forward = (i0start + dir0 + numberOfBoundaryPoints0) % numberOfBoundaryPoints0;
376 input->GetPoint(boundaryPointIds0->GetId(i0start), startingPoint);
377 input->GetPoint(boundaryPointIds0->GetId(i0forward), pointForward);
378 double nudgeVector0[3] = { nudgeFactor*(pointForward[0] - startingPoint[0]),
379 nudgeFactor*(pointForward[1] - startingPoint[1]),
380 nudgeFactor*(pointForward[2] - startingPoint[2]) };
381 i1forward = (i1start - dir1 + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
382 input->GetPoint(boundaryPointIds1->GetId(i1start), startingPoint);
383 input->GetPoint(boundaryPointIds1->GetId(i1forward), pointForward);
384 double nudgeVector1[3] = { nudgeFactor*(pointForward[0] - startingPoint[0]),
385 nudgeFactor*(pointForward[1] - startingPoint[1]),
386 nudgeFactor*(pointForward[2] - startingPoint[2]) };
389 info(
"Setting up n-sided polygon...");
391 int npts = numberOfBoundaryPoints0 + numberOfBoundaryPoints1 + 2;
392 vtkSmartPointer<vtkPolygon> polygon = vtkSmartPointer<vtkPolygon>::New();
393 vtkPoints * polygonPoints = polygon->GetPoints();
394 vtkIdList * polygonPointIds = polygon->GetPointIds();
395 polygonPoints->SetNumberOfPoints(npts);
396 polygonPointIds->SetNumberOfIds(npts);
397 vector<vtkIdType> polygonPointIdToGlobalPointId(npts);
401 for (
int k=0; k<=numberOfBoundaryPoints0; k++)
403 vtkIdType bp0id = (i0start + dir0*k + numberOfBoundaryPoints0) % numberOfBoundaryPoints0;
404 vtkIdType pid = boundaryPointIds0->GetId(bp0id);
406 input->GetPoint(pid, point);
409 point[0] += nudgeVector0[0];
410 point[1] += nudgeVector0[1];
411 point[2] += nudgeVector0[2];
413 polygonPointIdToGlobalPointId[kk] = pid;
414 polygonPointIds->SetId(kk,kk);
415 polygonPoints->InsertPoint(kk, point);
420 for (
int k=0; k<=numberOfBoundaryPoints1; k++)
422 vtkIdType bp1id = (i1start + dir1*k + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
423 vtkIdType pid = boundaryPointIds1->GetId(bp1id);
425 input->GetPoint(pid, point);
426 if (k == numberOfBoundaryPoints1)
428 point[0] += nudgeVector1[0];
429 point[1] += nudgeVector1[1];
430 point[2] += nudgeVector1[2];
432 polygonPointIdToGlobalPointId[kk] = pid;
433 polygonPointIds->SetId(kk,kk);
434 polygonPoints->InsertPoint(kk, point);
439 info(
"Triangulating...");
440 vtkSmartPointer<vtkIdList> outTris = vtkSmartPointer<vtkIdList>::New();
441 int triRes = polygon->NonDegenerateTriangulate(outTris);
444 vtkErrorMacro(<<
"Error: Numerical degeneracy when triangulating endcaps surface! Code: " << triRes);
448 info(
"Appending datasets...");
449 size_t nt = outTris->GetNumberOfIds()/3;
450 for (
size_t it=0; it<nt; ++it)
453 vtkIdType triangleIds[3] = { polygonPointIdToGlobalPointId[outTris->GetId(3*it+0)],
454 polygonPointIdToGlobalPointId[outTris->GetId(3*it+1)],
455 polygonPointIdToGlobalPointId[outTris->GetId(3*it+2)] };
458 newPolys->InsertNextCell(3, triangleIds);
464 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i0) : i0;
465 vtkIdType partnerBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(i1) : i1;
466 cellEntityIdsArray->InsertNextValue(
static_cast<int>(
473 output->SetPoints(newPoints);
474 output->SetPolys(newPolys);
475 output->GetPointData()->PassData(input->GetPointData());
478 output->GetCellData()->AddArray(cellEntityIdsArray);