forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkLoopSubdivisionFilter.cxx
374 lines (335 loc) · 9.92 KB
/
vtkLoopSubdivisionFilter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLoopSubdivisionFilter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkLoopSubdivisionFilter.h"
#include "vtkSmartPointer.h"
#include "vtkMath.h"
#include "vtkCell.h"
#include "vtkCellArray.h"
#include "vtkEdgeTable.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkStandardNewMacro(vtkLoopSubdivisionFilter);
static double LoopWeights[4] =
{.375, .375, .125, .125};
int vtkLoopSubdivisionFilter::GenerateSubdivisionPoints (vtkPolyData *inputDS,vtkIntArray *edgeData, vtkPoints *outputPts, vtkPointData *outputPD)
{
double *weights;
vtkIdType *pts = 0;
vtkIdType numPts, cellId, newId;
int edgeId;
vtkIdType npts;
vtkIdType p1, p2;
vtkCellArray *inputPolys=inputDS->GetPolys();
vtkSmartPointer<vtkIdList> cellIds = vtkSmartPointer<vtkIdList>::New();
vtkSmartPointer<vtkIdList> stencil = vtkSmartPointer<vtkIdList>::New();
vtkPoints *inputPts=inputDS->GetPoints();
vtkPointData *inputPD=inputDS->GetPointData();
weights = new double[256];
// Create an edge table to keep track of which edges we've processed
vtkSmartPointer<vtkEdgeTable> edgeTable =
vtkSmartPointer<vtkEdgeTable>::New();
edgeTable->InitEdgeInsertion(inputDS->GetNumberOfPoints());
// Generate even points. these are derived from the old points
numPts = inputDS->GetNumberOfPoints();
for (vtkIdType ptId=0; ptId < numPts; ptId++)
{
this->GenerateEvenStencil (ptId, inputDS, stencil, weights);
this->InterpolatePosition (inputPts, outputPts, stencil, weights);
outputPD->InterpolatePoint (inputPD, ptId, stencil, weights);
}
// Generate odd points. These will be inserted into the new dataset
for (cellId=0, inputPolys->InitTraversal();
inputPolys->GetNextCell(npts, pts); cellId++)
{
if ( inputDS->GetCellType(cellId) != VTK_TRIANGLE )
{
continue;
}
// start with one edge
p1 = pts[2];
p2 = pts[0];
for (edgeId=0; edgeId < 3; edgeId++)
{
// Do we need to create a point on this edge?
if (edgeTable->IsEdge (p1, p2) == -1)
{
edgeTable->InsertEdge (p1, p2);
inputDS->GetCellEdgeNeighbors (-1, p1, p2, cellIds);
if (cellIds->GetNumberOfIds() == 1)
{
// Compute new Position and PointData using the same subdivision scheme
stencil->SetNumberOfIds(2);
stencil->SetId(0,p1);
stencil->SetId(1,p2);
weights[0] = .5; weights[1] = .5;
} // boundary edge
else if (cellIds->GetNumberOfIds() == 2)
{
this->GenerateOddStencil (p1, p2,
inputDS, stencil, weights);
}
else
{
delete [] weights;
vtkErrorMacro ("Dataset is non-manifold and cannot be subdivided.");
return 0;
}
newId = this->InterpolatePosition (inputPts, outputPts,
stencil, weights);
outputPD->InterpolatePoint (inputPD, newId, stencil, weights);
}
else // we have already created a point on this edge. find it
{
newId = this->FindEdge (inputDS, cellId, p1, p2, edgeData, cellIds);
}
edgeData->InsertComponent(cellId,edgeId,newId);
p1 = p2;
if (edgeId < 2)
{
p2 = pts[edgeId + 1];
}
} // each interior edge
} // each cell
// cleanup
delete [] weights;
return 1;
}
void vtkLoopSubdivisionFilter::GenerateEvenStencil (vtkIdType p1,
vtkPolyData *polys,
vtkIdList *stencilIds,
double *weights)
{
vtkIdList *cellIds = vtkIdList::New();
vtkIdList *ptIds = vtkIdList::New();
vtkCell *cell;
int i;
vtkIdType j;
vtkIdType startCell, nextCell;
vtkIdType p, p2;
vtkIdType bp1, bp2;
vtkIdType K;
double beta, cosSQ;
// Get the cells that use this point
polys->GetPointCells (p1, cellIds);
vtkIdType numCellsInLoop = cellIds->GetNumberOfIds();
if (numCellsInLoop < 1)
{
vtkWarningMacro("numCellsInLoop < 1: " << numCellsInLoop);
stencilIds->Reset();
return;
}
// Find an edge to start with that contains p1
polys->GetCellPoints (cellIds->GetId(0), ptIds);
p2 = ptIds->GetId(0);
i = 1;
while (p1 == p2)
{
p2 = ptIds->GetId(i++);
}
polys->GetCellEdgeNeighbors (-1, p1, p2, cellIds);
nextCell = cellIds->GetId(0);
bp2 = -1;
bp1 = p2;
if (cellIds->GetNumberOfIds() == 1)
{
startCell = -1;
}
else
{
startCell = cellIds->GetId(1);
}
stencilIds->Reset();
stencilIds->InsertNextId(p2);
// walk around the loop counter-clockwise and get cells
for (j = 0; j < numCellsInLoop; j++)
{
cell = polys->GetCell(nextCell);
p = -1;
for (i = 0; i < 3; i++)
{
if ((p = cell->GetPointId(i)) != p1 && cell->GetPointId(i) != p2)
{
break;
}
}
p2 = p;
stencilIds->InsertNextId (p2);
polys->GetCellEdgeNeighbors (nextCell, p1, p2, cellIds);
if (cellIds->GetNumberOfIds() != 1)
{
bp2 = p2;
j++;
break;
}
nextCell = cellIds->GetId(0);
}
// now walk around the other way. this will only happen if there
// is a boundary cell left that we have not visited
nextCell = startCell;
p2 = bp1;
for (; j < numCellsInLoop && startCell != -1; j++)
{
cell = polys->GetCell(nextCell);
p = -1;
for (i = 0; i < 3; i++)
{
if ((p = cell->GetPointId(i)) != p1 && cell->GetPointId(i) != p2)
{
break;
}
}
p2 = p;
stencilIds->InsertNextId (p2);
polys->GetCellEdgeNeighbors (nextCell, p1, p2, cellIds);
if (cellIds->GetNumberOfIds() != 1)
{
bp1 = p2;
break;
}
nextCell = cellIds->GetId(0);
}
if (bp2 != -1) // boundary edge
{
stencilIds->SetNumberOfIds(3);
stencilIds->SetId(0,bp2);
stencilIds->SetId(1,bp1);
stencilIds->SetId(2,p1);
weights[0] = .125;
weights[1] = .125;
weights[2] = .75;
}
else
{
K = stencilIds->GetNumberOfIds();
// Remove last id. It's a duplicate of the first
K--;
if (K > 3)
{
// Generate weights
#define VTK_PI vtkMath::Pi()
cosSQ = .375 + .25 * cos (2.0 * VTK_PI / (double) K);
cosSQ = cosSQ * cosSQ;
beta = (.625 - cosSQ) / (double) K;
}
else
{
beta = 3.0 / 16.0;
}
for (j = 0; j < K; j++)
{
weights[j] = beta;
}
weights[K] = 1.0 - K * beta;
stencilIds->SetId (K,p1);
}
cellIds->Delete();
ptIds->Delete();
}
void vtkLoopSubdivisionFilter::GenerateOddStencil (vtkIdType p1, vtkIdType p2,
vtkPolyData *polys,
vtkIdList *stencilIds,
double *weights)
{
vtkIdList *cellIds = vtkIdList::New();
vtkCell *cell;
int i;
vtkIdType cell0, cell1;
vtkIdType p3=0, p4=0;
polys->GetCellEdgeNeighbors (-1, p1, p2, cellIds);
cell0 = cellIds->GetId(0);
cell1 = cellIds->GetId(1);
cell = polys->GetCell(cell0);
for (i = 0; i < 3; i++)
{
if ((p3 = cell->GetPointId(i)) != p1 && cell->GetPointId(i) != p2)
{
break;
}
}
cell = polys->GetCell(cell1);
for (i = 0; i < 3; i++)
{
if ((p4 = cell->GetPointId(i)) != p1 && cell->GetPointId(i) != p2)
{
break;
}
}
stencilIds->SetNumberOfIds (4);
stencilIds->SetId(0, p1);
stencilIds->SetId(1, p2);
stencilIds->SetId(2, p3);
stencilIds->SetId(3, p4);
for (i = 0; i < stencilIds->GetNumberOfIds (); i++)
{
weights[i] = LoopWeights[i];
}
cellIds->Delete();
}
int vtkLoopSubdivisionFilter::RequestUpdateExtent(
vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
int numPieces, ghostLevel;
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
if (!this->Superclass::RequestUpdateExtent(request, inputVector,
outputVector))
{
return 0;
}
numPieces =
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
ghostLevel =
outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
if (numPieces > 1 && this->NumberOfSubdivisions > 0)
{
inInfo->Set(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS(),
ghostLevel + 1);
}
return 1;
}
int vtkLoopSubdivisionFilter::RequestData(
vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkPolyData *input = vtkPolyData::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkCellArray *polys = input->GetPolys();
int hasTris = 0;
vtkIdType numPts = 0, *pts = 0;
input->BuildLinks();
polys->InitTraversal();
while (polys->GetNextCell(numPts, pts))
{
if (numPts == 3)
{
if (input->IsTriangle(pts[0], pts[1], pts[2]))
{
hasTris = 1;
break;
}
}
}
if (!hasTris)
{
vtkWarningMacro("vtkLoopSubdivisionFilter only operates on triangles, but this data set has no triangles to operate on.")
return 0;
}
return this->Superclass::RequestData(request, inputVector, outputVector);
}