76 vtkInformation *vtkNotUsed(request),
77 vtkInformationVector **inputVector,
78 vtkInformationVector *outputVector)
80 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
81 vtkInformation *outInfo = outputVector->GetInformationObject(0);
83 vtkPolyData *input = vtkPolyData::SafeDownCast(
84 inInfo->Get(vtkDataObject::DATA_OBJECT()));
85 vtkPolyData *output = vtkPolyData::SafeDownCast(
86 outInfo->Get(vtkDataObject::DATA_OBJECT()));
88 if ( input->GetNumberOfPoints() < 1 )
95 vtkPoints* newPoints = vtkPoints::New();
96 newPoints->DeepCopy(input->GetPoints());
98 vtkCellArray* newPolys = vtkCellArray::New();
99 newPolys->DeepCopy(input->GetPolys());
101 vtkIdTypeArray* cellEntityIdsArray = NULL;
105 cellEntityIdsArray = vtkIdTypeArray::New();
106 cellEntityIdsArray->SetName(this->CellEntityIdsArrayName);
107 if (input->GetCellData()->GetArray(this->CellEntityIdsArrayName))
109 cellEntityIdsArray->DeepCopy(input->GetCellData()->GetArray(this->CellEntityIdsArrayName));
113 cellEntityIdsArray->SetNumberOfTuples(newPolys->GetNumberOfCells());
121 vtkNew<vtkPolyData> boundaries;
122 vtkNew<vtkIdList> boundaryLabels;
125 int numberOfBoundaries = boundaries->GetNumberOfCells();
128 || (!this->BoundaryIds && numberOfBoundaries % 2) )
130 vtkErrorMacro(<<
"Error: the number of boundaries must be even.");
135 cellEntityIdsArray->Delete();
139 vtkPoints* barycenters = vtkPoints::New();
140 barycenters->SetNumberOfPoints(numberOfBoundaries);
142 vtkIdList* boundaryPairings = vtkIdList::New();
143 boundaryPairings->SetNumberOfIds(numberOfBoundaries);
144 vtkIdList* visitedBoundaries = vtkIdList::New();
145 visitedBoundaries->SetNumberOfIds(numberOfBoundaries);
147 double barycenter[3];
148 for (
int i=0; i<numberOfBoundaries; i++)
150 vtkCell* boundary = boundaries->GetCell(i);
152 barycenters->SetPoint(i,barycenter);
153 boundaryPairings->SetId(i,-1);
154 visitedBoundaries->SetId(i,-1);
157 double currentBarycenter[3];
158 double distance2, minDistance2 = 0.0;
159 vtkIdType closestBoundaryId;
160 for (
int i=0; i<numberOfBoundaries; i++)
166 if (boundaryPairings->GetId(i) != -1 || boundaryPairings->IsId(i) != -1)
170 barycenters->GetPoint(i,barycenter);
171 closestBoundaryId = -1;
172 for (
int j=i+1; j<numberOfBoundaries; j++)
178 if (boundaryPairings->GetId(j) != -1 || boundaryPairings->IsId(j) != -1)
182 barycenters->GetPoint(j,currentBarycenter);
183 distance2 = vtkMath::Distance2BetweenPoints(barycenter,currentBarycenter);
184 if (closestBoundaryId == -1 || distance2 < minDistance2)
186 minDistance2 = distance2;
187 closestBoundaryId = j;
190 boundaryPairings->SetId(i,closestBoundaryId);
191 boundaryPairings->SetId(closestBoundaryId,i);
194 for (
int i=0; i<numberOfBoundaries; i++)
201 if (boundaryPairings->GetId(i) == -1)
206 if (visitedBoundaries->GetId(i) != -1)
211 visitedBoundaries->SetId(i,i);
212 visitedBoundaries->SetId(boundaryPairings->GetId(i),i);
214 vtkIdTypeArray* boundaryPointIdsArray = vtkIdTypeArray::New();
215 boundaryPointIdsArray->DeepCopy(boundaries->GetPointData()->GetScalars());
217 vtkPolyLine* boundary0 = vtkPolyLine::SafeDownCast(boundaries->GetCell(i));
218 vtkIdType numberOfBoundaryPoints0 = boundary0->GetNumberOfPoints();
219 vtkIdList* boundaryPointIds0 = vtkIdList::New();
220 boundaryPointIds0->SetNumberOfIds(numberOfBoundaryPoints0);
221 for (
int j=0; j<numberOfBoundaryPoints0; j++)
223 boundaryPointIds0->SetId(j,boundaryPointIdsArray->GetValue(boundary0->GetPointId(j)));
226 vtkPolyLine* boundary1 = vtkPolyLine::SafeDownCast(boundaries->GetCell(boundaryPairings->GetId(i)));
227 vtkIdType numberOfBoundaryPoints1 = boundary1->GetNumberOfPoints();
228 vtkIdList* boundaryPointIds1 = vtkIdList::New();
229 boundaryPointIds1->SetNumberOfIds(numberOfBoundaryPoints1);
230 for (
int j=0; j<numberOfBoundaryPoints1; j++)
232 boundaryPointIds1->SetId(j,boundaryPointIdsArray->GetValue(boundary1->GetPointId(j)));
235 double startingPoint[3], currentPoint[3];
236 input->GetPoint(boundaryPointIds0->GetId(0),startingPoint);
238 vtkIdType offset = -1;
239 for (
int j=0; j<numberOfBoundaryPoints1; j++)
241 input->GetPoint(boundaryPointIds1->GetId(j),currentPoint);
242 distance2 = vtkMath::Distance2BetweenPoints(startingPoint,currentPoint);
243 if (offset == -1 || distance2 < minDistance2)
246 minDistance2 = distance2;
250 double vectorToStart[3], vectorToForward[3], cross0[3], cross1[3];
251 double pointForward[3];
253 barycenters->GetPoint(i,barycenter);
254 input->GetPoint(boundaryPointIds0->GetId(0),startingPoint);
255 input->GetPoint(boundaryPointIds0->GetId(numberOfBoundaryPoints0/8),pointForward);
257 vectorToStart[0] = startingPoint[0] - barycenter[0];
258 vectorToStart[1] = startingPoint[1] - barycenter[1];
259 vectorToStart[2] = startingPoint[2] - barycenter[2];
261 vectorToForward[0] = pointForward[0] - barycenter[0];
262 vectorToForward[1] = pointForward[1] - barycenter[1];
263 vectorToForward[2] = pointForward[2] - barycenter[2];
265 vtkMath::Cross(vectorToStart,vectorToForward,cross0);
267 barycenters->GetPoint(boundaryPairings->GetId(i),barycenter);
268 input->GetPoint(boundaryPointIds1->GetId(0),startingPoint);
269 input->GetPoint(boundaryPointIds1->GetId(numberOfBoundaryPoints1/8),pointForward);
271 vectorToStart[0] = startingPoint[0] - barycenter[0];
272 vectorToStart[1] = startingPoint[1] - barycenter[1];
273 vectorToStart[2] = startingPoint[2] - barycenter[2];
275 vectorToForward[0] = pointForward[0] - barycenter[0];
276 vectorToForward[1] = pointForward[1] - barycenter[1];
277 vectorToForward[2] = pointForward[2] - barycenter[2];
279 vtkMath::Cross(vectorToStart,vectorToForward,cross1);
281 bool backward =
false;
282 if (vtkMath::Dot(cross0,cross1) < 0.0)
287 double point0[3], nextPoint0[3];
288 double point1[3], nextPoint1[3];
289 vtkIdType pointId0, nextPointId0;
290 vtkIdType pointId1, nextPointId1;
295 if (j == numberOfBoundaryPoints0 && k == numberOfBoundaryPoints1)
300 pointId0 = j % numberOfBoundaryPoints0;
301 nextPointId0 = (pointId0 + 1) % numberOfBoundaryPoints0;
302 pointId1 = (k + offset) % numberOfBoundaryPoints1;
303 nextPointId1 = (pointId1 + 1) % numberOfBoundaryPoints1;
306 pointId1 = (numberOfBoundaryPoints1 - k + offset + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
307 nextPointId1 = (pointId1 - 1 + numberOfBoundaryPoints1) % numberOfBoundaryPoints1;
310 input->GetPoint(boundaryPointIds0->GetId(pointId0),point0);
311 input->GetPoint(boundaryPointIds0->GetId(nextPointId0),nextPoint0);
312 input->GetPoint(boundaryPointIds1->GetId(pointId1),point1);
313 input->GetPoint(boundaryPointIds1->GetId(nextPointId1),nextPoint1);
315 newPolys->InsertNextCell(3);
316 newPolys->InsertCellPoint(boundaryPointIds0->GetId(pointId0));
317 newPolys->InsertCellPoint(boundaryPointIds1->GetId(pointId1));
320 if (vtkMath::Distance2BetweenPoints(point0,nextPoint1) < vtkMath::Distance2BetweenPoints(point1,nextPoint0))
324 if (k == numberOfBoundaryPoints1)
328 else if (j == numberOfBoundaryPoints0)
335 newPolys->InsertCellPoint(boundaryPointIds1->GetId(nextPointId1));
340 newPolys->InsertCellPoint(boundaryPointIds0->GetId(nextPointId0));
348 vtkIdType boundaryId = useBoundaryLabels ? boundaryLabels->GetId(i) : i;
349 vtkIdType partnerBoundaryId = useBoundaryLabels ? boundaryLabels->GetId(boundaryPairings->GetId(i)) : boundaryPairings->GetId(i);
350 cellEntityIdsArray->InsertNextValue(
355 boundaryPointIdsArray->Delete();
356 boundaryPointIds0->Delete();
357 boundaryPointIds1->Delete();
360 output->SetPoints(newPoints);
361 output->SetPolys(newPolys);
363 output->GetPointData()->PassData(input->GetPointData());
367 output->GetCellData()->AddArray(cellEntityIdsArray);
368 cellEntityIdsArray->Delete();
373 barycenters->Delete();
374 boundaryPairings->Delete();
375 visitedBoundaries->Delete();