ITK  4.9.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 
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  typedef typename TImage::AccessorFunctorType AccessorFunctorType;
131 
135  m_Region(),
136  m_PixelAccessor(),
138  {
139  m_Image = ITK_NULLPTR;
140  m_Buffer = ITK_NULLPTR;
141  m_Offset = 0;
142  m_BeginOffset = 0;
143  m_EndOffset = 0;
145  }
147 
149  virtual ~ImageConstIterator() {}
150 
154  {
155  m_Image = it.m_Image; // copy the smart pointer
156 
157  m_Region = it.m_Region;
158 
159  m_Buffer = it.m_Buffer;
160  m_Offset = it.m_Offset;
166  }
167 
171  const RegionType & region)
172  {
173  m_Image = ptr;
174  m_Buffer = m_Image->GetBufferPointer();
176 
177  SetRegion(region);
178 
179  m_PixelAccessor = ptr->GetPixelAccessor();
180  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
182  }
183 
186  Self & operator=(const Self & it)
187  {
188  if(this != &it)
189  {
190  m_Image = it.m_Image; // copy the smart pointer
191  m_Region = it.m_Region;
193 
194  m_Buffer = it.m_Buffer;
195  m_Offset = it.m_Offset;
201  }
202  return *this;
203  }
204 
206  virtual void SetRegion(const RegionType & region)
207  {
208  m_Region = region;
209 
210  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
211  {
212  const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
213  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(m_Region) ),
214  "Region " << m_Region << " is outside of buffered region " << bufferedRegion );
215  }
216 
217  // Compute the start offset
218  m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
220 
221  // Compute the end offset. If any component of m_Region.GetSize()
222  // is zero, the region is not valid and we set the EndOffset
223  // to be same as BeginOffset so that iterator end condition is met
224  // immediately.
225  if ( m_Region.GetNumberOfPixels() == 0 )
226  {
227  // region is empty, probably has a size of 0 along one dimension
229  }
230  else
231  {
232  IndexType ind( m_Region.GetIndex() );
233  SizeType size( m_Region.GetSize() );
234  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
235  {
236  ind[i] += ( static_cast< IndexValueType >( size[i] ) - 1 );
237  }
238  m_EndOffset = m_Image->ComputeOffset(ind);
239  m_EndOffset++;
240  }
241  }
242 
244  static unsigned int GetImageIteratorDimension()
245  { return TImage::ImageDimension; }
246 
249  bool
250  operator!=(const Self & it) const
251  {
252  // two iterators are the same if they "point to" the same memory location
253  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
254  }
255 
258  bool
259  operator==(const Self & it) const
260  {
261  // two iterators are the same if they "point to" the same memory location
262  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
263  }
264 
267  bool
268  operator<=(const Self & it) const
269  {
270  // an iterator is "less than" another if it "points to" a lower
271  // memory location
272  return ( m_Buffer + m_Offset ) <= ( it.m_Buffer + it.m_Offset );
273  }
274 
277  bool
278  operator<(const Self & it) const
279  {
280  // an iterator is "less than" another if it "points to" a lower
281  // memory location
282  return ( m_Buffer + m_Offset ) < ( it.m_Buffer + it.m_Offset );
283  }
284 
287  bool
288  operator>=(const Self & it) const
289  {
290  // an iterator is "greater than" another if it "points to" a higher
291  // memory location
292  return ( m_Buffer + m_Offset ) >= ( it.m_Buffer + it.m_Offset );
293  }
294 
297  bool
298  operator>(const Self & it) const
299  {
300  // an iterator is "greater than" another if it "points to" a higher
301  // memory location
302  return ( m_Buffer + m_Offset ) > ( it.m_Buffer + it.m_Offset );
303  }
304 
309  const IndexType GetIndex() const
310  { return m_Image->ComputeIndex( static_cast< OffsetValueType >( m_Offset ) ); }
311 
314  virtual void SetIndex(const IndexType & ind)
315  { m_Offset = m_Image->ComputeOffset(ind); }
316 
319  const RegionType & GetRegion() const
320  { return m_Region; }
321 
323  const ImageType * GetImage() const
324  { return m_Image.GetPointer(); }
325 
327  PixelType Get(void) const
328  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
329 
333  const PixelType & Value(void) const
334  { return *( m_Buffer + m_Offset ); }
335 
340  itkLegacyMacro(Self Begin(void) const);
341 
344  void GoToBegin()
345  {
347  }
348 
353  itkLegacyMacro(Self End(void) const);
354 
357  void GoToEnd()
358  {
360  }
361 
364  bool IsAtBegin(void) const
365  {
366  return ( m_Offset == m_BeginOffset );
367  }
368 
371  bool IsAtEnd(void) const
372  {
373  return ( m_Offset == m_EndOffset );
374  }
375 
376 protected: //made protected so other iterators can access
377  typename TImage::ConstWeakPointer m_Image;
378 
379  RegionType m_Region; // region to iterate over
380 
382  OffsetValueType m_BeginOffset; // offset to first pixel in region
383  OffsetValueType m_EndOffset; // offset to one pixel past last pixel in region
384 
386 
389 };
390 } // end namespace itk
391 
392 #ifndef ITK_MANUAL_INSTANTIATION
393 #include "itkImageConstIterator.hxx"
394 #endif
395 
396 #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
itkTypeMacroNoParent(ImageConstIterator)
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