File: RLEImageConstIterator.h

package info (click to toggle)
itksnap 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,132 kB
  • sloc: cpp: 91,089; ansic: 1,994; sh: 327; makefile: 16
file content (407 lines) | stat: -rw-r--r-- 13,326 bytes parent folder | download | duplicates (2)
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
#ifndef RLEImageConstIterator_h
#define RLEImageConstIterator_h

#include "itkImage.h"
#include "itkIndex.h"
#include "itkNumericTraits.h"
#include "RLEImage.h"
#include "itkImageConstIterator.h"
#include "itkImageConstIteratorWithIndex.h"
#include "itkImageConstIteratorWithOnlyIndex.h"
#include "itkImageRegionIterator.h"

class MultiLabelMeshPipeline;

namespace itk
{
/** \class ImageConstIterator
 * \brief A multi-dimensional image iterator templated over image type.
 */

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
class ImageConstIterator<RLEImage<TPixel, VImageDimension, CounterType> >
{
    friend class ::MultiLabelMeshPipeline;
public:
  /** Standard class typedefs. */
  typedef ImageConstIterator Self;

  /** Dimension of the image the iterator walks.  This constant is needed so
   * functions that are templated over image iterator type (as opposed to
   * being templated over pixel type and dimension) can have compile time
   * access to the dimension of the image that the iterator walks. */
  itkStaticConstMacro(ImageIteratorDimension, unsigned int, VImageDimension);

  /** Run-time type information (and related methods). */
  itkTypeMacroNoParent(ImageConstIterator);

  /** Image typedef support. */
  typedef RLEImage<TPixel, VImageDimension, CounterType> ImageType;

  /** Run-Length Line (we iterate along it). */
  typedef typename ImageType::RLLine RLLine;

  /** Buffer Type used. */
  typedef typename ImageType::BufferType BufferType;

  /** Type for the internal buffer iterator. */
  typedef ImageRegionIterator<BufferType> BufferIterator;

  /** Index typedef support. */
  typedef typename ImageType::IndexType      IndexType;

  /** Index typedef support. */
  typedef typename ImageType::IndexValueType      IndexValueType;

  /** Size typedef support. */
  typedef typename ImageType::SizeType      SizeType;

  /** Offset typedef support. */
  typedef typename ImageType::OffsetType      OffsetType;

  /** Region typedef support. */
  typedef typename ImageType::RegionType RegionType;

  /** Internal Pixel Type */
  typedef typename ImageType::InternalPixelType InternalPixelType;

  /** External Pixel Type */
  typedef typename ImageType::PixelType PixelType;

  /** Default Constructor. Need to provide a default constructor since we
   * provide a copy constructor. */
  ImageConstIterator():
    myBuffer(0), rlLine(0)
  {
    m_Image = ITK_NULLPTR;
    m_Index0 = 0;
    m_BeginIndex0 = 0;
    m_EndIndex0 = 0;
    realIndex = 0;
    segmentRemainder = 0;
  }

  /** Default Destructor. */
  virtual ~ImageConstIterator() {}

  /** Copy Constructor. The copy constructor is provided to make sure the
   * handle to the image is properly reference counted. */
  ImageConstIterator(const Self & it)
      :myBuffer(const_cast<ImageType *>(it.GetImage())->GetBuffer())
  {
    rlLine = it.rlLine;
    m_Image = it.m_Image;     // copy the smart pointer
    m_Index0 = it.m_Index0;
    this->bi = it.bi;

    realIndex = it.realIndex;
    segmentRemainder = it.segmentRemainder;
    m_BeginIndex0 = it.m_BeginIndex0;
    m_EndIndex0 = it.m_EndIndex0;
  }

  /** Constructor establishes an iterator to walk a particular image and a
   * particular region of that image. */
  ImageConstIterator(const ImageType *ptr, const RegionType & region)
      :myBuffer(const_cast<ImageType *>(ptr)->GetBuffer())
  {
    m_Image = ptr;
    SetRegion(region);
  }

  /** operator= is provided to make sure the handle to the image is properly
   * reference counted. */
  Self & operator=(const Self & it)
  {
    if(this != &it)
      {
          myBuffer = it.myBuffer;
          rlLine = it.rlLine;
          m_Image = it.m_Image;     // copy the smart pointer
          m_Index0 = it.m_Index0;
          bi = it.bi;

          realIndex = it.realIndex;
          segmentRemainder = it.segmentRemainder;
          m_BeginIndex0 = it.m_BeginIndex0;
          m_EndIndex0 = it.m_EndIndex0;
      }
    return *this;
  }

  /** Set the region of the image to iterate over. */
  virtual void SetRegion(const RegionType & region)
  {
    //m_Region = region;

    if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
      {
      const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
      itkAssertOrThrowMacro( ( bufferedRegion.IsInside(region) ),
          "Region " << region << " is outside of buffered region " << bufferedRegion);
      }

    bi = BufferIterator(myBuffer, ImageType::truncateRegion(region));
    m_Index0 = region.GetIndex(0);
    m_BeginIndex0 = m_Index0 - m_Image->GetBufferedRegion().GetIndex(0);
    m_EndIndex0 = m_BeginIndex0 + region.GetSize(0);
    SetIndexInternal(m_BeginIndex0); //sets realIndex and segmentRemainder
  }

  /** Get the dimension (size) of the index. */
  static unsigned int GetImageIteratorDimension()
  { return VImageDimension; }

  /** Comparison operator. Two iterators are the same if they "point to" the
   * same memory location */
  bool operator!=(const Self & it) const
  {
      return bi != it.bi ||
          m_Index0 + m_BeginIndex0 != it.m_Index0 + it.m_BeginIndex0;
  }

  /** Comparison operator. Two iterators are the same if they "point to" the
   * same memory location */
  bool operator==(const Self & it) const
  {
      return bi == it.bi &&
          m_Index0 + m_BeginIndex0 == it.m_Index0 + it.m_BeginIndex0;
  }

  /** Comparison operator. An iterator is "less than" another if it "points to"
  * a lower memory location. */
  bool operator<=(const Self & it) const
  {
      if (bi < it.bi)
          return true;
      else if (bi > it.bi)
          return false;
      return m_Index0 + m_BeginIndex0 <= it.m_Index0 + it.m_BeginIndex0;
  }

  /** Comparison operator. An iterator is "less than" another if it "points to"
  * a lower memory location. */
  bool operator<(const Self & it) const
  {
      if (bi < it.bi)
          return true;
      else if (bi > it.bi)
          return false;
      return m_Index0 + m_BeginIndex0 < it.m_Index0 + it.m_BeginIndex0;
  }

  /** Comparison operator. An iterator is "greater than" another if it
  * "points to" a higher location. */
  bool operator>=(const Self & it) const
  {
      if (bi > it.bi)
          return true;
      else if (bi < it.bi)
          return false;
      return m_Index0 + m_BeginIndex0 >= it.m_Index0 + it.m_BeginIndex0;
  }

  /** Comparison operator. An iterator is "greater than" another if it
  * "points to" a higher location. */
  bool operator>(const Self & it) const
  {
      if (bi > it.bi)
          return true;
      else if (bi < it.bi)
          return false;
      return m_Index0 + m_BeginIndex0 > it.m_Index0 + it.m_BeginIndex0;
  }

  /** Get the index. This provides a read only copy of the index. */
  const IndexType GetIndex() const
  {
      IndexType indR(m_Image->GetBufferedRegion().GetIndex());
      indR[0] += m_Index0;
      typename BufferType::IndexType bufInd = bi.GetIndex();
      for (IndexValueType i = 1; i < VImageDimension; i++)
          indR[i] = bufInd[i - 1];
      return indR;
  }

  /** Sets the image index. No bounds checking is performed. */
  virtual void SetIndex(const IndexType & ind)
  {
      typename BufferType::IndexType bufInd;
      for (IndexValueType i = 1; i < VImageDimension; i++)
          bufInd[i - 1] = ind[i];
      bi.SetIndex(bufInd);
      SetIndexInternal(ind[0] - m_Image->GetBufferedRegion().GetIndex(0));
  }

  /** Get the region that this iterator walks. ImageConstIterators know the
   * beginning and the end of the region of the image to iterate over. */
  const RegionType GetRegion() const
  {
      RegionType r;
      r.SetIndex(0, m_BeginIndex0 + m_Image->GetBufferedRegion().GetIndex(0));
      r.SetSize(0, m_EndIndex0 - m_BeginIndex0);
      typename BufferType::RegionType ir = bi.GetRegion();
      for (IndexValueType i = 1; i < VImageDimension; i++)
      {
          r.SetIndex(i, ir.GetIndex(i - 1));
          r.SetSize(i, ir.GetSize(i - 1));
      }
      return r;
  }

  /** Get the image that this iterator walks. */
  const ImageType * GetImage() const
  { return m_Image.GetPointer(); }

  /** Get the pixel value */
  PixelType Get(void) const
  { return Value(); }

  /** Return a const reference to the pixel
   * This method will provide the fastest access to pixel
   * data, but it will NOT support ImageAdaptors. */
  const PixelType & Value(void) const
  {
      RLLine & line = const_cast<Self *>(this)->bi.Value();
      return line[realIndex].second;
  }

  /** Move an iterator to the beginning of the region. "Begin" is
   * defined as the first pixel in the region. */
  void GoToBegin()
  {
      bi.GoToBegin();
      SetIndexInternal(m_BeginIndex0);
  }

  /** Move an iterator to the end of the region. "End" is defined as
   * one pixel past the last pixel of the region. */
  void GoToEnd()
  {
      bi.GoToEnd();
      m_Index0 = m_BeginIndex0;
  }

  /** Is the iterator at the beginning of the region? "Begin" is defined
   * as the first pixel in the region. */
  bool IsAtBegin(void) const
  {
      return m_Index0 == m_BeginIndex0 && bi.IsAtBegin();
  }

  /** Is the iterator at the end of the region? "End" is defined as one
   * pixel past the last pixel of the region. */
  bool IsAtEnd(void) const
  {
      return m_Index0 == m_BeginIndex0 && bi.IsAtEnd();
  }

protected: //made protected so other iterators can access

  /** Set the internal index, realIndex and segmentRemainder. */
  virtual void SetIndexInternal(const IndexValueType ind0)
  {
      m_Index0 = ind0;
      rlLine = &bi.Value();

      CounterType t = 0;
      SizeValueType x = 0;

      for (; x < (*rlLine).size(); x++)
      {
          t += (*rlLine)[x].first;
          if (t > m_Index0)
              break;
      }
      realIndex = x;
      segmentRemainder = t - m_Index0;
  }

  typename ImageType::ConstWeakPointer m_Image;

  IndexValueType m_Index0; //index into the RLLine

  const RLLine * rlLine;

  mutable IndexValueType realIndex; // index into line's segment
  mutable IndexValueType segmentRemainder; // how many pixels remain in current segment

  IndexValueType m_BeginIndex0; // index to first pixel in region in relation to buffer start
  IndexValueType m_EndIndex0;   // index to one pixel past last pixel in region in relation to buffer start

  BufferIterator bi; //iterator over internal buffer image
  typename BufferType::Pointer myBuffer;
};

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
class ImageConstIteratorWithIndex<RLEImage<TPixel, VImageDimension, CounterType> >
    :public ImageConstIterator < RLEImage<TPixel, VImageDimension, CounterType> >
{
    //just inherit constructors
public:

    /** Image typedef support. */
    typedef RLEImage<TPixel, VImageDimension, CounterType> ImageType;

    typedef typename itk::ImageConstIterator<RLEImage<TPixel, VImageDimension, CounterType> >::RegionType RegionType;

    void GoToReverseBegin()
    {
        this->bi.GoToReverseBegin();
        this->m_Index0 = this->m_EndIndex0 - 1;
        SetIndexInternal(this->m_Index0);
    }

    bool IsAtReverseEnd()
    { return this->bi.IsAtReverseEnd(); }

    /** Default Constructor. Need to provide a default constructor since we
    * provide a copy constructor. */
    ImageConstIteratorWithIndex() :ImageConstIterator< ImageType >(){ }

    /** Copy Constructor. The copy constructor is provided to make sure the
    * handle to the image is properly reference counted. */
    ImageConstIteratorWithIndex(const ImageConstIteratorWithIndex & it)
    {
        this->ImageConstIterator< ImageType >::operator=(it);
    }

    /** Constructor establishes an iterator to walk a particular image and a
    * particular region of that image. */
    ImageConstIteratorWithIndex(const ImageType *ptr, const RegionType & region)
        :ImageConstIterator< ImageType >(ptr, region) { }
}; //no additional implementation required

template< typename TPixel, unsigned int VImageDimension, typename CounterType >
class ImageConstIteratorWithOnlyIndex<RLEImage<TPixel, VImageDimension, CounterType> >
    :public ImageConstIteratorWithIndex < RLEImage<TPixel, VImageDimension, CounterType> >
{
    //just inherit constructors
public:

    /** Image typedef support. */
    typedef RLEImage<TPixel, VImageDimension, CounterType> ImageType;

    typedef typename itk::ImageConstIterator<RLEImage<TPixel, VImageDimension, CounterType> >::RegionType RegionType;

    /** Default Constructor. Need to provide a default constructor since we
    * provide a copy constructor. */
    ImageConstIteratorWithOnlyIndex() :ImageConstIterator< ImageType >(){ }


    /** Copy Constructor. The copy constructor is provided to make sure the
    * handle to the image is properly reference counted. */
    ImageConstIteratorWithOnlyIndex(const ImageConstIteratorWithOnlyIndex & it)
    {
        this->ImageConstIterator< ImageType >::operator=(it);
    }

    /** Constructor establishes an iterator to walk a particular image and a
    * particular region of that image. */
    ImageConstIteratorWithOnlyIndex(const ImageType *ptr, const RegionType & region)
        :ImageConstIterator< ImageType >(ptr, region) { }
}; //no additional implementation required

} // end namespace itk

#endif //RLEImageConstIterator_h