72bool vtkvmtkBoundaryLabels::GetBoundaries(vtkPolyData* surface,
const char* boundaryLabelsArrayName,
const char* boundaryPointOrderArrayName, vtkPolyData* boundaries, vtkIdList* boundaryLabels)
79 boundaries->Initialize();
82 boundaryLabels->Initialize();
85 vtkDataArray* labelsArray = GetSingleComponentPointArray(surface,boundaryLabelsArrayName);
86 vtkDataArray* orderArray = GetSingleComponentPointArray(surface,boundaryPointOrderArrayName);
87 if (!labelsArray || !orderArray)
94 std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > > pointsByLabel;
95 vtkIdType numberOfPoints = surface->GetNumberOfPoints();
96 for (vtkIdType pointId=0; pointId<numberOfPoints; pointId++)
98 vtkIdType label = GetValue(labelsArray,pointId);
103 vtkIdType order = GetValue(orderArray,pointId);
110 pointsByLabel[label].push_back(std::make_pair(order,pointId));
113 if (pointsByLabel.empty())
117 vtkNew<vtkPoints> emptyPoints;
118 vtkNew<vtkCellArray> emptyLines;
119 vtkNew<vtkIdTypeArray> emptyScalars;
120 boundaries->SetPoints(emptyPoints);
121 boundaries->SetLines(emptyLines);
122 boundaries->GetPointData()->SetScalars(emptyScalars);
129 vtkNew<vtkPolyData> working;
130 working->CopyStructure(surface);
131 working->BuildLinks();
132 vtkNew<vtkIdList> edgeNeighbors;
134 std::vector<vtkIdType> closedLabels;
135 for (std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > >::iterator
136 boundary=pointsByLabel.begin(); boundary!=pointsByLabel.end(); ++boundary)
138 std::vector<std::pair<vtkIdType,vtkIdType> >& ring = boundary->second;
145 std::sort(ring.begin(),ring.end());
148 for (
size_t index=0; index<ring.size(); index++)
150 if (ring[index].first !=
static_cast<vtkIdType
>(index))
162 size_t numberOfRingPoints = ring.size();
163 bool stillOpen =
true;
164 for (
size_t index=0; index<numberOfRingPoints && stillOpen; index++)
166 working->GetCellEdgeNeighbors(-1,ring[index].second,
167 ring[(index+1)%numberOfRingPoints].second,edgeNeighbors);
171 stillOpen = (edgeNeighbors->GetNumberOfIds() == 1);
175 closedLabels.push_back(boundary->first);
178 for (
size_t index=0; index<closedLabels.size(); index++)
180 pointsByLabel.erase(closedLabels[index]);
186 vtkNew<vtkPoints> newPoints;
187 vtkNew<vtkCellArray> newLines;
188 vtkNew<vtkIdTypeArray> newScalars;
189 vtkNew<vtkIdList> newCell;
191 for (std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > >::iterator
192 boundary=pointsByLabel.begin(); boundary!=pointsByLabel.end(); ++boundary)
194 const std::vector<std::pair<vtkIdType,vtkIdType> >& ring = boundary->second;
196 newCell->Initialize();
197 newCell->SetNumberOfIds(
static_cast<vtkIdType
>(ring.size()));
198 for (
size_t index=0; index<ring.size(); index++)
200 vtkIdType pointId = ring[index].second;
201 newCell->SetId(
static_cast<vtkIdType
>(index),newPoints->InsertNextPoint(surface->GetPoint(pointId)));
202 newScalars->InsertNextValue(pointId);
205 newLines->InsertNextCell(newCell);
209 boundaryLabels->InsertNextId(boundary->first);
213 boundaries->SetPoints(newPoints);
214 boundaries->SetLines(newLines);
215 boundaries->GetPointData()->SetScalars(newScalars);
222 if (!surface || !boundaries)
229 boundaryLabels->Reset();
232 if (boundaryLabelsArrayName && boundaryLabelsArrayName[0]
233 && boundaryPointOrderArrayName && boundaryPointOrderArrayName[0])
241 vtkWarningWithObjectMacro(warningSource,<<
"The boundary label arrays are missing from the input surface or no longer describe it; "
242 <<
"extracting its boundaries instead, and naming the caps by position.");
246 vtkNew<vtkvmtkPolyDataBoundaryExtractor> boundaryExtractor;
247 boundaryExtractor->SetInputData(surface);
248 boundaryExtractor->Update();
249 boundaries->ShallowCopy(boundaryExtractor->GetOutput());