ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkRLEImageConstIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkRLEImageConstIterator_h
19 #define itkRLEImageConstIterator_h
20 
21 #include "itkImage.h"
22 #include "itkImageConstIterator.h"
25 #include "itkImageRegionIterator.h"
26 #include "itkIndex.h"
27 #include "itkNumericTraits.h"
28 #include "itkRLEImage.h"
29 
30 class MultiLabelMeshPipeline;
31 
32 namespace itk
33 {
41 template< typename TPixel, unsigned int VImageDimension, typename CounterType >
42 class ImageConstIterator< RLEImage< TPixel, VImageDimension, CounterType > >
43 {
44  friend class ::MultiLabelMeshPipeline;
45 
46 public:
49 
54  static constexpr unsigned int ImageIteratorDimension = VImageDimension;
55 
58 
61 
63  using RLLine = typename ImageType::RLLine;
64 
67 
70 
72  using IndexType = typename ImageType::IndexType;
73 
76 
78  using SizeType = typename ImageType::SizeType;
79 
82 
85 
88 
90  using PixelType = typename ImageType::PixelType;
91 
95  : m_RunLengthLine(nullptr),
96  m_Buffer( nullptr )
97  {
98  m_Image = nullptr;
99  m_Index0 = 0;
100  m_BeginIndex0 = 0;
101  m_EndIndex0 = 0;
102  m_RealIndex = 0;
103  m_SegmentRemainder = 0;
104  }
106 
108  virtual ~ImageConstIterator() {}
109 
113  : m_Buffer( const_cast< ImageType * >( it.GetImage() )->GetBuffer() )
114  {
115  m_RunLengthLine = it.m_RunLengthLine;
116  m_Image = it.m_Image; // copy the smart pointer
117  m_Index0 = it.m_Index0;
118  this->m_BI = it.m_BI;
120 
121  m_RealIndex = it.m_RealIndex;
122  m_SegmentRemainder = it.m_SegmentRemainder;
123  m_BeginIndex0 = it.m_BeginIndex0;
124  m_EndIndex0 = it.m_EndIndex0;
125  }
126 
129  ImageConstIterator( const ImageType* ptr, const RegionType& region )
130  : m_Buffer( const_cast< ImageType * >( ptr )->GetBuffer() )
131  {
132  m_Image = ptr;
133  SetRegion( region );
134  }
136 
139  Self &
140  operator=( const Self& it )
141  {
142  if ( this != &it )
143  {
144  m_Buffer = it.m_Buffer;
145  m_RunLengthLine = it.m_RunLengthLine;
146  m_Image = it.m_Image; // copy the smart pointer
147  m_Index0 = it.m_Index0;
148  m_BI = it.m_BI;
150 
151  m_RealIndex = it.m_RealIndex;
152  m_SegmentRemainder = it.m_SegmentRemainder;
153  m_BeginIndex0 = it.m_BeginIndex0;
154  m_EndIndex0 = it.m_EndIndex0;
155  }
156  return *this;
157  }
158 
160  virtual void
161  SetRegion( const RegionType& region )
162  {
163  // m_Region = region;
164 
165  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
166  {
167  const RegionType& bufferedRegion = m_Image->GetBufferedRegion();
168  itkAssertOrThrowMacro( ( bufferedRegion.IsInside( region ) ),
169  "Region " << region << " is outside of buffered region " << bufferedRegion );
170  }
171 
172  m_BI = BufferIterator( m_Buffer, region.Slice( 0 ) );
173  m_Index0 = region.GetIndex( 0 );
174  m_BeginIndex0 = m_Index0 - m_Image->GetBufferedRegion().GetIndex( 0 );
175  m_EndIndex0 = m_BeginIndex0 + region.GetSize( 0 );
176  SetIndexInternal( m_BeginIndex0 ); // sets m_RealIndex and m_SegmentRemainder
177  }
178 
180  static unsigned int
182  {
183  return VImageDimension;
184  }
185 
188  bool
189  operator!=( const Self& it ) const
190  {
191  return m_BI != it.m_BI || m_Index0 + m_BeginIndex0 != it.m_Index0 + it.m_BeginIndex0;
192  }
193 
196  bool
197  operator==( const Self& it ) const
198  {
199  return m_BI == it.m_BI && m_Index0 + m_BeginIndex0 == it.m_Index0 + it.m_BeginIndex0;
200  }
201 
204  bool
205  operator<=( const Self& it ) const
206  {
207  if ( m_BI < it.m_BI )
208  {
209  return true;
210  }
211  else if ( m_BI > it.m_BI )
212  {
213  return false;
214  }
215  return m_Index0 + m_BeginIndex0 <= it.m_Index0 + it.m_BeginIndex0;
216  }
218 
221  bool
222  operator<( const Self& it ) const
223  {
224  if ( m_BI < it.m_BI )
225  {
226  return true;
227  }
228  else if ( m_BI > it.m_BI )
229  {
230  return false;
231  }
232  return m_Index0 + m_BeginIndex0 < it.m_Index0 + it.m_BeginIndex0;
233  }
235 
238  bool
239  operator>=( const Self& it ) const
240  {
241  if ( m_BI > it.m_BI )
242  {
243  return true;
244  }
245  else if ( m_BI < it.m_BI )
246  {
247  return false;
248  }
249  return m_Index0 + m_BeginIndex0 >= it.m_Index0 + it.m_BeginIndex0;
250  }
252 
255  bool
256  operator>( const Self& it ) const
257  {
258  if ( m_BI > it.m_BI )
259  {
260  return true;
261  }
262  else if ( m_BI < it.m_BI )
263  {
264  return false;
265  }
266  return m_Index0 + m_BeginIndex0 > it.m_Index0 + it.m_BeginIndex0;
267  }
269 
271  const IndexType
272  GetIndex() const
273  {
274  IndexType indR( m_Image->GetBufferedRegion().GetIndex() );
275 
276  indR[0] += m_Index0;
277  typename BufferType::IndexType bufInd = m_BI.GetIndex();
278  for ( IndexValueType i = 1; i < VImageDimension; i++ )
279  {
280  indR[i] = bufInd[i - 1];
281  }
282  return indR;
283  }
284 
286  virtual void
287  SetIndex( const IndexType& ind )
288  {
289  typename BufferType::IndexType bufInd;
290  for ( IndexValueType i = 1; i < VImageDimension; i++ )
291  {
292  bufInd[i - 1] = ind[i];
293  }
294  m_BI.SetIndex( bufInd );
295  SetIndexInternal( ind[0] - m_Image->GetBufferedRegion().GetIndex( 0 ) );
296  }
298 
301  const RegionType
302  GetRegion() const
303  {
304  RegionType r;
305 
306  r.SetIndex( 0, m_BeginIndex0 + m_Image->GetBufferedRegion().GetIndex( 0 ) );
307  r.SetSize( 0, m_EndIndex0 - m_BeginIndex0 );
308  typename BufferType::RegionType ir = m_BI.GetRegion();
309  for ( IndexValueType i = 1; i < VImageDimension; i++ )
310  {
311  r.SetIndex( i, ir.GetIndex( i - 1 ) );
312  r.SetSize( i, ir.GetSize( i - 1 ) );
313  }
314  return r;
315  }
316 
318  const ImageType *
319  GetImage() const
320  {
321  return m_Image.GetPointer();
322  }
323 
325  PixelType
326  Get( void ) const
327  {
328  return Value();
329  }
330 
334  const PixelType &
335  Value( void ) const
336  {
337  RLLine& line = const_cast< Self * >( this )->m_BI.Value();
338 
339  return line[m_RealIndex].second;
340  }
341 
344  void
346  {
347  m_BI.GoToBegin();
348  SetIndexInternal( m_BeginIndex0 );
349  }
351 
354  void
356  {
357  m_BI.GoToEnd();
358  m_Index0 = m_BeginIndex0;
359  }
361 
364  bool
365  IsAtBegin( void ) const
366  {
367  return m_Index0 == m_BeginIndex0 && m_BI.IsAtBegin();
368  }
369 
372  bool
373  IsAtEnd( void ) const
374  {
375  return m_Index0 == m_BeginIndex0 && m_BI.IsAtEnd();
376  }
377 
378 protected: // made protected so other iterators can access
380  virtual void
382  {
383  m_Index0 = ind0;
384  m_RunLengthLine = &m_BI.Value();
386 
387  CounterType t = 0;
388  SizeValueType x = 0;
389  for (; x < ( *m_RunLengthLine ).size(); x++ )
390  {
391  t += ( *m_RunLengthLine )[x].first;
392  if ( t > m_Index0 )
393  {
394  break;
395  }
396  }
397  m_RealIndex = x;
398  m_SegmentRemainder = t - m_Index0;
399  } // SetIndexInternal
400 
402 
403  IndexValueType m_Index0; // index into the RLLine
404 
406 
407  mutable SizeValueType m_RealIndex; // index into line's segment
408  mutable IndexValueType m_SegmentRemainder; // how many pixels remain in current segment
409 
410  IndexValueType m_BeginIndex0; // index to first pixel in region in relation to buffer start
411  IndexValueType m_EndIndex0; // index to one pixel past last pixel in region in relation to buffer start
412  BufferIterator m_BI; // iterator over internal buffer image
413 
414  typename BufferType::Pointer m_Buffer;
415 };
416 
417 template< typename TPixel, unsigned int VImageDimension, typename CounterType >
418 class ImageConstIteratorWithIndex< RLEImage< TPixel, VImageDimension, CounterType > > :
419  public ImageConstIterator< RLEImage< TPixel, VImageDimension, CounterType > >
420 {
421 // just inherit constructors
422 
423 public:
426 
428 
429  void
431  {
432  this->m_BI.GoToReverseBegin();
433  this->m_Index0 = this->m_EndIndex0 - 1;
434  SetIndexInternal( this->m_Index0 );
435  }
436 
437  bool
439  {
440  return this->m_BI.IsAtReverseEnd();
441  }
442 
447  {}
448 
452  {
454  }
455 
458  ImageConstIteratorWithIndex( const ImageType* ptr, const RegionType& region )
459  : ImageConstIterator< ImageType >( ptr, region )
460  {}
461 }; // no additional implementation required
463 
464 template< typename TPixel, unsigned int VImageDimension, typename CounterType >
465 class ImageConstIteratorWithOnlyIndex< RLEImage< TPixel, VImageDimension, CounterType > > :
466  public ImageConstIteratorWithIndex< RLEImage< TPixel, VImageDimension, CounterType > >
467 {
468 // just inherit constructors
469 
470 public:
473 
475 
480  {}
481 
485  {
487  }
488 
492  : ImageConstIterator< ImageType >( ptr, region )
493  {}
494 }; // no additional implementation required
495 } // end namespace itk
497 
498 #endif // itkRLEImageConstIterator_h
TPixel PixelType
Definition: itkRLEImage.h:73
static constexpr unsigned int ImageIteratorDimension
unsigned long SizeValueType
Definition: itkIntTypes.h:83
RLLine InternalPixelType
Definition: itkRLEImage.h:91
typename itk::Image< RLLine, VImageDimension-1 > BufferType
Definition: itkRLEImage.h:224
const IndexType GetIndex() const
typename TImage::PixelType PixelType
itkTypeMacroNoParent(ImageConstIterator)
Implements a weak reference to an object.
A multi-dimensional image iterator templated over image type.
Self & operator=(const Self &it)
A base class for multi-dimensional iterators templated over image type that are designed to efficient...
std::vector< RLSegment > RLLine
Definition: itkRLEImage.h:85
const InternalPixelType * m_Buffer
A base class for multi-dimensional iterators templated over image type that are designed to provide o...
const PixelType & Value() const
signed long IndexValueType
Definition: itkIntTypes.h:90
typename TImage::IndexType IndexType
TImage::ConstWeakPointer m_Image
void SetIndex(const IndexValueType val[VDimension])
Definition: itkIndex.h:226
bool operator<(const Self &it) const
typename Superclass::OffsetType OffsetType
Definition: itkRLEImage.h:106
const IndexValueType * GetIndex() const
Definition: itkIndex.h:217
Run-Length Encoded image. It saves memory for label images at the expense of processing times...
Definition: itkRLEImage.h:52
typename TImage::RegionType RegionType
void SetIndex(const IndexType &index)
virtual void SetRegion(const RegionType &region)
bool operator<=(const Self &it) const
const ImageType * GetImage() const