Rebuild the boundaries of surface from the two arrays, without extracting them.
Returns false, leaving boundaries empty, when the arrays are missing or no longer describe surface, in which case the caller should extract the boundaries instead. What is checked is that every label owns a whole run of point order values, 0..n-1, with none missing, none repeated, and at least three points - enough to catch an array left over from an earlier mesh, a merge that collapsed two points of a ring onto one, and the fractional values a clipping filter leaves behind when it interpolates the arrays onto the points it creates. A boundary whose points were all removed simply disappears, which is not an error.
Nor is a boundary that is no longer open. The labels stay on the points of a boundary that has been capped, on purpose, as a record of which vessel end that ring was, so a surface that has been through a capper carries rings with cells on both sides of them. Only rings every edge of which still has a single cell behind it are returned, which is what the extractor would have found; a surface every boundary of which has been closed comes back with none, the same answer as for a surface that never had any.
73{
74 if (!boundaries)
75 {
76 return false;
77 }
78
79 boundaries->Initialize();
80 if (boundaryLabels)
81 {
82 boundaryLabels->Initialize();
83 }
84
85 vtkDataArray* labelsArray = GetSingleComponentPointArray(surface,boundaryLabelsArrayName);
86 vtkDataArray* orderArray = GetSingleComponentPointArray(surface,boundaryPointOrderArrayName);
87 if (!labelsArray || !orderArray)
88 {
89 return false;
90 }
91
92
93
94 std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > > pointsByLabel;
95 vtkIdType numberOfPoints = surface->GetNumberOfPoints();
96 for (vtkIdType pointId=0; pointId<numberOfPoints; pointId++)
97 {
98 vtkIdType label = GetValue(labelsArray,pointId);
99 if (label < 0)
100 {
101 continue;
102 }
103 vtkIdType order = GetValue(orderArray,pointId);
104 if (order < 0)
105 {
106
107
108 return false;
109 }
110 pointsByLabel[label].push_back(std::make_pair(order,pointId));
111 }
112
113 if (pointsByLabel.empty())
114 {
115
116
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);
123 return true;
124 }
125
126
127
128
129 vtkNew<vtkPolyData> working;
130 working->CopyStructure(surface);
131 working->BuildLinks();
132 vtkNew<vtkIdList> edgeNeighbors;
133
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)
137 {
138 std::vector<std::pair<vtkIdType,vtkIdType> >& ring = boundary->second;
139
140
141 if (ring.size() < 3)
142 {
143 return false;
144 }
145 std::sort(ring.begin(),ring.end());
146
147
148 for (size_t index=0; index<ring.size(); index++)
149 {
150 if (ring[index].first != static_cast<vtkIdType>(index))
151 {
152 return false;
153 }
154 }
155
156
157
158
159
160
161
162 size_t numberOfRingPoints = ring.size();
163 bool stillOpen = true;
164 for (size_t index=0; index<numberOfRingPoints && stillOpen; index++)
165 {
166 working->GetCellEdgeNeighbors(-1,ring[index].second,
167 ring[(index+1)%numberOfRingPoints].second,edgeNeighbors);
168
169
170
171 stillOpen = (edgeNeighbors->GetNumberOfIds() == 1);
172 }
173 if (!stillOpen)
174 {
175 closedLabels.push_back(boundary->first);
176 }
177 }
178 for (size_t index=0; index<closedLabels.size(); index++)
179 {
180 pointsByLabel.erase(closedLabels[index]);
181 }
182
183
184
185
186 vtkNew<vtkPoints> newPoints;
187 vtkNew<vtkCellArray> newLines;
188 vtkNew<vtkIdTypeArray> newScalars;
189 vtkNew<vtkIdList> newCell;
190
191 for (std::map<vtkIdType,std::vector<std::pair<vtkIdType,vtkIdType> > >::iterator
192 boundary=pointsByLabel.begin(); boundary!=pointsByLabel.end(); ++boundary)
193 {
194 const std::vector<std::pair<vtkIdType,vtkIdType> >& ring = boundary->second;
195
196 newCell->Initialize();
197 newCell->SetNumberOfIds(static_cast<vtkIdType>(ring.size()));
198 for (size_t index=0; index<ring.size(); index++)
199 {
200 vtkIdType pointId = ring[index].second;
201 newCell->SetId(static_cast<vtkIdType>(index),newPoints->InsertNextPoint(surface->GetPoint(pointId)));
202 newScalars->InsertNextValue(pointId);
203 }
204
205 newLines->InsertNextCell(newCell);
206
207 if (boundaryLabels)
208 {
209 boundaryLabels->InsertNextId(boundary->first);
210 }
211 }
212
213 boundaries->SetPoints(newPoints);
214 boundaries->SetLines(newLines);
215 boundaries->GetPointData()->SetScalars(newScalars);
216
217 return true;
218}