ITK  4.2.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 <string.h>
23 #include <iostream>
24 #include "itkImage.h"
25 #include "itkNeighborhood.h"
26 #include "itkMacro.h"
28 
29 namespace itk
30 {
49 template< class TImage, class TBoundaryCondition =
50  ZeroFluxNeumannBoundaryCondition< TImage > >
51 class ITK_EXPORT ConstNeighborhoodIterator:
52  public Neighborhood< typename TImage::InternalPixelType *,
53  TImage::ImageDimension >
54 {
55 public:
57  typedef typename TImage::InternalPixelType InternalPixelType;
58  typedef typename TImage::PixelType PixelType;
59 
61  itkStaticConstMacro(Dimension, unsigned int, TImage::ImageDimension);
62 
64  typedef unsigned int DimensionValueType;
65 
68  typedef Neighborhood< InternalPixelType *,
69  itkGetStaticConstMacro(Dimension) > Superclass;
70 
73  typedef typename Superclass::RadiusType RadiusType;
74  typedef typename Superclass::SizeType SizeType;
75  typedef typename Superclass::Iterator Iterator;
77 
79  typedef TImage ImageType;
80  typedef typename TImage::RegionType RegionType;
85 
88 
92  typedef typename ImageType::NeighborhoodAccessorFunctorType
94 
96  typedef TBoundaryCondition BoundaryConditionType;
97 
102 
105 
108 
111 
115  const ImageType *ptr,
116  const RegionType & region)
117  {
118  this->Initialize(radius, ptr, region);
119  for ( unsigned int i = 0; i < Dimension; i++ )
120  { m_InBounds[i] = false; }
121  this->ResetBoundaryCondition();
122  m_NeighborhoodAccessorFunctor = ptr->GetNeighborhoodAccessor();
123  m_NeighborhoodAccessorFunctor.SetBegin( ptr->GetBufferPointer() );
124  }
126 
128  Self & operator=(const Self & orig);
129 
131  virtual void PrintSelf(std::ostream &, Indent) const;
132 
135  OffsetType ComputeInternalIndex(NeighborIndexType n) const;
136 
138  IndexType GetBound() const
139  { return m_Bound; }
140 
144  { return m_Bound[n]; }
145 
147  const InternalPixelType * GetCenterPointer() const
148  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
149 
152  PixelType GetCenterPixel() const
153  { return m_NeighborhoodAccessorFunctor.Get( this->GetCenterPointer() ); }
154 
156  const ImageType * GetImagePointer(void) const
157  { return m_ConstImage; }
158 
161  virtual IndexType GetIndex(void) const
162  { return m_Loop; }
163 
166  virtual NeighborhoodType GetNeighborhood() const;
167 
169  virtual PixelType GetPixel(NeighborIndexType i) const
170  {
171  if ( !m_NeedToUseBoundaryCondition )
172  {
173  return ( m_NeighborhoodAccessorFunctor.Get( this->operator[](i) ) );
174  }
175  bool inbounds;
176  return this->GetPixel(i, inbounds);
177  }
179 
185  virtual PixelType GetPixel(NeighborIndexType i, bool & IsInBounds) const;
186 
189  virtual PixelType GetPixel(const OffsetType & o) const
190  {
191  bool inbounds;
192 
193  return ( this->GetPixel(this->GetNeighborhoodIndex(o), inbounds) );
194  }
195 
201  virtual PixelType GetPixel(const OffsetType & o,
202  bool & IsInBounds) const
203  { return ( this->GetPixel(this->GetNeighborhoodIndex(o), IsInBounds) ); }
204 
208  virtual PixelType GetNext(const unsigned axis, NeighborIndexType i) const
209  {
210  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
211  + ( i * this->GetStride(axis) ) ) );
212  }
213 
217  virtual PixelType GetNext(const unsigned axis) const
218  {
219  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
220  + this->GetStride(axis) ) );
221  }
222 
226  virtual PixelType GetPrevious(const unsigned axis, NeighborIndexType i) const
227  {
228  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
229  - ( i * this->GetStride(axis) ) ) );
230  }
231 
235  virtual PixelType GetPrevious(const unsigned axis) const
236  {
237  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
238  - this->GetStride(axis) ) );
239  }
240 
243  virtual IndexType GetIndex(const OffsetType & o) const
244  { return ( this->GetIndex() + o ); }
245 
248  virtual IndexType GetIndex(NeighborIndexType i) const
249  { return ( this->GetIndex() + this->GetOffset(i) ); }
250 
252  RegionType GetRegion() const
253  { return m_Region; }
254 
257  IndexType GetBeginIndex() const
258  { return m_BeginIndex; }
259 
262  RegionType GetBoundingBoxAsImageRegion() const;
263 
265  OffsetType GetWrapOffset() const
266  { return m_WrapOffset; }
267 
273  OffsetValueType GetWrapOffset(NeighborIndexType n) const
274  { return m_WrapOffset[n]; }
275 
279  virtual void GoToBegin();
280 
283  virtual void GoToEnd();
284 
287  virtual void Initialize(const SizeType & radius, const ImageType *ptr,
288  const RegionType & region);
289 
292  virtual bool IsAtBegin() const
293  { return ( this->GetCenterPointer() == m_Begin ); }
294 
297  virtual bool IsAtEnd() const
298  {
299  if ( this->GetCenterPointer() > m_End )
300  {
301  ExceptionObject e(__FILE__, __LINE__);
302  std::ostringstream msg;
303  msg << "In method IsAtEnd, CenterPointer = " << this->GetCenterPointer()
304  << " is greater than End = " << m_End
305  << std::endl
306  << " " << *this;
307  e.SetDescription( msg.str().c_str() );
308  throw e;
309  }
310  return ( this->GetCenterPointer() == m_End );
311  }
313 
318  Self & operator++();
319 
324  Self & operator--();
325 
329  bool operator==(const Self & it) const
330  { return it.GetCenterPointer() == this->GetCenterPointer(); }
331 
335  bool operator!=(const Self & it) const
336  { return it.GetCenterPointer() != this->GetCenterPointer(); }
337 
341  bool operator<(const Self & it) const
342  { return this->GetCenterPointer() < it.GetCenterPointer(); }
343 
347  bool operator<=(const Self & it) const
348  { return this->GetCenterPointer() <= it.GetCenterPointer(); }
349 
353  bool operator>(const Self & it) const
354  { return this->GetCenterPointer() > it.GetCenterPointer(); }
355 
359  bool operator>=(const Self & it) const
360  { return this->GetCenterPointer() >= it.GetCenterPointer(); }
361 
366  void SetLocation(const IndexType & position)
367  {
368  this->SetLoop(position);
369  this->SetPixelPointers(position);
370  }
372 
376  Self & operator+=(const OffsetType &);
377 
381  Self & operator-=(const OffsetType &);
382 
385  { return m_Loop - b.m_Loop; }
386 
390  bool InBounds() const;
391 
403  bool IndexInBounds(NeighborIndexType n, OffsetType & internalIndex, OffsetType & offset ) const;
404 
410  virtual void OverrideBoundaryCondition(const
412  { m_BoundaryCondition = i; }
413 
416  virtual void ResetBoundaryCondition()
417  { m_BoundaryCondition = &m_InternalBoundaryCondition; }
418 
420  void SetBoundaryCondition(const TBoundaryCondition & c)
421  { m_InternalBoundaryCondition = c; }
422 
424  ImageBoundaryConditionPointerType GetBoundaryCondition() const
425  { return m_BoundaryCondition; }
426 
428  void NeedToUseBoundaryConditionOn()
429  {
430  this->SetNeedToUseBoundaryCondition(true);
431  }
432 
433  void NeedToUseBoundaryConditionOff()
434  {
435  this->SetNeedToUseBoundaryCondition(false);
436  }
437 
438  void SetNeedToUseBoundaryCondition(bool b)
439  {
440  m_NeedToUseBoundaryCondition = b;
441  }
442 
443  bool GetNeedToUseBoundaryCondition() const
444  {
445  return m_NeedToUseBoundaryCondition;
446  }
447 
448 protected:
449 
452  virtual void SetLoop(const IndexType & p)
453  { m_Loop = p; m_IsInBoundsValid = false; }
454 
458  virtual void SetBound(const SizeType &);
459 
464  virtual void SetPixelPointers(const IndexType &);
465 
468  virtual void SetBeginIndex(const IndexType & start)
469  { m_BeginIndex = start; }
470 
473  virtual void SetEndIndex();
474 
478 
481 
483  const InternalPixelType *m_Begin;
484 
486  typename ImageType::ConstWeakPointer m_ConstImage;
487 
489  const InternalPixelType *m_End;
490 
494 
497 
499  RegionType m_Region;
500 
507 
513 
516  mutable bool m_InBounds[Dimension];
517 
519  mutable bool m_IsInBounds;
520 
524  mutable bool m_IsInBoundsValid;
525 
528 
531 
533  TBoundaryCondition m_InternalBoundaryCondition;
534 
537 
540 };
541 
542 template< class TImage >
546  ::OffsetType & ind)
547 {
549  ret = it;
550  ret += ind;
551  return ret;
552 }
553 
554 template< class TImage >
555 inline ConstNeighborhoodIterator< TImage >
557  ::OffsetType & ind,
559 { return ( it + ind ); }
560 
561 template< class TImage >
562 inline ConstNeighborhoodIterator< TImage >
565  ::OffsetType & ind)
566 {
568  ret = it;
569  ret -= ind;
570  return ret;
571 }
572 } // namespace itk
573 
574 // Define instantiation macro for this template.
575 #define ITK_TEMPLATE_ConstNeighborhoodIterator(_, EXPORT, TypeX, TypeY) \
576  namespace itk \
577  { \
578  _( 2 ( class EXPORT ConstNeighborhoodIterator< ITK_TEMPLATE_2 TypeX > ) ) \
579  namespace Templates \
580  { \
581  typedef ConstNeighborhoodIterator< ITK_TEMPLATE_2 TypeX > \
582  ConstNeighborhoodIterator##TypeY; \
583  } \
584  }
585 
586 #if ITK_TEMPLATE_EXPLICIT
587 #include "Templates/itkConstNeighborhoodIterator+-.h"
588 #endif
589 
590 #if ITK_TEMPLATE_TXX
591 #include "itkConstNeighborhoodIterator.hxx"
592 #endif
593 
594 /*
595 #ifndef ITK_MANUAL_INSTANTIATION
596 #include "itkConstNeighborhoodIterator.hxx"
597 #endif
598 */
599 #endif
600