ITK  4.2.0
Insight Segmentation and Registration Toolkit
itkImageReverseConstIterator.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 __itkImageReverseConstIterator_h
19 #define __itkImageReverseConstIterator_h
20 
21 #include "itkSize.h"
22 #include "itkImageConstIterator.h"
23 #include <memory>
24 
25 namespace itk
26 {
86 template< typename TImage >
87 class ITK_EXPORT ImageReverseConstIterator
88 {
89 public:
92 
97  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
98  TImage::ImageDimension);
99 
101  typedef typename TImage::IndexType IndexType;
102 
104  typedef typename TImage::SizeType SizeType;
105 
107  typedef typename TImage::OffsetType OffsetType;
108 
110  typedef typename TImage::RegionType RegionType;
111 
113  typedef TImage ImageType;
114 
118  typedef typename TImage::PixelContainer PixelContainer;
119  typedef typename PixelContainer::Pointer PixelContainerPointer;
120 
122  typedef typename TImage::InternalPixelType InternalPixelType;
123 
125  typedef typename TImage::PixelType PixelType;
126 
129  typedef typename TImage::AccessorType AccessorType;
130 
132  typedef typename TImage::AccessorFunctorType AccessorFunctorType;
133 
136  ImageReverseConstIterator():m_PixelAccessor(), m_PixelAccessorFunctor()
137  {
138  m_Buffer = 0;
139  m_Offset = 0;
140  m_BeginOffset = 0;
141  m_EndOffset = 0;
142  m_PixelAccessorFunctor.SetBegin(m_Buffer);
143  }
145 
148 
152  {
153  m_Image = it.m_Image; // copy the smart pointer
154 
155  m_Region = it.m_Region;
156 
157  m_Buffer = it.m_Buffer;
158  m_Offset = it.m_Offset;
159  m_BeginOffset = it.m_BeginOffset;
160  m_EndOffset = it.m_EndOffset;
161  m_PixelAccessor = it.m_PixelAccessor;
162  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
163  m_PixelAccessorFunctor.SetBegin(m_Buffer);
164  }
165 
168  ImageReverseConstIterator(const ImageType *ptr, const RegionType & region)
169  {
170  SizeValueType offset;
171 
172  m_Image = ptr;
173  m_Buffer = m_Image->GetBufferPointer();
174  m_Region = region;
175 
176  // Compute the end offset, one pixel before the first pixel
177  offset = m_Image->ComputeOffset( m_Region.GetIndex() );
178  m_EndOffset = offset - 1;
179 
180  // Compute the begin offset, the last pixel in the region
181  IndexType ind( m_Region.GetIndex() );
182  SizeType size( m_Region.GetSize() );
183  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
184  {
185  ind[i] += ( size[i] - 1 );
186  }
187  m_BeginOffset = m_Image->ComputeOffset(ind);
188  m_Offset = m_BeginOffset;
189 
190  m_PixelAccessor = ptr->GetPixelAccessor();
191  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
192  m_PixelAccessorFunctor.SetBegin(m_Buffer);
193  }
194 
203  {
204  m_Image = it.GetImage();
205  m_Region = it.GetRegion();
206  m_Buffer = m_Image->GetBufferPointer();
208 
209  IndexType ind = it.GetIndex();
210 
211  m_Offset = m_Image->ComputeOffset(ind);
212 
213  // Compute the end offset, one pixel before the first pixel
214  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
215 
216  // Compute the begin offset, the last pixel in the region
217  IndexType regInd( m_Region.GetIndex() );
218  SizeType regSize( m_Region.GetSize() );
219  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
220  {
221  regInd[i] += ( regSize[i] - 1 );
222  }
223  m_BeginOffset = m_Image->ComputeOffset(regInd);
224 
225  m_PixelAccessor = m_Image->GetPixelAccessor();
226  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
227  m_PixelAccessorFunctor.SetBegin(m_Buffer);
228  }
229 
232  Self & operator=(const Self & it)
233  {
234  m_Image = it.m_Image; // copy the smart pointer
235  m_Region = it.m_Region;
236 
237  m_Buffer = it.m_Buffer;
238  m_Offset = it.m_Offset;
239  m_BeginOffset = it.m_BeginOffset;
240  m_EndOffset = it.m_EndOffset;
241  m_PixelAccessor = it.m_PixelAccessor;
242  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
243  m_PixelAccessorFunctor.SetBegin(m_Buffer);
244  return *this;
245  }
246 
249  Self & operator=(const ImageConstIterator< TImage > & it)
250  {
251  m_Image = it.GetImage();
252  m_Region = it.GetRegion();
253  m_Buffer = m_Image->GetBufferPointer();
255 
256  IndexType ind = it.GetIndex();
257 
258  m_Offset = m_Image->ComputeOffset(ind);
259 
260  // Compute the end offset, one pixel before the first pixel
261  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
262 
263  // Compute the begin offset, the last pixel in the region
264  IndexType regInd( m_Region.GetIndex() );
265  SizeType regSize( m_Region.GetSize() );
266  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
267  {
268  regInd[i] += ( regSize[i] - 1 );
269  }
270  m_BeginOffset = m_Image->ComputeOffset(regInd);
271 
272  m_PixelAccessor = m_Image->GetPixelAccessor();
273  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
274  m_PixelAccessorFunctor.SetBegin(m_Buffer);
275 
276  return *this;
277  }
278 
280  static unsigned int GetImageIteratorDimension()
281  { return TImage::ImageDimension; }
282 
285  bool
286  operator!=(const Self & it) const
287  {
288  // two iterators are the same if they "point to" the same memory location
289  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
290  }
291 
294  bool
295  operator==(const Self & it) const
296  {
297  // two iterators are the same if they "point to" the same memory location
298  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
299  }
300 
305  const IndexType GetIndex()
306  { return m_Image->ComputeIndex(m_Offset); }
307 
310  virtual void SetIndex(const IndexType & ind)
311  { m_Offset = m_Image->ComputeOffset(ind); }
312 
315  const RegionType & GetRegion() const
316  { return m_Region; }
317 
319  const PixelType Get(void) const
320  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
321 
323  void Set(const PixelType & value) const
324  {
325  this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >(
326  this->m_Buffer + this->m_Offset ) ), value);
327  }
328 
332  const PixelType & Value(void) const
333  { return *( m_Buffer + m_Offset ); }
334 
338  const PixelType & Value(void)
339  { return *( m_Buffer + m_Offset ); }
340 
344  itkLegacyMacro(Self Begin() const);
345 
348  void GoToBegin()
349  {
350  m_Offset = m_BeginOffset;
351  }
352 
356  itkLegacyMacro(Self End() const);
357 
360  void GoToEnd()
361  {
362  m_Offset = m_EndOffset;
363  }
364 
367  bool IsAtBegin()
368  {
369  return ( m_Offset == m_BeginOffset );
370  }
371 
374  bool IsAtEnd()
375  {
376  return ( m_Offset == m_EndOffset );
377  }
378 
379 protected: //made protected so other iterators can access
380  typename ImageType::ConstWeakPointer m_Image;
381 
382  RegionType m_Region; // region to iterate over
383 
385  SizeValueType m_BeginOffset; // offset to last pixel in region
386  SizeValueType m_EndOffset; // offset to one pixel before first pixel
387 
389 
392 };
393 } // end namespace itk
394 
395 // Define instantiation macro for this template.
396 #define ITK_TEMPLATE_ImageReverseConstIterator(_, EXPORT, TypeX, TypeY) \
397  namespace itk \
398  { \
399  _( 1 ( class EXPORT ImageReverseConstIterator< ITK_TEMPLATE_1 TypeX > ) ) \
400  namespace Templates \
401  { \
402  typedef ImageReverseConstIterator< ITK_TEMPLATE_1 TypeX > ImageReverseConstIterator##TypeY; \
403  } \
404  }
405 
406 #if ITK_TEMPLATE_EXPLICIT
407 #include "Templates/itkImageReverseConstIterator+-.h"
408 #endif
409 
410 #if ITK_TEMPLATE_TXX
411 #include "itkImageReverseConstIterator.hxx"
412 #endif
413 
414 #endif
415