ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkImageConstIterator.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 __itkImageConstIterator_h
19 #define __itkImageConstIterator_h
20 
21 #include "itkImage.h"
22 #include "itkIndex.h"
23 #include "itkNumericTraits.h"
24 
25 namespace itk
26 {
83 template< typename TImage >
84 class ITK_EXPORT ImageConstIterator
85 {
86 public:
89 
94  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
95  TImage::ImageDimension);
96 
98  typedef typename TImage::IndexType IndexType;
99 
101  typedef typename TImage::SizeType SizeType;
102 
104  typedef typename TImage::OffsetType OffsetType;
105 
107  typedef typename TImage::RegionType RegionType;
108 
110  typedef TImage ImageType;
111 
115  typedef typename TImage::PixelContainer PixelContainer;
116  typedef typename PixelContainer::Pointer PixelContainerPointer;
117 
119  typedef typename TImage::InternalPixelType InternalPixelType;
120 
122  typedef typename TImage::PixelType PixelType;
123 
126  typedef typename TImage::AccessorType AccessorType;
127  typedef typename TImage::AccessorFunctorType AccessorFunctorType;
128 
132  m_Region(),
133  m_PixelAccessor(),
134  m_PixelAccessorFunctor()
135  {
136  m_Image = 0;
137  m_Buffer = 0;
138  m_Offset = 0;
139  m_BeginOffset = 0;
140  m_EndOffset = 0;
141  m_PixelAccessorFunctor.SetBegin(m_Buffer);
142  }
144 
146  virtual ~ImageConstIterator() {}
147 
151  {
152  m_Image = it.m_Image; // copy the smart pointer
153 
154  m_Region = it.m_Region;
155 
156  m_Buffer = it.m_Buffer;
157  m_Offset = it.m_Offset;
158  m_BeginOffset = it.m_BeginOffset;
159  m_EndOffset = it.m_EndOffset;
160  m_PixelAccessor = it.m_PixelAccessor;
161  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
162  m_PixelAccessorFunctor.SetBegin(m_Buffer);
163  }
164 
168  const RegionType & region)
169  {
170  m_Image = ptr;
171  m_Buffer = m_Image->GetBufferPointer();
173 
174  SetRegion(region);
175 
176  m_PixelAccessor = ptr->GetPixelAccessor();
177  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
178  m_PixelAccessorFunctor.SetBegin(m_Buffer);
179  }
180 
183  Self & operator=(const Self & it)
184  {
185  m_Image = it.m_Image; // copy the smart pointer
186  m_Region = it.m_Region;
187 
188  m_Buffer = it.m_Buffer;
189  m_Offset = it.m_Offset;
190  m_BeginOffset = it.m_BeginOffset;
191  m_EndOffset = it.m_EndOffset;
192  m_PixelAccessor = it.m_PixelAccessor;
193  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
194  m_PixelAccessorFunctor.SetBegin(m_Buffer);
195 
196  return *this;
197  }
198 
200  virtual void SetRegion(const RegionType & region)
201  {
202  m_Region = region;
203 
204  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
205  {
206  const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
207  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(m_Region) ),
208  "Region " << m_Region << " is outside of buffered region " << bufferedRegion );
209  }
210 
211  // Compute the start offset
212  m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
213  m_BeginOffset = m_Offset;
214 
215  // Compute the end offset. If any component of m_Region.GetSize()
216  // is zero, the region is not valid and we set the EndOffset
217  // to be same as BeginOffset so that iterator end condition is met
218  // immediately.
219  if ( m_Region.GetNumberOfPixels() == 0 )
220  {
221  // region is empty, probably has a size of 0 along one dimension
222  m_EndOffset = m_BeginOffset;
223  }
224  else
225  {
226  IndexType ind( m_Region.GetIndex() );
227  SizeType size( m_Region.GetSize() );
228  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
229  {
230  ind[i] += ( static_cast< IndexValueType >( size[i] ) - 1 );
231  }
232  m_EndOffset = m_Image->ComputeOffset(ind);
233  m_EndOffset++;
234  }
235  }
236 
238  static unsigned int GetImageIteratorDimension()
239  { return TImage::ImageDimension; }
240 
243  bool
244  operator!=(const Self & it) const
245  {
246  // two iterators are the same if they "point to" the same memory location
247  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
248  }
249 
252  bool
253  operator==(const Self & it) const
254  {
255  // two iterators are the same if they "point to" the same memory location
256  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
257  }
258 
261  bool
262  operator<=(const Self & it) const
263  {
264  // an iterator is "less than" another if it "points to" a lower
265  // memory location
266  return ( m_Buffer + m_Offset ) <= ( it.m_Buffer + it.m_Offset );
267  }
268 
271  bool
272  operator<(const Self & it) const
273  {
274  // an iterator is "less than" another if it "points to" a lower
275  // memory location
276  return ( m_Buffer + m_Offset ) < ( it.m_Buffer + it.m_Offset );
277  }
278 
281  bool
282  operator>=(const Self & it) const
283  {
284  // an iterator is "greater than" another if it "points to" a higher
285  // memory location
286  return ( m_Buffer + m_Offset ) >= ( it.m_Buffer + it.m_Offset );
287  }
288 
291  bool
292  operator>(const Self & it) const
293  {
294  // an iterator is "greater than" another if it "points to" a higher
295  // memory location
296  return ( m_Buffer + m_Offset ) > ( it.m_Buffer + it.m_Offset );
297  }
298 
303  const IndexType GetIndex() const
304  { return m_Image->ComputeIndex( static_cast< OffsetValueType >( m_Offset ) ); }
305 
308  virtual void SetIndex(const IndexType & ind)
309  { m_Offset = m_Image->ComputeOffset(ind); }
310 
313  const RegionType & GetRegion() const
314  { return m_Region; }
315 
317  const ImageType * GetImage() const
318  { return m_Image.GetPointer(); }
319 
321  PixelType Get(void) const
322  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
323 
327  const PixelType & Value(void) const
328  { return *( m_Buffer + m_Offset ); }
329 
334  itkLegacyMacro(Self Begin(void) const);
335 
338  void GoToBegin()
339  {
340  m_Offset = m_BeginOffset;
341  }
342 
347  itkLegacyMacro(Self End(void) const);
348 
351  void GoToEnd()
352  {
353  m_Offset = m_EndOffset;
354  }
355 
358  bool IsAtBegin(void) const
359  {
360  return ( m_Offset == m_BeginOffset );
361  }
362 
365  bool IsAtEnd(void) const
366  {
367  return ( m_Offset == m_EndOffset );
368  }
369 
370 protected: //made protected so other iterators can access
371  typename TImage::ConstWeakPointer m_Image;
372 
373  RegionType m_Region; // region to iterate over
374 
376  OffsetValueType m_BeginOffset; // offset to first pixel in region
377  OffsetValueType m_EndOffset; // offset to one pixel past last pixel in region
378 
379  const InternalPixelType *m_Buffer;
380 
381  AccessorType m_PixelAccessor;
382  AccessorFunctorType m_PixelAccessorFunctor;
383 };
384 } // end namespace itk
385 
386 #ifndef ITK_MANUAL_INSTANTIATION
387 #include "itkImageConstIterator.hxx"
388 #endif
389 
390 #endif
391