ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkConstNeighborhoodIterator.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 itkConstNeighborhoodIterator_h
19 #define itkConstNeighborhoodIterator_h
20 
21 #include <vector>
22 #include <cstring>
23 #include <iostream>
24 #include "itkImage.h"
25 #include "itkNeighborhood.h"
26 #include "itkMacro.h"
28 
29 namespace itk
30 {
49 template< typename TImage, typename TBoundaryCondition =
51 class ITK_TEMPLATE_EXPORT ConstNeighborhoodIterator:
52  public Neighborhood< typename TImage::InternalPixelType *,
53  TImage::ImageDimension >
54 {
55 public:
57  using InternalPixelType = typename TImage::InternalPixelType;
58  using PixelType = typename TImage::PixelType;
59 
61  using DimensionValueType = unsigned int;
62 
64  static constexpr DimensionValueType Dimension = TImage::ImageDimension;
65 
68  using Superclass = Neighborhood< InternalPixelType *,
70 
74  using SizeType = typename Superclass::SizeType;
75  using Iterator = typename Superclass::Iterator;
77 
79  using ImageType = TImage;
80  using RegionType = typename TImage::RegionType;
83 
84 
86  using BoundaryConditionType = TBoundaryCondition;
87 
89 
92 
96  using NeighborhoodAccessorFunctorType = typename ImageType::NeighborhoodAccessorFunctorType;
97 
101 
104 
106  ~ConstNeighborhoodIterator() override = default;
107 
110 
114  const ImageType *ptr,
115  const RegionType & region)
116  {
117  this->Initialize(radius, ptr, region);
118  for ( DimensionValueType i = 0; i < Dimension; i++ )
119  { m_InBounds[i] = false; }
120  this->ResetBoundaryCondition();
121  m_NeighborhoodAccessorFunctor = ptr->GetNeighborhoodAccessor();
122  m_NeighborhoodAccessorFunctor.SetBegin( ptr->GetBufferPointer() );
123  }
125 
127  Self & operator=(const Self & orig);
128 
130  void PrintSelf(std::ostream &, Indent) const override;
131 
134  OffsetType ComputeInternalIndex(const NeighborIndexType n) const;
135 
138  { return m_Bound; }
139 
143  { return m_Bound[n]; }
144 
146  const InternalPixelType * GetCenterPointer() const
147  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
148 
152  { return m_NeighborhoodAccessorFunctor.Get( this->GetCenterPointer() ); }
153 
155  const ImageType * GetImagePointer() const
156  { return m_ConstImage; }
157 
160  ITK_ITERATOR_VIRTUAL IndexType GetIndex() const ITK_ITERATOR_FINAL
161  { return m_Loop; }
162 
164  { return m_Loop + o; }
165 
168  ITK_ITERATOR_VIRTUAL NeighborhoodType GetNeighborhood() const ITK_ITERATOR_FINAL;
169 
171  ITK_ITERATOR_VIRTUAL PixelType GetPixel(const NeighborIndexType i) const ITK_ITERATOR_FINAL
172  {
173  if ( !m_NeedToUseBoundaryCondition || this->InBounds() )
174  {
175  return ( m_NeighborhoodAccessorFunctor.Get( this->operator[](i) ) );
176  }
177 
178  OffsetType internalIndex;
179  OffsetType offset;
180 
181  return this->IndexInBounds(i, internalIndex, offset) ?
182  m_NeighborhoodAccessorFunctor.Get(this->operator[](i)) :
183  m_NeighborhoodAccessorFunctor.BoundaryCondition(internalIndex, offset, this, m_BoundaryCondition);
184  }
185 
191  ITK_ITERATOR_VIRTUAL PixelType GetPixel(NeighborIndexType i, bool & IsInBounds) const ITK_ITERATOR_FINAL;
192 
195  ITK_ITERATOR_VIRTUAL PixelType GetPixel(const OffsetType & o) const ITK_ITERATOR_FINAL
196  {
197  bool inbounds;
198 
199  return ( this->GetPixel(this->GetNeighborhoodIndex(o), inbounds) );
200  }
201 
207  ITK_ITERATOR_VIRTUAL PixelType GetPixel(const OffsetType & o,
208  bool & IsInBounds) const ITK_ITERATOR_FINAL
209  { return ( this->GetPixel(this->GetNeighborhoodIndex(o), IsInBounds) ); }
210 
214  ITK_ITERATOR_VIRTUAL PixelType GetNext(const unsigned axis, NeighborIndexType i) const ITK_ITERATOR_FINAL
215  {
216  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
217  + ( i * this->GetStride(axis) ) ) );
218  }
219 
223  ITK_ITERATOR_VIRTUAL PixelType GetNext(const unsigned axis) const ITK_ITERATOR_FINAL
224  {
225  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
226  + this->GetStride(axis) ) );
227  }
228 
232  ITK_ITERATOR_VIRTUAL PixelType GetPrevious(const unsigned axis, NeighborIndexType i) const ITK_ITERATOR_FINAL
233  {
234  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
235  - ( i * this->GetStride(axis) ) ) );
236  }
237 
241  ITK_ITERATOR_VIRTUAL PixelType GetPrevious(const unsigned axis) const ITK_ITERATOR_FINAL
242  {
243  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
244  - this->GetStride(axis) ) );
245  }
246 
249  ITK_ITERATOR_VIRTUAL IndexType GetIndex(const OffsetType & o) const ITK_ITERATOR_FINAL
250  { return ( this->GetIndex() + o ); }
251 
254  ITK_ITERATOR_VIRTUAL IndexType GetIndex(NeighborIndexType i) const ITK_ITERATOR_FINAL
255  { return ( this->GetIndex() + this->GetOffset(i) ); }
256 
259  { return m_Region; }
260 
264  { return m_BeginIndex; }
265 
268  RegionType GetBoundingBoxAsImageRegion() const;
269 
272  { return m_WrapOffset; }
273 
280  { return m_WrapOffset[n]; }
281 
283  ITK_ITERATOR_VIRTUAL void GoToBegin() ITK_ITERATOR_FINAL;
284 
287  ITK_ITERATOR_VIRTUAL void GoToEnd() ITK_ITERATOR_FINAL;
288 
291  ITK_ITERATOR_VIRTUAL void Initialize(const SizeType & radius, const ImageType *ptr,
292  const RegionType & region) ITK_ITERATOR_FINAL;
293 
296  ITK_ITERATOR_VIRTUAL bool IsAtBegin() const ITK_ITERATOR_FINAL
297  { return ( this->GetCenterPointer() == m_Begin ); }
298 
301  ITK_ITERATOR_VIRTUAL bool IsAtEnd() const ITK_ITERATOR_FINAL
302  {
303  if ( this->GetCenterPointer() > m_End )
304  {
305  ExceptionObject e(__FILE__, __LINE__);
306  std::ostringstream msg;
307  msg << "In method IsAtEnd, CenterPointer = " << this->GetCenterPointer()
308  << " is greater than End = " << m_End
309  << std::endl
310  << " " << *this;
311  e.SetDescription( msg.str().c_str() );
312  throw e;
313  }
314  return ( this->GetCenterPointer() == m_End );
315  }
317 
322  Self & operator++();
323 
328  Self & operator--();
329 
333  bool operator==(const Self & it) const
334  { return it.GetCenterPointer() == this->GetCenterPointer(); }
335 
339  bool operator!=(const Self & it) const
340  { return it.GetCenterPointer() != this->GetCenterPointer(); }
341 
345  bool operator<(const Self & it) const
346  { return this->GetCenterPointer() < it.GetCenterPointer(); }
347 
351  bool operator<=(const Self & it) const
352  { return this->GetCenterPointer() <= it.GetCenterPointer(); }
353 
357  bool operator>(const Self & it) const
358  { return this->GetCenterPointer() > it.GetCenterPointer(); }
359 
363  bool operator>=(const Self & it) const
364  { return this->GetCenterPointer() >= it.GetCenterPointer(); }
365 
370  void SetLocation(const IndexType & position)
371  {
372  this->SetLoop(position);
373  this->SetPixelPointers(position);
374  }
376 
380  Self & operator+=(const OffsetType &);
381 
385  Self & operator-=(const OffsetType &);
386 
389  { return m_Loop - b.m_Loop; }
390 
394  bool InBounds() const;
395 
407  bool IndexInBounds(const NeighborIndexType n, OffsetType & internalIndex, OffsetType & offset ) const;
408 
411  bool IndexInBounds(const NeighborIndexType n ) const;
412 
418  ITK_ITERATOR_VIRTUAL void OverrideBoundaryCondition(const
419  ImageBoundaryConditionPointerType i) ITK_ITERATOR_FINAL
420  { m_BoundaryCondition = i; }
421 
424  ITK_ITERATOR_VIRTUAL void ResetBoundaryCondition() ITK_ITERATOR_FINAL
425  { m_BoundaryCondition = &m_InternalBoundaryCondition; }
426 
428  void SetBoundaryCondition(const TBoundaryCondition & c)
429  { m_InternalBoundaryCondition = c; }
430 
433  { return m_BoundaryCondition; }
434 
437  {
438  this->SetNeedToUseBoundaryCondition(true);
439  }
440 
442  {
443  this->SetNeedToUseBoundaryCondition(false);
444  }
445 
447  {
448  m_NeedToUseBoundaryCondition = b;
449  }
450 
452  {
453  return m_NeedToUseBoundaryCondition;
454  }
455 
457  ITK_ITERATOR_VIRTUAL void SetRegion(const RegionType & region) ITK_ITERATOR_FINAL;
458 
459 protected:
460 
463  ITK_ITERATOR_VIRTUAL void SetLoop(const IndexType & p) ITK_ITERATOR_FINAL
464  { m_Loop = p; m_IsInBoundsValid = false; }
465 
469  ITK_ITERATOR_VIRTUAL void SetBound(const SizeType &) ITK_ITERATOR_FINAL;
470 
475  ITK_ITERATOR_VIRTUAL void SetPixelPointers(const IndexType &) ITK_ITERATOR_FINAL;
476 
479  ITK_ITERATOR_VIRTUAL void SetBeginIndex(const IndexType & start) ITK_ITERATOR_FINAL
480  { m_BeginIndex = start; }
481 
484  ITK_ITERATOR_VIRTUAL void SetEndIndex() ITK_ITERATOR_FINAL;
485 
488  IndexType m_BeginIndex;
489 
491  IndexType m_Bound;
492 
494  const InternalPixelType *m_Begin;
495 
497  typename ImageType::ConstWeakPointer m_ConstImage;
498 
500  const InternalPixelType *m_End;
501 
504  IndexType m_EndIndex;
505 
507  IndexType m_Loop;
508 
510  RegionType m_Region;
511 
517  OffsetType m_WrapOffset;
518 
524 
527  mutable bool m_InBounds[Dimension];
528 
530  mutable bool m_IsInBounds{false};
531 
535  mutable bool m_IsInBoundsValid{false};
536 
539 
542 
544  TBoundaryCondition m_InternalBoundaryCondition;
545 
547  bool m_NeedToUseBoundaryCondition{false};
548 
551 };
552 
553 template< typename TImage >
557 {
559  ret += ind;
560  return ret;
561 }
562 
563 template< typename TImage >
567 { return ( it + ind ); }
568 
569 template< typename TImage >
573  ::OffsetType & ind)
574 {
576  ret -= ind;
577  return ret;
578 }
579 } // namespace itk
580 
581 #ifndef ITK_MANUAL_INSTANTIATION
582 #include "itkConstNeighborhoodIterator.hxx"
583 #endif
584 
585 #endif
typename NeighborhoodType::NeighborIndexType NeighborIndexType
ITK_ITERATOR_VIRTUAL bool IsAtEnd() const ITK_ITERATOR_FINAL
OffsetValueType GetWrapOffset(NeighborIndexType n) const
NeighborhoodAccessorFunctorType m_NeighborhoodAccessorFunctor
ITK_ITERATOR_VIRTUAL PixelType GetPrevious(const unsigned axis) const ITK_ITERATOR_FINAL
ConstNeighborhoodIterator< TImage > operator-(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
virtual void SetDescription(const std::string &s)
ITK_ITERATOR_VIRTUAL void SetLoop(const IndexType &p) ITK_ITERATOR_FINAL
ITK_ITERATOR_VIRTUAL PixelType GetNext(const unsigned axis) const ITK_ITERATOR_FINAL
A light-weight container object for storing an N-dimensional neighborhood of values.
SizeValueType NeighborIndexType
typename AllocatorType::iterator Iterator
typename BoundaryConditionType::OutputImageType OutputImageType
ITK_ITERATOR_VIRTUAL IndexType GetIndex(NeighborIndexType i) const ITK_ITERATOR_FINAL
Const version of NeighborhoodIterator, defining iteration of a local N-dimensional neighborhood of pi...
ITK_ITERATOR_VIRTUAL void ResetBoundaryCondition() ITK_ITERATOR_FINAL
IndexType GetFastIndexPlusOffset(const OffsetType &o) const
typename AllocatorType::const_iterator ConstIterator
IndexValueType GetBound(NeighborIndexType n) const
ITK_ITERATOR_VIRTUAL PixelType GetPrevious(const unsigned axis, NeighborIndexType i) const ITK_ITERATOR_FINAL
ConstNeighborhoodIterator(const SizeType &radius, const ImageType *ptr, const RegionType &region)
ITK_ITERATOR_VIRTUAL PixelType GetPixel(const OffsetType &o) const ITK_ITERATOR_FINAL
const ImageType * GetImagePointer() const
void SetLocation(const IndexType &position)
ConstNeighborhoodIterator< TImage > operator+(const ConstNeighborhoodIterator< TImage > &it, const typename ConstNeighborhoodIterator< TImage >::OffsetType &ind)
signed long IndexValueType
Definition: itkIntTypes.h:90
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image...
Definition: itkOffset.h:67
typename ImageType::NeighborhoodAccessorFunctorType NeighborhoodAccessorFunctorType
Standard exception handling object.
static constexpr double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:53
const InternalPixelType * GetCenterPointer() const
ITK_ITERATOR_VIRTUAL PixelType GetNext(const unsigned axis, NeighborIndexType i) const ITK_ITERATOR_FINAL
Control indentation during Print() invocation.
Definition: itkIndent.h:49
ImageBoundaryConditionPointerType GetBoundaryCondition() const
void SetBoundaryCondition(const TBoundaryCondition &c)
ITK_ITERATOR_VIRTUAL IndexType GetIndex(const OffsetType &o) const ITK_ITERATOR_FINAL
ITK_ITERATOR_VIRTUAL PixelType GetPixel(const OffsetType &o, bool &IsInBounds) const ITK_ITERATOR_FINAL
ITK_ITERATOR_VIRTUAL void OverrideBoundaryCondition(const ImageBoundaryConditionPointerType i) ITK_ITERATOR_FINAL
signed long OffsetValueType
Definition: itkIntTypes.h:94
ITK_ITERATOR_VIRTUAL IndexType GetIndex() const ITK_ITERATOR_FINAL