vtkImageStencilIterator.txx 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkImageStencilIterator.txx

  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.

=========================================================================*/
15
16
#ifndef vtkImageStencilIterator_txx
#define vtkImageStencilIterator_txx
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

#include "vtkImageStencilIterator.h"
#include "vtkImageData.h"
#include "vtkImageStencilData.h"
#include "vtkAlgorithm.h"

//----------------------------------------------------------------------------
class vtkImageStencilIteratorFriendship
{
  public:

    static int *GetExtentListLengths(vtkImageStencilData *stencil)
      {
      return stencil->ExtentListLengths;
      }

    static int **GetExtentLists(vtkImageStencilData *stencil)
      {
      return stencil->ExtentLists;
      }
};

//----------------------------------------------------------------------------
template <class DType>
vtkImageStencilIterator<DType>::vtkImageStencilIterator()
{
  this->Pointer = 0;
  this->SpanEndPointer = 0;
  this->RowEndPointer = 0;
  this->SliceEndPointer = 0;
  this->EndPointer = 0;

  this->PixelIncrement = 0;
  this->RowEndIncrement = 0;
  this->RowIncrement = 0;
  this->SliceEndIncrement = 0;
  this->SliceIncrement = 0;

55
  this->HasStencil = false;
56
57
58
59
60
  this->InStencil = false;
  this->SpanSliceEndIncrement = 0;
  this->SpanSliceIncrement = 0;
  this->SpanMinX = 0;
  this->SpanMaxX = 0;
61
62
63
64
65
66
67
  this->SpanMinY = 0;
  this->SpanMaxY = 0;
  this->SpanMinZ = 0;
  this->SpanMaxZ = 0;
  this->SpanIndexX = 0;
  this->SpanIndexY = 0;
  this->SpanIndexZ = 0;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  this->SpanCountPointer = 0;
  this->SpanListPointer = 0;

  this->Algorithm = 0;
  this->Count = 0;
  this->Target = 0;
}

//----------------------------------------------------------------------------
template <class DType>
void vtkImageStencilIterator<DType>::Initialize(
  vtkImageData *image, vtkImageStencilData *stencil, int extent[6])
{
  image->GetIncrements(
    this->PixelIncrement, this->RowIncrement, this->SliceIncrement);

  vtkIdType tmp;
  image->GetContinuousIncrements(
    extent, tmp, this->RowEndIncrement, this->SliceEndIncrement);
87
  this->SliceEndIncrement = this->SliceEndIncrement + this->RowEndIncrement;
88
89
90
91
92
93
94
95
96
97
98
99
100

  this->Pointer = static_cast<DType *>(
    image->GetScalarPointerForExtent(extent));

  this->SpanEndPointer = this->Pointer;
  this->RowEndPointer = this->Pointer;
  this->SliceEndPointer = this->Pointer;
  this->EndPointer = this->Pointer;

  if (extent[1] >= extent[0] &&
      extent[3] >= extent[2] &&
      extent[5] >= extent[4])
    {
101
102
103
104
105
106
107
    this->SpanEndPointer = this->Pointer +
      this->PixelIncrement*(extent[1] - extent[0] + 1);
    this->RowEndPointer = this->SpanEndPointer;
    this->SliceEndPointer = this->RowEndPointer +
      this->RowIncrement*(extent[3] - extent[2]);
    this->EndPointer = this->SliceEndPointer +
      this->SliceIncrement*(extent[5] - extent[4]);
108
109
110
111
112
113
    }

  // Code for when a stencil is provided
  if (stencil)
    {
    this->HasStencil = true;
114
    this->InStencil = false;
115

116
117
118
119
    this->SpanIndexX = 0;
    this->SpanIndexY = 0;
    this->SpanIndexZ = 0;

120
121
122
123
124
125
126
127
    int stencilExtent[6];
    stencil->GetExtent(stencilExtent);

    // The stencil has a YZ array of span lists, we need increments
    // to get to the next Z position in the YZ array.
    this->SpanSliceIncrement = 0;
    this->SpanSliceEndIncrement = 0;

128
    if (stencilExtent[3] >= stencilExtent[2] &&
129
130
131
        stencilExtent[5] >= stencilExtent[4])
      {
      this->SpanSliceIncrement = stencilExtent[3] - stencilExtent[2] + 1;
132
133
      int botOffset = extent[2] - stencilExtent[2];
      if (botOffset >= 0)
134
        {
135
136
137
138
139
140
        this->SpanSliceEndIncrement += botOffset;
        }
      int topOffset = stencilExtent[3] - extent[3];
      if (topOffset >= 0)
        {
        this->SpanSliceEndIncrement += topOffset;
141
142
143
        }
      }

144
145
146
147
    // The X extent for the spans we want to iterate through
    this->SpanMinX = extent[0];
    this->SpanMaxX = extent[1];

148
    // Find the offset to the start position within the YZ array.
149
150
151
152
153
154
    vtkIdType startOffset = 0;

    int yOffset = extent[2] - stencilExtent[2];
    if (yOffset >= 0)
      {
      this->SpanMinY = 0;
155
      // starting partway into the stencil, so add an offset
156
157
158
159
160
      startOffset += yOffset;
      }
    else
      {
      this->SpanMinY = -yOffset;
161
162
163
      // starting before start of stencil: subtract the increment that
      // will be added in NextSpan() upon entry into stencil extent
      startOffset -= 1;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
      }

    if (stencilExtent[3] > extent[3])
      {
      this->SpanMaxY = extent[3] - extent[2];
      }
    else
      {
      this->SpanMaxY = stencilExtent[3] - extent[2];
      }

    int zOffset = extent[4] - stencilExtent[4];
    if (zOffset >= 0)
      {
      this->SpanMinZ = 0;
179
      // starting partway into the stencil, so add an offset
180
      startOffset += zOffset*this->SpanSliceIncrement;
181
182
183
184
      }
    else
      {
      this->SpanMinZ = -zOffset;
185
186
187
188
189
190
      // starting before start of stencil: subtract the increment that
      // will be added in NextSpan() upon entry into stencil extent
      if (yOffset >= 0)
        {
        startOffset -= 1 + this->SpanSliceEndIncrement;
        }
191
192
193
194
195
196
197
198
199
200
      }

    if (stencilExtent[5] > extent[5])
      {
      this->SpanMaxZ = extent[5] - extent[4];
      }
    else
      {
      this->SpanMaxZ = stencilExtent[5] - extent[4];
      }
201

202
203
204
205
206
207
    if (this->SpanMinY <= this->SpanMaxY &&
        this->SpanMinZ <= this->SpanMaxZ)
      {
      this->SpanCountPointer =
        vtkImageStencilIteratorFriendship::GetExtentListLengths(stencil) +
        startOffset;
208

209
210
211
      this->SpanListPointer =
        vtkImageStencilIteratorFriendship::GetExtentLists(stencil) +
        startOffset;
212

213
214
215
216
217
218
219
      // Get the current position within the span list for the current row
      if (yOffset >= 0 && zOffset >= 0)
        {
        // If starting within stencil extent, check stencil immediately
        this->InStencil = true;
        this->SetSpanState(this->SpanMinX);
        }
220
221
222
223
224
225
      }
    else
      {
      this->SpanCountPointer = 0;
      this->SpanListPointer = 0;
      }
226
227
228
229
    }
  else
    {
    this->HasStencil = false;
230
    this->InStencil = true;
231
232
233
234
    this->SpanSliceEndIncrement = 0;
    this->SpanSliceIncrement = 0;
    this->SpanMinX = 0;
    this->SpanMaxX = 0;
235
236
237
238
239
240
241
    this->SpanMinY = 0;
    this->SpanMaxY = 0;
    this->SpanMinZ = 0;
    this->SpanMaxZ = 0;
    this->SpanIndexX = 0;
    this->SpanIndexY = 0;
    this->SpanIndexZ = 0;
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
    this->SpanCountPointer = 0;
    this->SpanListPointer = 0;
    }
}

//----------------------------------------------------------------------------
template <class DType>
vtkImageStencilIterator<DType>::vtkImageStencilIterator(
  vtkImageData *image, vtkImageStencilData *stencil, int extent[6],
  vtkAlgorithm *algorithm, int threadId)
{
  this->Initialize(image, stencil, extent);

  if (algorithm && threadId == 0)
    {
    this->Algorithm = algorithm;
    vtkIdType maxCount = extent[3] - extent[2] + 1;
    maxCount *= extent[5] - extent[4] + 1;
    this->Target = maxCount/50 + 1;
    this->Count = this->Target*50 - (maxCount/this->Target)*this->Target + 1;
    }
  else
    {
    this->Algorithm = 0;
    this->Target = 0;
    this->Count = 0;
    }
}

//----------------------------------------------------------------------------
template <class DType>
void vtkImageStencilIterator<DType>::SetSpanState(int idX)
{
  // Find the span that includes idX
  bool inStencil = false;
  int *spans = *this->SpanListPointer;
  int n = *this->SpanCountPointer;
  int i;
  for (i = 0; i < n; i++)
    {
    if (spans[i] > idX)
      {
      break;
      }
    inStencil = !inStencil;
    }

  // Set the primary span state variables
290
  this->SpanIndexX = i;
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
  this->InStencil = inStencil;

  // Clamp the span end to SpanMaxX+1
  int endIdX = this->SpanMaxX + 1;
  if (i < n && spans[i] <= this->SpanMaxX)
    {
    endIdX = spans[i];
    }

  // Compute the pointers for idX and endIdX
  DType *rowStartPointer =
    this->RowEndPointer - (this->RowIncrement - this->RowEndIncrement);

  this->Pointer =
    rowStartPointer + (idX - this->SpanMinX)*this->PixelIncrement;

  this->SpanEndPointer =
    rowStartPointer + (endIdX - this->SpanMinX)*this->PixelIncrement;
}

//----------------------------------------------------------------------------
template <class DType>
void vtkImageStencilIterator<DType>::NextSpan()
{
  if (this->SpanEndPointer == this->RowEndPointer)
    {
    int spanIncr = 1;

    if (this->SpanEndPointer != this->SliceEndPointer)
      {
      // Move to the next row
      this->Pointer = this->RowEndPointer + this->RowEndIncrement;
      this->RowEndPointer += this->RowIncrement;
      this->SpanEndPointer = this->RowEndPointer;
325
      this->SpanIndexY++;
326
      }
327
    else if (this->SpanEndPointer != this->EndPointer)
328
329
330
331
332
333
334
      {
      // Move to the next slice
      this->Pointer = this->SliceEndPointer + this->SliceEndIncrement;
      this->SliceEndPointer += this->SliceIncrement;
      this->RowEndPointer = this->Pointer +
        (this->RowIncrement - this->RowEndIncrement);
      this->SpanEndPointer = this->RowEndPointer;
335
336
      this->SpanIndexY = 0;
      this->SpanIndexZ++;
337
      spanIncr += this->SpanSliceEndIncrement;
338
      }
339
340
341
342
343
344
    else
      {
      // reached EndPointer
      this->Pointer = this->EndPointer;
      return;
      }
345
346
347

    if (this->HasStencil)
      {
348
349
350
      if ((this->SpanIndexY >= this->SpanMinY) &&
          (this->SpanIndexY <= this->SpanMaxY) &&
          (this->SpanIndexZ >= this->SpanMinZ) &&
351
          (this->SpanIndexZ <= this->SpanMaxZ))
352
353
354
355
356
357
358
359
360
        {
        this->SpanCountPointer += spanIncr;
        this->SpanListPointer += spanIncr;
        this->SetSpanState(this->SpanMinX);
        }
      else
        {
        this->InStencil = false;
        }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
      }

    if (this->Algorithm)
      {
      this->ReportProgress();
      }
    }
  else
    {
    // Move to the next span in the current row
    this->Pointer = this->SpanEndPointer;

    // Clamp the span end to SpanMaxX+1
    int endIdX = this->SpanMaxX + 1;
375
376
    this->SpanIndexX++;
    if (this->SpanIndexX < *this->SpanCountPointer)
377
      {
378
      int tmpIdX = (*this->SpanListPointer)[this->SpanIndexX];
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
      if (tmpIdX < endIdX)
        {
        endIdX = tmpIdX;
        }
      }

    // Compute the pointer for endIdX
    this->SpanEndPointer = this->RowEndPointer -
      (this->RowIncrement - this->RowEndIncrement) +
      (endIdX - this->SpanMinX)*this->PixelIncrement;

    // Flip the state
    this->InStencil = !this->InStencil;
    }
}

//----------------------------------------------------------------------------
template <class DType>
void vtkImageStencilIterator<DType>::ReportProgress()
{
  if (this->Count % this->Target == 0)
    {
    if (this->Algorithm->GetAbortExecute())
      {
      this->Pointer = this->EndPointer;
      this->SpanEndPointer = this->EndPointer;
      this->RowEndPointer = this->EndPointer;
      this->SliceEndPointer = this->EndPointer;
      }
    else
      {
      this->Algorithm->UpdateProgress(0.02*(this->Count/this->Target));
      }
    }
  this->Count++;
}

#endif