ITK  5.0.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_TEMPLATE_EXPORT ImageConstIterator
85 {
86 public:
89 
94  static constexpr unsigned int ImageIteratorDimension = TImage::ImageDimension;
95 
97  itkTypeMacroNoParent(ImageConstIterator);
98 
100  using IndexType = typename TImage::IndexType;
101 
103  using SizeType = typename TImage::SizeType;
104 
106  using OffsetType = typename TImage::OffsetType;
107 
109  using RegionType = typename TImage::RegionType;
110 
112  using ImageType = TImage;
113 
117  using PixelContainer = typename TImage::PixelContainer;
118  using PixelContainerPointer = typename PixelContainer::Pointer;
119 
121  using InternalPixelType = typename TImage::InternalPixelType;
122 
124  using PixelType = typename TImage::PixelType;
125 
128  using AccessorType = typename TImage::AccessorType;
129  using AccessorFunctorType = typename TImage::AccessorFunctorType;
130 
134  m_Region(),
135  m_PixelAccessor(),
136  m_PixelAccessorFunctor()
137  {
138  m_Image = nullptr;
139  m_Buffer = nullptr;
140  m_Offset = 0;
141  m_BeginOffset = 0;
142  m_EndOffset = 0;
143  m_PixelAccessorFunctor.SetBegin(m_Buffer);
144  }
146 
148  virtual ~ImageConstIterator() = default;
149 
153  {
154  m_Image = it.m_Image; // copy the smart pointer
155 
156  m_Region = it.m_Region;
157 
158  m_Buffer = it.m_Buffer;
159  m_Offset = it.m_Offset;
160  m_BeginOffset = it.m_BeginOffset;
161  m_EndOffset = it.m_EndOffset;
162  m_PixelAccessor = it.m_PixelAccessor;
163  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
164  m_PixelAccessorFunctor.SetBegin(m_Buffer);
165  }
166 
170  const RegionType & region)
171  {
172  m_Image = ptr;
173  m_Buffer = m_Image->GetBufferPointer();
175 
176  SetRegion(region);
177 
178  m_PixelAccessor = ptr->GetPixelAccessor();
179  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
180  m_PixelAccessorFunctor.SetBegin(m_Buffer);
181  }
182 
185  Self & operator=(const Self & it)
186  {
187  if(this != &it)
188  {
189  m_Image = it.m_Image; // copy the smart pointer
190  m_Region = it.m_Region;
192 
193  m_Buffer = it.m_Buffer;
194  m_Offset = it.m_Offset;
195  m_BeginOffset = it.m_BeginOffset;
196  m_EndOffset = it.m_EndOffset;
197  m_PixelAccessor = it.m_PixelAccessor;
198  m_PixelAccessorFunctor = it.m_PixelAccessorFunctor;
199  m_PixelAccessorFunctor.SetBegin(m_Buffer);
200  }
201  return *this;
202  }
203 
205  virtual void SetRegion(const RegionType & region)
206  {
207  m_Region = region;
208 
209  if ( region.GetNumberOfPixels() > 0 ) // If region is non-empty
210  {
211  const RegionType & bufferedRegion = m_Image->GetBufferedRegion();
212  itkAssertOrThrowMacro( ( bufferedRegion.IsInside(m_Region) ),
213  "Region " << m_Region << " is outside of buffered region " << bufferedRegion );
214  }
215 
216  // Compute the start offset
217  m_Offset = m_Image->ComputeOffset( m_Region.GetIndex() );
218  m_BeginOffset = m_Offset;
219 
220  // Compute the end offset. If any component of m_Region.GetSize()
221  // is zero, the region is not valid and we set the EndOffset
222  // to be same as BeginOffset so that iterator end condition is met
223  // immediately.
224  IndexType ind( m_Region.GetIndex() );
225  SizeType size( m_Region.GetSize() );
226  if ( m_Region.GetNumberOfPixels() == 0 )
227  {
228  // region is empty, probably has a size of 0 along one dimension
229  m_EndOffset = m_BeginOffset;
230  }
231  else
232  {
233  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
234  {
235  ind[i] += ( static_cast< IndexValueType >( size[i] ) - 1 );
236  }
237  m_EndOffset = m_Image->ComputeOffset(ind);
238  m_EndOffset++;
239  }
240  }
241 
243  static unsigned int GetImageIteratorDimension()
244  { return TImage::ImageDimension; }
245 
248  bool
249  operator!=(const Self & it) const
250  {
251  // two iterators are the same if they "point to" the same memory location
252  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
253  }
254 
257  bool
258  operator==(const Self & it) const
259  {
260  // two iterators are the same if they "point to" the same memory location
261  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
262  }
263 
266  bool
267  operator<=(const Self & it) const
268  {
269  // an iterator is "less than" another if it "points to" a lower
270  // memory location
271  return ( m_Buffer + m_Offset ) <= ( it.m_Buffer + it.m_Offset );
272  }
273 
276  bool
277  operator<(const Self & it) const
278  {
279  // an iterator is "less than" another if it "points to" a lower
280  // memory location
281  return ( m_Buffer + m_Offset ) < ( it.m_Buffer + it.m_Offset );
282  }
283 
286  bool
287  operator>=(const Self & it) const
288  {
289  // an iterator is "greater than" another if it "points to" a higher
290  // memory location
291  return ( m_Buffer + m_Offset ) >= ( it.m_Buffer + it.m_Offset );
292  }
293 
296  bool
297  operator>(const Self & it) const
298  {
299  // an iterator is "greater than" another if it "points to" a higher
300  // memory location
301  return ( m_Buffer + m_Offset ) > ( it.m_Buffer + it.m_Offset );
302  }
303 
308  const IndexType GetIndex() const
309  { return m_Image->ComputeIndex( static_cast< OffsetValueType >( m_Offset ) ); }
310 
313  virtual void SetIndex(const IndexType & ind)
314  { m_Offset = m_Image->ComputeOffset(ind); }
315 
318  const RegionType & GetRegion() const
319  { return m_Region; }
320 
322  const ImageType * GetImage() const
323  { return m_Image.GetPointer(); }
324 
326  PixelType Get() const
327  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
328 
332  const PixelType & Value() const
333  { return *( m_Buffer + m_Offset ); }
334 
337  void GoToBegin()
338  {
339  m_Offset = m_BeginOffset;
340  }
341 
344  void GoToEnd()
345  {
346  m_Offset = m_EndOffset;
347  }
348 
351  bool IsAtBegin() const
352  {
353  return ( m_Offset == m_BeginOffset );
354  }
355 
358  bool IsAtEnd() const
359  {
360  return ( m_Offset == m_EndOffset );
361  }
362 
363 protected: //made protected so other iterators can access
364  typename TImage::ConstWeakPointer m_Image;
365 
366  RegionType m_Region; // region to iterate over
367 
369  OffsetValueType m_BeginOffset; // offset to first pixel in region
370  OffsetValueType m_EndOffset; // offset to one pixel past last pixel in region
371 
373 
376 };
377 } // end namespace itk
378 
379 #endif
bool operator!=(const Self &it) const
const RegionType & GetRegion() const
typename BufferType::OffsetType OffsetType
typename BufferType::InternalPixelType InternalPixelType
typename PixelContainer::Pointer PixelContainerPointer
const IndexType GetIndex() const
typename BufferType::PixelType PixelType
typename BufferType::AccessorFunctorType AccessorFunctorType
typename BufferType::PixelContainer PixelContainer
A multi-dimensional image iterator templated over image type.
Self & operator=(const Self &it)
AccessorFunctorType m_PixelAccessorFunctor
bool operator<=(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:514
virtual void SetIndex(const IndexType &ind)
const InternalPixelType * m_Buffer
const PixelType & Value() const
ImageConstIterator(const ImageType *ptr, const RegionType &region)
signed long IndexValueType
Definition: itkIntTypes.h:90
typename BufferType::IndexType IndexType
TImage::ConstWeakPointer m_Image
bool operator==(const Self &it) const
typename BufferType::SizeType SizeType
bool operator<(const Index< VDimension > &one, const Index< VDimension > &two)
Definition: itkIndex.h:499
static unsigned int GetImageIteratorDimension()
bool operator>(const Self &it) const
typename BufferType::AccessorType AccessorType
typename BufferType::RegionType RegionType
signed long OffsetValueType
Definition: itkIntTypes.h:94
virtual void SetRegion(const RegionType &region)
bool operator>=(const Self &it) const
const ImageType * GetImage() const