Orfeo Toolbox  3.16
itkImageConstIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkImageConstIterator.h,v $
5  Language: C++
6  Date: $Date: 2009-05-11 21:48:29 $
7  Version: $Revision: 1.27 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkImageConstIterator_h
18 #define __itkImageConstIterator_h
19 
20 #include "itkImage.h"
21 #include "itkIndex.h"
22 #include "itkSize.h"
23 #include "itkOffset.h"
24 #include "itkNumericTraits.h"
25 
26 namespace itk
27 {
28 
84 template<typename TImage>
86 {
87 public:
90 
95  itkStaticConstMacro(ImageIteratorDimension, unsigned int,
96  TImage::ImageDimension);
97 
99  typedef typename TImage::IndexType IndexType;
100  typedef typename TImage::IndexValueType IndexValueType;
101 
103  typedef typename TImage::SizeType SizeType;
104  typedef typename TImage::SizeValueType SizeValueType;
105 
107  typedef typename TImage::OffsetType OffsetType;
108  typedef typename TImage::OffsetValueType OffsetValueType;
109 
111  typedef typename TImage::RegionType RegionType;
112 
114  typedef TImage ImageType;
115 
119  typedef typename TImage::PixelContainer PixelContainer;
120  typedef typename PixelContainer::Pointer PixelContainerPointer;
121 
123  typedef typename TImage::InternalPixelType InternalPixelType;
124 
126  typedef typename TImage::PixelType PixelType;
127 
130  typedef typename TImage::AccessorType AccessorType;
131  typedef typename TImage::AccessorFunctorType AccessorFunctorType;
132 
136  : m_Region(),
137  m_PixelAccessor(),
138  m_PixelAccessorFunctor()
139  {
140  m_Image = 0;
141  m_Buffer = 0;
142  m_Offset = 0;
143  m_BeginOffset = 0;
144  m_EndOffset = 0;
145  m_PixelAccessorFunctor.SetBegin( m_Buffer );
146  }
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;
161  m_BeginOffset = it.m_BeginOffset;
162  m_EndOffset = it.m_EndOffset;
163  m_PixelAccessor = it.m_PixelAccessor;
164  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
165  m_PixelAccessorFunctor.SetBegin( m_Buffer );
166  }
167 
171  const RegionType &region )
172  {
173  m_Image = ptr;
174  m_Buffer = m_Image->GetBufferPointer();
175  m_Region = region;
176 
177 #ifdef ITK_USE_REGION_VALIDATION_IN_ITERATORS
178  if( region.GetNumberOfPixels() > 0 ) // If region is non-empty
179  {
180  const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
181  itkAssertOrThrowMacro( (bufferedRegion.IsInside( m_Region )),
182  "Region " << m_Region << " is outside of buffered region " << bufferedRegion );
183  }
184 #endif
185 
186  // Compute the start offset
187  m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
188  m_BeginOffset = m_Offset;
189 
190  // Compute the end offset. If any component of m_Region.GetSize()
191  // is zero, the region is not valid and we set the EndOffset
192  // to be same as BeginOffset so that iterator end condition is met
193  // immediately.
194  if (m_Region.GetNumberOfPixels() == 0)
195  {
196  // region is empty, probably has a size of 0 along one dimension
197  m_EndOffset = m_BeginOffset;
198  }
199  else
200  {
201  IndexType ind(m_Region.GetIndex());
202  SizeType size(m_Region.GetSize());
203  for (unsigned int i=0; i < ImageIteratorDimension; ++i)
204  {
205  ind[i] += (static_cast<IndexValueType>(size[i]) - 1);
206  }
207  m_EndOffset = m_Image->ComputeOffset( ind );
208  m_EndOffset++;
209  }
210 
211  m_PixelAccessor = ptr->GetPixelAccessor();
212  m_PixelAccessorFunctor.SetPixelAccessor( m_PixelAccessor );
213  m_PixelAccessorFunctor.SetBegin( m_Buffer );
214  }
215 
218  Self &operator=(const Self& it)
219  {
220  m_Image = it.m_Image; // copy the smart pointer
221  m_Region = it.m_Region;
222 
223  m_Buffer = it.m_Buffer;
224  m_Offset = it.m_Offset;
225  m_BeginOffset = it.m_BeginOffset;
226  m_EndOffset = it.m_EndOffset;
227  m_PixelAccessor = it.m_PixelAccessor;
228  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
229  m_PixelAccessorFunctor.SetBegin( m_Buffer );
230 
231  return *this;
232  }
233 
235  static unsigned int GetImageIteratorDimension()
236  {return ImageIteratorDimension; }
237 
240  bool
241  operator!=(const Self &it) const
242  {
243  // two iterators are the same if they "point to" the same memory location
244  return (m_Buffer + m_Offset) != (it.m_Buffer + it.m_Offset);
245  };
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  // an iterator is "less than" another if it "points to" a lower
262  // memory location
263  return (m_Buffer + m_Offset) <= (it.m_Buffer + it.m_Offset);
264  };
265 
268  bool
269  operator<(const Self &it) const
270  {
271  // an iterator is "less than" another if it "points to" a lower
272  // memory location
273  return (m_Buffer + m_Offset) < (it.m_Buffer + it.m_Offset);
274  };
275 
278  bool
279  operator>=(const Self &it) const
280  {
281  // an iterator is "greater than" another if it "points to" a higher
282  // memory location
283  return (m_Buffer + m_Offset) >= (it.m_Buffer + it.m_Offset);
284  };
285 
288  bool
289  operator>(const Self &it) const
290  {
291  // an iterator is "greater than" another if it "points to" a higher
292  // memory location
293  return (m_Buffer + m_Offset) > (it.m_Buffer + it.m_Offset);
294  };
295 
300  const IndexType GetIndex() const
301  { return m_Image->ComputeIndex( static_cast<OffsetValueType>(m_Offset) ); }
302 
305  virtual void SetIndex(const IndexType &ind)
306  { m_Offset = m_Image->ComputeOffset( ind ); }
307 
310  const RegionType& GetRegion() const
311  { return m_Region; }
312 
314  const ImageType * GetImage() const
315  { return m_Image.GetPointer(); }
316 
318  PixelType Get(void) const
319  { return m_PixelAccessorFunctor.Get(*(m_Buffer+m_Offset)); }
320 
324  const PixelType & Value(void) const
325  { return *(m_Buffer + m_Offset); }
326 
331  Self Begin(void) const;
332 
335  void GoToBegin()
336  {
337  m_Offset = m_BeginOffset;
338  }
339 
344  Self End(void) const;
345 
348  void GoToEnd()
349  {
350  m_Offset = m_EndOffset;
351  }
352 
355  bool IsAtBegin(void) const
356  {
357  return (m_Offset == m_BeginOffset);
358  }
359 
362  bool IsAtEnd(void) const
363  {
364  return (m_Offset == m_EndOffset);
365  }
366 
367 
368 protected: //made protected so other iterators can access
369  typename TImage::ConstWeakPointer m_Image;
370  RegionType m_Region; // region to iterate over
371 
372  unsigned long m_Offset;
373  unsigned long m_BeginOffset; // offset to first pixel in region
374  unsigned long m_EndOffset; // offset to one pixel past last pixel in region
375 
377 
380 };
381 
382 } // end namespace itk
383 
384 // Define instantiation macro for this template.
385 #define ITK_TEMPLATE_ImageConstIterator(_, EXPORT, x, y) namespace itk { \
386  _(1(class EXPORT ImageConstIterator< ITK_TEMPLATE_1 x >)) \
387  namespace Templates { typedef ImageConstIterator< ITK_TEMPLATE_1 x > ImageConstIterator##y; } \
388  }
389 
390 
391 #if ITK_TEMPLATE_EXPLICIT
392 # include "Templates/itkImageConstIterator+-.h"
393 #endif
394 
395 #if ITK_TEMPLATE_TXX
396 # include "itkImageConstIterator.txx"
397 #endif
398 
399 #endif

Generated at Sat Feb 2 2013 23:42:41 for Orfeo Toolbox with doxygen 1.8.1.1