1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:  TestPolyDataRemoveDeletedCells.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10    This software is distributed WITHOUT ANY WARRANTY; without even
11    the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12    PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkCell.h"
16 #include "vtkCellData.h"
17 #include "vtkDataArray.h"
18 #include "vtkIdList.h"
19 #include "vtkIntArray.h"
20 #include "vtkPoints.h"
21 #include "vtkPolyData.h"
22 #include "vtkSmartPointer.h"
23 
24 #define VTK_SUCCESS 0
25 #define VTK_FAILURE 1
26 
CheckDeletedCells()27 int CheckDeletedCells()
28 {
29   vtkPoints* points = vtkPoints::New();
30   for(vtkIdType i=0;i<10;i++)
31   {
32     points->InsertNextPoint(i, i, i);
33   }
34   vtkSmartPointer<vtkPolyData> poly = vtkSmartPointer<vtkPolyData>::New();
35   poly->SetPoints(points);
36   points->Delete();
37   poly->Allocate(10);
38   for(vtkIdType i=0;i<10;i++)
39   {
40     poly->InsertNextCell(VTK_VERTEX, 1, &i);
41   }
42   poly->BuildCells();
43   if(poly->GetNumberOfPoints() != 10 || poly->GetNumberOfCells() != 10)
44   {
45     std::cout << "Wrong number of input points or cells!" << std::endl;
46     return VTK_FAILURE;
47   }
48 
49   for(vtkIdType i=0;i<5;i++)
50   {
51     poly->DeleteCell(i*2+1);
52   }
53   poly->RemoveDeletedCells();
54 
55   if(poly->GetNumberOfCells() != 5)
56   {
57     std::cout << "Wrong number of removed cells!" << std::endl;
58     return VTK_FAILURE;
59   }
60 
61   for(vtkIdType i=0;i<5;i++)
62   {
63     vtkCell* cell = poly->GetCell(i);
64     if(cell->GetPointId(0) != i*2)
65     {
66       std::cout << "Wrong point of cell " << i << ", should be point " << i*2
67                 << " but is " << cell->GetPointId(0) << std::endl;
68       return VTK_FAILURE;
69     }
70   }
71 
72 
73   return VTK_SUCCESS;
74 }
75 
TestPolyDataRemoveDeletedCells(int,char * [])76 int TestPolyDataRemoveDeletedCells (int, char*[])
77 {
78   int numCells = 8;
79 
80   vtkPoints *pts = vtkPoints::New();
81 
82   pts->InsertNextPoint(1, 0, 0); // 0
83   pts->InsertNextPoint(3, 0, 0); // 1
84   pts->InsertNextPoint(5, 0, 0); // 2
85   pts->InsertNextPoint(7, 0, 0); // 3
86 
87   pts->InsertNextPoint(0, 2, 0); // 4
88   pts->InsertNextPoint(2, 2, 0); // 5
89   pts->InsertNextPoint(4, 2, 0); // 6
90   pts->InsertNextPoint(6, 2, 0); // 7
91 
92   pts->InsertNextPoint(9, 0, 0); // 8
93   pts->InsertNextPoint(11, 0, 0); // 9
94   pts->InsertNextPoint(8, 2, 0); // 10
95   pts->InsertNextPoint(10, 2, 0); // 11
96 
97   vtkPolyData *pd = vtkPolyData::New();
98   pd->Allocate(numCells);
99   pd->SetPoints(pts);
100 
101   pts->Delete();
102 
103   vtkIdList *cell;
104   cell = vtkIdList::New();
105 
106   // adding different cells in arbitrary order
107 
108   // 1
109 
110   cell->InsertNextId(4);
111   cell->InsertNextId(0);
112   cell->InsertNextId(5);
113   cell->InsertNextId(1);
114   cell->InsertNextId(6);
115 
116   pd->InsertNextCell(VTK_TRIANGLE_STRIP, cell);
117 
118   cell->Reset();
119 
120   // 2
121 
122   cell->InsertNextId(1);
123   cell->InsertNextId(6);
124   cell->InsertNextId(2);
125   cell->InsertNextId(7);
126   cell->InsertNextId(3);
127 
128   pd->InsertNextCell(VTK_TRIANGLE_STRIP, cell);
129 
130   cell->Reset();
131 
132   // 3
133 
134   cell->InsertNextId(0);
135 
136   pd->InsertNextCell(VTK_VERTEX, cell);
137 
138   cell->Reset();
139 
140   // 4
141 
142   cell->InsertNextId(0);
143   cell->InsertNextId(1);
144   cell->InsertNextId(2);
145   cell->InsertNextId(3);
146   cell->InsertNextId(8);
147   cell->InsertNextId(11);
148   cell->InsertNextId(10);
149   cell->InsertNextId(7);
150   cell->InsertNextId(6);
151   cell->InsertNextId(5);
152   cell->InsertNextId(4);
153 
154   pd->InsertNextCell(VTK_POLY_LINE, cell);
155 
156   cell->Reset();
157 
158   // 5
159 
160   cell->InsertNextId(3);
161   cell->InsertNextId(8);
162   cell->InsertNextId(9);
163   cell->InsertNextId(11);
164   cell->InsertNextId(10);
165   cell->InsertNextId(7);
166 
167   pd->InsertNextCell(VTK_POLYGON, cell);
168 
169   cell->Reset();
170 
171   // 6
172 
173   cell->InsertNextId(1);
174 
175   pd->InsertNextCell(VTK_VERTEX, cell);
176 
177   cell->Reset();
178 
179   // 7
180 
181   cell->InsertNextId(3);
182   cell->InsertNextId(10);
183 
184   pd->InsertNextCell(VTK_LINE, cell);
185 
186   cell->Reset();
187 
188   // 8
189 
190   cell->InsertNextId(8);
191   cell->InsertNextId(9);
192   cell->InsertNextId(11);
193 
194   pd->InsertNextCell(VTK_TRIANGLE, cell);
195 
196   cell->Delete();
197 
198   vtkIntArray *scalars = vtkIntArray::New();
199 
200   scalars->InsertNextValue(VTK_TRIANGLE_STRIP);
201   scalars->InsertNextValue(VTK_TRIANGLE_STRIP);
202   scalars->InsertNextValue(VTK_VERTEX);
203   scalars->InsertNextValue(VTK_POLY_LINE);
204   scalars->InsertNextValue(VTK_POLYGON);
205   scalars->InsertNextValue(VTK_VERTEX);
206   scalars->InsertNextValue(VTK_LINE);
207   scalars->InsertNextValue(VTK_TRIANGLE);
208 
209   pd->GetCellData()->SetScalars(scalars);
210 
211   scalars->Delete();
212 
213   // delete some cells
214 
215   pd->DeleteCell(0);
216   pd->DeleteCell(3);
217   pd->DeleteCell(5);
218   pd->DeleteCell(7);
219 
220   int types[] = {
221     //VTK_TRIANGLE_STRIP,
222     VTK_TRIANGLE_STRIP,
223     VTK_VERTEX,
224     //VTK_POLY_LINE,
225     VTK_POLYGON,
226     //VTK_VERTEX,
227     VTK_LINE,
228     //VTK_TRIANGLE
229   };
230 
231   pd->RemoveDeletedCells();
232 
233   vtkIntArray *newScalars = vtkArrayDownCast<vtkIntArray>(pd->GetCellData()->GetScalars());
234 
235   int retVal = VTK_SUCCESS;
236 
237   if (pd->GetNumberOfCells() != numCells-4)
238   {
239     std::cout << "Number of cells does not match!" << std::endl;
240     retVal = VTK_FAILURE;
241   }
242 
243   for (vtkIdType i = 0; i < pd->GetNumberOfCells(); i++)
244   {
245     if (pd->GetCellType(i) != newScalars->GetValue(i))
246     {
247       std::cout << "Cell " << i << " has wrong data. Value should be " << pd->GetCellType(i)
248                 << " but is " << newScalars->GetValue(i) << std::endl;
249       retVal = VTK_FAILURE;
250     }
251 
252     if (pd->GetCellType(i) != types[i])
253     {
254       std::cout << "Cell " << i << " has wrong type. Value should be " << pd->GetCellType(i)
255                 << " but is " << newScalars->GetValue(i) << std::endl;
256       retVal = VTK_FAILURE;
257     }
258   }
259 
260   pd->Delete();
261   if(retVal != VTK_SUCCESS)
262   {
263     return retVal;
264   }
265 
266   return CheckDeletedCells();
267 }
268