45{
46 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
47 vtkInformation *outInfo = outputVector->GetInformationObject(0);
48
49 vtkPolyData *input = vtkPolyData::SafeDownCast(
50 inInfo->Get(vtkDataObject::DATA_OBJECT()));
51 vtkPolyData *output = vtkPolyData::SafeDownCast(
52 outInfo->Get(vtkDataObject::DATA_OBJECT()));
53
54 vtkIdList *boundary, *boundaryIds, *cellEdgeNeighbors, *newCell;
55 vtkIdType i, j, currentId, id, id0, id1;
56 vtkCell* cell;
57 bool foundAny, foundNeighbor, done;
58 vtkPoints* newPoints;
59 vtkCellArray* newLines;
60 vtkIdTypeArray* newScalars;
61
62
63
64
65
66
67
68
69
70
71 vtkPolyData* working = vtkPolyData::New();
72 working->CopyStructure(input);
73 working->BuildCells();
74 working->BuildLinks();
75
76
77 boundary = vtkIdList::New();
78 boundaryIds = vtkIdList::New();
79 cellEdgeNeighbors = vtkIdList::New();
80 newCell = vtkIdList::New();
81 newPoints = vtkPoints::New();
82 newLines = vtkCellArray::New();
83 newScalars = vtkIdTypeArray::New();
84
85
86 for (i=0; i<working->GetNumberOfCells(); i++)
87 {
88 cell = working->GetCell(i);
89 for (j=0; j<3; j++)
90 {
91 cellEdgeNeighbors->Initialize();
92 auto edge = cell->GetEdge(j);
93 if (!edge) continue;
94 auto pointIds = edge->GetPointIds();
95 id0 = pointIds->GetId(0);
96 id1 = pointIds->GetId(1);
97 working->GetCellEdgeNeighbors(i,id0,id1,cellEdgeNeighbors);
98 if (cellEdgeNeighbors->GetNumberOfIds()==0)
99 {
100 boundaryIds->InsertUniqueId(id0);
101 boundaryIds->InsertUniqueId(id1);
102 }
103 }
104 }
105
106 if (boundaryIds->GetNumberOfIds() == 0)
107 {
108 output->SetPoints(newPoints);
109 output->SetLines(newLines);
110 output->GetPointData()->SetScalars(newScalars);
111
112 newPoints->Delete();
113 newLines->Delete();
114 newScalars->Delete();
115
116 boundary->Delete();
117 boundaryIds->Delete();
118 cellEdgeNeighbors->Delete();
119 newCell->Delete();
120
121
122
123 working->Initialize();
124 working->Delete();
125
126 return 1;
127 }
128
129 foundAny = false;
130 foundNeighbor = false;
131 done = false;
132 currentId = -1;
133 int loopCount = 0;
134 bool isBoundaryEdge;
135
136 while (!done)
137 {
138 foundAny = false;
139 foundNeighbor = false;
140
141 for (i=0; i<boundaryIds->GetNumberOfIds(); i++)
142 {
143 id = boundaryIds->GetId(i);
144 if (id!=-1)
145 {
146 foundAny = true;
147 isBoundaryEdge = false;
148 if (currentId!=-1 && working->IsEdge(currentId,id))
149 {
150 cellEdgeNeighbors->Initialize();
151 working->GetCellEdgeNeighbors(-1,currentId,id,cellEdgeNeighbors);
152 if (cellEdgeNeighbors->GetNumberOfIds() == 1)
153 {
154 isBoundaryEdge = true;
155 }
156 }
157 if ((currentId==-1) || isBoundaryEdge)
158 {
159 foundNeighbor = true;
160 }
161
162 if (foundNeighbor)
163 {
164 currentId = id;
165 boundary->InsertNextId(currentId);
166 boundaryIds->SetId(i,-1);
167 break;
168 }
169 }
170 }
171
172 if ( (((!foundNeighbor)&&(foundAny))||(!foundAny)) && (boundary->GetNumberOfIds() > 2))
173 {
174
175
176
177
178
179
180 newCell->Initialize();
181 newCell->SetNumberOfIds(boundary->GetNumberOfIds());
182 for (j=0; j<boundary->GetNumberOfIds(); j++)
183 {
184 id = boundary->GetId(j);
185 newCell->SetId(j,newPoints->InsertNextPoint(working->GetPoint(id)));
186 newScalars->InsertNextValue(id);
187 }
188
189 newLines->InsertNextCell(newCell);
190
191 currentId = -1;
192 boundary->Initialize();
193 }
194
195 if (!foundAny)
196 {
197 done = true;
198 }
199
200 loopCount++;
201 if (loopCount > 2*boundaryIds->GetNumberOfIds())
202 {
203 int missing = 0;
204 for (int n=0; n<boundaryIds->GetNumberOfIds(); n++)
205 {
206 if (boundaryIds->GetId(n) != -1)
207 {
208 missing++;
209 }
210 }
211 vtkErrorMacro(<<"Can't find adjacent point. Bailing out.");
212 done = true;
213 }
214 }
215
216 output->SetPoints(newPoints);
217 output->SetLines(newLines);
218 output->GetPointData()->SetScalars(newScalars);
219
220
221 newPoints->Delete();
222 newLines->Delete();
223 newScalars->Delete();
224
225 boundary->Delete();
226 boundaryIds->Delete();
227 cellEdgeNeighbors->Delete();
228 newCell->Delete();
229
230
231
232 working->Initialize();
233 working->Delete();
234
235 return 1;
236}