forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkStructuredData.cxx
601 lines (544 loc) · 15.1 KB
/
vtkStructuredData.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStructuredData.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 "vtkStructuredData.h"
#include "vtkIdList.h"
#include "vtkObjectFactory.h"
#include "vtkStructuredExtent.h"
#include <algorithm>
#include <cassert>
// Return the topological dimension of the data (e.g., 0, 1, 2, or 3D).
int vtkStructuredData::GetDataDimension(int dataDescription)
{
switch (dataDescription)
{
case VTK_EMPTY:
return 0; // Should I put -1?
case VTK_SINGLE_POINT:
return 0;
case VTK_X_LINE:
case VTK_Y_LINE:
case VTK_Z_LINE:
return 1;
case VTK_XY_PLANE:
case VTK_YZ_PLANE:
case VTK_XZ_PLANE:
return 2;
case VTK_XYZ_GRID:
return 3;
default:
return -1;
}
}
//------------------------------------------------------------------------------
int vtkStructuredData::GetDataDimension( int ext[6] )
{
int dataDescription = vtkStructuredData::GetDataDescriptionFromExtent( ext );
return( vtkStructuredData::GetDataDimension( dataDescription ) );
}
//------------------------------------------------------------------------------
// Returns the data description given the dimensions (eg. VTK_SINGLE_POINT,
// VTK_X_LINE, VTK_XY_PLANE etc.)
int vtkStructuredData::GetDataDescription(int dims[3])
{
int tempDims[3];
// It is essential that dims != tempDims, then alone will SetDimensions()
// return the correct data description.
tempDims[0] = dims[0] + 1;
tempDims[1] = dims[1] + 1;
tempDims[2] = dims[2] + 1;
return vtkStructuredData::SetDimensions(dims, tempDims);
}
//------------------------------------------------------------------------------
// Given the extent, returns the data description given the dimensions
// (eg. VTK_SINGLE_POINT,VTK_X_LINE, VTK_XY_PLANE etc.)
int vtkStructuredData::GetDataDescriptionFromExtent(int ext[6] )
{
int dims[3];
vtkStructuredExtent::GetDimensions( ext, dims );
return( vtkStructuredData::GetDataDescription( dims ) );
}
//------------------------------------------------------------------------------
// Specify the dimensions of a regular, rectangular dataset. The input is
// the new dimensions (inDim) and the current dimensions (dim). The function
// returns the dimension of the dataset (0-3D). If the dimensions are
// improperly specified a -1 is returned. If the dimensions are unchanged, a
// value of 100 is returned.
int vtkStructuredData::SetDimensions(int inDim[3], int dim[3])
{
int dataDim, i;
int dataDescription = VTK_UNCHANGED;
if ( inDim[0] != dim[0] || inDim[1] != dim[1] || inDim[2] != dim[2] )
{
for (dataDim=0, i=0; i<3 ; i++)
{
dim[i] = inDim[i];
if (inDim[i] > 1)
{
dataDim++;
}
}
if ( inDim[0]<1 || inDim[1]<1 || inDim[2]<1 )
{
return VTK_EMPTY;
}
if ( dataDim == 3 )
{
dataDescription = VTK_XYZ_GRID;
}
else if ( dataDim == 2)
{
if ( inDim[0] == 1 )
{
dataDescription = VTK_YZ_PLANE;
}
else if ( inDim[1] == 1 )
{
dataDescription = VTK_XZ_PLANE;
}
else
{
dataDescription = VTK_XY_PLANE;
}
}
else if ( dataDim == 1 )
{
if ( inDim[0] != 1 )
{
dataDescription = VTK_X_LINE;
}
else if ( inDim[1] != 1 )
{
dataDescription = VTK_Y_LINE;
}
else
{
dataDescription = VTK_Z_LINE;
}
}
else
{
dataDescription = VTK_SINGLE_POINT;
}
}
return dataDescription;
}
//------------------------------------------------------------------------------
// Specify the extent of a regular, rectangular dataset. The input is
// the new extent (inExt) and the current extent (ext). The function
// returns the dimension of the dataset (0-3D). If the extents are
// improperly specified a -1 is returned. If the dimensions are unchanged, a
// value of 100 is returned.
int vtkStructuredData::SetExtent(int inExt[6], int ext[6])
{
int dataDim, i;
int dataDescription;
if ( inExt[0] == ext[0] && inExt[1] == ext[1] &&
inExt[2] == ext[2] && inExt[3] == ext[3] &&
inExt[4] == ext[4] && inExt[5] == ext[5])
{
return VTK_UNCHANGED;
}
dataDim = 0;
for (i=0; i<3 ; ++i)
{
ext[i*2] = inExt[i*2];
ext[i*2+1] = inExt[i*2+1];
if (inExt[i*2] < inExt[i*2+1])
{
dataDim++;
}
}
if ( inExt[0]>inExt[1] || inExt[2]>inExt[3] || inExt[4]>inExt[5] )
{
return VTK_EMPTY;
}
if ( dataDim == 3 )
{
dataDescription = VTK_XYZ_GRID;
}
else if ( dataDim == 2)
{
if ( inExt[0] == inExt[1] )
{
dataDescription = VTK_YZ_PLANE;
}
else if ( inExt[2] == inExt[3] )
{
dataDescription = VTK_XZ_PLANE;
}
else
{
dataDescription = VTK_XY_PLANE;
}
}
else if ( dataDim == 1 )
{
if ( inExt[0] < inExt[1] )
{
dataDescription = VTK_X_LINE;
}
else if ( inExt[2] < inExt[3] )
{
dataDescription = VTK_Y_LINE;
}
else
{
dataDescription = VTK_Z_LINE;
}
}
else
{
dataDescription = VTK_SINGLE_POINT;
}
return dataDescription;
}
//------------------------------------------------------------------------------
// Get the points defining a cell. (See vtkDataSet for more info.)
void vtkStructuredData::GetCellPoints(vtkIdType cellId, vtkIdList *ptIds,
int dataDescription, int dim[3])
{
int loc[3];
vtkIdType idx, npts;
int iMin, iMax, jMin, jMax, kMin, kMax;
vtkIdType d01 = static_cast<vtkIdType>(dim[0])*dim[1];
ptIds->Reset();
iMin = iMax = jMin = jMax = kMin = kMax = 0;
switch (dataDescription)
{
case VTK_EMPTY:
return;
case VTK_SINGLE_POINT: // cellId can only be = 0
break;
case VTK_X_LINE:
iMin = cellId;
iMax = cellId + 1;
break;
case VTK_Y_LINE:
jMin = cellId;
jMax = cellId + 1;
break;
case VTK_Z_LINE:
kMin = cellId;
kMax = cellId + 1;
break;
case VTK_XY_PLANE:
iMin = cellId % (dim[0]-1);
iMax = iMin + 1;
jMin = cellId / (dim[0]-1);
jMax = jMin + 1;
break;
case VTK_YZ_PLANE:
jMin = cellId % (dim[1]-1);
jMax = jMin + 1;
kMin = cellId / (dim[1]-1);
kMax = kMin + 1;
break;
case VTK_XZ_PLANE:
iMin = cellId % (dim[0]-1);
iMax = iMin + 1;
kMin = cellId / (dim[0]-1);
kMax = kMin + 1;
break;
case VTK_XYZ_GRID:
iMin = cellId % (dim[0] - 1);
iMax = iMin + 1;
jMin = (cellId / (dim[0] - 1)) % (dim[1] - 1);
jMax = jMin + 1;
kMin = cellId / (static_cast<vtkIdType>(dim[0] - 1) * (dim[1] - 1));
kMax = kMin + 1;
break;
default:
assert("check: impossible case." && 0); // reaching this line is a bug.
break;
}
// Extract point ids
for (npts=0,loc[2]=kMin; loc[2]<=kMax; loc[2]++)
{
for (loc[1]=jMin; loc[1]<=jMax; loc[1]++)
{
for (loc[0]=iMin; loc[0]<=iMax; loc[0]++)
{
idx = loc[0] + loc[1]*static_cast<vtkIdType>(dim[0]) + loc[2]*d01;
ptIds->InsertId(npts++,idx);
}
}
}
}
//------------------------------------------------------------------------------
// Get the cells using a point. (See vtkDataSet for more info.)
void vtkStructuredData::GetPointCells(vtkIdType ptId, vtkIdList *cellIds,
int dim[3])
{
vtkIdType cellDim[3];
int ptLoc[3], cellLoc[3];
int i, j;
vtkIdType cellId;
static const int offset[8][3] = {{-1,0,0}, {-1,-1,0}, {-1,-1,-1}, {-1,0,-1},
{0,0,0}, {0,-1,0}, {0,-1,-1}, {0,0,-1}};
for (i=0; i<3; i++)
{
cellDim[i] = dim[i] - 1;
if (cellDim[i] == 0)
{
cellDim[i] = 1;
}
}
// Get the location of the point
//
ptLoc[0] = ptId % dim[0];
ptLoc[1] = (ptId / dim[0]) % dim[1];
ptLoc[2] = ptId / (static_cast<vtkIdType>(dim[0])*dim[1]);
// From the point location, compute the cell locations. There are at
// most eight possible.
//
cellIds->Reset();
for (j=0; j<8; j++)
{
for (i=0; i<3; i++)
{
cellLoc[i] = ptLoc[i] + offset[j][i];
if ( cellLoc[i] < 0 || cellLoc[i] >= cellDim[i] )
{
break;
}
}
if ( i >= 3 ) //add cell
{
cellId = cellLoc[0] + cellLoc[1]*cellDim[0] +
cellLoc[2]*cellDim[0]*cellDim[1];
cellIds->InsertNextId(cellId);
}
}
}
//------------------------------------------------------------------------------
void vtkStructuredData::GetCellNeighbors(vtkIdType cellId, vtkIdList *ptIds,
vtkIdList *cellIds, int dim[3], int seedLoc[3])
{
vtkIdType numPts = ptIds->GetNumberOfIds();
cellIds->Reset();
// Start by finding the "space" of the points in i-j-k space.
// The points define a point, line, or plane in topological space,
// which results in degrees of freedom in three, two or one direction.
// The numbers of DOF determines which neighbors to select.
// Start by finding a seed point
vtkIdType id0 = seedLoc[0] + seedLoc[1]*dim[0] + seedLoc[2]*dim[0]*dim[1];
// This defines the space around the seed
int offset[8][3];
offset[0][0] = -1; offset[0][1] = -1; offset[0][2] = -1;
offset[1][0] = 0; offset[1][1] = -1; offset[1][2] = -1;
offset[2][0] = -1; offset[2][1] = 0; offset[2][2] = -1;
offset[3][0] = 0; offset[3][1] = 0; offset[3][2] = -1;
offset[4][0] = -1; offset[4][1] = -1; offset[4][2] = 0;
offset[5][0] = 0; offset[5][1] = -1; offset[5][2] = 0;
offset[6][0] = -1; offset[6][1] = 0; offset[6][2] = 0;
offset[7][0] = 0; offset[7][1] = 0; offset[7][2] = 0;
// For the rest of the points, trim the seed region
// This is essentially an intersection of edge neighbors.
for (vtkIdType i = 0; i < numPts; i++)
{
// Get the location of the point
vtkIdType id = ptIds->GetId(i);
if (id - 1 == id0)
{
offset[0][0] = -10;
offset[2][0] = -10;
offset[4][0] = -10;
offset[6][0] = -10;
}
else if (id + 1 == id0)
{
offset[1][0] = -10;
offset[3][0] = -10;
offset[5][0] = -10;
offset[7][0] = -10;
}
else if (id - dim[0] == id0)
{
offset[0][1] = -10;
offset[1][1] = -10;
offset[4][1] = -10;
offset[5][1] = -10;
}
else if (id + dim[0] == id0)
{
offset[2][1] = -10;
offset[3][1] = -10;
offset[6][1] = -10;
offset[7][1] = -10;
}
else if (id - dim[0]*dim[1] == id0)
{
offset[0][2] = -10;
offset[1][2] = -10;
offset[2][2] = -10;
offset[3][2] = -10;
}
else if (id + dim[0]*dim[1] == id0)
{
offset[4][2] = -10;
offset[5][2] = -10;
offset[6][2] = -10;
offset[7][2] = -10;
}
}
// Load the non-trimmed cells
int cellDim[3];
for (int i = 0; i < 3; i++)
{
cellDim[i] = std::max(dim[i] - 1, 1);
}
for (int j = 0; j < 8; j++)
{
int i = 0, cellLoc[3];
for (; i < 3; i++)
{
if ( offset[j][i] != -10 )
{
cellLoc[i] = seedLoc[i] + offset[j][i];
if ( cellLoc[i] < 0 || cellLoc[i] >= cellDim[i] )
{
break;
}
}
else
{
break;
}
}
if ( i >= 3 ) //add cell
{
vtkIdType id = cellLoc[0] + cellLoc[1] * cellDim[0] +
cellLoc[2] * cellDim[0] * cellDim[1];
if (id != cellId )
{
cellIds->InsertNextId(id);
}
}
}
}
//------------------------------------------------------------------------------
void vtkStructuredData::GetCellNeighbors(vtkIdType cellId, vtkIdList *ptIds,
vtkIdList *cellIds, int dim[3])
{
int j, seedLoc[3], ptLoc[3], cellLoc[3];
vtkIdType cellDim[3];
int offset[8][3];
vtkIdType numPts = ptIds->GetNumberOfIds(), id, i;
cellIds->Reset();
// Start by finding the "space" of the points in i-j-k space.
// The points define a point, line, or plane in topological space,
// which results in degrees of freedom in three, two or one direction.
// The numbers of DOF determines which neighbors to select.
// Start by finding a seed point
id = ptIds->GetId(0);
seedLoc[0] = id % dim[0];
seedLoc[1] = (id / dim[0]) % dim[1];
seedLoc[2] = id / (dim[0]*dim[1]);
// This defines the space around the seed
offset[0][0] = -1; offset[0][1] = -1; offset[0][2] = -1;
offset[1][0] = 0; offset[1][1] = -1; offset[1][2] = -1;
offset[2][0] = -1; offset[2][1] = 0; offset[2][2] = -1;
offset[3][0] = 0; offset[3][1] = 0; offset[3][2] = -1;
offset[4][0] = -1; offset[4][1] = -1; offset[4][2] = 0;
offset[5][0] = 0; offset[5][1] = -1; offset[5][2] = 0;
offset[6][0] = -1; offset[6][1] = 0; offset[6][2] = 0;
offset[7][0] = 0; offset[7][1] = 0; offset[7][2] = 0;
// For the rest of the points, trim the seed region
// This is essentially an intersection of edge neighbors.
for (i=1; i<numPts; i++)
{
// Get the location of the point
id = ptIds->GetId(i);
ptLoc[0] = id % dim[0];
ptLoc[1] = (id / dim[0]) % dim[1];
ptLoc[2] = id / (static_cast<vtkIdType>(dim[0])*dim[1]);
if ( (ptLoc[0]-1) == seedLoc[0] )
{
offset[0][0] = -10;
offset[2][0] = -10;
offset[4][0] = -10;
offset[6][0] = -10;
}
else if ( (ptLoc[0]+1) == seedLoc[0] )
{
offset[1][0] = -10;
offset[3][0] = -10;
offset[5][0] = -10;
offset[7][0] = -10;
}
else if ( (ptLoc[1]-1) == seedLoc[1] )
{
offset[0][1] = -10;
offset[1][1] = -10;
offset[4][1] = -10;
offset[5][1] = -10;
}
else if ( (ptLoc[1]+1) == seedLoc[1] )
{
offset[2][1] = -10;
offset[3][1] = -10;
offset[6][1] = -10;
offset[7][1] = -10;
}
else if ( (ptLoc[2]-1) == seedLoc[2] )
{
offset[0][2] = -10;
offset[1][2] = -10;
offset[2][2] = -10;
offset[3][2] = -10;
}
else if ( (ptLoc[2]+1) == seedLoc[2] )
{
offset[4][2] = -10;
offset[5][2] = -10;
offset[6][2] = -10;
offset[7][2] = -10;
}
}
// Load the non-trimmed cells
cellDim[0] = dim[0] - 1;
cellDim[1] = dim[1] - 1;
cellDim[2] = dim[2] - 1;
for(i=0; i<3; i++)
{
if ( cellDim[i] < 1 )
{
cellDim[i] = 1;
}
}
for (j=0; j<8; j++)
{
for (i=0; i<3; i++)
{
if ( offset[j][i] != -10 )
{
cellLoc[i] = seedLoc[i] + offset[j][i];
if ( cellLoc[i] < 0 || cellLoc[i] >= cellDim[i] )
{
break;
}
}
else
{
break;
}
}
if ( i >= 3 ) //add cell
{
id = cellLoc[0] + cellLoc[1]*cellDim[0] +
cellLoc[2]*cellDim[0]*cellDim[1];
if (id != cellId )
{
cellIds->InsertNextId(id);
}
}
}
}