ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkImageReverseConstIterator.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 itkImageReverseConstIterator_h
19 #define itkImageReverseConstIterator_h
20 
21 #include "itkSize.h"
22 #include "itkImageConstIterator.h"
23 #include <memory>
24 
25 namespace itk
26 {
86 template< typename TImage >
87 class ITK_TEMPLATE_EXPORT ImageReverseConstIterator
88 {
89 public:
92 
97  static constexpr unsigned int ImageIteratorDimension = TImage::ImageDimension;
98 
100  itkTypeMacroNoParent(ImageReverseConstIterator);
101 
103  using IndexType = typename TImage::IndexType;
104 
106  using SizeType = typename TImage::SizeType;
107 
109  using OffsetType = typename TImage::OffsetType;
110 
112  using RegionType = typename TImage::RegionType;
113 
115  using ImageType = TImage;
116 
120  using PixelContainer = typename TImage::PixelContainer;
121  using PixelContainerPointer = typename PixelContainer::Pointer;
122 
124  using InternalPixelType = typename TImage::InternalPixelType;
125 
127  using PixelType = typename TImage::PixelType;
128 
131  using AccessorType = typename TImage::AccessorType;
132 
134  using AccessorFunctorType = typename TImage::AccessorFunctorType;
135 
138  ImageReverseConstIterator():m_PixelAccessor(), m_PixelAccessorFunctor()
139  {
140  m_Buffer = 0;
141  m_Offset = 0;
142  m_BeginOffset = 0;
143  m_EndOffset = 0;
144  m_PixelAccessorFunctor.SetBegin(m_Buffer);
145  }
147 
149  virtual ~ImageReverseConstIterator() = default;
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 
170  ImageReverseConstIterator(const ImageType *ptr, const RegionType & region)
171  {
172  SizeValueType offset;
173 
174  m_Image = ptr;
175  m_Buffer = m_Image->GetBufferPointer();
176  m_Region = region;
177 
178  // Compute the end offset, one pixel before the first pixel
179  offset = m_Image->ComputeOffset( m_Region.GetIndex() );
180  m_EndOffset = offset - 1;
181 
182  // Compute the begin offset, the last pixel in the region
183  IndexType ind( m_Region.GetIndex() );
184  SizeType size( m_Region.GetSize() );
185  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
186  {
187  ind[i] += ( size[i] - 1 );
188  }
189  m_BeginOffset = m_Image->ComputeOffset(ind);
190  m_Offset = m_BeginOffset;
191 
192  m_PixelAccessor = ptr->GetPixelAccessor();
193  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
194  m_PixelAccessorFunctor.SetBegin(m_Buffer);
195  }
196 
205  {
206  m_Image = it.GetImage();
207  m_Region = it.GetRegion();
208  m_Buffer = m_Image->GetBufferPointer();
210 
211  IndexType ind = it.GetIndex();
212 
213  m_Offset = m_Image->ComputeOffset(ind);
214 
215  // Compute the end offset, one pixel before the first pixel
216  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
217 
218  // Compute the begin offset, the last pixel in the region
219  IndexType regInd( m_Region.GetIndex() );
220  SizeType regSize( m_Region.GetSize() );
221  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
222  {
223  regInd[i] += ( regSize[i] - 1 );
224  }
225  m_BeginOffset = m_Image->ComputeOffset(regInd);
226 
227  m_PixelAccessor = m_Image->GetPixelAccessor();
228  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
229  m_PixelAccessorFunctor.SetBegin(m_Buffer);
230  }
231 
234  Self & operator=(const Self & it)
235  {
236  if(this != &it)
237  {
238  m_Image = it.m_Image; // copy the smart pointer
239  m_Region = it.m_Region;
241 
242  m_Buffer = it.m_Buffer;
243  m_Offset = it.m_Offset;
244  m_BeginOffset = it.m_BeginOffset;
245  m_EndOffset = it.m_EndOffset;
246  m_PixelAccessor = it.m_PixelAccessor;
247  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
248  m_PixelAccessorFunctor.SetBegin(m_Buffer);
249  }
250  return *this;
251  }
252 
256  {
257  m_Image = it.GetImage();
258  m_Region = it.GetRegion();
259  m_Buffer = m_Image->GetBufferPointer();
261 
262  IndexType ind = it.GetIndex();
263 
264  m_Offset = m_Image->ComputeOffset(ind);
265 
266  // Compute the end offset, one pixel before the first pixel
267  m_EndOffset = m_Image->ComputeOffset( m_Region.GetIndex() ) - 1;
268 
269  // Compute the begin offset, the last pixel in the region
270  IndexType regInd( m_Region.GetIndex() );
271  SizeType regSize( m_Region.GetSize() );
272  for ( unsigned int i = 0; i < TImage::ImageDimension; ++i )
273  {
274  regInd[i] += ( regSize[i] - 1 );
275  }
276  m_BeginOffset = m_Image->ComputeOffset(regInd);
277 
278  m_PixelAccessor = m_Image->GetPixelAccessor();
279  m_PixelAccessorFunctor.SetPixelAccessor(m_PixelAccessor);
280  m_PixelAccessorFunctor.SetBegin(m_Buffer);
281  return *this;
282  }
283 
285  static unsigned int GetImageIteratorDimension()
286  { return TImage::ImageDimension; }
287 
290  bool
291  operator!=(const Self & it) const
292  {
293  // two iterators are the same if they "point to" the same memory location
294  return ( m_Buffer + m_Offset ) != ( it.m_Buffer + it.m_Offset );
295  }
296 
299  bool
300  operator==(const Self & it) const
301  {
302  // two iterators are the same if they "point to" the same memory location
303  return ( m_Buffer + m_Offset ) == ( it.m_Buffer + it.m_Offset );
304  }
305 
311  { return m_Image->ComputeIndex(m_Offset); }
312 
315  virtual void SetIndex(const IndexType & ind)
316  { m_Offset = m_Image->ComputeOffset(ind); }
317 
320  const RegionType & GetRegion() const
321  { return m_Region; }
322 
324  const PixelType Get() const
325  { return m_PixelAccessorFunctor.Get( *( m_Buffer + m_Offset ) ); }
326 
328  void Set(const PixelType & value) const
329  {
330  this->m_PixelAccessorFunctor.Set(*( const_cast< InternalPixelType * >(
331  this->m_Buffer + this->m_Offset ) ), value);
332  }
333 
337  const PixelType & Value() const
338  { return *( m_Buffer + m_Offset ); }
339 
343  const PixelType & Value()
344  { return *( m_Buffer + m_Offset ); }
345 
348  void GoToBegin()
349  {
350  m_Offset = m_BeginOffset;
351  }
352 
355  void GoToEnd()
356  {
357  m_Offset = m_EndOffset;
358  }
359 
362  bool IsAtBegin() const
363  {
364  return ( m_Offset == m_BeginOffset );
365  }
366 
369  bool IsAtEnd() const
370  {
371  return ( m_Offset == m_EndOffset );
372  }
373 
374 protected: //made protected so other iterators can access
375  typename ImageType::ConstWeakPointer m_Image;
376 
377  RegionType m_Region; // region to iterate over
378 
380  SizeValueType m_BeginOffset; // offset to last pixel in region
381  SizeValueType m_EndOffset; // offset to one pixel before first pixel
382 
384 
387 };
388 } // end namespace itk
389 
390 #endif
const RegionType & GetRegion() const
Self & operator=(const ImageConstIterator< TImage > &it)
unsigned long SizeValueType
Definition: itkIntTypes.h:83
const IndexType GetIndex() const
typename TImage::PixelContainer PixelContainer
typename TImage::AccessorType AccessorType
ImageReverseConstIterator(const ImageType *ptr, const RegionType &region)
A multi-dimensional image iterator templated over image type.
typename TImage::InternalPixelType InternalPixelType
ImageReverseConstIterator(const ImageConstIterator< TImage > &it)
Multi-dimensional image iterator.
virtual void SetIndex(const IndexType &ind)
void Set(const PixelType &value) const
typename PixelContainer::Pointer PixelContainerPointer
typename TImage::AccessorFunctorType AccessorFunctorType
const ImageType * GetImage() const