ITK  4.6.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 >
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(),
135  {
136  m_Image = ITK_NULLPTR;
137  m_Buffer = ITK_NULLPTR;
138  m_Offset = 0;
139  m_BeginOffset = 0;
140  m_EndOffset = 0;
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;
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);
179  }
180 
183  Self & operator=(const Self & it)
184  {
185  if(this != &it)
186  {
187  m_Image = it.m_Image; // copy the smart pointer
188  m_Region = it.m_Region;
190 
191  m_Buffer = it.m_Buffer;
192  m_Offset = it.m_Offset;
198  }
199  return *this;
200  }
201 
203  virtual void SetRegion(const RegionType & region)
204  {
205  m_Region = region;
206 
207  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
208  {
209  const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
210  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(m_Region) ),
211  "Region " << m_Region << " is outside of buffered region " << bufferedRegion );
212  }
213 
214  // Compute the start offset
215  m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
217 
218  // Compute the end offset. If any component of m_Region.GetSize()
219  // is zero, the region is not valid and we set the EndOffset
220  // to be same as BeginOffset so that iterator end condition is met
221  // immediately.
222  if ( m_Region.GetNumberOfPixels() == 0 )
223  {
224  // region is empty, probably has a size of 0 along one dimension
226  }
227  else
228  {
229  IndexType ind( m_Region.GetIndex() );
230  SizeType size( m_Region.GetSize() );
231  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
232  {
233  ind[i] += ( static_cast< IndexValueType >( size[i] ) - 1 );
234  }
235  m_EndOffset = m_Image->ComputeOffset(ind);
236  m_EndOffset++;
237  }
238  }
239 
241  static unsigned int GetImageIteratorDimension()
242  { return TImage::ImageDimension; }
243 
246  bool
247  operator!=(const Self & it) const
248  {
249  // two iterators are the same if they "point to" the same memory location
250  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
251  }
252 
255  bool
256  operator==(const Self & it) const
257  {
258  // two iterators are the same if they "point to" the same memory location
259  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
260  }
261 
264  bool
265  operator<=(const Self & it) const
266  {
267  // an iterator is "less than" another if it "points to" a lower
268  // memory location
269  return ( m_Buffer + m_Offset ) <= ( it.m_Buffer + it.m_Offset );
270  }
271 
274  bool
275  operator<(const Self & it) const
276  {
277  // an iterator is "less than" another if it "points to" a lower
278  // memory location
279  return ( m_Buffer + m_Offset ) < ( it.m_Buffer + it.m_Offset );
280  }
281 
284  bool
285  operator>=(const Self & it) const
286  {
287  // an iterator is "greater than" another if it "points to" a higher
288  // 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  // an iterator is "greater than" another if it "points to" a higher
298  // memory location
299  return ( m_Buffer + m_Offset ) > ( it.m_Buffer + it.m_Offset );
300  }
301 
306  const IndexType GetIndex() const
307  { return m_Image->ComputeIndex( static_cast< OffsetValueType >( m_Offset ) ); }
308 
311  virtual void SetIndex(const IndexType & ind)
312  { m_Offset = m_Image->ComputeOffset(ind); }
313 
316  const RegionType & GetRegion() const
317  { return m_Region; }
318 
320  const ImageType * GetImage() const
321  { return m_Image.GetPointer(); }
322 
324  PixelType Get(void) const
325  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
326 
330  const PixelType & Value(void) const
331  { return *( m_Buffer + m_Offset ); }
332 
337  itkLegacyMacro(Self Begin(void) const);
338 
341  void GoToBegin()
342  {
344  }
345 
350  itkLegacyMacro(Self End(void) const);
351 
354  void GoToEnd()
355  {
357  }
358 
361  bool IsAtBegin(void) const
362  {
363  return ( m_Offset == m_BeginOffset );
364  }
365 
368  bool IsAtEnd(void) const
369  {
370  return ( m_Offset == m_EndOffset );
371  }
372 
373 protected: //made protected so other iterators can access
374  typename TImage::ConstWeakPointer m_Image;
375 
376  RegionType m_Region; // region to iterate over
377 
379  OffsetValueType m_BeginOffset; // offset to first pixel in region
380  OffsetValueType m_EndOffset; // offset to one pixel past last pixel in region
381 
383 
386 };
387 } // end namespace itk
388 
389 #ifndef ITK_MANUAL_INSTANTIATION
390 #include "itkImageConstIterator.hxx"
391 #endif
392 
393 #endif
bool operator!=(const Self &it) const
PixelContainer::Pointer PixelContainerPointer
const RegionType & GetRegion() const
signed long OffsetValueType
Definition: itkIntTypes.h:154
signed long IndexValueType
Definition: itkIntTypes.h:150
const IndexType GetIndex() const
TImage::AccessorFunctorType AccessorFunctorType
itkLegacyMacro(Self Begin(void) const)
A multi-dimensional image iterator templated over image type.
Self & operator=(const Self &it)
AccessorFunctorType m_PixelAccessorFunctor
TImage::AccessorType AccessorType
TImage::InternalPixelType InternalPixelType
virtual void SetIndex(const IndexType &ind)
const InternalPixelType * m_Buffer
ImageConstIterator(const ImageType *ptr, const RegionType &region)
TImage::ConstWeakPointer m_Image
bool operator==(const Self &it) const
bool operator<(const Self &it) const
static unsigned int GetImageIteratorDimension()
static const unsigned int ImageIteratorDimension
bool operator>(const Self &it) const
TImage::PixelContainer PixelContainer
PixelType Get(void) const
virtual void SetRegion(const RegionType &region)
bool operator>=(const Self &it) const
bool operator<=(const Self &it) const
const ImageType * GetImage() const
const PixelType & Value(void) const