VMTK
Loading...
Searching...
No Matches
vtkvmtkPolyDataScissors.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkPolyDataScissors.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.5 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
23#include "vtkPointData.h"
24#include "vtkPoints.h"
25#include "vtkCellArray.h"
26#include "vtkIntArray.h"
27#include "vtkPolyLine.h"
28#include "vtkInformation.h"
29#include "vtkInformationVector.h"
30#include "vtkObjectFactory.h"
31
32
34
40
42{
43 if (this->CutLine)
44 {
45 this->CutLine->Delete();
46 this->CutLine = NULL;
47 }
48}
49
50int vtkvmtkPolyDataScissors::IsEdgeInCell(vtkPolyData *input, vtkIdType edgePointId0, vtkIdType edgePointId1, vtkIdType cellId)
51{
52 vtkIdType npts;
53 const vtkIdType *pts;
54 vtkIdType i;
55
56 input->GetCellPoints(cellId,npts,pts);
57
58 for (i=0; i<npts; i++)
59 {
60 if ((pts[i]==edgePointId0 && pts[(i+1)%npts]==edgePointId1) || (pts[i]==edgePointId1 && pts[(i+1)%npts]==edgePointId0))
61 {
62 return 1;
63 }
64 }
65
66 return 0;
67}
68
69int vtkvmtkPolyDataScissors::GetCellsOnSameSide(vtkPolyData* input, vtkIdType targetCellId0, vtkIdType targetCellId1, vtkIdType referenceCellId, vtkIdType linePointId0, vtkIdType linePointId1, vtkIdType linePointId2, vtkIdList *cellsOnSameSide)
70{
71 vtkIdType i, j;
72 vtkIdType *cells;
73 vtkIdType previousCellId;
74 vtkIdType npts;
75 const vtkIdType *pts;
76 vtkIdType ncells;
77 vtkIdType edge[2];
78 bool done;
79
80 if (cellsOnSameSide->GetNumberOfIds()>0)
81 {
82 cellsOnSameSide->Initialize();
83 }
84
85 input->GetPointCells(linePointId1,ncells,cells);
86
87 edge[0] = linePointId1;
88
89 input->GetCellPoints(referenceCellId,npts,pts);
90
91 edge[1] = -1;
92
93 for (i=0; i<npts; i++)
94 {
95 if (pts[i]!=linePointId1 && pts[i]!=linePointId0)
96 {
97 edge[1] = pts[i];
98 break;
99 }
100 }
101
102 if (edge[1]==-1)
103 {
104 vtkErrorMacro(<<"Bad request for CellsOnSameSide of CutLine. Not an edge.");
105 return -1;
106 }
107
108 previousCellId = referenceCellId;
109
110 done = false;
111
112 while(!done)
113 {
114 done = true;
115 for (i=0; i<ncells; i++)
116 {
117 if (cells[i]==previousCellId)
118 continue;
119
120 if (this->IsEdgeInCell(input,edge[0],edge[1],cells[i]))
121 {
122 if ((cells[i]==targetCellId0)||(cells[i]==targetCellId1))
123 {
124 cellsOnSameSide->InsertNextId(cells[i]);
125 return 1;
126 }
127
128 done = false;
129
130 cellsOnSameSide->InsertNextId(cells[i]);
131 previousCellId = cells[i];
132
133 input->GetCellPoints(cells[i],npts,pts);
134 for (j=0; j<npts; j++)
135 {
136 if (pts[j]!=edge[0] && pts[j]!=edge[1])
137 {
138 edge[1] = pts[j];
139 break;
140 }
141 }
142
143 break;
144 }
145 }
146 }
147
148 return 0;
149}
150
152 vtkInformation *vtkNotUsed(request),
153 vtkInformationVector **inputVector,
154 vtkInformationVector *outputVector)
155{
156 vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
157 vtkInformation *outInfo = outputVector->GetInformationObject(0);
158
159 vtkPolyData *input = vtkPolyData::SafeDownCast(
160 inInfo->Get(vtkDataObject::DATA_OBJECT()));
161 vtkPolyData *output = vtkPolyData::SafeDownCast(
162 outInfo->Get(vtkDataObject::DATA_OBJECT()));
163
164 vtkIdType k, i , j;
165 vtkIdType surfacePointId, newPointId;
166 vtkIdType id0, id1, previousId;
167 vtkIdType duplicatePointLocation;
168 vtkIdType numberOfLines, numberOfLinePoints, numberOfInputPoints;
169 vtkIdType cellId, previousCellToEdit;
170 vtkIdType npts;
171 const vtkIdType *pts;
172 vtkDataArray *cutLinePointIdsArray;
173 vtkCell *polyLine;
174 vtkPoints *newPoints;
175 vtkCellArray *newPolys;
176 vtkCellArray *editedCells;
177 vtkIdList *duplicatePointIds;
178 vtkIdList *duplicatedPointIds;
179 vtkIdList *cellsToEdit, *editedCellIds;
180 vtkIdList *cellEdgeNeighbors;
181 vtkIdList *cellsOnSameSide;
182 vtkPointData *inputPD = input->GetPointData();
183 vtkPointData *outputPD = output->GetPointData();
184
185 previousCellToEdit = -1;
186
187 if (this->CutLine==NULL)
188 {
189 vtkErrorMacro(<<"CutLine not specified!");
190 return 1;
191 }
192
193 if (this->CutLinePointIdsArrayName==NULL)
194 {
195 vtkErrorMacro(<<"CutLinePointIdsArrayName not specified!");
196 return 1;
197 }
198
199 cutLinePointIdsArray = this->CutLine->GetPointData()->GetArray(this->CutLinePointIdsArrayName);
200
201 if (cutLinePointIdsArray==NULL)
202 {
203 vtkErrorMacro(<<"CutLinePointIdsArray with name specified does not exist!");
204 return 1;
205 }
206
207 newPoints = vtkPoints::New();
208 newPolys = vtkCellArray::New();
209
210 editedCells = vtkCellArray::New();
211
212 duplicatePointIds = vtkIdList::New();
213 duplicatedPointIds = vtkIdList::New();
214 cellsToEdit = vtkIdList::New();
215 editedCellIds = vtkIdList::New();
216 cellEdgeNeighbors = vtkIdList::New();
217 cellsOnSameSide = vtkIdList::New();
218
219 numberOfInputPoints = input->GetNumberOfPoints();
220 newPoints->DeepCopy(input->GetPoints());
221
222 numberOfLinePoints = this->CutLine->GetNumberOfPoints();
223
224 numberOfLines = this->CutLine->GetNumberOfCells();
225
226 input->BuildCells();
227 input->BuildLinks();
228 this->CutLine->BuildCells();
229
230 for (k=0; k<numberOfLines; k++)
231 {
232 polyLine = this->CutLine->GetCell(k);
233 if (polyLine->GetCellType() != VTK_LINE && polyLine->GetCellType() != VTK_POLY_LINE)
234 {
235 continue;
236 }
237
238 numberOfLinePoints = polyLine->GetNumberOfPoints();
239
240 duplicatedPointIds->Initialize();
241 duplicatePointIds->Initialize();
242 cellsToEdit->Initialize();
243
244 for (i=0; i<numberOfLinePoints; i++)
245 {
246 surfacePointId = static_cast<vtkIdType>(cutLinePointIdsArray->GetComponent(polyLine->GetPointId(i),0));
247 newPointId = newPoints->InsertNextPoint(input->GetPoint(surfacePointId));
248 duplicatedPointIds->InsertNextId(surfacePointId);
249 duplicatePointIds->InsertNextId(newPointId);
250 }
251
252 for (i=0; i<numberOfLinePoints-1; i++)
253 {
254 id0 = static_cast<vtkIdType>(cutLinePointIdsArray->GetComponent(polyLine->GetPointId(i),0));
255 id1 = static_cast<vtkIdType>(cutLinePointIdsArray->GetComponent(polyLine->GetPointId(i+1),0));
256
257 if (id0==id1)
258 {
259 vtkErrorMacro(<<"Coincident points found on CutLine! ");
260 return 1;
261 }
262
263 if (!input->IsEdge(id0,id1))
264 {
265 vtkErrorMacro(<<"CutLine is not entirely defined on mesh edges! ");
266 return 1;
267 }
268
269 cellEdgeNeighbors->Initialize();
270 input->GetCellEdgeNeighbors(-1,id0,id1,cellEdgeNeighbors);
271
272 if (cellEdgeNeighbors->GetNumberOfIds()<2)
273 {
274 vtkWarningMacro(<<"Surface boundary or hole found.");
275 continue;
276 }
277
278 if (cellEdgeNeighbors->GetNumberOfIds()>2)
279 {
280 vtkErrorMacro(<<"Surface is non-manifold! ");
281 return 1;
282 }
283
284 // try to choose the first cell in order to maintain consistency in the orientation, if at all possible
285
286 // this is a bit too naive (it basically doesn't work most of the times). TODO: special treatment of joints + consistency.
287 if (i==0)
288 {
289 if (k==0)
290 {
291 cellsToEdit->InsertUniqueId(cellEdgeNeighbors->GetId(0));
292 previousCellToEdit = cellEdgeNeighbors->GetId(0);
293 }
294 else
295 {
296 if (cellsToEdit->IsId(cellEdgeNeighbors->GetId(0))!=-1)
297 {
298 cellsToEdit->InsertUniqueId(cellEdgeNeighbors->GetId(0));
299 previousCellToEdit = cellEdgeNeighbors->GetId(0);
300 }
301 else
302 {
303 cellsToEdit->InsertUniqueId(cellEdgeNeighbors->GetId(1));
304 previousCellToEdit = cellEdgeNeighbors->GetId(1);
305 }
306 }
307 continue;
308 }
309
310 previousId = static_cast<vtkIdType>(cutLinePointIdsArray->GetComponent(polyLine->GetPointId(i-1),0));
311
312 // select the cells lying on the same side of the cut line.
313
314 cellsOnSameSide->Initialize();
315 if (this->GetCellsOnSameSide(input,cellEdgeNeighbors->GetId(0),cellEdgeNeighbors->GetId(1),previousCellToEdit,previousId,id0,id1,cellsOnSameSide))
316 {
317 for (j=0; j<cellsOnSameSide->GetNumberOfIds(); j++)
318 {
319 cellsToEdit->InsertUniqueId(cellsOnSameSide->GetId(j));
320 }
321 previousCellToEdit = cellsOnSameSide->GetId(cellsOnSameSide->GetNumberOfIds()-1);
322 }
323 else
324 {
325 // well, we could try to, going the other way and getting the complementary cells, but there's no time now (this is a TODO)
326 vtkErrorMacro(<<"Hole in the surface found. Can't determine line orientation.");
327 return 1;
328 }
329 }
330
331 for (i=0; i<cellsToEdit->GetNumberOfIds(); i++)
332 {
333 cellId = cellsToEdit->GetId(i);
334 input->GetCellPoints(cellId,npts,pts);
335
336 editedCellIds->InsertUniqueId(cellId);
337 editedCells->InsertNextCell(npts);
338 for (j=0; j<npts; j++)
339 {
340 duplicatePointLocation = duplicatedPointIds->IsId(pts[j]);
341 if (duplicatePointLocation!=-1)
342 {
343 editedCells->InsertCellPoint(duplicatePointIds->GetId(duplicatePointLocation));
344 }
345 else
346 {
347 editedCells->InsertCellPoint(pts[j]);
348 }
349 }
350 }
351 }
352
353 input->GetPolys()->InitTraversal();
354 for (i=0; i<input->GetPolys()->GetNumberOfCells(); i++)
355 {
356 input->GetPolys()->GetNextCell(npts,pts);
357 if (editedCellIds->IsId(i)==-1)
358 {
359 newPolys->InsertNextCell(npts,pts);
360 }
361 }
362
363 editedCells->InitTraversal();
364 for (i=0; i<editedCells->GetNumberOfCells(); i++)
365 {
366 editedCells->GetNextCell(npts,pts);
367 newPolys->InsertNextCell(npts,pts);
368 }
369
370 output->SetPoints(newPoints);
371 output->SetPolys(newPolys);
372
373 outputPD->CopyAllocate(inputPD,numberOfInputPoints+numberOfLinePoints);
374
375 for (i=0; i<numberOfInputPoints; i++)
376 {
377 outputPD->CopyData(inputPD,i,i);
378 }
379
380 for (i=0; i<duplicatedPointIds->GetNumberOfIds(); i++)
381 {
382 outputPD->CopyData(inputPD,duplicatedPointIds->GetId(i),duplicatePointIds->GetId(i));
383 }
384
385 duplicatePointIds->Delete();
386 duplicatedPointIds->Delete();
387 cellsToEdit->Delete();
388 cellEdgeNeighbors->Delete();
389 cellsOnSameSide->Delete();
390 editedCells->Delete();
391 editedCellIds->Delete();
392 newPoints->Delete();
393 newPolys->Delete();
394
395 return 1;
396}
397
398void vtkvmtkPolyDataScissors::PrintSelf(std::ostream& os, vtkIndent indent)
399{
400 this->Superclass::PrintSelf(os,indent);
401}
Not implemented / experimental. Cuts a surface open along a polyline that follows existing mesh edges...
void PrintSelf(std::ostream &os, vtkIndent indent) override
int GetCellsOnSameSide(vtkPolyData *input, vtkIdType targetCellId0, vtkIdType targetCellId1, vtkIdType referenceCellId, vtkIdType linePointId0, vtkIdType linePointId1, vtkIdType linePointId2, vtkIdList *cellsOnSameSide)
int IsEdgeInCell(vtkPolyData *input, vtkIdType edgePointId0, vtkIdType edgePointId1, vtkIdType cellId)
virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *) override
vtkStandardNewMacro(vtkvmtkPolyDataScissors)